nimbus-eth1/nimbus/rpc/rpc_utils.nim

34 lines
1.3 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
2018-12-11 10:05:49 +00:00
import hexstrings, nimcrypto, eth_common, byteutils,
../db/[db_chain, state_db, storage_types], strutils,
../constants, stint
func toAddress*(value: EthAddressStr): EthAddress = hexToPaddedByteArray[20](value.string)
func toHash*(value: array[32, byte]): Hash256 {.inline.} =
result.data = value
func toHash*(value: EthHashStr): Hash256 {.inline.} =
result = hexToPaddedByteArray[32](value.string).toHash
2018-12-11 10:05:49 +00:00
func headerFromTag*(chain: BaseChainDB, blockTag: string): BlockHeader =
let tag = blockTag.toLowerAscii
case tag
of "latest": result = chain.getCanonicalHead()
of "earliest": result = chain.getBlockHeader(GENESIS_BLOCK_NUMBER)
of "pending":
#TODO: Implement get pending block
raise newException(ValueError, "Pending tag not yet implemented")
else:
# Raises are trapped and wrapped in JSON when returned to the user.
tag.validateHexQuantity
let blockNum = stint.fromHex(UInt256, tag)
result = chain.getBlockHeader(blockNum)