aboutsummaryrefslogtreecommitdiffstats
path: root/chain
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-11-04 19:46:33 +0800
committerobscuren <geffobscura@gmail.com>2014-11-04 19:46:33 +0800
commit699dcaf65ced99517724984f5930845417cfdfca (patch)
tree36bdb1d1c992924750088b431cac94566df443cd /chain
parentf4b717cb9da6113304f243caea6a3799a1aeecf3 (diff)
downloadgo-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar.gz
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar.bz2
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar.lz
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar.xz
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.tar.zst
go-tangerine-699dcaf65ced99517724984f5930845417cfdfca.zip
Reworked chain handling process
* Forks * Rename * Moved inserting of blocks & processing * Added chain testing method for validating pieces of a **a** chain.
Diffstat (limited to 'chain')
-rw-r--r--chain/block_manager.go68
-rw-r--r--chain/chain_manager.go80
2 files changed, 104 insertions, 44 deletions
diff --git a/chain/block_manager.go b/chain/block_manager.go
index 1a5e3aeb3..ed2fbfe8c 100644
--- a/chain/block_manager.go
+++ b/chain/block_manager.go
@@ -102,7 +102,7 @@ func (self *BlockManager) Stop() {
func (self *BlockManager) updateThread() {
for ev := range self.events.Chan() {
for _, block := range ev.(Blocks) {
- err := self.Process(block)
+ _, err := self.Process(block)
if err != nil {
statelogger.Infoln(err)
statelogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4])
@@ -208,25 +208,27 @@ done:
return receipts, handled, unhandled, erroneous, err
}
-func (sm *BlockManager) Process(block *Block) (err error) {
+func (sm *BlockManager) Process(block *Block) (td *big.Int, err error) {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
if sm.bc.HasBlock(block.Hash()) {
- return nil
+ return nil, nil
}
if !sm.bc.HasBlock(block.PrevHash) {
- return ParentError(block.PrevHash)
+ return nil, ParentError(block.PrevHash)
}
+ parent := sm.bc.GetBlock(block.PrevHash)
+
+ return sm.ProcessWithParent(block, parent)
+}
+func (sm *BlockManager) ProcessWithParent(block, parent *Block) (td *big.Int, err error) {
sm.lastAttemptedBlock = block
- var (
- parent = sm.bc.GetBlock(block.PrevHash)
- state = parent.State()
- )
+ state := parent.State()
// Defer the Undo on the Trie. If the block processing happened
// we don't want to undo but since undo only happens on dirty
@@ -240,32 +242,32 @@ func (sm *BlockManager) Process(block *Block) (err error) {
txSha := DeriveSha(block.transactions)
if bytes.Compare(txSha, block.TxSha) != 0 {
- return fmt.Errorf("Error validating transaction sha. Received %x, got %x", block.TxSha, txSha)
+ return nil, fmt.Errorf("Error validating transaction sha. Received %x, got %x", block.TxSha, txSha)
}
receipts, err := sm.ApplyDiff(state, parent, block)
if err != nil {
- return err
+ return nil, err
}
receiptSha := DeriveSha(receipts)
if bytes.Compare(receiptSha, block.ReceiptSha) != 0 {
- return fmt.Errorf("Error validating receipt sha. Received %x, got %x", block.ReceiptSha, receiptSha)
+ return nil, fmt.Errorf("Error validating receipt sha. Received %x, got %x", block.ReceiptSha, receiptSha)
}
// Block validation
- if err = sm.ValidateBlock(block); err != nil {
+ if err = sm.ValidateBlock(block, parent); err != nil {
statelogger.Errorln("Error validating block:", err)
- return err
+ return nil, err
}
if err = sm.AccumelateRewards(state, block, parent); err != nil {
statelogger.Errorln("Error accumulating reward", err)
- return err
+ return nil, err
}
if bytes.Compare(CreateBloom(block), block.LogsBloom) != 0 {
- return errors.New("Unable to replicate block's bloom")
+ return nil, errors.New("Unable to replicate block's bloom")
}
state.Update()
@@ -276,27 +278,22 @@ func (sm *BlockManager) Process(block *Block) (err error) {
}
// Calculate the new total difficulty and sync back to the db
- if sm.CalculateTD(block) {
+ if td, ok := sm.CalculateTD(block); ok {
// Sync the current block's state to the database and cancelling out the deferred Undo
state.Sync()
- // Add the block to the chain
- sm.bc.Add(block)
-
// TODO at this point we should also insert LOGS in to a database
sm.transState = state.Copy()
- statelogger.Infof("Imported block #%d (%x...)\n", block.Number, block.Hash()[0:4])
-
state.Manifest().Reset()
sm.eth.TxPool().RemoveSet(block.Transactions())
+
+ return td, nil
} else {
- statelogger.Errorln("total diff failed")
+ return nil, errors.New("total diff failed")
}
-
- return nil
}
func (sm *BlockManager) ApplyDiff(state *state.State, parent, block *Block) (receipts Receipts, err error) {
@@ -312,7 +309,7 @@ func (sm *BlockManager) ApplyDiff(state *state.State, parent, block *Block) (rec
return receipts, nil
}
-func (sm *BlockManager) CalculateTD(block *Block) bool {
+func (sm *BlockManager) CalculateTD(block *Block) (*big.Int, bool) {
uncleDiff := new(big.Int)
for _, uncle := range block.Uncles {
uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
@@ -326,30 +323,19 @@ func (sm *BlockManager) CalculateTD(block *Block) bool {
// The new TD will only be accepted if the new difficulty is
// is greater than the previous.
if td.Cmp(sm.bc.TD) > 0 {
- // Set the new total difficulty back to the block chain
- sm.bc.SetTotalDifficulty(td)
+ return td, true
- return true
+ // Set the new total difficulty back to the block chain
+ //sm.bc.SetTotalDifficulty(td)
}
- return false
+ return nil, false
}
// Validates the current block. Returns an error if the block was invalid,
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
-func (sm *BlockManager) ValidateBlock(block *Block) error {
- // Check each uncle's previous hash. In order for it to be valid
- // is if it has the same block hash as the current
- parent := sm.bc.GetBlock(block.PrevHash)
- /*
- for _, uncle := range block.Uncles {
- if bytes.Compare(uncle.PrevHash,parent.PrevHash) != 0 {
- return ValidationError("Mismatch uncle's previous hash. Expected %x, got %x",parent.PrevHash, uncle.PrevHash)
- }
- }
- */
-
+func (sm *BlockManager) ValidateBlock(block, parent *Block) error {
expd := CalcDifficulty(block, parent)
if expd.Cmp(block.Difficulty) < 0 {
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
diff --git a/chain/chain_manager.go b/chain/chain_manager.go
index 8ee7a85cc..60de377aa 100644
--- a/chain/chain_manager.go
+++ b/chain/chain_manager.go
@@ -2,6 +2,7 @@ package chain
import (
"bytes"
+ "container/list"
"fmt"
"math/big"
@@ -86,7 +87,7 @@ func (bc *ChainManager) Reset() {
bc.genesisBlock.state.Trie.Sync()
// Prepare the genesis block
- bc.Add(bc.genesisBlock)
+ bc.add(bc.genesisBlock)
bc.CurrentBlock = bc.genesisBlock
bc.SetTotalDifficulty(ethutil.Big("0"))
@@ -191,9 +192,8 @@ func (bc *ChainManager) SetTotalDifficulty(td *big.Int) {
}
// Add a block to the chain and record addition information
-func (bc *ChainManager) Add(block *Block) {
+func (bc *ChainManager) add(block *Block) {
bc.writeBlockInfo(block)
- // Prepare the genesis block
bc.CurrentBlock = block
bc.LastBlockHash = block.Hash()
@@ -201,6 +201,8 @@ func (bc *ChainManager) Add(block *Block) {
encodedBlock := block.RlpEncode()
ethutil.Config.Db.Put(block.Hash(), encodedBlock)
ethutil.Config.Db.Put([]byte("LastBlock"), encodedBlock)
+
+ chainlogger.Infof("Imported block #%d (%x...)\n", block.Number, block.Hash()[0:4])
}
func (self *ChainManager) CalcTotalDiff(block *Block) (*big.Int, error) {
@@ -287,3 +289,75 @@ func (bc *ChainManager) Stop() {
chainlogger.Infoln("Stopped")
}
}
+
+type link struct {
+ block *Block
+ td *big.Int
+}
+
+type BlockChain struct {
+ *list.List
+}
+
+func NewChain(blocks Blocks) *BlockChain {
+ chain := &BlockChain{list.New()}
+
+ for _, block := range blocks {
+ chain.PushBack(&link{block, nil})
+ }
+
+ return chain
+}
+
+// This function assumes you've done your checking. No checking is done at this stage anymore
+func (self *ChainManager) InsertChain(chain *BlockChain) {
+ for e := chain.Front(); e != nil; e = e.Next() {
+ link := e.Value.(*link)
+
+ self.SetTotalDifficulty(link.td)
+ self.add(link.block)
+ }
+}
+
+func (self *ChainManager) TestChain(chain *BlockChain) (i int, err error) {
+ var (
+ td *big.Int
+ )
+ for e := chain.Front(); e != nil; e = e.Next() {
+ var (
+ l = e.Value.(*link)
+ block = l.block
+ parent *Block
+ prev = e.Prev()
+ )
+ if prev == nil {
+ parent = self.GetBlock(block.PrevHash)
+ } else {
+ parent = prev.Value.(*link).block
+ }
+
+ if parent == nil {
+ err = fmt.Errorf("incoming chain broken on hash %x\n", block.PrevHash[0:4])
+ return
+ }
+
+ td, err = self.Ethereum.BlockManager().ProcessWithParent(block, parent)
+ if err != nil {
+ chainlogger.Infoln(err)
+ chainlogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4])
+ chainlogger.Debugln(block)
+
+ err = fmt.Errorf("incoming chain failed %v\n", err)
+ return
+ }
+ l.td = td
+ i++
+ }
+
+ if td.Cmp(self.TD) <= 0 {
+ err = fmt.Errorf("incoming chain has a lower or equal TD (%v <= %v)", td, self.TD)
+ return
+ }
+
+ return i, nil
+}