From a098285a7bcf73de205c99f857f221213cf7b1fa Mon Sep 17 00:00:00 2001 From: andri lim Date: Fri, 30 Nov 2018 17:07:20 +0700 Subject: [PATCH] put prune switch into usage --- nimbus/db/db_chain.nim | 6 ++++-- nimbus/db/state_db.nim | 4 ++-- nimbus/genesis.nim | 6 +++--- nimbus/nimbus.nim | 2 +- nimbus/p2p/chain.nim | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index 7caddeb4a..6c88aceb7 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -18,6 +18,7 @@ type # debt while setting a CI baseline from which to improve/replace. accountCodes*: TableRef[Hash256, ByteRange] # TODO db*: JournalDB + pruneTrie*: bool KeyType = enum blockNumberToHash @@ -27,10 +28,11 @@ type blockNumber: BlockNumber index: int -proc newBaseChainDB*(db: TrieDatabaseRef): BaseChainDB = +proc newBaseChainDB*(db: TrieDatabaseRef, pruneTrie: bool): BaseChainDB = new(result) result.db = db result.accountCodes = newTable[Hash256, ByteRange]() + result.pruneTrie = pruneTrie proc `$`*(db: BaseChainDB): string = result = "BaseChainDB" @@ -275,7 +277,7 @@ proc persistBlockToDb*(self: BaseChainDB; blk: Block) = proc getStateDb*(self: BaseChainDB; stateRoot: Hash256; readOnly: bool = false): AccountStateDB = # TODO: readOnly is not used. - result = newAccountStateDB(self.db, stateRoot, readOnly, self.accountCodes) + result = newAccountStateDB(self.db, stateRoot, self.pruneTrie, readOnly, self.accountCodes) # Deprecated: diff --git a/nimbus/db/state_db.nim b/nimbus/db/state_db.nim index c9e5810a0..73930def5 100644 --- a/nimbus/db/state_db.nim +++ b/nimbus/db/state_db.nim @@ -25,10 +25,10 @@ proc rootHash*(accountDb: AccountStateDB): KeccakHash = # TODO: self.Trie.rootHash = value proc newAccountStateDB*(backingStore: TrieDatabaseRef, - root: KeccakHash, readOnly: bool = false, + root: KeccakHash, pruneTrie: bool, readOnly: bool = false, accountCodes = newTable[Hash256, ByteRange]()): AccountStateDB = result.new() - result.trie = initSecureHexaryTrie(backingStore, root) + result.trie = initSecureHexaryTrie(backingStore, root, pruneTrie) result.accountCodes = accountCodes template createRangeFromAddress(address: EthAddress): ByteRange = diff --git a/nimbus/genesis.nim b/nimbus/genesis.nim index e426f2994..736d7753a 100644 --- a/nimbus/genesis.nim +++ b/nimbus/genesis.nim @@ -66,10 +66,10 @@ proc defaultGenesisBlockForNetwork*(id: PublicNetwork): Genesis = result.config = publicChainConfig(id) proc toBlock*(g: Genesis, db: BaseChainDB = nil): BlockHeader = - let tdb = if db.isNil: newMemoryDB() - else: db.db + let (tdb, pruneTrie) = if db.isNil: (newMemoryDB(), true) + else: (db.db, db.pruneTrie) var trie = initHexaryTrie(tdb) - var sdb = newAccountStateDB(tdb, trie.rootHash) + var sdb = newAccountStateDB(tdb, trie.rootHash, pruneTrie) for address, account in g.alloc: sdb.setAccount(address, newAccount(account.nonce, account.balance)) diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index d24f45367..8a3f9fe13 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -74,7 +74,7 @@ proc start(): NimbusObject = createDir(conf.dataDir) let trieDB = trieDB newChainDb(conf.dataDir) - let chainDB = newBaseChainDB(trieDB) + let chainDB = newBaseChainDB(trieDB, conf.prune == PruneMode.Full) if canonicalHeadHashKey().toOpenArray notin trieDB: initializeEmptyDb(chainDb) diff --git a/nimbus/p2p/chain.nim b/nimbus/p2p/chain.nim index d00d78270..544a02c03 100644 --- a/nimbus/p2p/chain.nim +++ b/nimbus/p2p/chain.nim @@ -118,7 +118,7 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr for i in 0 ..< headers.len: let head = c.db.getCanonicalHead() assert(head.blockNumber == headers[i].blockNumber - 1) - var stateDb = newAccountStateDB(c.db.db, head.stateRoot) + var stateDb = newAccountStateDB(c.db.db, head.stateRoot, c.db.pruneTrie) var gasReward = 0.u256 assert(bodies[i].transactions.calcTxRoot == headers[i].txRoot)