Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
evm: change log tx index to index in block (#354)
Browse files Browse the repository at this point in the history
Closes #334
  • Loading branch information
yihuang committed Jul 26, 2021
1 parent 2828fa1 commit 5458126
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (evm) [tharsis#276](https://github.com/tharsis/ethermint/pull/276) Vm errors don't result in cosmos tx failure, just
different tx state and events.
* (evm) [tharsis#342](https://github.com/tharsis/ethermint/issues/342) Don't clear balance when resetting the account.
* (evm) [tharsis#334](https://github.com/tharsis/ethermint/pull/334) Log index changed to the index in block rather than
tx.

### API Breaking

Expand Down
19 changes: 19 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ func (k Keeper) DeleteTxLogs(ctx sdk.Context, txHash common.Hash) {
store.Delete(txHash.Bytes())
}

// GetLogSizeTransient returns EVM log index on the current block.
func (k Keeper) GetLogSizeTransient() uint64 {
store := k.ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientLogSize)
if len(bz) == 0 {
return 0
}

return sdk.BigEndianToUint64(bz)
}

// IncreaseLogSizeTransient fetches the current EVM log index from the transient store, increases its
// value by one and then sets the new index back to the transient store.
func (k Keeper) IncreaseLogSizeTransient() {
logSize := k.GetLogSizeTransient()
store := k.ctx.TransientStore(k.transientKey)
store.Set(types.KeyPrefixTransientLogSize, sdk.Uint64ToBigEndian(logSize+1))
}

// ----------------------------------------------------------------------------
// Storage
// ----------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,10 @@ func (k *Keeper) AddLog(log *ethtypes.Log) {
log.TxIndex = uint(k.GetTxIndexTransient())
log.TxHash = k.GetTxHashTransient()

logs := k.GetTxLogs(log.TxHash)
log.Index = uint(k.GetLogSizeTransient())
k.IncreaseLogSizeTransient()

log.Index = uint(len(logs))
logs := k.GetTxLogs(log.TxHash)
logs = append(logs, log)

k.SetLogs(log.TxHash, logs)
Expand Down
38 changes: 31 additions & 7 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ func (suite *KeeperTestSuite) TestAddLog() {
msg, _ = tx.GetMsgs()[0].(*evmtypes.MsgEthereumTx)
txHash := msg.AsTransaction().Hash()

msg2 := types.NewTx(big.NewInt(1), 1, &suite.address, big.NewInt(1), 100000, big.NewInt(1), []byte("test"), nil)
msg2.From = addr.Hex()

tx2 := suite.CreateTestTx(msg2, privKey)
msg2, _ = tx2.GetMsgs()[0].(*evmtypes.MsgEthereumTx)
txHash2 := msg2.AsTransaction().Hash()

testCases := []struct {
name string
hash common.Hash
Expand All @@ -503,21 +510,38 @@ func (suite *KeeperTestSuite) TestAddLog() {
},
func() {},
},
{
"log index keep increasing in new tx",
txHash2,
&ethtypes.Log{
Address: addr,
},
&ethtypes.Log{
Address: addr,
TxHash: txHash2,
TxIndex: 1,
Index: 1,
},
func() {
suite.app.EvmKeeper.SetTxHashTransient(txHash)
suite.app.EvmKeeper.AddLog(&ethtypes.Log{
Address: addr,
})
suite.app.EvmKeeper.IncreaseTxIndexTransient()
},
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
tc.malleate()

prev := suite.app.EvmKeeper.GetTxLogs(tc.hash)
suite.app.EvmKeeper.SetTxHashTransient(tc.hash)
suite.app.EvmKeeper.AddLog(tc.log)
post := suite.app.EvmKeeper.GetTxLogs(tc.hash)

suite.Require().NotZero(len(post), tc.hash.Hex())
suite.Require().Equal(len(prev)+1, len(post))
suite.Require().NotNil(post[len(post)-1])
suite.Require().Equal(tc.log, post[len(post)-1])
logs := suite.app.EvmKeeper.GetTxLogs(tc.hash)
suite.Require().Equal(1, len(logs))
suite.Require().Equal(tc.expLog, logs[0])
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions x/evm/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
prefixTransientAccessListAddress
prefixTransientAccessListSlot
prefixTransientTxHash
prefixTransientLogSize
)

// KVStore key prefixes
Expand All @@ -65,6 +66,7 @@ var (
KeyPrefixTransientAccessListAddress = []byte{prefixTransientAccessListAddress}
KeyPrefixTransientAccessListSlot = []byte{prefixTransientAccessListSlot}
KeyPrefixTransientTxHash = []byte{prefixTransientTxHash}
KeyPrefixTransientLogSize = []byte{prefixTransientLogSize}
)

// BloomKey defines the store key for a block Bloom
Expand Down
1 change: 1 addition & 0 deletions x/evm/types/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func NewLogFromEth(log *ethtypes.Log) *Log {
BlockNumber: log.BlockNumber,
TxHash: log.TxHash.String(),
TxIndex: uint64(log.TxIndex),
Index: uint64(log.Index),
BlockHash: log.BlockHash.String(),
Removed: log.Removed,
}
Expand Down

0 comments on commit 5458126

Please sign in to comment.