github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/blockchain/weak_subjectivity_checks_test.go (about) 1 package blockchain 2 3 import ( 4 "context" 5 "testing" 6 7 types "github.com/prysmaticlabs/eth2-types" 8 testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" 9 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 10 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper" 11 "github.com/prysmaticlabs/prysm/shared/bytesutil" 12 "github.com/prysmaticlabs/prysm/shared/testutil" 13 "github.com/prysmaticlabs/prysm/shared/testutil/require" 14 ) 15 16 func TestService_VerifyWeakSubjectivityRoot(t *testing.T) { 17 beaconDB := testDB.SetupDB(t) 18 19 b := testutil.NewBeaconBlock() 20 b.Block.Slot = 32 21 require.NoError(t, beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(b))) 22 r, err := b.Block.HashTreeRoot() 23 require.NoError(t, err) 24 tests := []struct { 25 wsVerified bool 26 wantErr bool 27 checkpt *ethpb.Checkpoint 28 finalizedEpoch types.Epoch 29 errString string 30 name string 31 }{ 32 { 33 name: "nil root and epoch", 34 wantErr: false, 35 }, 36 { 37 name: "already verified", 38 checkpt: ðpb.Checkpoint{Epoch: 2}, 39 finalizedEpoch: 2, 40 wsVerified: true, 41 wantErr: false, 42 }, 43 { 44 name: "not yet to verify, ws epoch higher than finalized epoch", 45 checkpt: ðpb.Checkpoint{Epoch: 2}, 46 finalizedEpoch: 1, 47 wantErr: false, 48 }, 49 { 50 name: "can't find the block in DB", 51 checkpt: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'a'}, 32), Epoch: 1}, 52 finalizedEpoch: 3, 53 wantErr: true, 54 errString: "node does not have root in DB", 55 }, 56 { 57 name: "can't find the block corresponds to ws epoch in DB", 58 checkpt: ðpb.Checkpoint{Root: r[:], Epoch: 2}, // Root belongs in epoch 1. 59 finalizedEpoch: 3, 60 wantErr: true, 61 errString: "node does not have root in db corresponding to epoch", 62 }, 63 { 64 name: "can verify and pass", 65 checkpt: ðpb.Checkpoint{Root: r[:], Epoch: 1}, 66 finalizedEpoch: 3, 67 wantErr: false, 68 }, 69 } 70 for _, tt := range tests { 71 t.Run(tt.name, func(t *testing.T) { 72 s := &Service{ 73 cfg: &Config{BeaconDB: beaconDB, WeakSubjectivityCheckpt: tt.checkpt}, 74 wsVerified: tt.wsVerified, 75 finalizedCheckpt: ðpb.Checkpoint{Epoch: tt.finalizedEpoch}, 76 } 77 if err := s.VerifyWeakSubjectivityRoot(context.Background()); (err != nil) != tt.wantErr { 78 require.ErrorContains(t, tt.errString, err) 79 } 80 }) 81 } 82 }