Skip to content

Commit 8d9141e

Browse files
committed
ethereum: add new Go API interfaces
1 parent b0d9f73 commit 8d9141e

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

interfaces.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package ethereum defines interfaces for interacting with Ethereum.
18+
package ethereum
19+
20+
import (
21+
"math/big"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/types"
25+
"github.com/ethereum/go-ethereum/core/vm"
26+
"golang.org/x/net/context"
27+
)
28+
29+
// TODO: move subscription to package event
30+
31+
// Subscription represents an event subscription where events are
32+
// delivered on a data channel.
33+
type Subscription interface {
34+
// Unsubscribe cancels the sending of events to the data channel
35+
// and closes the error channel.
36+
Unsubscribe()
37+
// Err returns the subscription error channel. The error channel receives
38+
// a value if there is an issue with the subscription (e.g. the network connection
39+
// delivering the events has been closed). Only one value will ever be sent.
40+
// The error channel is closed by Unsubscribe.
41+
Err() <-chan error
42+
}
43+
44+
// ChainReader provides access to the blockchain. The methods in this interface access raw
45+
// data from either the canonical chain (when requesting by block number) or any
46+
// blockchain fork that was previously downloaded and processed by the node. The block
47+
// number argument can be nil to select the latest canonical block. Reading block headers
48+
// should be preferred over full blocks whenever possible.
49+
type ChainReader interface {
50+
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
51+
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
52+
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
53+
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
54+
TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)
55+
TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)
56+
TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, error)
57+
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
58+
}
59+
60+
// ChainStateReader wraps access to the state trie of the canonical blockchain. Note that
61+
// implementations of the interface may be unable to return state values for old blocks.
62+
// In many cases, using CallContract can be preferable to reading raw contract storage.
63+
type ChainStateReader interface {
64+
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
65+
StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)
66+
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
67+
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
68+
}
69+
70+
// A ChainHeadEventer returns notifications whenever the canonical head block is updated.
71+
type ChainHeadEventer interface {
72+
SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (Subscription, error)
73+
}
74+
75+
// CallMsg contains parameters for contract calls.
76+
type CallMsg struct {
77+
From common.Address // the sender of the 'transaction'
78+
To *common.Address // the destination contract (nil for contract creation)
79+
Gas *big.Int // if nil, the call executes with near-infinite gas
80+
GasPrice *big.Int // wei <-> gas exchange ratio
81+
Value *big.Int // amount of wei sent along with the call
82+
Data []byte // input data, usually an ABI-encoded contract method invocation
83+
}
84+
85+
// A ContractCaller provides contract calls, essentially transactions that are executed by
86+
// the EVM but not mined into the blockchain. ContractCall is a low-level method to
87+
// execute such calls. For applications which are structured around specific contracts,
88+
// the abigen tool provides a nicer, properly typed way to perform calls.
89+
type ContractCaller interface {
90+
CallContract(ctx context.Context, call CallMsg, blockNumber *big.Int) ([]byte, error)
91+
}
92+
93+
// FilterQuery contains options for contact log filtering.
94+
type FilterQuery struct {
95+
FromBlock *big.Int // beginning of the queried range, nil means genesis block
96+
ToBlock *big.Int // end of the range, nil means latest block
97+
Addresses []common.Address // restricts matches to events created by specific contracts
98+
99+
// The Topic list restricts matches to particular event topics. Each event has a list
100+
// of topics. Topics matches a prefix of that list. An empty element slice matches any
101+
// topic. Non-empty elements represent an alternative that matches any of the
102+
// contained topics.
103+
//
104+
// Examples:
105+
// {} or nil matches any topic list
106+
// {{A}} matches topic A in first position
107+
// {{}, {B}} matches any topic in first position, B in second position
108+
// {{A}}, {B}} matches topic A in first position, B in second position
109+
// {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
110+
Topics [][]common.Hash
111+
}
112+
113+
// LogFilterer provides access to contract log events using a one-off query or continuous
114+
// event subscription.
115+
type LogFilterer interface {
116+
FilterLogs(ctx context.Context, q FilterQuery) ([]vm.Log, error)
117+
SubscribeFilterLogs(ctx context.Context, q FilterQuery, ch chan<- vm.Log) (Subscription, error)
118+
}
119+
120+
// TransactionSender wraps transaction sending. The SendTransaction method injects a
121+
// signed transaction into the pending transaction pool for execution. If the transaction
122+
// was a contract creation, the TransactionReceipt method can be used to retrieve the
123+
// contract address after the transaction has been mined.
124+
//
125+
// The transaction must be signed and have a valid nonce to be included. Consumers of the
126+
// API can use package accounts to maintain local private keys and need can retrieve the
127+
// next available nonce using PendingNonceAt.
128+
type TransactionSender interface {
129+
SendTransaction(ctx context.Context, tx *types.Transaction) error
130+
}
131+
132+
// GasPricer wraps the gas price oracle, which monitors the blockchain to determine the
133+
// optimal gas price given current fee market conditions.
134+
type GasPricer interface {
135+
SuggestGasPrice(ctx context.Context) (*big.Int, error)
136+
}
137+
138+
// A PendingStateReader provides access to the pending state, which is the result of all
139+
// known executable transactions which have not yet been included in the blockchain. It is
140+
// commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value
141+
// transfers) initiated by the user. The PendingNonceAt operation is a good way to
142+
// retrieve the next available transaction nonce for a specific account.
143+
type PendingStateReader interface {
144+
PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error)
145+
PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error)
146+
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
147+
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
148+
PendingTransactionCount(ctx context.Context) (uint, error)
149+
}
150+
151+
// PendingContractCaller can be used to perform calls against the pending state.
152+
type PendingContractCaller interface {
153+
PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error)
154+
}
155+
156+
// GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a
157+
// specific transaction based on the pending state. There is no guarantee that this is the
158+
// true gas limit requirement as other transactions may be added or removed by miners, but
159+
// it should provide a basis for setting a reasonable default.
160+
type GasEstimator interface {
161+
EstimateGas(ctx context.Context, call CallMsg) (usedGas *big.Int, err error)
162+
}
163+
164+
// A PendingStateEventer provides access to real time notifications about changes to the
165+
// pending state.
166+
type PendingStateEventer interface {
167+
SubscribePendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (Subscription, error)
168+
}

0 commit comments

Comments
 (0)