From 8fc983097150325063a2e7558e0e14b2a8acca34 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 17 Jul 2015 15:43:16 +0200 Subject: cmd/core,xeth: removed unneeded states & added batch writes --- core/chain_manager.go | 22 ---------------------- core/chain_manager_test.go | 1 - core/transaction_util.go | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 27 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index b2fcb677c..0f008dfa7 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -73,9 +73,6 @@ type ChainManager struct { lastBlockHash common.Hash currentGasLimit *big.Int - transState *state.StateDB - txState *state.ManagedState - cache *lru.Cache // cache is the LRU caching futureBlocks *lru.Cache // future blocks are blocks added for later processing @@ -128,9 +125,7 @@ func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux } } - bc.transState = bc.State().Copy() // Take ownership of this particular state - bc.txState = state.ManageState(bc.State().Copy()) bc.futureBlocks, _ = lru.New(maxFutureBlocks) bc.makeCache() @@ -152,9 +147,6 @@ func (bc *ChainManager) SetHead(head *types.Block) { bc.currentBlock = head bc.makeCache() - statedb := state.New(head.Root(), bc.stateDb) - bc.txState = state.ManageState(statedb) - bc.transState = statedb.Copy() bc.setTotalDifficulty(head.Td) bc.insert(head) bc.setLastState() @@ -203,17 +195,6 @@ func (self *ChainManager) State() *state.StateDB { return state.New(self.CurrentBlock().Root(), self.stateDb) } -func (self *ChainManager) TransState() *state.StateDB { - self.tsmu.RLock() - defer self.tsmu.RUnlock() - - return self.transState -} - -func (self *ChainManager) setTransState(statedb *state.StateDB) { - self.transState = statedb -} - func (bc *ChainManager) recover() bool { data, _ := bc.blockDb.Get([]byte("checkpoint")) if len(data) != 0 { @@ -529,9 +510,6 @@ func (self *ChainManager) WriteBlock(block *types.Block, queued bool) (status wr self.insert(block) self.mu.Unlock() - self.setTransState(state.New(block.Root(), self.stateDb)) - self.txState.SetState(state.New(block.Root(), self.stateDb)) - status = CanonStatTy } else { status = SideStatTy diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index d247c3e50..e2ad86942 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -392,7 +392,6 @@ func chm(genesis *types.Block, db common.Database) *ChainManager { bc.futureBlocks, _ = lru.New(100) bc.processor = bproc{} bc.ResetWithGenesisBlock(genesis) - bc.txState = state.ManageState(bc.State()) return bc } diff --git a/core/transaction_util.go b/core/transaction_util.go index 1020fbd6e..d4b9f53c9 100644 --- a/core/transaction_util.go +++ b/core/transaction_util.go @@ -19,9 +19,11 @@ package core import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/rlp" + "github.com/syndtr/goleveldb/leveldb" ) var ( @@ -31,13 +33,21 @@ var ( // PutTransactions stores the transactions in the given database func PutTransactions(db common.Database, block *types.Block, txs types.Transactions) { + batch := new(leveldb.Batch) + _, batchWrite := db.(*ethdb.LDBDatabase) + for i, tx := range block.Transactions() { rlpEnc, err := rlp.EncodeToBytes(tx) if err != nil { glog.V(logger.Debug).Infoln("Failed encoding tx", err) return } - db.Put(tx.Hash().Bytes(), rlpEnc) + + if batchWrite { + batch.Put(tx.Hash().Bytes(), rlpEnc) + } else { + db.Put(tx.Hash().Bytes(), rlpEnc) + } var txExtra struct { BlockHash common.Hash @@ -52,20 +62,44 @@ func PutTransactions(db common.Database, block *types.Block, txs types.Transacti glog.V(logger.Debug).Infoln("Failed encoding tx meta data", err) return } - db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta) + + if batchWrite { + batch.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta) + } else { + db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta) + } + } + + if db, ok := db.(*ethdb.LDBDatabase); ok { + if err := db.LDB().Write(batch, nil); err != nil { + glog.V(logger.Error).Infoln("db write err:", err) + } } } // PutReceipts stores the receipts in the current database func PutReceipts(db common.Database, receipts types.Receipts) error { + batch := new(leveldb.Batch) + _, batchWrite := db.(*ethdb.LDBDatabase) + for _, receipt := range receipts { storageReceipt := (*types.ReceiptForStorage)(receipt) bytes, err := rlp.EncodeToBytes(storageReceipt) if err != nil { return err } - err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes) - if err != nil { + + if batchWrite { + batch.Put(append(receiptsPre, receipt.TxHash[:]...), bytes) + } else { + err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes) + if err != nil { + return err + } + } + } + if db, ok := db.(*ethdb.LDBDatabase); ok { + if err := db.LDB().Write(batch, nil); err != nil { return err } } -- cgit v1.2.3