Browse Source

mempool: IDs issue fixes (#7763)

pull/7780/head
JayT106 3 years ago
committed by GitHub
parent
commit
97d47b5263
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 5 deletions
  1. +8
    -0
      internal/mempool/ids.go
  2. +67
    -2
      internal/mempool/ids_test.go
  3. +5
    -3
      internal/mempool/reactor_test.go

+ 8
- 0
internal/mempool/ids.go View File

@ -30,6 +30,11 @@ func (ids *IDs) ReserveForPeer(peerID types.NodeID) {
ids.mtx.Lock() ids.mtx.Lock()
defer ids.mtx.Unlock() defer ids.mtx.Unlock()
if _, ok := ids.peerMap[peerID]; ok {
// the peer has been reserved
return
}
curID := ids.nextPeerID() curID := ids.nextPeerID()
ids.peerMap[peerID] = curID ids.peerMap[peerID] = curID
ids.activeIDs[curID] = struct{}{} ids.activeIDs[curID] = struct{}{}
@ -44,6 +49,9 @@ func (ids *IDs) Reclaim(peerID types.NodeID) {
if ok { if ok {
delete(ids.activeIDs, removedID) delete(ids.activeIDs, removedID)
delete(ids.peerMap, peerID) delete(ids.peerMap, peerID)
if removedID < ids.nextID {
ids.nextID = removedID
}
} }
} }


+ 67
- 2
internal/mempool/ids_test.go View File

@ -12,12 +12,77 @@ func TestMempoolIDsBasic(t *testing.T) {
peerID, err := types.NewNodeID("0011223344556677889900112233445566778899") peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 0, ids.GetForPeer(peerID))
ids.ReserveForPeer(peerID) ids.ReserveForPeer(peerID)
require.EqualValues(t, 1, ids.GetForPeer(peerID)) require.EqualValues(t, 1, ids.GetForPeer(peerID))
ids.Reclaim(peerID) ids.Reclaim(peerID)
require.EqualValues(t, 0, ids.GetForPeer(peerID))
ids.ReserveForPeer(peerID) ids.ReserveForPeer(peerID)
require.EqualValues(t, 2, ids.GetForPeer(peerID))
ids.Reclaim(peerID)
require.EqualValues(t, 1, ids.GetForPeer(peerID))
}
func TestMempoolIDsPeerDupReserve(t *testing.T) {
ids := NewMempoolIDs()
peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
require.NoError(t, err)
require.EqualValues(t, 0, ids.GetForPeer(peerID))
ids.ReserveForPeer(peerID)
require.EqualValues(t, 1, ids.GetForPeer(peerID))
ids.ReserveForPeer(peerID)
require.EqualValues(t, 1, ids.GetForPeer(peerID))
}
func TestMempoolIDs2Peers(t *testing.T) {
ids := NewMempoolIDs()
peer1ID, _ := types.NewNodeID("0011223344556677889900112233445566778899")
require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
ids.ReserveForPeer(peer1ID)
require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
ids.Reclaim(peer1ID)
require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
peer2ID, _ := types.NewNodeID("1011223344556677889900112233445566778899")
ids.ReserveForPeer(peer2ID)
require.EqualValues(t, 1, ids.GetForPeer(peer2ID))
ids.ReserveForPeer(peer1ID)
require.EqualValues(t, 2, ids.GetForPeer(peer1ID))
}
func TestMempoolIDsNextExistID(t *testing.T) {
ids := NewMempoolIDs()
peer1ID, _ := types.NewNodeID("0011223344556677889900112233445566778899")
ids.ReserveForPeer(peer1ID)
require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
peer2ID, _ := types.NewNodeID("1011223344556677889900112233445566778899")
ids.ReserveForPeer(peer2ID)
require.EqualValues(t, 2, ids.GetForPeer(peer2ID))
peer3ID, _ := types.NewNodeID("2011223344556677889900112233445566778899")
ids.ReserveForPeer(peer3ID)
require.EqualValues(t, 3, ids.GetForPeer(peer3ID))
ids.Reclaim(peer1ID)
require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
ids.Reclaim(peer3ID)
require.EqualValues(t, 0, ids.GetForPeer(peer3ID))
ids.ReserveForPeer(peer1ID)
require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
ids.ReserveForPeer(peer3ID)
require.EqualValues(t, 3, ids.GetForPeer(peer3ID))
} }

+ 5
- 3
internal/mempool/reactor_test.go View File

@ -2,6 +2,7 @@ package mempool
import ( import (
"context" "context"
"fmt"
"os" "os"
"runtime" "runtime"
"strings" "strings"
@ -370,13 +371,14 @@ func TestMempoolIDsPanicsIfNodeRequestsOvermaxActiveIDs(t *testing.T) {
// 0 is already reserved for UnknownPeerID // 0 is already reserved for UnknownPeerID
ids := NewMempoolIDs() ids := NewMempoolIDs()
peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
require.NoError(t, err)
for i := 0; i < MaxActiveIDs-1; i++ { for i := 0; i < MaxActiveIDs-1; i++ {
peerID, err := types.NewNodeID(fmt.Sprintf("%040d", i))
require.NoError(t, err)
ids.ReserveForPeer(peerID) ids.ReserveForPeer(peerID)
} }
peerID, err := types.NewNodeID(fmt.Sprintf("%040d", MaxActiveIDs-1))
require.NoError(t, err)
require.Panics(t, func() { require.Panics(t, func() {
ids.ReserveForPeer(peerID) ids.ReserveForPeer(peerID)
}) })


Loading…
Cancel
Save