aboutsummaryrefslogtreecommitdiffstats
path: root/chain
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-12-02 18:52:56 +0800
committerobscuren <geffobscura@gmail.com>2014-12-02 18:52:56 +0800
commit64f35ba8d1f31d6821a0a1bf946c71396a996f30 (patch)
tree375a081536c7c0b329a3b0c2e812ff05f81dd64c /chain
parent616066a598933df7ef126186eb9c647094f665ca (diff)
parent99481a245adc2c4814ab6b38d94d63114f7bbb15 (diff)
downloadgo-tangerine-64f35ba8d1f31d6821a0a1bf946c71396a996f30.tar.gz
go-tangerine-64f35ba8d1f31d6821a0a1bf946c71396a996f30.tar.zst
go-tangerine-64f35ba8d1f31d6821a0a1bf946c71396a996f30.zip
merge errors fixed
Diffstat (limited to 'chain')
-rw-r--r--chain/block_manager.go32
-rw-r--r--chain/chain_manager.go119
-rw-r--r--chain/error.go13
-rw-r--r--chain/transaction_pool.go45
-rw-r--r--chain/types/transaction.go7
5 files changed, 81 insertions, 135 deletions
diff --git a/chain/block_manager.go b/chain/block_manager.go
index 4cc43840c..e40ff7955 100644
--- a/chain/block_manager.go
+++ b/chain/block_manager.go
@@ -187,6 +187,7 @@ func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Mes
defer sm.mutex.Unlock()
if sm.bc.HasBlock(block.Hash()) {
+ fmt.Println("already having this block")
return nil, nil, nil
}
@@ -213,7 +214,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I
fmt.Printf("## %x %x ##\n", block.Hash(), block.Number)
}
- receipts, err := sm.ApplyDiff(state, parent, block)
+ _, err = sm.ApplyDiff(state, parent, block)
if err != nil {
return
}
@@ -224,11 +225,13 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I
return
}
- receiptSha := types.DeriveSha(receipts)
- if bytes.Compare(receiptSha, block.ReceiptSha) != 0 {
- err = fmt.Errorf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha)
- return
- }
+ /*
+ receiptSha := types.DeriveSha(receipts)
+ if bytes.Compare(receiptSha, block.ReceiptSha) != 0 {
+ err = fmt.Errorf("validating receipt root. received=%x got=%x", block.ReceiptSha, receiptSha)
+ return
+ }
+ */
// Block validation
if err = sm.ValidateBlock(block, parent); err != nil {
@@ -241,14 +244,16 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I
return
}
- //block.receipts = receipts // although this isn't necessary it be in the future
- rbloom := types.CreateBloom(receipts)
- if bytes.Compare(rbloom, block.LogsBloom) != 0 {
- err = fmt.Errorf("unable to replicate block's bloom=%x", rbloom)
- return
- }
+ /*
+ //block.receipts = receipts // although this isn't necessary it be in the future
+ rbloom := types.CreateBloom(receipts)
+ if bytes.Compare(rbloom, block.LogsBloom) != 0 {
+ err = fmt.Errorf("unable to replicate block's bloom=%x", rbloom)
+ return
+ }
+ */
- state.Update(nil)
+ state.Update(ethutil.Big0)
if !block.State().Cmp(state) {
err = fmt.Errorf("invalid merkle root. received=%x got=%x", block.Root(), state.Root())
@@ -268,6 +273,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I
sm.transState = state.Copy()
sm.eth.TxPool().RemoveSet(block.Transactions())
+ fmt.Println("TD", td)
return td, messages, nil
} else {
diff --git a/chain/chain_manager.go b/chain/chain_manager.go
index 115acb1c8..8e0529533 100644
--- a/chain/chain_manager.go
+++ b/chain/chain_manager.go
@@ -1,15 +1,12 @@
package chain
import (
- "bytes"
- "container/list"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/chain/types"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/state"
)
var chainlogger = logger.NewLogger("CHAIN")
@@ -56,8 +53,6 @@ type ChainManager struct {
CurrentBlock *types.Block
LastBlockHash []byte
-
- workingChain *BlockChain
}
func NewChainManager() *ChainManager {
@@ -186,15 +181,6 @@ func (self *ChainManager) GetChainHashesFromHash(hash []byte, max uint64) (chain
func (self *ChainManager) GetBlock(hash []byte) *types.Block {
data, _ := ethutil.Config.Db.Get(hash)
if len(data) == 0 {
- if self.workingChain != nil {
- // Check the temp chain
- for e := self.workingChain.Front(); e != nil; e = e.Next() {
- if bytes.Compare(e.Value.(*link).Block.Hash(), hash) == 0 {
- return e.Value.(*link).Block
- }
- }
- }
-
return nil
}
@@ -264,103 +250,22 @@ func (bc *ChainManager) Stop() {
}
}
-func (self *ChainManager) NewIterator(startHash []byte) *ChainIterator {
- return &ChainIterator{self, self.GetBlock(startHash)}
-}
-
-// This function assumes you've done your checking. No checking is done at this stage anymore
-func (self *ChainManager) InsertChain(chain *BlockChain, call func(*types.Block, state.Messages)) {
- for e := chain.Front(); e != nil; e = e.Next() {
- link := e.Value.(*link)
-
- self.add(link.Block)
- self.SetTotalDifficulty(link.Td)
-
- call(link.Block, link.Messages)
- }
-
- b, e := chain.Front(), chain.Back()
- if b != nil && e != nil {
- front, back := b.Value.(*link).Block, e.Value.(*link).Block
- chainlogger.Infof("Imported %d blocks. #%v (%x) / %#v (%x)", chain.Len(), front.Number, front.Hash()[0:4], back.Number, back.Hash()[0:4])
- }
-}
-
-func (self *ChainManager) TestChain(chain *BlockChain) (td *big.Int, err error) {
- self.workingChain = chain
- defer func() { self.workingChain = nil }()
-
- for e := chain.Front(); e != nil; e = e.Next() {
- var (
- l = e.Value.(*link)
- block = l.Block
- parent = self.GetBlock(block.PrevHash)
- )
-
- if parent == nil {
- err = fmt.Errorf("incoming chain broken on hash %x\n", block.PrevHash[0:4])
- return
- }
-
- var messages state.Messages
- td, messages, err = self.processor.ProcessWithParent(block, parent) //self.eth.BlockManager().ProcessWithParent(block, parent)
+func (self *ChainManager) InsertChain(chain Blocks) error {
+ for _, block := range chain {
+ td, messages, err := self.Ethereum.BlockManager().Process(block)
if err != nil {
- chainlogger.Infoln(err)
- chainlogger.Debugf("Block #%v failed (%x...)\n", block.Number, block.Hash()[0:4])
- chainlogger.Debugln(block)
+ if IsKnownBlockErr(err) {
+ continue
+ }
- err = fmt.Errorf("incoming chain failed %v\n", err)
- return
+ return err
}
- l.Td = td
- l.Messages = messages
- }
- if td.Cmp(self.TD) <= 0 {
- err = &TDError{td, self.TD}
- return
+ self.add(block)
+ self.SetTotalDifficulty(td)
+ self.Ethereum.EventMux().Post(NewBlockEvent{block})
+ self.Ethereum.EventMux().Post(messages)
}
- self.workingChain = nil
-
- return
-}
-
-type link struct {
- Block *types.Block
- Messages state.Messages
- Td *big.Int
-}
-
-type BlockChain struct {
- *list.List
-}
-
-func NewChain(blocks types.Blocks) *BlockChain {
- chain := &BlockChain{list.New()}
-
- for _, block := range blocks {
- chain.PushBack(&link{block, nil, nil})
- }
-
- return chain
-}
-
-func (self *BlockChain) RlpEncode() []byte {
- dat := make([]interface{}, 0)
- for e := self.Front(); e != nil; e = e.Next() {
- dat = append(dat, e.Value.(*link).Block.RlpData())
- }
-
- return ethutil.Encode(dat)
-}
-
-type ChainIterator struct {
- cm *ChainManager
- block *types.Block // current block in the iterator
-}
-
-func (self *ChainIterator) Prev() *types.Block {
- self.block = self.cm.GetBlock(self.block.PrevHash)
- return self.block
+ return nil
}
diff --git a/chain/error.go b/chain/error.go
index 540eda95a..7dce2b608 100644
--- a/chain/error.go
+++ b/chain/error.go
@@ -126,3 +126,16 @@ func IsTDError(e error) bool {
_, ok := e.(*TDError)
return ok
}
+
+type KnownBlockError struct {
+ number uint64
+ hash []byte
+}
+
+func (self *KnownBlockError) Error() string {
+ return fmt.Sprintf("block %d already known (%x)", self.number, self.hash[0:4])
+}
+func IsKnownBlockErr(e error) bool {
+ _, ok := e.(*KnownBlockError)
+ return ok
+}
diff --git a/chain/transaction_pool.go b/chain/transaction_pool.go
index 119712ba8..15c34cc39 100644
--- a/chain/transaction_pool.go
+++ b/chain/transaction_pool.go
@@ -103,11 +103,15 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
block := pool.Ethereum.ChainManager().CurrentBlock
// Something has gone horribly wrong if this happens
if block == nil {
- return fmt.Errorf("[TXPL] No last block on the block chain")
+ return fmt.Errorf("No last block on the block chain")
}
if len(tx.Recipient) != 0 && len(tx.Recipient) != 20 {
- return fmt.Errorf("[TXPL] Invalid recipient. len = %d", len(tx.Recipient))
+ return fmt.Errorf("Invalid recipient. len = %d", len(tx.Recipient))
+ }
+
+ if tx.v > 28 || tx.v < 27 {
+ return fmt.Errorf("tx.v != (28 || 27)")
}
if tx.GasPrice.Cmp(MinGasPrice) < 0 {
@@ -115,19 +119,18 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
}
// Get the sender
- //sender := pool.Ethereum.BlockManager().procState.GetAccount(tx.Sender())
sender := pool.Ethereum.BlockManager().CurrentState().GetAccount(tx.Sender())
totAmount := new(big.Int).Set(tx.Value)
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
if sender.Balance().Cmp(totAmount) < 0 {
- return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
+ return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.Sender())
}
if tx.IsContract() {
if tx.GasPrice.Cmp(big.NewInt(minGasPrice)) < 0 {
- return fmt.Errorf("[TXPL] Gasprice too low, %s given should be at least %d.", tx.GasPrice, minGasPrice)
+ return fmt.Errorf("Gasprice too low, %s given should be at least %d.", tx.GasPrice, minGasPrice)
}
}
@@ -137,6 +140,34 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
return nil
}
+func (self *TxPool) Add(tx *types.Transaction) error {
+ hash := tx.Hash()
+ foundTx := FindTx(self.pool, func(tx *Transaction, e *list.Element) bool {
+ return bytes.Compare(tx.Hash(), hash) == 0
+ })
+
+ if foundTx != nil {
+ return fmt.Errorf("Known transaction (%x)", hash[0:4])
+ }
+
+ err := self.ValidateTransaction(tx)
+ if err != nil {
+ return err
+ }
+
+ self.addTransaction(tx)
+
+ tmp := make([]byte, 4)
+ copy(tmp, tx.Recipient)
+
+ txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash())
+
+ // Notify the subscribers
+ self.Ethereum.EventMux().Post(TxPreEvent{tx})
+
+ return nil
+}
+
func (pool *TxPool) queueHandler() {
out:
for {
@@ -173,10 +204,6 @@ out:
}
}
-func (pool *TxPool) QueueTransaction(tx *types.Transaction) {
- pool.queueChan <- tx
-}
-
func (pool *TxPool) CurrentTransactions() []*types.Transaction {
pool.mutex.Lock()
defer pool.mutex.Unlock()
diff --git a/chain/types/transaction.go b/chain/types/transaction.go
index 626a7e5ce..b6424482a 100644
--- a/chain/types/transaction.go
+++ b/chain/types/transaction.go
@@ -79,12 +79,7 @@ func (tx *Transaction) IsContract() bool {
func (tx *Transaction) CreationAddress(state *state.State) []byte {
// Generate a new address
- addr := crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
- //for i := uint64(0); state.GetStateObject(addr) != nil; i++ {
- // addr = crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce + i}).Encode())[12:]
- //}
-
- return addr
+ return crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
}
func (tx *Transaction) Signature(key []byte) []byte {