@@ -82,6 +82,9 @@ func appendValidators(genesis *core.Genesis, addrs []common.Address) {
82
82
}
83
83
genesis .ExtraData = genesis .ExtraData [:extraVanity ]
84
84
85
+ validatorSize := byte (len (addrs ))
86
+ genesis .ExtraData = append (genesis .ExtraData , validatorSize )
87
+
85
88
for _ , addr := range addrs {
86
89
genesis .ExtraData = append (genesis .ExtraData , addr [:]... )
87
90
}
@@ -428,3 +431,149 @@ OUT3:
428
431
}
429
432
}
430
433
}
434
+
435
+ func TestPrepareExtra (t * testing.T ) {
436
+ validatorN := 4
437
+ buf := make ([]byte , 0 )
438
+ buf = append (buf , make ([]byte , extraVanity )... )
439
+ buf = append (buf , byte (validatorN ))
440
+ buf = append (buf , make ([]byte , validatorN * common .AddressLength )... )
441
+ buf = append (buf , make ([]byte , extraSeal )... )
442
+
443
+ parentHeader := & types.Header {}
444
+ parentHeader .Extra = buf
445
+
446
+ header := & types.Header {}
447
+ header .Extra = common .StringToHash ("123" ).Bytes ()
448
+
449
+ expectedExtra := parentHeader .Extra
450
+ copy (expectedExtra [0 :extraVanity ], header .Extra )
451
+
452
+ b , _ , _ := newSimpleBackend ()
453
+ extra := b .prepareExtra (header , parentHeader )
454
+ if bytes .Compare (extra , expectedExtra ) != 0 {
455
+ t .Errorf ("expected: %v, got: %v" , expectedExtra , extra )
456
+ }
457
+
458
+ // append useless information
459
+ buf = append (buf , make ([]byte , 15 )... )
460
+ header .Extra = buf
461
+
462
+ extra = b .prepareExtra (header , parentHeader )
463
+ if bytes .Compare (extra , expectedExtra ) != 0 {
464
+ t .Errorf ("expected: %v, got: %v" , expectedExtra , extra )
465
+ }
466
+ }
467
+
468
+ func TestSignaturePosition (t * testing.T ) {
469
+ validatorN := 2
470
+ buf := make ([]byte , 0 )
471
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
472
+ buf = append (buf , byte (validatorN ))
473
+ buf = append (buf , make ([]byte , validatorN * common .AddressLength )... )
474
+ buf = append (buf , make ([]byte , extraSeal )... )
475
+
476
+ expectedStart := extraVanity + extraValidatorSize + validatorN * common .AddressLength
477
+ expectedtEnd := expectedStart + extraSeal
478
+
479
+ header := & types.Header {}
480
+ header .Extra = buf
481
+
482
+ b , _ , _ := newSimpleBackend ()
483
+ start , end := b .signaturePosition (header )
484
+ if expectedStart != start && expectedtEnd != end {
485
+ t .Errorf ("expected start: %v, got: %v, expected end: %v, got: %v" , expectedStart , start , expectedtEnd , end )
486
+ }
487
+ }
488
+
489
+ func TestValidExtra (t * testing.T ) {
490
+
491
+ testCases := []struct {
492
+ extra []byte
493
+ expectedValid bool
494
+ }{
495
+ {
496
+ // normal case
497
+ func () []byte {
498
+ validatorN := 4
499
+ buf := make ([]byte , 0 )
500
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
501
+ buf = append (buf , byte (validatorN ))
502
+ buf = append (buf , make ([]byte , validatorN * common .AddressLength )... )
503
+ buf = append (buf , make ([]byte , extraSeal )... )
504
+ return buf
505
+ }(),
506
+ true ,
507
+ },
508
+ {
509
+ // missing validator
510
+ func () []byte {
511
+ validatorN := 4
512
+ buf := make ([]byte , 0 )
513
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
514
+ buf = append (buf , byte (validatorN ))
515
+ buf = append (buf , make ([]byte , extraSeal )... )
516
+ return buf
517
+ }(),
518
+ false ,
519
+ },
520
+ {
521
+ // validator N is 0
522
+ func () []byte {
523
+ validatorN := 0
524
+ buf := make ([]byte , 0 )
525
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
526
+ buf = append (buf , byte (validatorN ))
527
+ buf = append (buf , make ([]byte , validatorN * common .AddressLength )... )
528
+ buf = append (buf , make ([]byte , extraSeal )... )
529
+ return buf
530
+ }(),
531
+ false ,
532
+ },
533
+ {
534
+ // validator N is 0, but have 1 validator in field
535
+ func () []byte {
536
+ validatorN := 0
537
+ buf := make ([]byte , 0 )
538
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
539
+ buf = append (buf , byte (validatorN ))
540
+ buf = append (buf , make ([]byte , common .AddressLength )... )
541
+ buf = append (buf , make ([]byte , extraSeal )... )
542
+ return buf
543
+ }(),
544
+ false ,
545
+ },
546
+ {
547
+ // missing seal
548
+ func () []byte {
549
+ validatorN := 4
550
+ buf := make ([]byte , 0 )
551
+ buf = append (buf , common .StringToHash ("123" ).Bytes ()... )
552
+ buf = append (buf , byte (validatorN ))
553
+ buf = append (buf , make ([]byte , validatorN * common .AddressLength )... )
554
+ return buf
555
+ }(),
556
+ false ,
557
+ },
558
+ {
559
+ // missing few data
560
+ func () []byte {
561
+ buf := make ([]byte , 0 )
562
+ return buf
563
+ }(),
564
+ false ,
565
+ },
566
+ }
567
+
568
+ b , _ , _ := newSimpleBackend ()
569
+
570
+ for _ , test := range testCases {
571
+ header := & types.Header {}
572
+ header .Extra = test .extra
573
+
574
+ valid := b .validExtraFormat (header )
575
+ if valid != test .expectedValid {
576
+ t .Errorf ("expected: %v, but: %v" , test .expectedValid , valid )
577
+ }
578
+ }
579
+ }
0 commit comments