fix withdrawals problem in tx-pool

This commit is contained in:
jangko 2023-05-22 17:55:19 +07:00 committed by zah
parent 27393e9420
commit 9868eceed1
5 changed files with 46 additions and 7 deletions

View File

@ -11,9 +11,9 @@ import
type
CasperRef* = ref object
feeRecipient* : EthAddress
timestamp* : EthTime
prevRandao* : Hash256
feeRecipient : EthAddress
timestamp : EthTime
prevRandao : Hash256
proc prepare*(ctx: CasperRef, header: var BlockHeader) =
header.coinbase = ctx.feeRecipient
@ -21,7 +21,7 @@ proc prepare*(ctx: CasperRef, header: var BlockHeader) =
header.prevRandao = ctx.prevRandao
header.difficulty = DifficultyInt.zero
proc prepareForSeal*(ctx: CasperRef, header: var BlockHeader) =
proc prepareForSeal*(ctx: CasperRef, header: var BlockHeader) {.gcsafe, raises:[RlpError].} =
header.nonce = default(BlockNonce)
header.extraData = @[] # TODO: probably this should be configurable by user?
# this repetition, assigning prevRandao is because how txpool works

View File

@ -33,6 +33,10 @@ import
from web3/ethtypes as web3types import nil, TypedTransaction, WithdrawalV1, ExecutionPayloadV1OrV2, toExecutionPayloadV1OrV2, toExecutionPayloadV1
from web3/engine_api_types import PayloadAttributesV1, ExecutionPayloadV1, PayloadAttributesV2, ExecutionPayloadV2
export
# generateExecutionPayload caller will need this
casper
type
EngineState* = enum
EngineStopped,
@ -142,6 +146,17 @@ template unsafeQuantityToInt64(q: web3types.Quantity): int64 =
proc toTypedTransaction(tx: Transaction): TypedTransaction =
web3types.TypedTransaction(rlp.encode(tx))
func toWithdrawal(x: WithdrawalV1): Withdrawal =
result.index = x.index.uint64
result.validatorIndex = x.validatorIndex.uint64
result.address = x.address.EthAddress
result.amount = x.amount.uint64
func toWithdrawals(list: openArray[WithdrawalV1]): seq[Withdrawal] =
result = newSeqOfCap[Withdrawal](list.len)
for x in list:
result.add toWithdrawal(x)
proc generateExecutionPayload*(engine: SealingEngineRef,
payloadAttrs: PayloadAttributesV1 | PayloadAttributesV2): Result[ExecutionPayloadV1OrV2, string] =
let
@ -153,6 +168,11 @@ proc generateExecutionPayload*(engine: SealingEngineRef,
pos.timestamp = fromUnix(payloadAttrs.timestamp.unsafeQuantityToInt64)
pos.feeRecipient = EthAddress payloadAttrs.suggestedFeeRecipient
when payloadAttrs is PayloadAttributesV2:
engine.txPool.withdrawals = payloadAttrs.withdrawals.toWithdrawals
else:
engine.txPool.withdrawals = @[]
if headBlock.blockHash != engine.txPool.head.blockHash:
# reorg
discard engine.txPool.smartHead(headBlock)

View File

@ -436,7 +436,8 @@ import
chronicles,
eth/keys,
stew/[keyed_queue, results],
../common/common
../common/common,
./casper
export
TxItemRef,
@ -621,6 +622,10 @@ proc ethBlock*(xp: TxPoolRef): EthBlock
for (_,nonceList) in xp.txDB.packingOrderAccounts(txItemPacked):
result.txs.add toSeq(nonceList.incNonce).mapIt(it.tx)
let com = xp.chain.com
if com.consensus == ConsensusType.POS:
result.withdrawals = some(xp.chain.withdrawals)
proc gasCumulative*(xp: TxPoolRef): GasInt =
## Getter, retrieves the gas that will be burned in the block after
## retrieving it via `ethBlock`.
@ -766,6 +771,9 @@ proc `minTipPrice=`*(xp: TxPoolRef; val: GasPrice) =
xp.pMinTipPrice = val
xp.pDirtyBuckets = true
proc `withdrawals=`*(xp: TxPoolRef, val: sink seq[Withdrawal]) =
xp.chain.withdrawals = system.move(val)
# ------------------------------------------------------------------------------
# Public functions, per-tx-item operations
# ------------------------------------------------------------------------------

View File

@ -66,6 +66,7 @@ type
limits: TxChainGasLimits ## Gas limits for packer and next header
txEnv: TxChainPackerEnv ## Assorted parameters, tx packer environment
prepHeader: BlockHeader ## Prepared Header from Consensus Engine
withdrawals: seq[Withdrawal]
# ------------------------------------------------------------------------------
# Private functions
@ -90,7 +91,7 @@ proc prepareHeader(dh: TxChainRef; parent: BlockHeader)
of ConsensusType.POS:
dh.com.pos.prepare(dh.prepHeader)
proc prepareForSeal(dh: TxChainRef; header: var BlockHeader) {.gcsafe, raises: [].} =
proc prepareForSeal(dh: TxChainRef; header: var BlockHeader) {.gcsafe, raises: [RlpError].} =
case dh.com.consensus
of ConsensusType.POW:
# do nothing, tx pool was designed with POW in mind
@ -201,6 +202,9 @@ proc getHeader*(dh: TxChainRef): BlockHeader
# nonce: BlockNonce # mining free vaiable
fee: dh.txEnv.vmState.fee)
if dh.com.forkGTE(Shanghai):
result.withdrawalsRoot = some(calcWithdrawalsRoot(dh.withdrawals))
dh.prepareForSeal(result)
proc clearAccounts*(dh: TxChainRef)
@ -280,6 +284,10 @@ proc vmState*(dh: TxChainRef): BaseVMState =
## Getter, `BaseVmState` descriptor based on the current insertion point.
dh.txEnv.vmState
proc withdrawals*(dh: TxChainRef): seq[Withdrawal] =
## Getter, `BaseVmState` descriptor based on the current insertion point.
result = system.move(dh.withdrawals)
# ------------------------------------------------------------------------------
# Public functions, setters
# ------------------------------------------------------------------------------
@ -342,6 +350,9 @@ proc `txRoot=`*(dh: TxChainRef; val: Hash256) =
## Setter
dh.txEnv.txRoot = val
proc `withdrawals=`*(dh: TxChainRef, val: sink seq[Withdrawal]) =
dh.withdrawals = system.move(val)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit d57a1094b1c0d6faa6b3b70ae653948299266fa3
Subproject commit e639dc1e145722ec137639ee040eddc524f853dd