Disable txpool in eth wire protocol handler (#2540)
This commit is contained in:
parent
01b5c08763
commit
9dacfed943
|
@ -23,7 +23,7 @@ const
|
|||
|
||||
proc getPayloadBodyByHeader(db: CoreDbRef,
|
||||
header: common.BlockHeader,
|
||||
output: var seq[Opt[ExecutionPayloadBodyV1]]) =
|
||||
output: var seq[Opt[ExecutionPayloadBodyV1]]) {.gcsafe, raises:[].} =
|
||||
|
||||
var body: common.BlockBody
|
||||
if not db.getBlockBody(header, body):
|
||||
|
|
|
@ -35,7 +35,7 @@ proc getMultiKeys*(
|
|||
com: CommonRef,
|
||||
blockHeader: BlockHeader,
|
||||
statePostExecution: bool): MultiKeysRef
|
||||
{.raises: [RlpError, BlockNotFound, ValueError].} =
|
||||
{.raises: [BlockNotFound, ValueError].} =
|
||||
|
||||
let
|
||||
chainDB = com.db
|
||||
|
|
|
@ -11,15 +11,14 @@
|
|||
{.push raises: [].}
|
||||
|
||||
import
|
||||
std/[tables, times, hashes, sets, sequtils],
|
||||
std/[tables, times, hashes, sets],
|
||||
chronicles, chronos,
|
||||
stew/endians2,
|
||||
eth/p2p,
|
||||
eth/p2p/peer_pool,
|
||||
".."/[types, protocol],
|
||||
../protocol/eth/eth_types,
|
||||
../protocol/trace_config, # gossip noise control
|
||||
../../core/[chain, tx_pool, tx_pool/tx_item]
|
||||
../../core/[chain, tx_pool]
|
||||
|
||||
logScope:
|
||||
topics = "eth-wire"
|
||||
|
@ -27,44 +26,19 @@ logScope:
|
|||
type
|
||||
HashToTime = TableRef[Hash256, Time]
|
||||
|
||||
NewBlockHandler* = proc(
|
||||
arg: pointer,
|
||||
peer: Peer,
|
||||
blk: EthBlock,
|
||||
totalDifficulty: DifficultyInt) {.
|
||||
gcsafe, raises: [CatchableError].}
|
||||
|
||||
NewBlockHashesHandler* = proc(
|
||||
arg: pointer,
|
||||
peer: Peer,
|
||||
hashes: openArray[NewBlockHashesAnnounce]) {.
|
||||
gcsafe, raises: [CatchableError].}
|
||||
|
||||
NewBlockHandlerPair = object
|
||||
arg: pointer
|
||||
handler: NewBlockHandler
|
||||
|
||||
NewBlockHashesHandlerPair = object
|
||||
arg: pointer
|
||||
handler: NewBlockHashesHandler
|
||||
|
||||
EthWireRunState = enum
|
||||
Enabled
|
||||
Suspended
|
||||
NotAvailable
|
||||
|
||||
EthWireRef* = ref object of EthWireBase
|
||||
db: CoreDbRef
|
||||
chain: ChainRef
|
||||
txPool: TxPoolRef
|
||||
peerPool: PeerPool
|
||||
enableTxPool: EthWireRunState
|
||||
knownByPeer: Table[Peer, HashToTime]
|
||||
pending: HashSet[Hash256]
|
||||
lastCleanup: Time
|
||||
newBlockHandler: NewBlockHandlerPair
|
||||
newBlockHashesHandler: NewBlockHashesHandlerPair
|
||||
|
||||
const
|
||||
txpool_enabled = defined(enable_txpool_in_synchronizer)
|
||||
|
||||
when txpool_enabled:
|
||||
const
|
||||
NUM_PEERS_REBROADCAST_QUOTIENT = 4
|
||||
POOLED_STORAGE_TIME_LIMIT = initDuration(minutes = 20)
|
||||
|
@ -110,6 +84,7 @@ proc blockHeader(db: CoreDbRef,
|
|||
proc hash(peer: Peer): hashes.Hash {.used.} =
|
||||
hash(peer.remote)
|
||||
|
||||
when txpool_enabled:
|
||||
proc getPeers(ctx: EthWireRef, thisPeer: Peer): seq[Peer] =
|
||||
# do not send back tx or txhash to thisPeer
|
||||
for peer in peers(ctx.peerPool):
|
||||
|
@ -170,6 +145,7 @@ proc addToKnownByPeer(ctx: EthWireRef,
|
|||
# Private functions: async workers
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
when txpool_enabled:
|
||||
proc sendNewTxHashes(ctx: EthWireRef,
|
||||
txHashes: seq[Hash256],
|
||||
peers: seq[Peer]): Future[void] {.async.} =
|
||||
|
@ -257,11 +233,7 @@ proc fetchTransactions(ctx: EthWireRef, reqHashes: seq[Hash256], peer: Peer): Fu
|
|||
# ------------------------------------------------------------------------------
|
||||
|
||||
proc onPeerConnected(ctx: EthWireRef, peer: Peer) =
|
||||
if ctx.enableTxPool != Enabled:
|
||||
when trMissingOrDisabledGossipOk:
|
||||
notEnabled("onPeerConnected")
|
||||
return
|
||||
|
||||
when txpool_enabled:
|
||||
var txHashes = newSeqOfCap[Hash256](ctx.txPool.numTxs)
|
||||
for txHash, item in okPairs(ctx.txPool):
|
||||
txHashes.add txHash
|
||||
|
@ -273,6 +245,8 @@ proc onPeerConnected(ctx: EthWireRef, peer: Peer) =
|
|||
number = txHashes.len
|
||||
|
||||
asyncSpawn ctx.sendNewTxHashes(txHashes, @[peer])
|
||||
else:
|
||||
discard
|
||||
|
||||
proc onPeerDisconnected(ctx: EthWireRef, peer: Peer) =
|
||||
debug "remove peer from knownByPeer",
|
||||
|
@ -304,43 +278,11 @@ proc new*(_: type EthWireRef,
|
|||
chain: chain,
|
||||
txPool: txPool,
|
||||
peerPool: peerPool,
|
||||
enableTxPool: Enabled,
|
||||
lastCleanup: getTime())
|
||||
if txPool.isNil:
|
||||
ctx.enableTxPool = NotAvailable
|
||||
when trMissingOrDisabledGossipOk:
|
||||
trace "New eth handler, minimal/outbound support only"
|
||||
|
||||
ctx.setupPeerObserver()
|
||||
ctx
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Public functions: callbacks setters
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
proc setNewBlockHandler*(ctx: EthWireRef, handler: NewBlockHandler, arg: pointer) =
|
||||
ctx.newBlockHandler = NewBlockHandlerPair(
|
||||
arg: arg,
|
||||
handler: handler
|
||||
)
|
||||
|
||||
proc setNewBlockHashesHandler*(ctx: EthWireRef, handler: NewBlockHashesHandler, arg: pointer) =
|
||||
ctx.newBlockHashesHandler = NewBlockHashesHandlerPair(
|
||||
arg: arg,
|
||||
handler: handler
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Public getters/setters
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
proc `txPoolEnabled=`*(ctx: EthWireRef; ena: bool) {.gcsafe, raises: [].} =
|
||||
if ctx.enableTxPool != NotAvailable:
|
||||
ctx.enableTxPool = if ena: Enabled else: Suspended
|
||||
|
||||
proc txPoolEnabled*(ctx: EthWireRef): bool {.gcsafe, raises: [].} =
|
||||
ctx.enableTxPool == Enabled
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Public functions: eth wire protocol handlers
|
||||
# ------------------------------------------------------------------------------
|
||||
|
@ -391,6 +333,8 @@ method getPooledTxs*(ctx: EthWireRef,
|
|||
hashes: openArray[Hash256]):
|
||||
Result[seq[PooledTransaction], string]
|
||||
{.gcsafe.} =
|
||||
|
||||
when txpool_enabled:
|
||||
let txPool = ctx.txPool
|
||||
var list: seq[PooledTransaction]
|
||||
for txHash in hashes:
|
||||
|
@ -400,6 +344,8 @@ method getPooledTxs*(ctx: EthWireRef,
|
|||
else:
|
||||
trace "handlers.getPooledTxs: tx not found", txHash
|
||||
ok(list)
|
||||
else:
|
||||
err("txpool disabled")
|
||||
|
||||
method getBlockBodies*(ctx: EthWireRef,
|
||||
hashes: openArray[Hash256]):
|
||||
|
@ -446,12 +392,8 @@ method handleAnnouncedTxs*(ctx: EthWireRef,
|
|||
Result[void, string]
|
||||
{.gcsafe.} =
|
||||
|
||||
when txpool_enabled:
|
||||
try:
|
||||
if ctx.enableTxPool != Enabled:
|
||||
when trMissingOrDisabledGossipOk:
|
||||
notEnabled("handleAnnouncedTxs")
|
||||
return ok()
|
||||
|
||||
if txs.len == 0:
|
||||
return ok()
|
||||
|
||||
|
@ -495,6 +437,8 @@ method handleAnnouncedTxs*(ctx: EthWireRef,
|
|||
return ok()
|
||||
except CatchableError as exc:
|
||||
return err(exc.msg)
|
||||
else:
|
||||
err("txpool disabled")
|
||||
|
||||
method handleAnnouncedTxsHashes*(
|
||||
ctx: EthWireRef;
|
||||
|
@ -504,11 +448,6 @@ method handleAnnouncedTxsHashes*(
|
|||
txHashes: openArray[Hash256];
|
||||
): Result[void, string] =
|
||||
## `Eth68` method
|
||||
if ctx.enableTxPool != Enabled:
|
||||
when trMissingOrDisabledGossipOk:
|
||||
notEnabled("handleAnnouncedTxsHashes")
|
||||
return ok()
|
||||
|
||||
notImplemented "handleAnnouncedTxsHashes()/eth68"
|
||||
|
||||
method handleNewBlock*(ctx: EthWireRef,
|
||||
|
|
|
@ -19,17 +19,6 @@ import
|
|||
# ------------------------------------------------------------------------------
|
||||
# Public functions: convenience mappings for `eth`
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
proc setEthHandlerNewBlocksAndHashes*(
|
||||
node: EthereumNode;
|
||||
blockHandler: NewBlockHandler;
|
||||
hashesHandler: NewBlockHashesHandler;
|
||||
arg: pointer;
|
||||
) {.gcsafe, raises: [].} =
|
||||
let w = EthWireRef(node.protocolState protocol.eth)
|
||||
w.setNewBlockHandler(blockHandler, arg)
|
||||
w.setNewBlockHashesHandler(hashesHandler, arg)
|
||||
|
||||
proc addEthHandlerCapability*(
|
||||
node: EthereumNode;
|
||||
peerPool: PeerPool;
|
||||
|
|
Loading…
Reference in New Issue