github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/blocks/eth1_data_test.go (about) 1 package blocks_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 types "github.com/prysmaticlabs/eth2-types" 9 "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" 10 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" 11 pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 12 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 13 "github.com/prysmaticlabs/prysm/shared/bytesutil" 14 "github.com/prysmaticlabs/prysm/shared/copyutil" 15 "github.com/prysmaticlabs/prysm/shared/params" 16 "github.com/prysmaticlabs/prysm/shared/testutil" 17 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 18 "github.com/prysmaticlabs/prysm/shared/testutil/require" 19 "google.golang.org/protobuf/proto" 20 ) 21 22 func FakeDeposits(n uint64) []*ethpb.Eth1Data { 23 deposits := make([]*ethpb.Eth1Data, n) 24 for i := uint64(0); i < n; i++ { 25 deposits[i] = ðpb.Eth1Data{ 26 DepositCount: 1, 27 DepositRoot: bytesutil.PadTo([]byte("root"), 32), 28 } 29 } 30 return deposits 31 } 32 33 func TestEth1DataHasEnoughSupport(t *testing.T) { 34 tests := []struct { 35 stateVotes []*ethpb.Eth1Data 36 data *ethpb.Eth1Data 37 hasSupport bool 38 votingPeriodLength types.Epoch 39 }{ 40 { 41 stateVotes: FakeDeposits(uint64(params.BeaconConfig().SlotsPerEpoch.Mul(4))), 42 data: ðpb.Eth1Data{ 43 DepositCount: 1, 44 DepositRoot: bytesutil.PadTo([]byte("root"), 32), 45 }, 46 hasSupport: true, 47 votingPeriodLength: 7, 48 }, { 49 stateVotes: FakeDeposits(uint64(params.BeaconConfig().SlotsPerEpoch.Mul(4))), 50 data: ðpb.Eth1Data{ 51 DepositCount: 1, 52 DepositRoot: bytesutil.PadTo([]byte("root"), 32), 53 }, 54 hasSupport: false, 55 votingPeriodLength: 8, 56 }, { 57 stateVotes: FakeDeposits(uint64(params.BeaconConfig().SlotsPerEpoch.Mul(4))), 58 data: ðpb.Eth1Data{ 59 DepositCount: 1, 60 DepositRoot: bytesutil.PadTo([]byte("root"), 32), 61 }, 62 hasSupport: false, 63 votingPeriodLength: 10, 64 }, 65 } 66 67 params.SetupTestConfigCleanup(t) 68 for i, tt := range tests { 69 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 70 c := params.BeaconConfig() 71 c.EpochsPerEth1VotingPeriod = tt.votingPeriodLength 72 params.OverrideBeaconConfig(c) 73 74 s, err := v1.InitializeFromProto(&pb.BeaconState{ 75 Eth1DataVotes: tt.stateVotes, 76 }) 77 require.NoError(t, err) 78 result, err := blocks.Eth1DataHasEnoughSupport(s, tt.data) 79 require.NoError(t, err) 80 81 if result != tt.hasSupport { 82 t.Errorf( 83 "blocks.Eth1DataHasEnoughSupport(%+v) = %t, wanted %t", 84 tt.data, 85 result, 86 tt.hasSupport, 87 ) 88 } 89 }) 90 } 91 } 92 93 func TestAreEth1DataEqual(t *testing.T) { 94 type args struct { 95 a *ethpb.Eth1Data 96 b *ethpb.Eth1Data 97 } 98 tests := []struct { 99 name string 100 args args 101 want bool 102 }{ 103 { 104 name: "true when both are nil", 105 args: args{ 106 a: nil, 107 b: nil, 108 }, 109 want: true, 110 }, 111 { 112 name: "false when only one is nil", 113 args: args{ 114 a: nil, 115 b: ðpb.Eth1Data{ 116 DepositRoot: make([]byte, 32), 117 DepositCount: 0, 118 BlockHash: make([]byte, 32), 119 }, 120 }, 121 want: false, 122 }, 123 { 124 name: "true when real equality", 125 args: args{ 126 a: ðpb.Eth1Data{ 127 DepositRoot: make([]byte, 32), 128 DepositCount: 0, 129 BlockHash: make([]byte, 32), 130 }, 131 b: ðpb.Eth1Data{ 132 DepositRoot: make([]byte, 32), 133 DepositCount: 0, 134 BlockHash: make([]byte, 32), 135 }, 136 }, 137 want: true, 138 }, 139 { 140 name: "false is field value differs", 141 args: args{ 142 a: ðpb.Eth1Data{ 143 DepositRoot: make([]byte, 32), 144 DepositCount: 0, 145 BlockHash: make([]byte, 32), 146 }, 147 b: ðpb.Eth1Data{ 148 DepositRoot: make([]byte, 32), 149 DepositCount: 64, 150 BlockHash: make([]byte, 32), 151 }, 152 }, 153 want: false, 154 }, 155 } 156 for _, tt := range tests { 157 t.Run(tt.name, func(t *testing.T) { 158 assert.Equal(t, tt.want, blocks.AreEth1DataEqual(tt.args.a, tt.args.b)) 159 }) 160 } 161 } 162 163 func TestProcessEth1Data_SetsCorrectly(t *testing.T) { 164 beaconState, err := v1.InitializeFromProto(&pb.BeaconState{ 165 Eth1DataVotes: []*ethpb.Eth1Data{}, 166 }) 167 require.NoError(t, err) 168 169 b := testutil.NewBeaconBlock() 170 b.Block = ðpb.BeaconBlock{ 171 Body: ðpb.BeaconBlockBody{ 172 Eth1Data: ðpb.Eth1Data{ 173 DepositRoot: []byte{2}, 174 BlockHash: []byte{3}, 175 }, 176 }, 177 } 178 179 period := uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))) 180 var ok bool 181 for i := uint64(0); i < period; i++ { 182 processedState, err := blocks.ProcessEth1DataInBlock(context.Background(), beaconState, b.Block.Body.Eth1Data) 183 require.NoError(t, err) 184 beaconState, ok = processedState.(*v1.BeaconState) 185 require.Equal(t, true, ok) 186 } 187 188 newETH1DataVotes := beaconState.Eth1DataVotes() 189 if len(newETH1DataVotes) <= 1 { 190 t.Error("Expected new ETH1 data votes to have length > 1") 191 } 192 if !proto.Equal(beaconState.Eth1Data(), copyutil.CopyETH1Data(b.Block.Body.Eth1Data)) { 193 t.Errorf( 194 "Expected latest eth1 data to have been set to %v, received %v", 195 b.Block.Body.Eth1Data, 196 beaconState.Eth1Data(), 197 ) 198 } 199 }