From bceb15e91a43e7db47f6ec2256fd1137379f8d1f Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Thu, 6 Jun 2019 18:21:32 +0200 Subject: [PATCH] change to int for max number of pending requests --- blockchain/peer.go | 4 ++-- blockchain/peer_test.go | 4 ++-- blockchain/pool.go | 6 +++--- blockchain/pool_test.go | 14 +++++++------- blockchain/reactor.go | 4 ++-- blockchain/reactor_fsm.go | 4 ++-- blockchain/reactor_fsm_test.go | 16 ++++++++-------- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/blockchain/peer.go b/blockchain/peer.go index 00a8fc96..ac84ae65 100644 --- a/blockchain/peer.go +++ b/blockchain/peer.go @@ -25,7 +25,7 @@ type bpPeer struct { ID p2p.ID Height int64 // the peer reported height - NumPendingBlockRequests int32 // number of requests still waiting for block responses + NumPendingBlockRequests int // number of requests still waiting for block responses blocks map[int64]*types.Block // blocks received or expected to be received from this peer blockResponseTimer *time.Timer recvMonitor *flow.Monitor @@ -70,7 +70,7 @@ func (peer *bpPeer) Cleanup() { if peer.NumPendingBlockRequests != 0 { peer.logger.Info("peer with pending requests is being cleaned") } - if int32(len(peer.blocks))-peer.NumPendingBlockRequests != 0 { + if len(peer.blocks)-peer.NumPendingBlockRequests != 0 { peer.logger.Info("peer with pending blocks is being cleaned") } for h := range peer.blocks { diff --git a/blockchain/peer_test.go b/blockchain/peer_test.go index 8b764e9c..6b566775 100644 --- a/blockchain/peer_test.go +++ b/blockchain/peer_test.go @@ -84,12 +84,12 @@ func TestPeerRequestSent(t *testing.T) { peer.RequestSent(1) assert.NotNil(t, peer.recvMonitor) assert.NotNil(t, peer.blockResponseTimer) - assert.Equal(t, int32(1), peer.NumPendingBlockRequests) + assert.Equal(t, 1, peer.NumPendingBlockRequests) peer.RequestSent(1) assert.NotNil(t, peer.recvMonitor) assert.NotNil(t, peer.blockResponseTimer) - assert.Equal(t, int32(2), peer.NumPendingBlockRequests) + assert.Equal(t, 2, peer.NumPendingBlockRequests) } func TestPeerGetAndRemoveBlock(t *testing.T) { diff --git a/blockchain/pool.go b/blockchain/pool.go index 8bb9b943..85e81b94 100644 --- a/blockchain/pool.go +++ b/blockchain/pool.go @@ -168,7 +168,7 @@ func (pool *blockPool) removeBadPeers() { // Makes a batch of requests sorted by height up to a specified maximum. // The parameter 'maxNumRequests' includes the number of block requests already made. -func (pool *blockPool) makeRequestBatch(maxNumRequests int32) []int { +func (pool *blockPool) makeRequestBatch(maxNumRequests int) []int { pool.removeBadPeers() // At this point pool.requests may include heights for requests to be redone due to removal of peers: // - peers timed out or were removed by switch @@ -191,7 +191,7 @@ func (pool *blockPool) makeRequestBatch(maxNumRequests int32) []int { return heights } -func (pool *blockPool) MakeNextRequests(maxNumRequests int32) { +func (pool *blockPool) MakeNextRequests(maxNumRequests int) { heights := pool.makeRequestBatch(maxNumRequests) pool.logger.Info("makeNextRequests will make following requests", "number", len(heights), "heights", heights) @@ -208,7 +208,7 @@ func (pool *blockPool) MakeNextRequests(maxNumRequests int32) { func (pool *blockPool) sendRequest(height int64) bool { for _, peer := range pool.peers { - if peer.NumPendingBlockRequests >= int32(maxRequestsPerPeer) { + if peer.NumPendingBlockRequests >= maxRequestsPerPeer { continue } if peer.Height < height { diff --git a/blockchain/pool_test.go b/blockchain/pool_test.go index b7431adf..643ca027 100644 --- a/blockchain/pool_test.go +++ b/blockchain/pool_test.go @@ -21,7 +21,7 @@ type testBcR struct { } type testValues struct { - numRequestsSent int32 + numRequestsSent int } var testResults testValues @@ -283,7 +283,7 @@ func TestBlockPoolRemoveShortPeers(t *testing.T) { func TestBlockPoolSendRequestBatch(t *testing.T) { type testPeerResult struct { id p2p.ID - numPendingBlockRequests int32 + numPendingBlockRequests int } testBcR := newTestBcR() @@ -291,10 +291,10 @@ func TestBlockPoolSendRequestBatch(t *testing.T) { tests := []struct { name string pool *blockPool - maxRequestsPerPeer int32 + maxRequestsPerPeer int expRequests map[int64]bool expPeerResults []testPeerResult - expnumPendingBlockRequests int32 + expnumPendingBlockRequests int }{ { name: "one peer - send up to maxRequestsPerPeer block requests", @@ -321,16 +321,16 @@ func TestBlockPoolSendRequestBatch(t *testing.T) { resetPoolTestResults() var pool = tt.pool - maxRequestsPerPeer = int32(tt.maxRequestsPerPeer) + maxRequestsPerPeer = tt.maxRequestsPerPeer pool.MakeNextRequests(10) - assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*int32(len(pool.peers))) + assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*len(pool.peers)) for _, tPeer := range tt.expPeerResults { var peer = pool.peers[tPeer.id] assert.NotNil(t, peer) assert.Equal(t, tPeer.numPendingBlockRequests, peer.NumPendingBlockRequests) } - assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*int32(len(pool.peers))) + assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*len(pool.peers)) }) } diff --git a/blockchain/reactor.go b/blockchain/reactor.go index c78e3edb..7d4cb027 100644 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -35,9 +35,9 @@ const ( var ( // Maximum number of requests that can be pending per peer, i.e. for which requests have been sent but blocks // have not been received. - maxRequestsPerPeer int32 = 20 + maxRequestsPerPeer int = 20 // Maximum number of block requests for the reactor, pending or for which blocks have been received. - maxNumRequests int32 = 64 + maxNumRequests int = 64 ) type consensusReactor interface { diff --git a/blockchain/reactor_fsm.go b/blockchain/reactor_fsm.go index 38b56cef..f5d99ff2 100644 --- a/blockchain/reactor_fsm.go +++ b/blockchain/reactor_fsm.go @@ -61,7 +61,7 @@ type bReactorEventData struct { block *types.Block // for block response stateName string // for state timeout events length int // for block response event, length of received block, used to detect slow peers - maxNumRequests int32 // for request needed event, maximum number of pending requests + maxNumRequests int // for request needed event, maximum number of pending requests } // Blockchain Reactor Events (the input to the state machine) @@ -405,7 +405,7 @@ func (fsm *bReactorFSM) isCaughtUp() bool { return fsm.state == finished } -func (fsm *bReactorFSM) makeNextRequests(maxNumRequests int32) { +func (fsm *bReactorFSM) makeNextRequests(maxNumRequests int) { fsm.pool.MakeNextRequests(maxNumRequests) } diff --git a/blockchain/reactor_fsm_test.go b/blockchain/reactor_fsm_test.go index 862280df..a7de6bc5 100644 --- a/blockchain/reactor_fsm_test.go +++ b/blockchain/reactor_fsm_test.go @@ -27,7 +27,7 @@ type testReactor struct { logger log.Logger fsm *bReactorFSM numStatusRequests int - numBlockRequests int32 + numBlockRequests int lastBlockRequest lastBlockRequestT lastPeerError lastPeerErrorT stateTimerStarts map[string]int @@ -109,7 +109,7 @@ func makeStepStatusEv(current, expected string, peerID p2p.ID, height int64, err errWanted: err} } -func makeStepMakeRequestsEv(current, expected string, maxPendingRequests int32) fsmStepTestValues { +func makeStepMakeRequestsEv(current, expected string, maxPendingRequests int) fsmStepTestValues { return fsmStepTestValues{ currentState: current, event: makeRequestsEv, @@ -120,7 +120,7 @@ func makeStepMakeRequestsEv(current, expected string, maxPendingRequests int32) } func makeStepMakeRequestsEvErrored(current, expected string, - maxPendingRequests int32, err error, peersRemoved []p2p.ID) fsmStepTestValues { + maxPendingRequests int, err error, peersRemoved []p2p.ID) fsmStepTestValues { return fsmStepTestValues{ currentState: current, event: makeRequestsEv, @@ -204,8 +204,8 @@ func fixBlockResponseEvStep(step *fsmStepTestValues, testBcR *testReactor) { type testFields struct { name string startingHeight int64 - maxRequestsPerPeer int32 - maxPendingRequests int32 + maxRequestsPerPeer int + maxPendingRequests int steps []fsmStepTestValues } @@ -785,7 +785,7 @@ func TestFSMPeerStateTimeoutEvent(t *testing.T) { } func makeCorrectTransitionSequence(startingHeight int64, numBlocks int64, numPeers int, randomPeerHeights bool, - maxRequestsPerPeer int32, maxPendingRequests int32) testFields { + maxRequestsPerPeer int, maxPendingRequests int) testFields { // Generate numPeers peers with random or numBlocks heights according to the randomPeerHeights flag. peerHeights := make([]int64, numPeers) @@ -888,10 +888,10 @@ func makeCorrectTransitionSequenceWithRandomParameters() testFields { startingHeight := int64(cmn.RandIntn(maxStartingHeightTest) + 1) // Generate the number of requests per peer. - maxRequestsPerPeer := int32(cmn.RandIntn(maxRequestsPerPeerTest) + 1) + maxRequestsPerPeer := cmn.RandIntn(maxRequestsPerPeerTest) + 1 // Generate the maximum number of total pending requests, >= maxRequestsPerPeer. - maxPendingRequests := int32(cmn.RandIntn(maxTotalPendingRequestsTest-int(maxRequestsPerPeer))) + maxRequestsPerPeer + maxPendingRequests := cmn.RandIntn(maxTotalPendingRequestsTest-int(maxRequestsPerPeer)) + maxRequestsPerPeer // Generate the number of blocks to be synced. numBlocks := int64(cmn.RandIntn(maxNumBlocksInChainTest)) + startingHeight