TxPool will not pack tx with invalid chainId

This commit is contained in:
jangko 2023-10-31 15:12:41 +07:00
parent 494a61ceb6
commit b74a080a3d
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 21 additions and 1 deletions

View File

@ -45,6 +45,9 @@ type
## Implied disposal, typically implied by greater nonces (fallback value)
"implied disposal"
txInfoChainIdMismatch = ##\
## Tx chainId does not match with network chainId
"chainId mismatch"
# ------ Miscellaneous errors ----------------------------------------------
txInfoErrUnspecified = ##\

View File

@ -27,7 +27,7 @@ import
../../../transaction,
../../../vm_state,
../../../vm_types,
".."/[tx_chain, tx_desc, tx_item, tx_tabs, tx_tabs/tx_status],
".."/[tx_chain, tx_desc, tx_item, tx_tabs, tx_tabs/tx_status, tx_info],
"."/[tx_bucket, tx_classify]
type
@ -186,6 +186,10 @@ proc vmExecGrabItem(pst: TxPackerStateRef; item: TxItemRef): Result[bool,void]
xp = pst.xp
vmState = xp.chain.vmState
if not item.tx.validateChainId(xp.chain.com.chainId):
discard xp.txDB.dispose(item, txInfoChainIdMismatch)
return ok(false) # continue with next account
# EIP-4844
if pst.numBlobPerBlock + item.tx.versionedHashes.len > MAX_BLOBS_PER_BLOCK:
return err() # stop collecting

View File

@ -191,6 +191,19 @@ proc signTransaction*(tx: Transaction, privateKey: PrivateKey, chainId: ChainId,
result.R = UInt256.fromBytesBE(sig[0..31])
result.S = UInt256.fromBytesBE(sig[32..63])
# deriveChainId derives the chain id from the given v parameter
func deriveChainId*(v: int64, chainId: ChainId): ChainId =
if v == 27 or v == 28:
chainId
else:
((v - 35) div 2).ChainId
func validateChainId*(tx: Transaction, chainId: ChainId): bool =
if tx.txType == TxLegacy:
chainId.uint64 == deriveChainId(tx.V, chainId).uint64
else:
chainId.uint64 == tx.chainId.uint64
func eip1559TxNormalization*(tx: Transaction;
baseFee: GasInt): Transaction =
## This function adjusts a legacy transaction to EIP-1559 standard. This