aboutsummaryrefslogtreecommitdiffstats
path: root/eth
diff options
context:
space:
mode:
Diffstat (limited to 'eth')
-rw-r--r--eth/api.go50
-rw-r--r--eth/backend.go22
-rw-r--r--eth/bind.go110
-rw-r--r--eth/filters/filter_system.go2
4 files changed, 161 insertions, 23 deletions
diff --git a/eth/api.go b/eth/api.go
index a0b1f8ac2..bd8179962 100644
--- a/eth/api.go
+++ b/eth/api.go
@@ -18,6 +18,7 @@ package eth
import (
"bytes"
+ "encoding/hex"
"encoding/json"
"errors"
"fmt"
@@ -51,6 +52,15 @@ import (
"golang.org/x/net/context"
)
+// errNoCode is returned by call and transact operations for which the requested
+// recipient contract to operate on does not exist in the state db or does not
+// have any code associated with it (i.e. suicided).
+//
+// Please note, this error string is part of the RPC API and is expected by the
+// native contract bindings to signal this particular error. Do not change this
+// as it will break all dependent code!
+var errNoCode = errors.New("no contract code at given address")
+
const defaultGas = uint64(90000)
// blockByNumber is a commonly used helper function which retrieves and returns
@@ -97,8 +107,11 @@ type PublicEthereumAPI struct {
}
// NewPublicEthereumAPI creates a new Ethereum protocol API.
-func NewPublicEthereumAPI(e *Ethereum, gpo *GasPriceOracle) *PublicEthereumAPI {
- return &PublicEthereumAPI{e, gpo}
+func NewPublicEthereumAPI(e *Ethereum) *PublicEthereumAPI {
+ return &PublicEthereumAPI{
+ e: e,
+ gpo: e.gpo,
+ }
}
// GasPrice returns a suggestion for a gas price.
@@ -439,6 +452,16 @@ func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error)
return common.Address{}, err
}
+func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (common.Address, error) {
+ hexkey, err := hex.DecodeString(privkey)
+ if err != nil {
+ return common.Address{}, err
+ }
+
+ acc, err := s.am.ImportECDSA(crypto.ToECDSA(hexkey), password)
+ return acc.Address, err
+}
+
// UnlockAccount will unlock the account associated with the given address with
// the given password for duration seconds. If duration is nil it will use a
// default of 300 seconds. It returns an indication if the account was unlocked.
@@ -694,6 +717,12 @@ func (s *PublicBlockChainAPI) doCall(args CallArgs, blockNr rpc.BlockNumber) (st
}
stateDb = stateDb.Copy()
+ // If there's no code to interact with, respond with an appropriate error
+ if args.To != nil {
+ if code := stateDb.GetCode(*args.To); len(code) == 0 {
+ return "0x", nil, errNoCode
+ }
+ }
// Retrieve the account state object to interact with
var from *state.StateObject
if args.From == (common.Address{}) {
@@ -888,18 +917,17 @@ type PublicTransactionPoolAPI struct {
}
// NewPublicTransactionPoolAPI creates a new RPC service with methods specific for the transaction pool.
-func NewPublicTransactionPoolAPI(e *Ethereum, gpo *GasPriceOracle) *PublicTransactionPoolAPI {
+func NewPublicTransactionPoolAPI(e *Ethereum) *PublicTransactionPoolAPI {
api := &PublicTransactionPoolAPI{
- eventMux: e.EventMux(),
- gpo: gpo,
- chainDb: e.ChainDb(),
- bc: e.BlockChain(),
- am: e.AccountManager(),
- txPool: e.TxPool(),
- miner: e.Miner(),
+ eventMux: e.eventMux,
+ gpo: e.gpo,
+ chainDb: e.chainDb,
+ bc: e.blockchain,
+ am: e.accountManager,
+ txPool: e.txPool,
+ miner: e.miner,
pendingTxSubs: make(map[string]rpc.Subscription),
}
-
go api.subscriptionLoop()
return api
diff --git a/eth/backend.go b/eth/backend.go
index 76cf8783b..9722e9625 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -119,6 +119,7 @@ type Ethereum struct {
protocolManager *ProtocolManager
SolcPath string
solc *compiler.Solidity
+ gpo *GasPriceOracle
GpoMinGasPrice *big.Int
GpoMaxGasPrice *big.Int
@@ -260,6 +261,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
}
return nil, err
}
+ eth.gpo = NewGasPriceOracle(eth)
+
newPool := core.NewTxPool(eth.chainConfig, eth.EventMux(), eth.blockchain.State, eth.blockchain.GasLimit)
eth.txPool = newPool
@@ -276,34 +279,31 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, 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 {
- // share gas price oracle in API's
- gpo := NewGasPriceOracle(s)
-
return []rpc.API{
{
Namespace: "eth",
Version: "1.0",
- Service: NewPublicEthereumAPI(s, gpo),
+ Service: NewPublicEthereumAPI(s),
Public: true,
}, {
Namespace: "eth",
Version: "1.0",
- Service: NewPublicAccountAPI(s.AccountManager()),
+ Service: NewPublicAccountAPI(s.accountManager),
Public: true,
}, {
Namespace: "personal",
Version: "1.0",
- Service: NewPrivateAccountAPI(s.AccountManager()),
+ Service: NewPrivateAccountAPI(s.accountManager),
Public: false,
}, {
Namespace: "eth",
Version: "1.0",
- Service: NewPublicBlockChainAPI(s.chainConfig, s.BlockChain(), s.Miner(), s.ChainDb(), gpo, s.EventMux(), s.AccountManager()),
+ Service: NewPublicBlockChainAPI(s.chainConfig, s.blockchain, s.miner, s.chainDb, s.gpo, s.eventMux, s.accountManager),
Public: true,
}, {
Namespace: "eth",
Version: "1.0",
- Service: NewPublicTransactionPoolAPI(s, gpo),
+ Service: NewPublicTransactionPoolAPI(s),
Public: true,
}, {
Namespace: "eth",
@@ -313,7 +313,7 @@ func (s *Ethereum) APIs() []rpc.API {
}, {
Namespace: "eth",
Version: "1.0",
- Service: downloader.NewPublicDownloaderAPI(s.Downloader(), s.EventMux()),
+ Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
Public: true,
}, {
Namespace: "miner",
@@ -328,7 +328,7 @@ func (s *Ethereum) APIs() []rpc.API {
}, {
Namespace: "eth",
Version: "1.0",
- Service: filters.NewPublicFilterAPI(s.ChainDb(), s.EventMux()),
+ Service: filters.NewPublicFilterAPI(s.chainDb, s.eventMux),
Public: true,
}, {
Namespace: "admin",
@@ -351,7 +351,7 @@ func (s *Ethereum) APIs() []rpc.API {
}, {
Namespace: "admin",
Version: "1.0",
- Service: ethreg.NewPrivateRegistarAPI(s.chainConfig, s.BlockChain(), s.ChainDb(), s.TxPool(), s.AccountManager()),
+ Service: ethreg.NewPrivateRegistarAPI(s.chainConfig, s.blockchain, s.chainDb, s.txPool, s.accountManager),
},
}
}
diff --git a/eth/bind.go b/eth/bind.go
new file mode 100644
index 000000000..3a3eca062
--- /dev/null
+++ b/eth/bind.go
@@ -0,0 +1,110 @@
+// 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 eth
+
+import (
+ "math/big"
+
+ "github.com/ethereum/go-ethereum/accounts/abi/bind"
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/rlp"
+ "github.com/ethereum/go-ethereum/rpc"
+)
+
+// 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 *PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
+ bcapi *PublicBlockChainAPI // Wrapper around the blockchain to access chain data
+ txapi *PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
+}
+
+// NewContractBackend creates a new native contract backend using an existing
+// Etheruem object.
+func NewContractBackend(eth *Ethereum) *ContractBackend {
+ return &ContractBackend{
+ eapi: NewPublicEthereumAPI(eth),
+ bcapi: NewPublicBlockChainAPI(eth.chainConfig, eth.blockchain, eth.miner, eth.chainDb, eth.gpo, eth.eventMux, eth.accountManager),
+ txapi: NewPublicTransactionPoolAPI(eth),
+ }
+}
+
+// ContractCall implements bind.ContractCaller executing an Ethereum contract
+// call with the specified data as the input. The pending flag requests execution
+// against the pending block, not the stable head of the chain.
+func (b *ContractBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) {
+ // Convert the input args to the API spec
+ args := CallArgs{
+ To: &contract,
+ Data: common.ToHex(data),
+ }
+ block := rpc.LatestBlockNumber
+ if pending {
+ block = rpc.PendingBlockNumber
+ }
+ // Execute the call and convert the output back to Go types
+ out, err := b.bcapi.Call(args, block)
+ if err == errNoCode {
+ err = bind.ErrNoCode
+ }
+ return common.FromHex(out), err
+}
+
+// PendingAccountNonce implements bind.ContractTransactor retrieving the current
+// pending nonce associated with an account.
+func (b *ContractBackend) PendingAccountNonce(account common.Address) (uint64, error) {
+ out, err := b.txapi.GetTransactionCount(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() (*big.Int, error) {
+ return b.eapi.GasPrice(), nil
+}
+
+// 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) EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) {
+ out, err := b.bcapi.EstimateGas(CallArgs{
+ From: sender,
+ To: contract,
+ Value: *rpc.NewHexNumber(value),
+ Data: common.ToHex(data),
+ })
+ if err == errNoCode {
+ err = bind.ErrNoCode
+ }
+ return out.BigInt(), err
+}
+
+// SendTransaction implements bind.ContractTransactor injects the transaction
+// into the pending pool for execution.
+func (b *ContractBackend) SendTransaction(tx *types.Transaction) error {
+ raw, _ := rlp.EncodeToBytes(tx)
+ _, err := b.txapi.SendRawTransaction(common.ToHex(raw))
+ return err
+}
diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go
index 29968530a..4343dfa21 100644
--- a/eth/filters/filter_system.go
+++ b/eth/filters/filter_system.go
@@ -164,7 +164,7 @@ func (fs *FilterSystem) filterLoop() {
fs.filterMu.RLock()
for _, filter := range fs.logFilters {
if filter.LogCallback != nil && !filter.created.After(event.Time) {
- for _, removedLog := range ev.Logs {
+ for _, removedLog := range filter.FilterLogs(ev.Logs) {
filter.LogCallback(removedLog, true)
}
}