Skip to content

Commit

Permalink
serve state in rpc (#2824)
Browse files Browse the repository at this point in the history
* simpler state replay logic

* add tests
  • Loading branch information
advaita-saha authored Nov 22, 2024
1 parent 7b2b59a commit ac2f3a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 4 additions & 5 deletions nimbus/rpc/server_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_forked_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit ac2f3a4

Please sign in to comment.