1// Copyright 2015 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
17package eth
18
19import (
20	"context"
21	"errors"
22	"math/big"
23	"time"
24
25	"github.com/ethereum/go-ethereum"
26	"github.com/ethereum/go-ethereum/accounts"
27	"github.com/ethereum/go-ethereum/common"
28	"github.com/ethereum/go-ethereum/consensus"
29	"github.com/ethereum/go-ethereum/core"
30	"github.com/ethereum/go-ethereum/core/bloombits"
31	"github.com/ethereum/go-ethereum/core/rawdb"
32	"github.com/ethereum/go-ethereum/core/state"
33	"github.com/ethereum/go-ethereum/core/types"
34	"github.com/ethereum/go-ethereum/core/vm"
35	"github.com/ethereum/go-ethereum/eth/gasprice"
36	"github.com/ethereum/go-ethereum/ethdb"
37	"github.com/ethereum/go-ethereum/event"
38	"github.com/ethereum/go-ethereum/miner"
39	"github.com/ethereum/go-ethereum/params"
40	"github.com/ethereum/go-ethereum/rpc"
41)
42
43// EthAPIBackend implements ethapi.Backend for full nodes
44type EthAPIBackend struct {
45	extRPCEnabled       bool
46	allowUnprotectedTxs bool
47	eth                 *Ethereum
48	gpo                 *gasprice.Oracle
49}
50
51// ChainConfig returns the active chain configuration.
52func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
53	return b.eth.blockchain.Config()
54}
55
56func (b *EthAPIBackend) CurrentBlock() *types.Block {
57	return b.eth.blockchain.CurrentBlock()
58}
59
60func (b *EthAPIBackend) SetHead(number uint64) {
61	b.eth.handler.downloader.Cancel()
62	b.eth.blockchain.SetHead(number)
63}
64
65func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
66	// Pending block is only known by the miner
67	if number == rpc.PendingBlockNumber {
68		block := b.eth.miner.PendingBlock()
69		return block.Header(), nil
70	}
71	// Otherwise resolve and return the block
72	if number == rpc.LatestBlockNumber {
73		return b.eth.blockchain.CurrentBlock().Header(), nil
74	}
75	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
76}
77
78func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
79	if blockNr, ok := blockNrOrHash.Number(); ok {
80		return b.HeaderByNumber(ctx, blockNr)
81	}
82	if hash, ok := blockNrOrHash.Hash(); ok {
83		header := b.eth.blockchain.GetHeaderByHash(hash)
84		if header == nil {
85			return nil, errors.New("header for hash not found")
86		}
87		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
88			return nil, errors.New("hash is not currently canonical")
89		}
90		return header, nil
91	}
92	return nil, errors.New("invalid arguments; neither block nor hash specified")
93}
94
95func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
96	return b.eth.blockchain.GetHeaderByHash(hash), nil
97}
98
99func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
100	// Pending block is only known by the miner
101	if number == rpc.PendingBlockNumber {
102		block := b.eth.miner.PendingBlock()
103		return block, nil
104	}
105	// Otherwise resolve and return the block
106	if number == rpc.LatestBlockNumber {
107		return b.eth.blockchain.CurrentBlock(), nil
108	}
109	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
110}
111
112func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
113	return b.eth.blockchain.GetBlockByHash(hash), nil
114}
115
116func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
117	if blockNr, ok := blockNrOrHash.Number(); ok {
118		return b.BlockByNumber(ctx, blockNr)
119	}
120	if hash, ok := blockNrOrHash.Hash(); ok {
121		header := b.eth.blockchain.GetHeaderByHash(hash)
122		if header == nil {
123			return nil, errors.New("header for hash not found")
124		}
125		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
126			return nil, errors.New("hash is not currently canonical")
127		}
128		block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
129		if block == nil {
130			return nil, errors.New("header found, but block body is missing")
131		}
132		return block, nil
133	}
134	return nil, errors.New("invalid arguments; neither block nor hash specified")
135}
136
137func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
138	return b.eth.miner.PendingBlockAndReceipts()
139}
140
141func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
142	// Pending state is only known by the miner
143	if number == rpc.PendingBlockNumber {
144		block, state := b.eth.miner.Pending()
145		return state, block.Header(), nil
146	}
147	// Otherwise resolve the block number and return its state
148	header, err := b.HeaderByNumber(ctx, number)
149	if err != nil {
150		return nil, nil, err
151	}
152	if header == nil {
153		return nil, nil, errors.New("header not found")
154	}
155	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
156	return stateDb, header, err
157}
158
159func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
160	if blockNr, ok := blockNrOrHash.Number(); ok {
161		return b.StateAndHeaderByNumber(ctx, blockNr)
162	}
163	if hash, ok := blockNrOrHash.Hash(); ok {
164		header, err := b.HeaderByHash(ctx, hash)
165		if err != nil {
166			return nil, nil, err
167		}
168		if header == nil {
169			return nil, nil, errors.New("header for hash not found")
170		}
171		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
172			return nil, nil, errors.New("hash is not currently canonical")
173		}
174		stateDb, err := b.eth.BlockChain().StateAt(header.Root)
175		return stateDb, header, err
176	}
177	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
178}
179
180func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
181	return b.eth.blockchain.GetReceiptsByHash(hash), nil
182}
183
184func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
185	db := b.eth.ChainDb()
186	number := rawdb.ReadHeaderNumber(db, hash)
187	if number == nil {
188		return nil, errors.New("failed to get block number from hash")
189	}
190	logs := rawdb.ReadLogs(db, hash, *number, b.eth.blockchain.Config())
191	if logs == nil {
192		return nil, errors.New("failed to get logs for block")
193	}
194	return logs, nil
195}
196
197func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
198	if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil {
199		return b.eth.blockchain.GetTd(hash, header.Number.Uint64())
200	}
201	return nil
202}
203
204func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
205	vmError := func() error { return nil }
206	if vmConfig == nil {
207		vmConfig = b.eth.blockchain.GetVMConfig()
208	}
209	txContext := core.NewEVMTxContext(msg)
210	context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
211	return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil
212}
213
214func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
215	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
216}
217
218func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
219	return b.eth.miner.SubscribePendingLogs(ch)
220}
221
222func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
223	return b.eth.BlockChain().SubscribeChainEvent(ch)
224}
225
226func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
227	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
228}
229
230func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
231	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
232}
233
234func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
235	return b.eth.BlockChain().SubscribeLogsEvent(ch)
236}
237
238func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
239	return b.eth.txPool.AddLocal(signedTx)
240}
241
242func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
243	pending := b.eth.txPool.Pending(false)
244	var txs types.Transactions
245	for _, batch := range pending {
246		txs = append(txs, batch...)
247	}
248	return txs, nil
249}
250
251func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
252	return b.eth.txPool.Get(hash)
253}
254
255func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
256	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
257	return tx, blockHash, blockNumber, index, nil
258}
259
260func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
261	return b.eth.txPool.Nonce(addr), nil
262}
263
264func (b *EthAPIBackend) Stats() (pending int, queued int) {
265	return b.eth.txPool.Stats()
266}
267
268func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
269	return b.eth.TxPool().Content()
270}
271
272func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
273	return b.eth.TxPool().ContentFrom(addr)
274}
275
276func (b *EthAPIBackend) TxPool() *core.TxPool {
277	return b.eth.TxPool()
278}
279
280func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
281	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
282}
283
284func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress {
285	return b.eth.Downloader().Progress()
286}
287
288func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
289	return b.gpo.SuggestTipCap(ctx)
290}
291
292func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
293	return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
294}
295
296func (b *EthAPIBackend) ChainDb() ethdb.Database {
297	return b.eth.ChainDb()
298}
299
300func (b *EthAPIBackend) EventMux() *event.TypeMux {
301	return b.eth.EventMux()
302}
303
304func (b *EthAPIBackend) AccountManager() *accounts.Manager {
305	return b.eth.AccountManager()
306}
307
308func (b *EthAPIBackend) ExtRPCEnabled() bool {
309	return b.extRPCEnabled
310}
311
312func (b *EthAPIBackend) UnprotectedAllowed() bool {
313	return b.allowUnprotectedTxs
314}
315
316func (b *EthAPIBackend) RPCGasCap() uint64 {
317	return b.eth.config.RPCGasCap
318}
319
320func (b *EthAPIBackend) RPCEVMTimeout() time.Duration {
321	return b.eth.config.RPCEVMTimeout
322}
323
324func (b *EthAPIBackend) RPCTxFeeCap() float64 {
325	return b.eth.config.RPCTxFeeCap
326}
327
328func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
329	sections, _, _ := b.eth.bloomIndexer.Sections()
330	return params.BloomBitsBlocks, sections
331}
332
333func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
334	for i := 0; i < bloomFilterThreads; i++ {
335		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
336	}
337}
338
339func (b *EthAPIBackend) Engine() consensus.Engine {
340	return b.eth.engine
341}
342
343func (b *EthAPIBackend) CurrentHeader() *types.Header {
344	return b.eth.blockchain.CurrentHeader()
345}
346
347func (b *EthAPIBackend) Miner() *miner.Miner {
348	return b.eth.Miner()
349}
350
351func (b *EthAPIBackend) StartMining(threads int) error {
352	return b.eth.StartMining(threads)
353}
354
355func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error) {
356	return b.eth.StateAtBlock(block, reexec, base, checkLive, preferDisk)
357}
358
359func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {
360	return b.eth.stateAtTransaction(block, txIndex, reexec)
361}
362