BlockNumber, rlpHash, utils

This commit is contained in:
Yuriy Glukhov 2018-05-30 19:02:12 +03:00
parent 7877ae8312
commit 71e7cb836b
5 changed files with 28 additions and 9 deletions

View File

@ -1,2 +1,2 @@
import eth_common / [eth_types, rlp_serialization]
export eth_types, rlp_serialization
import eth_common / [eth_types, rlp_serialization, utils]
export eth_types, rlp_serialization, utils

View File

@ -9,4 +9,5 @@ requires "nim > 0.18.0",
"rlp",
"nimcrypto",
"ranges",
"stint"
"stint",
"https://github.com/status-im/nim-byteutils"

View File

@ -8,7 +8,7 @@ type
Blob* = seq[byte]
BloomFilter* = StUint[2048]
EthAddress* = MDigest[160]
EthAddress* = array[20, byte]
GasInt* = int64
## Type alias used for gas computation
@ -23,6 +23,8 @@ type
payload*: Blob
V*, R*, S*: UInt256
BlockNumber* = UInt256
BlockHeader* = object
parentHash*: Hash256
ommersHash*: Hash256
@ -32,10 +34,10 @@ type
receiptRoot*: Hash256
bloom*: BloomFilter
difficulty*: UInt256
blockNumber*: uint
blockNumber*: BlockNumber
gasLimit*: GasInt
gasUsed*: GasInt
timestamp*: uint64
timestamp*: EthTime
extraData*: Blob
mixDigest*: Hash256
nonce*: BlockNonce
@ -77,15 +79,15 @@ type
coinbase*: EthAddress
stateRoot*: Hash256
receiptRoot*: Hash256
blockNumber*: uint
blockNumber*: BlockNumber
HashOrNum* = object
case isHash*: bool
of true:
hash*: Hash256
else:
number*: uint
number*: BlockNumber
BlocksRequest* = object
startBlock*: HashOrNum
maxResults*, skip*, reverse*: uint
maxResults*, skip*, reverse*: uint64

View File

@ -12,3 +12,7 @@ proc read*(rlp: var Rlp, T: typedesc[EthTime]): T {.inline.} =
proc append*(rlpWriter: var RlpWriter, t: EthTime) {.inline.} =
rlpWriter.append(t.toUnix())
proc rlpHash*[T](v: T): Hash256 =
keccak256.digest(rlp.encode(v).toOpenArray)

12
eth_common/utils.nim Normal file
View File

@ -0,0 +1,12 @@
import nimcrypto, hashes, byteutils, eth_types
proc hash*(d: MDigest): Hash {.inline.} = hash(d.data)
proc toDigestAux(len: static[int], s: string): MDigest[len] =
hexToByteArray(s, result.data)
proc toDigest*(hexString: static[string]): auto =
toDigestAux(hexString.len div 2 * 8, hexString)
proc parseAddress*(hexString: string): EthAddress =
hexToByteArray(hexString, result)