2023-11-01 03:32:09 +00:00
# Nimbus
2024-02-15 02:57:05 +00:00
# Copyright (c) 2023-2024 Status Research & Development GmbH
2023-11-01 03:32:09 +00:00
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
2023-10-19 03:28:52 +00:00
import
2024-03-20 07:35:38 +00:00
std / [ options , strutils , typetraits ] ,
2023-10-19 03:28:52 +00:00
stew / byteutils ,
. / blobs ,
2023-10-23 13:59:57 +00:00
.. / types ,
2023-10-19 03:28:52 +00:00
.. / tx_sender ,
.. / .. / .. / .. / nimbus / constants ,
.. / .. / .. / .. / nimbus / utils / utils ,
.. / .. / .. / .. / nimbus / common as nimbus_common ,
.. / .. / .. / .. / nimbus / beacon / web3_eth_conv ,
.. / .. / .. / .. / nimbus / beacon / payload_conv ,
2023-12-08 09:35:50 +00:00
web3 / execution_types
2023-10-19 03:28:52 +00:00
type
EngineAPIVersionResolver * = ref object of RootRef
com : CommonRef
2024-02-15 02:57:05 +00:00
method setEngineAPIVersionResolver * ( cust : EngineAPIVersionResolver , v : CommonRef ) {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
cust . com = v
method forkchoiceUpdatedVersion * ( cust : EngineAPIVersionResolver ,
2024-06-14 07:31:08 +00:00
headTimestamp : uint64 , payloadAttributesTimestamp : Opt [ uint64 ] = Opt . none ( uint64 ) ) : Version {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
let ts = if payloadAttributesTimestamp . isNone : headTimestamp . EthTime
else : payloadAttributesTimestamp . get ( ) . EthTime
if cust . com . isCancunOrLater ( ts ) :
Version . V3
elif cust . com . isShanghaiOrLater ( ts ) :
Version . V2
else :
Version . V1
2024-02-15 02:57:05 +00:00
method newPayloadVersion * ( cust : EngineAPIVersionResolver , timestamp : uint64 ) : Version {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
let ts = timestamp . EthTime
if cust . com . isCancunOrLater ( ts ) :
Version . V3
elif cust . com . isShanghaiOrLater ( ts ) :
Version . V2
else :
Version . V1
2024-02-15 02:57:05 +00:00
method getPayloadVersion * ( cust : EngineAPIVersionResolver , timestamp : uint64 ) : Version {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
let ts = timestamp . EthTime
if cust . com . isCancunOrLater ( ts ) :
Version . V3
elif cust . com . isShanghaiOrLater ( ts ) :
Version . V2
else :
Version . V1
type
GetPayloadCustomizer * = ref object of EngineAPIVersionResolver
method getPayloadID * ( cust : GetPayloadCustomizer ,
2024-02-15 02:57:05 +00:00
basePayloadID : PayloadID ) : PayloadID {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getPayloadID unimplemented " )
2024-02-15 02:57:05 +00:00
method getExpectedError * ( cust : GetPayloadCustomizer ) : int {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getExpectedError unimplemented " )
type
BaseGetPayloadCustomizer * = ref object of GetPayloadCustomizer
2024-06-14 07:31:08 +00:00
customPayloadID * : Opt [ PayloadID ]
2023-10-23 13:59:57 +00:00
expectedError * : int
2023-10-19 03:28:52 +00:00
method getPayloadID ( cust : BaseGetPayloadCustomizer ,
basePayloadID : PayloadID ) : PayloadID =
if cust . customPayloadID . isSome :
return cust . customPayloadID . get
return basePayloadID
method getExpectedError ( cust : BaseGetPayloadCustomizer ) : int =
cust . expectedError
type
2024-03-20 07:35:38 +00:00
UpgradeGetPayloadVersion * = ref object of BaseGetPayloadCustomizer
2023-10-19 03:28:52 +00:00
2024-03-20 07:35:38 +00:00
method getPayloadVersion ( cust : UpgradeGetPayloadVersion , timestamp : uint64 ) : Version =
2023-10-19 03:28:52 +00:00
let version = procCall getPayloadVersion ( cust . GetPayloadCustomizer , timestamp )
doAssert ( version ! = Version . high , " cannot upgrade version " & $ Version . high )
version . succ
type
2024-03-20 07:35:38 +00:00
DowngradeGetPayloadVersion * = ref object of BaseGetPayloadCustomizer
2023-10-19 03:28:52 +00:00
2024-03-20 07:35:38 +00:00
method getPayloadVersion ( cust : DowngradeGetPayloadVersion , timestamp : uint64 ) : Version =
2023-10-19 03:28:52 +00:00
let version = procCall getPayloadVersion ( cust . GetPayloadCustomizer , timestamp )
doAssert ( version ! = Version . V1 , " cannot downgrade version 1 " )
version . pred
type
2023-10-23 13:59:57 +00:00
PayloadAttributesCustomizer * = ref object of BaseGetPayloadCustomizer
2023-10-19 03:28:52 +00:00
2024-02-15 02:57:05 +00:00
method getPayloadAttributes * ( cust : PayloadAttributesCustomizer , basePayloadAttributes : PayloadAttributes ) : PayloadAttributes {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getPayloadAttributes unimplemented " )
type
BasePayloadAttributesCustomizer * = ref object of PayloadAttributesCustomizer
2024-06-14 07:31:08 +00:00
timestamp * : Opt [ uint64 ]
prevRandao * : Opt [ common . Hash256 ]
suggestedFeeRecipient * : Opt [ common . EthAddress ]
withdrawals * : Opt [ seq [ Withdrawal ] ]
2023-10-23 13:59:57 +00:00
removeWithdrawals * : bool
2024-06-14 07:31:08 +00:00
beaconRoot * : Opt [ common . Hash256 ]
2023-10-23 13:59:57 +00:00
removeBeaconRoot * : bool
2023-10-19 03:28:52 +00:00
method getPayloadAttributes ( cust : BasePayloadAttributesCustomizer , basePayloadAttributes : PayloadAttributes ) : PayloadAttributes =
var customPayloadAttributes = PayloadAttributes (
timestamp : basePayloadAttributes . timestamp ,
prevRandao : basePayloadAttributes . prevRandao ,
suggestedFeeRecipient : basePayloadAttributes . suggestedFeeRecipient ,
withdrawals : basePayloadAttributes . withdrawals ,
parentBeaconBlockRoot : basePayloadAttributes . parentBeaconBlockRoot ,
)
if cust . timestamp . isSome :
customPayloadAttributes . timestamp = w3Qty cust . timestamp . get
if cust . prevRandao . isSome :
customPayloadAttributes . prevRandao = w3Hash cust . prevRandao . get
if cust . suggestedFeeRecipient . isSome :
customPayloadAttributes . suggestedFeeRecipient = w3Addr cust . suggestedFeeRecipient . get
if cust . removeWithdrawals :
2024-06-14 07:31:08 +00:00
customPayloadAttributes . withdrawals = Opt . none ( seq [ WithdrawalV1 ] )
2023-10-19 03:28:52 +00:00
elif cust . withdrawals . isSome :
customPayloadAttributes . withdrawals = w3Withdrawals cust . withdrawals
if cust . removeBeaconRoot :
2024-06-14 07:31:08 +00:00
customPayloadAttributes . parentBeaconBlockRoot = Opt . none ( Web3Hash )
2023-10-19 03:28:52 +00:00
elif cust . beaconRoot . isSome :
customPayloadAttributes . parentBeaconBlockRoot = w3Hash cust . beaconRoot
return customPayloadAttributes
type
ForkchoiceUpdatedCustomizer * = ref object of BasePayloadAttributesCustomizer
method getForkchoiceState * ( cust : ForkchoiceUpdatedCustomizer ,
2024-02-15 02:57:05 +00:00
baseForkchoiceUpdate : ForkchoiceStateV1 ) : ForkchoiceStateV1 {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getForkchoiceState unimplemented " )
2024-02-15 02:57:05 +00:00
method getExpectInvalidStatus * ( cust : ForkchoiceUpdatedCustomizer ) : bool {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getExpectInvalidStatus unimplemented " )
# Customizer that makes no modifications to the forkchoice directive call.
# Used as base to other customizers.
type
BaseForkchoiceUpdatedCustomizer * = ref object of ForkchoiceUpdatedCustomizer
2023-10-23 13:59:57 +00:00
expectInvalidStatus * : bool
2023-10-19 03:28:52 +00:00
method getPayloadAttributes ( cust : BaseForkchoiceUpdatedCustomizer , basePayloadAttributes : PayloadAttributes ) : PayloadAttributes =
var customPayloadAttributes = procCall getPayloadAttributes ( cust . BasePayloadAttributesCustomizer , basePayloadAttributes )
return customPayloadAttributes
method getForkchoiceState ( cust : BaseForkchoiceUpdatedCustomizer , baseForkchoiceUpdate : ForkchoiceStateV1 ) : ForkchoiceStateV1 =
return baseForkchoiceUpdate
method getExpectInvalidStatus ( cust : BaseForkchoiceUpdatedCustomizer ) : bool =
return cust . expectInvalidStatus
# Customizer that upgrades the version of the forkchoice directive call to the next version.
type
2024-03-20 07:35:38 +00:00
UpgradeForkchoiceUpdatedVersion * = ref object of BaseForkchoiceUpdatedCustomizer
2023-10-19 03:28:52 +00:00
2024-03-20 07:35:38 +00:00
method forkchoiceUpdatedVersion ( cust : UpgradeForkchoiceUpdatedVersion , headTimestamp :
2024-06-14 07:31:08 +00:00
uint64 , payloadAttributesTimestamp : Opt [ uint64 ] = Opt . none ( uint64 ) ) : Version =
2023-10-19 03:28:52 +00:00
let version = procCall forkchoiceUpdatedVersion ( EngineAPIVersionResolver ( cust ) , headTimestamp , payloadAttributesTimestamp )
doAssert ( version ! = Version . high , " cannot upgrade version " & $ Version . high )
version . succ
2023-10-24 04:30:48 +00:00
2023-10-19 03:28:52 +00:00
# Customizer that downgrades the version of the forkchoice directive call to the previous version.
type
2024-03-20 07:35:38 +00:00
DowngradeForkchoiceUpdatedVersion * = ref object of BaseForkchoiceUpdatedCustomizer
2023-10-19 03:28:52 +00:00
2024-03-20 07:35:38 +00:00
method forkchoiceUpdatedVersion ( cust : DowngradeForkchoiceUpdatedVersion , headTimestamp : uint64 ,
2024-06-14 07:31:08 +00:00
payloadAttributesTimestamp : Opt [ uint64 ] = Opt . none ( uint64 ) ) : Version =
2023-10-19 03:28:52 +00:00
let version = procCall forkchoiceUpdatedVersion ( EngineAPIVersionResolver ( cust ) , headTimestamp , payloadAttributesTimestamp )
doAssert ( version ! = Version . V1 , " cannot downgrade version 1 " )
version . pred
type
2023-10-23 13:59:57 +00:00
TimestampDeltaPayloadAttributesCustomizer * = ref object of BaseForkchoiceUpdatedCustomizer
timestampDelta * : int
method getPayloadAttributes ( cust : TimestampDeltaPayloadAttributesCustomizer , basePayloadAttributes : PayloadAttributes ) : PayloadAttributes =
var customPayloadAttributes = procCall getPayloadAttributes ( cust . BasePayloadAttributesCustomizer , basePayloadAttributes )
customPayloadAttributes . timestamp = w3Qty ( customPayloadAttributes . timestamp , cust . timestampDelta )
return customPayloadAttributes
2023-10-24 04:30:48 +00:00
2023-10-23 13:59:57 +00:00
type
VersionedHashesCustomizer * = ref object of RootRef
2024-06-14 07:31:08 +00:00
blobs * : Opt [ seq [ BlobID ] ]
2023-10-19 03:28:52 +00:00
hashVersions * : seq [ byte ]
2023-10-23 13:59:57 +00:00
method getVersionedHashes * ( cust : VersionedHashesCustomizer ,
2024-06-14 07:31:08 +00:00
baseVersionedHashes : openArray [ common . Hash256 ] ) : Opt [ seq [ common . Hash256 ] ] {. base , gcsafe . } =
2023-10-23 13:59:57 +00:00
if cust . blobs . isNone :
2024-06-14 07:31:08 +00:00
return Opt . none ( seq [ common . Hash256 ] )
2023-10-19 03:28:52 +00:00
2023-10-23 13:59:57 +00:00
let blobs = cust . blobs . get
var v = newSeq [ common . Hash256 ] ( blobs . len )
2023-10-19 03:28:52 +00:00
var version : byte
2023-10-23 13:59:57 +00:00
for i , blobID in blobs :
if cust . hashVersions . len > i :
version = cust . hashVersions [ i ]
v [ i ] = blobID . getVersionedHash ( version )
2024-06-14 07:31:08 +00:00
Opt . some ( v )
2023-10-19 03:28:52 +00:00
2024-02-15 02:57:05 +00:00
method description * ( cust : VersionedHashesCustomizer ) : string {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
result = " VersionedHashes: "
2023-10-23 13:59:57 +00:00
if cust . blobs . isSome :
for x in cust . blobs . get :
result . add x . toHex
2023-10-19 03:28:52 +00:00
2023-10-23 13:59:57 +00:00
if cust . hashVersions . len > 0 :
2023-10-19 03:28:52 +00:00
result . add " with versions "
2023-10-23 13:59:57 +00:00
result . add cust . hashVersions . toHex
2023-10-19 03:28:52 +00:00
type
IncreaseVersionVersionedHashes * = ref object of VersionedHashesCustomizer
2023-10-23 13:59:57 +00:00
method getVersionedHashes ( cust : IncreaseVersionVersionedHashes ,
2024-06-14 07:31:08 +00:00
baseVersionedHashes : openArray [ common . Hash256 ] ) : Opt [ seq [ common . Hash256 ] ] =
2023-10-19 03:28:52 +00:00
doAssert ( baseVersionedHashes . len > 0 , " no versioned hashes available for modification " )
2023-10-23 13:59:57 +00:00
var v = newSeq [ common . Hash256 ] ( baseVersionedHashes . len )
2023-10-19 03:28:52 +00:00
for i , h in baseVersionedHashes :
2023-10-23 13:59:57 +00:00
v [ i ] = h
v [ i ] . data [ 0 ] = v [ i ] . data [ 0 ] + 1
2024-06-14 07:31:08 +00:00
Opt . some ( v )
2023-10-19 03:28:52 +00:00
type
CorruptVersionedHashes * = ref object of VersionedHashesCustomizer
2023-10-23 13:59:57 +00:00
method getVersionedHashes ( cust : CorruptVersionedHashes ,
2024-06-14 07:31:08 +00:00
baseVersionedHashes : openArray [ common . Hash256 ] ) : Opt [ seq [ common . Hash256 ] ] =
2023-10-19 03:28:52 +00:00
doAssert ( baseVersionedHashes . len > 0 , " no versioned hashes available for modification " )
2023-10-23 13:59:57 +00:00
var v = newSeq [ common . Hash256 ] ( baseVersionedHashes . len )
2023-10-19 03:28:52 +00:00
for i , h in baseVersionedHashes :
2023-10-23 13:59:57 +00:00
v [ i ] = h
v [ i ] . data [ h . data . len - 1 ] = v [ i ] . data [ h . data . len - 1 ] + 1
2024-06-14 07:31:08 +00:00
Opt . some ( v )
2023-10-19 03:28:52 +00:00
type
RemoveVersionedHash * = ref object of VersionedHashesCustomizer
2023-10-23 13:59:57 +00:00
method getVersionedHashes ( cust : RemoveVersionedHash ,
2024-06-14 07:31:08 +00:00
baseVersionedHashes : openArray [ common . Hash256 ] ) : Opt [ seq [ common . Hash256 ] ] =
2023-10-19 03:28:52 +00:00
doAssert ( baseVersionedHashes . len > 0 , " no versioned hashes available for modification " )
2023-10-23 13:59:57 +00:00
var v = newSeq [ common . Hash256 ] ( baseVersionedHashes . len - 1 )
2023-10-19 03:28:52 +00:00
for i , h in baseVersionedHashes :
if i < baseVersionedHashes . len - 1 :
2023-10-23 13:59:57 +00:00
v [ i ] = h
v [ i ] . data [ h . data . len - 1 ] = v [ i ] . data [ h . data . len - 1 ] + 1
2024-06-14 07:31:08 +00:00
Opt . some ( v )
2023-10-19 03:28:52 +00:00
type
ExtraVersionedHash * = ref object of VersionedHashesCustomizer
2023-10-23 13:59:57 +00:00
method getVersionedHashes ( cust : ExtraVersionedHash ,
2024-06-14 07:31:08 +00:00
baseVersionedHashes : openArray [ common . Hash256 ] ) : Opt [ seq [ common . Hash256 ] ] =
2023-10-23 13:59:57 +00:00
var v = newSeq [ common . Hash256 ] ( baseVersionedHashes . len + 1 )
2023-10-19 03:28:52 +00:00
for i , h in baseVersionedHashes :
2023-10-23 13:59:57 +00:00
v [ i ] = h
2023-10-19 03:28:52 +00:00
2023-10-31 03:18:37 +00:00
var extraHash = common . Hash256 . randomBytes ( )
2023-10-19 03:28:52 +00:00
extraHash . data [ 0 ] = VERSIONED_HASH_VERSION_KZG
2023-10-23 13:59:57 +00:00
v [ ^ 1 ] = extraHash
2024-06-14 07:31:08 +00:00
Opt . some ( v )
2023-10-19 03:28:52 +00:00
type
PayloadCustomizer * = ref object of EngineAPIVersionResolver
2024-02-15 02:57:05 +00:00
method customizePayload * ( cust : PayloadCustomizer , data : ExecutableData ) : ExecutableData {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " customizePayload unimplemented " )
2024-02-15 02:57:05 +00:00
method getTimestamp ( cust : PayloadCustomizer , basePayload : ExecutionPayload ) : uint64 {. base , gcsafe . } =
2023-10-19 03:28:52 +00:00
doAssert ( false , " getTimestamp unimplemented " )
type
NewPayloadCustomizer * = ref object of PayloadCustomizer
2023-10-23 13:59:57 +00:00
expectedError * : int
expectInvalidStatus * : bool
2023-10-19 03:28:52 +00:00
2024-02-15 02:57:05 +00:00
method getExpectedError * ( cust : NewPayloadCustomizer ) : int {. base , gcsafe . } =
2023-10-23 13:59:57 +00:00
cust . expectedError
2023-10-19 03:28:52 +00:00
2024-02-15 02:57:05 +00:00
method getExpectInvalidStatus * ( cust : NewPayloadCustomizer ) : bool {. base , gcsafe . } =
2023-10-23 13:59:57 +00:00
cust . expectInvalidStatus
2023-10-19 03:28:52 +00:00
type
2023-10-23 13:59:57 +00:00
CustomPayloadData * = object
2024-06-14 07:31:08 +00:00
parentHash * : Opt [ common . Hash256 ]
feeRecipient * : Opt [ common . EthAddress ]
stateRoot * : Opt [ common . Hash256 ]
receiptsRoot * : Opt [ common . Hash256 ]
logsBloom * : Opt [ BloomFilter ]
prevRandao * : Opt [ common . Hash256 ]
number * : Opt [ uint64 ]
gasLimit * : Opt [ GasInt ]
gasUsed * : Opt [ GasInt ]
timestamp * : Opt [ uint64 ]
extraData * : Opt [ common . Blob ]
baseFeePerGas * : Opt [ UInt256 ]
blockHash * : Opt [ common . Hash256 ]
transactions * : Opt [ seq [ Transaction ] ]
withdrawals * : Opt [ seq [ Withdrawal ] ]
2023-10-19 03:28:52 +00:00
removeWithdrawals * : bool
2024-06-14 07:31:08 +00:00
blobGasUsed * : Opt [ uint64 ]
2023-10-19 03:28:52 +00:00
removeBlobGasUsed * : bool
2024-06-14 07:31:08 +00:00
excessBlobGas * : Opt [ uint64 ]
2023-10-19 03:28:52 +00:00
removeExcessBlobGas * : bool
2024-06-14 07:31:08 +00:00
parentBeaconRoot * : Opt [ common . Hash256 ]
2023-10-19 03:28:52 +00:00
removeParentBeaconRoot * : bool
versionedHashesCustomizer * : VersionedHashesCustomizer
func getTimestamp * ( cust : CustomPayloadData , basePayload : ExecutionPayload ) : uint64 =
if cust . timestamp . isSome :
return cust . timestamp . get
return basePayload . timestamp . uint64
# Construct a customized payload by taking an existing payload as base and mixing it CustomPayloadData
# blockHash is calculated automatically.
2024-02-15 02:57:05 +00:00
proc customizePayload * ( cust : CustomPayloadData , data : ExecutableData ) : ExecutableData {. gcsafe . } =
2024-05-15 03:07:59 +00:00
var customHeader = blockHeader ( data . basePayload , beaconRoot = data . beaconRoot )
2023-10-19 03:28:52 +00:00
if cust . transactions . isSome :
customHeader . txRoot = calcTxRoot ( cust . transactions . get )
# Overwrite custom information
if cust . parentHash . isSome :
customHeader . parentHash = cust . parentHash . get
if cust . feeRecipient . isSome :
customHeader . coinbase = cust . feeRecipient . get
if cust . stateRoot . isSome :
customHeader . stateRoot = cust . stateRoot . get
if cust . receiptsRoot . isSome :
2024-06-14 07:31:08 +00:00
customHeader . receiptsRoot = cust . receiptsRoot . get
2023-10-19 03:28:52 +00:00
if cust . logsBloom . isSome :
2024-06-14 07:31:08 +00:00
customHeader . logsBloom = cust . logsBloom . get
2023-10-19 03:28:52 +00:00
if cust . prevRandao . isSome :
2024-06-14 07:31:08 +00:00
customHeader . mixHash = cust . prevRandao . get
2023-10-19 03:28:52 +00:00
if cust . number . isSome :
2024-06-14 07:31:08 +00:00
customHeader . number = cust . number . get
2023-10-19 03:28:52 +00:00
if cust . gasLimit . isSome :
customHeader . gasLimit = cust . gasLimit . get
if cust . gasUsed . isSome :
customHeader . gasUsed = cust . gasUsed . get
if cust . timestamp . isSome :
customHeader . timestamp = cust . timestamp . get . EthTime
if cust . extraData . isSome :
customHeader . extraData = cust . extraData . get
if cust . baseFeePerGas . isSome :
2024-06-14 07:31:08 +00:00
customHeader . baseFeePerGas = cust . baseFeePerGas
2023-10-19 03:28:52 +00:00
if cust . removeWithdrawals :
2024-06-14 07:31:08 +00:00
customHeader . withdrawalsRoot = Opt . none ( common . Hash256 )
2023-10-19 03:28:52 +00:00
elif cust . withdrawals . isSome :
let h = calcWithdrawalsRoot ( cust . withdrawals . get )
2024-06-14 07:31:08 +00:00
customHeader . withdrawalsRoot = Opt . some ( h )
2023-10-19 03:28:52 +00:00
if cust . removeBlobGasUsed :
2024-06-14 07:31:08 +00:00
customHeader . blobGasUsed = Opt . none ( uint64 )
2023-10-19 03:28:52 +00:00
elif cust . blobGasUsed . isSome :
customHeader . blobGasUsed = cust . blobGasUsed
if cust . removeExcessBlobGas :
2024-06-14 07:31:08 +00:00
customHeader . excessBlobGas = Opt . none ( uint64 )
2023-10-19 03:28:52 +00:00
elif cust . excessBlobGas . isSome :
customHeader . excessBlobGas = cust . excessBlobGas
if cust . removeParentBeaconRoot :
2024-06-14 07:31:08 +00:00
customHeader . parentBeaconBlockRoot = Opt . none ( common . Hash256 )
2023-10-19 03:28:52 +00:00
elif cust . parentBeaconRoot . isSome :
customHeader . parentBeaconBlockRoot = cust . parentBeaconRoot
var blk = EthBlock (
header : customHeader ,
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
transactions :
if cust . transactions . isSome :
cust . transactions . get
else :
ethTxs data . basePayload . transactions
2023-10-19 03:28:52 +00:00
)
if cust . removeWithdrawals :
2024-06-14 07:31:08 +00:00
blk . withdrawals = Opt . none ( seq [ Withdrawal ] )
2023-10-19 03:28:52 +00:00
elif cust . withdrawals . isSome :
blk . withdrawals = cust . withdrawals
elif data . basePayload . withdrawals . isSome :
blk . withdrawals = ethWithdrawals data . basePayload . withdrawals
result = ExecutableData (
basePayload : executionPayload ( blk ) ,
beaconRoot : blk . header . parentBeaconBlockRoot ,
attr : data . attr ,
versionedHashes : data . versionedHashes ,
)
if cust . versionedHashesCustomizer . isNil . not :
2023-10-23 13:59:57 +00:00
doAssert ( data . versionedHashes . isSome )
result . versionedHashes = cust . versionedHashesCustomizer . getVersionedHashes ( data . versionedHashes . get )
2024-04-19 19:43:13 +00:00
2023-10-19 03:28:52 +00:00
# Base new payload directive call cust.
# Used as base to other customizers.
type
BaseNewPayloadVersionCustomizer * = ref object of NewPayloadCustomizer
payloadCustomizer * : CustomPayloadData
method customizePayload ( cust : BaseNewPayloadVersionCustomizer , data : ExecutableData ) : ExecutableData =
cust . payloadCustomizer . customizePayload ( data )
# Customizer that upgrades the version of the payload to the next version.
type
2023-10-23 13:59:57 +00:00
UpgradeNewPayloadVersion * = ref object of BaseNewPayloadVersionCustomizer
2023-10-19 03:28:52 +00:00
method newPayloadVersion ( cust : UpgradeNewPayloadVersion , timestamp : uint64 ) : Version =
let version = procCall newPayloadVersion ( EngineAPIVersionResolver ( cust ) , timestamp )
doAssert ( version ! = Version . high , " cannot upgrade version " & $ Version . high )
version . succ
# Customizer that downgrades the version of the payload to the previous version.
type
2023-10-23 13:59:57 +00:00
DowngradeNewPayloadVersion * = ref object of BaseNewPayloadVersionCustomizer
2023-10-19 03:28:52 +00:00
method newPayloadVersion ( cust : DowngradeNewPayloadVersion , timestamp : uint64 ) : Version =
let version = procCall newPayloadVersion ( EngineAPIVersionResolver ( cust ) , timestamp )
doAssert ( version ! = Version . V1 , " cannot downgrade version 1 " )
version . pred
proc customizePayloadTransactions * ( data : ExecutableData , customTransactions : openArray [ Transaction ] ) : ExecutableData =
let cpd = CustomPayloadData (
2024-06-14 07:31:08 +00:00
transactions : Opt . some ( @ customTransactions ) ,
2023-10-19 03:28:52 +00:00
)
customizePayload ( cpd , data )
proc `$` * ( cust : CustomPayloadData ) : string =
var fieldList = newSeq [ string ] ( )
if cust . parentHash . isSome :
fieldList . add " parentHash= " & cust . parentHash . get . short
if cust . feeRecipient . isSome :
fieldList . add " Coinbase= " & $ cust . feeRecipient . get
if cust . stateRoot . isSome :
fieldList . add " stateRoot= " & cust . stateRoot . get . short
if cust . receiptsRoot . isSome :
fieldList . add " receiptsRoot= " & cust . receiptsRoot . get . short
if cust . logsBloom . isSome :
fieldList . add " logsBloom= " & cust . logsBloom . get . toHex
if cust . prevRandao . isSome :
fieldList . add " prevRandao= " & cust . prevRandao . get . short
if cust . number . isSome :
fieldList . add " Number= " & $ cust . number . get
if cust . gasLimit . isSome :
fieldList . add " gasLimit= " & $ cust . gasLimit . get
if cust . gasUsed . isSome :
fieldList . add " gasUsed= " & $ cust . gasUsed . get
if cust . timestamp . isSome :
fieldList . add " timestamp= " & $ cust . timestamp . get
if cust . extraData . isSome :
fieldList . add " extraData= " & cust . extraData . get . toHex
if cust . baseFeePerGas . isSome :
fieldList . add " baseFeePerGas= " & $ cust . baseFeePerGas . get
if cust . transactions . isSome :
fieldList . add " transactions= " & $ cust . transactions . get . len
if cust . withdrawals . isSome :
fieldList . add " withdrawals= " & $ cust . withdrawals . get . len
fieldList . join ( " , " )
type
InvalidPayloadBlockField * = enum
InvalidParentHash
InvalidStateRoot
InvalidReceiptsRoot
InvalidNumber
InvalidGasLimit
InvalidGasUsed
InvalidTimestamp
InvalidPrevRandao
RemoveTransaction
InvalidTransactionSignature
InvalidTransactionNonce
InvalidTransactionGas
InvalidTransactionGasPrice
InvalidTransactionValue
InvalidTransactionGasTipPrice
InvalidTransactionChainID
InvalidParentBeaconBlockRoot
InvalidExcessBlobGas
InvalidBlobGasUsed
InvalidBlobCountGasUsed
InvalidVersionedHashesVersion
InvalidVersionedHashes
IncompleteVersionedHashes
ExtraVersionedHashes
InvalidWithdrawals
2024-06-14 07:31:08 +00:00
func scramble ( data : Web3Hash ) : Opt [ common . Hash256 ] =
2023-10-19 03:28:52 +00:00
var h = ethHash data
h . data [ ^ 1 ] = byte ( 255 - h . data [ ^ 1 ] )
2024-06-14 07:31:08 +00:00
Opt . some ( h )
2023-10-19 03:28:52 +00:00
2024-06-14 07:31:08 +00:00
func scramble ( data : common . Hash256 ) : Opt [ common . Hash256 ] =
2023-10-19 03:28:52 +00:00
var h = data
2024-05-15 16:22:03 +00:00
h . data [ 0 ] = byte ( 255 - h . data [ 0 ] )
2024-06-14 07:31:08 +00:00
Opt . some ( h )
2023-10-19 03:28:52 +00:00
# This function generates an invalid payload by taking a base payload and modifying the specified field such that it ends up being invalid.
# One small consideration is that the payload needs to contain transactions and specially transactions using the PREVRANDAO opcode for all the fields to be compatible with this function.
proc generateInvalidPayload * ( sender : TxSender , data : ExecutableData , payloadField : InvalidPayloadBlockField ) : ExecutableData =
var customPayloadMod : CustomPayloadData
let basePayload = data . basePayload
case payloadField
of InvalidParentHash :
customPayloadMod = CustomPayloadData (
parentHash : scramble ( basePayload . parentHash ) ,
)
of InvalidStateRoot :
customPayloadMod = CustomPayloadData (
stateRoot : scramble ( basePayload . stateRoot ) ,
)
of InvalidReceiptsRoot :
customPayloadMod = CustomPayloadData (
receiptsRoot : scramble ( basePayload . receiptsRoot ) ,
)
of InvalidNumber :
let modNumber = basePayload . blockNumber . uint64 - 1
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
number : Opt . some ( modNumber ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidGasLimit :
let modGasLimit = basePayload . gasLimit . GasInt * 2
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
gasLimit : Opt . some ( modGasLimit ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidGasUsed :
let modGasUsed = basePayload . gasUsed . GasInt - 1
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
gasUsed : Opt . some ( modGasUsed ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidTimestamp :
let modTimestamp = basePayload . timestamp . uint64 - 1
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
timestamp : Opt . some ( modTimestamp ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidPrevRandao :
# This option potentially requires a transaction that uses the PREVRANDAO opcode.
# Otherwise the payload will still be valid.
2023-10-31 03:18:37 +00:00
let randomHash = common . Hash256 . randomBytes ( )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
prevRandao : Opt . some ( randomHash ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidParentBeaconBlockRoot :
doAssert ( data . beaconRoot . isSome ,
" no parent beacon block root available for modification " )
customPayloadMod = CustomPayloadData (
parentBeaconRoot : scramble ( data . beaconRoot . get ) ,
)
of InvalidBlobGasUsed :
doAssert ( basePayload . blobGasUsed . isSome , " no blob gas used available for modification " )
let modBlobGasUsed = basePayload . blobGasUsed . get . uint64 + 1
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
blobGasUsed : Opt . some ( modBlobGasUsed ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidBlobCountGasUsed :
doAssert ( basePayload . blobGasUsed . isSome , " no blob gas used available for modification " )
let modBlobGasUsed = basePayload . blobGasUsed . get . uint64 + GAS_PER_BLOB
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
blobGasUsed : Opt . some ( modBlobGasUsed ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidExcessBlobGas :
doAssert ( basePayload . excessBlobGas . isSome , " no excess blob gas available for modification " )
let modExcessBlobGas = basePayload . excessBlobGas . get . uint64 + 1
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
excessBlobGas : Opt . some ( modExcessBlobGas ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidVersionedHashesVersion :
2024-04-19 19:43:13 +00:00
doAssert ( data . versionedHashes . isSome , " no versioned hashes available for modification " )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
versionedHashesCustomizer : IncreaseVersionVersionedHashes ( ) ,
)
of InvalidVersionedHashes :
2024-04-19 19:43:13 +00:00
doAssert ( data . versionedHashes . isSome , " no versioned hashes available for modification " )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
versionedHashesCustomizer : CorruptVersionedHashes ( ) ,
)
of IncompleteVersionedHashes :
2024-04-19 19:43:13 +00:00
doAssert ( data . versionedHashes . isSome , " no versioned hashes available for modification " )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
versionedHashesCustomizer : RemoveVersionedHash ( ) ,
)
of ExtraVersionedHashes :
2024-04-19 19:43:13 +00:00
doAssert ( data . versionedHashes . isSome , " no versioned hashes available for modification " )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
versionedHashesCustomizer : ExtraVersionedHash ( ) ,
)
of InvalidWithdrawals :
# These options are not supported yet.
# TODO: Implement
doAssert ( false , " invalid payload field not supported yet: " & $ payloadField )
of RemoveTransaction :
let emptyTxs = newSeq [ Transaction ] ( )
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
transactions : Opt . some ( emptyTxs ) ,
2023-10-19 03:28:52 +00:00
)
of InvalidTransactionSignature ,
InvalidTransactionNonce ,
InvalidTransactionGas ,
InvalidTransactionGasPrice ,
InvalidTransactionGasTipPrice ,
InvalidTransactionValue ,
InvalidTransactionChainID :
doAssert ( basePayload . transactions . len > 0 , " no transactions available for modification " )
let baseTx = rlp . decode ( distinctBase basePayload . transactions [ 0 ] , Transaction )
var custTx : CustomTransactionData
case payloadField
of InvalidTransactionSignature :
2023-10-31 03:18:37 +00:00
var sig = CustSig ( R : baseTx . R - 1 . u256 )
2024-06-14 07:31:08 +00:00
custTx . signature = Opt . some ( sig )
2023-10-19 03:28:52 +00:00
of InvalidTransactionNonce :
2024-06-14 07:31:08 +00:00
custTx . nonce = Opt . some ( baseTx . nonce - 1 )
2023-10-19 03:28:52 +00:00
of InvalidTransactionGas :
2024-06-14 07:31:08 +00:00
custTx . gas = Opt . some ( 0 . GasInt )
2023-10-19 03:28:52 +00:00
of InvalidTransactionGasPrice :
2024-06-14 07:31:08 +00:00
custTx . gasPriceOrGasFeeCap = Opt . some ( 0 . GasInt )
2023-10-19 03:28:52 +00:00
of InvalidTransactionGasTipPrice :
2024-06-14 07:31:08 +00:00
custTx . gasTipCap = Opt . some ( gasTipPrice . GasInt * 2 . GasInt )
2023-10-19 03:28:52 +00:00
of InvalidTransactionValue :
# Vault account initially has 0x123450000000000000000, so this value should overflow
2024-06-14 07:31:08 +00:00
custTx . value = Opt . some ( UInt256 . fromHex ( " 0x123450000000000000001 " ) )
2023-10-19 03:28:52 +00:00
of InvalidTransactionChainID :
2024-06-14 07:31:08 +00:00
custTx . chainId = Opt . some ( ChainId ( baseTx . chainId . uint64 + 1 ) )
2023-10-19 03:28:52 +00:00
else : discard
2023-10-31 03:18:37 +00:00
let acc = sender . getNextAccount ( )
let modifiedTx = sender . customizeTransaction ( acc , baseTx , custTx )
2023-10-19 03:28:52 +00:00
customPayloadMod = CustomPayloadData (
2024-06-14 07:31:08 +00:00
transactions : Opt . some ( @ [ modifiedTx ] ) ,
2023-10-19 03:28:52 +00:00
)
customPayloadMod . customizePayload ( data )
# Generates an alternative withdrawals list that contains the same
# amounts and accounts, but the order in the list is different, so
# stateRoot of the resulting payload should be the same.
2024-03-20 07:35:38 +00:00
when false :
proc randomizeWithdrawalsOrder ( src : openArray [ Withdrawal ] ) : seq [ Withdrawal ] =
result = @ src
result . shuffle