From 82405501872385b240012070bad2f0eda643d423 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 3 Dec 2014 14:05:19 +0100 Subject: [PATCH] updated to types --- chain/chain_manager.go | 14 +++++++----- chain/transaction_pool.go | 45 ++++---------------------------------- chain/types/common.go | 3 ++- chain/types/transaction.go | 14 ++++++++---- ethereum.go | 2 +- xeth/hexface.go | 2 +- xeth/pipe.go | 10 ++++----- 7 files changed, 31 insertions(+), 59 deletions(-) diff --git a/chain/chain_manager.go b/chain/chain_manager.go index 8525ffdfd..9b35ce08a 100644 --- a/chain/chain_manager.go +++ b/chain/chain_manager.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" ) @@ -45,6 +46,7 @@ func CalcDifficulty(block, parent *types.Block) *big.Int { type ChainManager struct { //eth EthManager processor types.BlockProcessor + eventMux *event.TypeMux genesisBlock *types.Block // Last known total difficulty TD *big.Int @@ -55,10 +57,10 @@ type ChainManager struct { LastBlockHash []byte } -func NewChainManager() *ChainManager { +func NewChainManager(mux *event.TypeMux) *ChainManager { bc := &ChainManager{} bc.genesisBlock = types.NewBlockFromBytes(ethutil.Encode(Genesis)) - //bc.eth = ethereum + bc.eventMux = mux bc.setLastBlock() @@ -250,9 +252,9 @@ func (bc *ChainManager) Stop() { } } -func (self *ChainManager) InsertChain(chain Blocks) error { +func (self *ChainManager) InsertChain(chain types.Blocks) error { for _, block := range chain { - td, messages, err := self.Ethereum.BlockManager().Process(block) + td, messages, err := self.processor.Process(block) if err != nil { if IsKnownBlockErr(err) { continue @@ -266,8 +268,8 @@ func (self *ChainManager) InsertChain(chain Blocks) error { self.add(block) self.SetTotalDifficulty(td) - self.Ethereum.EventMux().Post(NewBlockEvent{block}) - self.Ethereum.EventMux().Post(messages) + self.eventMux.Post(NewBlockEvent{block}) + self.eventMux.Post(messages) } return nil diff --git a/chain/transaction_pool.go b/chain/transaction_pool.go index 15c34cc39..ad89c0a98 100644 --- a/chain/transaction_pool.go +++ b/chain/transaction_pool.go @@ -110,7 +110,8 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { return fmt.Errorf("Invalid recipient. len = %d", len(tx.Recipient)) } - if tx.v > 28 || tx.v < 27 { + v, _, _ := tx.Curve() + if v > 28 || v < 27 { return fmt.Errorf("tx.v != (28 || 27)") } @@ -142,7 +143,7 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { func (self *TxPool) Add(tx *types.Transaction) error { hash := tx.Hash() - foundTx := FindTx(self.pool, func(tx *Transaction, e *list.Element) bool { + foundTx := FindTx(self.pool, func(tx *types.Transaction, e *list.Element) bool { return bytes.Compare(tx.Hash(), hash) == 0 }) @@ -168,42 +169,6 @@ func (self *TxPool) Add(tx *types.Transaction) error { return nil } -func (pool *TxPool) queueHandler() { -out: - for { - select { - case tx := <-pool.queueChan: - hash := tx.Hash() - foundTx := FindTx(pool.pool, func(tx *types.Transaction, e *list.Element) bool { - return bytes.Compare(tx.Hash(), hash) == 0 - }) - - if foundTx != nil { - break - } - - // Validate the transaction - err := pool.ValidateTransaction(tx) - if err != nil { - txplogger.Debugln("Validating Tx failed", err) - } else { - // Call blocking version. - pool.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 - pool.Ethereum.EventMux().Post(TxPreEvent{tx}) - } - case <-pool.quit: - break out - } - } -} - func (pool *TxPool) CurrentTransactions() []*types.Transaction { pool.mutex.Lock() defer pool.mutex.Unlock() @@ -261,12 +226,10 @@ func (pool *TxPool) Flush() []*types.Transaction { } func (pool *TxPool) Start() { - go pool.queueHandler() + //go pool.queueHandler() } func (pool *TxPool) Stop() { - close(pool.quit) - pool.Flush() txplogger.Infoln("Stopped") diff --git a/chain/types/common.go b/chain/types/common.go index ae0e7c3fa..ba88b77e1 100644 --- a/chain/types/common.go +++ b/chain/types/common.go @@ -2,9 +2,10 @@ package types import ( "math/big" + "github.com/ethereum/go-ethereum/state" ) type BlockProcessor interface { - ProcessWithParent(*Block, *Block) (*big.Int, state.Messages, error) + Process(*Block) (*big.Int, state.Messages, error) } diff --git a/chain/types/transaction.go b/chain/types/transaction.go index b6424482a..efc90b6f2 100644 --- a/chain/types/transaction.go +++ b/chain/types/transaction.go @@ -82,6 +82,14 @@ func (tx *Transaction) CreationAddress(state *state.State) []byte { return crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:] } +func (tx *Transaction) Curve() (v byte, r []byte, s []byte) { + v = tx.v + r = ethutil.LeftPadBytes(tx.r, 32) + s = ethutil.LeftPadBytes(tx.s, 32) + + return +} + func (tx *Transaction) Signature(key []byte) []byte { hash := tx.Hash() @@ -93,12 +101,10 @@ func (tx *Transaction) Signature(key []byte) []byte { func (tx *Transaction) PublicKey() []byte { hash := tx.Hash() - // TODO - r := ethutil.LeftPadBytes(tx.r, 32) - s := ethutil.LeftPadBytes(tx.s, 32) + v, r, s := tx.Curve() sig := append(r, s...) - sig = append(sig, tx.v-27) + sig = append(sig, v-27) pubkey := crypto.Ecrecover(append(hash, sig...)) //pubkey, _ := secp256k1.RecoverPubkey(hash, sig) diff --git a/ethereum.go b/ethereum.go index 879a14bd5..530d26fce 100644 --- a/ethereum.go +++ b/ethereum.go @@ -129,7 +129,7 @@ func New(db ethutil.Database, clientIdentity wire.ClientIdentity, keyManager *cr ethereum.blockPool = NewBlockPool(ethereum) ethereum.txPool = chain.NewTxPool(ethereum) - ethereum.blockChain = chain.NewChainManager() + ethereum.blockChain = chain.NewChainManager(ethereum.EventMux()) ethereum.blockManager = chain.NewBlockManager(ethereum) ethereum.blockChain.SetProcessor(ethereum.blockManager) diff --git a/xeth/hexface.go b/xeth/hexface.go index 9b7aa6b9c..6360c7675 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -215,7 +215,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr if err != nil { return "", err } - if chain.IsContractAddr(to) { + if types.IsContractAddr(to) { return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil } diff --git a/xeth/pipe.go b/xeth/pipe.go index 9cc163a81..c96c6efc0 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -93,7 +93,7 @@ func (self *XEth) Exists(addr []byte) bool { return self.World().Get(addr) != nil } -func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { +func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { // Check if an address is stored by this address var hash []byte addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() @@ -108,10 +108,10 @@ func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, pr return self.Transact(key, hash, value, gas, price, data) } -func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { +func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { var hash []byte var contractCreation bool - if chain.IsContractAddr(to) { + if types.IsContractAddr(to) { contractCreation = true } else { // Check if an address is stored by this address @@ -125,9 +125,9 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et var tx *types.Transaction if contractCreation { - tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) + tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) } else { - tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) + tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } state := self.blockManager.TransState()