nimbus-eth1/tests/test_forked_chain.nim

645 lines
20 KiB
Nim
Raw Normal View History

# 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
# 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.
import
pkg/chronicles,
pkg/unittest2,
../execution_chain/common,
../execution_chain/config,
../execution_chain/utils/utils,
../execution_chain/core/chain/forked_chain,
../execution_chain/db/ledger,
./test_forked_chain/chain_debug
const
genesisFile = "tests/customgenesis/cancun123.json"
senderAddr = address"73cf19657412508833f618a15e8251306b3e6ee5"
type
TestEnv = object
conf: NimbusConf
proc setupEnv(): TestEnv =
let
conf = makeConfig(@[
"--custom-network:" & genesisFile
])
TestEnv(conf: conf)
proc newCom(env: TestEnv): CommonRef =
CommonRef.new(
newCoreDbRef DefaultDbMemory,
nil,
env.conf.networkId,
env.conf.networkParams
)
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
proc newCom(env: TestEnv, db: CoreDbRef): CommonRef =
CommonRef.new(
db,
nil,
env.conf.networkId,
env.conf.networkParams
)
proc makeBlk(txFrame: CoreDbTxRef, number: BlockNumber, parentBlk: Block): Block =
template parent(): Header =
parentBlk.header
var wds = newSeqOfCap[Withdrawal](number.int)
for i in 0..<number:
wds.add Withdrawal(
index: i,
validatorIndex: 1,
address: senderAddr,
amount: 1,
)
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 ledger = LedgerRef.init(txFrame)
for wd in wds:
ledger.addBalance(wd.address, wd.weiAmount)
ledger.persist()
let wdRoot = calcWithdrawalsRoot(wds)
var body = BlockBody(
withdrawals: Opt.some(move(wds))
)
let header = Header(
number : number,
parentHash : parent.blockHash,
difficulty : 0.u256,
timestamp : parent.timestamp + 1,
gasLimit : parent.gasLimit,
stateRoot : ledger.getStateRoot(),
transactionsRoot : parent.txRoot,
baseFeePerGas : parent.baseFeePerGas,
receiptsRoot : parent.receiptsRoot,
ommersHash : parent.ommersHash,
withdrawalsRoot: Opt.some(wdRoot),
blobGasUsed : parent.blobGasUsed,
excessBlobGas : parent.excessBlobGas,
parentBeaconBlockRoot: parent.parentBeaconBlockRoot,
)
Block.init(header, body)
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
proc makeBlk(txFrame: CoreDbTxRef, number: BlockNumber, parentBlk: Block, extraData: byte): Block =
var blk = txFrame.makeBlk(number, parentBlk)
blk.header.extraData = @[extraData]
blk
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
proc headHash(c: ForkedChainRef): Hash32 =
c.latestTxFrame.getCanonicalHead().expect("canonical head exists").blockHash
func blockHash(x: Block): Hash32 =
x.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
proc wdWritten(c: ForkedChainRef, blk: Block): int =
if blk.header.withdrawalsRoot.isSome:
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
c.latestTxFrame.getWithdrawals(blk.header.withdrawalsRoot.get).
expect("withdrawals exists").len
else:
0
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
template checkImportBlock(chain, blk) =
let res = chain.importBlock(blk)
check res.isOk
if res.isErr:
debugEcho "IMPORT BLOCK FAIL: ", res.error
debugEcho "Block Number: ", blk.header.number
template checkImportBlockErr(chain, blk) =
let res = chain.importBlock(blk)
check res.isErr
if res.isOk:
debugEcho "IMPORT BLOCK SHOULD FAIL"
debugEcho "Block Number: ", blk.header.number
template checkForkChoice(chain, a, b) =
let res = chain.forkChoice(a.blockHash, b.blockHash)
check res.isOk
if res.isErr:
debugEcho "FORK CHOICE FAIL: ", res.error
debugEcho "Block Number: ", a.header.number, " ", b.header.number
template checkForkChoiceErr(chain, a, b) =
let res = chain.forkChoice(a.blockHash, b.blockHash)
check res.isErr
if res.isOk:
debugEcho "FORK CHOICE SHOULD FAIL"
debugEcho "Block Number: ", a.header.number, " ", b.header.number
template checkPersisted(chain, blk) =
let res = chain.baseTxFrame.getBlockHeader(blk.blockHash)
check res.isOk
if res.isErr:
debugEcho "CHECK FINALIZED FAIL: ", res.error
debugEcho "Block Number: ", blk.header.number
proc forkedChainMain*() =
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
suite "ForkedChainRef tests":
var env = setupEnv()
let
cc = env.newCom
genesisHash = cc.genesisHeader.blockHash
genesis = Block.init(cc.genesisHeader, BlockBody())
baseTxFrame = cc.db.baseTxFrame()
let
blk1 = baseTxFrame.makeBlk(1, genesis)
blk2 = baseTxFrame.makeBlk(2, blk1)
blk3 = baseTxFrame.makeBlk(3, blk2)
dbTx = baseTxFrame.txFrameBegin
blk4 = dbTx.makeBlk(4, blk3)
blk5 = dbTx.makeBlk(5, blk4)
blk6 = dbTx.makeBlk(6, blk5)
blk7 = dbTx.makeBlk(7, blk6)
dbTx.dispose()
let
B4 = baseTxFrame.makeBlk(4, blk3, 1.byte)
dbTx2 = baseTxFrame.txFrameBegin
B5 = dbTx2.makeBlk(5, B4)
B6 = dbTx2.makeBlk(6, B5)
B7 = dbTx2.makeBlk(7, B6)
dbTx2.dispose()
let
C5 = baseTxFrame.makeBlk(5, blk4, 1.byte)
C6 = baseTxFrame.makeBlk(6, C5)
C7 = baseTxFrame.makeBlk(7, C6)
test "newBase == oldBase":
const info = "newBase == oldBase"
let com = env.newCom()
var chain = ForkedChainRef.init(com)
# same header twice
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
check chain.validate info & " (1)"
# no parent
checkImportBlockErr(chain, blk5)
check chain.headHash == genesisHash
check chain.latestHash == blk3.blockHash
check chain.validate info & " (2)"
# finalized > head -> error
checkForkChoiceErr(chain, blk1, blk3)
check chain.validate info & " (3)"
# blk4 is not part of chain
checkForkChoiceErr(chain, blk4, blk2)
# finalized > head -> error
checkForkChoiceErr(chain, blk1, blk2)
# blk4 is not part of chain
checkForkChoiceErr(chain, blk2, blk4)
# finalized < head -> ok
checkForkChoice(chain, blk2, blk1)
check chain.headHash == blk2.blockHash
check chain.latestHash == blk2.blockHash
check chain.validate info & " (7)"
# finalized == head -> ok
checkForkChoice(chain, blk2, blk2)
check chain.headHash == blk2.blockHash
check chain.latestHash == blk2.blockHash
check chain.baseNumber == 0'u64
check chain.validate info & " (8)"
# baggage written
check chain.wdWritten(blk1) == 1
check chain.wdWritten(blk2) == 2
check chain.validate info & " (9)"
test "newBase on activeBranch":
const info = "newBase on activeBranch"
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, blk4)
check chain.validate info & " (1)"
# newbase == head
checkForkChoice(chain, blk7, blk6)
check chain.validate info & " (2)"
check chain.headHash == blk7.blockHash
check chain.latestHash == blk7.blockHash
check chain.baseBranch == chain.activeBranch
check chain.wdWritten(blk7) == 7
# head - baseDistance must been persisted
checkPersisted(chain, blk3)
# make sure aristo not wiped out baggage
check chain.wdWritten(blk3) == 3
check chain.validate info & " (9)"
test "newBase between oldBase and head":
const info = "newBase between oldBase and head"
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
check chain.validate info & " (1)"
checkForkChoice(chain, blk7, blk6)
check chain.validate info & " (2)"
check chain.headHash == blk7.blockHash
check chain.latestHash == blk7.blockHash
check chain.baseBranch == chain.activeBranch
check chain.wdWritten(blk6) == 6
check chain.wdWritten(blk7) == 7
# head - baseDistance must been persisted
checkPersisted(chain, blk3)
# make sure aristo not wiped out baggage
check chain.wdWritten(blk3) == 3
check chain.validate info & " (9)"
test "newBase == oldBase, fork and stay on that fork":
const info = "newBase == oldBase, fork .."
let com = env.newCom()
var chain = ForkedChainRef.init(com)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, B7, B5)
check chain.headHash == B7.blockHash
check chain.latestHash == B7.blockHash
check chain.baseNumber == 0'u64
check chain.branches.len == 2
check chain.validate info & " (9)"
test "newBase move forward, fork and stay on that fork":
const info = "newBase move forward, fork .."
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
checkImportBlock(chain, B4)
check chain.validate info & " (1)"
checkForkChoice(chain, B6, B4)
check chain.validate info & " (2)"
check chain.headHash == B6.blockHash
check chain.latestHash == B6.blockHash
check chain.baseNumber == 3'u64
check chain.branches.len == 2
check chain.validate info & " (9)"
test "newBase on shorter canonical arc, remove oldBase branches":
const info = "newBase on shorter canonical, remove oldBase branches"
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, B7, B6)
check chain.validate info & " (2)"
check chain.headHash == B7.blockHash
check chain.latestHash == B7.blockHash
check chain.baseNumber == 4'u64
check chain.branches.len == 1
check chain.validate info & " (9)"
test "newBase on curbed non-canonical arc":
const info = "newBase on curbed non-canonical .."
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 5)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, B7, B5)
check chain.validate info & " (2)"
check chain.headHash == B7.blockHash
check chain.latestHash == B7.blockHash
check chain.baseNumber > 0
check chain.baseNumber < B4.header.number
check chain.branches.len == 2
check chain.validate info & " (9)"
test "newBase == oldBase, fork and return to old chain":
const info = "newBase == oldBase, fork .."
let com = env.newCom()
var chain = ForkedChainRef.init(com)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, blk7, blk5)
check chain.validate info & " (2)"
check chain.headHash == blk7.blockHash
check chain.latestHash == blk7.blockHash
check chain.baseNumber == 0'u64
check chain.validate info & " (9)"
test "newBase on activeBranch, fork and return to old chain":
const info = "newBase on activeBranch, fork .."
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
checkImportBlock(chain, blk4)
check chain.validate info & " (1)"
checkForkChoice(chain, blk7, blk5)
check chain.validate info & " (2)"
check chain.headHash == blk7.blockHash
check chain.latestHash == blk7.blockHash
check chain.baseBranch == chain.activeBranch
check chain.validate info & " (9)"
test "newBase on shorter canonical arc, discard arc with oldBase" &
" (ign dup block)":
const info = "newBase on shorter canonical .."
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
checkImportBlock(chain, blk4)
check chain.validate info & " (1)"
checkForkChoice(chain, B7, B5)
check chain.validate info & " (2)"
check chain.headHash == B7.blockHash
check chain.latestHash == B7.blockHash
check chain.baseNumber == 4'u64
check chain.branches.len == 1
check chain.validate info & " (9)"
test "newBase on longer canonical arc, discard new branch":
const info = "newBase on longer canonical .."
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, blk7, blk5)
check chain.validate info & " (2)"
check chain.headHash == blk7.blockHash
check chain.latestHash == blk7.blockHash
check chain.baseNumber > 0
check chain.baseNumber < blk5.header.number
check chain.branches.len == 1
check chain.validate info & " (9)"
test "headerByNumber":
const info = "headerByNumber"
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, blk4)
checkImportBlock(chain, blk5)
checkImportBlock(chain, blk6)
checkImportBlock(chain, blk7)
checkImportBlock(chain, B4)
checkImportBlock(chain, B5)
checkImportBlock(chain, B6)
checkImportBlock(chain, B7)
check chain.validate info & " (1)"
checkForkChoice(chain, blk7, blk5)
check chain.validate info & " (2)"
# cursor
check chain.headerByNumber(8).isErr
check chain.headerByNumber(7).expect("OK").number == 7
check chain.headerByNumber(7).expect("OK").blockHash == blk7.blockHash
# from db
check chain.headerByNumber(3).expect("OK").number == 3
check chain.headerByNumber(3).expect("OK").blockHash == blk3.blockHash
# base
check chain.headerByNumber(4).expect("OK").number == 4
check chain.headerByNumber(4).expect("OK").blockHash == blk4.blockHash
# from cache
check chain.headerByNumber(5).expect("OK").number == 5
check chain.headerByNumber(5).expect("OK").blockHash == blk5.blockHash
check chain.validate info & " (9)"
test "3 branches, alternating imports":
const info = "3 branches, alternating imports"
let com = env.newCom()
var chain = ForkedChainRef.init(com, baseDistance = 3)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkImportBlock(chain, B4)
checkImportBlock(chain, blk4)
checkImportBlock(chain, B5)
checkImportBlock(chain, blk5)
checkImportBlock(chain, C5)
checkImportBlock(chain, B6)
checkImportBlock(chain, blk6)
checkImportBlock(chain, C6)
checkImportBlock(chain, B7)
checkImportBlock(chain, blk7)
checkImportBlock(chain, C7)
check chain.validate info & " (1)"
check chain.latestHash == C7.blockHash
check chain.latestNumber == 7'u64
check chain.branches.len == 3
checkForkChoice(chain, B7, blk3)
check chain.validate info & " (2)"
check chain.branches.len == 3
checkForkChoice(chain, B7, B6)
check chain.validate info & " (2)"
check chain.branches.len == 1
test "importing blocks with new CommonRef and FC instance, 3 blocks":
const info = "importing blocks with new CommonRef and FC instance, 3 blocks"
let com = env.newCom()
let chain = ForkedChainRef.init(com, baseDistance = 0)
checkImportBlock(chain, blk1)
checkImportBlock(chain, blk2)
checkImportBlock(chain, blk3)
checkForkChoice(chain, blk3, blk3)
check chain.validate info & " (1)"
let cc = env.newCom(com.db)
let fc = ForkedChainRef.init(cc, baseDistance = 0)
check fc.headHash == blk3.blockHash
checkImportBlock(fc, blk4)
checkForkChoice(fc, blk4, blk4)
check chain.validate info & " (2)"
test "importing blocks with new CommonRef and FC instance, 1 block":
const info = "importing blocks with new CommonRef and FC instance, 1 block"
let com = env.newCom()
let chain = ForkedChainRef.init(com, baseDistance = 0)
checkImportBlock(chain, blk1)
checkForkChoice(chain, blk1, blk1)
check chain.validate info & " (1)"
let cc = env.newCom(com.db)
let fc = ForkedChainRef.init(cc, baseDistance = 0)
check fc.headHash == blk1.blockHash
checkImportBlock(fc, blk2)
checkForkChoice(fc, blk2, blk2)
check chain.validate info & " (2)"
forkedChainMain()