Skip to content

Commit

Permalink
chore: cosmetic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Oct 3, 2024
1 parent 7acd795 commit 2cc64b8
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 22 deletions.
7 changes: 5 additions & 2 deletions app/evmante/evmante_emit_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ func (eeed EthEmitEventDecorator) AnteHandle(
msg, (*evm.MsgEthereumTx)(nil),
)
}
// Untyped event: "message", used for tendermint subscription
// Untyped event "pending_ethereum_tx" is emitted for then indexing purposes.
// Tendermint tx_search can only search the untyped events.
// TxHash and TxIndex values are exposed in the ante handler (before the actual tx execution)
// to allow searching for txs which are failed due to "out of block gas limit" error.
ctx.EventManager().EmitEvent(
sdk.NewEvent(
evm.PendingEthereumTxEvent,
sdk.NewAttribute(evm.PendingEthereumTxEventAttrEthHash, msgEthTx.Hash),
sdk.NewAttribute(evm.PendingEthereumTxEventTxAttrIndex, strconv.FormatUint(txIndex+uint64(i), 10)),
sdk.NewAttribute(evm.PendingEthereumTxEventAttrIndex, strconv.FormatUint(txIndex+uint64(i), 10)),
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion app/evmante/evmante_emit_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *TestSuite) TestEthEmitEventDecorator() {
s.Require().Equal(txMsg.Hash, attr.Value)

// TX index attr must present
attr, ok = event.GetAttribute(evm.PendingEthereumTxEventTxAttrIndex)
attr, ok = event.GetAttribute(evm.PendingEthereumTxEventAttrIndex)
s.Require().True(ok, "tx index attribute not found")
s.Require().Equal("0", attr.Value)
})
Expand Down
4 changes: 2 additions & 2 deletions eth/indexer/evm_tx_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestEVMTxIndexer(t *testing.T) {
Type: evm.PendingEthereumTxEvent,
Attributes: []abci.EventAttribute{
{Key: evm.PendingEthereumTxEventAttrEthHash, Value: txHash.Hex()},
{Key: evm.PendingEthereumTxEventTxAttrIndex, Value: "0"},
{Key: evm.PendingEthereumTxEventAttrIndex, Value: "0"},
},
},
},
Expand All @@ -99,7 +99,7 @@ func TestEVMTxIndexer(t *testing.T) {
Type: evm.PendingEthereumTxEvent,
Attributes: []abci.EventAttribute{
{Key: evm.PendingEthereumTxEventAttrEthHash, Value: txHash.Hex()},
{Key: evm.PendingEthereumTxEventTxAttrIndex, Value: "0"},
{Key: evm.PendingEthereumTxEventAttrIndex, Value: "0"},
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func generateStorageKey(key gethcommon.Address, slot uint64) string {
// Concatenate key and slot
data := append(keyBytes, slotBytes...)

// EthHash the data using Keccak256
// Compute the data hash using Keccak256
hash := sha3.NewLegacyKeccak256()
hash.Write(data)
return gethcommon.BytesToHash(hash.Sum(nil)).Hex()
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (b *Backend) BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (gethcore.
}
return gethcore.BytesToBloom(hexutils.HexToBytes(blockBloomEvent.Bloom)), nil
}
return gethcore.Bloom{}, errors.New("block bloom event is not found")
return gethcore.Bloom{}, errors.New(msgType + " not found in end block results")

Check warning on line 342 in eth/rpc/backend/blocks.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/blocks.go#L342

Added line #L342 was not covered by tests
}

// RPCBlockFromTendermintBlock returns a JSON-RPC compatible Ethereum block from a
Expand Down
5 changes: 3 additions & 2 deletions eth/rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ func (b *Backend) GetTxByTxIndex(height int64, index uint) (*eth.TxResult, error

// fallback to tendermint tx evmTxIndexer
query := fmt.Sprintf("tx.height=%d AND %s.%s=%d",
height, evm.PendingEthereumTxEventTxAttrIndex,
evm.PendingEthereumTxEventTxAttrIndex,
height,
evm.PendingEthereumTxEvent,
evm.PendingEthereumTxEventAttrIndex,
index,

Check warning on line 332 in eth/rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/tx_info.go#L329-L332

Added lines #L329 - L332 were not covered by tests
)
txResult, err := b.queryTendermintTxIndexer(query, func(txs *rpc.ParsedTxs) *rpc.ParsedTx {
Expand Down
6 changes: 3 additions & 3 deletions eth/rpc/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (b *Backend) retrieveEVMTxFeesFromBlock(
func AllTxLogsFromEvents(events []abci.Event) ([][]*gethcore.Log, error) {
allLogs := make([][]*gethcore.Log, 0, 4)
for _, event := range events {
if event.Type != proto.MessageName((*evm.EventTxLog)(nil)) {
if event.Type != proto.MessageName(new(evm.EventTxLog)) {
continue
}

Expand All @@ -231,7 +231,7 @@ func AllTxLogsFromEvents(events []abci.Event) ([][]*gethcore.Log, error) {
// TxLogsFromEvents parses ethereum logs from cosmos events for specific msg index
func TxLogsFromEvents(events []abci.Event, msgIndex int) ([]*gethcore.Log, error) {
for _, event := range events {
if event.Type != proto.MessageName((*evm.EventTxLog)(nil)) {
if event.Type != proto.MessageName(new(evm.EventTxLog)) {
continue
}

Expand All @@ -256,7 +256,7 @@ func ParseTxLogsFromEvent(event abci.Event) ([]*gethcore.Log, error) {
for _, logString := range eventTxLog.TxLogs {
var evmLog evm.Log
if err = json.Unmarshal([]byte(logString), &evmLog); err != nil {
return nil, errors.Wrapf(err, "failed unmarshal event tx log")
return nil, errors.Wrapf(err, "failed to unmarshal event tx log")

Check warning on line 259 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L259

Added line #L259 was not covered by tests
}
evmLogs = append(evmLogs, &evmLog)
}
Expand Down
13 changes: 10 additions & 3 deletions eth/rpc/events_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func ParseTxResult(result *abci.ResponseDeliverTx, tx sdk.Tx) (*ParsedTxs, error
}
ethTxIndexFromEvent, err := strconv.ParseUint(eventEthereumTx.Index, 10, 31)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse EthTxIndex from event: %w", err)

Check warning on line 76 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L76

Added line #L76 was not covered by tests
}
gasUsed, err := strconv.ParseUint(eventEthereumTx.GasUsed, 10, 64)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse GasUsed from event: %w", err)

Check warning on line 80 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L80

Added line #L80 was not covered by tests
}
committedTx := ParsedTx{
MsgIndex: msgIndex,
Expand All @@ -102,7 +102,11 @@ func ParseTxResult(result *abci.ResponseDeliverTx, tx sdk.Tx) (*ParsedTxs, error
parsedTxs.Txs[i].Failed = true

Check warning on line 102 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L101-L102

Added lines #L101 - L102 were not covered by tests

// replace gasUsed with gasLimit because that's what's actually deducted.
gasLimit := tx.GetMsgs()[i].(*evm.MsgEthereumTx).GetGas()
msgEthereumTx, ok := tx.GetMsgs()[i].(*evm.MsgEthereumTx)
if !ok {
return nil, fmt.Errorf("unexpected message type at index %d", i)

Check warning on line 107 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L105-L107

Added lines #L105 - L107 were not covered by tests
}
gasLimit := msgEthereumTx.GetGas()
parsedTxs.Txs[i].GasUsed = gasLimit

Check warning on line 110 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L109-L110

Added lines #L109 - L110 were not covered by tests
}
}
Expand Down Expand Up @@ -162,6 +166,9 @@ func (p *ParsedTxs) GetTxByTxIndex(txIndex int) *ParsedTx {

// AccumulativeGasUsed calculates the accumulated gas used within the batch of txs
func (p *ParsedTxs) AccumulativeGasUsed(msgIndex int) (result uint64) {
if msgIndex < 0 || msgIndex >= len(p.Txs) {
return 0

Check warning on line 170 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L168-L170

Added lines #L168 - L170 were not covered by tests
}
for i := 0; i <= msgIndex; i++ {
result += p.Txs[i].GasUsed

Check warning on line 173 in eth/rpc/events_parser.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/events_parser.go#L172-L173

Added lines #L172 - L173 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion eth/rpc/events_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func pendingEthereumTxEvent(txHash string, txIndex int) abci.Event {
Type: evm.PendingEthereumTxEvent,
Attributes: []abci.EventAttribute{
{Key: evm.PendingEthereumTxEventAttrEthHash, Value: txHash},
{Key: evm.PendingEthereumTxEventTxAttrIndex, Value: strconv.Itoa(txIndex)},
{Key: evm.PendingEthereumTxEventAttrIndex, Value: strconv.Itoa(txIndex)},
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions x/evm/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
// Used in non-typed event "pending_ethereum_tx"
PendingEthereumTxEvent = "pending_ethereum_tx"
PendingEthereumTxEventAttrEthHash = "eth_hash"
PendingEthereumTxEventTxAttrIndex = "index"
PendingEthereumTxEventAttrIndex = "index"
)

func EventTxLogFromABCIEvent(event abci.Event) (*EventTxLog, error) {
Expand Down Expand Up @@ -86,7 +86,7 @@ func GetEthHashAndIndexFromPendingEthereumTxEvent(event abci.Event) (gethcommon.
if attr.Key == PendingEthereumTxEventAttrEthHash {
ethHash = gethcommon.HexToHash(attr.Value)

Check warning on line 87 in x/evm/events.go

View check run for this annotation

Codecov / codecov/patch

x/evm/events.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
if attr.Key == PendingEthereumTxEventTxAttrIndex {
if attr.Key == PendingEthereumTxEventAttrIndex {
parsedIndex, err := strconv.ParseInt(attr.Value, 10, 32)
if err != nil {
return ethHash, -1, fmt.Errorf(
Expand Down
8 changes: 6 additions & 2 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ func (k *Keeper) ApplyEvmTx(
}

// refund gas in order to match the Ethereum gas consumption instead of the default SDK one.
if err = k.RefundGas(ctx, msg, msg.Gas()-evmResp.GasUsed, evmConfig.Params.EvmDenom); err != nil {
return nil, errors.Wrapf(err, "failed to refund gas leftover gas to sender %s", msg.From())
refundGas := uint64(0)
if msg.Gas() > evmResp.GasUsed {
refundGas = msg.Gas() - evmResp.GasUsed
}
if err = k.RefundGas(ctx, msg, refundGas, evmConfig.Params.EvmDenom); err != nil {
return nil, errors.Wrapf(err, "failed to refund leftover gas to sender %s", msg.From())

Check warning on line 115 in x/evm/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/msg_server.go#L115

Added line #L115 was not covered by tests
}

if len(receipt.Logs) > 0 {
Expand Down
3 changes: 1 addition & 2 deletions x/evm/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ func (msg *MsgEthereumTx) FromEthereumTx(tx *gethcore.Transaction) error {
// Route returns the route value of an MsgEthereumTx.
func (msg MsgEthereumTx) Route() string { return RouterKey }

// Type returns the type value of an MsgEthereumTx.
//func (msg MsgEthereumTx) Type() string { return TypeMsgEthereumTx }
func (msg MsgEthereumTx) Type() string { return proto.MessageName(new(MsgEthereumTx)) }

Check warning on line 144 in x/evm/msg.go

View check run for this annotation

Codecov / codecov/patch

x/evm/msg.go#L144

Added line #L144 was not covered by tests

// ValidateBasic implements the sdk.Msg interface. It performs basic validation
// checks of a Transaction. If returns an error if validation fails.
Expand Down

0 comments on commit 2cc64b8

Please sign in to comment.