|
| 1 | +# Nimbus |
| 2 | +# Copyright (c) 2025-2026 Status Research & Development GmbH |
| 3 | +# Licensed under either of |
| 4 | +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0) |
| 6 | +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 7 | +# http://opensource.org/licenses/MIT) |
| 8 | +# at your option. This file may not be copied, modified, or distributed |
| 9 | +# except according to those terms. |
| 10 | + |
| 11 | +{.push raises: [].} |
| 12 | + |
| 13 | +import |
| 14 | + pkg/[chronicles, chronos], |
| 15 | + account/account_fetch, |
| 16 | + ./[helpers, header, state_db, worker_desc] |
| 17 | + |
| 18 | +# ------------------------------------------------------------------------------ |
| 19 | +# Private helpers |
| 20 | +# ------------------------------------------------------------------------------ |
| 21 | + |
| 22 | +template updateTarget( |
| 23 | + buddy: SnapPeerRef; |
| 24 | + info: static[string]; |
| 25 | + ) = |
| 26 | + ## Async/template |
| 27 | + ## |
| 28 | + block body: |
| 29 | + # Check whether explicit target setup is configured |
| 30 | + if buddy.ctx.pool.target.isSome(): |
| 31 | + let |
| 32 | + peer {.inject,used.} = $buddy.peer # logging only |
| 33 | + ctx = buddy.ctx |
| 34 | + |
| 35 | + # Single target block hash |
| 36 | + if ctx.pool.target.value.blockHash != BlockHash(zeroHash32): |
| 37 | + let rc = buddy.headerStateSet(ctx.pool.target.value.blockHash) |
| 38 | + if rc.isErr and rc.error: # real error |
| 39 | + trace info & ": failed fetching pivot hash", peer, |
| 40 | + hash=ctx.pool.target.value.blockHash.toStr |
| 41 | + elif ctx.pool.target.value.updateFile.len == 0: |
| 42 | + ctx.pool.target = Opt.none(SnapTarget) # No more target entries |
| 43 | + break body # noting more to do here |
| 44 | + else: |
| 45 | + var target = ctx.pool.target.value |
| 46 | + target.blockHash = BlockHash(zeroHash32) |
| 47 | + ctx.pool.target = Opt.some(target) |
| 48 | + |
| 49 | + # Check whether a file target setup is configured |
| 50 | + if 0 < ctx.pool.target.value.updateFile.len: |
| 51 | + discard buddy.headerStateLoad( ctx.pool.target.value.updateFile, info) |
| 52 | + |
| 53 | + discard # visual alignment |
| 54 | + |
| 55 | + |
| 56 | +template download( |
| 57 | + buddy: SnapPeerRef; |
| 58 | + state: StateDataRef; |
| 59 | + info: static[string]; |
| 60 | + ) = |
| 61 | + ## Async/template |
| 62 | + ## |
| 63 | + block body: |
| 64 | + let |
| 65 | + ctx = buddy.ctx |
| 66 | + sdb = ctx.pool.stateDB |
| 67 | + |
| 68 | + peer {.inject,used.} = $buddy.peer # logging only |
| 69 | + root {.inject,used.} = state.rootStr # logging only |
| 70 | + |
| 71 | + iv = state.unproc.fetchLeast(unprocAccountsRangeMax).valueOr: |
| 72 | + trace info & ": no more unpocessed", peer, root, stateDB=sdb.toStr |
| 73 | + break body |
| 74 | + |
| 75 | + accData = buddy.fetchAccounts(state.root, iv).valueOr: |
| 76 | + state.unproc.commit(iv, iv) # registry roll back |
| 77 | + state.downScore() |
| 78 | + trace info & ": account download failed", peer, root, |
| 79 | + iv=iv.to(float).toStr, stateDB=sdb.toStr |
| 80 | + break body |
| 81 | + |
| 82 | + # Accept, update registry |
| 83 | + if 0 < accData.accounts.len: |
| 84 | + let accTop = accData.accounts[^1].accHash.to(ItemKey) |
| 85 | + state.unproc.commit(iv, accTop + 1, iv.maxPt) |
| 86 | + state.unproc.overCommit(iv.maxPt + 1, accTop) |
| 87 | + state.upScore() |
| 88 | + else: |
| 89 | + state.downScore() |
| 90 | + |
| 91 | + debug info & ": accounts downloaded", peer, root, iv=iv.to(float).toStr, |
| 92 | + nAccounts=accData.accounts.len, nProof=accData.proof.len, |
| 93 | + stateDB=sdb.toStr |
| 94 | + discard |
| 95 | + |
| 96 | +# ------------------------------------------------------------------------------ |
| 97 | +# Public function |
| 98 | +# ------------------------------------------------------------------------------ |
| 99 | + |
| 100 | +template accountRangeImport*(buddy: SnapPeerRef; info: static[string]) = |
| 101 | + ## Async/template |
| 102 | + ## |
| 103 | + ## Fetch and stash account ranges -- TBD |
| 104 | + ## |
| 105 | + block body: |
| 106 | + let |
| 107 | + ctx = buddy.ctx |
| 108 | + sdb = ctx.pool.stateDB |
| 109 | + peer {.inject,used.} = $buddy.peer # logging only |
| 110 | + |
| 111 | + # Update state db, add new state records if pivots are available |
| 112 | + if buddy.only.pivotRoot.isNone(): |
| 113 | + let ethPeer = buddy.getEthPeer() # get `ethXX` peer if avail |
| 114 | + if not ethPeer.isNil: |
| 115 | + trace info & ": processing best/latest pivotHash", peer, |
| 116 | + hash=ethPeer.only.pivotHash.short |
| 117 | + buddy.headerStateSet(BlockHash(ethPeer.only.pivotHash)).isErrOr: |
| 118 | + buddy.only.pivotRoot = Opt.some(value) |
| 119 | + |
| 120 | + # Check for maual target settings |
| 121 | + buddy.updateTarget info |
| 122 | + |
| 123 | + if ctx.pool.stateDB.len == 0: |
| 124 | + trace info & ": no state records", peer, stateDB=sdb.toStr |
| 125 | + break body |
| 126 | + |
| 127 | + # Fetch for state DB items, start with pivot root |
| 128 | + for state in sdb.items(startWith=buddy.only.pivotRoot, truncate=true): |
| 129 | + if buddy.ctrl.stopped: |
| 130 | + break |
| 131 | + trace info & ": download state", peer, root=state.rootStr, |
| 132 | + stateDB=sdb.toStr |
| 133 | + buddy.download(state, info) |
| 134 | + |
| 135 | + discard # visual alignment |
| 136 | + |
| 137 | +# ------------------------------------------------------------------------------ |
| 138 | +# End |
| 139 | +# ------------------------------------------------------------------------------ |
0 commit comments