aboutsummaryrefslogtreecommitdiffstats
path: root/eth
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-07-01 23:59:55 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-09-02 19:12:03 +0800
commit0ef327bbee79c01a69ba59258acc6ce3a48bc288 (patch)
tree1d43179977d96c5ca7de85e0727cfa69dbb230ed /eth
parent795b70423eac7180ab85b735f64aae9d6a10449d (diff)
downloadgo-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar.gz
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar.bz2
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar.lz
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar.xz
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.tar.zst
go-tangerine-0ef327bbee79c01a69ba59258acc6ce3a48bc288.zip
core, eth, internal, miner: optimize txpool for quick ops
Diffstat (limited to 'eth')
-rw-r--r--eth/api_backend.go14
-rw-r--r--eth/handler.go2
-rw-r--r--eth/helper_test.go22
-rw-r--r--eth/protocol.go8
-rw-r--r--eth/protocol_test.go2
-rw-r--r--eth/sync.go5
6 files changed, 33 insertions, 20 deletions
diff --git a/eth/api_backend.go b/eth/api_backend.go
index e19254374..4f8f06529 100644
--- a/eth/api_backend.go
+++ b/eth/api_backend.go
@@ -118,21 +118,25 @@ func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
b.eth.txMu.Lock()
defer b.eth.txMu.Unlock()
- b.eth.txPool.RemoveTx(txHash)
+ b.eth.txPool.Remove(txHash)
}
func (b *EthApiBackend) GetPoolTransactions() types.Transactions {
b.eth.txMu.Lock()
defer b.eth.txMu.Unlock()
- return b.eth.txPool.GetTransactions()
+ var txs types.Transactions
+ for _, batch := range b.eth.txPool.Pending() {
+ txs = append(txs, batch...)
+ }
+ return txs
}
-func (b *EthApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
+func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
b.eth.txMu.Lock()
defer b.eth.txMu.Unlock()
- return b.eth.txPool.GetTransaction(txHash)
+ return b.eth.txPool.Get(hash)
}
func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
@@ -149,7 +153,7 @@ func (b *EthApiBackend) Stats() (pending int, queued int) {
return b.eth.txPool.Stats()
}
-func (b *EthApiBackend) TxPoolContent() (map[common.Address]core.TxList, map[common.Address]core.TxList) {
+func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
b.eth.txMu.Lock()
defer b.eth.txMu.Unlock()
diff --git a/eth/handler.go b/eth/handler.go
index 570c79dac..e6c547c02 100644
--- a/eth/handler.go
+++ b/eth/handler.go
@@ -677,7 +677,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
}
p.MarkTransaction(tx.Hash())
}
- pm.txpool.AddTransactions(txs)
+ pm.txpool.AddBatch(txs)
default:
return errResp(ErrInvalidMsgCode, "%v", msg.Code)
diff --git a/eth/helper_test.go b/eth/helper_test.go
index 28ff69b17..732fe89ee 100644
--- a/eth/helper_test.go
+++ b/eth/helper_test.go
@@ -23,6 +23,7 @@ import (
"crypto/ecdsa"
"crypto/rand"
"math/big"
+ "sort"
"sync"
"testing"
@@ -89,9 +90,9 @@ type testTxPool struct {
lock sync.RWMutex // Protects the transaction pool
}
-// AddTransactions appends a batch of transactions to the pool, and notifies any
+// AddBatch appends a batch of transactions to the pool, and notifies any
// listeners if the addition channel is non nil
-func (p *testTxPool) AddTransactions(txs []*types.Transaction) {
+func (p *testTxPool) AddBatch(txs []*types.Transaction) {
p.lock.Lock()
defer p.lock.Unlock()
@@ -101,15 +102,20 @@ func (p *testTxPool) AddTransactions(txs []*types.Transaction) {
}
}
-// GetTransactions returns all the transactions known to the pool
-func (p *testTxPool) GetTransactions() types.Transactions {
+// Pending returns all the transactions known to the pool
+func (p *testTxPool) Pending() map[common.Address]types.Transactions {
p.lock.RLock()
defer p.lock.RUnlock()
- txs := make([]*types.Transaction, len(p.pool))
- copy(txs, p.pool)
-
- return txs
+ batches := make(map[common.Address]types.Transactions)
+ for _, tx := range p.pool {
+ from, _ := tx.From()
+ batches[from] = append(batches[from], tx)
+ }
+ for _, batch := range batches {
+ sort.Sort(types.TxByNonce(batch))
+ }
+ return batches
}
// newTestTransaction create a new dummy transaction.
diff --git a/eth/protocol.go b/eth/protocol.go
index 69b3be578..3f65c204b 100644
--- a/eth/protocol.go
+++ b/eth/protocol.go
@@ -97,12 +97,12 @@ var errorToString = map[int]string{
}
type txPool interface {
- // AddTransactions should add the given transactions to the pool.
- AddTransactions([]*types.Transaction)
+ // AddBatch should add the given transactions to the pool.
+ AddBatch([]*types.Transaction)
- // GetTransactions should return pending transactions.
+ // Pending should return pending transactions.
// The slice should be modifiable by the caller.
- GetTransactions() types.Transactions
+ Pending() map[common.Address]types.Transactions
}
// statusData is the network packet for the status message.
diff --git a/eth/protocol_test.go b/eth/protocol_test.go
index 4633344da..0aac19f43 100644
--- a/eth/protocol_test.go
+++ b/eth/protocol_test.go
@@ -130,7 +130,7 @@ func testSendTransactions(t *testing.T, protocol int) {
for nonce := range alltxs {
alltxs[nonce] = newTestTransaction(testAccount, uint64(nonce), txsize)
}
- pm.txpool.AddTransactions(alltxs)
+ pm.txpool.AddBatch(alltxs)
// Connect several peers. They should all receive the pending transactions.
var wg sync.WaitGroup
diff --git a/eth/sync.go b/eth/sync.go
index e1946edda..6584bb1e2 100644
--- a/eth/sync.go
+++ b/eth/sync.go
@@ -45,7 +45,10 @@ type txsync struct {
// syncTransactions starts sending all currently pending transactions to the given peer.
func (pm *ProtocolManager) syncTransactions(p *peer) {
- txs := pm.txpool.GetTransactions()
+ var txs types.Transactions
+ for _, batch := range pm.txpool.Pending() {
+ txs = append(txs, batch...)
+ }
if len(txs) == 0 {
return
}