evm: change log tx index to index in block (#354)

Closes #334
This commit is contained in:
yihuang 2021-07-26 16:40:59 +08:00 committed by GitHub
parent 2828fa1e62
commit 54581269b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 9 deletions

View File

@ -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

View File

@ -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
// ----------------------------------------------------------------------------

View File

@ -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)

View File

@ -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
@ -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])
})
}
}

View File

@ -43,6 +43,7 @@ const (
prefixTransientAccessListAddress
prefixTransientAccessListSlot
prefixTransientTxHash
prefixTransientLogSize
)
// KVStore key prefixes
@ -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

View File

@ -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,
}