Skip to content

Commit

Permalink
chore: stopping point
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Sep 12, 2024
1 parent 1df1e9d commit 2f8b1e4
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 80 deletions.
10 changes: 6 additions & 4 deletions eth/rpc/backend/call_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ func (b *Backend) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
}

txHash := ethereumTx.AsTransaction().Hash()

syncCtx := b.clientCtx.WithBroadcastMode(flags.BroadcastSync)
rsp, err := syncCtx.BroadcastTx(txBytes)
b.logger.Debug("eth_sendRawTransaction",
"txHash", txHash.Hex(),
"blockHeight", fmt.Sprintf("%d", rsp.Height),
)

syncCtx := b.clientCtx.WithBroadcastMode(flags.BroadcastSync)
rsp, err := syncCtx.BroadcastTx(txBytes)
if rsp != nil && rsp.Code != 0 {
err = errorsmod.ABCIError(rsp.Codespace, rsp.Code, rsp.RawLog)
}
if err != nil {
b.logger.Error("failed to broadcast tx", "error", err.Error())
return txHash, err
}
b.logger.Debug("eth_sendRawTransaction",
"blockHeight", fmt.Sprintf("%d", rsp.Height),
)

return txHash, nil
}
Expand Down
67 changes: 2 additions & 65 deletions eth/rpc/rpcapi/eth_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (s *NodeSuite) Test_SimpleTransferTransaction() {
txReceipt, err := s.ethClient.TransactionReceipt(blankCtx, tx.Hash())
s.NoError(err)

s.T().Log("Assert event expectations")
s.T().Log("Assert event expectations - successful eth tx")
{
blockHeightOfTx := int64(txReceipt.BlockNumber.Uint64())
blockOfTx, err := s.val.RPCClient.BlockResults(blankCtx, &blockHeightOfTx)
Expand All @@ -282,80 +282,17 @@ func (s *NodeSuite) Test_SimpleTransferTransaction() {
}
}

// TODO: UD-DEBUG: REMOVE
for eventIdx, event := range blockOfTx.TxsResults[0].Events {
eventJson, _ := json.Marshal(event)
fmt.Printf("Events[%d]: %s\n", eventIdx, string(eventJson))
}

eventsJson, _ := json.Marshal(events)
s.Require().Equal(len(ethTxEvents), 2, "events: ", eventsJson)
hash0, _ := ethTxEvents[0].GetAttribute(evm.AttributeKeyEthereumTxHash)
hash1, _ := ethTxEvents[1].GetAttribute(evm.AttributeKeyEthereumTxHash)
s.Require().Equal(hash0, hash1)
}

s.T().Log("Assert balances")
senderBalanceAfterWei, err := s.ethClient.BalanceAt(context.Background(), s.fundedAccEthAddr, nil)
s.NoError(err)

// TODO: UD-DEBUG: REMOVE --------------- Fail TX on purpose
weiToSend = new(big.Int).Add(senderBalanceAfterWei, evm.NativeToWei(big.NewInt(5)))
tx, err = gethcore.SignNewTx(
s.fundedAccPrivateKey,
signer,
&gethcore.LegacyTx{
Nonce: nonce,
To: &recipientAddr,
Value: weiToSend,
Gas: params.TxGas,
GasPrice: gasPrice, // 1 micronibi per gas
})
s.NoError(err)
// err = s.ethClient.SendTransaction(context.Background(), tx)
txBz, err := tx.MarshalBinary()
s.NoError(err)
_, err = s.ethAPI.SendRawTransaction(txBz)
// txHash, err := s.ethAPI.SendRawTransaction(txBz)
s.Require().Error(err) // Expect failure

s.T().Log("You do not get a receipt for failed transactions")
txReceipt, err = s.ethClient.TransactionReceipt(blankCtx, tx.Hash())
s.Error(err)

// go back changing the requested block height until it has the failed tx.

