QSP-39 AreEth1DataEqual Should Return True If Both Nil (#6372)

* resolve small core issue

* add tests

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Raul Jordan 2020-06-25 17:01:58 -05:00 committed by GitHub
parent ac69dbc2f8
commit 6b27452947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 4 deletions

View File

@ -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++
}
}

View File

@ -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)
}
}

View File

@ -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: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
},
want: false,
},
{
name: "true when real equality",
args: args{
a: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
b: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
},
want: true,
},
{
name: "false is field value differs",
args: args{
a: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
b: &ethpb.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)
}
})
}
}