diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-08-09 20:16:37 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-08-09 20:16:37 +0800 |
commit | 2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a (patch) | |
tree | e3e7d013e2ccae939df7dd1d8aa7dcb3802fadd7 /core | |
parent | 07cb8092e7a41e80224dc63691146e8714f94ebf (diff) | |
parent | a23478c0be94e1e727a64d20341b8d6f98d7f0a0 (diff) | |
download | go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar.gz go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar.bz2 go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar.lz go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar.xz go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.tar.zst go-tangerine-2fcf7f1241648dc2c0ed90a122c5945f25b3ce1a.zip |
Merge pull request #1604 from obscuren/db-merge
core, eth, trie, xeth: merged state, chain, extra databases in one
Diffstat (limited to 'core')
-rw-r--r-- | core/bench_test.go | 4 | ||||
-rw-r--r-- | core/block_processor.go | 16 | ||||
-rw-r--r-- | core/block_processor_test.go | 8 | ||||
-rw-r--r-- | core/chain_makers.go | 6 | ||||
-rw-r--r-- | core/chain_makers_test.go | 4 | ||||
-rw-r--r-- | core/chain_manager.go | 48 | ||||
-rw-r--r-- | core/chain_manager_test.go | 18 | ||||
-rw-r--r-- | core/genesis.go | 18 | ||||
-rw-r--r-- | core/manager.go | 5 |
9 files changed, 60 insertions, 67 deletions
diff --git a/core/bench_test.go b/core/bench_test.go index 67ba15970..baae8a7a5 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -168,8 +168,8 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { // Time the insertion of the new chain. // State and blocks are stored in the same DB. evmux := new(event.TypeMux) - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + chainman.SetProcessor(NewBlockProcessor(db, FakePow{}, chainman, evmux)) defer chainman.Stop() b.ReportAllocs() b.ResetTimer() diff --git a/core/block_processor.go b/core/block_processor.go index 6ed1bc8ef..477215356 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -41,8 +41,7 @@ const ( ) type BlockProcessor struct { - db common.Database - extraDb common.Database + chainDb common.Database // Mutex for locking the block processor. Blocks can only be handled one at a time mutex sync.Mutex // Canonical block chain @@ -57,10 +56,9 @@ type BlockProcessor struct { eventMux *event.TypeMux } -func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { +func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { sm := &BlockProcessor{ - db: db, - extraDb: extra, + chainDb: db, mem: make(map[string]*big.Int), Pow: pow, bc: chainManager, @@ -199,7 +197,7 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, receipts func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, receipts types.Receipts, err error) { // Create a new state based on the parent's root (e.g., create copy) - state := state.New(parent.Root(), sm.db) + state := state.New(parent.Root(), sm.chainDb) header := block.Header() uncles := block.Uncles() txs := block.Transactions() @@ -340,7 +338,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty // GetBlockReceipts returns the receipts beloniging to the block hash func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) types.Receipts { if block := sm.ChainManager().GetBlock(bhash); block != nil { - return GetBlockReceipts(sm.extraDb, block.Hash()) + return GetBlockReceipts(sm.chainDb, block.Hash()) } return nil @@ -350,7 +348,7 @@ func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) types.Receipts { // where it tries to get it from the (updated) method which gets them from the receipts or // the depricated way by re-processing the block. func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err error) { - receipts := GetBlockReceipts(sm.extraDb, block.Hash()) + receipts := GetBlockReceipts(sm.chainDb, block.Hash()) if len(receipts) > 0 { // coalesce logs for _, receipt := range receipts { @@ -362,7 +360,7 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro // TODO: remove backward compatibility var ( parent = sm.bc.GetBlock(block.ParentHash()) - state = state.New(parent.Root(), sm.db) + state = state.New(parent.Root(), sm.chainDb) ) sm.TransitionState(state, parent, block, true) diff --git a/core/block_processor_test.go b/core/block_processor_test.go index f48ce9607..4525f417b 100644 --- a/core/block_processor_test.go +++ b/core/block_processor_test.go @@ -33,19 +33,19 @@ func proc() (*BlockProcessor, *ChainManager) { db, _ := ethdb.NewMemDatabase() var mux event.TypeMux - WriteTestNetGenesisBlock(db, db, 0) - chainMan, err := NewChainManager(db, db, db, thePow(), &mux) + WriteTestNetGenesisBlock(db, 0) + chainMan, err := NewChainManager(db, thePow(), &mux) if err != nil { fmt.Println(err) } - return NewBlockProcessor(db, db, ezp.New(), chainMan, &mux), chainMan + return NewBlockProcessor(db, ezp.New(), chainMan, &mux), chainMan } func TestNumber(t *testing.T) { pow := ezp.New() _, chain := proc() - statedb := state.New(chain.Genesis().Root(), chain.stateDb) + statedb := state.New(chain.Genesis().Root(), chain.chainDb) header := makeHeader(chain.Genesis(), statedb) header.Number = big.NewInt(3) err := ValidateHeader(pow, header, chain.Genesis(), false) diff --git a/core/chain_makers.go b/core/chain_makers.go index 85a6175dc..0bb1df95a 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -184,9 +184,9 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header { func newCanonical(n int, db common.Database) (*BlockProcessor, error) { evmux := &event.TypeMux{} - WriteTestNetGenesisBlock(db, db, 0) - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux) + WriteTestNetGenesisBlock(db, 0) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + bman := NewBlockProcessor(db, FakePow{}, chainman, evmux) bman.bc.SetProcessor(bman) parent := bman.bc.CurrentBlock() if n == 0 { diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go index 98a585f9b..1c868624d 100644 --- a/core/chain_makers_test.go +++ b/core/chain_makers_test.go @@ -77,8 +77,8 @@ func ExampleGenerateChain() { // Import the chain. This runs all block validation rules. evmux := &event.TypeMux{} - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + chainman.SetProcessor(NewBlockProcessor(db, FakePow{}, chainman, evmux)) if i, err := chainman.InsertChain(chain); err != nil { fmt.Printf("insert error (block %d): %v\n", i, err) return diff --git a/core/chain_manager.go b/core/chain_manager.go index 1b792933c..1647031b1 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -56,9 +56,7 @@ const ( type ChainManager struct { //eth EthManager - blockDb common.Database - stateDb common.Database - extraDb common.Database + chainDb common.Database processor types.BlockProcessor eventMux *event.TypeMux genesisBlock *types.Block @@ -85,12 +83,10 @@ type ChainManager struct { pow pow.PoW } -func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) { +func NewChainManager(chainDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) { cache, _ := lru.New(blockCacheLimit) bc := &ChainManager{ - blockDb: blockDb, - stateDb: stateDb, - extraDb: extraDb, + chainDb: chainDb, eventMux: mux, quit: make(chan struct{}), cache: cache, @@ -103,7 +99,7 @@ func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux if err != nil { return nil, err } - bc.genesisBlock, err = WriteGenesisBlock(stateDb, blockDb, reader) + bc.genesisBlock, err = WriteGenesisBlock(chainDb, reader) if err != nil { return nil, err } @@ -195,15 +191,15 @@ func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { } func (self *ChainManager) State() *state.StateDB { - return state.New(self.CurrentBlock().Root(), self.stateDb) + return state.New(self.CurrentBlock().Root(), self.chainDb) } func (bc *ChainManager) recover() bool { - data, _ := bc.blockDb.Get([]byte("checkpoint")) + data, _ := bc.chainDb.Get([]byte("checkpoint")) if len(data) != 0 { block := bc.GetBlock(common.BytesToHash(data)) if block != nil { - err := bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes()) + err := bc.chainDb.Put([]byte("LastBlock"), block.Hash().Bytes()) if err != nil { glog.Fatalln("db write err:", err) } @@ -217,7 +213,7 @@ func (bc *ChainManager) recover() bool { } func (bc *ChainManager) setLastState() error { - data, _ := bc.blockDb.Get([]byte("LastBlock")) + data, _ := bc.chainDb.Get([]byte("LastBlock")) if len(data) != 0 { block := bc.GetBlock(common.BytesToHash(data)) if block != nil { @@ -264,7 +260,7 @@ func (bc *ChainManager) Reset() { bc.cache, _ = lru.New(blockCacheLimit) // Prepare the genesis block - err := WriteBlock(bc.blockDb, bc.genesisBlock) + err := WriteBlock(bc.chainDb, bc.genesisBlock) if err != nil { glog.Fatalln("db err:", err) } @@ -277,7 +273,7 @@ func (bc *ChainManager) Reset() { } func (bc *ChainManager) removeBlock(block *types.Block) { - bc.blockDb.Delete(append(blockHashPre, block.Hash().Bytes()...)) + bc.chainDb.Delete(append(blockHashPre, block.Hash().Bytes()...)) } func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { @@ -292,7 +288,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { gb.Td = gb.Difficulty() bc.genesisBlock = gb - err := WriteBlock(bc.blockDb, bc.genesisBlock) + err := WriteBlock(bc.chainDb, bc.genesisBlock) if err != nil { glog.Fatalln("db err:", err) } @@ -339,14 +335,14 @@ func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error // insert injects a block into the current chain block chain. Note, this function // assumes that the `mu` mutex is held! func (bc *ChainManager) insert(block *types.Block) { - err := WriteHead(bc.blockDb, block) + err := WriteHead(bc.chainDb, block) if err != nil { glog.Fatal("db write fail:", err) } bc.checkpoint++ if bc.checkpoint > checkpointLimit { - err = bc.blockDb.Put([]byte("checkpoint"), block.Hash().Bytes()) + err = bc.chainDb.Put([]byte("checkpoint"), block.Hash().Bytes()) if err != nil { glog.Fatal("db write fail:", err) } @@ -369,7 +365,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool { return true } - data, _ := bc.blockDb.Get(append(blockHashPre, hash[:]...)) + data, _ := bc.chainDb.Get(append(blockHashPre, hash[:]...)) return len(data) != 0 } @@ -399,7 +395,7 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block { return block.(*types.Block) } - block := GetBlockByHash(self.blockDb, hash) + block := GetBlockByHash(self.chainDb, hash) if block == nil { return nil } @@ -433,7 +429,7 @@ func (self *ChainManager) GetBlocksFromHash(hash common.Hash, n int) (blocks []* // non blocking version func (self *ChainManager) getBlockByNumber(num uint64) *types.Block { - return GetBlockByNumber(self.blockDb, num) + return GetBlockByNumber(self.chainDb, num) } func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) { @@ -521,7 +517,7 @@ func (self *ChainManager) WriteBlock(block *types.Block, queued bool) (status wr status = SideStatTy } - err = WriteBlock(self.blockDb, block) + err = WriteBlock(self.chainDb, block) if err != nil { glog.Fatalln("db err:", err) } @@ -638,9 +634,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queueEvent.canonicalCount++ // This puts transactions in a extra db for rpc - PutTransactions(self.extraDb, block, block.Transactions()) + PutTransactions(self.chainDb, block, block.Transactions()) // store the receipts - PutReceipts(self.extraDb, receipts) + PutReceipts(self.chainDb, receipts) case SideStatTy: if glog.V(logger.Detail) { glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) @@ -651,7 +647,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queue[i] = ChainSplitEvent{block, logs} queueEvent.splitCount++ } - PutBlockReceipts(self.extraDb, block, receipts) + PutBlockReceipts(self.chainDb, block, receipts) stats.processed++ } @@ -733,8 +729,8 @@ func (self *ChainManager) merge(oldBlock, newBlock *types.Block) error { // insert the block in the canonical way, re-writing history self.insert(block) // write canonical receipts and transactions - PutTransactions(self.extraDb, block, block.Transactions()) - PutReceipts(self.extraDb, GetBlockReceipts(self.extraDb, block.Hash())) + PutTransactions(self.chainDb, block, block.Transactions()) + PutReceipts(self.chainDb, GetBlockReceipts(self.chainDb, block.Hash())) } self.mu.Unlock() diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index f0c097df6..002dcbe44 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -48,14 +48,14 @@ func thePow() pow.PoW { func theChainManager(db common.Database, t *testing.T) *ChainManager { var eventMux event.TypeMux - WriteTestNetGenesisBlock(db, db, 0) - chainMan, err := NewChainManager(db, db, db, thePow(), &eventMux) + WriteTestNetGenesisBlock(db, 0) + chainMan, err := NewChainManager(db, thePow(), &eventMux) if err != nil { t.Error("failed creating chainmanager:", err) t.FailNow() return nil } - blockMan := NewBlockProcessor(db, db, nil, chainMan, &eventMux) + blockMan := NewBlockProcessor(db, nil, chainMan, &eventMux) chainMan.SetProcessor(blockMan) return chainMan @@ -125,7 +125,7 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { bman.bc.mu.Lock() { - WriteBlock(bman.bc.blockDb, block) + WriteBlock(bman.bc.chainDb, block) } bman.bc.mu.Unlock() } @@ -387,7 +387,7 @@ func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block func chm(genesis *types.Block, db common.Database) *ChainManager { var eventMux event.TypeMux - bc := &ChainManager{extraDb: db, blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} + bc := &ChainManager{chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} bc.cache, _ = lru.New(100) bc.futureBlocks, _ = lru.New(100) bc.processor = bproc{} @@ -399,7 +399,7 @@ func chm(genesis *types.Block, db common.Database) *ChainManager { func TestReorgLongest(t *testing.T) { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() @@ -422,7 +422,7 @@ func TestReorgLongest(t *testing.T) { func TestReorgShortest(t *testing.T) { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() @@ -446,13 +446,13 @@ func TestReorgShortest(t *testing.T) { func TestInsertNonceError(t *testing.T) { for i := 1; i < 25 && !t.Failed(); i++ { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() } bc := chm(genesis, db) - bc.processor = NewBlockProcessor(db, db, bc.pow, bc, bc.eventMux) + bc.processor = NewBlockProcessor(db, bc.pow, bc, bc.eventMux) blocks := makeChain(bc.currentBlock, i, db, 0) fail := rand.Int() % len(blocks) diff --git a/core/genesis.go b/core/genesis.go index 4c0323c17..97afb3a4a 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -33,7 +33,7 @@ import ( ) // WriteGenesisBlock writes the genesis block to the database as block number 0 -func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*types.Block, error) { +func WriteGenesisBlock(chainDb common.Database, reader io.Reader) (*types.Block, error) { contents, err := ioutil.ReadAll(reader) if err != nil { return nil, err @@ -59,7 +59,7 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ return nil, err } - statedb := state.New(common.Hash{}, stateDb) + statedb := state.New(common.Hash{}, chainDb) for addr, account := range genesis.Alloc { address := common.HexToAddress(addr) statedb.AddBalance(address, common.String2Big(account.Balance)) @@ -84,9 +84,9 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ }, nil, nil, nil) block.Td = difficulty - if block := GetBlockByHash(blockDb, block.Hash()); block != nil { + if block := GetBlockByHash(chainDb, block.Hash()); block != nil { glog.V(logger.Info).Infoln("Genesis block already in chain. Writing canonical number") - err := WriteCanonNumber(blockDb, block) + err := WriteCanonNumber(chainDb, block) if err != nil { return nil, err } @@ -95,11 +95,11 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ statedb.Sync() - err = WriteBlock(blockDb, block) + err = WriteBlock(chainDb, block) if err != nil { return nil, err } - err = WriteHead(blockDb, block) + err = WriteHead(chainDb, block) if err != nil { return nil, err } @@ -133,11 +133,11 @@ func WriteGenesisBlockForTesting(db common.Database, addr common.Address, balanc "0x%x":{"balance":"0x%x"} } }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), addr, balance.Bytes()) - block, _ := WriteGenesisBlock(db, db, strings.NewReader(testGenesis)) + block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis)) return block } -func WriteTestNetGenesisBlock(stateDb, blockDb common.Database, nonce uint64) (*types.Block, error) { +func WriteTestNetGenesisBlock(chainDb common.Database, nonce uint64) (*types.Block, error) { testGenesis := fmt.Sprintf(`{ "nonce":"0x%x", "gasLimit":"0x%x", @@ -157,5 +157,5 @@ func WriteTestNetGenesisBlock(stateDb, blockDb common.Database, nonce uint64) (* "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"} } }`, types.EncodeNonce(nonce), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes()) - return WriteGenesisBlock(stateDb, blockDb, strings.NewReader(testGenesis)) + return WriteGenesisBlock(chainDb, strings.NewReader(testGenesis)) } diff --git a/core/manager.go b/core/manager.go index a07c32659..8b0401b03 100644 --- a/core/manager.go +++ b/core/manager.go @@ -28,8 +28,7 @@ type Backend interface { BlockProcessor() *BlockProcessor ChainManager() *ChainManager TxPool() *TxPool - BlockDb() common.Database - StateDb() common.Database - ExtraDb() common.Database + ChainDb() common.Database + DappDb() common.Database EventMux() *event.TypeMux } |