8
8
"testing"
9
9
10
10
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
11
+ "github.com/ava-labs/avalanchego/utils/set"
11
12
"github.com/ava-labs/coreth/core/types"
12
13
"github.com/ava-labs/coreth/params"
13
14
"github.com/ava-labs/coreth/precompile/precompileconfig"
@@ -31,8 +32,6 @@ func TestCheckPredicate(t *testing.T) {
31
32
addr2 := common .HexToAddress ("0xbb" )
32
33
addr3 := common .HexToAddress ("0xcc" )
33
34
addr4 := common .HexToAddress ("0xdd" )
34
- predicateResultBytes1 := []byte {1 , 2 , 3 }
35
- predicateResultBytes2 := []byte {3 , 2 , 1 }
36
35
predicateContext := & precompileconfig.PredicateContext {
37
36
ProposerVMBlockCtx : & block.Context {
38
37
PChainHeight : 10 ,
@@ -142,7 +141,7 @@ func TestCheckPredicate(t *testing.T) {
142
141
predicater := precompileconfig .NewMockPredicater (gomock .NewController (t ))
143
142
arg := common.Hash {1 }
144
143
predicater .EXPECT ().PredicateGas (arg [:]).Return (uint64 (0 ), nil ).Times (2 )
145
- predicater .EXPECT ().VerifyPredicate (gomock .Any (), [][] byte { arg [:]} ).Return (predicateResultBytes1 )
144
+ predicater .EXPECT ().VerifyPredicate (gomock .Any (), arg [:]).Return (nil )
146
145
return map [common.Address ]precompileconfig.Predicater {
147
146
addr1 : predicater ,
148
147
}
@@ -156,7 +155,7 @@ func TestCheckPredicate(t *testing.T) {
156
155
},
157
156
}),
158
157
expectedRes : map [common.Address ][]byte {
159
- addr1 : predicateResultBytes1 ,
158
+ addr1 : {}, // valid bytes
160
159
},
161
160
expectedErr : nil ,
162
161
},
@@ -188,7 +187,7 @@ func TestCheckPredicate(t *testing.T) {
188
187
predicater := precompileconfig .NewMockPredicater (gomock .NewController (t ))
189
188
arg := common.Hash {1 }
190
189
predicater .EXPECT ().PredicateGas (arg [:]).Return (uint64 (0 ), nil ).Times (2 )
191
- predicater .EXPECT ().VerifyPredicate (gomock .Any (), [][] byte { arg [:]} ).Return (predicateResultBytes1 )
190
+ predicater .EXPECT ().VerifyPredicate (gomock .Any (), arg [:]).Return (nil )
192
191
return map [common.Address ]precompileconfig.Predicater {
193
192
addr1 : predicater ,
194
193
addr2 : predicater ,
@@ -203,7 +202,7 @@ func TestCheckPredicate(t *testing.T) {
203
202
},
204
203
}),
205
204
expectedRes : map [common.Address ][]byte {
206
- addr1 : predicateResultBytes1 ,
205
+ addr1 : {}, // valid bytes
207
206
},
208
207
expectedErr : nil ,
209
208
},
@@ -215,11 +214,11 @@ func TestCheckPredicate(t *testing.T) {
215
214
predicate1 := precompileconfig .NewMockPredicater (ctrl )
216
215
arg1 := common.Hash {1 }
217
216
predicate1 .EXPECT ().PredicateGas (arg1 [:]).Return (uint64 (0 ), nil ).Times (2 )
218
- predicate1 .EXPECT ().VerifyPredicate (gomock .Any (), [][] byte { arg1 [:]} ).Return (predicateResultBytes1 )
217
+ predicate1 .EXPECT ().VerifyPredicate (gomock .Any (), arg1 [:]).Return (nil )
219
218
predicate2 := precompileconfig .NewMockPredicater (ctrl )
220
219
arg2 := common.Hash {2 }
221
220
predicate2 .EXPECT ().PredicateGas (arg2 [:]).Return (uint64 (0 ), nil ).Times (2 )
222
- predicate2 .EXPECT ().VerifyPredicate (gomock .Any (), [][] byte { arg2 [:]} ).Return (predicateResultBytes2 )
221
+ predicate2 .EXPECT ().VerifyPredicate (gomock .Any (), arg2 [:]).Return (testErr )
223
222
return map [common.Address ]precompileconfig.Predicater {
224
223
addr1 : predicate1 ,
225
224
addr2 : predicate2 ,
@@ -240,8 +239,8 @@ func TestCheckPredicate(t *testing.T) {
240
239
},
241
240
}),
242
241
expectedRes : map [common.Address ][]byte {
243
- addr1 : predicateResultBytes1 ,
244
- addr2 : predicateResultBytes2 ,
242
+ addr1 : {}, // valid bytes
243
+ addr2 : { 1 }, // invalid bytes
245
244
},
246
245
expectedErr : nil ,
247
246
},
@@ -322,3 +321,141 @@ func TestCheckPredicate(t *testing.T) {
322
321
})
323
322
}
324
323
}
324
+
325
+ func TestCheckPredicatesOutput (t * testing.T ) {
326
+ testErr := errors .New ("test error" )
327
+ addr1 := common .HexToAddress ("0xaa" )
328
+ addr2 := common .HexToAddress ("0xbb" )
329
+ validHash := common.Hash {1 }
330
+ invalidHash := common.Hash {2 }
331
+ predicateContext := & precompileconfig.PredicateContext {
332
+ ProposerVMBlockCtx : & block.Context {
333
+ PChainHeight : 10 ,
334
+ },
335
+ }
336
+ type testTuple struct {
337
+ address common.Address
338
+ isValidPredicate bool
339
+ }
340
+ type resultTest struct {
341
+ name string
342
+ expectedRes map [common.Address ][]byte
343
+ testTuple []testTuple
344
+ }
345
+ tests := []resultTest {
346
+ {name : "no predicates" , expectedRes : map [common.Address ][]byte {}},
347
+ {
348
+ name : "one address one predicate" ,
349
+ testTuple : []testTuple {
350
+ {address : addr1 , isValidPredicate : true },
351
+ },
352
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits ().Bytes ()},
353
+ },
354
+ {
355
+ name : "one address one invalid predicate" ,
356
+ testTuple : []testTuple {
357
+ {address : addr1 , isValidPredicate : false },
358
+ },
359
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (0 ).Bytes ()},
360
+ },
361
+ {
362
+ name : "one address two invalid predicates" ,
363
+ testTuple : []testTuple {
364
+ {address : addr1 , isValidPredicate : false },
365
+ {address : addr1 , isValidPredicate : false },
366
+ },
367
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (0 , 1 ).Bytes ()},
368
+ },
369
+ {
370
+ name : "one address two mixed predicates" ,
371
+ testTuple : []testTuple {
372
+ {address : addr1 , isValidPredicate : true },
373
+ {address : addr1 , isValidPredicate : false },
374
+ },
375
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (1 ).Bytes ()},
376
+ },
377
+ {
378
+ name : "one address mixed predicates" ,
379
+ testTuple : []testTuple {
380
+ {address : addr1 , isValidPredicate : true },
381
+ {address : addr1 , isValidPredicate : false },
382
+ {address : addr1 , isValidPredicate : false },
383
+ {address : addr1 , isValidPredicate : true },
384
+ },
385
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (1 , 2 ).Bytes ()},
386
+ },
387
+ {
388
+ name : "two addresses mixed predicates" ,
389
+ testTuple : []testTuple {
390
+ {address : addr1 , isValidPredicate : true },
391
+ {address : addr2 , isValidPredicate : false },
392
+ {address : addr1 , isValidPredicate : false },
393
+ {address : addr1 , isValidPredicate : false },
394
+ {address : addr2 , isValidPredicate : true },
395
+ {address : addr2 , isValidPredicate : true },
396
+ {address : addr2 , isValidPredicate : false },
397
+ {address : addr2 , isValidPredicate : true },
398
+ },
399
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (1 , 2 ).Bytes (), addr2 : set .NewBits (0 , 3 ).Bytes ()},
400
+ },
401
+ {
402
+ name : "two addresses all valid predicates" ,
403
+ testTuple : []testTuple {
404
+ {address : addr1 , isValidPredicate : true },
405
+ {address : addr2 , isValidPredicate : true },
406
+ {address : addr1 , isValidPredicate : true },
407
+ {address : addr1 , isValidPredicate : true },
408
+ },
409
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits ().Bytes (), addr2 : set .NewBits ().Bytes ()},
410
+ },
411
+ {
412
+ name : "two addresses all invalid predicates" ,
413
+ testTuple : []testTuple {
414
+ {address : addr1 , isValidPredicate : false },
415
+ {address : addr2 , isValidPredicate : false },
416
+ {address : addr1 , isValidPredicate : false },
417
+ {address : addr1 , isValidPredicate : false },
418
+ },
419
+ expectedRes : map [common.Address ][]byte {addr1 : set .NewBits (0 , 1 , 2 ).Bytes (), addr2 : set .NewBits (0 ).Bytes ()},
420
+ },
421
+ }
422
+ for _ , test := range tests {
423
+ t .Run (test .name , func (t * testing.T ) {
424
+ require := require .New (t )
425
+ // Create the rules from TestChainConfig and update the predicates based on the test params
426
+ rules := params .TestChainConfig .AvalancheRules (common .Big0 , 0 )
427
+ predicater := precompileconfig .NewMockPredicater (gomock .NewController (t ))
428
+ predicater .EXPECT ().PredicateGas (gomock .Any ()).Return (uint64 (0 ), nil ).Times (len (test .testTuple ))
429
+
430
+ var txAccessList types.AccessList
431
+ for _ , tuple := range test .testTuple {
432
+ var predicateHash common.Hash
433
+ if tuple .isValidPredicate {
434
+ predicateHash = validHash
435
+ predicater .EXPECT ().VerifyPredicate (gomock .Any (), validHash [:]).Return (nil )
436
+ } else {
437
+ predicateHash = invalidHash
438
+ predicater .EXPECT ().VerifyPredicate (gomock .Any (), invalidHash [:]).Return (testErr )
439
+ }
440
+ txAccessList = append (txAccessList , types.AccessTuple {
441
+ Address : tuple .address ,
442
+ StorageKeys : []common.Hash {
443
+ predicateHash ,
444
+ },
445
+ })
446
+ }
447
+
448
+ rules .Predicaters [addr1 ] = predicater
449
+ rules .Predicaters [addr2 ] = predicater
450
+
451
+ // Specify only the access list, since this test should not depend on any other values
452
+ tx := types .NewTx (& types.DynamicFeeTx {
453
+ AccessList : txAccessList ,
454
+ Gas : 53000 ,
455
+ })
456
+ predicateRes , err := CheckPredicates (rules , predicateContext , tx )
457
+ require .NoError (err )
458
+ require .Equal (test .expectedRes , predicateRes )
459
+ })
460
+ }
461
+ }
0 commit comments