2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-05-17 11:09:49 +00:00
|
|
|
# Copyright (c) 2018-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.
|
|
|
|
|
|
|
|
import
|
2022-09-16 07:24:12 +00:00
|
|
|
std/[math, hashes],
|
2022-09-03 18:15:35 +00:00
|
|
|
eth/common/eth_types_rlp,
|
2022-05-17 11:09:49 +00:00
|
|
|
stew/byteutils
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
type
|
|
|
|
BlockHash* = distinct Hash256
|
|
|
|
## Hash of a block, goes with `BlockNumber`.
|
|
|
|
##
|
|
|
|
## Note that the `ethXX` protocol driver always uses the
|
2022-08-04 08:04:30 +00:00
|
|
|
## underlying `Hash256` type which needs to be converted to `BlockHash`.
|
2022-05-17 11:09:49 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-06-16 08:58:50 +00:00
|
|
|
# Public constructors
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc new*(T: type BlockHash): T =
|
2022-08-04 08:04:30 +00:00
|
|
|
Hash256().T
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-06-16 08:58:50 +00:00
|
|
|
# Public (probably non-trivial) type conversions
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc to*(num: SomeInteger; T: type float): T =
|
|
|
|
## Convert to float. Result an d argument are not strictly equivalent. Though
|
|
|
|
## sort of `(num.to(float) + 0.5).int == num` might hold in many cases.
|
|
|
|
num.T
|
|
|
|
|
|
|
|
proc to*(longNum: UInt256; T: type float): T =
|
|
|
|
## Convert to float (see also comment at `num.to(float)`, above.)
|
|
|
|
let mantissaLen = 256 - longNum.leadingZeros
|
2022-06-16 08:58:50 +00:00
|
|
|
if mantissaLen <= 64:
|
2022-08-04 08:04:30 +00:00
|
|
|
longNum.truncate(uint64).T
|
2022-06-16 08:58:50 +00:00
|
|
|
else:
|
|
|
|
let exp = mantissaLen - 64
|
2022-08-04 08:04:30 +00:00
|
|
|
(longNum shr exp).truncate(uint64).T * (2.0 ^ exp)
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(w: BlockHash; T: type Hash256): T =
|
2022-08-04 08:04:30 +00:00
|
|
|
## Syntactic sugar
|
2022-05-17 11:09:49 +00:00
|
|
|
w.Hash256
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(w: seq[BlockHash]; T: type seq[Hash256]): T =
|
2022-05-17 11:09:49 +00:00
|
|
|
## Ditto
|
|
|
|
cast[seq[Hash256]](w)
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
proc to*(bh: BlockHash; T: type HashOrNum): T =
|
|
|
|
## Convert argument blocj hash `bh` to `HashOrNum`
|
|
|
|
T(isHash: true, hash: bh.Hash256)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc read*(rlp: var Rlp, T: type BlockHash): T
|
2022-06-16 08:58:50 +00:00
|
|
|
{.gcsafe, raises: [Defect,RlpError]} =
|
|
|
|
## RLP mixin reader
|
|
|
|
rlp.read(Hash256).T
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc append*(writer: var RlpWriter; h: BlockHash) =
|
2022-08-04 08:04:30 +00:00
|
|
|
## RLP mixin
|
|
|
|
append(writer, h.Hash256)
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc `==`*(a: BlockHash; b: Hash256): bool =
|
2022-08-04 08:04:30 +00:00
|
|
|
a.Hash256 == b
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc `==`*[T: BlockHash](a,b: T): bool =
|
2022-08-04 08:04:30 +00:00
|
|
|
a.Hash256 == b.Hash256
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc hash*(root: BlockHash): Hash =
|
2022-08-12 15:42:07 +00:00
|
|
|
## Mixin for `Table` or `KeyedQueue`
|
2022-07-01 11:42:17 +00:00
|
|
|
root.Hash256.data.hash
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-06-16 08:58:50 +00:00
|
|
|
# Public printing and pretty printing
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
func toHex*(hash: Hash256): string =
|
2022-06-16 08:58:50 +00:00
|
|
|
## Shortcut for `byteutils.toHex(hash.data)`
|
2022-05-23 16:53:19 +00:00
|
|
|
hash.data.toHex
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
func `$`*(h: BlockHash): string =
|
2022-07-01 11:42:17 +00:00
|
|
|
$h.Hash256.data.toHex
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
func `$`*(blob: Blob): string =
|
|
|
|
blob.toHex
|
|
|
|
|
|
|
|
func `$`*(hashOrNum: HashOrNum): string =
|
|
|
|
# It's always obvious which one from the visible length of the string.
|
|
|
|
if hashOrNum.isHash: $hashOrNum.hash
|
|
|
|
else: $hashOrNum.number
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public debug printing helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
func traceStep*(request: BlocksRequest): string =
|
|
|
|
var str = if request.reverse: "-" else: "+"
|
|
|
|
if request.skip < high(typeof(request.skip)):
|
|
|
|
return str & $(request.skip + 1)
|
|
|
|
return static($(high(typeof(request.skip)).u256 + 1))
|
2022-05-17 11:09:49 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|