2021-04-08 15:52:10 +01:00
|
|
|
# Nimbus
|
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
|
|
|
# Copyright (c) 2018-2025 Status Research & Development GmbH
|
2021-04-08 15:52:10 +01: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-02-14 21:27:17 +01:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-04-08 15:52:10 +01:00
|
|
|
import
|
2023-11-20 20:22:27 +00:00
|
|
|
std/[options, sets, strformat],
|
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 16:32:20 +02:00
|
|
|
stew/assign2,
|
2023-12-12 19:12:56 +00:00
|
|
|
../db/ledger,
|
2022-12-02 11:35:41 +07:00
|
|
|
../common/[common, evmforks],
|
2023-08-27 13:13:37 +07:00
|
|
|
./interpreter/[op_codes, gas_costs],
|
2024-06-07 15:24:32 +07:00
|
|
|
./types,
|
|
|
|
./evm_errors
|
2021-07-27 12:28:05 +01:00
|
|
|
|
2024-07-04 20:48:36 +07:00
|
|
|
func forkDeterminationInfoForVMState(vmState: BaseVMState): ForkDeterminationInfo =
|
|
|
|
forkDeterminationInfo(vmState.parent.number + 1, vmState.blockCtx.timestamp)
|
|
|
|
|
|
|
|
func determineFork(vmState: BaseVMState): EVMFork =
|
|
|
|
vmState.com.toEVMFork(vmState.forkDeterminationInfoForVMState)
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc init(
|
2023-04-12 08:39:11 -04:00
|
|
|
self: BaseVMState;
|
2023-12-12 19:12:56 +00:00
|
|
|
ac: LedgerRef,
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header;
|
2023-09-24 22:25:41 +07:00
|
|
|
blockCtx: BlockContext;
|
2023-04-12 08:39:11 -04:00
|
|
|
com: CommonRef;
|
2023-08-02 17:17:40 +07:00
|
|
|
tracer: TracerRef,
|
2024-05-30 16:03:54 +07:00
|
|
|
flags: set[VMFlag] = self.flags) =
|
2022-01-18 16:19:32 +00:00
|
|
|
## Initialisation helper
|
2024-12-18 13:21:20 +01:00
|
|
|
# Take care to (re)set all fields since the VMState might be recycled
|
2022-12-02 11:35:41 +07:00
|
|
|
self.com = com
|
2024-12-21 20:46:13 +07:00
|
|
|
self.ledger = ac
|
2024-12-18 13:21:20 +01:00
|
|
|
self.gasPool = blockCtx.gasLimit
|
|
|
|
assign(self.parent, parent)
|
|
|
|
assign(self.blockCtx, blockCtx)
|
|
|
|
const txCtx = default(TxContext)
|
|
|
|
assign(self.txCtx, txCtx)
|
2024-01-09 23:15:19 +08:00
|
|
|
self.flags = flags
|
2024-07-04 20:48:36 +07:00
|
|
|
self.fork = self.determineFork
|
2024-12-18 13:21:20 +01:00
|
|
|
self.tracer = tracer
|
|
|
|
self.receipts.setLen(0)
|
|
|
|
self.cumulativeGasUsed = 0
|
2024-07-04 20:48:36 +07:00
|
|
|
self.gasCosts = self.fork.forkToSchedule
|
2024-12-18 13:21:20 +01:00
|
|
|
self.blobGasUsed = 0'u64
|
|
|
|
self.allLogs.setLen(0)
|
|
|
|
self.gasRefunded = 0
|
2022-01-18 16:19:32 +00:00
|
|
|
|
2024-12-18 13:21:20 +01:00
|
|
|
func blockCtx(header: Header): BlockContext =
|
2023-09-24 22:25:41 +07:00
|
|
|
BlockContext(
|
|
|
|
timestamp : header.timestamp,
|
|
|
|
gasLimit : header.gasLimit,
|
2024-06-14 14:31:08 +07:00
|
|
|
baseFeePerGas: header.baseFeePerGas,
|
2023-09-24 22:25:41 +07:00
|
|
|
prevRandao : header.prevRandao,
|
|
|
|
difficulty : header.difficulty,
|
2024-07-17 17:05:53 +07:00
|
|
|
coinbase : header.coinbase,
|
2023-09-24 22:25:41 +07:00
|
|
|
excessBlobGas: header.excessBlobGas.get(0'u64),
|
2024-10-08 09:37:36 +07:00
|
|
|
parentHash : header.parentHash,
|
2023-09-24 22:25:41 +07:00
|
|
|
)
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
# --------------
|
|
|
|
|
|
|
|
proc `$`*(vmState: BaseVMState): string
|
2024-06-19 03:27:54 +02:00
|
|
|
{.gcsafe, raises: [].} =
|
2022-01-18 16:19:32 +00:00
|
|
|
if vmState.isNil:
|
|
|
|
result = "nil"
|
|
|
|
else:
|
2022-12-02 11:35:41 +07:00
|
|
|
result = &"VMState:"&
|
2024-06-14 14:31:08 +07:00
|
|
|
&"\n blockNumber: {vmState.parent.number + 1}"
|
2022-01-18 16:19:32 +00:00
|
|
|
|
|
|
|
proc new*(
|
2023-09-24 22:25:41 +07:00
|
|
|
T: type BaseVMState;
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header; ## parent header, account sync position
|
2023-09-24 22:25:41 +07:00
|
|
|
blockCtx: BlockContext;
|
|
|
|
com: CommonRef; ## block chain config
|
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: CoreDbTxRef;
|
2024-08-16 08:22:51 +02:00
|
|
|
tracer: TracerRef = nil,
|
|
|
|
storeSlotHash = false): T =
|
2022-01-18 16:19:32 +00:00
|
|
|
## Create a new `BaseVMState` descriptor from a parent block header. This
|
|
|
|
## function internally constructs a new account state cache rooted at
|
|
|
|
## `parent.stateRoot`
|
|
|
|
##
|
|
|
|
## This `new()` constructor and its variants (see below) provide a save
|
|
|
|
## `BaseVMState` environment where the account state cache is synchronised
|
|
|
|
## with the `parent` block header.
|
|
|
|
new result
|
|
|
|
result.init(
|
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
|
|
|
ac = LedgerRef.init(txFrame, storeSlotHash),
|
2023-09-24 22:25:41 +07:00
|
|
|
parent = parent,
|
|
|
|
blockCtx = blockCtx,
|
|
|
|
com = com,
|
|
|
|
tracer = tracer)
|
|
|
|
|
|
|
|
proc reinit*(self: BaseVMState; ## Object descriptor
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header; ## parent header, account sync pos.
|
2024-06-14 15:56:56 +02:00
|
|
|
blockCtx: BlockContext;
|
2024-05-30 16:03:54 +07:00
|
|
|
): bool =
|
2023-12-12 19:12:56 +00:00
|
|
|
## Re-initialise state descriptor. The `LedgerRef` database is
|
2024-10-27 19:56:28 +01:00
|
|
|
## re-initilaise only if its `getStateRoot()` doe not point to `parent.stateRoot`,
|
2024-06-14 15:56:56 +02:00
|
|
|
## already. Accumulated state data are reset. When linear, we assume that
|
|
|
|
## the state recently processed the parent block.
|
2022-01-18 16:19:32 +00:00
|
|
|
##
|
2023-12-12 19:12:56 +00:00
|
|
|
## This function returns `true` unless the `LedgerRef` database could be
|
2024-10-27 19:56:28 +01:00
|
|
|
## queries about its `getStateRoot()`, i.e. `isTopLevelClean` evaluated `true`. If
|
2022-01-18 16:19:32 +00:00
|
|
|
## this function returns `false`, the function argument `self` is left
|
|
|
|
## untouched.
|
2024-12-21 20:46:13 +07:00
|
|
|
if not self.ledger.isTopLevelClean:
|
2024-12-18 13:21:20 +01:00
|
|
|
return false
|
|
|
|
|
|
|
|
let
|
|
|
|
tracer = self.tracer
|
|
|
|
com = self.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
|
|
|
ac = self.ledger
|
2024-12-18 13:21:20 +01:00
|
|
|
flags = self.flags
|
|
|
|
self.init(
|
|
|
|
ac = ac,
|
|
|
|
parent = parent,
|
|
|
|
blockCtx = blockCtx,
|
|
|
|
com = com,
|
|
|
|
tracer = tracer,
|
|
|
|
flags = flags)
|
|
|
|
true
|
2022-01-18 16:19:32 +00:00
|
|
|
|
2023-09-24 22:25:41 +07:00
|
|
|
proc reinit*(self: BaseVMState; ## Object descriptor
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header; ## parent header, account sync pos.
|
|
|
|
header: Header; ## header with tx environment data fields
|
2024-05-30 16:03:54 +07:00
|
|
|
): bool =
|
2022-01-18 16:19:32 +00:00
|
|
|
## Variant of `reinit()`. The `parent` argument is used to sync the accounts
|
|
|
|
## cache and the `header` is used as a container to pass the `timestamp`,
|
|
|
|
## `gasLimit`, and `fee` values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2024-06-14 15:56:56 +02:00
|
|
|
self.reinit(
|
2023-09-24 22:25:41 +07:00
|
|
|
parent = parent,
|
2024-12-18 13:21:20 +01:00
|
|
|
blockCtx = blockCtx(header),
|
2023-09-24 22:25:41 +07:00
|
|
|
)
|
2022-01-18 16:19:32 +00:00
|
|
|
|
|
|
|
proc init*(
|
2023-08-02 17:17:40 +07:00
|
|
|
self: BaseVMState; ## Object descriptor
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header; ## parent header, account sync position
|
|
|
|
header: Header; ## header with tx environment data fields
|
2023-08-02 17:17:40 +07:00
|
|
|
com: CommonRef; ## block chain config
|
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: CoreDbTxRef;
|
2024-08-16 08:22:51 +02:00
|
|
|
tracer: TracerRef = nil,
|
|
|
|
storeSlotHash = false) =
|
2022-01-18 16:19:32 +00:00
|
|
|
## Variant of `new()` constructor above for in-place initalisation. The
|
|
|
|
## `parent` argument is used to sync the accounts cache and the `header`
|
|
|
|
## is used as a container to pass the `timestamp`, `gasLimit`, and `fee`
|
|
|
|
## values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2022-03-15 17:21:41 +00:00
|
|
|
self.init(
|
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
|
|
|
ac = LedgerRef.init(txFrame, storeSlotHash),
|
2023-09-24 22:25:41 +07:00
|
|
|
parent = parent,
|
2024-12-18 13:21:20 +01:00
|
|
|
blockCtx = blockCtx(header),
|
2023-09-24 22:25:41 +07:00
|
|
|
com = com,
|
|
|
|
tracer = tracer)
|
2022-01-18 16:19:32 +00:00
|
|
|
|
|
|
|
proc new*(
|
2023-08-02 17:17:40 +07:00
|
|
|
T: type BaseVMState;
|
2024-10-16 07:04:12 +05:30
|
|
|
parent: Header; ## parent header, account sync position
|
|
|
|
header: Header; ## header with tx environment data fields
|
2023-08-02 17:17:40 +07:00
|
|
|
com: CommonRef; ## block chain config
|
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: CoreDbTxRef;
|
2024-08-16 08:22:51 +02:00
|
|
|
tracer: TracerRef = nil,
|
|
|
|
storeSlotHash = false): T =
|
2022-01-18 16:19:32 +00:00
|
|
|
## This is a variant of the `new()` constructor above where the `parent`
|
|
|
|
## argument is used to sync the accounts cache and the `header` is used
|
|
|
|
## as a container to pass the `timestamp`, `gasLimit`, and `fee` values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2021-04-08 15:52:10 +01:00
|
|
|
new result
|
2022-01-18 16:19:32 +00:00
|
|
|
result.init(
|
2023-08-02 17:17:40 +07:00
|
|
|
parent = parent,
|
|
|
|
header = header,
|
|
|
|
com = 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 = txFrame,
|
2024-08-16 08:22:51 +02:00
|
|
|
tracer = tracer,
|
|
|
|
storeSlotHash = storeSlotHash)
|
2022-01-18 16:19:32 +00:00
|
|
|
|
2024-10-16 07:04:12 +05:30
|
|
|
func coinbase*(vmState: BaseVMState): Address =
|
2023-09-24 22:25:41 +07:00
|
|
|
vmState.blockCtx.coinbase
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2024-07-17 17:05:53 +07:00
|
|
|
func blockNumber*(vmState: BaseVMState): BlockNumber =
|
2021-04-08 15:52:10 +01:00
|
|
|
# it should return current block number
|
2024-06-14 14:31:08 +07:00
|
|
|
# and not head.number
|
|
|
|
vmState.parent.number + 1
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2024-10-08 09:37:36 +07:00
|
|
|
proc proofOfStake*(vmState: BaseVMState): bool =
|
|
|
|
vmState.com.proofOfStake(Header(
|
|
|
|
number: vmState.blockNumber,
|
|
|
|
parentHash: vmState.blockCtx.parentHash,
|
|
|
|
difficulty: vmState.blockCtx.difficulty,
|
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
|
|
|
), vmState.ledger.txFrame)
|
2024-10-08 09:37:36 +07:00
|
|
|
|
|
|
|
proc difficultyOrPrevRandao*(vmState: BaseVMState): UInt256 =
|
|
|
|
if vmState.proofOfStake():
|
2022-02-05 16:15:50 +07:00
|
|
|
# EIP-4399/EIP-3675
|
2023-09-24 22:25:41 +07:00
|
|
|
UInt256.fromBytesBE(vmState.blockCtx.prevRandao.data)
|
2022-02-05 16:15:50 +07:00
|
|
|
else:
|
2023-09-24 22:25:41 +07:00
|
|
|
vmState.blockCtx.difficulty
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2024-07-17 17:05:53 +07:00
|
|
|
func baseFeePerGas*(vmState: BaseVMState): UInt256 =
|
2024-06-14 14:31:08 +07:00
|
|
|
vmState.blockCtx.baseFeePerGas.get(0.u256)
|
2021-06-27 20:19:22 +07:00
|
|
|
|
2023-01-31 13:38:08 +01:00
|
|
|
method getAncestorHash*(
|
2024-10-16 07:04:12 +05:30
|
|
|
vmState: BaseVMState, blockNumber: BlockNumber): Hash32 {.gcsafe, base.} =
|
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 db = vmState.ledger.txFrame
|
2024-11-07 08:24:21 +07:00
|
|
|
let blockHash = db.getBlockHash(blockNumber).valueOr:
|
|
|
|
return default(Hash32)
|
|
|
|
blockHash
|
2022-12-02 11:35:41 +07:00
|
|
|
|
2024-12-21 20:46:13 +07:00
|
|
|
proc readOnlyLedger*(vmState: BaseVMState): ReadOnlyLedger {.inline.} =
|
|
|
|
ReadOnlyLedger(vmState.ledger)
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2024-12-21 20:46:13 +07:00
|
|
|
template mutateLedger*(vmState: BaseVMState, body: untyped) =
|
2021-04-08 15:52:10 +01:00
|
|
|
block:
|
2024-12-21 20:46:13 +07:00
|
|
|
var db {.inject.} = vmState.ledger
|
2021-04-08 15:52:10 +01:00
|
|
|
body
|
|
|
|
|
|
|
|
proc getAndClearLogEntries*(vmState: BaseVMState): seq[Log] =
|
2024-12-21 20:46:13 +07:00
|
|
|
vmState.ledger.getAndClearLogEntries()
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc status*(vmState: BaseVMState): bool =
|
2021-04-08 15:52:10 +01:00
|
|
|
ExecutionOK in vmState.flags
|
|
|
|
|
|
|
|
proc `status=`*(vmState: BaseVMState, status: bool) =
|
|
|
|
if status: vmState.flags.incl ExecutionOK
|
|
|
|
else: vmState.flags.excl ExecutionOK
|
|
|
|
|
2024-06-08 16:05:00 +08:00
|
|
|
proc collectWitnessData*(vmState: BaseVMState): bool =
|
|
|
|
CollectWitnessData in vmState.flags
|
2021-04-08 15:52:10 +01:00
|
|
|
|
2024-06-08 16:05:00 +08:00
|
|
|
proc `collectWitnessData=`*(vmState: BaseVMState, status: bool) =
|
|
|
|
if status: vmState.flags.incl CollectWitnessData
|
|
|
|
else: vmState.flags.excl CollectWitnessData
|
2024-01-22 17:11:37 +08:00
|
|
|
|
2023-08-02 17:17:40 +07:00
|
|
|
func tracingEnabled*(vmState: BaseVMState): bool =
|
|
|
|
vmState.tracer.isNil.not
|
|
|
|
|
|
|
|
proc captureTxStart*(vmState: BaseVMState, gasLimit: GasInt) =
|
|
|
|
if vmState.tracingEnabled:
|
|
|
|
vmState.tracer.captureTxStart(gasLimit)
|
|
|
|
|
|
|
|
proc captureTxEnd*(vmState: BaseVMState, restGas: GasInt) =
|
|
|
|
if vmState.tracingEnabled:
|
|
|
|
vmState.tracer.captureTxEnd(restGas)
|
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureStart*(vmState: BaseVMState, comp: Computation,
|
2024-10-16 07:04:12 +05:30
|
|
|
sender: Address, to: Address,
|
2023-08-02 17:17:40 +07:00
|
|
|
create: bool, input: openArray[byte],
|
2023-08-25 16:07:20 +07:00
|
|
|
gasLimit: GasInt, value: UInt256) =
|
2023-08-02 17:17:40 +07:00
|
|
|
if vmState.tracingEnabled:
|
2023-08-25 16:07:20 +07:00
|
|
|
vmState.tracer.captureStart(comp, sender, to, create, input, gasLimit, value)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureEnd*(vmState: BaseVMState, comp: Computation, output: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
gasUsed: GasInt, error: Opt[string]) =
|
2023-08-02 17:17:40 +07:00
|
|
|
if vmState.tracingEnabled:
|
2023-08-25 16:07:20 +07:00
|
|
|
vmState.tracer.captureEnd(comp, output, gasUsed, error)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureEnter*(vmState: BaseVMState, comp: Computation, op: Op,
|
2024-10-16 07:04:12 +05:30
|
|
|
sender: Address, to: Address,
|
2023-08-25 16:07:20 +07:00
|
|
|
input: openArray[byte], gasLimit: GasInt,
|
2023-08-02 17:17:40 +07:00
|
|
|
value: UInt256) =
|
|
|
|
if vmState.tracingEnabled:
|
2023-08-25 16:07:20 +07:00
|
|
|
vmState.tracer.captureEnter(comp, op, sender, to, input, gasLimit, value)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureExit*(vmState: BaseVMState, comp: Computation, output: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
gasUsed: GasInt, error: Opt[string]) =
|
2023-08-02 17:17:40 +07:00
|
|
|
if vmState.tracingEnabled:
|
2023-08-25 16:07:20 +07:00
|
|
|
vmState.tracer.captureExit(comp, output, gasUsed, error)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureOpStart*(vmState: BaseVMState, comp: Computation, pc: int,
|
2023-08-02 17:17:40 +07:00
|
|
|
op: Op, gas: GasInt,
|
|
|
|
depth: int): int =
|
|
|
|
if vmState.tracingEnabled:
|
2023-08-27 13:13:37 +07:00
|
|
|
let fixed = vmState.gasCosts[op].kind == GckFixed
|
|
|
|
result = vmState.tracer.captureOpStart(comp, fixed, pc, op, gas, depth)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-27 13:13:37 +07:00
|
|
|
proc captureGasCost*(vmState: BaseVMState,
|
2023-08-25 16:07:20 +07:00
|
|
|
comp: Computation,
|
2023-08-27 13:13:37 +07:00
|
|
|
op: Op, gasCost: GasInt, gasRemaining: GasInt,
|
2023-08-23 16:29:33 +07:00
|
|
|
depth: int) =
|
2024-07-13 20:42:49 +02:00
|
|
|
let fixed = vmState.gasCosts[op].kind == GckFixed
|
|
|
|
vmState.tracer.captureGasCost(comp, fixed, op, gasCost, gasRemaining, depth)
|
2023-08-25 16:07:20 +07:00
|
|
|
|
|
|
|
proc captureOpEnd*(vmState: BaseVMState, comp: Computation, pc: int,
|
2024-07-07 13:52:11 +07:00
|
|
|
op: Op, gas: GasInt, refund: int64,
|
2023-08-02 17:17:40 +07:00
|
|
|
rData: openArray[byte],
|
|
|
|
depth: int, opIndex: int) =
|
|
|
|
if vmState.tracingEnabled:
|
2023-08-27 13:13:37 +07:00
|
|
|
let fixed = vmState.gasCosts[op].kind == GckFixed
|
|
|
|
vmState.tracer.captureOpEnd(comp, fixed, pc, op, gas, refund, rData, depth, opIndex)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc captureFault*(vmState: BaseVMState, comp: Computation, pc: int,
|
2024-07-07 13:52:11 +07:00
|
|
|
op: Op, gas: GasInt, refund: int64,
|
2023-08-02 17:17:40 +07:00
|
|
|
rData: openArray[byte],
|
2024-06-07 21:39:58 +00:00
|
|
|
depth: int, error: Opt[string]) =
|
2023-08-02 17:17:40 +07:00
|
|
|
if vmState.tracingEnabled:
|
2023-08-27 13:13:37 +07:00
|
|
|
let fixed = vmState.gasCosts[op].kind == GckFixed
|
|
|
|
vmState.tracer.captureFault(comp, fixed, pc, op, gas, refund, rData, depth, error)
|
2023-08-02 17:17:40 +07:00
|
|
|
|
2023-08-25 16:07:20 +07:00
|
|
|
proc capturePrepare*(vmState: BaseVMState, comp: Computation, depth: int) =
|
2023-08-02 17:17:40 +07:00
|
|
|
if vmState.tracingEnabled:
|
2023-08-25 16:07:20 +07:00
|
|
|
vmState.tracer.capturePrepare(comp, depth)
|