@@ -3,230 +3,219 @@ package evm_test
33import (
44 "testing"
55
6- "github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/chain-capabilities/evm"
6+ protoevm "github.com/smartcontractkit/chainlink-common/pkg/capabilities/v2/chain-capabilities/evm"
7+ chainevm "github.com/smartcontractkit/chainlink-common/pkg/chains/evm"
78 evmtypes "github.com/smartcontractkit/chainlink-common/pkg/types/chains/evm"
89 valuespb "github.com/smartcontractkit/chainlink-protos/cre/go/values/pb"
910
1011 "github.com/stretchr/testify/assert"
1112 "github.com/stretchr/testify/require"
1213)
1314
14- func TestConvertFilterFromProto ( t * testing. T ) {
15- validBlockHash := make ([ ]byte , 32 )
15+ func h32 ( b byte ) evmtypes. Hash {
16+ var h [ 32 ]byte
1617 for i := 0 ; i < 32 ; i ++ {
17- validBlockHash [i ] = byte (i )
18+ h [i ] = b + byte (i )
1819 }
19-
20- validAddress := make ([]byte , 20 )
20+ return h
21+ }
22+ func a20 (b byte ) evmtypes.Address {
23+ var a [20 ]byte
2124 for i := 0 ; i < 20 ; i ++ {
22- validAddress [i ] = byte ( i + 10 )
25+ a [i ] = b + byte ( i )
2326 }
24-
25- validTopic := make ([]byte , 32 )
27+ return a
28+ }
29+ func b32 (b byte ) []byte {
30+ out := make ([]byte , 32 )
2631 for i := 0 ; i < 32 ; i ++ {
27- validTopic [i ] = byte ( i + 20 )
32+ out [i ] = b + byte ( i )
2833 }
29-
30- t .Run ("nil protoFilter returns error" , func (t * testing.T ) {
31- _ , err := evm .ConvertFilterFromProto (nil )
32- assert .ErrorContains (t , err , "filter can't be nil" )
33- })
34-
35- t .Run ("successful conversion" , func (t * testing.T ) {
36- fromBlock := & valuespb.BigInt {AbsVal : []byte {1 , 2 , 3 }, Sign : 0 }
37- toBlock := & valuespb.BigInt {AbsVal : []byte {1 , 2 , 4 }, Sign : 0 }
38- validTopics := []* evm.Topics {{Topic : [][]byte {validTopic }}}
39- input := & evm.FilterQuery {
40- BlockHash : validBlockHash ,
41- FromBlock : fromBlock ,
42- ToBlock : toBlock ,
43- Addresses : [][]byte {validAddress },
44- Topics : validTopics ,
45- }
46-
47- // expected outputs from conversions
48- expectedAddr := [20 ]byte (validAddress )
49- expectedTopics := evm .ConvertTopicsFromProto (validTopics )
50-
51- result , err := evm .ConvertFilterFromProto (input )
52- require .NoError (t , err )
53-
54- assert .ElementsMatch (t , validBlockHash , result .BlockHash )
55- assert .Equal (t , valuespb .NewIntFromBigInt (fromBlock ), result .FromBlock )
56- assert .Equal (t , valuespb .NewIntFromBigInt (toBlock ), result .ToBlock )
57- assert .ElementsMatch (t , [][20 ]byte {expectedAddr }, result .Addresses )
58- assert .ElementsMatch (t , expectedTopics , result .Topics )
59- })
34+ return out
35+ }
36+ func b20 (b byte ) []byte {
37+ out := make ([]byte , 20 )
38+ for i := 0 ; i < 20 ; i ++ {
39+ out [i ] = b + byte (i )
40+ }
41+ return out
6042}
6143
62- func TestConvertAddressesFromProto (t * testing.T ) {
63- t .Run ("empty input " , func (t * testing.T ) {
64- addrs := evm . ConvertAddressesFromProto (nil )
65- assert .Empty (t , addrs )
44+ func TestConvertHeaderToProto (t * testing.T ) {
45+ t .Run ("nil header returns error " , func (t * testing.T ) {
46+ _ , err := protoevm . ConvertHeaderToProto (nil )
47+ assert .ErrorIs (t , err , chainevm . ErrEmptyHead )
6648 })
6749
68- t .Run ("invalid and valid addresses" , func (t * testing.T ) {
69- valid := make ([]byte , 20 )
70- for i := 0 ; i < 20 ; i ++ {
71- valid [i ] = byte (i + 1 )
50+ t .Run ("happy path" , func (t * testing.T ) {
51+ h := & evmtypes.Header {
52+ Timestamp : 123456 ,
53+ Hash : h32 (0x10 ),
54+ ParentHash : h32 (0x11 ),
55+ Number : valuespb .NewIntFromBigInt (& valuespb.BigInt {AbsVal : []byte {0x01 }, Sign : 0 }),
7256 }
73- invalid := []byte {0x01 , 0x02 }
74-
75- result := evm .ConvertAddressesFromProto ([][]byte {valid , invalid })
76- assert .Len (t , result , 1 )
77- assert .Equal (t , evmtypes .Address (valid ), result [0 ])
57+ p , err := protoevm .ConvertHeaderToProto (h )
58+ require .NoError (t , err )
59+ assert .Equal (t , h .Timestamp , p .Timestamp )
60+ assert .Equal (t , h .Hash [:], p .Hash )
61+ assert .Equal (t , h .ParentHash [:], p .ParentHash )
62+ assert .Equal (t , valuespb .NewBigIntFromInt (h .Number ), p .BlockNumber )
7863 })
7964}
8065
81- func TestConvertHashesFromProto (t * testing.T ) {
82- t .Run ("empty input" , func (t * testing.T ) {
83- hashes := evm .ConvertHashesFromProto (nil )
84- assert .Empty (t , hashes )
85- })
86-
87- t .Run ("invalid and valid hashes" , func (t * testing.T ) {
88- valid := make ([]byte , 32 )
89- for i := 0 ; i < 32 ; i ++ {
90- valid [i ] = byte (i + 10 )
91- }
92- invalid := []byte {0xAA }
93-
94- result := evm .ConvertHashesFromProto ([][]byte {valid , invalid })
95- assert .Len (t , result , 1 )
96- assert .Equal (t , evmtypes .Hash (valid ), result [0 ])
66+ func TestConvertHeaderFromProto (t * testing.T ) {
67+ t .Run ("nil proto returns error" , func (t * testing.T ) {
68+ _ , err := protoevm .ConvertHeaderFromProto (nil )
69+ assert .ErrorIs (t , err , chainevm .ErrEmptyHead )
9770 })
98- }
9971
100- func TestConvertTopicsFromProto (t * testing.T ) {
101- t .Run ("single topic with one hash" , func (t * testing.T ) {
102- topicBytes := make ([]byte , 32 )
103- for i := 0 ; i < 32 ; i ++ {
104- topicBytes [i ] = byte (i + 100 )
105- }
106- input := []* evm.Topics {
107- {Topic : [][]byte {topicBytes }},
72+ t .Run ("happy path" , func (t * testing.T ) {
73+ num := & valuespb.BigInt {AbsVal : []byte {0x01 }, Sign : 0 }
74+ p := & protoevm.Header {
75+ Timestamp : 42 ,
76+ BlockNumber : num ,
77+ Hash : b32 (0x20 ),
78+ ParentHash : b32 (0x21 ),
10879 }
109- result := evm .ConvertTopicsFromProto (input )
110- assert .Len (t , result , 1 )
111- assert .Len (t , result [0 ], 1 )
112- assert .Equal (t , evmtypes .Hash (topicBytes ), result [0 ][0 ])
80+ h , err := protoevm .ConvertHeaderFromProto (p )
81+ require .NoError (t , err )
82+ assert .Equal (t , uint64 (42 ), h .Timestamp )
83+ assert .Equal (t , evmtypes .Hash (b32 (0x20 )), h .Hash )
84+ assert .Equal (t , evmtypes .Hash (b32 (0x21 )), h .ParentHash )
85+ assert .Equal (t , valuespb .NewIntFromBigInt (num ), h .Number )
11386 })
11487}
11588
116- func TestConvertHeadFromProto (t * testing.T ) {
117- t .Run ("nil input returns error" , func (t * testing.T ) {
118- _ , err := evm . ConvertHeadFromProto (nil )
119- assert .ErrorContains (t , err , "head is nil" )
89+ func TestConvertReceiptToProto (t * testing.T ) {
90+ t .Run ("nil receipt returns error" , func (t * testing.T ) {
91+ _ , err := protoevm . ConvertReceiptToProto (nil )
92+ assert .ErrorIs (t , err , chainevm . ErrEmptyReceipt )
12093 })
12194
122- t .Run ("valid head" , func (t * testing.T ) {
123- hash := make ([]byte , 32 )
124- parent := make ([]byte , 32 )
125- for i := 0 ; i < 32 ; i ++ {
126- hash [i ] = byte (i + 30 )
127- parent [i ] = byte (i + 60 )
128- }
129- num := & valuespb.BigInt {AbsVal : []byte {0x01 , 0x02 , 0x03 }, Sign : 1 }
130- timestamp := uint64 (42 )
131-
132- proto := & evm.Header {
133- Timestamp : timestamp ,
134- BlockNumber : num ,
135- Hash : hash ,
136- ParentHash : parent ,
95+ t .Run ("happy path" , func (t * testing.T ) {
96+ r := & evmtypes.Receipt {
97+ Status : 1 ,
98+ Logs : []* evmtypes.Log {{}},
99+ TxHash : h32 (0x30 ),
100+ ContractAddress : a20 (0x31 ),
101+ GasUsed : 21000 ,
102+ BlockHash : h32 (0x32 ),
103+ BlockNumber : valuespb .NewIntFromBigInt (& valuespb.BigInt {AbsVal : []byte {0x02 }, Sign : 0 }),
104+ TransactionIndex : 9 ,
105+ EffectiveGasPrice : valuespb .NewIntFromBigInt (& valuespb.BigInt {AbsVal : []byte {0x03 }, Sign : 0 }),
137106 }
138-
139- result , err := evm .ConvertHeadFromProto (proto )
107+ p , err := protoevm .ConvertReceiptToProto (r )
140108 require .NoError (t , err )
141- assert .Equal (t , timestamp , result .Timestamp )
142- assert .Equal (t , evmtypes .Hash (hash ), result .Hash )
143- assert .Equal (t , evmtypes .Hash (parent ), result .ParentHash )
144- assert .Equal (t , valuespb .NewIntFromBigInt (num ), result .Number )
109+ assert .Equal (t , r .Status , p .Status )
110+ assert .Equal (t , r .TxHash [:], p .TxHash )
111+ assert .Equal (t , r .ContractAddress [:], p .ContractAddress )
112+ assert .Equal (t , r .GasUsed , p .GasUsed )
113+ assert .Equal (t , r .BlockHash [:], p .BlockHash )
114+ assert .Equal (t , valuespb .NewBigIntFromInt (r .BlockNumber ), p .BlockNumber )
115+ assert .Equal (t , r .TransactionIndex , p .TxIndex )
116+ assert .Equal (t , valuespb .NewBigIntFromInt (r .EffectiveGasPrice ), p .EffectiveGasPrice )
145117 })
146118}
147119
148120func TestConvertReceiptFromProto (t * testing.T ) {
149- t .Run ("nil input returns error" , func (t * testing.T ) {
150- _ , err := evm .ConvertReceiptFromProto (nil )
151- assert .ErrorContains (t , err , "receipt is nil" )
121+ t .Run ("nil proto returns error" , func (t * testing.T ) {
122+ _ , err := protoevm .ConvertReceiptFromProto (nil )
123+ assert .ErrorIs (t , err , chainevm . ErrEmptyReceipt )
152124 })
153125
154- t .Run ("valid receipt" , func (t * testing.T ) {
155- txHash := make ([]byte , 32 )
156- addr := make ([]byte , 20 )
157- blockHash := make ([]byte , 32 )
158- for i := range txHash {
159- txHash [i ] = byte (i + 1 )
160- }
161- for i := range addr {
162- addr [i ] = byte (i + 50 )
163- }
164- for i := range blockHash {
165- blockHash [i ] = byte (i + 100 )
166- }
167-
168- num := & valuespb.BigInt {AbsVal : []byte {0x01 , 0x02 , 0x03 }, Sign : 1 }
169- price := & valuespb.BigInt {AbsVal : []byte {0x0A , 0x0B }, Sign : 1 }
170-
171- proto := & evm.Receipt {
126+ t .Run ("empty logs slice ok + happy path" , func (t * testing.T ) {
127+ num := & valuespb.BigInt {AbsVal : []byte {0x01 }, Sign : 0 }
128+ price := & valuespb.BigInt {AbsVal : []byte {0x02 }, Sign : 0 }
129+ p := & protoevm.Receipt {
172130 Status : 1 ,
173- Logs : []* evm .Log {},
174- TxHash : txHash ,
175- ContractAddress : addr ,
131+ Logs : []* protoevm .Log {},
132+ TxHash : b32 ( 0x40 ) ,
133+ ContractAddress : b20 ( 0x41 ) ,
176134 GasUsed : 21000 ,
177- BlockHash : blockHash ,
135+ BlockHash : b32 ( 0x42 ) ,
178136 BlockNumber : num ,
179137 TxIndex : 3 ,
180138 EffectiveGasPrice : price ,
181139 }
182-
183- result , err := evm .ConvertReceiptFromProto (proto )
140+ r , err := protoevm .ConvertReceiptFromProto (p )
184141 require .NoError (t , err )
185- assert .Equal (t , evmtypes .Hash (txHash ), result .TxHash )
186- assert .Equal (t , evmtypes .Address (addr ), result .ContractAddress )
187- assert .Equal (t , evmtypes .Hash (blockHash ), result .BlockHash )
188- assert .Equal (t , valuespb .NewIntFromBigInt (num ), result .BlockNumber )
189- assert .Equal (t , valuespb .NewIntFromBigInt (price ), result .EffectiveGasPrice )
142+ assert .Equal (t , evmtypes .Hash (b32 (0x40 )), r .TxHash )
143+ assert .Equal (t , evmtypes .Address (b20 (0x41 )), r .ContractAddress )
144+ assert .Equal (t , evmtypes .Hash (b32 (0x42 )), r .BlockHash )
145+ assert .Equal (t , valuespb .NewIntFromBigInt (num ), r .BlockNumber )
146+ assert .Equal (t , valuespb .NewIntFromBigInt (price ), r .EffectiveGasPrice )
147+ assert .Equal (t , uint64 (21000 ), r .GasUsed )
148+ assert .Equal (t , uint64 (3 ), r .TransactionIndex )
190149 })
191150}
192151
193- func TestConvertTransactionFromProto (t * testing.T ) {
194- t .Run ("nil input returns error" , func (t * testing.T ) {
195- _ , err := evm . ConvertTransactionFromProto (nil )
196- assert .ErrorContains (t , err , "transaction is nil" )
152+ func TestConvertTransactionToProto (t * testing.T ) {
153+ t .Run ("nil tx returns error" , func (t * testing.T ) {
154+ _ , err := protoevm . ConvertTransactionToProto (nil )
155+ assert .ErrorIs (t , err , chainevm . ErrEmptyTx )
197156 })
198157
199- t .Run ("valid transaction" , func (t * testing.T ) {
200- to := make ([]byte , 20 )
201- hash := make ([]byte , 32 )
202- for i := 0 ; i < 20 ; i ++ {
203- to [i ] = byte (i + 9 )
204- }
205- for i := 0 ; i < 32 ; i ++ {
206- hash [i ] = byte (i + 33 )
158+ t .Run ("happy path" , func (t * testing.T ) {
159+ tx := & evmtypes.Transaction {
160+ To : a20 (0x50 ),
161+ Data : []byte {0xDE , 0xAD },
162+ Hash : h32 (0x51 ),
163+ Nonce : 7 ,
164+ Gas : 50000 ,
165+ GasPrice : valuespb .NewIntFromBigInt (& valuespb.BigInt {AbsVal : []byte {0x0F }, Sign : 1 }),
166+ Value : valuespb .NewIntFromBigInt (& valuespb.BigInt {AbsVal : []byte {0x0E }, Sign : 1 }),
207167 }
168+ p , err := protoevm .ConvertTransactionToProto (tx )
169+ require .NoError (t , err )
170+ assert .Equal (t , tx .To [:], p .To )
171+ assert .Equal (t , tx .Data , p .Data )
172+ assert .Equal (t , tx .Hash [:], p .Hash )
173+ assert .Equal (t , tx .Nonce , p .Nonce )
174+ assert .Equal (t , tx .Gas , p .Gas )
175+ assert .Equal (t , valuespb .NewBigIntFromInt (tx .GasPrice ), p .GasPrice )
176+ assert .Equal (t , valuespb .NewBigIntFromInt (tx .Value ), p .Value )
177+ })
178+ }
208179
209- gasPrice := & valuespb.BigInt {AbsVal : []byte {0x05 , 0x06 }, Sign : 1 }
210- value := & valuespb.BigInt {AbsVal : []byte {0x07 , 0x08 }, Sign : 1 }
180+ func TestConvertTransactionFromProto (t * testing.T ) {
181+ t .Run ("nil proto returns error" , func (t * testing.T ) {
182+ _ , err := protoevm .ConvertTransactionFromProto (nil )
183+ assert .ErrorIs (t , err , chainevm .ErrEmptyTx )
184+ })
211185
212- proto := & evm.Transaction {
213- To : to ,
186+ t .Run ("happy path" , func (t * testing.T ) {
187+ gp := & valuespb.BigInt {AbsVal : []byte {0x05 }, Sign : 1 }
188+ val := & valuespb.BigInt {AbsVal : []byte {0x06 }, Sign : 1 }
189+ p := & protoevm.Transaction {
190+ To : b20 (0x60 ),
214191 Data : []byte {0x01 , 0x02 },
215- Hash : hash ,
192+ Hash : b32 ( 0x61 ) ,
216193 Nonce : 1 ,
217194 Gas : 30000 ,
218- GasPrice : gasPrice ,
219- Value : value ,
195+ GasPrice : gp ,
196+ Value : val ,
220197 }
198+ tx , err := protoevm .ConvertTransactionFromProto (p )
199+ require .NoError (t , err )
200+ assert .Equal (t , evmtypes .Address (b20 (0x60 )), tx .To )
201+ assert .Equal (t , []byte {0x01 , 0x02 }, tx .Data )
202+ assert .Equal (t , evmtypes .Hash (b32 (0x61 )), tx .Hash )
203+ assert .Equal (t , uint64 (1 ), tx .Nonce )
204+ assert .Equal (t , uint64 (30000 ), tx .Gas )
205+ assert .Equal (t , valuespb .NewIntFromBigInt (gp ), tx .GasPrice )
206+ assert .Equal (t , valuespb .NewIntFromBigInt (val ), tx .Value )
207+ })
221208
222- result , err := evm .ConvertTransactionFromProto (proto )
209+ t .Run ("optional To nil is accepted" , func (t * testing.T ) {
210+ p := & protoevm.Transaction {
211+ Hash : b32 (0x62 ),
212+ GasPrice : & valuespb.BigInt {AbsVal : []byte {0x01 }, Sign : 1 },
213+ Value : & valuespb.BigInt {AbsVal : []byte {0x02 }, Sign : 1 },
214+ }
215+ tx , err := protoevm .ConvertTransactionFromProto (p )
223216 require .NoError (t , err )
224- assert .Equal (t , evmtypes .Address (to ), result .To )
225- assert .Equal (t , []byte {0x01 , 0x02 }, result .Data )
226- assert .Equal (t , evmtypes .Hash (hash ), result .Hash )
227- assert .Equal (t , uint64 (1 ), result .Nonce )
228- assert .Equal (t , uint64 (30000 ), result .Gas )
229- assert .Equal (t , valuespb .NewIntFromBigInt (gasPrice ), result .GasPrice )
230- assert .Equal (t , valuespb .NewIntFromBigInt (value ), result .Value )
217+
218+ var zero evmtypes.Address
219+ assert .Equal (t , zero , tx .To )
231220 })
232221}
0 commit comments