s.T().Log("Assert event expectations")
{
latestHeight, _ := s.network.LatestHeight()
blockHeightOfTx := int64(latestHeight)
// blockHeightOfTx := int64(txReceipt.BlockNumber.Uint64())
blockOfTx, err := s.val.RPCClient.BlockResults(blankCtx, &blockHeightOfTx)
s.NoError(err)
ethTxEvents := []sdk.Event{}
events := blockOfTx.TxsResults[0].Events
for _, event := range events {
if event.Type == "ethereum_tx" {
ethTxEvents = append(ethTxEvents,
sdk.Event{Type: event.Type, Attributes: event.Attributes},
)
}
}

// TODO: UD-DEBUG: REMOVE
for eventIdx, event := range blockOfTx.TxsResults[0].Events {
eventJson, _ := json.Marshal(event)
fmt.Printf("Events[%d]: %s\n", eventIdx, string(eventJson))
}

eventsJson, _ := json.Marshal(events)
s.Require().Equal(len(ethTxEvents), 2, "events: ", eventsJson)
hash0, _ := ethTxEvents[0].GetAttribute(evm.AttributeKeyEthereumTxHash)
hash1, _ := ethTxEvents[1].GetAttribute(evm.AttributeKeyEthereumTxHash)
s.Require().Equal(hash0, hash1)
}

s.Require().False(true)

costOfTx := new(big.Int).Add(
weiToSend,
new(big.Int).Mul((new(big.Int).SetUint64(params.TxGas)), gasPrice),
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/rpcapi/event_subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *Suite) TestParseBloomFromEvents() {
err := deps.Ctx.EventManager().EmitTypedEvents(
&evm.EventTransfer{},
&evm.EventBlockBloom{
Bloom: eth.BloomToString(bloom),
Bloom: eth.BloomToHex(bloom),
},
)
s.NoError(err, "emitting bloom event failed")
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/rpcapi/filter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func ParseBloomFromEvents(events []abci.Event) (bloom gethcore.Bloom, err error)
err, "failed to parse event of type %s", bloomEventType)
}

return eth.BloomFromString(bloomEvent.Bloom)
return eth.BloomFromHex(bloomEvent.Bloom)
}
return bloom, err
}
11 changes: 4 additions & 7 deletions eth/stringify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
fmt "fmt"

gethcommon "github.com/ethereum/go-ethereum/common"
// "github.com/ethereum/go-ethereum/common/hexutil"
gethcore "github.com/ethereum/go-ethereum/core/types"
)

Expand All @@ -21,15 +20,13 @@ func EthTxHashToString(hash gethcommon.Hash) string {
return hash.Hex()
}

// TODO: test with real bloom
// BloomToString returns bloom filter as a string.
func BloomToString(bloom gethcore.Bloom) string {
// BloomToHex returns the bloom filter as a string.
func BloomToHex(bloom gethcore.Bloom) string {
return BytesToHex(bloom.Bytes())
}

// TODO: test with real bloom
// BloomFromString returns bloom filter as a string.
func BloomFromString(bloomHex string) (gethcore.Bloom, error) {
// BloomFromHex converts a hex-encoded bloom filter to a gethcore.Bloom.
func BloomFromHex(bloomHex string) (gethcore.Bloom, error) {
bloomBz, err := hex.DecodeString(bloomHex)
if err != nil {
return gethcore.Bloom{}, fmt.Errorf("could not construct bloom: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion eth/stringify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (s *Suite) TestStringify() {
gethcore.BytesToBloom(gethcommon.Big1.Bytes()),
}
for tcIdx, bloom := range testCases {
gotStr := eth.BloomToString(bloom)
gotStr := eth.BloomToHex(bloom)
gotBloom, err := eth.BloomFromHex(gotStr)
s.NoError(err)
s.Equalf(bloom, gotBloom, "test case: %d", tcIdx)
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
bloom := gethcoretypes.BytesToBloom(k.EvmState.GetBlockBloomTransient(ctx).Bytes())
_ = ctx.EventManager().EmitTypedEvent(&evm.EventBlockBloom{
Bloom: eth.BloomToString(bloom),
Bloom: eth.BloomToHex(bloom),
})
// The bloom logic doesn't update the validator set.
return []abci.ValidatorUpdate{}
Expand Down

0 comments on commit 2f8b1e4

Please sign in to comment.