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