@@ -6,6 +6,8 @@ package proposervm
6
6
import (
7
7
"bytes"
8
8
"context"
9
+ "crypto"
10
+ "encoding/binary"
9
11
"testing"
10
12
"time"
11
13
@@ -23,7 +25,10 @@ import (
23
25
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
24
26
"github.com/ava-labs/avalanchego/snow/snowtest"
25
27
"github.com/ava-labs/avalanchego/snow/validators"
28
+ "github.com/ava-labs/avalanchego/staking"
26
29
"github.com/ava-labs/avalanchego/utils/timer/mockable"
30
+
31
+ blockbuilder "github.com/ava-labs/avalanchego/vms/proposervm/block"
27
32
)
28
33
29
34
func TestCoreVMNotRemote (t * testing.T ) {
@@ -52,8 +57,9 @@ func TestCoreVMNotRemote(t *testing.T) {
52
57
require .ErrorIs (err , block .ErrRemoteVMNotImplemented )
53
58
54
59
var blks [][]byte
55
- _ , err = proVM .BatchedParseBlock (context .Background (), blks )
56
- require .ErrorIs (err , block .ErrRemoteVMNotImplemented )
60
+ shouldBeEmpty , err := proVM .BatchedParseBlock (context .Background (), blks )
61
+ require .NoError (err )
62
+ require .Empty (shouldBeEmpty )
57
63
}
58
64
59
65
func TestGetAncestorsPreForkOnly (t * testing.T ) {
@@ -583,6 +589,102 @@ func TestBatchedParseBlockPreForkOnly(t *testing.T) {
583
589
require .Equal (builtBlk3 .ID (), res [2 ].ID ())
584
590
}
585
591
592
+ func TestBatchedParseBlockParallel (t * testing.T ) {
593
+ parentID := ids.ID {1 }
594
+ timestamp := time .Unix (123 , 0 )
595
+ pChainHeight := uint64 (2 )
596
+ chainID := ids .GenerateTestID ()
597
+
598
+ vm := VM {
599
+ ctx : & snow.Context {ChainID : chainID },
600
+ ChainVM : & block.TestVM {
601
+ ParseBlockF : func (_ context.Context , rawBlock []byte ) (snowman.Block , error ) {
602
+ return & snowmantest.Block {BytesV : rawBlock }, nil
603
+ },
604
+ },
605
+ }
606
+
607
+ tlsCert , err := staking .NewTLSCert ()
608
+ require .NoError (t , err )
609
+
610
+ cert , err := staking .ParseCertificate (tlsCert .Leaf .Raw )
611
+ require .NoError (t , err )
612
+ key := tlsCert .PrivateKey .(crypto.Signer )
613
+
614
+ blockThatCantBeParsed := snowmantest .BuildChild (snowmantest .Genesis )
615
+
616
+ blocksWithUnparsable := makeParseableBlocks (t , parentID , timestamp , pChainHeight , cert , chainID , key )
617
+ blocksWithUnparsable [50 ] = blockThatCantBeParsed .Bytes ()
618
+
619
+ parsableBlocks := makeParseableBlocks (t , parentID , timestamp , pChainHeight , cert , chainID , key )
620
+
621
+ for _ , testCase := range []struct {
622
+ name string
623
+ preForkIndex int
624
+ rawBlocks [][]byte
625
+ }{
626
+ {
627
+ name : "empty input" ,
628
+ rawBlocks : [][]byte {},
629
+ },
630
+ {
631
+ name : "pre-fork is somewhere in the middle" ,
632
+ rawBlocks : blocksWithUnparsable ,
633
+ preForkIndex : 50 ,
634
+ },
635
+ {
636
+ name : "all blocks are post fork" ,
637
+ rawBlocks : parsableBlocks ,
638
+ preForkIndex : len (parsableBlocks ),
639
+ },
640
+ } {
641
+ t .Run (testCase .name , func (t * testing.T ) {
642
+ require := require .New (t )
643
+ blocks , err := vm .BatchedParseBlock (context .Background (), testCase .rawBlocks )
644
+ require .NoError (err )
645
+
646
+ returnedBlockBytes := make ([][]byte , len (blocks ))
647
+ for i , block := range blocks {
648
+ returnedBlockBytes [i ] = block .Bytes ()
649
+ }
650
+ require .Equal (testCase .rawBlocks , returnedBlockBytes )
651
+
652
+ for i , block := range blocks {
653
+ if i < testCase .preForkIndex {
654
+ require .IsType (& postForkBlock {}, block )
655
+ } else {
656
+ require .IsType (& preForkBlock {}, block )
657
+ }
658
+ }
659
+ })
660
+ }
661
+ }
662
+
663
+ func makeParseableBlocks (t * testing.T , parentID ids.ID , timestamp time.Time , pChainHeight uint64 , cert * staking.Certificate , chainID ids.ID , key crypto.Signer ) [][]byte {
664
+ makeSignedBlock := func (i int ) []byte {
665
+ buff := binary .AppendVarint (nil , int64 (i ))
666
+
667
+ signedBlock , err := blockbuilder .Build (
668
+ parentID ,
669
+ timestamp ,
670
+ pChainHeight ,
671
+ cert ,
672
+ buff ,
673
+ chainID ,
674
+ key ,
675
+ )
676
+ require .NoError (t , err )
677
+
678
+ return signedBlock .Bytes ()
679
+ }
680
+
681
+ blockBytes := make ([][]byte , 100 )
682
+ for i := range blockBytes {
683
+ blockBytes [i ] = makeSignedBlock (i )
684
+ }
685
+ return blockBytes
686
+ }
687
+
586
688
func TestBatchedParseBlockPostForkOnly (t * testing.T ) {
587
689
require := require .New (t )
588
690
var (
0 commit comments