github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/types/genesis_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	ethcmn "github.com/ethereum/go-ethereum/common"
     7  	ethtypes "github.com/ethereum/go-ethereum/core/types"
     8  	ethcrypto "github.com/ethereum/go-ethereum/crypto"
     9  	"github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var address = ethcmn.BytesToAddress([]byte{1, 2, 3, 4, 5})
    14  
    15  func TestValidateGenesisAccount(t *testing.T) {
    16  	testCases := []struct {
    17  		name           string
    18  		genesisAccount GenesisAccount
    19  		expPass        bool
    20  	}{
    21  		{
    22  			"valid genesis account",
    23  			GenesisAccount{
    24  				Address: address.String(),
    25  				Code:    []byte{1, 2, 3},
    26  				Storage: Storage{
    27  					NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
    28  				},
    29  			},
    30  			true,
    31  		},
    32  		{
    33  			"empty account address bytes",
    34  			GenesisAccount{
    35  				Address: ethcmn.Address{}.String(),
    36  			},
    37  			false,
    38  		},
    39  		{
    40  			"empty code bytes",
    41  			GenesisAccount{
    42  				Address: address.String(),
    43  				Code:    []byte{},
    44  			},
    45  			false,
    46  		},
    47  	}
    48  
    49  	for _, tc := range testCases {
    50  		tc := tc
    51  		err := tc.genesisAccount.Validate()
    52  		if tc.expPass {
    53  			require.NoError(t, err, tc.name)
    54  		} else {
    55  			require.Error(t, err, tc.name)
    56  		}
    57  	}
    58  }
    59  
    60  func TestValidateGenesis(t *testing.T) {
    61  	priv, err := ethsecp256k1.GenerateKey()
    62  	require.NoError(t, err)
    63  	addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
    64  
    65  	testCases := []struct {
    66  		name     string
    67  		genState GenesisState
    68  		expPass  bool
    69  	}{
    70  		{
    71  			name:     "default",
    72  			genState: DefaultGenesisState(),
    73  			expPass:  true,
    74  		},
    75  		{
    76  			name: "valid genesis",
    77  			genState: GenesisState{
    78  				Accounts: []GenesisAccount{
    79  					{
    80  						Address: address.String(),
    81  						Code:    []byte{1, 2, 3},
    82  						Storage: Storage{
    83  							{Key: ethcmn.BytesToHash([]byte{1, 2, 3})},
    84  						},
    85  					},
    86  				},
    87  				TxsLogs: []TransactionLogs{
    88  					{
    89  						Hash: ethcmn.BytesToHash([]byte("tx_hash")),
    90  						Logs: []*ethtypes.Log{
    91  							{
    92  								Address:     addr,
    93  								Topics:      []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))},
    94  								Data:        []byte("data"),
    95  								BlockNumber: 1,
    96  								TxHash:      ethcmn.BytesToHash([]byte("tx_hash")),
    97  								TxIndex:     1,
    98  								BlockHash:   ethcmn.BytesToHash([]byte("block_hash")),
    99  								Index:       1,
   100  								Removed:     false,
   101  							},
   102  						},
   103  					},
   104  				},
   105  				ChainConfig: DefaultChainConfig(),
   106  				Params:      DefaultParams(),
   107  			},
   108  			expPass: true,
   109  		},
   110  		{
   111  			name:     "empty genesis",
   112  			genState: GenesisState{},
   113  			expPass:  false,
   114  		},
   115  		{
   116  			name: "invalid genesis",
   117  			genState: GenesisState{
   118  				Accounts: []GenesisAccount{
   119  					{
   120  						Address: ethcmn.Address{}.String(),
   121  					},
   122  				},
   123  			},
   124  			expPass: false,
   125  		},
   126  		{
   127  			name: "duplicated genesis account",
   128  			genState: GenesisState{
   129  				Accounts: []GenesisAccount{
   130  					{
   131  						Address: address.String(),
   132  						Code:    []byte{1, 2, 3},
   133  						Storage: Storage{
   134  							NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
   135  						},
   136  					},
   137  					{
   138  						Address: address.String(),
   139  						Code:    []byte{1, 2, 3},
   140  						Storage: Storage{
   141  							NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
   142  						},
   143  					},
   144  				},
   145  			},
   146  			expPass: false,
   147  		},
   148  		{
   149  			name: "duplicated tx log",
   150  			genState: GenesisState{
   151  				Accounts: []GenesisAccount{
   152  					{
   153  						Address: address.String(),
   154  						Code:    []byte{1, 2, 3},
   155  						Storage: Storage{
   156  							{Key: ethcmn.BytesToHash([]byte{1, 2, 3})},
   157  						},
   158  					},
   159  				},
   160  				TxsLogs: []TransactionLogs{
   161  					{
   162  						Hash: ethcmn.BytesToHash([]byte("tx_hash")),
   163  						Logs: []*ethtypes.Log{
   164  							{
   165  								Address:     addr,
   166  								Topics:      []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))},
   167  								Data:        []byte("data"),
   168  								BlockNumber: 1,
   169  								TxHash:      ethcmn.BytesToHash([]byte("tx_hash")),
   170  								TxIndex:     1,
   171  								BlockHash:   ethcmn.BytesToHash([]byte("block_hash")),
   172  								Index:       1,
   173  								Removed:     false,
   174  							},
   175  						},
   176  					},
   177  					{
   178  						Hash: ethcmn.BytesToHash([]byte("tx_hash")),
   179  						Logs: []*ethtypes.Log{
   180  							{
   181  								Address:     addr,
   182  								Topics:      []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))},
   183  								Data:        []byte("data"),
   184  								BlockNumber: 1,
   185  								TxHash:      ethcmn.BytesToHash([]byte("tx_hash")),
   186  								TxIndex:     1,
   187  								BlockHash:   ethcmn.BytesToHash([]byte("block_hash")),
   188  								Index:       1,
   189  								Removed:     false,
   190  							},
   191  						},
   192  					},
   193  				},
   194  			},
   195  			expPass: false,
   196  		},
   197  		{
   198  			name: "invalid tx log",
   199  			genState: GenesisState{
   200  				Accounts: []GenesisAccount{
   201  					{
   202  						Address: address.String(),
   203  						Code:    []byte{1, 2, 3},
   204  						Storage: Storage{
   205  							{Key: ethcmn.BytesToHash([]byte{1, 2, 3})},
   206  						},
   207  					},
   208  				},
   209  				TxsLogs: []TransactionLogs{NewTransactionLogs(ethcmn.Hash{}, nil)},
   210  			},
   211  			expPass: false,
   212  		},
   213  		{
   214  			name: "invalid params",
   215  			genState: GenesisState{
   216  				ChainConfig: DefaultChainConfig(),
   217  				Params:      Params{ExtraEIPs: []int{1}},
   218  			},
   219  			expPass: false,
   220  		},
   221  		{
   222  			name: "invalid chain config",
   223  			genState: GenesisState{
   224  				ChainConfig: ChainConfig{},
   225  				Params:      DefaultParams(),
   226  			},
   227  			expPass: false,
   228  		},
   229  	}
   230  
   231  	for _, tc := range testCases {
   232  		tc := tc
   233  		err := tc.genState.Validate()
   234  		if tc.expPass {
   235  			require.NoError(t, err, tc.name)
   236  		} else {
   237  			require.Error(t, err, tc.name)
   238  		}
   239  	}
   240  }