diff --git a/beacon-chain/core/blocks/block_operations.go b/beacon-chain/core/blocks/block_operations.go index 4b857ae5c..d028c6b8b 100644 --- a/beacon-chain/core/blocks/block_operations.go +++ b/beacon-chain/core/blocks/block_operations.go @@ -112,7 +112,11 @@ func ProcessEth1DataInBlock(beaconState *stateTrie.BeaconState, block *ethpb.Bea return beaconState, nil } -func areEth1DataEqual(a, b *ethpb.Eth1Data) bool { +// AreEth1DataEqual checks equality between two eth1 data objects. +func AreEth1DataEqual(a, b *ethpb.Eth1Data) bool { + if a == nil && b == nil { + return true + } if a == nil || b == nil { return false } @@ -130,7 +134,7 @@ func Eth1DataHasEnoughSupport(beaconState *stateTrie.BeaconState, data *ethpb.Et data = stateTrie.CopyETH1Data(data) for _, vote := range beaconState.Eth1DataVotes() { - if areEth1DataEqual(vote, data) { + if AreEth1DataEqual(vote, data) { voteCount++ } } diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go index fd23a6947..fe1c42dd9 100644 --- a/beacon-chain/core/blocks/block_operations_fuzz_test.go +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -90,8 +90,8 @@ func TestFuzzareEth1DataEqual_10000(t *testing.T) { for i := 0; i < 10000; i++ { fuzzer.Fuzz(eth1data) fuzzer.Fuzz(eth1data2) - areEth1DataEqual(eth1data, eth1data2) - areEth1DataEqual(eth1data, eth1data) + AreEth1DataEqual(eth1data, eth1data2) + AreEth1DataEqual(eth1data, eth1data) } } diff --git a/beacon-chain/core/blocks/block_operations_test.go b/beacon-chain/core/blocks/block_operations_test.go index 54b7be2c0..14b53dc33 100644 --- a/beacon-chain/core/blocks/block_operations_test.go +++ b/beacon-chain/core/blocks/block_operations_test.go @@ -2224,3 +2224,75 @@ func TestVerifyAttestations_HandlesPlannedFork(t *testing.T) { t.Fatal(err) } } + +func TestAreEth1DataEqual(t *testing.T) { + type args struct { + a *ethpb.Eth1Data + b *ethpb.Eth1Data + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "true when both are nil", + args: args{ + a: nil, + b: nil, + }, + want: true, + }, + { + name: "false when only one is nil", + args: args{ + a: nil, + b: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + }, + want: false, + }, + { + name: "true when real equality", + args: args{ + a: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + b: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + }, + want: true, + }, + { + name: "false is field value differs", + args: args{ + a: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + DepositCount: 0, + BlockHash: make([]byte, 32), + }, + b: ðpb.Eth1Data{ + DepositRoot: make([]byte, 32), + DepositCount: 64, + BlockHash: make([]byte, 32), + }, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := blocks.AreEth1DataEqual(tt.args.a, tt.args.b); got != tt.want { + t.Errorf("AreEth1DataEqual() = %v, want %v", got, tt.want) + } + }) + } +}