github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/types/logs_test.go (about) 1 package types 2 3 import ( 4 "testing" 5 6 "github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1" 7 "github.com/stretchr/testify/require" 8 9 ethcmn "github.com/ethereum/go-ethereum/common" 10 ethtypes "github.com/ethereum/go-ethereum/core/types" 11 ethcrypto "github.com/ethereum/go-ethereum/crypto" 12 ) 13 14 func TestTransactionLogsValidate(t *testing.T) { 15 priv, err := ethsecp256k1.GenerateKey() 16 require.NoError(t, err) 17 addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) 18 19 testCases := []struct { 20 name string 21 txLogs TransactionLogs 22 expPass bool 23 }{ 24 { 25 "valid log", 26 TransactionLogs{ 27 Hash: ethcmn.BytesToHash([]byte("tx_hash")), 28 Logs: []*ethtypes.Log{ 29 { 30 Address: addr, 31 Topics: []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))}, 32 Data: []byte("data"), 33 BlockNumber: 1, 34 TxHash: ethcmn.BytesToHash([]byte("tx_hash")), 35 TxIndex: 1, 36 BlockHash: ethcmn.BytesToHash([]byte("block_hash")), 37 Index: 1, 38 Removed: false, 39 }, 40 }, 41 }, 42 true, 43 }, 44 { 45 "empty hash", 46 TransactionLogs{ 47 Hash: ethcmn.Hash{}, 48 }, 49 false, 50 }, 51 { 52 "invalid log", 53 TransactionLogs{ 54 Hash: ethcmn.BytesToHash([]byte("tx_hash")), 55 Logs: []*ethtypes.Log{nil}, 56 }, 57 false, 58 }, 59 { 60 "hash mismatch log", 61 TransactionLogs{ 62 Hash: ethcmn.BytesToHash([]byte("tx_hash")), 63 Logs: []*ethtypes.Log{ 64 { 65 Address: addr, 66 Topics: []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))}, 67 Data: []byte("data"), 68 BlockNumber: 1, 69 TxHash: ethcmn.BytesToHash([]byte("other_hash")), 70 TxIndex: 1, 71 BlockHash: ethcmn.BytesToHash([]byte("block_hash")), 72 Index: 1, 73 Removed: false, 74 }, 75 }, 76 }, 77 false, 78 }, 79 } 80 81 for _, tc := range testCases { 82 tc := tc 83 err := tc.txLogs.Validate() 84 if tc.expPass { 85 require.NoError(t, err, tc.name) 86 } else { 87 require.Error(t, err, tc.name) 88 } 89 } 90 } 91 92 func TestValidateLog(t *testing.T) { 93 priv, err := ethsecp256k1.GenerateKey() 94 require.NoError(t, err) 95 addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) 96 97 testCases := []struct { 98 name string 99 log *ethtypes.Log 100 expPass bool 101 }{ 102 { 103 "valid log", 104 ðtypes.Log{ 105 Address: addr, 106 Topics: []ethcmn.Hash{ethcmn.BytesToHash([]byte("topic"))}, 107 Data: []byte("data"), 108 BlockNumber: 1, 109 TxHash: ethcmn.BytesToHash([]byte("tx_hash")), 110 TxIndex: 1, 111 BlockHash: ethcmn.BytesToHash([]byte("block_hash")), 112 Index: 1, 113 Removed: false, 114 }, 115 true, 116 }, 117 { 118 "nil log", nil, false, 119 }, 120 { 121 "zero address", 122 ðtypes.Log{ 123 Address: ethcmn.Address{}, 124 }, 125 false, 126 }, 127 { 128 "empty block hash", 129 ðtypes.Log{ 130 Address: addr, 131 BlockHash: ethcmn.Hash{}, 132 }, 133 false, 134 }, 135 { 136 "zero block number", 137 ðtypes.Log{ 138 Address: addr, 139 BlockHash: ethcmn.BytesToHash([]byte("block_hash")), 140 BlockNumber: 0, 141 }, 142 false, 143 }, 144 { 145 "empty tx hash", 146 ðtypes.Log{ 147 Address: addr, 148 BlockHash: ethcmn.BytesToHash([]byte("block_hash")), 149 BlockNumber: 1, 150 TxHash: ethcmn.Hash{}, 151 }, 152 false, 153 }, 154 } 155 156 for _, tc := range testCases { 157 tc := tc 158 err := ValidateLog(tc.log) 159 if tc.expPass { 160 require.NoError(t, err, tc.name) 161 } else { 162 require.Error(t, err, tc.name) 163 } 164 } 165 }