2023-03-02 09:57:58 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[os, strutils],
|
|
|
|
chronicles,
|
|
|
|
eth/[common, p2p]
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "sync-ctrl"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-04-26 15:46:42 +00:00
|
|
|
proc getDataLine(
|
|
|
|
name: string;
|
|
|
|
lineNum: int;
|
|
|
|
): string {.gcsafe, raises: [IOError].} =
|
2023-03-02 09:57:58 +00:00
|
|
|
if name.fileExists:
|
|
|
|
let file = name.open
|
|
|
|
defer: file.close
|
2023-04-26 15:46:42 +00:00
|
|
|
let linesRead = file.readAll.splitLines
|
|
|
|
if lineNum < linesRead.len:
|
|
|
|
return linesRead[lineNum].strip
|
2023-03-02 09:57:58 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc syncCtrlBlockNumberFromFile*(
|
2023-04-26 15:46:42 +00:00
|
|
|
fileName: Option[string]; # Optional file name
|
|
|
|
lineNum = 0; # Read line from file
|
2023-03-02 09:57:58 +00:00
|
|
|
): Result[BlockNumber,void] =
|
|
|
|
## Returns a block number from the file name argument `fileName`. The first
|
|
|
|
## line of the file is parsed as a decimal encoded block number.
|
|
|
|
if fileName.isSome:
|
|
|
|
let file = fileName.get
|
|
|
|
try:
|
2023-04-26 15:46:42 +00:00
|
|
|
let data = file.getDataLine(lineNum)
|
2023-03-02 09:57:58 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
let num = parse(data,UInt256)
|
|
|
|
return ok(num.toBlockNumber)
|
|
|
|
except CatchableError as e:
|
|
|
|
let
|
|
|
|
name {.used.} = $e.name
|
|
|
|
msg {.used.} = e.msg
|
|
|
|
debug "Exception while parsing block number", file, name, msg
|
|
|
|
err()
|
|
|
|
|
2023-03-07 14:23:22 +00:00
|
|
|
proc syncCtrlHashOrBlockNumFromFile*(
|
2023-04-26 15:46:42 +00:00
|
|
|
fileName: Option[string]; # Optional file name
|
|
|
|
lineNum = 0; # Read line from file
|
2023-03-07 14:23:22 +00:00
|
|
|
): Result[HashOrNum,void] =
|
|
|
|
## Returns a block number or a hash from the file name argument `fileName`.
|
|
|
|
## A block number is decimal encoded and a hash is expexted to be a 66 hex
|
|
|
|
## digits string startnib wiyh `0x`.
|
|
|
|
if fileName.isSome:
|
|
|
|
let file = fileName.get
|
|
|
|
|
|
|
|
# Parse value dump and fetch a header from the peer (if any)
|
|
|
|
try:
|
2023-04-26 15:46:42 +00:00
|
|
|
let data = file.getDataLine(lineNum)
|
2023-03-07 14:23:22 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
if 66 == data.len:
|
|
|
|
let hash = HashOrNum(
|
|
|
|
isHash: true,
|
|
|
|
hash: Hash256(
|
|
|
|
data: UInt256.fromHex(data).toBytesBE))
|
|
|
|
return ok(hash)
|
|
|
|
else:
|
|
|
|
let num = HashOrNum(
|
|
|
|
isHash: false,
|
|
|
|
number: parse(data,UInt256))
|
|
|
|
return ok(num)
|
|
|
|
except CatchableError as e:
|
|
|
|
let
|
|
|
|
name {.used.} = $e.name
|
|
|
|
msg {.used.} = e.msg
|
|
|
|
debug "Exception while parsing hash or block number", file, name, msg
|
|
|
|
err()
|
|
|
|
|
2023-03-02 09:57:58 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|