core: renamed next to pending & fixed tests

This commit is contained in:
obscuren 2015-06-20 15:56:34 +02:00 committed by Jeffrey Wilcke
parent 855e76fddd
commit 07db098ccf
2 changed files with 14 additions and 17 deletions

View File

@ -108,9 +108,9 @@ type ChainManager struct {
transState *state.StateDB transState *state.StateDB
txState *state.ManagedState txState *state.ManagedState
cache *lru.Cache // cache is the LRU caching cache *lru.Cache // cache is the LRU caching
futureBlocks *BlockCache // future blocks are blocks added for later processing futureBlocks *BlockCache // future blocks are blocks added for later processing
nextBlocks *BlockCache // next blocks is used during large inserts pendingBlocks *BlockCache // pending blocks contain blocks not yet written to the db
quit chan struct{} quit chan struct{}
// procInterrupt must be atomically called // procInterrupt must be atomically called
@ -389,8 +389,8 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool {
return true return true
} }
if bc.nextBlocks != nil { if bc.pendingBlocks != nil {
if block := bc.nextBlocks.Get(hash); block != nil { if block := bc.pendingBlocks.Get(hash); block != nil {
return true return true
} }
} }
@ -425,8 +425,8 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
return block.(*types.Block) return block.(*types.Block)
} }
if self.nextBlocks != nil { if self.pendingBlocks != nil {
if block := self.nextBlocks.Get(hash); block != nil { if block := self.pendingBlocks.Get(hash); block != nil {
return block return block
} }
} }
@ -521,13 +521,13 @@ func (self *ChainManager) procFutureBlocks() {
} }
func (self *ChainManager) enqueueForWrite(block *types.Block) { func (self *ChainManager) enqueueForWrite(block *types.Block) {
self.nextBlocks.Push(block) self.pendingBlocks.Push(block)
} }
func (self *ChainManager) flushQueuedBlocks() { func (self *ChainManager) flushQueuedBlocks() {
db, batchWrite := self.blockDb.(*ethdb.LDBDatabase) db, batchWrite := self.blockDb.(*ethdb.LDBDatabase)
batch := new(leveldb.Batch) batch := new(leveldb.Batch)
self.nextBlocks.Each(func(i int, block *types.Block) { self.pendingBlocks.Each(func(i int, block *types.Block) {
enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block)) enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
key := append(blockHashPre, block.Hash().Bytes()...) key := append(blockHashPre, block.Hash().Bytes()...)
if batchWrite { if batchWrite {
@ -539,10 +539,6 @@ func (self *ChainManager) flushQueuedBlocks() {
if batchWrite { if batchWrite {
db.LDB().Write(batch, nil) db.LDB().Write(batch, nil)
} }
// reset the next blocks cache
self.nextBlocks = nil
} }
// InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
@ -554,7 +550,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
self.chainmu.Lock() self.chainmu.Lock()
defer self.chainmu.Unlock() defer self.chainmu.Unlock()
self.nextBlocks = NewBlockCache(len(chain)) self.pendingBlocks = NewBlockCache(len(chain))
// A queued approach to delivering events. This is generally // A queued approach to delivering events. This is generally
// faster than direct delivery and requires much less mutex // faster than direct delivery and requires much less mutex
@ -687,7 +683,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
queue[i] = ChainSideEvent{block, logs} queue[i] = ChainSideEvent{block, logs}
queueEvent.sideCount++ queueEvent.sideCount++
} }
// Write block to database. Eventually we'll have to improve on this and throw away blocks that are
// not in the canonical chain. // not in the canonical chain.
self.enqueueForWrite(block) self.enqueueForWrite(block)
// Delete from future blocks // Delete from future blocks

View File

@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/hashicorp/golang-lru"
) )
func init() { func init() {
@ -109,7 +110,8 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) {
bman.bc.mu.Lock() bman.bc.mu.Lock()
{ {
bman.bc.write(block) bman.bc.enqueueForWrite(block)
//bman.bc.write(block)
} }
bman.bc.mu.Unlock() bman.bc.mu.Unlock()
} }
@ -391,7 +393,7 @@ func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block
func chm(genesis *types.Block, db common.Database) *ChainManager { func chm(genesis *types.Block, db common.Database) *ChainManager {
var eventMux event.TypeMux var eventMux event.TypeMux
bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}}
bc.cache = NewBlockCache(100) bc.cache, _ = lru.New(100)
bc.futureBlocks = NewBlockCache(100) bc.futureBlocks = NewBlockCache(100)
bc.processor = bproc{} bc.processor = bproc{}
bc.ResetWithGenesisBlock(genesis) bc.ResetWithGenesisBlock(genesis)