2020-04-23 15:49:25 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2020-10-06 18:57:55 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
2020-04-23 15:49:25 +00:00
|
|
|
)
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
type GenesisTestSuite struct {
|
|
|
|
suite.Suite
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
address string
|
2021-01-06 20:56:40 +00:00
|
|
|
hash ethcmn.Hash
|
|
|
|
code string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) SetupTest() {
|
|
|
|
priv, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.address = ethcmn.BytesToAddress(priv.PubKey().Address().Bytes()).String()
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.hash = ethcmn.BytesToHash([]byte("hash"))
|
|
|
|
suite.code = ethcmn.Bytes2Hex([]byte{1, 2, 3})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GenesisTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) TestValidateGenesisAccount() {
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2020-05-28 21:22:41 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
genesisAccount GenesisAccount
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid genesis account",
|
|
|
|
GenesisAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-01-06 20:56:40 +00:00
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
NewState(suite.hash, suite.hash),
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty account address bytes",
|
|
|
|
GenesisAccount{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: ethcmn.Address{}.String(),
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty code bytes",
|
|
|
|
GenesisAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-01-06 20:56:40 +00:00
|
|
|
Code: "",
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
err := tc.genesisAccount.Validate()
|
|
|
|
if tc.expPass {
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().NoError(err, tc.name)
|
2020-05-28 21:22:41 +00:00
|
|
|
} else {
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().Error(err, tc.name)
|
2020-05-28 21:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
func (suite *GenesisTestSuite) TestValidateGenesis() {
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
testCases := []struct {
|
2020-05-18 19:21:12 +00:00
|
|
|
name string
|
2021-04-17 10:00:07 +00:00
|
|
|
genState *GenesisState
|
2020-04-23 15:49:25 +00:00
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
2020-05-18 19:21:12 +00:00
|
|
|
name: "default",
|
|
|
|
genState: DefaultGenesisState(),
|
2020-04-23 15:49:25 +00:00
|
|
|
expPass: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-28 21:22:41 +00:00
|
|
|
name: "valid genesis",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-05-18 19:21:12 +00:00
|
|
|
Accounts: []GenesisAccount{
|
|
|
|
{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-04-18 17:23:26 +00:00
|
|
|
|
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: suite.hash.String()},
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-07-08 18:11:02 +00:00
|
|
|
TxsLogs: []TransactionLogs{
|
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Hash: suite.hash.String(),
|
2021-04-17 10:00:07 +00:00
|
|
|
Logs: []*Log{
|
2020-07-08 18:11:02 +00:00
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Address: suite.address,
|
2021-04-17 10:00:07 +00:00
|
|
|
Topics: []string{suite.hash.String()},
|
2020-07-08 18:11:02 +00:00
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
TxHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
TxIndex: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
BlockHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
Index: 1,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 19:41:05 +00:00
|
|
|
ChainConfig: DefaultChainConfig(),
|
|
|
|
Params: DefaultParams(),
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
expPass: true,
|
|
|
|
},
|
2020-09-02 19:41:05 +00:00
|
|
|
{
|
|
|
|
name: "empty genesis",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{},
|
2020-09-02 19:41:05 +00:00
|
|
|
expPass: false,
|
|
|
|
},
|
2020-05-28 21:22:41 +00:00
|
|
|
{
|
|
|
|
name: "invalid genesis",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-05-28 21:22:41 +00:00
|
|
|
Accounts: []GenesisAccount{
|
|
|
|
{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: ethcmn.Address{}.String(),
|
2020-05-18 19:21:12 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-23 15:49:25 +00:00
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
|
|
|
{
|
2020-05-28 21:22:41 +00:00
|
|
|
name: "duplicated genesis account",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-05-18 19:21:12 +00:00
|
|
|
Accounts: []GenesisAccount{
|
|
|
|
{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-04-18 17:23:26 +00:00
|
|
|
|
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
NewState(suite.hash, suite.hash),
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-04-18 17:23:26 +00:00
|
|
|
|
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
NewState(suite.hash, suite.hash),
|
2020-05-28 21:22:41 +00:00
|
|
|
},
|
2020-05-18 19:21:12 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-23 15:49:25 +00:00
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
2020-07-08 18:11:02 +00:00
|
|
|
{
|
|
|
|
name: "duplicated tx log",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-07-08 18:11:02 +00:00
|
|
|
Accounts: []GenesisAccount{
|
|
|
|
{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-04-18 17:23:26 +00:00
|
|
|
|
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: suite.hash.String()},
|
2020-07-08 18:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TxsLogs: []TransactionLogs{
|
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Hash: suite.hash.String(),
|
2021-04-17 10:00:07 +00:00
|
|
|
Logs: []*Log{
|
2020-07-08 18:11:02 +00:00
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Address: suite.address,
|
2021-04-17 10:00:07 +00:00
|
|
|
Topics: []string{suite.hash.String()},
|
2020-07-08 18:11:02 +00:00
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
TxHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
TxIndex: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
BlockHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
Index: 1,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Hash: suite.hash.String(),
|
2021-04-17 10:00:07 +00:00
|
|
|
Logs: []*Log{
|
2020-07-08 18:11:02 +00:00
|
|
|
{
|
2021-01-06 20:56:40 +00:00
|
|
|
Address: suite.address,
|
2021-04-17 10:00:07 +00:00
|
|
|
Topics: []string{suite.hash.String()},
|
2020-07-08 18:11:02 +00:00
|
|
|
Data: []byte("data"),
|
|
|
|
BlockNumber: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
TxHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
TxIndex: 1,
|
2021-04-17 10:00:07 +00:00
|
|
|
BlockHash: suite.hash.String(),
|
2020-07-08 18:11:02 +00:00
|
|
|
Index: 1,
|
|
|
|
Removed: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid tx log",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-07-08 18:11:02 +00:00
|
|
|
Accounts: []GenesisAccount{
|
|
|
|
{
|
2021-04-17 10:00:07 +00:00
|
|
|
Address: suite.address,
|
2021-04-18 17:23:26 +00:00
|
|
|
|
|
|
|
Code: suite.code,
|
2020-07-13 20:01:45 +00:00
|
|
|
Storage: Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: suite.hash.String()},
|
2020-07-08 18:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TxsLogs: []TransactionLogs{NewTransactionLogs(ethcmn.Hash{}, nil)},
|
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
2020-09-02 19:41:05 +00:00
|
|
|
{
|
|
|
|
name: "invalid params",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-09-02 19:41:05 +00:00
|
|
|
ChainConfig: DefaultChainConfig(),
|
|
|
|
Params: Params{},
|
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid chain config",
|
2021-04-17 10:00:07 +00:00
|
|
|
genState: &GenesisState{
|
2020-09-02 19:41:05 +00:00
|
|
|
ChainConfig: ChainConfig{},
|
|
|
|
Params: DefaultParams(),
|
|
|
|
},
|
|
|
|
expPass: false,
|
|
|
|
},
|
2020-04-23 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
err := tc.genState.Validate()
|
2020-04-23 15:49:25 +00:00
|
|
|
if tc.expPass {
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().NoError(err, tc.name)
|
2020-04-23 15:49:25 +00:00
|
|
|
} else {
|
2021-01-06 20:56:40 +00:00
|
|
|
suite.Require().Error(err, tc.name)
|
2020-04-23 15:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|