serve state in rpc (#2824)

* simpler state replay logic

* add tests
This commit is contained in:
Advaita Saha 2024-11-22 16:45:52 +05:30 committed by GitHub
parent 7b2b59a976
commit ac2f3a4358
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 5 deletions

View File

@ -99,11 +99,10 @@ proc headerFromTag(api: ServerAPIRef, blockTag: Opt[BlockTag]): Result[Header, s
proc ledgerFromTag(api: ServerAPIRef, blockTag: BlockTag): Result[LedgerRef, string] =
let header = ?api.headerFromTag(blockTag)
if api.chain.stateReady(header):
ok(LedgerRef.init(api.com.db))
else:
# TODO: Replay state?
err("Block state not ready")
if not api.chain.stateReady(header):
api.chain.replaySegment(header.blockHash)
ok(LedgerRef.init(api.com.db))
proc blockFromTag(api: ServerAPIRef, blockTag: BlockTag): Result[Block, string] =
if blockTag.kind == bidAlias:

View File

@ -420,5 +420,21 @@ proc forkedChainMain*() =
check chain.headerByNumber(5).expect("OK").number == 5
check chain.headerByNumber(5).expect("OK").blockHash == blk5.blockHash
test "Import after Replay Segment":
let com = env.newCom()
var chain = newForkedChain(com, com.genesisHeader, baseDistance = 3)
check chain.importBlock(blk1).isOk
check chain.importBlock(blk2).isOk
check chain.importBlock(blk3).isOk
check chain.importBlock(blk4).isOk
check chain.importBlock(blk5).isOk
chain.replaySegment(blk2.header.blockHash)
chain.replaySegment(blk5.header.blockHash)
check chain.importBlock(blk6).isOk
check chain.importBlock(blk7).isOk
when isMainModule:
forkedChainMain()