Merge pull request #124 from status-im/chain-db-interface
Separate Chain from ChainDB
This commit is contained in:
commit
3adc9f04f7
|
@ -11,7 +11,7 @@ import
|
|||
../errors, ../block_types, ../utils/header, ../constants, ./storage_types.nim
|
||||
|
||||
type
|
||||
BaseChainDB* = ref object of AbstractChainDB
|
||||
BaseChainDB* = ref object
|
||||
db*: TrieDatabaseRef
|
||||
# TODO db*: JournalDB
|
||||
|
||||
|
@ -80,6 +80,9 @@ proc getBlockHeader*(self: BaseChainDB; n: BlockNumber): BlockHeader =
|
|||
## Raises BlockNotFound error if the block is not in the DB.
|
||||
self.getBlockHeader(self.getBlockHash(n))
|
||||
|
||||
proc getBlockBody*(self: BaseChainDB, h: Hash256, output: var BlockBody): bool =
|
||||
discard # TODO:
|
||||
|
||||
proc getScore*(self: BaseChainDB; blockHash: Hash256): int =
|
||||
rlp.decode(self.db.get(blockHashToScoreKey(blockHash).toOpenArray).toRange, int)
|
||||
|
||||
|
@ -250,34 +253,6 @@ proc persistBlockToDb*(self: BaseChainDB; blk: Block) =
|
|||
proc getStateDb*(self: BaseChainDB; stateRoot: Hash256; readOnly: bool = false): AccountStateDB =
|
||||
result = newAccountStateDB(self.db, stateRoot)
|
||||
|
||||
method genesisHash*(db: BaseChainDB): KeccakHash =
|
||||
db.getBlockHash(0.toBlockNumber)
|
||||
|
||||
method getBlockHeader*(db: BaseChainDB, b: HashOrNum): BlockHeaderRef =
|
||||
var h: BlockHeader
|
||||
var ok = case b.isHash
|
||||
of true:
|
||||
db.getBlockHeader(b.hash, h)
|
||||
else:
|
||||
db.getBlockHeader(b.number, h)
|
||||
|
||||
if ok:
|
||||
result.new()
|
||||
result[] = h
|
||||
|
||||
method getBestBlockHeader*(self: BaseChainDB): BlockHeaderRef =
|
||||
result.new()
|
||||
result[] = self.getCanonicalHead()
|
||||
|
||||
method getSuccessorHeader*(db: BaseChainDB, h: BlockHeader): BlockHeaderRef =
|
||||
let n = h.blockNumber + 1
|
||||
var r: BlockHeader
|
||||
if db.getBlockHeader(n, r):
|
||||
result.new()
|
||||
result[] = r
|
||||
|
||||
method getBlockBody*(db: BaseChainDB, blockHash: KeccakHash): BlockBodyRef =
|
||||
result = nil
|
||||
|
||||
# Deprecated:
|
||||
proc getBlockHeaderByHash*(self: BaseChainDB; blockHash: Hash256): BlockHeader {.deprecated.} =
|
||||
|
@ -288,3 +263,4 @@ proc lookupBlockHash*(self: BaseChainDB; n: BlockNumber): Hash256 {.deprecated.}
|
|||
|
||||
proc getCanonicalBlockHeaderByNumber*(self: BaseChainDB; n: BlockNumber): BlockHeader {.deprecated.} =
|
||||
self.getBlockHeader(n)
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import
|
|||
os, strutils, net, eth_common, db/[storage_types, db_chain],
|
||||
asyncdispatch2, json_rpc/rpcserver, eth_keys,
|
||||
eth_p2p, eth_p2p/rlpx_protocols/[eth, les],
|
||||
config, genesis, rpc/[common, p2p],
|
||||
config, genesis, rpc/[common, p2p], p2p/chain,
|
||||
eth_trie
|
||||
|
||||
const UseSqlite = true
|
||||
|
@ -85,10 +85,10 @@ proc start(): NimbusObject =
|
|||
nimbus.ethNode = newEthereumNode(keypair, address, conf.net.networkId,
|
||||
nil, nimbusClientId)
|
||||
|
||||
nimbus.ethNode.chain = chainDB
|
||||
nimbus.ethNode.chain = newChain(chainDB)
|
||||
|
||||
if RpcFlags.Enabled in conf.rpc.flags:
|
||||
setupP2PRpc(nimbus.ethNode, nimbus.rpcServer)
|
||||
setupEthRpc(nimbus.ethNode, chainDB, nimbus.rpcServer)
|
||||
|
||||
## Starting servers
|
||||
nimbus.state = Starting
|
||||
|
@ -101,10 +101,9 @@ proc start(): NimbusObject =
|
|||
waitFor nimbus.ethNode.connectToNetwork(conf.net.bootNodes)
|
||||
|
||||
# TODO: temp code until the CLI/RPC interface is fleshed out
|
||||
if os.getenv("START_SYNC") == "1":
|
||||
let status = waitFor nimbus.ethNode.fastBlockchainSync()
|
||||
if status != syncSuccess:
|
||||
echo "Block sync failed: ", status
|
||||
let status = waitFor nimbus.ethNode.fastBlockchainSync()
|
||||
if status != syncSuccess:
|
||||
echo "Block sync failed: ", status
|
||||
|
||||
nimbus.state = Running
|
||||
result = nimbus
|
||||
|
@ -119,8 +118,8 @@ proc process*(nimbus: NimbusObject) =
|
|||
proc signalBreak(udata: pointer) =
|
||||
nimbus.state = Stopping
|
||||
# Adding SIGINT, SIGTERM handlers
|
||||
discard addSignal(SIGINT, signalBreak)
|
||||
discard addSignal(SIGTERM, signalBreak)
|
||||
# discard addSignal(SIGINT, signalBreak)
|
||||
# discard addSignal(SIGTERM, signalBreak)
|
||||
|
||||
# Main loop
|
||||
while nimbus.state == Running:
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
import ../db/db_chain, eth_common, chronicles, ../vm_state, ../vm_types, ../transaction,
|
||||
../vm/[computation, interpreter_dispatch, message]
|
||||
|
||||
|
||||
type
|
||||
Chain* = ref object of AbstractChainDB
|
||||
db: BaseChainDB
|
||||
|
||||
proc newChain*(db: BaseChainDB): Chain =
|
||||
result.new
|
||||
result.db = db
|
||||
|
||||
method genesisHash*(c: Chain): KeccakHash =
|
||||
c.db.getBlockHash(0.toBlockNumber)
|
||||
|
||||
method getBlockHeader*(c: Chain, b: HashOrNum, output: var BlockHeader): bool =
|
||||
case b.isHash
|
||||
of true:
|
||||
c.db.getBlockHeader(b.hash, output)
|
||||
else:
|
||||
c.db.getBlockHeader(b.number, output)
|
||||
|
||||
method getBestBlockHeader*(c: Chain): BlockHeader =
|
||||
c.db.getCanonicalHead()
|
||||
|
||||
method getSuccessorHeader*(c: Chain, h: BlockHeader, output: var BlockHeader): bool =
|
||||
let n = h.blockNumber + 1
|
||||
c.db.getBlockHeader(n, output)
|
||||
|
||||
method getBlockBody*(c: Chain, blockHash: KeccakHash): BlockBodyRef =
|
||||
result = nil
|
||||
|
||||
method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarray[BlockBody]) =
|
||||
# Run the VM here
|
||||
assert(headers.len == bodies.len)
|
||||
|
||||
for i in 0 ..< headers.len:
|
||||
let head = c.db.getCanonicalHead()
|
||||
# assert(head.blockNumber == headers[i].blockNumber - 1)
|
||||
let vmState = newBaseVMState(head, c.db)
|
||||
if bodies[i].transactions.len != 0:
|
||||
# echo "block: ", headers[i].blockNumber
|
||||
for t in bodies[i].transactions:
|
||||
var msg: Message
|
||||
# echo "trns: ", t
|
||||
|
||||
# let msg = newMessage(t.gasLimit, t.gasPrice, t.to, t.getSender,
|
||||
|
||||
# let c = newBaseComputation(vmState,
|
||||
|
||||
discard
|
|
@ -93,22 +93,17 @@ func headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
|
|||
let blockNum = stint.fromHex(UInt256, tag)
|
||||
result = chain.getBlockHeader(blockNum)
|
||||
|
||||
proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
||||
template chain: untyped = BaseChainDB(node.chain) # TODO: Sensible casting
|
||||
|
||||
proc getAccountDb(readOnly = true): AccountStateDb =
|
||||
## Retrieves the account db from canonical head
|
||||
let
|
||||
header = chain.getCanonicalHead()
|
||||
vmState = newBaseVMState(header, chain)
|
||||
result = vmState.chaindb.getStateDb(vmState.blockHeader.hash, readOnly)
|
||||
|
||||
proc getAccountDbFromTag(tag: string, readOnly = true): AccountStateDb =
|
||||
proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||
proc accountDbFromTag(tag: string, readOnly = true): AccountStateDb =
|
||||
let
|
||||
header = chain.headerFromTag(tag)
|
||||
vmState = newBaseVMState(header, chain)
|
||||
result = vmState.chaindb.getStateDb(vmState.blockHeader.hash, readOnly)
|
||||
|
||||
proc getBlockBody(hash: KeccakHash): BlockBody =
|
||||
if not chain.getBlockBody(hash, result):
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
|
||||
rpcsrv.rpc("net_version") do() -> uint:
|
||||
let conf = getConfiguration()
|
||||
result = conf.net.networkId
|
||||
|
@ -197,23 +192,15 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## data: hash of a block
|
||||
## Returns integer of the number of transactions in this block.
|
||||
var hashData = strToHash(data.string)
|
||||
let body = chain.getBlockBody(hashData)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = body.transactions.len
|
||||
result = getBlockBody(hashData).transactions.len
|
||||
|
||||
rpcsrv.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: string) -> int:
|
||||
## Returns the number of transactions in a block matching the given block number.
|
||||
##
|
||||
## data: integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
|
||||
## Returns integer of the number of transactions in this block.
|
||||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
accountDb = getAccountDbFromTag(quantityTag)
|
||||
body = chain.getBlockBody(header.hash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = body.transactions.len
|
||||
let header = chain.headerFromTag(quantityTag)
|
||||
result = getBlockBody(header.hash).transactions.len
|
||||
|
||||
rpcsrv.rpc("eth_getUncleCountByBlockHash") do(data: HexDataStr) -> int:
|
||||
## Returns the number of uncles in a block from a block matching the given block hash.
|
||||
|
@ -221,22 +208,15 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## data: hash of a block.
|
||||
## Returns integer of the number of uncles in this block.
|
||||
var hashData = strToHash(data.string)
|
||||
let body = chain.getBlockBody(hashData)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = body.uncles.len
|
||||
result = getBlockBody(hashData).uncles.len
|
||||
|
||||
rpcsrv.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: string) -> int:
|
||||
## Returns the number of uncles in a block from a block matching the given block number.
|
||||
##
|
||||
## quantityTag: integer of a block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
||||
## Returns integer of uncles in this block.
|
||||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
body = chain.getBlockBody(header.hash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = body.uncles.len
|
||||
let header = chain.headerFromTag(quantityTag)
|
||||
result = getBlockBody(header.hash).uncles.len
|
||||
|
||||
rpcsrv.rpc("eth_getCode") do(data: EthAddressStr, quantityTag: string) -> HexDataStr:
|
||||
## Returns code at a given address.
|
||||
|
@ -303,7 +283,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## Returns the amount of gas used.
|
||||
discard
|
||||
|
||||
func populateBlockObject(header: BlockHeader, blockBody: BlockBodyRef): BlockObject =
|
||||
func populateBlockObject(header: BlockHeader, blockBody: BlockBody): BlockObject =
|
||||
result.number = some(header.blockNumber)
|
||||
result.hash = some(header.hash)
|
||||
result.parentHash = header.parentHash
|
||||
|
@ -344,10 +324,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
let
|
||||
h = data.string.strToHash
|
||||
header = chain.getBlockHeader(h)
|
||||
body = chain.getBlockBody(h)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = some(populateBlockObject(header, body))
|
||||
result = some(populateBlockObject(header, getBlockBody(h)))
|
||||
|
||||
rpcsrv.rpc("eth_getBlockByNumber") do(quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
|
||||
## Returns information about a block by block number.
|
||||
|
@ -357,10 +334,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## Returns BlockObject or nil when no block was found.
|
||||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
body = chain.getBlockBody(header.hash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
result = some(populateBlockObject(header, body))
|
||||
result = some(populateBlockObject(header, getBlockBody(header.hash)))
|
||||
|
||||
proc populateTransactionObject(transaction: Transaction, txIndex: int64, blockHeader: BlockHeader, blockHash: Hash256): TransactionObject =
|
||||
let
|
||||
|
@ -393,10 +367,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
txDetails = chain.getTransactionKey(h)
|
||||
header = chain.getBlockHeader(txDetails.blockNumber)
|
||||
blockHash = chain.getBlockHash(txDetails.blockNumber)
|
||||
body = chain.getBlockBody(blockHash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
let transaction = body.transactions[txDetails.index]
|
||||
transaction = getBlockBody(blockHash).transactions[txDetails.index]
|
||||
populateTransactionObject(transaction, txDetails.index, header, blockHash)
|
||||
|
||||
rpcsrv.rpc("eth_getTransactionByBlockHashAndIndex") do(data: HexDataStr, quantity: int) -> TransactionObject:
|
||||
|
@ -407,12 +378,8 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## Returns requested transaction information.
|
||||
let
|
||||
blockHash = data.string.strToHash()
|
||||
body = chain.getBlockBody(blockHash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
let
|
||||
header = chain.getBlockHeader(blockHash)
|
||||
transaction = body.transactions[quantity]
|
||||
transaction = getBlockBody(blockHash).transactions[quantity]
|
||||
populateTransactionObject(transaction, quantity, header, blockHash)
|
||||
|
||||
rpcsrv.rpc("eth_getTransactionByBlockNumberAndIndex") do(quantityTag: string, quantity: int) -> TransactionObject:
|
||||
|
@ -423,11 +390,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
blockHash = header.hash
|
||||
body = chain.getBlockBody(blockHash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
let
|
||||
transaction = body.transactions[quantity]
|
||||
transaction = getBlockBody(blockHash).transactions[quantity]
|
||||
populateTransactionObject(transaction, quantity, header, blockHash)
|
||||
|
||||
proc populateReceipt(receipt: Receipt, cumulativeGas: GasInt, transaction: Transaction, txIndex: int, blockHeader: BlockHeader): ReceiptObject =
|
||||
|
@ -458,9 +421,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
h = data.string.strToHash()
|
||||
txDetails = chain.getTransactionKey(h)
|
||||
header = chain.getBlockHeader(txDetails.blockNumber)
|
||||
body = chain.getBlockBody(h)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
body = getBlockBody(header.hash)
|
||||
var
|
||||
idx = 0
|
||||
cumulativeGas: GasInt
|
||||
|
@ -478,9 +439,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## Returns BlockObject or nil when no block was found.
|
||||
let
|
||||
blockHash = data.string.strToHash()
|
||||
body = chain.getBlockBody(blockHash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
body = getBlockBody(blockHash)
|
||||
if quantity < 0 or quantity >= body.uncles.len:
|
||||
raise newException(ValueError, "Uncle index out of range")
|
||||
let uncle = body.uncles[quantity]
|
||||
|
@ -494,9 +453,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
|
|||
## Returns BlockObject or nil when no block was found.
|
||||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
body = chain.getBlockBody(header.hash)
|
||||
if body == nil:
|
||||
raise newException(ValueError, "Cannot find hash")
|
||||
body = getBlockBody(header.hash)
|
||||
if quantity < 0 or quantity >= body.uncles.len:
|
||||
raise newException(ValueError, "Uncle index out of range")
|
||||
let uncle = body.uncles[quantity]
|
||||
|
|
|
@ -60,7 +60,7 @@ proc doTests =
|
|||
rpcServer = newRpcSocketServer(["localhost:8545"])
|
||||
client = newRpcSocketClient()
|
||||
setupCommonRpc(rpcServer)
|
||||
setupP2PRpc(ethNode, rpcServer)
|
||||
setupEthRpc(ethNode, chain, rpcServer)
|
||||
|
||||
# Begin tests
|
||||
rpcServer.start()
|
||||
|
@ -79,4 +79,4 @@ proc doTests =
|
|||
rpcServer.stop()
|
||||
rpcServer.close()
|
||||
|
||||
doTests()
|
||||
doTests()
|
||||
|
|
Loading…
Reference in New Issue