all: run RPC APIs on top of the stable Go API
In this commit, the RPC API is adapted to run on top of the new Go API. This required lots of changes to many packages, but has a few side benefits: - Ethereum and LightEthereum can now be used as a contract backend. - Some duplicated code could be removed (there is added duplication in other places though) - It is now much easier to see which operations are unsupported with the light client. Package les previously relied on the full node RPC API backend, which masked several issues because the RPC API performed direct access to the chain database. Changes to packages in detail: accounts/abi/bind: - Contract call boilerplate has moved to package core. cmd/utils: - les now inherits the event.TypeMux from the Node instance contracts/release: - The ReleaseService now uses Ethereum and LightEthereum as backend. core: - MissingNumber is exposed so it can be used in package eth. - GetTransaction now returns the index as an int, for convenience reasons. - ApplyCallMessage has been added as the new one and only implementation of read-only contract calls. - TxPool exposes NonceAt instead of the more general accessor for the ManagedState. core/types: - Signer.SignECDSA is gone (it was basically unused). - WithSignature doesn't return an error anymore (all implementations panic for invalid length). I made this change to avoid code duplication in the API. eth: - EthApiBackend is gone. In its place, Ethereum gains many new methods which implement a large portion of the new Go API. It does not yet support event subscriptions and log filtering. - Some accessors for internal objects are gone. - ethapi.PrivateDebugAPI and ethapi.TxPoolDebugAPI are now created in package eth for dependency reasons. eth/downloader: - Progress returns a pointer to simplify callers. eth/filters: - The Backend interface is simpler and uses the stable Go API where possible. The new BlockReceipts method saves one DB read because BlockReceipts also takes a block number argument. - ChainDB is no longer passed via the Backend interface. - EventSystem now relies on HeaderByHash for reorgs in light client mode instead of reading from the chain database. eth/gasprice: - The LightPriceOracle now uses ethereum.ChainReader instead of ethapi.Backend. ethclient: - TransactionByHash is adapted for the last-minute API change which adds the isPending return value. internal/ethapi: - PublicTxPoolAPI is now called TxPoolDebugAPI, moved to its own file and talks to the transaction pool instead of using the main Backend. - The API no longer accesses the chain database directly. All access is mediated through Backend methods. - The backend is now split into three interfaces. Implementing Backend is mandatory but does not require the pending state. The other two (PendingState, TransactionInclusionBlock) are optional and discovered at runtime. les: - LesApiBackend is gone, LightEthereum gets all the methods. - Weird accessors copied from package eth are now gone as well. light: - TxPool.Stats now returns a queued count of zero. It implements the ethapi.TxPool interface and can be used with TxPoolDebugAPI.
This commit is contained in:
parent
6ca8f57b08
commit
d258e4cf2a
|
@ -56,7 +56,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tx.WithSignature(signer, signature)
|
||||
return tx.WithSignature(signer, signature), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,7 +167,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rval, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state)
|
||||
rval, _, err := b.callContract(call, b.blockchain.CurrentBlock(), state)
|
||||
return rval, err
|
||||
}
|
||||
|
||||
|
@ -177,7 +177,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu
|
|||
defer b.mu.Unlock()
|
||||
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
||||
|
||||
rval, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
||||
rval, _, err := b.callContract(call, b.pendingBlock, b.pendingState)
|
||||
return rval, err
|
||||
}
|
||||
|
||||
|
@ -203,32 +203,16 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
|
|||
defer b.mu.Unlock()
|
||||
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
|
||||
|
||||
_, gas, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
|
||||
_, gas, err := b.callContract(call, b.pendingBlock, b.pendingState)
|
||||
return gas, err
|
||||
}
|
||||
|
||||
// callContract implemens common code between normal and pending contract calls.
|
||||
// state is modified during execution, make sure to copy it if necessary.
|
||||
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, *big.Int, error) {
|
||||
// Ensure message is initialized properly.
|
||||
if call.GasPrice == nil {
|
||||
call.GasPrice = big.NewInt(1)
|
||||
}
|
||||
if call.Gas == nil || call.Gas.BitLen() == 0 {
|
||||
call.Gas = big.NewInt(50000000)
|
||||
}
|
||||
if call.Value == nil {
|
||||
call.Value = new(big.Int)
|
||||
}
|
||||
// Set infinite balance to the fake caller account.
|
||||
from := statedb.GetOrNewStateObject(call.From)
|
||||
from.SetBalance(common.MaxBig)
|
||||
// Execute the call.
|
||||
msg := callmsg{call}
|
||||
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
||||
gaspool := new(core.GasPool).AddGas(common.MaxBig)
|
||||
ret, gasUsed, _, err := core.NewStateTransition(vmenv, msg, gaspool).TransitionDb()
|
||||
return ret, gasUsed, err
|
||||
func (b *SimulatedBackend) callContract(call ethereum.CallMsg, block *types.Block, state *state.StateDB) ([]byte, *big.Int, error) {
|
||||
return core.ApplyCallMessage(call, func(msg core.Message) vm.Environment {
|
||||
return core.NewEnv(state, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
|
||||
})
|
||||
}
|
||||
|
||||
// SendTransaction updates the pending block to include the given transaction.
|
||||
|
@ -256,17 +240,3 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
|
|||
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
|
||||
return nil
|
||||
}
|
||||
|
||||
// callmsg implements core.Message to allow passing it as a transaction simulator.
|
||||
type callmsg struct {
|
||||
ethereum.CallMsg
|
||||
}
|
||||
|
||||
func (m callmsg) From() common.Address { return m.CallMsg.From }
|
||||
func (m callmsg) Nonce() uint64 { return 0 }
|
||||
func (m callmsg) CheckNonce() bool { return false }
|
||||
func (m callmsg) To() *common.Address { return m.CallMsg.To }
|
||||
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
|
||||
func (m callmsg) Gas() *big.Int { return m.CallMsg.Gas }
|
||||
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
|
||||
func (m callmsg) Data() []byte { return m.CallMsg.Data }
|
||||
|
|
|
@ -776,7 +776,7 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) {
|
|||
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
||||
fullNode, err := eth.New(ctx, ethConf)
|
||||
if fullNode != nil && ethConf.LightServ > 0 {
|
||||
ls, _ := les.NewLesServer(fullNode, ethConf)
|
||||
ls, _ := les.NewLesServer(fullNode, stack.EventMux(), ethConf)
|
||||
fullNode.AddLesServer(ls)
|
||||
}
|
||||
return fullNode, err
|
||||
|
|
|
@ -27,7 +27,6 @@ import (
|
|||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/les"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
|
@ -62,20 +61,20 @@ type ReleaseService struct {
|
|||
// releases and notify the user of such.
|
||||
func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) {
|
||||
// Retrieve the Ethereum service dependency to access the blockchain
|
||||
var apiBackend ethapi.Backend
|
||||
var apiBackend bind.ContractBackend
|
||||
var ethereum *eth.Ethereum
|
||||
if err := ctx.Service(ðereum); err == nil {
|
||||
apiBackend = ethereum.ApiBackend
|
||||
apiBackend = ethereum
|
||||
} else {
|
||||
var ethereum *les.LightEthereum
|
||||
if err := ctx.Service(ðereum); err == nil {
|
||||
apiBackend = ethereum.ApiBackend
|
||||
apiBackend = ethereum
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// Construct the release service
|
||||
contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(apiBackend))
|
||||
contract, err := NewReleaseOracle(config.Oracle, apiBackend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ func GetCanonicalHash(db ethdb.Database, number uint64) common.Hash {
|
|||
|
||||
// missingNumber is returned by GetBlockNumber if no header with the
|
||||
// given block hash has been stored in the database
|
||||
const missingNumber = uint64(0xffffffffffffffff)
|
||||
const MissingNumber = uint64(0xffffffffffffffff)
|
||||
|
||||
// GetBlockNumber returns the block number assigned to a block hash
|
||||
// if the corresponding header is present in the database
|
||||
|
@ -95,7 +95,7 @@ func GetBlockNumber(db ethdb.Database, hash common.Hash) uint64 {
|
|||
if len(data) != 8 {
|
||||
data, _ := db.Get(append(append(oldBlockPrefix, hash.Bytes()...), oldHeaderSuffix...))
|
||||
if len(data) == 0 {
|
||||
return missingNumber
|
||||
return MissingNumber
|
||||
}
|
||||
header := new(types.Header)
|
||||
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
|
||||
|
@ -251,7 +251,7 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.
|
|||
|
||||
// GetTransaction retrieves a specific transaction from the database, along with
|
||||
// its added positional metadata.
|
||||
func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
|
||||
func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, int) {
|
||||
// Retrieve the transaction itself from the database
|
||||
data, _ := db.Get(hash.Bytes())
|
||||
if len(data) == 0 {
|
||||
|
@ -274,7 +274,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co
|
|||
if err := rlp.DecodeBytes(data, &meta); err != nil {
|
||||
return nil, common.Hash{}, 0, 0
|
||||
}
|
||||
return &tx, meta.BlockHash, meta.BlockIndex, meta.Index
|
||||
return &tx, meta.BlockHash, meta.BlockIndex, int(meta.Index)
|
||||
}
|
||||
|
||||
// GetReceipt returns a receipt by hash
|
||||
|
|
|
@ -369,7 +369,7 @@ func TestTransactionStorage(t *testing.T) {
|
|||
if txn, hash, number, index := GetTransaction(db, tx.Hash()); txn == nil {
|
||||
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
|
||||
} else {
|
||||
if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
|
||||
if hash != block.Hash() || number != block.NumberU64() || index != i {
|
||||
t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i)
|
||||
}
|
||||
if tx.String() != txn.String() {
|
||||
|
|
|
@ -124,7 +124,7 @@ func (hc *HeaderChain) GetBlockNumber(hash common.Hash) uint64 {
|
|||
return cached.(uint64)
|
||||
}
|
||||
number := GetBlockNumber(hc.chainDb, hash)
|
||||
if number != missingNumber {
|
||||
if number != MissingNumber {
|
||||
hc.numberCache.Add(hash, number)
|
||||
}
|
||||
return number
|
||||
|
|
|
@ -1,96 +0,0 @@
|
|||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
// "github.com/ethereum/go-ethereum/crypto"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
)
|
||||
|
||||
// Implement our EthTest Manager
|
||||
type TestManager struct {
|
||||
// stateManager *StateManager
|
||||
eventMux *event.TypeMux
|
||||
|
||||
db ethdb.Database
|
||||
txPool *TxPool
|
||||
blockChain *BlockChain
|
||||
Blocks []*types.Block
|
||||
}
|
||||
|
||||
func (s *TestManager) IsListening() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *TestManager) IsMining() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *TestManager) PeerCount() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *TestManager) Peers() *list.List {
|
||||
return list.New()
|
||||
}
|
||||
|
||||
func (s *TestManager) BlockChain() *BlockChain {
|
||||
return s.blockChain
|
||||
}
|
||||
|
||||
func (tm *TestManager) TxPool() *TxPool {
|
||||
return tm.txPool
|
||||
}
|
||||
|
||||
// func (tm *TestManager) StateManager() *StateManager {
|
||||
// return tm.stateManager
|
||||
// }
|
||||
|
||||
func (tm *TestManager) EventMux() *event.TypeMux {
|
||||
return tm.eventMux
|
||||
}
|
||||
|
||||
// func (tm *TestManager) KeyManager() *crypto.KeyManager {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func (tm *TestManager) Db() ethdb.Database {
|
||||
return tm.db
|
||||
}
|
||||
|
||||
func NewTestManager() *TestManager {
|
||||
db, err := ethdb.NewMemDatabase()
|
||||
if err != nil {
|
||||
fmt.Println("Could not create mem-db, failing")
|
||||
return nil
|
||||
}
|
||||
|
||||
testManager := &TestManager{}
|
||||
testManager.eventMux = new(event.TypeMux)
|
||||
testManager.db = db
|
||||
// testManager.txPool = NewTxPool(testManager)
|
||||
// testManager.blockChain = NewBlockChain(testManager)
|
||||
// testManager.stateManager = NewStateManager(testManager)
|
||||
|
||||
return testManager
|
||||
}
|
|
@ -20,6 +20,7 @@ import (
|
|||
"fmt"
|
||||
"math/big"
|
||||
|
||||
ethereum "github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
|
@ -134,6 +135,39 @@ func ApplyMessage(env vm.Environment, msg Message, gp *GasPool) ([]byte, *big.In
|
|||
return ret, gasUsed, err
|
||||
}
|
||||
|
||||
// ApplyCallMessage runs the given message as a read-only contract call.
|
||||
//
|
||||
// It modifies the environment to provide a near infinite amount of ether to the sender
|
||||
// account and runs the message with the amount of gas configured in the message. This
|
||||
// function is meant to be used outside of regular block processing.
|
||||
//
|
||||
// It returns the bytes returned by any EVM execution (if it took place), the gas used
|
||||
// (not including gas refunds) and an error if it failed. An error always indicates a core
|
||||
// error meaning that the message would always fail for that particular state and would
|
||||
// never be accepted within a block.
|
||||
func ApplyCallMessage(call ethereum.CallMsg, newEnv func(Message) vm.Environment) ([]byte, *big.Int, error) {
|
||||
// Ensure message is initialized properly.
|
||||
if call.GasPrice == nil {
|
||||
call.GasPrice = big.NewInt(1)
|
||||
}
|
||||
if call.Gas == nil || call.Gas.BitLen() == 0 {
|
||||
call.Gas = big.NewInt(50000000)
|
||||
}
|
||||
if call.Value == nil {
|
||||
call.Value = new(big.Int)
|
||||
}
|
||||
msg := callmsg{call}
|
||||
env := newEnv(msg)
|
||||
// Provide ether to the caller account.
|
||||
have := env.Db().GetBalance(call.From)
|
||||
need := new(big.Int).Sub(common.MaxBig, have)
|
||||
env.Db().AddBalance(call.From, need)
|
||||
// Execute the call.
|
||||
gaspool := new(GasPool).AddGas(call.Gas)
|
||||
ret, gasUsed, _, err := NewStateTransition(env, msg, gaspool).TransitionDb()
|
||||
return ret, gasUsed, err
|
||||
}
|
||||
|
||||
func (self *StateTransition) from() vm.Account {
|
||||
f := self.msg.From()
|
||||
if !self.state.Exist(f) {
|
||||
|
@ -283,3 +317,17 @@ func (self *StateTransition) refundGas() {
|
|||
func (self *StateTransition) gasUsed() *big.Int {
|
||||
return new(big.Int).Sub(self.initialGas, self.gas)
|
||||
}
|
||||
|
||||
// callmsg wraps ethereum.CallMsg so it implements Message.
|
||||
type callmsg struct {
|
||||
ethereum.CallMsg
|
||||
}
|
||||
|
||||
func (m callmsg) From() common.Address { return m.CallMsg.From }
|
||||
func (m callmsg) Nonce() uint64 { return 0 }
|
||||
func (m callmsg) CheckNonce() bool { return false }
|
||||
func (m callmsg) To() *common.Address { return m.CallMsg.To }
|
||||
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
|
||||
func (m callmsg) Gas() *big.Int { return m.CallMsg.Gas }
|
||||
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
|
||||
func (m callmsg) Data() []byte { return m.CallMsg.Data }
|
||||
|
|
|
@ -195,11 +195,12 @@ func (pool *TxPool) Stop() {
|
|||
glog.V(logger.Info).Infoln("Transaction pool stopped")
|
||||
}
|
||||
|
||||
func (pool *TxPool) State() *state.ManagedState {
|
||||
pool.mu.RLock()
|
||||
defer pool.mu.RUnlock()
|
||||
// NonceAt returns the next valid nonce for the given account.
|
||||
func (pool *TxPool) NonceAt(addr common.Address) uint64 {
|
||||
pool.mu.Lock()
|
||||
defer pool.mu.Unlock()
|
||||
|
||||
return pool.pendingState
|
||||
return pool.pendingState.GetNonce(addr)
|
||||
}
|
||||
|
||||
// Stats retrieves the current pool stats, namely the number of pending and the
|
||||
|
|
|
@ -18,7 +18,6 @@ package types
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
@ -52,11 +51,10 @@ func TestBlockEncoding(t *testing.T) {
|
|||
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
|
||||
|
||||
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), big.NewInt(50000), big.NewInt(10), nil)
|
||||
|
||||
tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b11b"))
|
||||
fmt.Println(block.Transactions()[0].Hash())
|
||||
fmt.Println(tx1.data)
|
||||
fmt.Println(tx1.Hash())
|
||||
tx1 = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b11b"))
|
||||
t.Log(block.Transactions()[0].Hash())
|
||||
t.Log(tx1.data)
|
||||
t.Log(tx1.Hash())
|
||||
check("len(Transactions)", len(block.Transactions()), 1)
|
||||
check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())
|
||||
|
||||
|
|
|
@ -340,12 +340,12 @@ func (tx *Transaction) AsMessage(s Signer) (Message, error) {
|
|||
// XXX This only makes for a nice API: NewTx(...).SignECDSA(signer, prv). Should
|
||||
// we keep this?
|
||||
func (tx *Transaction) SignECDSA(signer Signer, prv *ecdsa.PrivateKey) (*Transaction, error) {
|
||||
return signer.SignECDSA(tx, prv)
|
||||
return SignECDSA(signer, tx, prv)
|
||||
}
|
||||
|
||||
// WithSignature returns a new transaction with the given signature.
|
||||
// This signature needs to be formatted as described in the yellow paper (v+27).
|
||||
func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, error) {
|
||||
func (tx *Transaction) WithSignature(signer Signer, sig []byte) *Transaction {
|
||||
return signer.WithSignature(tx, sig)
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ func SignECDSA(s Signer, tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction,
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.WithSignature(tx, sig)
|
||||
return s.WithSignature(tx, sig), nil
|
||||
}
|
||||
|
||||
// Sender derives the sender from the tx using the signer derivation
|
||||
|
@ -101,10 +101,9 @@ type Signer interface {
|
|||
Hash(tx *Transaction) common.Hash
|
||||
// PubilcKey returns the public key derived from the signature
|
||||
PublicKey(tx *Transaction) ([]byte, error)
|
||||
// SignECDSA signs the transaction with the given and returns a copy of the tx
|
||||
SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error)
|
||||
// WithSignature returns a copy of the transaction with the given signature
|
||||
WithSignature(tx *Transaction, sig []byte) (*Transaction, error)
|
||||
// WithSignature returns a copy of the transaction with the given signature.
|
||||
// It panics if the signature has the wrong size.
|
||||
WithSignature(tx *Transaction, sig []byte) *Transaction
|
||||
// Checks for equality on the signers
|
||||
Equal(Signer) bool
|
||||
}
|
||||
|
@ -129,10 +128,6 @@ func (s EIP155Signer) Equal(s2 Signer) bool {
|
|||
return ok && eip155.chainId.Cmp(s.chainId) == 0
|
||||
}
|
||||
|
||||
func (s EIP155Signer) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
|
||||
return SignECDSA(s, tx, prv)
|
||||
}
|
||||
|
||||
func (s EIP155Signer) PublicKey(tx *Transaction) ([]byte, error) {
|
||||
// if the transaction is not protected fall back to homestead signer
|
||||
if !tx.Protected() {
|
||||
|
@ -169,7 +164,7 @@ func (s EIP155Signer) PublicKey(tx *Transaction) ([]byte, error) {
|
|||
|
||||
// WithSignature returns a new transaction with the given signature.
|
||||
// This signature needs to be formatted as described in the yellow paper (v+27).
|
||||
func (s EIP155Signer) WithSignature(tx *Transaction, sig []byte) (*Transaction, error) {
|
||||
func (s EIP155Signer) WithSignature(tx *Transaction, sig []byte) *Transaction {
|
||||
if len(sig) != 65 {
|
||||
panic(fmt.Sprintf("wrong size for snature: got %d, want 65", len(sig)))
|
||||
}
|
||||
|
@ -182,7 +177,7 @@ func (s EIP155Signer) WithSignature(tx *Transaction, sig []byte) (*Transaction,
|
|||
cpy.data.V = big.NewInt(int64(sig[64] - 27 + 35))
|
||||
cpy.data.V.Add(cpy.data.V, s.chainIdMul)
|
||||
}
|
||||
return cpy, nil
|
||||
return cpy
|
||||
}
|
||||
|
||||
// Hash returns the hash to be signed by the sender.
|
||||
|
@ -199,15 +194,6 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
|
|||
})
|
||||
}
|
||||
|
||||
func (s EIP155Signer) SigECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
|
||||
h := s.Hash(tx)
|
||||
sig, err := crypto.SignEthereum(h[:], prv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.WithSignature(tx, sig)
|
||||
}
|
||||
|
||||
// HomesteadTransaction implements TransactionInterface using the
|
||||
// homestead rules.
|
||||
type HomesteadSigner struct{ FrontierSigner }
|
||||
|
@ -219,7 +205,7 @@ func (s HomesteadSigner) Equal(s2 Signer) bool {
|
|||
|
||||
// WithSignature returns a new transaction with the given snature.
|
||||
// This snature needs to be formatted as described in the yellow paper (v+27).
|
||||
func (hs HomesteadSigner) WithSignature(tx *Transaction, sig []byte) (*Transaction, error) {
|
||||
func (hs HomesteadSigner) WithSignature(tx *Transaction, sig []byte) *Transaction {
|
||||
if len(sig) != 65 {
|
||||
panic(fmt.Sprintf("wrong size for snature: got %d, want 65", len(sig)))
|
||||
}
|
||||
|
@ -227,16 +213,7 @@ func (hs HomesteadSigner) WithSignature(tx *Transaction, sig []byte) (*Transacti
|
|||
cpy.data.R = new(big.Int).SetBytes(sig[:32])
|
||||
cpy.data.S = new(big.Int).SetBytes(sig[32:64])
|
||||
cpy.data.V = new(big.Int).SetBytes([]byte{sig[64]})
|
||||
return cpy, nil
|
||||
}
|
||||
|
||||
func (hs HomesteadSigner) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
|
||||
h := hs.Hash(tx)
|
||||
sig, err := crypto.SignEthereum(h[:], prv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return hs.WithSignature(tx, sig)
|
||||
return cpy
|
||||
}
|
||||
|
||||
func (hs HomesteadSigner) PublicKey(tx *Transaction) ([]byte, error) {
|
||||
|
@ -275,7 +252,7 @@ func (s FrontierSigner) Equal(s2 Signer) bool {
|
|||
|
||||
// WithSignature returns a new transaction with the given snature.
|
||||
// This snature needs to be formatted as described in the yellow paper (v+27).
|
||||
func (fs FrontierSigner) WithSignature(tx *Transaction, sig []byte) (*Transaction, error) {
|
||||
func (fs FrontierSigner) WithSignature(tx *Transaction, sig []byte) *Transaction {
|
||||
if len(sig) != 65 {
|
||||
panic(fmt.Sprintf("wrong size for snature: got %d, want 65", len(sig)))
|
||||
}
|
||||
|
@ -283,16 +260,7 @@ func (fs FrontierSigner) WithSignature(tx *Transaction, sig []byte) (*Transactio
|
|||
cpy.data.R = new(big.Int).SetBytes(sig[:32])
|
||||
cpy.data.S = new(big.Int).SetBytes(sig[32:64])
|
||||
cpy.data.V = new(big.Int).SetBytes([]byte{sig[64]})
|
||||
return cpy, nil
|
||||
}
|
||||
|
||||
func (fs FrontierSigner) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
|
||||
h := fs.Hash(tx)
|
||||
sig, err := crypto.SignEthereum(h[:], prv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.WithSignature(tx, sig)
|
||||
return cpy
|
||||
}
|
||||
|
||||
// Hash returns the hash to be sned by the sender.
|
||||
|
|
|
@ -37,8 +37,7 @@ var (
|
|||
big.NewInt(0), big.NewInt(0), big.NewInt(0),
|
||||
nil,
|
||||
)
|
||||
|
||||
rightvrsTx, _ = NewTransaction(
|
||||
rightvrsTx = NewTransaction(
|
||||
3,
|
||||
common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
|
||||
big.NewInt(10),
|
||||
|
|
|
@ -516,7 +516,7 @@ func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, txHash common.
|
|||
return nil, fmt.Errorf("sender retrieval failed: %v", err)
|
||||
}
|
||||
// Mutate the state if we haven't reached the tracing transaction yet
|
||||
if uint64(idx) < txIndex {
|
||||
if idx < txIndex {
|
||||
vmenv := core.NewEnv(stateDb, api.config, api.eth.BlockChain(), msg, block.Header(), vm.Config{})
|
||||
_, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()))
|
||||
if err != nil {
|
||||
|
|
|
@ -17,197 +17,305 @@
|
|||
package eth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
ethereum "github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
"github.com/ethereum/go-ethereum/eth/gasprice"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
rpc "github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// EthApiBackend implements ethapi.Backend for full nodes
|
||||
type EthApiBackend struct {
|
||||
eth *Ethereum
|
||||
gpo *gasprice.GasPriceOracle
|
||||
}
|
||||
// Assert at compile time that this implementation has all optional API features.
|
||||
var (
|
||||
_ ethapi.TransactionInclusionBlock = (*Ethereum)(nil)
|
||||
_ ethapi.PendingState = (*Ethereum)(nil)
|
||||
_ bind.ContractBackend = (*Ethereum)(nil)
|
||||
_ bind.PendingContractCaller = (*Ethereum)(nil)
|
||||
)
|
||||
|
||||
func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
|
||||
return b.eth.chainConfig
|
||||
}
|
||||
var (
|
||||
ErrBlockNotFound = errors.New("block not found")
|
||||
ErrTxNotFound = errors.New("transaction not found")
|
||||
)
|
||||
|
||||
func (b *EthApiBackend) CurrentBlock() *types.Block {
|
||||
return b.eth.blockchain.CurrentBlock()
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) SetHead(number uint64) {
|
||||
b.eth.blockchain.SetHead(number)
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
|
||||
// Pending block is only known by the miner
|
||||
if blockNr == rpc.PendingBlockNumber {
|
||||
block, _ := b.eth.miner.Pending()
|
||||
return block.Header(), nil
|
||||
// HeaderByNumber returns headers from the canonical chain.
|
||||
func (eth *Ethereum) HeaderByNumber(ctx context.Context, num *big.Int) (*types.Header, error) {
|
||||
if num == nil {
|
||||
return eth.blockchain.CurrentBlock().Header(), nil
|
||||
}
|
||||
// Otherwise resolve and return the block
|
||||
if blockNr == rpc.LatestBlockNumber {
|
||||
return b.eth.blockchain.CurrentBlock().Header(), nil
|
||||
if h := eth.blockchain.GetHeaderByNumber(num.Uint64()); h != nil {
|
||||
return h, nil
|
||||
}
|
||||
return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
|
||||
// Pending block is only known by the miner
|
||||
if blockNr == rpc.PendingBlockNumber {
|
||||
block, _ := b.eth.miner.Pending()
|
||||
return block, nil
|
||||
// HeaderByHash returns the header with the given hash.
|
||||
func (eth *Ethereum) HeaderByHash(ctx context.Context, blockhash common.Hash) (*types.Header, error) {
|
||||
if h := eth.blockchain.GetHeaderByHash(blockhash); h != nil {
|
||||
return h, nil
|
||||
}
|
||||
// Otherwise resolve and return the block
|
||||
if blockNr == rpc.LatestBlockNumber {
|
||||
return b.eth.blockchain.CurrentBlock(), nil
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
|
||||
// BlockByNumber returns blocks from the canonical chain.
|
||||
func (eth *Ethereum) BlockByNumber(ctx context.Context, num *big.Int) (*types.Block, error) {
|
||||
if num == nil {
|
||||
return eth.blockchain.CurrentBlock(), nil
|
||||
}
|
||||
return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
|
||||
// Pending state is only known by the miner
|
||||
if blockNr == rpc.PendingBlockNumber {
|
||||
block, state := b.eth.miner.Pending()
|
||||
return EthApiState{state}, block.Header(), nil
|
||||
if b := eth.blockchain.GetBlockByNumber(num.Uint64()); b != nil {
|
||||
return b, nil
|
||||
}
|
||||
// Otherwise resolve the block number and return its state
|
||||
header, err := b.HeaderByNumber(ctx, blockNr)
|
||||
if header == nil || err != nil {
|
||||
return nil, nil, err
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
|
||||
// BlockByHash returns the block with the given hash.
|
||||
func (eth *Ethereum) BlockByHash(ctx context.Context, blockhash common.Hash) (*types.Block, error) {
|
||||
if b := eth.blockchain.GetBlockByHash(blockhash); b != nil {
|
||||
return b, nil
|
||||
}
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||
return EthApiState{stateDb}, header, err
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
|
||||
return b.eth.blockchain.GetBlockByHash(blockHash), nil
|
||||
// BlockReceipts returns all receipts contained in the given block.
|
||||
func (eth *Ethereum) BlockReceipts(ctx context.Context, blockhash common.Hash, number uint64) ([]*types.Receipt, error) {
|
||||
r := core.GetBlockReceipts(eth.chainDb, blockhash, number)
|
||||
if r == nil {
|
||||
return nil, errors.New("database has no valid receipts for the given block")
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
|
||||
return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
|
||||
// TransactionCount returns the number of transactions in a block.
|
||||
func (eth *Ethereum) TransactionCount(ctx context.Context, blockhash common.Hash) (uint, error) {
|
||||
b := eth.blockchain.GetBlockByHash(blockhash)
|
||||
if b == nil {
|
||||
return 0, nil
|
||||
}
|
||||
return uint(len(b.Transactions())), nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
|
||||
return b.eth.blockchain.GetTdByHash(blockHash)
|
||||
// TransactionInBlock returns the i'th transaction in the given block.
|
||||
func (eth *Ethereum) TransactionInBlock(ctx context.Context, blockhash common.Hash, i uint) (*types.Transaction, error) {
|
||||
b := eth.blockchain.GetBlockByHash(blockhash)
|
||||
if b == nil {
|
||||
return nil, fmt.Errorf("transaction index %d out of range for non-existent block", i)
|
||||
}
|
||||
if i >= uint(len(b.Transactions())) {
|
||||
return nil, fmt.Errorf("transaction index %d out of range", i)
|
||||
}
|
||||
return b.Transactions()[i], nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (vm.Environment, func() error, error) {
|
||||
statedb := state.(EthApiState).state
|
||||
from := statedb.GetOrNewStateObject(msg.From())
|
||||
from.SetBalance(common.MaxBig)
|
||||
vmError := func() error { return nil }
|
||||
return core.NewEnv(statedb, b.eth.chainConfig, b.eth.blockchain, msg, header, vm.Config{}), vmError, nil
|
||||
// TransactionByHash returns the transaction with the given hash.
|
||||
func (eth *Ethereum) TransactionByHash(ctx context.Context, txhash common.Hash) (tx *types.Transaction, isPending bool, err error) {
|
||||
if tx = eth.txPool.Get(txhash); tx != nil {
|
||||
return tx, true, nil
|
||||
}
|
||||
if tx, _, _, _ = core.GetTransaction(eth.chainDb, txhash); tx != nil {
|
||||
return tx, false, nil
|
||||
}
|
||||
return nil, false, ErrTxNotFound
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
|
||||
b.eth.txPool.SetLocal(signedTx)
|
||||
return b.eth.txPool.Add(signedTx)
|
||||
// TransactionInclusionBlock returns the block in which the given transaction was included.
|
||||
func (eth *Ethereum) TransactionInclusionBlock(txhash common.Hash) (bhash common.Hash, bnum uint64, index int, err error) {
|
||||
var tx *types.Transaction
|
||||
if tx, bhash, bnum, index = core.GetTransaction(eth.chainDb, txhash); tx == nil {
|
||||
err = ErrTxNotFound
|
||||
}
|
||||
return bhash, bnum, index, err
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
|
||||
b.eth.txPool.Remove(txHash)
|
||||
// TransactionReceipt returns the receipt of a transaction.
|
||||
func (eth *Ethereum) TransactionReceipt(ctx context.Context, txhash common.Hash) (*types.Receipt, error) {
|
||||
r := core.GetReceipt(eth.chainDb, txhash)
|
||||
if r == nil {
|
||||
return nil, ErrTxNotFound
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetPoolTransactions() types.Transactions {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
// BlockTD returns the total difficulty of a certain block.
|
||||
func (eth *Ethereum) BlockTD(blockhash common.Hash) *big.Int {
|
||||
return eth.blockchain.GetTdByHash(blockhash)
|
||||
}
|
||||
|
||||
// BalanceAt returns the balance of the given account.
|
||||
func (eth *Ethereum) BalanceAt(ctx context.Context, addr common.Address, block *big.Int) (bal *big.Int, err error) {
|
||||
err = eth.withStateAt(ctx, block, false, func(st *state.StateDB) { bal = st.GetBalance(addr) })
|
||||
return bal, err
|
||||
}
|
||||
|
||||
// CodeAt returns the code of the given account.
|
||||
func (eth *Ethereum) CodeAt(ctx context.Context, addr common.Address, block *big.Int) (code []byte, err error) {
|
||||
err = eth.withStateAt(ctx, block, false, func(st *state.StateDB) { code = st.GetCode(addr) })
|
||||
return code, err
|
||||
}
|
||||
|
||||
// NonceAt returns the nonce of the given account.
|
||||
func (eth *Ethereum) NonceAt(ctx context.Context, addr common.Address, block *big.Int) (nonce uint64, err error) {
|
||||
err = eth.withStateAt(ctx, block, false, func(st *state.StateDB) { nonce = st.GetNonce(addr) })
|
||||
return nonce, err
|
||||
}
|
||||
|
||||
// StorageAt returns a storage value of the given account.
|
||||
func (eth *Ethereum) StorageAt(ctx context.Context, addr common.Address, key common.Hash, block *big.Int) (val []byte, err error) {
|
||||
err = eth.withStateAt(ctx, block, false, func(st *state.StateDB) { v := st.GetState(addr, key); val = v[:] })
|
||||
return val, err
|
||||
}
|
||||
|
||||
// PendingBalanceAt returns the balance of the given account in the pending state.
|
||||
func (eth *Ethereum) PendingBalanceAt(ctx context.Context, addr common.Address) (bal *big.Int, err error) {
|
||||
err = eth.withStateAt(ctx, nil, true, func(st *state.StateDB) { bal = st.GetBalance(addr) })
|
||||
return bal, err
|
||||
}
|
||||
|
||||
// PendingBalanceAt returns the code of the given account in the pending state.
|
||||
func (eth *Ethereum) PendingCodeAt(ctx context.Context, addr common.Address) (code []byte, err error) {
|
||||
err = eth.withStateAt(ctx, nil, true, func(st *state.StateDB) { code = st.GetCode(addr) })
|
||||
return code, err
|
||||
}
|
||||
|
||||
// PendingBalanceAt returns a storage value of the given account in the pending state.
|
||||
func (eth *Ethereum) PendingStorageAt(ctx context.Context, addr common.Address, key common.Hash) (val []byte, err error) {
|
||||
err = eth.withStateAt(ctx, nil, true, func(st *state.StateDB) { v := st.GetState(addr, key); val = v[:] })
|
||||
return val, err
|
||||
}
|
||||
|
||||
// PendingTransaction count returns the number of transactions in the pending block.
|
||||
func (eth *Ethereum) PendingTransactionCount(ctx context.Context) (uint, error) {
|
||||
b, _ := eth.miner.Pending()
|
||||
return uint(len(b.Transactions())), nil
|
||||
}
|
||||
|
||||
func (eth *Ethereum) withStateAt(ctx context.Context, block *big.Int, pending bool, fn func(st *state.StateDB)) error {
|
||||
var st *state.StateDB
|
||||
if pending {
|
||||
_, st = eth.miner.Pending()
|
||||
} else {
|
||||
header, err := eth.HeaderByNumber(ctx, block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st, err = eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fn(st)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PendingBlock returns the next block as envisioned by the pending state.
|
||||
func (eth *Ethereum) PendingBlock() (*types.Block, error) {
|
||||
b, _ := eth.miner.Pending()
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// PendingNonceAt returns the next valid nonce according to the local transaction pool.
|
||||
func (eth *Ethereum) PendingNonceAt(ctx context.Context, addr common.Address) (uint64, error) {
|
||||
return eth.txPool.NonceAt(addr), nil
|
||||
}
|
||||
|
||||
// PendingTransactions returns all known pending transactions.
|
||||
func (eth *Ethereum) PendingTransactions() []*types.Transaction {
|
||||
eth.txMu.Lock()
|
||||
defer eth.txMu.Unlock()
|
||||
|
||||
var txs types.Transactions
|
||||
for _, batch := range b.eth.txPool.Pending() {
|
||||
for _, batch := range eth.txPool.Pending() {
|
||||
txs = append(txs, batch...)
|
||||
}
|
||||
return txs
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
// SendTransaction queues a transaction in the pool.
|
||||
func (eth *Ethereum) SendTransaction(ctx context.Context, signedTx *types.Transaction) error {
|
||||
eth.txMu.Lock()
|
||||
defer eth.txMu.Unlock()
|
||||
|
||||
return b.eth.txPool.Get(hash)
|
||||
eth.txPool.SetLocal(signedTx)
|
||||
return eth.txPool.Add(signedTx)
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
|
||||
return b.eth.txPool.State().GetNonce(addr), nil
|
||||
// SyncProgress returns sync status information.
|
||||
func (b *Ethereum) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
|
||||
return b.protocolManager.downloader.Progress(), nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) Stats() (pending int, queued int) {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
|
||||
return b.eth.txPool.Stats()
|
||||
// SuggestGasPrice returns a suitable gas price based on the content of recently seen blocks.
|
||||
func (eth *Ethereum) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
||||
return eth.gpo.SuggestPrice(), nil
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
|
||||
b.eth.txMu.Lock()
|
||||
defer b.eth.txMu.Unlock()
|
||||
// RemoveTransaction removes the given transaction from the local pool.
|
||||
func (eth *Ethereum) RemoveTransaction(txHash common.Hash) {
|
||||
eth.txMu.Lock()
|
||||
defer eth.txMu.Unlock()
|
||||
|
||||
return b.eth.TxPool().Content()
|
||||
eth.txPool.Remove(txHash)
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) Downloader() *downloader.Downloader {
|
||||
return b.eth.Downloader()
|
||||
// ProtocolVersion returns the active protocol version.
|
||||
func (eth *Ethereum) ProtocolVersion() int {
|
||||
return int(eth.protocolManager.SubProtocols[0].Version)
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) ProtocolVersion() int {
|
||||
return b.eth.EthVersion()
|
||||
// ChainConfig returns the active chain configuration.
|
||||
func (eth *Ethereum) ChainConfig() *params.ChainConfig {
|
||||
return eth.chainConfig
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
|
||||
return b.gpo.SuggestPrice(), nil
|
||||
// AccountManager returns the internal account manager that accesses the data directory.
|
||||
// Deprecated: get the account manager through node.Node instead.
|
||||
func (eth *Ethereum) AccountManager() *accounts.Manager {
|
||||
return eth.accountManager
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) ChainDb() ethdb.Database {
|
||||
return b.eth.ChainDb()
|
||||
// ResetHeadBlock resets the blockchain to the given number.
|
||||
// Use this method if you know what you're doing.
|
||||
func (eth *Ethereum) ResetHeadBlock(blocknum uint64) {
|
||||
eth.blockchain.SetHead(blocknum)
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) EventMux() *event.TypeMux {
|
||||
return b.eth.EventMux()
|
||||
func (eth *Ethereum) EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error) {
|
||||
block, state := eth.miner.Pending()
|
||||
_, gas, err := eth.callContract(call, block.Header(), state)
|
||||
return gas, err
|
||||
}
|
||||
|
||||
func (b *EthApiBackend) AccountManager() *accounts.Manager {
|
||||
return b.eth.AccountManager()
|
||||
func (eth *Ethereum) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
|
||||
block, state := eth.miner.Pending()
|
||||
val, _, err := eth.callContract(call, block.Header(), state)
|
||||
return val, err
|
||||
}
|
||||
|
||||
type EthApiState struct {
|
||||
state *state.StateDB
|
||||
func (eth *Ethereum) CallContract(ctx context.Context, call ethereum.CallMsg, blocknum *big.Int) ([]byte, error) {
|
||||
var head *types.Header
|
||||
if blocknum == nil {
|
||||
head = eth.blockchain.CurrentHeader()
|
||||
} else if head = eth.blockchain.GetHeaderByNumber(blocknum.Uint64()); head == nil {
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
state, err := eth.blockchain.StateAt(head.Root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
val, _, err := eth.callContract(call, head, state)
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
|
||||
return s.state.GetBalance(addr), nil
|
||||
}
|
||||
|
||||
func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
|
||||
return s.state.GetCode(addr), nil
|
||||
}
|
||||
|
||||
func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
|
||||
return s.state.GetState(a, b), nil
|
||||
}
|
||||
|
||||
func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
|
||||
return s.state.GetNonce(addr), nil
|
||||
func (eth *Ethereum) callContract(call ethereum.CallMsg, head *types.Header, state *state.StateDB) ([]byte, *big.Int, error) {
|
||||
return core.ApplyCallMessage(call, func(msg core.Message) vm.Environment {
|
||||
return core.NewEnv(state, eth.chainConfig, eth.blockchain, msg, head, vm.Config{})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ import (
|
|||
"github.com/ethereum/ethash"
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/httpclient"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
|
@ -128,14 +127,13 @@ type Ethereum struct {
|
|||
pow *ethash.Ethash
|
||||
accountManager *accounts.Manager
|
||||
|
||||
ApiBackend *EthApiBackend
|
||||
|
||||
miner *miner.Miner
|
||||
Mining bool
|
||||
MinerThreads int
|
||||
AutoDAG bool
|
||||
autodagquit chan bool
|
||||
etherbase common.Address
|
||||
gpo *gasprice.GasPriceOracle
|
||||
solcPath string
|
||||
|
||||
NatSpec bool
|
||||
|
@ -215,14 +213,14 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
|
||||
eth.chainConfig = config.ChainConfig
|
||||
|
||||
eth.blockchain, err = core.NewBlockChain(chainDb, eth.chainConfig, eth.pow, eth.EventMux())
|
||||
eth.blockchain, err = core.NewBlockChain(chainDb, eth.chainConfig, eth.pow, eth.eventMux)
|
||||
if err != nil {
|
||||
if err == core.ErrNoGenesis {
|
||||
return nil, fmt.Errorf(`No chain found. Please initialise a new chain using the "init" subcommand.`)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
newPool := core.NewTxPool(eth.chainConfig, eth.EventMux(), eth.blockchain.State, eth.blockchain.GasLimit)
|
||||
newPool := core.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain.State, eth.blockchain.GasLimit)
|
||||
eth.txPool = newPool
|
||||
|
||||
maxPeers := config.MaxPeers
|
||||
|
@ -239,10 +237,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.FastSync, config.NetworkId, maxPeers, eth.eventMux, eth.txPool, eth.pow, eth.blockchain, chainDb); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.pow)
|
||||
|
||||
// Set up the miner.
|
||||
eth.miner = miner.New(eth, eth.chainConfig, eth.eventMux, eth.pow)
|
||||
eth.miner.SetGasPrice(config.GasPrice)
|
||||
eth.miner.SetExtra(config.ExtraData)
|
||||
|
||||
// Set up the gas price oracle.
|
||||
gpoParams := &gasprice.GpoParams{
|
||||
GpoMinGasPrice: config.GpoMinGasPrice,
|
||||
GpoMaxGasPrice: config.GpoMaxGasPrice,
|
||||
|
@ -251,8 +252,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
GpobaseStepUp: config.GpobaseStepUp,
|
||||
GpobaseCorrectionFactor: config.GpobaseCorrectionFactor,
|
||||
}
|
||||
gpo := gasprice.NewGasPriceOracle(eth.blockchain, chainDb, eth.eventMux, gpoParams)
|
||||
eth.ApiBackend = &EthApiBackend{eth, gpo}
|
||||
eth.gpo = gasprice.NewGasPriceOracle(eth.blockchain, chainDb, eth.eventMux, gpoParams)
|
||||
|
||||
return eth, nil
|
||||
}
|
||||
|
@ -306,50 +306,58 @@ func CreatePoW(config *Config) (*ethash.Ethash, error) {
|
|||
|
||||
// APIs returns the collection of RPC services the ethereum package offers.
|
||||
// NOTE, some of these services probably need to be moved to somewhere else.
|
||||
func (s *Ethereum) APIs() []rpc.API {
|
||||
return append(ethapi.GetAPIs(s.ApiBackend, s.solcPath), []rpc.API{
|
||||
func (eth *Ethereum) APIs() []rpc.API {
|
||||
return append(ethapi.GetAPIs(eth, eth.solcPath), []rpc.API{
|
||||
{
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: NewPublicEthereumAPI(s),
|
||||
Service: NewPublicEthereumAPI(eth),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: NewPublicMinerAPI(s),
|
||||
Service: NewPublicMinerAPI(eth),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
|
||||
Service: downloader.NewPublicDownloaderAPI(eth.protocolManager.downloader, eth.eventMux),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "miner",
|
||||
Version: "1.0",
|
||||
Service: NewPrivateMinerAPI(s),
|
||||
Service: NewPrivateMinerAPI(eth),
|
||||
Public: false,
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: filters.NewPublicFilterAPI(s.ApiBackend, false),
|
||||
Service: filters.NewPublicFilterAPI(eth, eth.eventMux, eth.chainDb, false),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "admin",
|
||||
Version: "1.0",
|
||||
Service: NewPrivateAdminAPI(s),
|
||||
Service: NewPrivateAdminAPI(eth),
|
||||
}, {
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: NewPublicDebugAPI(s),
|
||||
Service: NewPublicDebugAPI(eth),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: NewPrivateDebugAPI(s.chainConfig, s),
|
||||
Service: ethapi.NewPrivateDebugAPI(eth, eth.chainDb),
|
||||
}, {
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: NewPrivateDebugAPI(eth.chainConfig, eth),
|
||||
}, {
|
||||
Namespace: "txpool",
|
||||
Version: "1.0",
|
||||
Service: ethapi.TxPoolDebugAPI{Pool: eth.txPool},
|
||||
}, {
|
||||
Namespace: "net",
|
||||
Version: "1.0",
|
||||
Service: s.netRPCService,
|
||||
Service: eth.netRPCService,
|
||||
Public: true,
|
||||
},
|
||||
}...)
|
||||
|
@ -388,20 +396,16 @@ func (s *Ethereum) StartMining(threads int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Ethereum) StopMining() { s.miner.Stop() }
|
||||
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
|
||||
func (s *Ethereum) Miner() *miner.Miner { return s.miner }
|
||||
func (s *Ethereum) StopMining() { s.miner.Stop() }
|
||||
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
|
||||
|
||||
func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager }
|
||||
func (s *Ethereum) BlockChain() *core.BlockChain { return s.blockchain }
|
||||
func (s *Ethereum) TxPool() *core.TxPool { return s.txPool }
|
||||
func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux }
|
||||
func (s *Ethereum) Pow() *ethash.Ethash { return s.pow }
|
||||
func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb }
|
||||
func (s *Ethereum) IsListening() bool { return true } // Always listening
|
||||
func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
||||
func (s *Ethereum) NetVersion() int { return s.netVersionId }
|
||||
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
||||
// Deprecated Accessors
|
||||
|
||||
func (s *Ethereum) BlockChain() *core.BlockChain { return s.blockchain }
|
||||
func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb }
|
||||
func (s *Ethereum) TxPool() *core.TxPool { return s.txPool }
|
||||
func (s *Ethereum) Pow() *ethash.Ethash { return s.pow }
|
||||
func (s *Ethereum) Miner() *miner.Miner { return s.miner }
|
||||
|
||||
// Protocols implements node.Service, returning all the currently configured
|
||||
// network protocols to start.
|
||||
|
@ -416,7 +420,7 @@ func (s *Ethereum) Protocols() []p2p.Protocol {
|
|||
// Start implements node.Service, starting all internal goroutines needed by the
|
||||
// Ethereum protocol implementation.
|
||||
func (s *Ethereum) Start(srvr *p2p.Server) error {
|
||||
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())
|
||||
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId)
|
||||
if s.AutoDAG {
|
||||
s.StartAutoDAG()
|
||||
}
|
||||
|
@ -518,12 +522,6 @@ func (self *Ethereum) StopAutoDAG() {
|
|||
glog.V(logger.Info).Infof("Automatic pregeneration of ethash DAG OFF (ethash dir: %s)", ethash.DefaultDir)
|
||||
}
|
||||
|
||||
// HTTPClient returns the light http client used for fetching offchain docs
|
||||
// (natspec, source for verification)
|
||||
func (self *Ethereum) HTTPClient() *httpclient.HTTPClient {
|
||||
return self.httpclient
|
||||
}
|
||||
|
||||
// dagFiles(epoch) returns the two alternative DAG filenames (not a path)
|
||||
// 1) <revision>-<hex(seedhash[8])> 2) full-R<revision>-<hex(seedhash[8])>
|
||||
func dagFiles(epoch uint64) (string, string) {
|
||||
|
|
136
eth/bind.go
136
eth/bind.go
|
@ -1,136 +0,0 @@
|
|||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContractBackend implements bind.ContractBackend with direct calls to Ethereum
|
||||
// internals to support operating on contracts within subprotocols like eth and
|
||||
// swarm.
|
||||
//
|
||||
// Internally this backend uses the already exposed API endpoints of the Ethereum
|
||||
// object. These should be rewritten to internal Go method calls when the Go API
|
||||
// is refactored to support a clean library use.
|
||||
type ContractBackend struct {
|
||||
eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
|
||||
bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data
|
||||
txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
|
||||
}
|
||||
|
||||
// NewContractBackend creates a new native contract backend using an existing
|
||||
// Etheruem object.
|
||||
func NewContractBackend(apiBackend ethapi.Backend) *ContractBackend {
|
||||
return &ContractBackend{
|
||||
eapi: ethapi.NewPublicEthereumAPI(apiBackend),
|
||||
bcapi: ethapi.NewPublicBlockChainAPI(apiBackend),
|
||||
txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend),
|
||||
}
|
||||
}
|
||||
|
||||
// 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) 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: msg.To,
|
||||
From: msg.From,
|
||||
Data: common.ToHex(msg.Data),
|
||||
}
|
||||
if msg.Gas != nil {
|
||||
args.Gas = *rpc.NewHexNumber(msg.Gas)
|
||||
}
|
||||
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) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
||||
out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber)
|
||||
return out.Uint64(), err
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return b.eapi.GasPrice(ctx)
|
||||
}
|
||||
|
||||
// EstimateGasLimit implements bind.ContractTransactor triing to estimate the gas
|
||||
// needed to execute a specific transaction based on the current pending state of
|
||||
// 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) 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 {
|
||||
raw, _ := rlp.EncodeToBytes(tx)
|
||||
_, err := b.txapi.SendRawTransaction(ctx, common.ToHex(raw))
|
||||
return err
|
||||
}
|
|
@ -76,7 +76,7 @@ func (api *PublicDownloaderAPI) eventLoop() {
|
|||
case StartEvent:
|
||||
notification = &SyncingResult{
|
||||
Syncing: true,
|
||||
Status: api.d.Progress(),
|
||||
Status: *api.d.Progress(),
|
||||
}
|
||||
case DoneEvent, FailedEvent:
|
||||
notification = false
|
||||
|
|
|
@ -212,7 +212,7 @@ func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, hasHeader he
|
|||
// In addition, during the state download phase of fast synchronisation the number
|
||||
// of processed and the total number of known states are also returned. Otherwise
|
||||
// these are zero.
|
||||
func (d *Downloader) Progress() ethereum.SyncProgress {
|
||||
func (d *Downloader) Progress() *ethereum.SyncProgress {
|
||||
// Fetch the pending state count outside of the lock to prevent unforeseen deadlocks
|
||||
pendingStates := uint64(d.queue.PendingNodeData())
|
||||
|
||||
|
@ -229,7 +229,7 @@ func (d *Downloader) Progress() ethereum.SyncProgress {
|
|||
case LightSync:
|
||||
current = d.headHeader().Number.Uint64()
|
||||
}
|
||||
return ethereum.SyncProgress{
|
||||
return ðereum.SyncProgress{
|
||||
StartingBlock: d.syncStatsChainOrigin,
|
||||
CurrentBlock: current,
|
||||
HighestBlock: d.syncStatsChainHeight,
|
||||
|
|
|
@ -56,20 +56,20 @@ type PublicFilterAPI struct {
|
|||
useMipMap bool
|
||||
mux *event.TypeMux
|
||||
quit chan struct{}
|
||||
chainDb ethdb.Database
|
||||
chaindb ethdb.Database
|
||||
events *EventSystem
|
||||
filtersMu sync.Mutex
|
||||
filters map[rpc.ID]*filter
|
||||
}
|
||||
|
||||
// NewPublicFilterAPI returns a new PublicFilterAPI instance.
|
||||
func NewPublicFilterAPI(backend Backend, lightMode bool) *PublicFilterAPI {
|
||||
func NewPublicFilterAPI(backend Backend, mux *event.TypeMux, chaindb ethdb.Database, lightMode bool) *PublicFilterAPI {
|
||||
api := &PublicFilterAPI{
|
||||
backend: backend,
|
||||
useMipMap: !lightMode,
|
||||
mux: backend.EventMux(),
|
||||
chainDb: backend.ChainDb(),
|
||||
events: NewEventSystem(backend.EventMux(), backend, lightMode),
|
||||
mux: mux,
|
||||
chaindb: chaindb,
|
||||
events: NewEventSystem(mux, backend, lightMode),
|
||||
filters: make(map[rpc.ID]*filter),
|
||||
}
|
||||
|
||||
|
@ -326,7 +326,7 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
|
|||
crit.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
|
||||
}
|
||||
|
||||
filter := New(api.backend, api.useMipMap)
|
||||
filter := New(api.backend, api.chaindb, api.useMipMap)
|
||||
filter.SetBeginBlock(crit.FromBlock.Int64())
|
||||
filter.SetEndBlock(crit.ToBlock.Int64())
|
||||
filter.SetAddresses(crit.Addresses)
|
||||
|
@ -366,7 +366,7 @@ func (api *PublicFilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]Log
|
|||
return []Log{}, nil
|
||||
}
|
||||
|
||||
filter := New(api.backend, api.useMipMap)
|
||||
filter := New(api.backend, api.chaindb, api.useMipMap)
|
||||
filter.SetBeginBlock(f.crit.FromBlock.Int64())
|
||||
filter.SetEndBlock(f.crit.ToBlock.Int64())
|
||||
filter.SetAddresses(f.crit.Addresses)
|
||||
|
|
|
@ -18,22 +18,20 @@ package filters
|
|||
|
||||
import (
|
||||
"math"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Backend interface {
|
||||
ChainDb() ethdb.Database
|
||||
EventMux() *event.TypeMux
|
||||
HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
|
||||
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
|
||||
HeaderByHash(context.Context, common.Hash) (*types.Header, error)
|
||||
HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
|
||||
BlockReceipts(ctx context.Context, blockhash common.Hash, blocknum uint64) ([]*types.Receipt, error)
|
||||
}
|
||||
|
||||
// Filter can be used to retrieve and filter logs
|
||||
|
@ -51,11 +49,11 @@ type Filter struct {
|
|||
|
||||
// New creates a new filter which uses a bloom filter on blocks to figure out whether
|
||||
// a particular block is interesting or not.
|
||||
func New(backend Backend, useMipMap bool) *Filter {
|
||||
func New(backend Backend, chaindb ethdb.Database, useMipMap bool) *Filter {
|
||||
return &Filter{
|
||||
backend: backend,
|
||||
useMipMap: useMipMap,
|
||||
db: backend.ChainDb(),
|
||||
db: chaindb,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,7 +82,7 @@ func (f *Filter) SetTopics(topics [][]common.Hash) {
|
|||
|
||||
// Run filters logs with the current parameters set
|
||||
func (f *Filter) Find(ctx context.Context) ([]Log, error) {
|
||||
head, _ := f.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
|
||||
head, _ := f.backend.HeaderByNumber(ctx, nil)
|
||||
if head == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -141,7 +139,7 @@ func (f *Filter) mipFind(start, end uint64, depth int) (logs []Log) {
|
|||
|
||||
func (f *Filter) getLogs(ctx context.Context, start, end uint64) (logs []Log, err error) {
|
||||
for i := start; i <= end; i++ {
|
||||
header, err := f.backend.HeaderByNumber(ctx, rpc.BlockNumber(i))
|
||||
header, err := f.backend.HeaderByNumber(ctx, new(big.Int).SetUint64(i))
|
||||
if header == nil || err != nil {
|
||||
return logs, err
|
||||
}
|
||||
|
@ -150,7 +148,7 @@ func (f *Filter) getLogs(ctx context.Context, start, end uint64) (logs []Log, er
|
|||
// current parameters
|
||||
if f.bloomFilter(header.Bloom) {
|
||||
// Get the logs of the block
|
||||
receipts, err := f.backend.GetReceipts(ctx, header.Hash())
|
||||
receipts, err := f.backend.BlockReceipts(ctx, header.Hash(), i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
@ -301,27 +303,44 @@ func (es *EventSystem) broadcast(filters filterIndex, ev *event.Event) {
|
|||
|
||||
func (es *EventSystem) lightFilterNewHead(newHeader *types.Header, callBack func(*types.Header, bool)) {
|
||||
oldh := es.lastHead
|
||||
es.lastHead = newHeader
|
||||
if oldh == nil {
|
||||
return
|
||||
}
|
||||
newh := newHeader
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
// find common ancestor, create list of rolled back and new block hashes
|
||||
var oldHeaders, newHeaders []*types.Header
|
||||
var (
|
||||
oldHeaders, newHeaders []*types.Header
|
||||
newh = newHeader
|
||||
err error
|
||||
)
|
||||
for oldh.Hash() != newh.Hash() {
|
||||
if oldh.Number.Uint64() >= newh.Number.Uint64() {
|
||||
oldHeaders = append(oldHeaders, oldh)
|
||||
oldh = core.GetHeader(es.backend.ChainDb(), oldh.ParentHash, oldh.Number.Uint64()-1)
|
||||
oldh, err = es.backend.HeaderByHash(ctx, oldh.ParentHash)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if oldh.Number.Uint64() < newh.Number.Uint64() {
|
||||
newHeaders = append(newHeaders, newh)
|
||||
newh = core.GetHeader(es.backend.ChainDb(), newh.ParentHash, newh.Number.Uint64()-1)
|
||||
newh, err = es.backend.HeaderByHash(ctx, newh.ParentHash)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if newh == nil {
|
||||
// happens when CHT syncing, nothing to do
|
||||
newh = oldh
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if glog.V(logger.Error) {
|
||||
glog.Errorf("aborted rollback, can't fetch header: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// roll back old blocks
|
||||
for _, h := range oldHeaders {
|
||||
callBack(h, true)
|
||||
|
@ -330,6 +349,7 @@ func (es *EventSystem) lightFilterNewHead(newHeader *types.Header, callBack func
|
|||
for i := len(newHeaders) - 1; i >= 0; i-- {
|
||||
callBack(newHeaders[i], false)
|
||||
}
|
||||
es.lastHead = newh
|
||||
}
|
||||
|
||||
// filter logs of a single header in light client mode
|
||||
|
@ -339,7 +359,7 @@ func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.
|
|||
//fmt.Println("bloom match")
|
||||
// Get the logs of the block
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
|
||||
receipts, err := es.backend.GetReceipts(ctx, header.Hash())
|
||||
receipts, err := es.backend.BlockReceipts(ctx, header.Hash(), header.Number.Uint64())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ var (
|
|||
mux = new(event.TypeMux)
|
||||
db, _ = ethdb.NewMemDatabase()
|
||||
backend = &testBackend{mux, db}
|
||||
api = NewPublicFilterAPI(backend, false)
|
||||
api = NewPublicFilterAPI(backend, mux, db, false)
|
||||
)
|
||||
|
||||
type testBackend struct {
|
||||
|
@ -46,30 +46,26 @@ type testBackend struct {
|
|||
db ethdb.Database
|
||||
}
|
||||
|
||||
func (b *testBackend) ChainDb() ethdb.Database {
|
||||
return b.db
|
||||
}
|
||||
|
||||
func (b *testBackend) EventMux() *event.TypeMux {
|
||||
return b.mux
|
||||
}
|
||||
|
||||
func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
|
||||
func (b *testBackend) HeaderByNumber(ctx context.Context, blockNum *big.Int) (*types.Header, error) {
|
||||
var hash common.Hash
|
||||
var num uint64
|
||||
if blockNr == rpc.LatestBlockNumber {
|
||||
if blockNum == nil {
|
||||
hash = core.GetHeadBlockHash(b.db)
|
||||
num = core.GetBlockNumber(b.db, hash)
|
||||
} else {
|
||||
num = uint64(blockNr)
|
||||
num = blockNum.Uint64()
|
||||
hash = core.GetCanonicalHash(b.db, num)
|
||||
}
|
||||
return core.GetHeader(b.db, hash, num), nil
|
||||
}
|
||||
|
||||
func (b *testBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
|
||||
func (b *testBackend) HeaderByHash(ctx context.Context, blockHash common.Hash) (*types.Header, error) {
|
||||
num := core.GetBlockNumber(b.db, blockHash)
|
||||
return core.GetBlockReceipts(b.db, blockHash, num), nil
|
||||
return core.GetHeader(b.db, blockHash, num), nil
|
||||
}
|
||||
|
||||
func (b *testBackend) BlockReceipts(ctx context.Context, blockHash common.Hash, blockNum uint64) ([]*types.Receipt, error) {
|
||||
return core.GetBlockReceipts(b.db, blockHash, blockNum), nil
|
||||
}
|
||||
|
||||
// TestBlockSubscription tests if a block subscription returns block hashes for posted chain events.
|
||||
|
|
|
@ -104,7 +104,7 @@ func BenchmarkMipmaps(b *testing.B) {
|
|||
}
|
||||
b.ResetTimer()
|
||||
|
||||
filter := New(backend, true)
|
||||
filter := New(backend, db, true)
|
||||
filter.SetAddresses([]common.Address{addr1, addr2, addr3, addr4})
|
||||
filter.SetBeginBlock(0)
|
||||
filter.SetEndBlock(-1)
|
||||
|
@ -206,7 +206,7 @@ func TestFilters(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
filter := New(backend, true)
|
||||
filter := New(backend, db, true)
|
||||
filter.SetAddresses([]common.Address{addr})
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{hash1, hash2, hash3, hash4}})
|
||||
filter.SetBeginBlock(0)
|
||||
|
@ -217,7 +217,7 @@ func TestFilters(t *testing.T) {
|
|||
t.Error("expected 4 log, got", len(logs))
|
||||
}
|
||||
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetAddresses([]common.Address{addr})
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{hash3}})
|
||||
filter.SetBeginBlock(900)
|
||||
|
@ -230,7 +230,7 @@ func TestFilters(t *testing.T) {
|
|||
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
|
||||
}
|
||||
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetAddresses([]common.Address{addr})
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{hash3}})
|
||||
filter.SetBeginBlock(990)
|
||||
|
@ -243,7 +243,7 @@ func TestFilters(t *testing.T) {
|
|||
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
|
||||
}
|
||||
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{hash1, hash2}})
|
||||
filter.SetBeginBlock(1)
|
||||
filter.SetEndBlock(10)
|
||||
|
@ -254,7 +254,7 @@ func TestFilters(t *testing.T) {
|
|||
}
|
||||
|
||||
failHash := common.BytesToHash([]byte("fail"))
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{failHash}})
|
||||
filter.SetBeginBlock(0)
|
||||
filter.SetEndBlock(-1)
|
||||
|
@ -265,7 +265,7 @@ func TestFilters(t *testing.T) {
|
|||
}
|
||||
|
||||
failAddr := common.BytesToAddress([]byte("failmenow"))
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetAddresses([]common.Address{failAddr})
|
||||
filter.SetBeginBlock(0)
|
||||
filter.SetEndBlock(-1)
|
||||
|
@ -275,7 +275,7 @@ func TestFilters(t *testing.T) {
|
|||
t.Error("expected 0 log, got", len(logs))
|
||||
}
|
||||
|
||||
filter = New(backend, true)
|
||||
filter = New(backend, db, true)
|
||||
filter.SetTopics([][]common.Hash{[]common.Hash{failHash}, []common.Hash{hash1}})
|
||||
filter.SetBeginBlock(0)
|
||||
filter.SetEndBlock(-1)
|
||||
|
|
|
@ -21,9 +21,8 @@ import (
|
|||
"sort"
|
||||
"sync"
|
||||
|
||||
ethereum "github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
|
@ -38,7 +37,7 @@ const (
|
|||
// LightPriceOracle recommends gas prices based on the content of recent
|
||||
// blocks. Suitable for both light and full clients.
|
||||
type LightPriceOracle struct {
|
||||
backend ethapi.Backend
|
||||
backend ethereum.ChainReader
|
||||
lastHead common.Hash
|
||||
lastPrice *big.Int
|
||||
cacheLock sync.RWMutex
|
||||
|
@ -46,7 +45,7 @@ type LightPriceOracle struct {
|
|||
}
|
||||
|
||||
// NewLightPriceOracle returns a new oracle.
|
||||
func NewLightPriceOracle(backend ethapi.Backend) *LightPriceOracle {
|
||||
func NewLightPriceOracle(backend ethereum.ChainReader) *LightPriceOracle {
|
||||
return &LightPriceOracle{
|
||||
backend: backend,
|
||||
lastPrice: big.NewInt(LpoDefaultPrice),
|
||||
|
@ -60,7 +59,7 @@ func (self *LightPriceOracle) SuggestPrice(ctx context.Context) (*big.Int, error
|
|||
lastPrice := self.lastPrice
|
||||
self.cacheLock.RUnlock()
|
||||
|
||||
head, _ := self.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
|
||||
head, _ := self.backend.HeaderByNumber(ctx, nil)
|
||||
headHash := head.Hash()
|
||||
if headHash == lastHead {
|
||||
return lastPrice, nil
|
||||
|
@ -132,7 +131,7 @@ type lpResult struct {
|
|||
// getLowestPrice calculates the lowest transaction gas price in a given block
|
||||
// and sends it to the result channel. If the block is empty, price is nil.
|
||||
func (self *LightPriceOracle) getLowestPrice(ctx context.Context, blockNum uint64, chn chan lpResult) {
|
||||
block, err := self.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
|
||||
block, err := self.backend.BlockByNumber(ctx, new(big.Int).SetUint64(blockNum))
|
||||
if block == nil {
|
||||
chn <- lpResult{nil, err}
|
||||
return
|
||||
|
|
|
@ -19,6 +19,7 @@ package ethclient
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
|
@ -143,15 +144,18 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H
|
|||
}
|
||||
|
||||
// TransactionByHash returns the transaction with the given hash.
|
||||
func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (*types.Transaction, error) {
|
||||
var tx *types.Transaction
|
||||
err := ec.c.CallContext(ctx, &tx, "eth_getTransactionByHash", hash)
|
||||
func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
|
||||
var rtx struct {
|
||||
*types.Transaction
|
||||
BlockHash *common.Hash
|
||||
}
|
||||
err = ec.c.CallContext(ctx, &rtx, "eth_getTransactionByHash", hash)
|
||||
if err == nil {
|
||||
if _, r, _ := tx.RawSignatureValues(); r == nil {
|
||||
return nil, fmt.Errorf("server returned transaction without signature")
|
||||
return nil, false, errors.New("server returned transaction without signature")
|
||||
}
|
||||
}
|
||||
return tx, err
|
||||
return rtx.Transaction, rtx.BlockHash == nil, err
|
||||
}
|
||||
|
||||
// TransactionCount returns the total number of transactions in the given block.
|
||||
|
|
|
@ -53,8 +53,8 @@ type ChainReader interface {
|
|||
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
||||
TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)
|
||||
TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)
|
||||
TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, error)
|
||||
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
|
||||
TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error)
|
||||
}
|
||||
|
||||
// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that
|
||||
|
@ -164,6 +164,9 @@ type PendingStateReader interface {
|
|||
PendingTransactionCount(ctx context.Context) (uint, error)
|
||||
}
|
||||
|
||||
// TODO(fjl): PendingNonceAt should have its own interface because it is provided by the tx pool
|
||||
// and more widely available than the others. We could also make it part of TransactionSender.
|
||||
|
||||
// PendingContractCaller can be used to perform calls against the pending state.
|
||||
type PendingContractCaller interface {
|
||||
PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -20,56 +20,46 @@ package ethapi
|
|||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Backend interface provides the common API services (that are provided by
|
||||
// both full and light clients) with access to necessary functions.
|
||||
// Backend defines most methods required for the RPC API.
|
||||
type Backend interface {
|
||||
// general Ethereum API
|
||||
Downloader() *downloader.Downloader
|
||||
ProtocolVersion() int
|
||||
SuggestPrice(ctx context.Context) (*big.Int, error)
|
||||
ChainDb() ethdb.Database
|
||||
EventMux() *event.TypeMux
|
||||
AccountManager() *accounts.Manager
|
||||
// BlockChain API
|
||||
SetHead(number uint64)
|
||||
HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
|
||||
BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
|
||||
StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (State, *types.Header, error)
|
||||
GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
|
||||
GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
|
||||
GetTd(blockHash common.Hash) *big.Int
|
||||
GetVMEnv(ctx context.Context, msg core.Message, state State, header *types.Header) (vm.Environment, func() error, error)
|
||||
// TxPool API
|
||||
SendTx(ctx context.Context, signedTx *types.Transaction) error
|
||||
RemoveTx(txHash common.Hash)
|
||||
GetPoolTransactions() types.Transactions
|
||||
GetPoolTransaction(txHash common.Hash) *types.Transaction
|
||||
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
|
||||
Stats() (pending int, queued int)
|
||||
TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
|
||||
ethereum.ChainReader
|
||||
ethereum.ChainStateReader
|
||||
ethereum.ChainSyncReader
|
||||
ethereum.TransactionSender
|
||||
ethereum.GasPricer
|
||||
ethereum.GasEstimator
|
||||
ethereum.ContractCaller
|
||||
|
||||
ProtocolVersion() int
|
||||
ChainConfig() *params.ChainConfig
|
||||
CurrentBlock() *types.Block
|
||||
AccountManager() *accounts.Manager // TODO(fjl): this should be a constructor argb
|
||||
BlockTD(common.Hash) *big.Int
|
||||
RemoveTransaction(txhash common.Hash)
|
||||
PendingTransactions() []*types.Transaction
|
||||
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
|
||||
ResetHeadBlock(number uint64) // for admin API
|
||||
}
|
||||
|
||||
type State interface {
|
||||
GetBalance(ctx context.Context, addr common.Address) (*big.Int, error)
|
||||
GetCode(ctx context.Context, addr common.Address) ([]byte, error)
|
||||
GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error)
|
||||
GetNonce(ctx context.Context, addr common.Address) (uint64, error)
|
||||
// PendingState is implemented by the eth.Ethereum backend and provides access to optional
|
||||
// features that can only be provided by a full pending state.
|
||||
type PendingState interface {
|
||||
PendingBlock() (*types.Block, error)
|
||||
ethereum.PendingStateReader
|
||||
ethereum.PendingContractCaller
|
||||
}
|
||||
|
||||
type TransactionInclusionBlock interface {
|
||||
// returns the block at which the given transaction was included in the blockchain
|
||||
TransactionInclusionBlock(txhash common.Hash) (blockhash common.Hash, blocknum uint64, index int, err error)
|
||||
}
|
||||
|
||||
func GetAPIs(apiBackend Backend, solcPath string) []rpc.API {
|
||||
|
@ -90,20 +80,11 @@ func GetAPIs(apiBackend Backend, solcPath string) []rpc.API {
|
|||
Version: "1.0",
|
||||
Service: NewPublicTransactionPoolAPI(apiBackend),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "txpool",
|
||||
Version: "1.0",
|
||||
Service: NewPublicTxPoolAPI(apiBackend),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: NewPublicDebugAPI(apiBackend),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: NewPrivateDebugAPI(apiBackend),
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package ethapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
|
||||
type TxPool interface {
|
||||
Stats() (pending int, queued int)
|
||||
Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
|
||||
}
|
||||
|
||||
// TxPoolDebugAPI offers and API for the transaction pool. It only operates on data that
|
||||
// is non confidential.
|
||||
type TxPoolDebugAPI struct{ Pool TxPool }
|
||||
|
||||
// Content returns the transactions contained within the transaction pool.
|
||||
func (s TxPoolDebugAPI) Content() map[string]map[string]map[string]*RPCTransaction {
|
||||
content := map[string]map[string]map[string]*RPCTransaction{
|
||||
"pending": make(map[string]map[string]*RPCTransaction),
|
||||
"queued": make(map[string]map[string]*RPCTransaction),
|
||||
}
|
||||
pending, queue := s.Pool.Content()
|
||||
|
||||
// Flatten the pending transactions
|
||||
for account, txs := range pending {
|
||||
dump := make(map[string]*RPCTransaction)
|
||||
for nonce, tx := range txs {
|
||||
dump[fmt.Sprintf("%d", nonce)] = newRPCTransaction(tx)
|
||||
}
|
||||
content["pending"][account.Hex()] = dump
|
||||
}
|
||||
// Flatten the queued transactions
|
||||
for account, txs := range queue {
|
||||
dump := make(map[string]*RPCTransaction)
|
||||
for nonce, tx := range txs {
|
||||
dump[fmt.Sprintf("%d", nonce)] = newRPCTransaction(tx)
|
||||
}
|
||||
content["queued"][account.Hex()] = dump
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
// Status returns the number of pending and queued transaction in the pool.
|
||||
func (s TxPoolDebugAPI) Status() map[string]*rpc.HexNumber {
|
||||
pending, queue := s.Pool.Stats()
|
||||
return map[string]*rpc.HexNumber{
|
||||
"pending": rpc.NewHexNumber(pending),
|
||||
"queued": rpc.NewHexNumber(queue),
|
||||
}
|
||||
}
|
||||
|
||||
// Inspect retrieves the content of the transaction pool and flattens it into an
|
||||
// easily inspectable list.
|
||||
func (s TxPoolDebugAPI) Inspect() map[string]map[string]map[string]string {
|
||||
content := map[string]map[string]map[string]string{
|
||||
"pending": make(map[string]map[string]string),
|
||||
"queued": make(map[string]map[string]string),
|
||||
}
|
||||
pending, queue := s.Pool.Content()
|
||||
|
||||
// Define a formatter to flatten a transaction into a string
|
||||
var format = func(tx *types.Transaction) string {
|
||||
if to := tx.To(); to != nil {
|
||||
return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice())
|
||||
}
|
||||
return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice())
|
||||
}
|
||||
// Flatten the pending transactions
|
||||
for account, txs := range pending {
|
||||
dump := make(map[string]string)
|
||||
for nonce, tx := range txs {
|
||||
dump[fmt.Sprintf("%d", nonce)] = format(tx)
|
||||
}
|
||||
content["pending"][account.Hex()] = dump
|
||||
}
|
||||
// Flatten the queued transactions
|
||||
for account, txs := range queue {
|
||||
dump := make(map[string]string)
|
||||
for nonce, tx := range txs {
|
||||
dump[fmt.Sprintf("%d", nonce)] = format(tx)
|
||||
}
|
||||
content["queued"][account.Hex()] = dump
|
||||
}
|
||||
return content
|
||||
}
|
|
@ -17,137 +17,208 @@
|
|||
package les
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
ethereum "github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/accounts"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
"github.com/ethereum/go-ethereum/eth/gasprice"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/light"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
rpc "github.com/ethereum/go-ethereum/rpc"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type LesApiBackend struct {
|
||||
eth *LightEthereum
|
||||
gpo *gasprice.LightPriceOracle
|
||||
}
|
||||
var (
|
||||
ErrBlockNotFound = errors.New("block not found")
|
||||
ErrTxNotFound = errors.New("the light client cannot retrieve past transactions by hash")
|
||||
ErrReceiptNotFound = errors.New("the light client cannot retrieve receipts by hash")
|
||||
)
|
||||
|
||||
func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
|
||||
return b.eth.chainConfig
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) CurrentBlock() *types.Block {
|
||||
return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) SetHead(number uint64) {
|
||||
b.eth.blockchain.SetHead(number)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
|
||||
if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
|
||||
return b.eth.blockchain.CurrentHeader(), nil
|
||||
func (eth *LightEthereum) HeaderByHash(ctx context.Context, blockhash common.Hash) (*types.Header, error) {
|
||||
if h := eth.blockchain.GetHeaderByHash(blockhash); h != nil {
|
||||
return h, nil
|
||||
}
|
||||
|
||||
return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
|
||||
header, err := b.HeaderByNumber(ctx, blockNr)
|
||||
if header == nil || err != nil {
|
||||
func (eth *LightEthereum) HeaderByNumber(ctx context.Context, blocknum *big.Int) (*types.Header, error) {
|
||||
if blocknum == nil {
|
||||
return eth.blockchain.CurrentHeader(), nil
|
||||
}
|
||||
h, err := eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blocknum.Uint64()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.GetBlock(ctx, header.Hash())
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
|
||||
header, err := b.HeaderByNumber(ctx, blockNr)
|
||||
if header == nil || err != nil {
|
||||
return nil, nil, err
|
||||
if h == nil {
|
||||
return nil, ErrBlockNotFound
|
||||
}
|
||||
return light.NewLightState(light.StateTrieID(header), b.eth.odr), header, nil
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
|
||||
return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
|
||||
return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
|
||||
return b.eth.blockchain.GetTdByHash(blockHash)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (vm.Environment, func() error, error) {
|
||||
stateDb := state.(*light.LightState).Copy()
|
||||
addr := msg.From()
|
||||
from, err := stateDb.GetOrNewStateObject(ctx, addr)
|
||||
func (eth *LightEthereum) BlockByNumber(ctx context.Context, blocknum *big.Int) (*types.Block, error) {
|
||||
header, err := eth.HeaderByNumber(ctx, blocknum)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
from.SetBalance(common.MaxBig)
|
||||
env := light.NewEnv(ctx, stateDb, b.eth.chainConfig, b.eth.blockchain, msg, header, vm.Config{})
|
||||
return env, env.Error, nil
|
||||
return eth.BlockByHash(ctx, header.Hash())
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
|
||||
return b.eth.txPool.Add(ctx, signedTx)
|
||||
func (eth *LightEthereum) BlockByHash(ctx context.Context, blockhash common.Hash) (*types.Block, error) {
|
||||
return eth.blockchain.GetBlockByHash(ctx, blockhash)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
|
||||
b.eth.txPool.RemoveTx(txHash)
|
||||
func (eth *LightEthereum) BlockReceipts(ctx context.Context, blockhash common.Hash, blocknum uint64) ([]*types.Receipt, error) {
|
||||
return light.GetBlockReceipts(ctx, eth.odr, blockhash, blocknum)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetPoolTransactions() types.Transactions {
|
||||
return b.eth.txPool.GetTransactions()
|
||||
func (eth *LightEthereum) BlockTD(blockHash common.Hash) *big.Int {
|
||||
return eth.blockchain.GetTdByHash(blockHash)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
|
||||
return b.eth.txPool.GetTransaction(txHash)
|
||||
func (eth *LightEthereum) TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) {
|
||||
if tx = eth.txPool.GetTransaction(txHash); tx != nil {
|
||||
return tx, true, nil
|
||||
}
|
||||
return nil, false, ErrTxNotFound
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
|
||||
return b.eth.txPool.GetNonce(ctx, addr)
|
||||
func (eth *LightEthereum) TransactionInBlock(ctx context.Context, blockhash common.Hash, index uint) (*types.Transaction, error) {
|
||||
b, err := eth.blockchain.GetBlockByHash(ctx, blockhash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if index >= uint(len(b.Transactions())) {
|
||||
return nil, fmt.Errorf("transaction index out of range")
|
||||
}
|
||||
return b.Transactions()[index], nil
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) Stats() (pending int, queued int) {
|
||||
return b.eth.txPool.Stats(), 0
|
||||
func (eth *LightEthereum) TransactionReceipt(ctx context.Context, txhash common.Hash) (*types.Receipt, error) {
|
||||
return nil, ErrReceiptNotFound
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
|
||||
return b.eth.txPool.Content()
|
||||
func (eth *LightEthereum) TransactionCount(ctx context.Context, blockhash common.Hash) (uint, error) {
|
||||
b, err := eth.blockchain.GetBlockByHash(ctx, blockhash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint(len(b.Transactions())), nil
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) Downloader() *downloader.Downloader {
|
||||
return b.eth.Downloader()
|
||||
func (eth *LightEthereum) BalanceAt(ctx context.Context, addr common.Address, blocknum *big.Int) (*big.Int, error) {
|
||||
st, err := eth.state(ctx, blocknum)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return st.GetBalance(ctx, addr)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) ProtocolVersion() int {
|
||||
return b.eth.LesVersion() + 10000
|
||||
func (eth *LightEthereum) CodeAt(ctx context.Context, addr common.Address, blocknum *big.Int) ([]byte, error) {
|
||||
st, err := eth.state(ctx, blocknum)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return st.GetCode(ctx, addr)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
|
||||
return b.gpo.SuggestPrice(ctx)
|
||||
func (eth *LightEthereum) NonceAt(ctx context.Context, addr common.Address, blocknum *big.Int) (uint64, error) {
|
||||
st, err := eth.state(ctx, blocknum)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return st.GetNonce(ctx, addr)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) ChainDb() ethdb.Database {
|
||||
return b.eth.chainDb
|
||||
func (eth *LightEthereum) StorageAt(ctx context.Context, addr common.Address, key common.Hash, blocknum *big.Int) ([]byte, error) {
|
||||
st, err := eth.state(ctx, blocknum)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err := st.GetState(ctx, addr, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v[:], nil
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) EventMux() *event.TypeMux {
|
||||
return b.eth.eventMux
|
||||
func (eth *LightEthereum) PendingCodeAt(ctx context.Context, addr common.Address) ([]byte, error) {
|
||||
// TODO(fjl): find a way to get rid of PendingCodeAt here. CodeAt is a bad emulation
|
||||
// of PendingCodeAt because it forces users to wait for transactions to get mined.
|
||||
return eth.CodeAt(ctx, addr, nil)
|
||||
}
|
||||
|
||||
func (b *LesApiBackend) AccountManager() *accounts.Manager {
|
||||
return b.eth.accountManager
|
||||
func (eth *LightEthereum) state(ctx context.Context, blocknum *big.Int) (*light.LightState, error) {
|
||||
header, err := eth.HeaderByNumber(ctx, blocknum)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return light.NewLightState(light.StateTrieID(header), eth.odr), nil
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) SendTransaction(ctx context.Context, signedTx *types.Transaction) error {
|
||||
return eth.txPool.Add(ctx, signedTx)
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) RemoveTransaction(txHash common.Hash) {
|
||||
eth.txPool.RemoveTx(txHash)
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) PendingTransactions() []*types.Transaction {
|
||||
return eth.txPool.GetTransactions()
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) PendingNonceAt(ctx context.Context, addr common.Address) (uint64, error) {
|
||||
return eth.txPool.GetNonce(ctx, addr)
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
|
||||
return eth.protocolManager.downloader.Progress(), nil
|
||||
}
|
||||
|
||||
// ChainConfig returns the active chain configuration.
|
||||
func (eth *LightEthereum) ChainConfig() *params.ChainConfig {
|
||||
return eth.chainConfig
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) ProtocolVersion() int {
|
||||
return int(eth.protocolManager.SubProtocols[0].Version) + 10000
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
||||
return eth.gpo.SuggestPrice(ctx)
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) AccountManager() *accounts.Manager {
|
||||
return eth.accountManager
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) ResetHeadBlock(number uint64) {
|
||||
eth.blockchain.SetHead(number)
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) EstimateGas(ctx context.Context, call ethereum.CallMsg) (usedGas *big.Int, err error) {
|
||||
_, gas, err := eth.callContract(ctx, call, nil)
|
||||
return gas, err
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) CallContract(ctx context.Context, call ethereum.CallMsg, blocknum *big.Int) ([]byte, error) {
|
||||
val, _, err := eth.callContract(ctx, call, blocknum)
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (eth *LightEthereum) callContract(ctx context.Context, call ethereum.CallMsg, blocknum *big.Int) ([]byte, *big.Int, error) {
|
||||
var head *types.Header
|
||||
if blocknum == nil {
|
||||
head = eth.blockchain.CurrentHeader()
|
||||
} else if head = eth.blockchain.GetHeaderByNumber(blocknum.Uint64()); head == nil {
|
||||
return nil, nil, ErrBlockNotFound
|
||||
}
|
||||
state := light.NewLightState(light.StateTrieID(head), eth.odr)
|
||||
return core.ApplyCallMessage(call, func(msg core.Message) vm.Environment {
|
||||
return light.NewEnv(ctx, state, eth.chainConfig, eth.blockchain, msg, head, vm.Config{})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ type LightEthereum struct {
|
|||
// DB interfaces
|
||||
chainDb ethdb.Database // Block chain database
|
||||
|
||||
ApiBackend *LesApiBackend
|
||||
gpo *gasprice.LightPriceOracle
|
||||
|
||||
eventMux *event.TypeMux
|
||||
pow *ethash.Ethash
|
||||
|
@ -120,8 +120,8 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
eth.ApiBackend = &LesApiBackend{eth, nil}
|
||||
eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend)
|
||||
eth.gpo = gasprice.NewLightPriceOracle(eth)
|
||||
|
||||
return eth, nil
|
||||
}
|
||||
|
||||
|
@ -149,9 +149,13 @@ func (s *LightDummyAPI) Mining() bool {
|
|||
|
||||
// APIs returns the collection of RPC services the ethereum package offers.
|
||||
// NOTE, some of these services probably need to be moved to somewhere else.
|
||||
func (s *LightEthereum) APIs() []rpc.API {
|
||||
return append(ethapi.GetAPIs(s.ApiBackend, s.solcPath), []rpc.API{
|
||||
func (eth *LightEthereum) APIs() []rpc.API {
|
||||
return append(ethapi.GetAPIs(eth, eth.solcPath), []rpc.API{
|
||||
{
|
||||
Namespace: "debug",
|
||||
Version: "1.0",
|
||||
Service: ethapi.NewPrivateDebugAPI(eth, eth.chainDb),
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: &LightDummyAPI{},
|
||||
|
@ -159,17 +163,22 @@ func (s *LightEthereum) APIs() []rpc.API {
|
|||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
|
||||
Service: downloader.NewPublicDownloaderAPI(eth.protocolManager.downloader, eth.eventMux),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: filters.NewPublicFilterAPI(s.ApiBackend, true),
|
||||
Service: filters.NewPublicFilterAPI(eth, eth.eventMux, eth.chainDb, true),
|
||||
Public: true,
|
||||
}, {
|
||||
Namespace: "txpool",
|
||||
Version: "1.0",
|
||||
Service: ethapi.TxPoolDebugAPI{Pool: eth.txPool},
|
||||
},
|
||||
{
|
||||
Namespace: "net",
|
||||
Version: "1.0",
|
||||
Service: s.netRPCService,
|
||||
Service: eth.netRPCService,
|
||||
Public: true,
|
||||
},
|
||||
}...)
|
||||
|
@ -179,11 +188,6 @@ func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
|
|||
s.blockchain.ResetWithGenesisBlock(gb)
|
||||
}
|
||||
|
||||
func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
|
||||
func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
|
||||
func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
||||
func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
||||
|
||||
// Protocols implements node.Service, returning all the currently configured
|
||||
// network protocols to start.
|
||||
func (s *LightEthereum) Protocols() []p2p.Protocol {
|
||||
|
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/les/flowcontrol"
|
||||
"github.com/ethereum/go-ethereum/light"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
|
@ -44,8 +45,8 @@ type LesServer struct {
|
|||
defParams *flowcontrol.ServerParams
|
||||
}
|
||||
|
||||
func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
||||
pm, err := NewProtocolManager(config.ChainConfig, false, config.NetworkId, eth.EventMux(), eth.Pow(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil)
|
||||
func NewLesServer(eth *eth.Ethereum, mux *event.TypeMux, config *eth.Config) (*LesServer, error) {
|
||||
pm, err := NewProtocolManager(config.ChainConfig, false, config.NetworkId, mux, eth.Pow(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -325,12 +325,11 @@ func (pool *TxPool) Stop() {
|
|||
}
|
||||
|
||||
// Stats returns the number of currently pending (locally created) transactions
|
||||
func (pool *TxPool) Stats() (pending int) {
|
||||
func (pool *TxPool) Stats() (pending int, queued int) {
|
||||
pool.mu.RLock()
|
||||
defer pool.mu.RUnlock()
|
||||
|
||||
pending = len(pool.pending)
|
||||
return
|
||||
return len(pool.pending), 0
|
||||
}
|
||||
|
||||
// validateTx checks whether a transaction is valid according to the consensus rules.
|
||||
|
|
|
@ -73,7 +73,8 @@ func (ec *EthereumClient) GetHeaderByNumber(ctx *Context, number int64) (*Header
|
|||
|
||||
// GetTransactionByHash returns the transaction with the given hash.
|
||||
func (ec *EthereumClient) GetTransactionByHash(ctx *Context, hash *Hash) (*Transaction, error) {
|
||||
tx, err := ec.client.TransactionByHash(ctx.context, hash.hash)
|
||||
// TODO(karalabe): use isPending result
|
||||
tx, _, err := ec.client.TransactionByHash(ctx.context, hash.hash)
|
||||
return &Transaction{tx}, err
|
||||
}
|
||||
|
||||
|
|
|
@ -154,9 +154,9 @@ func (tx *Transaction) GetTo() *Address {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (tx *Transaction) WithSignature(sig []byte) (*Transaction, error) {
|
||||
t, err := tx.tx.WithSignature(types.HomesteadSigner{}, sig)
|
||||
return &Transaction{t}, err
|
||||
func (tx *Transaction) WithSignature(sig []byte) *Transaction {
|
||||
t := tx.tx.WithSignature(types.HomesteadSigner{}, sig)
|
||||
return &Transaction{t}
|
||||
}
|
||||
|
||||
// Transactions represents a slice of transactions.
|
||||
|
|
Loading…
Reference in New Issue