github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/v1/state_test.go (about) 1 package v1 2 3 import ( 4 "strconv" 5 "sync" 6 "testing" 7 8 "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" 9 pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 10 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 11 "github.com/prysmaticlabs/prysm/shared/bytesutil" 12 "github.com/prysmaticlabs/prysm/shared/params" 13 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 14 ) 15 16 func TestValidatorMap_DistinctCopy(t *testing.T) { 17 count := uint64(100) 18 vals := make([]*ethpb.Validator, 0, count) 19 for i := uint64(1); i < count; i++ { 20 someRoot := [32]byte{} 21 someKey := [48]byte{} 22 copy(someRoot[:], strconv.Itoa(int(i))) 23 copy(someKey[:], strconv.Itoa(int(i))) 24 vals = append(vals, ðpb.Validator{ 25 PublicKey: someKey[:], 26 WithdrawalCredentials: someRoot[:], 27 EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, 28 Slashed: false, 29 ActivationEligibilityEpoch: 1, 30 ActivationEpoch: 1, 31 ExitEpoch: 1, 32 WithdrawableEpoch: 1, 33 }) 34 } 35 handler := stateutil.NewValMapHandler(vals) 36 newHandler := handler.Copy() 37 wantedPubkey := strconv.Itoa(22) 38 handler.Set(bytesutil.ToBytes48([]byte(wantedPubkey)), 27) 39 val1, _ := handler.Get(bytesutil.ToBytes48([]byte(wantedPubkey))) 40 val2, _ := newHandler.Get(bytesutil.ToBytes48([]byte(wantedPubkey))) 41 assert.NotEqual(t, val1, val2, "Values are supposed to be unequal due to copy") 42 } 43 44 func TestBeaconState_NoDeadlock(t *testing.T) { 45 count := uint64(100) 46 vals := make([]*ethpb.Validator, 0, count) 47 for i := uint64(1); i < count; i++ { 48 someRoot := [32]byte{} 49 someKey := [48]byte{} 50 copy(someRoot[:], strconv.Itoa(int(i))) 51 copy(someKey[:], strconv.Itoa(int(i))) 52 vals = append(vals, ðpb.Validator{ 53 PublicKey: someKey[:], 54 WithdrawalCredentials: someRoot[:], 55 EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance, 56 Slashed: false, 57 ActivationEligibilityEpoch: 1, 58 ActivationEpoch: 1, 59 ExitEpoch: 1, 60 WithdrawableEpoch: 1, 61 }) 62 } 63 st, err := InitializeFromProtoUnsafe(&pb.BeaconState{ 64 Validators: vals, 65 }) 66 assert.NoError(t, err) 67 68 wg := new(sync.WaitGroup) 69 70 wg.Add(1) 71 go func() { 72 // Continuously lock and unlock the state 73 // by acquiring the lock. 74 for i := 0; i < 1000; i++ { 75 for _, f := range st.stateFieldLeaves { 76 f.Lock() 77 if len(f.fieldLayers) == 0 { 78 f.fieldLayers = make([][]*[32]byte, 10) 79 } 80 f.Unlock() 81 f.reference.AddRef() 82 } 83 } 84 wg.Done() 85 }() 86 // Constantly read from the offending portion 87 // of the code to ensure there is no possible 88 // recursive read locking. 89 for i := 0; i < 1000; i++ { 90 go func() { 91 _ = st.FieldReferencesCount() 92 }() 93 } 94 // Test will not terminate in the event of a deadlock. 95 wg.Wait() 96 } 97 98 func TestStateTrie_IsNil(t *testing.T) { 99 var emptyState *BeaconState 100 assert.Equal(t, true, emptyState.IsNil()) 101 102 emptyProto := &BeaconState{state: nil} 103 assert.Equal(t, true, emptyProto.IsNil()) 104 105 nonNilState := &BeaconState{state: &pb.BeaconState{}} 106 assert.Equal(t, false, nonNilState.IsNil()) 107 }