diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/block_processor.go | 10 | ||||
-rw-r--r-- | core/chain_makers.go | 2 | ||||
-rw-r--r-- | core/chain_manager.go | 72 | ||||
-rw-r--r-- | core/chain_manager_test.go | 2 | ||||
-rw-r--r-- | core/error.go | 4 | ||||
-rw-r--r-- | core/events.go | 2 | ||||
-rw-r--r-- | core/manager.go | 3 | ||||
-rw-r--r-- | core/state_transition.go | 1 |
8 files changed, 58 insertions, 38 deletions
diff --git a/core/block_processor.go b/core/block_processor.go index af47069ad..9a213686f 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -24,8 +24,6 @@ const ( BlockChainVersion = 2 ) -var statelogger = logger.NewLogger("BLOCK") - type BlockProcessor struct { db common.Database extraDb common.Database @@ -197,7 +195,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st // There can be at most two uncles if len(block.Uncles()) > 2 { - return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles())) + return nil, ValidationError("Block can only contain maximum 2 uncles (contained %v)", len(block.Uncles())) } receipts, err := sm.TransitionState(state, parent, block, false) @@ -347,17 +345,17 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty for i, uncle := range block.Uncles() { if uncles.Has(uncle.Hash()) { // Error not unique - return UncleError("Uncle not unique") + return UncleError("uncle[%d] not unique", i) } uncles.Add(uncle.Hash()) if ancestors.Has(uncle.Hash()) { - return UncleError("Uncle is ancestor") + return UncleError("uncle[%d] is ancestor", i) } if !ancestors.Has(uncle.ParentHash) { - return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) + return UncleError("uncle[%d]'s parent unknown (%x)", i, uncle.ParentHash[0:4]) } if err := sm.ValidateHeader(uncle, ancestorHeaders[uncle.ParentHash]); err != nil { diff --git a/core/chain_makers.go b/core/chain_makers.go index 5cd7ab4ab..acf7b39cc 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -98,7 +98,7 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Dat fmt.Println("process with parent failed", err) panic(err) } - block.Td = CalculateTD(block, parent) + block.Td = CalcTD(block, parent) blocks[i] = block parent = block } diff --git a/core/chain_manager.go b/core/chain_manager.go index 0480f692b..2e8eb927d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -48,7 +48,7 @@ func CalcDifficulty(block, parent *types.Header) *big.Int { return diff } -func CalculateTD(block, parent *types.Block) *big.Int { +func CalcTD(block, parent *types.Block) *big.Int { if parent == nil { return block.Difficulty() } @@ -59,14 +59,20 @@ func CalculateTD(block, parent *types.Block) *big.Int { } func CalcGasLimit(parent *types.Block) *big.Int { - // ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024 - previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit()) - current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5)) - curInt := new(big.Int).Div(current.Num(), current.Denom()) + decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor) + contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3)) + contrib = contrib.Div(contrib, big.NewInt(2)) + contrib = contrib.Div(contrib, params.GasLimitBoundDivisor) - result := new(big.Int).Add(previous, curInt) - result.Div(result, big.NewInt(1024)) - return common.BigMax(params.GenesisGasLimit, result) + gl := new(big.Int).Sub(parent.GasLimit(), decay) + gl = gl.Add(gl, contrib) + gl = common.BigMax(gl, params.MinGasLimit) + + if gl.Cmp(params.GenesisGasLimit) < 0 { + gl2 := new(big.Int).Add(parent.GasLimit(), decay) + return common.BigMin(params.GenesisGasLimit, gl2) + } + return gl } type ChainManager struct { @@ -525,7 +531,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { } // Setting block.Td regardless of error (known for example) prevents errors down the line // in the protocol handler - block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash()))) + block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) // Call in to the block processor and check for errors. It's likely that if one block fails // all others will fail too (unless a known block is returned). @@ -571,17 +577,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { // Compare the TD of the last known block in the canonical chain to make sure it's greater. // At this point it's possible that a different chain (fork) becomes the new canonical chain. if block.Td.Cmp(self.td) > 0 { - // Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging - if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() { - chash := cblock.Hash() - hash := block.Hash() - - if glog.V(logger.Info) { - glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], block.Td, cblock.Header().Number, chash[:4], self.td) - } - + // chain fork + if block.ParentHash() != cblock.Hash() { // during split we merge two different chains and create the new canonical chain - self.merge(previous, block) + self.merge(cblock, block) queue[i] = ChainSplitEvent{block, logs} queueEvent.splitCount++ @@ -600,7 +599,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { self.setTransState(state.New(block.Root(), self.stateDb)) self.txState.SetState(state.New(block.Root(), self.stateDb)) - queue[i] = ChainEvent{block, logs} + queue[i] = ChainEvent{block, block.Hash(), logs} queueEvent.canonicalCount++ if glog.V(logger.Debug) { @@ -636,17 +635,29 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { // diff takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them // to be part of the new canonical chain. func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { - glog.V(logger.Debug).Infof("Applying diff to %x & %x\n", oldBlock.Hash().Bytes()[:4], newBlock.Hash().Bytes()[:4]) + var ( + newChain types.Blocks + commonBlock *types.Block + oldStart = oldBlock + newStart = newBlock + ) - var newChain types.Blocks - // first find common number - for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { - newChain = append(newChain, newBlock) + // first reduce whoever is higher bound + if oldBlock.NumberU64() > newBlock.NumberU64() { + // reduce old chain + for oldBlock = oldBlock; oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) { + } + } else { + // reduce new chain and append new chain blocks for inserting later on + for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { + newChain = append(newChain, newBlock) + } } - glog.V(logger.Debug).Infoln("Found common number", newBlock.Number()) + numSplit := newBlock.Number() for { if oldBlock.Hash() == newBlock.Hash() { + commonBlock = oldBlock break } newChain = append(newChain, newBlock) @@ -654,6 +665,11 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash()) } + if glog.V(logger.Info) { + commonHash := commonBlock.Hash() + glog.Infof("Fork detected @ %x. Reorganising chain from #%v %x to %x", commonHash[:4], numSplit, oldStart.Hash().Bytes()[:4], newStart.Hash().Bytes()[:4]) + } + return newChain } @@ -661,7 +677,7 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { func (self *ChainManager) merge(oldBlock, newBlock *types.Block) { newChain := self.diff(oldBlock, newBlock) - // insert blocks + // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly for _, block := range newChain { self.insert(block) } @@ -681,7 +697,7 @@ out: case ChainEvent: // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long // and in most cases isn't even necessary. - if i+1 == ev.canonicalCount { + if self.lastBlockHash == event.Hash { self.currentGasLimit = CalcGasLimit(event.Block) self.eventMux.Post(ChainHeadEvent{event.Block}) } diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index f456e4fff..b5155e223 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -81,7 +81,7 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { return nil, err } parent := bman.bc.GetBlock(block.ParentHash()) - block.Td = CalculateTD(block, parent) + block.Td = CalcTD(block, parent) td = block.Td bman.bc.mu.Lock() diff --git a/core/error.go b/core/error.go index 40db99ecd..2bdad364f 100644 --- a/core/error.go +++ b/core/error.go @@ -42,8 +42,8 @@ func (err *UncleErr) Error() string { return err.Message } -func UncleError(str string) error { - return &UncleErr{Message: str} +func UncleError(format string, v ...interface{}) error { + return &UncleErr{Message: fmt.Sprintf(format, v...)} } func IsUncleErr(err error) bool { diff --git a/core/events.go b/core/events.go index 1ea35c2f4..7b56f8bb6 100644 --- a/core/events.go +++ b/core/events.go @@ -3,6 +3,7 @@ package core import ( "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" ) @@ -27,6 +28,7 @@ type ChainSplitEvent struct { type ChainEvent struct { Block *types.Block + Hash common.Hash Logs state.Logs } diff --git a/core/manager.go b/core/manager.go index 433ada7ee..695f0e99c 100644 --- a/core/manager.go +++ b/core/manager.go @@ -3,10 +3,12 @@ package core import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/p2p" ) +// TODO move this to types? type Backend interface { AccountManager() *accounts.Manager BlockProcessor() *BlockProcessor @@ -18,4 +20,5 @@ type Backend interface { BlockDb() common.Database StateDb() common.Database EventMux() *event.TypeMux + Downloader() *downloader.Downloader } diff --git a/core/state_transition.go b/core/state_transition.go index d95cbd35a..3d11a0464 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -205,6 +205,7 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er if err := self.UseGas(dataGas); err == nil { ref.SetCode(ret) } else { + ret = nil // does not affect consensus but useful for StateTests validations glog.V(logger.Core).Infoln("Insufficient gas for creating code. Require", dataGas, "and have", self.gas) } } |