diff --git a/beacon-chain/state/getters.go b/beacon-chain/state/getters.go index 7484fbe709d5..87c8e97f15b6 100644 --- a/beacon-chain/state/getters.go +++ b/beacon-chain/state/getters.go @@ -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[:] @@ -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 } diff --git a/beacon-chain/state/types_test.go b/beacon-chain/state/types_test.go index 5c11ebc8f715..53b63bc60c3d 100644 --- a/beacon-chain/state/types_test.go +++ b/beacon-chain/state/types_test.go @@ -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" @@ -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 { @@ -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++ {