laconicd/x/evm/types/logs_test.go

166 lines
3.4 KiB
Go
Raw Normal View History

package types
import (
2021-04-18 15:54:18 +00:00
"testing"
"github.com/stretchr/testify/require"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
2021-04-18 15:54:18 +00:00
ethcmn "github.com/ethereum/go-ethereum/common"
2021-04-18 15:54:18 +00:00
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
2021-04-18 15:54:18 +00:00
func TestTransactionLogsValidate(t *testing.T) {
priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey).String()
testCases := []struct {
name string
txLogs TransactionLogs
expPass bool
}{
{
"valid log",
TransactionLogs{
2021-04-18 15:54:18 +00:00
Hash: ethcmn.BytesToHash([]byte("tx_hash")).String(),
2021-04-17 10:00:07 +00:00
Logs: []*Log{
{
2021-04-18 15:54:18 +00:00
Address: addr,
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
Data: []byte("data"),
BlockNumber: 1,
2021-04-18 15:54:18 +00:00
TxHash: ethcmn.BytesToHash([]byte("tx_hash")).String(),
TxIndex: 1,
2021-04-18 15:54:18 +00:00
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
Index: 1,
Removed: false,
},
},
},
true,
},
{
"empty hash",
TransactionLogs{
Hash: ethcmn.Hash{}.String(),
},
false,
},
{
"invalid log",
TransactionLogs{
2021-04-18 15:54:18 +00:00
Hash: ethcmn.BytesToHash([]byte("tx_hash")).String(),
Logs: []*Log{{}},
},
false,
},
{
"hash mismatch log",
TransactionLogs{
2021-04-18 15:54:18 +00:00
Hash: ethcmn.BytesToHash([]byte("tx_hash")).String(),
2021-04-17 10:00:07 +00:00
Logs: []*Log{
{
2021-04-18 15:54:18 +00:00
Address: addr,
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
Data: []byte("data"),
BlockNumber: 1,
2021-04-17 10:00:07 +00:00
TxHash: ethcmn.BytesToHash([]byte("other_hash")).String(),
TxIndex: 1,
2021-04-18 15:54:18 +00:00
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
Index: 1,
Removed: false,
},
},
},
false,
},
}
for _, tc := range testCases {
tc := tc
err := tc.txLogs.Validate()
if tc.expPass {
2021-04-18 15:54:18 +00:00
require.NoError(t, err, tc.name)
} else {
2021-04-18 15:54:18 +00:00
require.Error(t, err, tc.name)
}
}
}
2021-04-18 15:54:18 +00:00
func TestValidateLog(t *testing.T) {
priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey).String()
testCases := []struct {
name string
2021-04-17 10:00:07 +00:00
log *Log
expPass bool
}{
{
"valid log",
2021-04-17 10:00:07 +00:00
&Log{
2021-04-18 15:54:18 +00:00
Address: addr,
Topics: []string{ethcmn.BytesToHash([]byte("topic")).String()},
Data: []byte("data"),
BlockNumber: 1,
2021-04-18 15:54:18 +00:00
TxHash: ethcmn.BytesToHash([]byte("tx_hash")).String(),
TxIndex: 1,
2021-04-18 15:54:18 +00:00
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
Index: 1,
Removed: false,
},
true,
},
{
2021-04-17 10:00:07 +00:00
"empty log", &Log{}, false,
},
{
"zero address",
2021-04-17 10:00:07 +00:00
&Log{
Address: ethcmn.Address{}.String(),
},
false,
},
{
"empty block hash",
2021-04-17 10:00:07 +00:00
&Log{
2021-04-18 15:54:18 +00:00
Address: addr,
2021-04-17 10:00:07 +00:00
BlockHash: ethcmn.Hash{}.String(),
},
false,
},
{
"zero block number",
2021-04-17 10:00:07 +00:00
&Log{
2021-04-18 15:54:18 +00:00
Address: addr,
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
BlockNumber: 0,
},
false,
},
{
"empty tx hash",
2021-04-17 10:00:07 +00:00
&Log{
2021-04-18 15:54:18 +00:00
Address: addr,
BlockHash: ethcmn.BytesToHash([]byte("block_hash")).String(),
BlockNumber: 1,
2021-04-17 10:00:07 +00:00
TxHash: ethcmn.Hash{}.String(),
},
false,
},
}
for _, tc := range testCases {
tc := tc
2021-04-17 10:00:07 +00:00
err := tc.log.Validate()
if tc.expPass {
2021-04-18 15:54:18 +00:00
require.NoError(t, err, tc.name)
} else {
2021-04-18 15:54:18 +00:00
require.Error(t, err, tc.name)
}
}
}