2022-08-24 13:44:18 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2019 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
|
|
|
|
std/[os, sequtils, strformat, strutils],
|
|
|
|
eth/common,
|
2022-09-03 18:15:35 +00:00
|
|
|
nimcrypto/utils,
|
2022-08-24 13:44:18 +00:00
|
|
|
stew/byteutils,
|
2022-09-02 18:16:09 +00:00
|
|
|
../../nimbus/sync/snap/[range_desc, worker/db/hexary_desc],
|
|
|
|
../../nimbus/sync/protocol,
|
2022-08-24 13:44:18 +00:00
|
|
|
./gunzip
|
|
|
|
|
|
|
|
type
|
|
|
|
UndumpState = enum
|
2022-09-02 18:16:09 +00:00
|
|
|
UndumpStoragesHeader
|
|
|
|
UndumpStoragesRoot
|
|
|
|
UndumpSlotsHeader
|
|
|
|
UndumpSlotsAccount
|
|
|
|
UndumpSlotsRoot
|
|
|
|
UndumpSlotsList
|
2022-08-24 13:44:18 +00:00
|
|
|
UndumpProofs
|
|
|
|
UndumpCommit
|
|
|
|
UndumpError
|
2022-09-02 18:16:09 +00:00
|
|
|
UndumpSkipUntilCommit
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
UndumpStorages* = object
|
2022-08-24 13:44:18 +00:00
|
|
|
## Palatable output for iterator
|
|
|
|
root*: Hash256
|
2022-09-02 18:16:09 +00:00
|
|
|
data*: AccountStorageRange
|
|
|
|
seenAccounts*: int
|
|
|
|
seenStorages*: int
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template say(args: varargs[untyped]) =
|
|
|
|
# echo args
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc toByteSeq(s: string): seq[byte] =
|
2022-09-03 18:15:35 +00:00
|
|
|
utils.fromHex(s)
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
proc fromHex(T: type Hash256; s: string): T =
|
|
|
|
result.data = ByteArray32.fromHex(s)
|
|
|
|
|
|
|
|
proc fromHex(T: type NodeTag; s: string): T =
|
|
|
|
UInt256.fromBytesBE(ByteArray32.fromHex(s)).T
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public capture
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
proc dumpStorages*(
|
2022-08-24 13:44:18 +00:00
|
|
|
root: Hash256;
|
2022-09-02 18:16:09 +00:00
|
|
|
data: AccountStorageRange
|
2022-08-24 13:44:18 +00:00
|
|
|
): string =
|
2022-09-02 18:16:09 +00:00
|
|
|
## Dump account and storage data in parseable Ascii text
|
2022-08-24 13:44:18 +00:00
|
|
|
proc ppStr(blob: Blob): string =
|
|
|
|
blob.mapIt(it.toHex(2)).join.toLowerAscii
|
|
|
|
|
|
|
|
proc ppStr(hash: Hash256): string =
|
|
|
|
hash.data.mapIt(it.toHex(2)).join.toLowerAscii
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
result = "storages " & $data.storages.len & " " & $data.proof.len & "\n"
|
2022-08-24 13:44:18 +00:00
|
|
|
result &= root.ppStr & "\n"
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
for n in 0 ..< data.storages.len:
|
|
|
|
let slots = data.storages[n]
|
|
|
|
result &= "# -- " & $n & " --\n"
|
|
|
|
result &= "slots " & $slots.data.len & "\n"
|
|
|
|
result &= slots.account.accHash.ppStr & "\n"
|
|
|
|
result &= slots.account.storageRoot.ppStr & "\n"
|
|
|
|
|
|
|
|
for i in 0 ..< slots.data.len:
|
|
|
|
result &= slots.data[i].slotHash.ppStr & " "
|
|
|
|
result &= slots.data[i].slotData.ppStr & "\n"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
if 0 < data.proof.len:
|
|
|
|
result &= "# ----\n"
|
|
|
|
for n in 0 ..< data.proof.len:
|
|
|
|
result &= data.proof[n].ppStr & "\n"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
result &= "commit\n"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public undump
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
iterator undumpNextStorages*(gzFile: string): UndumpStorages =
|
2022-08-24 13:44:18 +00:00
|
|
|
var
|
|
|
|
line = ""
|
|
|
|
lno = 0
|
2022-09-02 18:16:09 +00:00
|
|
|
state = UndumpStoragesHeader
|
|
|
|
data: UndumpStorages
|
2022-08-24 13:44:18 +00:00
|
|
|
nAccounts = 0u
|
|
|
|
nProofs = 0u
|
2022-09-02 18:16:09 +00:00
|
|
|
nSlots = 0u
|
|
|
|
seenAccounts = 0
|
|
|
|
seenStorages = 0
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
if not gzFile.fileExists:
|
|
|
|
raiseAssert &"No such file: \"{gzFile}\""
|
|
|
|
|
|
|
|
for lno,line in gzFile.gunzipLines:
|
|
|
|
if line.len == 0 or line[0] == '#':
|
|
|
|
continue
|
|
|
|
var flds = line.split
|
|
|
|
#echo ">>> ",
|
|
|
|
# " lno=", lno,
|
|
|
|
# " state=", state,
|
|
|
|
# " nAccounts=", nAccounts,
|
|
|
|
# " nProofs=", nProofs,
|
2022-09-02 18:16:09 +00:00
|
|
|
# " nSlots=", nSlots,
|
2022-08-24 13:44:18 +00:00
|
|
|
# " flds=", flds
|
|
|
|
|
|
|
|
case state:
|
2022-09-02 18:16:09 +00:00
|
|
|
of UndumpSkipUntilCommit:
|
|
|
|
if flds.len == 1 and flds[0] == "commit":
|
|
|
|
state = UndumpStoragesHeader
|
|
|
|
|
|
|
|
of UndumpStoragesHeader, UndumpError:
|
|
|
|
if flds.len == 3 and flds[0] == "storages":
|
2022-08-24 13:44:18 +00:00
|
|
|
nAccounts = flds[1].parseUInt
|
|
|
|
nProofs = flds[2].parseUInt
|
|
|
|
data.reset
|
2022-09-02 18:16:09 +00:00
|
|
|
state = UndumpStoragesRoot
|
|
|
|
seenStorages.inc
|
|
|
|
continue
|
|
|
|
if 1 < flds.len and flds[0] == "accounts":
|
|
|
|
state = UndumpSkipUntilCommit
|
|
|
|
seenAccounts.inc
|
2022-08-24 13:44:18 +00:00
|
|
|
continue
|
|
|
|
if state != UndumpError:
|
|
|
|
state = UndumpError
|
2022-09-02 18:16:09 +00:00
|
|
|
say &"*** line {lno}: expected storages header, got {line}"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
of UndumpStoragesRoot:
|
2022-08-24 13:44:18 +00:00
|
|
|
if flds.len == 1:
|
|
|
|
data.root = Hash256.fromHex(flds[0])
|
2022-09-02 18:16:09 +00:00
|
|
|
if 0 < nAccounts:
|
|
|
|
state = UndumpSlotsHeader
|
|
|
|
continue
|
|
|
|
state = UndumpCommit
|
|
|
|
continue
|
|
|
|
state = UndumpError
|
|
|
|
say &"*** line {lno}: expected storages state root, got {line}"
|
|
|
|
|
|
|
|
of UndumpSlotsHeader:
|
|
|
|
if flds.len == 2 and flds[0] == "slots":
|
|
|
|
nSlots = flds[1].parseUInt
|
|
|
|
state = UndumpSlotsAccount
|
|
|
|
continue
|
|
|
|
state = UndumpError
|
|
|
|
say &"*** line {lno}: expected slots header, got {line}"
|
|
|
|
|
|
|
|
of UndumpSlotsAccount:
|
|
|
|
if flds.len == 1:
|
|
|
|
data.data.storages.add AccountSlots(
|
|
|
|
account: AccountSlotsHeader(
|
|
|
|
accHash: Hash256.fromHex(flds[0])))
|
|
|
|
state = UndumpSlotsRoot
|
2022-08-24 13:44:18 +00:00
|
|
|
continue
|
|
|
|
state = UndumpError
|
2022-09-02 18:16:09 +00:00
|
|
|
say &"*** line {lno}: expected slots account, got {line}"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
of UndumpSlotsRoot:
|
2022-08-24 13:44:18 +00:00
|
|
|
if flds.len == 1:
|
2022-09-02 18:16:09 +00:00
|
|
|
data.data.storages[^1].account.storageRoot = Hash256.fromHex(flds[0])
|
|
|
|
state = UndumpSlotsList
|
2022-08-24 13:44:18 +00:00
|
|
|
continue
|
|
|
|
state = UndumpError
|
2022-09-02 18:16:09 +00:00
|
|
|
say &"*** line {lno}: expected slots storage root, got {line}"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
of UndumpSlotsList:
|
2022-08-24 13:44:18 +00:00
|
|
|
if flds.len == 2:
|
2022-09-02 18:16:09 +00:00
|
|
|
data.data.storages[^1].data.add SnapStorage(
|
|
|
|
slotHash: Hash256.fromHex(flds[0]),
|
|
|
|
slotData: flds[1].toByteSeq)
|
|
|
|
nSlots.dec
|
|
|
|
if 0 < nSlots:
|
|
|
|
continue
|
2022-08-24 13:44:18 +00:00
|
|
|
nAccounts.dec
|
2022-09-02 18:16:09 +00:00
|
|
|
if 0 < nAccounts:
|
|
|
|
state = UndumpSlotsHeader
|
|
|
|
continue
|
|
|
|
if 0 < nProofs:
|
2022-08-24 13:44:18 +00:00
|
|
|
state = UndumpProofs
|
2022-09-02 18:16:09 +00:00
|
|
|
continue
|
|
|
|
state = UndumpCommit
|
2022-08-24 13:44:18 +00:00
|
|
|
continue
|
|
|
|
state = UndumpError
|
2022-09-02 18:16:09 +00:00
|
|
|
say &"*** line {lno}: expected slot data, got {line}"
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
of UndumpProofs:
|
|
|
|
if flds.len == 1:
|
|
|
|
data.data.proof.add flds[0].toByteSeq
|
|
|
|
nProofs.dec
|
|
|
|
if nProofs <= 0:
|
|
|
|
state = UndumpCommit
|
|
|
|
continue
|
|
|
|
state = UndumpError
|
|
|
|
say &"*** expected proof data, got {line}"
|
|
|
|
|
|
|
|
of UndumpCommit:
|
|
|
|
if flds.len == 1 and flds[0] == "commit":
|
2022-09-02 18:16:09 +00:00
|
|
|
data.seenAccounts = seenAccounts
|
|
|
|
data.seenStorages = seenStorages
|
2022-08-24 13:44:18 +00:00
|
|
|
yield data
|
2022-09-02 18:16:09 +00:00
|
|
|
state = UndumpStoragesHeader
|
2022-08-24 13:44:18 +00:00
|
|
|
continue
|
|
|
|
state = UndumpError
|
|
|
|
say &"*** line {lno}: expected commit, got {line}"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|