From d6d6fbab05634069bc1bc0ef74dff8ad9f4c7e0f Mon Sep 17 00:00:00 2001 From: nisdas Date: Fri, 10 Apr 2020 01:43:31 +0800 Subject: [PATCH 1/2] fix overflow --- beacon-chain/sync/BUILD.bazel | 1 + beacon-chain/sync/service.go | 4 ++-- beacon-chain/sync/service_test.go | 35 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 beacon-chain/sync/service_test.go diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index 2de56737e3ec..3fe9df374e27 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -100,6 +100,7 @@ go_test( "rpc_ping_test.go", "rpc_status_test.go", "rpc_test.go", + "service_test.go", "subscriber_beacon_aggregate_proof_test.go", "subscriber_beacon_blocks_test.go", "subscriber_committee_index_beacon_attestation_test.go", diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index bc14e292940e..af1de1211b51 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -160,8 +160,8 @@ func (r *Service) Status() error { } // If our head slot is on a previous epoch and our peers are reporting their head block are // in the most recent epoch, then we might be out of sync. - if headEpoch := helpers.SlotToEpoch(r.chain.HeadSlot()); headEpoch < helpers.SlotToEpoch(r.chain.CurrentSlot())-1 && - headEpoch < r.p2p.Peers().CurrentEpoch()-1 { + if headEpoch := helpers.SlotToEpoch(r.chain.HeadSlot()); headEpoch+1 < helpers.SlotToEpoch(r.chain.CurrentSlot()) && + headEpoch+1 < r.p2p.Peers().CurrentEpoch() { return errors.New("out of sync") } } diff --git a/beacon-chain/sync/service_test.go b/beacon-chain/sync/service_test.go new file mode 100644 index 000000000000..578165823915 --- /dev/null +++ b/beacon-chain/sync/service_test.go @@ -0,0 +1,35 @@ +package sync + +import ( + "testing" + "time" + + stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + + mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + + p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" + mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" +) + +func TestService_StatusZeroEpoch(t *testing.T) { + bState, err := stateTrie.InitializeFromProto(&pb.BeaconState{Slot: 0}) + if err != nil { + t.Fatal(err) + } + r := &Service{ + p2p: p2ptest.NewTestP2P(t), + initialSync: new(mockSync.Sync), + chain: &mockChain.ChainService{ + Genesis: time.Now(), + State: bState, + }, + } + r.chainStarted = true + + err = r.Status() + if err != nil { + t.Errorf("Wanted non failing status but got: %v", err) + } +} From 4a0f968da60d8590c0c4808f5a486f37fa19088d Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 10 Apr 2020 01:47:28 +0800 Subject: [PATCH 2/2] Apply suggestions from code review --- beacon-chain/sync/service_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/beacon-chain/sync/service_test.go b/beacon-chain/sync/service_test.go index 578165823915..2bae57c3da40 100644 --- a/beacon-chain/sync/service_test.go +++ b/beacon-chain/sync/service_test.go @@ -6,9 +6,7 @@ import ( stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" - p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" )