aboutsummaryrefslogtreecommitdiffstats
path: root/eth
diff options
context:
space:
mode:
Diffstat (limited to 'eth')
-rw-r--r--eth/api_backend.go14
-rw-r--r--eth/bind.go92
-rw-r--r--eth/handler.go5
-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
7 files changed, 80 insertions, 68 deletions
diff --git a/eth/api_backend.go b/eth/api_backend.go
index efcdb3361..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]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) {
+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/bind.go b/eth/bind.go
index bf7a7fb53..532e94460 100644
--- a/eth/bind.go
+++ b/eth/bind.go
@@ -19,6 +19,7 @@ package eth
import (
"math/big"
+ "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
@@ -50,47 +51,62 @@ func NewContractBackend(eth *Ethereum) *ContractBackend {
}
}
-// HasCode implements bind.ContractVerifier.HasCode by retrieving any code associated
-// with the contract from the local API, and checking its size.
-func (b *ContractBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) {
- if ctx == nil {
- ctx = context.Background()
- }
- block := rpc.LatestBlockNumber
- if pending {
- block = rpc.PendingBlockNumber
- }
- out, err := b.bcapi.GetCode(ctx, contract, block)
- return len(common.FromHex(out)) > 0, err
+// CodeAt retrieves any code associated with the contract from the local API.
+func (b *ContractBackend) CodeAt(ctx context.Context, contract common.Address, blockNum *big.Int) ([]byte, error) {
+ out, err := b.bcapi.GetCode(ctx, contract, toBlockNumber(blockNum))
+ return common.FromHex(out), err
+}
+
+// CodeAt retrieves any code associated with the contract from the local API.
+func (b *ContractBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
+ out, err := b.bcapi.GetCode(ctx, contract, rpc.PendingBlockNumber)
+ return common.FromHex(out), err
}
// ContractCall implements bind.ContractCaller executing an Ethereum contract
// call with the specified data as the input. The pending flag requests execution
// against the pending block, not the stable head of the chain.
-func (b *ContractBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) {
- if ctx == nil {
- ctx = context.Background()
- }
- // Convert the input args to the API spec
+func (b *ContractBackend) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNum *big.Int) ([]byte, error) {
+ out, err := b.bcapi.Call(ctx, toCallArgs(msg), toBlockNumber(blockNum))
+ return common.FromHex(out), err
+}
+
+// ContractCall implements bind.ContractCaller executing an Ethereum contract
+// call with the specified data as the input. The pending flag requests execution
+// against the pending block, not the stable head of the chain.
+func (b *ContractBackend) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
+ out, err := b.bcapi.Call(ctx, toCallArgs(msg), rpc.PendingBlockNumber)
+ return common.FromHex(out), err
+}
+
+func toCallArgs(msg ethereum.CallMsg) ethapi.CallArgs {
args := ethapi.CallArgs{
- To: &contract,
- Data: common.ToHex(data),
+ To: msg.To,
+ From: msg.From,
+ Data: common.ToHex(msg.Data),
}
- block := rpc.LatestBlockNumber
- if pending {
- block = rpc.PendingBlockNumber
+ if msg.Gas != nil {
+ args.Gas = *rpc.NewHexNumber(msg.Gas)
}
- // Execute the call and convert the output back to Go types
- out, err := b.bcapi.Call(ctx, args, block)
- return common.FromHex(out), err
+ if msg.GasPrice != nil {
+ args.GasPrice = *rpc.NewHexNumber(msg.GasPrice)
+ }
+ if msg.Value != nil {
+ args.Value = *rpc.NewHexNumber(msg.Value)
+ }
+ return args
+}
+
+func toBlockNumber(num *big.Int) rpc.BlockNumber {
+ if num == nil {
+ return rpc.LatestBlockNumber
+ }
+ return rpc.BlockNumber(num.Int64())
}
// PendingAccountNonce implements bind.ContractTransactor retrieving the current
// pending nonce associated with an account.
-func (b *ContractBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) {
- if ctx == nil {
- ctx = context.Background()
- }
+func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber)
return out.Uint64(), err
}
@@ -98,9 +114,6 @@ func (b *ContractBackend) PendingAccountNonce(ctx context.Context, account commo
// SuggestGasPrice implements bind.ContractTransactor retrieving the currently
// suggested gas price to allow a timely execution of a transaction.
func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
- if ctx == nil {
- ctx = context.Background()
- }
return b.eapi.GasPrice(ctx)
}
@@ -109,25 +122,14 @@ func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error)
// the backend blockchain. There is no guarantee that this is the true gas limit
// requirement as other transactions may be added or removed by miners, but it
// should provide a basis for setting a reasonable default.
-func (b *ContractBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) {
- if ctx == nil {
- ctx = context.Background()
- }
- out, err := b.bcapi.EstimateGas(ctx, ethapi.CallArgs{
- From: sender,
- To: contract,
- Value: *rpc.NewHexNumber(value),
- Data: common.ToHex(data),
- })
+func (b *ContractBackend) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) {
+ out, err := b.bcapi.EstimateGas(ctx, toCallArgs(msg))
return out.BigInt(), err
}
// SendTransaction implements bind.ContractTransactor injects the transaction
// into the pending pool for execution.
func (b *ContractBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
- if ctx == nil {
- ctx = context.Background()
- }
raw, _ := rlp.EncodeToBytes(tx)
_, err := b.txapi.SendRawTransaction(ctx, common.ToHex(raw))
return err
diff --git a/eth/handler.go b/eth/handler.go
index 886d89fd1..e6c547c02 100644
--- a/eth/handler.go
+++ b/eth/handler.go
@@ -634,9 +634,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if err := msg.Decode(&request); err != nil {
return errResp(ErrDecode, "%v: %v", msg, err)
}
- if err := request.Block.ValidateFields(); err != nil {
- return errResp(ErrDecode, "block validation %v: %v", msg, err)
- }
request.Block.ReceivedAt = msg.ReceivedAt
request.Block.ReceivedFrom = p
@@ -680,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
}