fix: empty log topics shouldn't be encoded as nil (#840)

* Problem: empty topics shouldn't be encoded as nil

Closes: #839

Solution:
- encode it as empty array

* fix unit tests

* changelog
This commit is contained in:
yihuang 2021-12-14 22:52:22 +08:00 committed by GitHub
parent 423944bf79
commit 924232f02e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
## Unreleased ## Unreleased
### State Machine Breaking
- (evm) [tharsis#840](https://github.com/tharsis/ethermint/pull/840) Store empty topics as empty array rather than nil.
### Improvements ### Improvements
* (evm) [tharsis#826](https://github.com/tharsis/ethermint/issues/826) Improve allocation of bytes of `tx.To` address. * (evm) [tharsis#826](https://github.com/tharsis/ethermint/issues/826) Improve allocation of bytes of `tx.To` address.

View File

@ -592,6 +592,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
&ethtypes.Log{ &ethtypes.Log{
Address: addr, Address: addr,
TxHash: txHash, TxHash: txHash,
Topics: make([]common.Hash, 0),
}, },
func() {}, func() {},
}, },
@ -606,6 +607,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
TxHash: txHash2, TxHash: txHash2,
TxIndex: 1, TxIndex: 1,
Index: 1, Index: 1,
Topics: make([]common.Hash, 0),
}, },
func() { func() {
suite.app.EvmKeeper.SetTxHashTransient(txHash) suite.app.EvmKeeper.SetTxHashTransient(txHash)
@ -624,6 +626,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
&ethtypes.Log{ &ethtypes.Log{
Address: addr, Address: addr,
TxHash: txHash3, TxHash: txHash3,
Topics: make([]common.Hash, 0),
}, },
func() {}, func() {},
}, },
@ -638,6 +641,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
TxHash: txHash4, TxHash: txHash4,
TxIndex: 1, TxIndex: 1,
Index: 1, Index: 1,
Topics: make([]common.Hash, 0),
}, },
func() { func() {
suite.app.EvmKeeper.SetTxHashTransient(txHash) suite.app.EvmKeeper.SetTxHashTransient(txHash)

View File

@ -70,9 +70,9 @@ func (log *Log) Validate() error {
// ToEthereum returns the Ethereum type Log from a Ethermint proto compatible Log. // ToEthereum returns the Ethereum type Log from a Ethermint proto compatible Log.
func (log *Log) ToEthereum() *ethtypes.Log { func (log *Log) ToEthereum() *ethtypes.Log {
var topics []common.Hash // nolint: prealloc topics := make([]common.Hash, len(log.Topics))
for i := range log.Topics { for i, topic := range log.Topics {
topics = append(topics, common.HexToHash(log.Topics[i])) topics[i] = common.HexToHash(topic)
} }
return &ethtypes.Log{ return &ethtypes.Log{
@ -108,9 +108,9 @@ func LogsToEthereum(logs []*Log) []*ethtypes.Log {
// NewLogFromEth creates a new Log instance from a Ethereum type Log. // NewLogFromEth creates a new Log instance from a Ethereum type Log.
func NewLogFromEth(log *ethtypes.Log) *Log { func NewLogFromEth(log *ethtypes.Log) *Log {
var topics []string // nolint: prealloc topics := make([]string, len(log.Topics))
for _, topic := range log.Topics { for i, topic := range log.Topics {
topics = append(topics, topic.String()) topics[i] = topic.String()
} }
return &Log{ return &Log{