|
| 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 | + std/sequtils, |
| 15 | + pkg/[chronicles, chronos], |
| 16 | + ../[helpers, mpt, state_db, worker_desc], |
| 17 | + ./code_fetch |
| 18 | + |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | +# Private helpers |
| 21 | +# ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +proc register(state: StateDataRef, acc: seq[(ItemKey,CodeHash)]) = |
| 24 | + for (key,val) in acc: |
| 25 | + state.register(key,val) |
| 26 | + |
| 27 | +# ------------------------------------------------------------------------------ |
| 28 | +# Private functions |
| 29 | +# ------------------------------------------------------------------------------ |
| 30 | + |
| 31 | +template downloadImpl( |
| 32 | + buddy: SnapPeerRef; # Snap peer |
| 33 | + state: StateDataRef; # Current state |
| 34 | + accounts: seq[(ItemKey,CodeHash)]; # Acoounts with contracts |
| 35 | + info: static[string]; # Log message prefix |
| 36 | + ): bool = |
| 37 | + ## Async/template |
| 38 | + ## |
| 39 | + ## The template will return `true` if there were some data that could be |
| 40 | + ## downloaded and processed. |
| 41 | + ## |
| 42 | + var bodyRc = false |
| 43 | + block body: |
| 44 | + let |
| 45 | + ctx = buddy.ctx |
| 46 | + adb = ctx.pool.mptAsm |
| 47 | + sRoot = state.root |
| 48 | + peerID = buddy.peerID |
| 49 | + |
| 50 | + peer {.inject,used.} = $buddy.peer # logging only |
| 51 | + root {.inject,used.} = state.rootStr # logging only |
| 52 | + |
| 53 | + # Fetch storage slots from argument list `accounts` |
| 54 | + var start {.inject.} = 0 |
| 55 | + while start < accounts.len: |
| 56 | + let |
| 57 | + accLeft = if start == 0: accounts else: accounts[start .. ^1] |
| 58 | + codeHashes = accLeft.mapIt(it[1]) |
| 59 | + |
| 60 | + # Fetch from network |
| 61 | + let data = buddy.fetchCodes(sRoot, codeHashes).valueOr: |
| 62 | + state.register accLeft # stash data and return |
| 63 | + trace info & ": fetching codes failed", peer, root, |
| 64 | + start, nAccLeft=accLeft.len |
| 65 | + break body # error => return |
| 66 | + |
| 67 | + # Store byte codes on database |
| 68 | + adb.putRawAyteCode( |
| 69 | + sRoot, accLeft[0][0], accLeft[^1][0], |
| 70 | + codeHashes.zip data.codes, peerID).isOkOr: |
| 71 | + state.register(accLeft) # stash data and return |
| 72 | + trace info & ": storing codes failed", peer, root, |
| 73 | + start, nAccLeft=accLeft.len |
| 74 | + break body # error => return |
| 75 | + |
| 76 | + start += data.codes.len |
| 77 | + bodyRc = true # did something |
| 78 | + |
| 79 | + # End `while` |
| 80 | + |
| 81 | + bodyRc |
| 82 | + |
| 83 | +template downloadFromQueue( |
| 84 | + buddy: SnapPeerRef; # Snap peer |
| 85 | + state: StateDataRef; # Current state |
| 86 | + info: static[string]; # Log message prefix |
| 87 | + ): bool = |
| 88 | + ## Async/template |
| 89 | + ## |
| 90 | + ## Process stashed unprocessed byte codes from the state DB. |
| 91 | + ## |
| 92 | + ## The template will return `true` if there were some data that could be |
| 93 | + ## downloaded and processed. |
| 94 | + ## |
| 95 | + var bodyRc = false |
| 96 | + block body: |
| 97 | + let |
| 98 | + peer {.inject,used.} = $buddy.peer # logging only |
| 99 | + root {.inject,used.} = state.rootStr # logging only |
| 100 | + var |
| 101 | + accQueue: seq[(ItemKey,CodeHash)] |
| 102 | + |
| 103 | + for w in state.stoItems: |
| 104 | + accQueue.add (w.key, w.data.code) |
| 105 | + state.delCode w.key |
| 106 | + |
| 107 | + if 0 < accQueue.len: |
| 108 | + trace info & ": processing from codes queue", peer, root, |
| 109 | + nAccQueue=accQueue.len |
| 110 | + if buddy.downloadImpl(state, accQueue, info): |
| 111 | + bodyRc = true |
| 112 | + |
| 113 | + bodyRc |
| 114 | + |
| 115 | +# ------------------------------------------------------------------------------ |
| 116 | +# Public function |
| 117 | +# ------------------------------------------------------------------------------ |
| 118 | + |
| 119 | +template codeDownload*( |
| 120 | + buddy: SnapPeerRef; # Snap peer |
| 121 | + state: StateDataRef; # Current state |
| 122 | + accounts: seq[SnapAccount]; # Acoounts with sub-tries |
| 123 | + info: static[string]; # Log message prefix |
| 124 | + ) = |
| 125 | + ## Async/template |
| 126 | + ## |
| 127 | + block body: |
| 128 | + let acc = accounts |
| 129 | + .filterIt(not it.accBody.codeHash.isEmpty) |
| 130 | + .mapIt( (it.accHash.to(ItemKey), |
| 131 | + it.accBody.codeHash.to(Hash32).to(CodeHash)) ) |
| 132 | + |
| 133 | + if buddy.ctrl.stopped: |
| 134 | + state.register acc # stash data and return |
| 135 | + break body # all done |
| 136 | + |
| 137 | + discard buddy.downloadImpl(state, acc, info) |
| 138 | + |
| 139 | + while not buddy.ctrl.stopped and |
| 140 | + 0 < state.len and |
| 141 | + buddy.downloadFromQueue(state, info): |
| 142 | + continue |
| 143 | + |
| 144 | + discard # visual alignment |
| 145 | + |
| 146 | +# ------------------------------------------------------------------------------ |
| 147 | +# End |
| 148 | +# ------------------------------------------------------------------------------ |
0 commit comments