Skip to content

Commit

Permalink
fixed up some bugs in the clone comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
rauljordan committed Jan 21, 2020
1 parent dd68b33 commit 354ed4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
8 changes: 4 additions & 4 deletions beacon-chain/state/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (b *BeaconState) LatestBlockHeader() *ethpb.BeaconBlockHeader {
var stateRoot [32]byte

copy(parentRoot[:], b.state.LatestBlockHeader.ParentRoot)
copy(bodyRoot[:], b.state.LatestBlockHeader.StateRoot)
copy(stateRoot[:], b.state.LatestBlockHeader.BodyRoot)
copy(bodyRoot[:], b.state.LatestBlockHeader.BodyRoot)
copy(stateRoot[:], b.state.LatestBlockHeader.StateRoot)
hdr.ParentRoot = parentRoot[:]
hdr.BodyRoot = bodyRoot[:]
hdr.StateRoot = stateRoot[:]
Expand Down Expand Up @@ -259,8 +259,8 @@ func (b *BeaconState) JustificationBits() bitfield.Bitvector4 {
if b.state.JustificationBits == nil {
return nil
}
res := bitfield.Bitvector4{}
copy(res, b.state.JustificationBits)
res := make([]byte, len(b.state.JustificationBits.Bytes()))
copy(res, b.state.JustificationBits.Bytes())
return res
}

Expand Down
31 changes: 30 additions & 1 deletion beacon-chain/state/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"

"github.com/prysmaticlabs/prysm/shared/interop"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/stateutil"
Expand All @@ -20,6 +19,12 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cloned := proto.Clone(genesis).(*pb.BeaconState)
custom := customState.Clone()
if !proto.Equal(cloned, custom) {
t.Fatal("Cloned states did not match")
}

r1 := customState.HashTreeRoot()
r2, err := stateutil.HashTreeRootState(genesis)
if err != nil {
Expand Down Expand Up @@ -117,6 +122,30 @@ func BenchmarkCloneValidators_Manual(b *testing.B) {
}
}

func BenchmarkStateClone_Proto(b *testing.B) {
b.StopTimer()
params.UseMinimalConfig()
genesis := setupGenesisState(b, 64)
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = proto.Clone(genesis).(*pb.BeaconState)
}
}

func BenchmarkStateClone_Manual(b *testing.B) {
b.StopTimer()
params.UseMinimalConfig()
genesis := setupGenesisState(b, 64)
st, err := InitializeFromProto(genesis)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = st.Clone()
}
}

func cloneValidatorsWithProto(vals []*ethpb.Validator) []*ethpb.Validator {
res := make([]*ethpb.Validator, len(vals))
for i := 0; i < len(res); i++ {
Expand Down

0 comments on commit 354ed4e

Please sign in to comment.