From 1b840f8854018f27cbe836d19bfe9c46abd41f1f Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 22 Apr 2020 14:25:21 -0700 Subject: [PATCH 1/5] Add a 2 second timeout to fetching eth1data --- beacon-chain/rpc/validator/proposer.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index c716746c1f8a..1fedd4995a6e 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -154,6 +154,9 @@ func (vs *Server) ProposeBlock(ctx context.Context, blk *ethpb.SignedBeaconBlock // - Subtract that eth1block.number by ETH1_FOLLOW_DISTANCE. // - This is the eth1block to use for the block proposal. func (vs *Server) eth1Data(ctx context.Context, slot uint64) (*ethpb.Eth1Data, error) { + ctx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + if vs.MockEth1Votes { return vs.mockETH1DataVote(ctx, slot) } @@ -169,11 +172,13 @@ func (vs *Server) eth1Data(ctx context.Context, slot uint64) (*ethpb.Eth1Data, e // Look up most recent block up to timestamp blockNumber, err := vs.Eth1BlockFetcher.BlockNumberByTimestamp(ctx, eth1VotingPeriodStartTime) if err != nil { - return nil, errors.Wrap(err, "could not get block number from timestamp") + log.WithError(err).Error("Failed to get block number from timestamp") + return vs.randomETH1DataVote(ctx, slot) } eth1Data, err := vs.defaultEth1DataResponse(ctx, blockNumber) if err != nil { - return nil, errors.Wrap(err, "could not get eth1 data from block number") + log.WithError(err).Error("Failed to get eth1 data from block number") + return vs.randomETH1DataVote(ctx, slot) } return eth1Data, nil @@ -362,6 +367,9 @@ func (vs *Server) canonicalEth1Data(ctx context.Context, beaconState *stateTrie. // hash of eth1 block hash that is FOLLOW_DISTANCE back from its // latest block. func (vs *Server) defaultEth1DataResponse(ctx context.Context, currentHeight *big.Int) (*ethpb.Eth1Data, error) { + if ctx.Err() != nil { + return nil, ctx.Err() + } eth1FollowDistance := int64(params.BeaconConfig().Eth1FollowDistance) ancestorHeight := big.NewInt(0).Sub(currentHeight, big.NewInt(eth1FollowDistance)) blockHash, err := vs.Eth1BlockFetcher.BlockHashByHeight(ctx, ancestorHeight) From 6ddab97d9af5dbc0cfdd65e7d8701452aa924ab1 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 22 Apr 2020 14:28:51 -0700 Subject: [PATCH 2/5] goimports --- beacon-chain/rpc/validator/proposer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index 1fedd4995a6e..a85e51f0fa3a 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -5,6 +5,7 @@ import ( "fmt" "math/big" "math/rand" + "time" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" From e9e7911eaa31417d3c049b48e46c4e636696433d Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 22 Apr 2020 14:29:31 -0700 Subject: [PATCH 3/5] fix --- beacon-chain/rpc/validator/proposer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index a85e51f0fa3a..00e5a4cf51da 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -174,12 +174,12 @@ func (vs *Server) eth1Data(ctx context.Context, slot uint64) (*ethpb.Eth1Data, e blockNumber, err := vs.Eth1BlockFetcher.BlockNumberByTimestamp(ctx, eth1VotingPeriodStartTime) if err != nil { log.WithError(err).Error("Failed to get block number from timestamp") - return vs.randomETH1DataVote(ctx, slot) + return vs.randomETH1DataVote(ctx) } eth1Data, err := vs.defaultEth1DataResponse(ctx, blockNumber) if err != nil { log.WithError(err).Error("Failed to get eth1 data from block number") - return vs.randomETH1DataVote(ctx, slot) + return vs.randomETH1DataVote(ctx) } return eth1Data, nil From 1a201dcb5f6c4ad7e023e4e4273b70b11085b8fe Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 22 Apr 2020 14:52:33 -0700 Subject: [PATCH 4/5] use package const --- beacon-chain/rpc/validator/proposer.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/beacon-chain/rpc/validator/proposer.go b/beacon-chain/rpc/validator/proposer.go index 00e5a4cf51da..2dcfa504ba49 100644 --- a/beacon-chain/rpc/validator/proposer.go +++ b/beacon-chain/rpc/validator/proposer.go @@ -31,6 +31,8 @@ import ( // eth1DataNotification is a latch to stop flooding logs with the same warning. var eth1DataNotification bool +const eth1dataTimeout = 2 * time.Second + // GetBlock is called by a proposer during its assigned slot to request a block to sign // by passing in the slot and the signed randao reveal of the slot. func (vs *Server) GetBlock(ctx context.Context, req *ethpb.BlockRequest) (*ethpb.BeaconBlock, error) { @@ -155,7 +157,7 @@ func (vs *Server) ProposeBlock(ctx context.Context, blk *ethpb.SignedBeaconBlock // - Subtract that eth1block.number by ETH1_FOLLOW_DISTANCE. // - This is the eth1block to use for the block proposal. func (vs *Server) eth1Data(ctx context.Context, slot uint64) (*ethpb.Eth1Data, error) { - ctx, cancel := context.WithTimeout(ctx, 2*time.Second) + ctx, cancel := context.WithTimeout(ctx, eth1dataTimeout) defer cancel() if vs.MockEth1Votes { From 8aa9117fd9ea502239bc36feb278ee6724a08033 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 22 Apr 2020 19:17:26 -0700 Subject: [PATCH 5/5] fix test --- beacon-chain/rpc/validator/proposer_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/validator/proposer_test.go b/beacon-chain/rpc/validator/proposer_test.go index fe2856b01125..b5ef06e079c8 100644 --- a/beacon-chain/rpc/validator/proposer_test.go +++ b/beacon-chain/rpc/validator/proposer_test.go @@ -5,7 +5,6 @@ import ( "context" "math/big" "reflect" - "strings" "testing" "github.com/gogo/protobuf/proto" @@ -1130,9 +1129,8 @@ func TestEth1Data_EmptyVotesFetchBlockHashFailure(t *testing.T) { BlockReceiver: &mock.ChainService{State: beaconState}, HeadFetcher: &mock.ChainService{State: beaconState}, } - want := "could not fetch ETH1_FOLLOW_DISTANCE ancestor" - if _, err := proposerServer.eth1Data(context.Background(), beaconState.Slot()+1); err == nil || !strings.Contains(err.Error(), want) { - t.Errorf("Expected error %v, received %v", want, err) + if _, err := proposerServer.eth1Data(context.Background(), beaconState.Slot()+1); err != nil { + t.Errorf("A failed request should not have returned an error, got %v", err) } }