2023-08-27 08:23:45 +07:00
|
|
|
# Nimbus
|
2025-01-09 12:33:29 +05:30
|
|
|
# Copyright (c) 2023-2025 Status Research & Development GmbH
|
2023-08-27 08:23:45 +07:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
2024-05-30 14:54:03 +02:00
|
|
|
results,
|
2024-12-26 17:07:25 +07:00
|
|
|
chronicles,
|
2024-10-16 07:04:12 +05:30
|
|
|
eth/common/hashes,
|
2024-10-28 03:50:04 +05:30
|
|
|
web3/[execution_types, primitives],
|
2024-12-26 17:07:25 +07:00
|
|
|
../../core/tx_pool,
|
|
|
|
../web3_eth_conv,
|
|
|
|
../beacon_engine,
|
2023-08-27 08:23:45 +07:00
|
|
|
../payload_conv,
|
2024-12-26 17:07:25 +07:00
|
|
|
./api_utils
|
2023-08-27 08:23:45 +07:00
|
|
|
|
|
|
|
{.push gcsafe, raises:[CatchableError].}
|
|
|
|
|
2025-01-09 12:33:29 +05:30
|
|
|
logScope:
|
|
|
|
topics = "beacon engine"
|
|
|
|
|
2024-04-20 02:43:13 +07:00
|
|
|
func validateVersionedHashed(payload: ExecutionPayload,
|
2024-10-16 07:04:12 +05:30
|
|
|
expected: openArray[Hash32]): bool =
|
|
|
|
var versionedHashes: seq[VersionedHash]
|
2024-04-20 02:43:13 +07:00
|
|
|
for x in payload.transactions:
|
|
|
|
let tx = rlp.decode(distinctBase(x), Transaction)
|
|
|
|
versionedHashes.add tx.versionedHashes
|
|
|
|
|
|
|
|
if versionedHashes.len != expected.len:
|
|
|
|
return false
|
|
|
|
|
|
|
|
for i, x in expected:
|
|
|
|
if distinctBase(x) != versionedHashes[i].data:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
template validateVersion(com, timestamp, payloadVersion, apiVersion) =
|
2024-03-28 18:59:23 +07:00
|
|
|
if apiVersion == Version.V4:
|
|
|
|
if not com.isPragueOrLater(timestamp):
|
|
|
|
raise unsupportedFork("newPayloadV4 expect payload timestamp fall within Prague")
|
|
|
|
|
|
|
|
if com.isPragueOrLater(timestamp):
|
2024-10-26 18:10:54 +07:00
|
|
|
if payloadVersion != Version.V3:
|
2024-03-28 18:59:23 +07:00
|
|
|
raise invalidParams("if timestamp is Prague or later, " &
|
2024-10-26 18:10:54 +07:00
|
|
|
"payload must be ExecutionPayloadV3, got ExecutionPayload" & $payloadVersion)
|
2024-03-28 18:59:23 +07:00
|
|
|
|
2023-10-24 11:30:48 +07:00
|
|
|
if apiVersion == Version.V3:
|
|
|
|
if not com.isCancunOrLater(timestamp):
|
|
|
|
raise unsupportedFork("newPayloadV3 expect payload timestamp fall within Cancun")
|
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
if com.isCancunOrLater(timestamp):
|
2024-10-26 18:10:54 +07:00
|
|
|
if payloadVersion != Version.V3:
|
2023-08-27 08:23:45 +07:00
|
|
|
raise invalidParams("if timestamp is Cancun or later, " &
|
2024-10-26 18:10:54 +07:00
|
|
|
"payload must be ExecutionPayloadV3, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 08:23:45 +07:00
|
|
|
|
|
|
|
elif com.isShanghaiOrLater(timestamp):
|
2024-10-26 18:10:54 +07:00
|
|
|
if payloadVersion != Version.V2:
|
2023-08-27 08:23:45 +07:00
|
|
|
raise invalidParams("if timestamp is Shanghai or later, " &
|
2024-10-26 18:10:54 +07:00
|
|
|
"payload must be ExecutionPayloadV2, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 08:23:45 +07:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
elif payloadVersion != Version.V1:
|
2024-05-30 20:30:40 +00:00
|
|
|
raise invalidParams("if timestamp is earlier than Shanghai, " &
|
2024-10-26 18:10:54 +07:00
|
|
|
"payload must be ExecutionPayloadV1, got ExecutionPayload" & $payloadVersion)
|
2023-08-27 08:23:45 +07:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
if apiVersion == Version.V3 or apiVersion == Version.V4:
|
|
|
|
# both newPayloadV3 and newPayloadV4 expect ExecutionPayloadV3
|
|
|
|
if payloadVersion != Version.V3:
|
2023-10-24 11:30:48 +07:00
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
2024-10-26 18:10:54 +07:00
|
|
|
" expect ExecutionPayload3" &
|
|
|
|
" but got ExecutionPayload" & $payloadVersion)
|
2023-10-23 09:25:03 +07:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
template validatePayload(apiVersion, payloadVersion, payload) =
|
|
|
|
if payloadVersion >= Version.V2:
|
2024-03-28 18:59:23 +07:00
|
|
|
if payload.withdrawals.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"withdrawals is expected from execution payload")
|
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
if apiVersion >= Version.V3 or payloadVersion >= Version.V3:
|
2024-03-26 22:13:30 +07:00
|
|
|
if payload.blobGasUsed.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"blobGasUsed is expected from execution payload")
|
|
|
|
if payload.excessBlobGas.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
"excessBlobGas is expected from execution payload")
|
2023-10-23 09:25:03 +07:00
|
|
|
|
2024-12-21 12:59:30 +07:00
|
|
|
func validateExecutionRequest(requests: openArray[seq[byte]]): Result[void, string] {.raises:[].} =
|
|
|
|
var previousRequestType = -1
|
|
|
|
for request in requests:
|
|
|
|
if request.len == 0:
|
|
|
|
return err("Execution request data must not be empty")
|
|
|
|
|
|
|
|
let requestType = request[0]
|
|
|
|
if requestType.int <= previousRequestType:
|
|
|
|
return err("Execution requests are not in strictly ascending order")
|
|
|
|
|
|
|
|
if request.len == 1:
|
|
|
|
return err("Empty data for request type " & $requestType)
|
|
|
|
|
|
|
|
if requestType notin [
|
|
|
|
DEPOSIT_REQUEST_TYPE,
|
|
|
|
WITHDRAWAL_REQUEST_TYPE,
|
|
|
|
CONSOLIDATION_REQUEST_TYPE]:
|
|
|
|
return err("Invalid execution request type: " & $requestType)
|
|
|
|
|
|
|
|
previousRequestType = requestType.int
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
proc newPayload*(ben: BeaconEngineRef,
|
2023-10-24 11:30:48 +07:00
|
|
|
apiVersion: Version,
|
2023-08-27 08:23:45 +07:00
|
|
|
payload: ExecutionPayload,
|
2024-10-16 07:04:12 +05:30
|
|
|
versionedHashes = Opt.none(seq[Hash32]),
|
2024-10-18 16:38:18 +07:00
|
|
|
beaconRoot = Opt.none(Hash32),
|
2024-12-21 12:59:30 +07:00
|
|
|
executionRequests = Opt.none(seq[seq[byte]])): PayloadStatusV1 =
|
2023-08-27 08:23:45 +07:00
|
|
|
|
|
|
|
trace "Engine API request received",
|
|
|
|
meth = "newPayload",
|
|
|
|
number = payload.blockNumber,
|
|
|
|
hash = payload.blockHash
|
|
|
|
|
2024-04-20 02:43:13 +07:00
|
|
|
if apiVersion >= Version.V3:
|
2023-10-23 20:59:57 +07:00
|
|
|
if beaconRoot.isNone:
|
|
|
|
raise invalidParams("newPayloadV3 expect beaconRoot but got none")
|
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
if apiVersion >= Version.V4:
|
|
|
|
if executionRequests.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
": executionRequests is expected from execution payload")
|
|
|
|
|
2024-12-21 12:59:30 +07:00
|
|
|
validateExecutionRequest(executionRequests.get).isOkOr:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
": " & error)
|
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
let
|
|
|
|
com = ben.com
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
txFrame = ben.chain.latestTxFrame()
|
2023-08-27 08:23:45 +07:00
|
|
|
timestamp = ethTime payload.timestamp
|
|
|
|
version = payload.version
|
2024-10-18 16:38:18 +07:00
|
|
|
requestsHash = calcRequestsHash(executionRequests)
|
2023-08-27 08:23:45 +07:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
validatePayload(apiVersion, version, payload)
|
2024-04-24 07:50:50 +07:00
|
|
|
validateVersion(com, timestamp, version, apiVersion)
|
2024-05-15 06:07:59 +03:00
|
|
|
|
2024-10-18 16:38:18 +07:00
|
|
|
var blk = ethBlock(payload, beaconRoot, requestsHash)
|
2024-10-16 07:04:12 +05:30
|
|
|
template header: Header = blk.header
|
2024-05-15 06:07:59 +03:00
|
|
|
|
2024-04-20 02:43:13 +07:00
|
|
|
if apiVersion >= Version.V3:
|
|
|
|
if versionedHashes.isNone:
|
|
|
|
raise invalidParams("newPayload" & $apiVersion &
|
|
|
|
" expect blobVersionedHashes but got none")
|
|
|
|
if not validateVersionedHashed(payload, versionedHashes.get):
|
|
|
|
return invalidStatus(header.parentHash, "invalid blob versionedHashes")
|
2024-05-15 06:07:59 +03:00
|
|
|
|
2024-10-16 07:04:12 +05:30
|
|
|
let blockHash = payload.blockHash
|
2024-04-21 21:44:05 +07:00
|
|
|
header.validateBlockHash(blockHash, version).isOkOr:
|
|
|
|
return error
|
2024-05-15 06:07:59 +03:00
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
# If we already have the block locally, ignore the entire execution and just
|
|
|
|
# return a fake success.
|
2024-09-04 16:54:54 +07:00
|
|
|
if ben.chain.haveBlockLocally(blockHash):
|
2023-08-27 08:23:45 +07:00
|
|
|
warn "Ignoring already known beacon payload",
|
2024-06-14 14:31:08 +07:00
|
|
|
number = header.number, hash = blockHash.short
|
2023-08-27 08:23:45 +07:00
|
|
|
return validStatus(blockHash)
|
|
|
|
|
2024-05-17 08:38:46 +07:00
|
|
|
# If this block was rejected previously, keep rejecting it
|
|
|
|
let res = ben.checkInvalidAncestor(blockHash, blockHash)
|
|
|
|
if res.isSome:
|
|
|
|
return res.get
|
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
# If the parent is missing, we - in theory - could trigger a sync, but that
|
|
|
|
# would also entail a reorg. That is problematic if multiple sibling blocks
|
|
|
|
# are being fed to us, and even moreso, if some semi-distant uncle shortens
|
|
|
|
# our live chain. As such, payload execution will not permit reorgs and thus
|
|
|
|
# will not trigger a sync cycle. That is fine though, if we get a fork choice
|
|
|
|
# update after legit payload executions.
|
2024-09-04 16:54:54 +07:00
|
|
|
let parent = ben.chain.headerByHash(header.parentHash).valueOr:
|
2024-05-17 08:38:46 +07:00
|
|
|
return ben.delayPayloadImport(header)
|
2023-08-27 08:23:45 +07:00
|
|
|
|
|
|
|
# We have an existing parent, do some sanity checks to avoid the beacon client
|
|
|
|
# triggering too early
|
2024-06-14 14:31:08 +07:00
|
|
|
let ttd = com.ttd.get(high(UInt256))
|
2023-08-27 08:23:45 +07:00
|
|
|
|
|
|
|
if version == Version.V1:
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
let ptd = txFrame.getScore(header.parentHash).valueOr:
|
2024-06-14 07:10:00 +02:00
|
|
|
0.u256
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
let gptd = txFrame.getScore(parent.parentHash)
|
2024-07-17 17:05:53 +07:00
|
|
|
if ptd < ttd:
|
2023-08-27 08:23:45 +07:00
|
|
|
warn "Ignoring pre-merge payload",
|
2024-07-17 17:05:53 +07:00
|
|
|
number = header.number, hash = blockHash.short, ptd, ttd
|
|
|
|
return invalidStatus()
|
|
|
|
if parent.difficulty > 0.u256 and gptd.isSome and gptd.value >= ttd:
|
|
|
|
warn "Ignoring pre-merge parent block",
|
|
|
|
number = header.number, hash = blockHash.short, ptd, ttd
|
2023-08-27 08:23:45 +07:00
|
|
|
return invalidStatus()
|
|
|
|
|
|
|
|
if header.timestamp <= parent.timestamp:
|
|
|
|
warn "Invalid timestamp",
|
2024-06-14 14:31:08 +07:00
|
|
|
number = header.number, parentNumber = parent.number,
|
2023-11-03 21:41:05 +07:00
|
|
|
parent = parent.timestamp, header = header.timestamp
|
|
|
|
return invalidStatus(parent.blockHash, "Invalid timestamp")
|
2023-08-27 08:23:45 +07:00
|
|
|
|
2024-05-17 08:38:46 +07:00
|
|
|
# Another corner case: if the node is in snap sync mode, but the CL client
|
|
|
|
# tries to make it import a block. That should be denied as pushing something
|
|
|
|
# into the database directly will conflict with the assumptions of snap sync
|
|
|
|
# that it has an empty db that it can fill itself.
|
|
|
|
when false:
|
|
|
|
if api.eth.SyncMode() != downloader.FullSync:
|
|
|
|
return api.delayPayloadImport(header)
|
|
|
|
|
2024-09-04 16:54:54 +07:00
|
|
|
if not ben.chain.haveBlockAndState(header.parentHash):
|
2023-08-27 08:23:45 +07:00
|
|
|
ben.put(blockHash, header)
|
|
|
|
warn "State not available, ignoring new payload",
|
|
|
|
hash = blockHash,
|
2024-06-14 14:31:08 +07:00
|
|
|
number = header.number
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
let blockHash = latestValidHash(txFrame, parent, ttd)
|
2023-08-27 08:23:45 +07:00
|
|
|
return acceptedStatus(blockHash)
|
|
|
|
|
|
|
|
trace "Inserting block without sethead",
|
2024-06-14 14:31:08 +07:00
|
|
|
hash = blockHash, number = header.number
|
2024-09-04 16:54:54 +07:00
|
|
|
let vres = ben.chain.importBlock(blk)
|
2024-05-31 09:13:56 +02:00
|
|
|
if vres.isErr:
|
2024-11-02 15:00:45 +05:30
|
|
|
warn "Error importing block",
|
|
|
|
number = header.number,
|
|
|
|
hash = blockHash.short,
|
2024-12-21 12:59:30 +07:00
|
|
|
parent = header.parentHash.short,
|
2024-11-02 15:00:45 +05:30
|
|
|
error = vres.error()
|
2024-05-17 08:38:46 +07:00
|
|
|
ben.setInvalidAncestor(header, blockHash)
|
aristo: fork support via layers/txframes (#2960)
* aristo: fork support via layers/txframes
This change reorganises how the database is accessed: instead holding a
"current frame" in the database object, a dag of frames is created based
on the "base frame" held in `AristoDbRef` and all database access
happens through this frame, which can be thought of as a consistent
point-in-time snapshot of the database based on a particular fork of the
chain.
In the code, "frame", "transaction" and "layer" is used to denote more
or less the same thing: a dag of stacked changes backed by the on-disk
database.
Although this is not a requirement, in practice each frame holds the
change set of a single block - as such, the frame and its ancestors
leading up to the on-disk state represents the state of the database
after that block has been applied.
"committing" means merging the changes to its parent frame so that the
difference between them is lost and only the cumulative changes remain -
this facility enables frames to be combined arbitrarily wherever they
are in the dag.
In particular, it becomes possible to consolidate a set of changes near
the base of the dag and commit those to disk without having to re-do the
in-memory frames built on top of them - this is useful for "flattening"
a set of changes during a base update and sending those to storage
without having to perform a block replay on top.
Looking at abstractions, a side effect of this change is that the KVT
and Aristo are brought closer together by considering them to be part of
the "same" atomic transaction set - the way the code gets organised,
applying a block and saving it to the kvt happens in the same "logical"
frame - therefore, discarding the frame discards both the aristo and kvt
changes at the same time - likewise, they are persisted to disk together
- this makes reasoning about the database somewhat easier but has the
downside of increased memory usage, something that perhaps will need
addressing in the future.
Because the code reasons more strictly about frames and the state of the
persisted database, it also makes it more visible where ForkedChain
should be used and where it is still missing - in particular, frames
represent a single branch of history while forkedchain manages multiple
parallel forks - user-facing services such as the RPC should use the
latter, ie until it has been finalized, a getBlock request should
consider all forks and not just the blocks in the canonical head branch.
Another advantage of this approach is that `AristoDbRef` conceptually
becomes more simple - removing its tracking of the "current" transaction
stack simplifies reasoning about what can go wrong since this state now
has to be passed around in the form of `AristoTxRef` - as such, many of
the tests and facilities in the code that were dealing with "stack
inconsistency" are now structurally prevented from happening. The test
suite will need significant refactoring after this change.
Once this change has been merged, there are several follow-ups to do:
* there's no mechanism for keeping frames up to date as they get
committed or rolled back - TODO
* naming is confused - many names for the same thing for legacy reason
* forkedchain support is still missing in lots of code
* clean up redundant logic based on previous designs - in particular the
debug and introspection code no longer makes sense
* the way change sets are stored will probably need revisiting - because
it's a stack of changes where each frame must be interrogated to find an
on-disk value, with a base distance of 128 we'll at minimum have to
perform 128 frame lookups for *every* database interaction - regardless,
the "dag-like" nature will stay
* dispose and commit are poorly defined and perhaps redundant - in
theory, one could simply let the GC collect abandoned frames etc, though
it's likely an explicit mechanism will remain useful, so they stay for
now
More about the changes:
* `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less"
corresponds to the old `balancer` field
* `AristoDbRef.stack` is gone - instead, there's a chain of
`AristoTxRef` objects that hold their respective "layer" which has the
actual changes
* No more reasoning about "top" and "stack" - instead, each
`AristoTxRef` can be a "head" that "more or less" corresponds to the old
single-history `top` notion and its stack
* `level` still represents "distance to base" - it's computed from the
parent chain instead of being stored
* one has to be careful not to use frames where forkedchain was intended
- layers are only for a single branch of history!
* fix layer vtop after rollback
* engine fix
* Fix test_txpool
* Fix test_rpc
* Fix copyright year
* fix simulator
* Fix copyright year
* Fix copyright year
* Fix tracer
* Fix infinite recursion bug
* Remove aristo and kvt empty files
* Fic copyright year
* Fix fc chain_kvt
* ForkedChain refactoring
* Fix merge master conflict
* Fix copyright year
* Reparent txFrame
* Fix test
* Fix txFrame reparent again
* Cleanup and fix test
* UpdateBase bugfix and fix test
* Fixe newPayload bug discovered by hive
* Fix engine api fcu
* Clean up call template, chain_kvt, andn txguid
* Fix copyright year
* work around base block loading issue
* Add test
* Fix updateHead bug
* Fix updateBase bug
* Change func commitBase to proc commitBase
* Touch up and fix debug mode crash
---------
Co-authored-by: jangko <jangko128@gmail.com>
2025-02-06 08:04:50 +01:00
|
|
|
let blockHash = latestValidHash(txFrame, parent, ttd)
|
2024-05-31 09:13:56 +02:00
|
|
|
return invalidStatus(blockHash, vres.error())
|
2023-08-27 08:23:45 +07:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
ben.txPool.removeNewBlockTxs(blk, Opt.some(blockHash))
|
|
|
|
|
2024-10-28 03:50:04 +05:30
|
|
|
info "New payload received and validated",
|
2024-10-29 12:01:59 +07:00
|
|
|
number = header.number,
|
|
|
|
hash = blockHash.short,
|
2024-10-28 03:50:04 +05:30
|
|
|
parent = header.parentHash.short,
|
|
|
|
txs = blk.transactions.len,
|
|
|
|
gasUsed = header.gasUsed,
|
|
|
|
blobGas = header.blobGasUsed.get(0'u64)
|
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
return validStatus(blockHash)
|