Skip to content

Commit 2440987

Browse files
committed
add fallback
* align patch * cleanup
1 parent f5c9459 commit 2440987

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [\#774](https://github.com/cosmos/evm/pull/774) Emit proper allowance amount in erc20 event.
2626
- [\#790](https://github.com/cosmos/evm/pull/790) fix panic in historical query due to missing EvmCoinInfo.
2727
- [\#800](https://github.com/cosmos/evm/pull/800) Fix denom exponent validation in virtual fee deduct in vm module.
28+
- [\#812](https://github.com/cosmos/evm/pull/812) Patch evm tx index and log indexes, cleanup EmitTxHashEvent and ResetTransientGasUsed.
2829

2930
## v0.5.0
3031

rpc/backend/comet_to_eth.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package backend
22

33
import (
44
"fmt"
5+
"math"
56
"math/big"
67

78
"github.com/ethereum/go-ethereum/common"
@@ -273,6 +274,20 @@ func (b *Backend) ReceiptsFromCometBlock(
273274
return nil, fmt.Errorf("failed to convert tx result to eth receipt: %w", err)
274275
}
275276

277+
if txResult.EthTxIndex == -1 {
278+
// Fallback to find tx index by iterating all valid eth transactions
279+
msgs := b.EthMsgsFromCometBlock(resBlock, blockRes)
280+
for i := range msgs {
281+
if msgs[i].Hash() == ethMsg.Hash() {
282+
if i > math.MaxInt32 {
283+
return nil, errors.New("tx index overflow")
284+
}
285+
txResult.EthTxIndex = int32(i) //#nosec G115 -- checked for int overflow already
286+
break
287+
}
288+
}
289+
}
290+
276291
bloom := ethtypes.CreateBloom(&ethtypes.Receipt{Logs: logs})
277292

278293
receipt := &ethtypes.Receipt{

x/vm/keeper/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress) (*s
3838
// TxConfig loads `TxConfig` from current transient storage
3939
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) statedb.TxConfig {
4040
return statedb.NewTxConfig(
41-
txHash, // TxHash
42-
uint(ctx.TxIndex()), //#nosec G115 // TxIndex
41+
txHash, // TxHash
42+
0,
4343
)
4444
}
4545

x/vm/keeper/state_transition.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
244244
GasUsed: res.GasUsed,
245245
BlockHash: common.BytesToHash(ctx.HeaderHash()),
246246
BlockNumber: big.NewInt(ctx.BlockHeight()),
247-
TransactionIndex: uint(ctx.TxIndex()), //#nosec G115
248247
}
249248

250249
if res.Failed() {

x/vm/types/response.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ func PatchTxResponses(input []*abci.ExecTxResult) []*abci.ExecTxResult {
2828
panic(err)
2929
}
3030

31-
var dataDirty bool
32-
ethTxHashes := make(map[string]uint64)
33-
31+
var (
32+
anteEvents []abci.Event
33+
// if the response data is modified and need to be marshaled back
34+
dataDirty bool
35+
)
3436
for i, rsp := range txMsgData.MsgResponses {
3537
var response MsgEthereumTxResponse
3638
if rsp.TypeUrl != "/"+proto.MessageName(&response) {
@@ -41,7 +43,13 @@ func PatchTxResponses(input []*abci.ExecTxResult) []*abci.ExecTxResult {
4143
panic(err)
4244
}
4345

44-
ethTxHashes[response.Hash] = txIndex
46+
anteEvents = append(anteEvents, abci.Event{
47+
Type: EventTypeEthereumTx,
48+
Attributes: []abci.EventAttribute{
49+
{Key: AttributeKeyEthereumTxHash, Value: response.Hash},
50+
{Key: AttributeKeyTxIndex, Value: strconv.FormatUint(txIndex, 10)},
51+
},
52+
})
4553

4654
if len(response.Logs) > 0 {
4755
for _, log := range response.Logs {
@@ -62,32 +70,21 @@ func PatchTxResponses(input []*abci.ExecTxResult) []*abci.ExecTxResult {
6270
txIndex++
6371
}
6472

65-
for i := range res.Events {
66-
if res.Events[i].Type == EventTypeEthereumTx {
67-
var txHash string
68-
for _, attr := range res.Events[i].Attributes {
69-
if attr.Key == AttributeKeyEthereumTxHash {
70-
txHash = attr.Value
71-
break
72-
}
73-
}
73+
if len(anteEvents) > 0 {
74+
// prepend ante events in front to emulate the side effect of `EthEmitEventDecorator`
75+
events := make([]abci.Event, len(anteEvents)+len(res.Events))
76+
copy(events, anteEvents)
77+
copy(events[len(anteEvents):], res.Events)
78+
res.Events = events
7479

75-
if idx, ok := ethTxHashes[txHash]; ok {
76-
res.Events[i].Attributes = append(res.Events[i].Attributes, abci.EventAttribute{
77-
Key: AttributeKeyTxIndex,
78-
Value: strconv.FormatUint(idx, 10),
79-
})
80+
if dataDirty {
81+
data, err := proto.Marshal(&txMsgData)
82+
if err != nil {
83+
panic(err)
8084
}
81-
}
82-
}
8385

84-
if dataDirty {
85-
data, err := proto.Marshal(&txMsgData)
86-
if err != nil {
87-
panic(err)
86+
res.Data = data
8887
}
89-
90-
res.Data = data
9188
}
9289
}
9390
return input

0 commit comments

Comments
 (0)