core: actually convert transaction pool

This commit is contained in:
Felix Lange 2015-03-17 12:16:21 +01:00
parent ee7202fa00
commit 17c5ba2b6b
3 changed files with 19 additions and 17 deletions

View File

@ -5,7 +5,6 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/vm" "github.com/ethereum/go-ethereum/vm"
) )
@ -57,13 +56,8 @@ type Message interface {
Data() []byte Data() []byte
} }
func AddressFromMessage(msg Message) []byte {
// Generate a new address
return crypto.Sha3(common.NewValue([]interface{}{msg.From(), msg.Nonce()}).Encode())[12:]
}
func MessageCreatesContract(msg Message) bool { func MessageCreatesContract(msg Message) bool {
return len(msg.To()) == 0 return msg.To() == nil
} }
func MessageGasValue(msg Message) *big.Int { func MessageGasValue(msg Message) *big.Int {
@ -93,13 +87,18 @@ func (self *StateTransition) Coinbase() *state.StateObject {
return self.state.GetOrNewStateObject(self.coinbase) return self.state.GetOrNewStateObject(self.coinbase)
} }
func (self *StateTransition) From() *state.StateObject { func (self *StateTransition) From() *state.StateObject {
return self.state.GetOrNewStateObject(self.msg.From()) f, _ := self.msg.From()
return self.state.GetOrNewStateObject(f)
} }
func (self *StateTransition) To() *state.StateObject { func (self *StateTransition) To() *state.StateObject {
if self.msg != nil && MessageCreatesContract(self.msg) { if self.msg == nil {
return nil return nil
} }
return self.state.GetOrNewStateObject(self.msg.To()) to := self.msg.To()
if to == nil {
return nil // contract creation
}
return self.state.GetOrNewStateObject(*to)
} }
func (self *StateTransition) UseGas(amount *big.Int) error { func (self *StateTransition) UseGas(amount *big.Int) error {

View File

@ -91,8 +91,9 @@ func (self *TxPool) addTx(tx *types.Transaction) {
} }
func (self *TxPool) add(tx *types.Transaction) error { func (self *TxPool) add(tx *types.Transaction) error {
if self.txs[tx.Hash()] != nil { hash := tx.Hash()
return fmt.Errorf("Known transaction (%x)", tx.Hash()[0:4]) if self.txs[hash] != nil {
return fmt.Errorf("Known transaction (%x)", hash[0:4])
} }
err := self.ValidateTransaction(tx) err := self.ValidateTransaction(tx)
if err != nil { if err != nil {
@ -102,7 +103,7 @@ func (self *TxPool) add(tx *types.Transaction) error {
self.addTx(tx) self.addTx(tx)
var toname string var toname string
if to, valid := tx.To(); valid { if to := tx.To(); to != nil {
toname = common.Bytes2Hex(to[:4]) toname = common.Bytes2Hex(to[:4])
} else { } else {
toname = "[NEW_CONTRACT]" toname = "[NEW_CONTRACT]"
@ -111,7 +112,7 @@ func (self *TxPool) add(tx *types.Transaction) error {
// verified in ValidateTransaction. // verified in ValidateTransaction.
f, _ := tx.From() f, _ := tx.From()
from := common.Bytes2Hex(f[:4]) from := common.Bytes2Hex(f[:4])
txplogger.Debugf("(t) %x => %s (%v) %x\n", from, to, tx.Value, tx.Hash()) txplogger.Debugf("(t) %x => %s (%v) %x\n", from, toname, tx.Value, tx.Hash())
// Notify the subscribers // Notify the subscribers
go self.eventMux.Post(TxPreEvent{tx}) go self.eventMux.Post(TxPreEvent{tx})
@ -137,7 +138,8 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) {
if err := self.add(tx); err != nil { if err := self.add(tx); err != nil {
txplogger.Debugln(err) txplogger.Debugln(err)
} else { } else {
txplogger.Debugf("tx %x\n", tx.Hash()[0:4]) h := tx.Hash()
txplogger.Debugf("tx %x\n", h[:4])
} }
} }
} }
@ -161,7 +163,8 @@ func (pool *TxPool) RemoveInvalid(query StateQuery) {
var removedTxs types.Transactions var removedTxs types.Transactions
for _, tx := range pool.txs { for _, tx := range pool.txs {
sender := query.GetAccount(tx.From()) from, _ := tx.From()
sender := query.GetAccount(from[:])
err := pool.ValidateTransaction(tx) err := pool.ValidateTransaction(tx)
if err != nil || sender.Nonce() >= tx.Nonce() { if err != nil || sender.Nonce() >= tx.Nonce() {
removedTxs = append(removedTxs, tx) removedTxs = append(removedTxs, tx)

View File

@ -28,7 +28,7 @@ func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types
} }
} }
func (self *VMEnv) Origin() common.Address { return self.msg.From() } func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() }
func (self *VMEnv) Coinbase() common.Address { return self.block.Coinbase() } func (self *VMEnv) Coinbase() common.Address { return self.block.Coinbase() }
func (self *VMEnv) Time() int64 { return self.block.Time() } func (self *VMEnv) Time() int64 { return self.block.Time() }