From 47bb927029d578e8c1f4cb163e75e67f20368a9d Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 21 Feb 2020 21:49:42 +0800 Subject: [PATCH] Fix Fork Copying (#4922) * add fix and reg test * goimports --- beacon-chain/state/BUILD.bazel | 1 + beacon-chain/state/getters.go | 4 ++-- beacon-chain/state/types_test.go | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/beacon-chain/state/BUILD.bazel b/beacon-chain/state/BUILD.bazel index 99a5dbd3b..f356f7401 100644 --- a/beacon-chain/state/BUILD.bazel +++ b/beacon-chain/state/BUILD.bazel @@ -46,5 +46,6 @@ go_test( "//shared/stateutil:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", + "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], ) diff --git a/beacon-chain/state/getters.go b/beacon-chain/state/getters.go index ba4594df5..c605fcfdd 100644 --- a/beacon-chain/state/getters.go +++ b/beacon-chain/state/getters.go @@ -135,8 +135,8 @@ func (b *BeaconState) Fork() *pbp2p.Fork { prevVersion := make([]byte, len(b.state.Fork.PreviousVersion)) copy(prevVersion, b.state.Fork.PreviousVersion) - currVersion := make([]byte, len(b.state.Fork.PreviousVersion)) - copy(currVersion, b.state.Fork.PreviousVersion) + currVersion := make([]byte, len(b.state.Fork.CurrentVersion)) + copy(currVersion, b.state.Fork.CurrentVersion) return &pbp2p.Fork{ PreviousVersion: prevVersion, CurrentVersion: currVersion, diff --git a/beacon-chain/state/types_test.go b/beacon-chain/state/types_test.go index f51935012..6e7cd80bc 100644 --- a/beacon-chain/state/types_test.go +++ b/beacon-chain/state/types_test.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/go-ssz" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" @@ -226,3 +227,24 @@ func TestBeaconState_ImmutabilityWithSharedResources(t *testing.T) { t.Fatal("Expected a.BlockRoots() to be different from b.BlockRoots()") } } + +func TestForkManualCopy_OK(t *testing.T) { + params.UseMinimalConfig() + genesis := setupGenesisState(t, 64) + a, err := stateTrie.InitializeFromProto(genesis) + if err != nil { + t.Fatal(err) + } + wantedFork := &pb.Fork{ + PreviousVersion: []byte{'a', 'b', 'c'}, + CurrentVersion: []byte{'d', 'e', 'f'}, + Epoch: 0, + } + a.SetFork(wantedFork) + + newState := a.CloneInnerState() + if !ssz.DeepEqual(newState.Fork, wantedFork) { + t.Errorf("Wanted %v but got %v", wantedFork, newState.Fork) + } + +}