change to int for max number of pending requests

This commit is contained in:
Anca Zamfir 2019-06-06 18:21:32 +02:00
parent 7e5e167066
commit bceb15e91a
No known key found for this signature in database
GPG Key ID: 61B09A558E4B6A7D
7 changed files with 26 additions and 26 deletions

View File

@ -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 {

View File

@ -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) {

View File

@ -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 {

View File

@ -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))
})
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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