core: convert transaction pool to common.{Address,Hash}

This commit is contained in:
Felix Lange 2015-03-17 11:59:26 +01:00
parent e91ab84dbe
commit b95387a0dc

View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
) )
@ -20,9 +20,7 @@ var (
const txPoolQueueSize = 50 const txPoolQueueSize = 50
type TxPoolHook chan *types.Transaction type TxPoolHook chan *types.Transaction
type TxMsg struct { type TxMsg struct{ Tx *types.Transaction }
Tx *types.Transaction
}
const ( const (
minGasPrice = 1000000 minGasPrice = 1000000
@ -44,7 +42,7 @@ type TxPool struct {
quit chan bool quit chan bool
// The actual pool // The actual pool
//pool *list.List //pool *list.List
txs map[string]*types.Transaction txs map[common.Hash]*types.Transaction
SecondaryProcessor TxProcessor SecondaryProcessor TxProcessor
@ -55,7 +53,7 @@ type TxPool struct {
func NewTxPool(eventMux *event.TypeMux) *TxPool { func NewTxPool(eventMux *event.TypeMux) *TxPool {
return &TxPool{ return &TxPool{
txs: make(map[string]*types.Transaction), txs: make(map[common.Hash]*types.Transaction),
queueChan: make(chan *types.Transaction, txPoolQueueSize), queueChan: make(chan *types.Transaction, txPoolQueueSize),
quit: make(chan bool), quit: make(chan bool),
eventMux: eventMux, eventMux: eventMux,
@ -63,21 +61,16 @@ func NewTxPool(eventMux *event.TypeMux) *TxPool {
} }
func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error { func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
if len(tx.To()) != 0 && len(tx.To()) != 20 { // Validate sender
return fmt.Errorf("Invalid recipient. len = %d", len(tx.To())) if _, err := tx.From(); err != nil {
return err
} }
// Validate curve param // Validate curve param
v, _, _ := tx.Curve() v, _, _ := tx.Curve()
if v > 28 || v < 27 { if v > 28 || v < 27 {
return fmt.Errorf("tx.v != (28 || 27) => %v", v) return fmt.Errorf("tx.v != (28 || 27) => %v", v)
} }
return nil
// Validate sender address
senderAddr := tx.From()
if senderAddr == nil || len(senderAddr) != 20 {
return ErrInvalidSender
}
/* XXX this kind of validation needs to happen elsewhere in the gui when sending txs. /* XXX this kind of validation needs to happen elsewhere in the gui when sending txs.
Other clients should do their own validation. Value transfer could throw error Other clients should do their own validation. Value transfer could throw error
@ -91,19 +84,16 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From()) return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From())
} }
*/ */
return nil
} }
func (self *TxPool) addTx(tx *types.Transaction) { func (self *TxPool) addTx(tx *types.Transaction) {
self.txs[string(tx.Hash())] = tx self.txs[tx.Hash()] = tx
} }
func (self *TxPool) add(tx *types.Transaction) error { func (self *TxPool) add(tx *types.Transaction) error {
if self.txs[string(tx.Hash())] != nil { if self.txs[tx.Hash()] != nil {
return fmt.Errorf("Known transaction (%x)", tx.Hash()[0:4]) return fmt.Errorf("Known transaction (%x)", tx.Hash()[0:4])
} }
err := self.ValidateTransaction(tx) err := self.ValidateTransaction(tx)
if err != nil { if err != nil {
return err return err
@ -111,18 +101,16 @@ func (self *TxPool) add(tx *types.Transaction) error {
self.addTx(tx) self.addTx(tx)
var to string var toname string
if len(tx.To()) > 0 { if to, valid := tx.To(); valid {
to = common.Bytes2Hex(tx.To()[:4]) toname = common.Bytes2Hex(to[:4])
} else { } else {
to = "[NEW_CONTRACT]" toname = "[NEW_CONTRACT]"
}
var from string
if len(tx.From()) > 0 {
from = common.Bytes2Hex(tx.From()[:4])
} else {
return errors.New(fmt.Sprintf("FROM ADDRESS MUST BE POSITIVE (was %v)", tx.From()))
} }
// we can ignore the error here because From is
// verified in ValidateTransaction.
f, _ := tx.From()
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, to, tx.Value, tx.Hash())
// Notify the subscribers // Notify the subscribers
@ -140,6 +128,7 @@ func (self *TxPool) Add(tx *types.Transaction) error {
defer self.mu.Unlock() defer self.mu.Unlock()
return self.add(tx) return self.add(tx)
} }
func (self *TxPool) AddTransactions(txs []*types.Transaction) { func (self *TxPool) AddTransactions(txs []*types.Transaction) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
@ -186,14 +175,13 @@ func (pool *TxPool) RemoveInvalid(query StateQuery) {
func (self *TxPool) RemoveSet(txs types.Transactions) { func (self *TxPool) RemoveSet(txs types.Transactions) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
for _, tx := range txs { for _, tx := range txs {
delete(self.txs, string(tx.Hash())) delete(self.txs, tx.Hash())
} }
} }
func (pool *TxPool) Flush() { func (pool *TxPool) Flush() {
pool.txs = make(map[string]*types.Transaction) pool.txs = make(map[common.Hash]*types.Transaction)
} }
func (pool *TxPool) Start() { func (pool *TxPool) Start() {