2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 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.
|
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
import
|
|
|
|
tables, sequtils, algorithm,
|
2018-12-14 12:46:56 +00:00
|
|
|
rlp, ranges, state_db, nimcrypto, eth_trie/[hexary, db], eth_common, byteutils, chronicles,
|
2019-01-08 10:47:40 +00:00
|
|
|
../errors, ../block_types, ../utils/header, ../constants, ./storage_types
|
2018-02-12 16:59:12 +00:00
|
|
|
|
2018-01-17 12:57:50 +00:00
|
|
|
type
|
2018-08-29 08:49:01 +00:00
|
|
|
BaseChainDB* = ref object
|
2018-12-31 03:27:02 +00:00
|
|
|
db* : TrieDatabaseRef
|
2018-11-30 10:07:20 +00:00
|
|
|
pruneTrie*: bool
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
KeyType = enum
|
|
|
|
blockNumberToHash
|
|
|
|
blockHashToScore
|
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
TransactionKey = tuple
|
|
|
|
blockNumber: BlockNumber
|
|
|
|
index: int
|
|
|
|
|
2018-12-04 02:06:53 +00:00
|
|
|
proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool = true): BaseChainDB =
|
2018-02-14 16:38:01 +00:00
|
|
|
new(result)
|
2018-02-12 16:59:12 +00:00
|
|
|
result.db = db
|
2018-11-30 10:07:20 +00:00
|
|
|
result.pruneTrie = pruneTrie
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-02-14 16:38:01 +00:00
|
|
|
proc `$`*(db: BaseChainDB): string =
|
|
|
|
result = "BaseChainDB"
|
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
proc getBlockHeader*(self: BaseChainDB; blockHash: Hash256, output: var BlockHeader): bool =
|
2018-09-06 17:05:22 +00:00
|
|
|
let data = self.db.get(genericHashKey(blockHash).toOpenArray).toRange
|
|
|
|
if data.len != 0:
|
|
|
|
output = rlp.decode(data, BlockHeader)
|
|
|
|
result = true
|
2018-08-03 11:56:49 +00:00
|
|
|
|
|
|
|
proc getBlockHeader*(self: BaseChainDB, blockHash: Hash256): BlockHeader =
|
|
|
|
## Returns the requested block header as specified by block hash.
|
|
|
|
##
|
|
|
|
## Raises BlockNotFound if it is not present in the db.
|
|
|
|
if not self.getBlockHeader(blockHash, result):
|
2018-05-30 16:11:15 +00:00
|
|
|
raise newException(BlockNotFound, "No block with hash " & blockHash.data.toHex)
|
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
proc getHash(self: BaseChainDB, key: DbKey, output: var Hash256): bool {.inline.} =
|
2018-09-06 17:05:22 +00:00
|
|
|
let data = self.db.get(key.toOpenArray).toRange
|
|
|
|
if data.len != 0:
|
|
|
|
output = rlp.decode(data, Hash256)
|
2018-08-03 11:56:49 +00:00
|
|
|
result = true
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
proc getCanonicalHead*(self: BaseChainDB): BlockHeader =
|
2018-08-03 11:56:49 +00:00
|
|
|
var headHash: Hash256
|
|
|
|
if not self.getHash(canonicalHeadHashKey(), headHash) or
|
|
|
|
not self.getBlockHeader(headHash, result):
|
2018-06-20 12:33:57 +00:00
|
|
|
raise newException(CanonicalHeadNotFound,
|
|
|
|
"No canonical head set for this chain")
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
proc getBlockHash*(self: BaseChainDB, n: BlockNumber, output: var Hash256): bool {.inline.} =
|
|
|
|
## Return the block hash for the given block number.
|
|
|
|
self.getHash(blockNumberToHashKey(n), output)
|
2018-05-30 16:11:15 +00:00
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
proc getBlockHash*(self: BaseChainDB, n: BlockNumber): Hash256 {.inline.} =
|
|
|
|
## Return the block hash for the given block number.
|
|
|
|
if not self.getHash(blockNumberToHashKey(n), result):
|
|
|
|
raise newException(BlockNotFound, "No block hash for number " & $n)
|
|
|
|
|
|
|
|
proc getBlockHeader*(self: BaseChainDB; n: BlockNumber, output: var BlockHeader): bool =
|
|
|
|
## Returns the block header with the given number in the canonical chain.
|
|
|
|
var blockHash: Hash256
|
|
|
|
if self.getBlockHash(n, blockHash):
|
|
|
|
result = self.getBlockHeader(blockHash, output)
|
|
|
|
|
|
|
|
proc getBlockHeader*(self: BaseChainDB; n: BlockNumber): BlockHeader =
|
|
|
|
## Returns the block header with the given number in the canonical chain.
|
|
|
|
## Raises BlockNotFound error if the block is not in the DB.
|
|
|
|
self.getBlockHeader(self.getBlockHash(n))
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-12-25 14:21:47 +00:00
|
|
|
proc getScore*(self: BaseChainDB; blockHash: Hash256): uint64 =
|
|
|
|
rlp.decode(self.db.get(blockHashToScoreKey(blockHash).toOpenArray).toRange, uint64)
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
iterator findNewAncestors(self: BaseChainDB; header: BlockHeader): BlockHeader =
|
2018-05-30 16:11:15 +00:00
|
|
|
## Returns the chain leading up from the given header until the first ancestor it has in
|
|
|
|
## common with our canonical chain.
|
|
|
|
var h = header
|
2018-08-03 11:56:49 +00:00
|
|
|
var orig: BlockHeader
|
2018-05-30 16:11:15 +00:00
|
|
|
while true:
|
2018-08-03 11:56:49 +00:00
|
|
|
if self.getBlockHeader(h.blockNumber, orig) and orig.hash == h.hash:
|
|
|
|
break
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
yield h
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
if h.parentHash == GENESIS_PARENT_HASH:
|
|
|
|
break
|
2018-06-20 12:33:57 +00:00
|
|
|
else:
|
2018-08-03 11:56:49 +00:00
|
|
|
h = self.getBlockHeader(h.parentHash)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-11-28 02:25:36 +00:00
|
|
|
proc addBlockNumberToHashLookup*(self: BaseChainDB; header: BlockHeader) =
|
2018-07-05 12:41:01 +00:00
|
|
|
self.db.put(blockNumberToHashKey(header.blockNumber).toOpenArray,
|
2018-09-25 12:52:28 +00:00
|
|
|
rlp.encode(header.hash))
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-11-28 02:25:36 +00:00
|
|
|
proc persistTransactions*(self: BaseChainDB, blockNumber: BlockNumber, transactions: openArray[Transaction]) =
|
|
|
|
var trie = initHexaryTrie(self.db)
|
|
|
|
for idx, tx in transactions:
|
|
|
|
let
|
|
|
|
encodedTx = rlp.encode(tx).toRange
|
|
|
|
txHash = keccak256.digest(encodedTx.toOpenArray)
|
|
|
|
txKey: TransactionKey = (blockNumber, idx)
|
|
|
|
trie.put(rlp.encode(idx).toRange, encodedTx)
|
|
|
|
self.db.put(transactionHashToBlockKey(txHash).toOpenArray, rlp.encode(txKey))
|
2018-11-24 01:00:58 +00:00
|
|
|
|
|
|
|
iterator getBlockTransactionData(self: BaseChainDB, transactionRoot: Hash256): BytesRange =
|
|
|
|
var transactionDb = initHexaryTrie(self.db, transactionRoot)
|
|
|
|
var transactionIdx = 0
|
|
|
|
while true:
|
|
|
|
let transactionKey = rlp.encode(transactionIdx).toRange
|
|
|
|
if transactionKey in transactionDb:
|
|
|
|
yield transactionDb.get(transactionKey)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc transactionIdx
|
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
iterator getBlockTransactionHashes(self: BaseChainDB, blockHeader: BlockHeader): Hash256 =
|
|
|
|
## Returns an iterable of the transaction hashes from th block specified
|
|
|
|
## by the given block header.
|
2018-11-24 01:00:58 +00:00
|
|
|
for encodedTx in self.getBlockTransactionData(blockHeader.txRoot):
|
|
|
|
yield keccak256.digest(encodedTx.toOpenArray)
|
|
|
|
|
|
|
|
proc getBlockBody*(self: BaseChainDB, blockHash: Hash256, output: var BlockBody): bool =
|
|
|
|
var header: BlockHeader
|
|
|
|
if self.getBlockHeader(blockHash, header):
|
|
|
|
result = true
|
|
|
|
output.transactions = @[]
|
|
|
|
output.uncles = @[]
|
|
|
|
for encodedTx in self.getBlockTransactionData(header.txRoot):
|
|
|
|
output.transactions.add(rlp.decode(encodedTx, Transaction))
|
|
|
|
|
|
|
|
if header.ommersHash != EMPTY_UNCLE_HASH:
|
|
|
|
let encodedUncles = self.db.get(genericHashKey(header.ommersHash).toOpenArray).toRange
|
|
|
|
if encodedUncles.len != 0:
|
|
|
|
output.uncles = rlp.decode(encodedUncles, seq[BlockHeader])
|
|
|
|
else:
|
|
|
|
result = false
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-12-14 07:32:45 +00:00
|
|
|
proc getBlockBody*(self: BaseChainDB, hash: Hash256): BlockBody =
|
|
|
|
if not self.getBlockBody(hash, result):
|
|
|
|
raise newException(ValueError, "Error when retrieving block body")
|
|
|
|
|
2018-08-17 18:52:48 +00:00
|
|
|
proc getTransactionKey*(self: BaseChainDB, transactionHash: Hash256): tuple[blockNumber: BlockNumber, index: int] {.inline.} =
|
|
|
|
let
|
|
|
|
tx = self.db.get(transactionHashToBlockKey(transactionHash).toOpenArray).toRange
|
|
|
|
key = rlp.decode(tx, TransactionKey)
|
|
|
|
return (key.blockNumber, key.index)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
proc removeTransactionFromCanonicalChain(self: BaseChainDB, transactionHash: Hash256) {.inline.} =
|
|
|
|
## Removes the transaction specified by the given hash from the canonical chain.
|
2018-07-05 12:41:01 +00:00
|
|
|
self.db.del(transactionHashToBlockKey(transactionHash).toOpenArray)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
proc setAsCanonicalChainHead(self: BaseChainDB; headerHash: Hash256): seq[BlockHeader] =
|
|
|
|
## Sets the header as the canonical chain HEAD.
|
2018-08-03 11:56:49 +00:00
|
|
|
let header = self.getBlockHeader(headerHash)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
var newCanonicalHeaders = sequtils.toSeq(findNewAncestors(self, header))
|
|
|
|
reverse(newCanonicalHeaders)
|
|
|
|
for h in newCanonicalHeaders:
|
|
|
|
var oldHash: Hash256
|
2018-08-03 11:56:49 +00:00
|
|
|
if not self.getBlockHash(h.blockNumber, oldHash):
|
2018-06-20 12:33:57 +00:00
|
|
|
break
|
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
let oldHeader = self.getBlockHeader(oldHash)
|
2018-06-20 12:33:57 +00:00
|
|
|
for txHash in self.getBlockTransactionHashes(oldHeader):
|
|
|
|
self.removeTransactionFromCanonicalChain(txHash)
|
|
|
|
# TODO re-add txn to internal pending pool (only if local sender)
|
|
|
|
|
|
|
|
for h in newCanonicalHeaders:
|
|
|
|
self.addBlockNumberToHashLookup(h)
|
|
|
|
|
2018-09-25 12:52:28 +00:00
|
|
|
self.db.put(canonicalHeadHashKey().toOpenArray, rlp.encode(headerHash))
|
2018-07-05 12:41:01 +00:00
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
return newCanonicalHeaders
|
2018-05-30 16:11:15 +00:00
|
|
|
|
2019-01-01 03:55:40 +00:00
|
|
|
proc setHead*(self: BaseChainDB, header: BlockHeader, writeHeader = false) =
|
|
|
|
var headerHash = rlpHash(header)
|
|
|
|
if writeHeader:
|
|
|
|
self.db.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header))
|
|
|
|
self.addBlockNumberToHashLookup(header)
|
|
|
|
self.db.put(canonicalHeadHashKey().toOpenArray, rlp.encode(headerHash))
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc headerExists*(self: BaseChainDB; blockHash: Hash256): bool =
|
|
|
|
## Returns True if the header with the given block hash is in our DB.
|
2018-09-06 17:05:22 +00:00
|
|
|
self.db.contains(genericHashKey(blockHash).toOpenArray)
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-12-10 12:04:34 +00:00
|
|
|
proc persistReceipts*(self: BaseChainDB, receipts: openArray[Receipt]) =
|
|
|
|
var trie = initHexaryTrie(self.db)
|
|
|
|
for idx, rec in receipts:
|
|
|
|
trie.put(rlp.encode(idx).toRange, rlp.encode(rec).toRange)
|
|
|
|
|
|
|
|
iterator getReceipts*(self: BaseChainDB; header: BlockHeader): Receipt =
|
2018-07-05 12:41:01 +00:00
|
|
|
var receiptDb = initHexaryTrie(self.db, header.receiptRoot)
|
|
|
|
var receiptIdx = 0
|
|
|
|
while true:
|
|
|
|
let receiptKey = rlp.encode(receiptIdx).toRange
|
|
|
|
if receiptKey in receiptDb:
|
|
|
|
let receiptData = receiptDb.get(receiptKey)
|
2018-12-10 12:04:34 +00:00
|
|
|
yield rlp.decode(receiptData, Receipt)
|
2018-07-05 12:41:01 +00:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc receiptIdx
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
iterator getBlockTransactions(self: BaseChainDB; transactionRoot: Hash256;
|
|
|
|
transactionClass: typedesc): transactionClass =
|
|
|
|
for encodedTransaction in self.getBlockTransactionData(transactionRoot):
|
|
|
|
yield rlp.decode(encodedTransaction, transactionClass)
|
|
|
|
|
|
|
|
proc persistHeaderToDb*(self: BaseChainDB; header: BlockHeader): seq[BlockHeader] =
|
|
|
|
let isGenesis = header.parentHash == GENESIS_PARENT_HASH
|
2018-09-06 17:05:22 +00:00
|
|
|
let headerHash = header.blockHash
|
2018-06-20 12:33:57 +00:00
|
|
|
if not isGenesis and not self.headerExists(header.parentHash):
|
|
|
|
raise newException(ParentNotFound, "Cannot persist block header " &
|
2018-09-06 17:05:22 +00:00
|
|
|
$headerHash & " with unknown parent " & $header.parentHash)
|
2018-09-25 12:52:28 +00:00
|
|
|
self.db.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header))
|
2018-07-05 12:41:01 +00:00
|
|
|
|
2018-06-20 12:33:57 +00:00
|
|
|
let score = if isGenesis: header.difficulty
|
|
|
|
else: self.getScore(header.parentHash).u256 + header.difficulty
|
2018-09-25 12:52:28 +00:00
|
|
|
self.db.put(blockHashToScoreKey(headerHash).toOpenArray, rlp.encode(score))
|
2018-07-05 12:41:01 +00:00
|
|
|
|
2018-08-01 12:50:44 +00:00
|
|
|
self.addBlockNumberToHashLookup(header)
|
|
|
|
|
2018-12-25 14:21:47 +00:00
|
|
|
var headScore: uint64
|
2018-06-20 12:33:57 +00:00
|
|
|
try:
|
|
|
|
headScore = self.getScore(self.getCanonicalHead().hash)
|
|
|
|
except CanonicalHeadNotFound:
|
2018-09-06 17:05:22 +00:00
|
|
|
return self.setAsCanonicalChainHead(headerHash)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
if score > headScore.u256:
|
2018-09-06 17:05:22 +00:00
|
|
|
result = self.setAsCanonicalChainHead(headerHash)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
proc addTransactionToCanonicalChain(self: BaseChainDB, txHash: Hash256,
|
|
|
|
blockHeader: BlockHeader, index: int) =
|
|
|
|
let k: TransactionKey = (blockHeader.blockNumber, index)
|
2018-09-25 12:52:28 +00:00
|
|
|
self.db.put(transactionHashToBlockKey(txHash).toOpenArray, rlp.encode(k))
|
2018-06-20 12:33:57 +00:00
|
|
|
|
|
|
|
proc persistUncles*(self: BaseChainDB, uncles: openarray[BlockHeader]): Hash256 =
|
|
|
|
## Persists the list of uncles to the database.
|
|
|
|
## Returns the uncles hash.
|
|
|
|
let enc = rlp.encode(uncles)
|
2018-09-25 12:52:28 +00:00
|
|
|
result = keccak256.digest(enc)
|
|
|
|
self.db.put(genericHashKey(result).toOpenArray, enc)
|
2018-06-20 12:33:57 +00:00
|
|
|
|
2018-12-14 12:46:56 +00:00
|
|
|
proc persistBlockToDb*(self: BaseChainDB; blk: Block): ValidationResult =
|
2018-06-20 12:33:57 +00:00
|
|
|
## Persist the given block's header and uncles.
|
|
|
|
## Assumes all block transactions have been persisted already.
|
|
|
|
let newCanonicalHeaders = self.persistHeaderToDb(blk.header)
|
|
|
|
for header in newCanonicalHeaders:
|
|
|
|
var index = 0
|
|
|
|
for txHash in self.getBlockTransactionHashes(header):
|
|
|
|
self.addTransactionToCanonicalChain(txHash, header, index)
|
|
|
|
inc index
|
|
|
|
|
|
|
|
if blk.uncles.len != 0:
|
|
|
|
let ommersHash = self.persistUncles(blk.uncles)
|
2018-12-14 12:46:56 +00:00
|
|
|
if ommersHash != blk.header.ommersHash:
|
|
|
|
debug "ommersHash mismatch"
|
|
|
|
return ValidationResult.Error
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
# Deprecated:
|
|
|
|
proc getBlockHeaderByHash*(self: BaseChainDB; blockHash: Hash256): BlockHeader {.deprecated.} =
|
|
|
|
self.getBlockHeader(blockHash)
|
|
|
|
|
|
|
|
proc lookupBlockHash*(self: BaseChainDB; n: BlockNumber): Hash256 {.deprecated.} =
|
|
|
|
self.getBlockHash(n)
|
2018-08-01 12:50:44 +00:00
|
|
|
|
2018-08-03 11:56:49 +00:00
|
|
|
proc getCanonicalBlockHeaderByNumber*(self: BaseChainDB; n: BlockNumber): BlockHeader {.deprecated.} =
|
|
|
|
self.getBlockHeader(n)
|
2018-08-29 08:49:01 +00:00
|
|
|
|