api_backend.go 10.1 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4 5
// 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
6 7 8
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10 11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU Lesser General Public License for more details.
13
//
14 15
// 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/>.
16 17 18 19

package eth

import (
20
	"context"
21
	"errors"
22 23 24 25
	"math/big"

	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/common"
26
	"github.com/ethereum/go-ethereum/common/math"
27
	"github.com/ethereum/go-ethereum/core"
28
	"github.com/ethereum/go-ethereum/core/bloombits"
29
	"github.com/ethereum/go-ethereum/core/rawdb"
30 31 32 33 34 35 36
	"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"
37
	"github.com/ethereum/go-ethereum/params"
38
	"github.com/ethereum/go-ethereum/rpc"
39 40
)

41 42
// EthAPIBackend implements ethapi.Backend for full nodes
type EthAPIBackend struct {
43 44 45
	extRPCEnabled bool
	eth           *Ethereum
	gpo           *gasprice.Oracle
46 47
}

48
// ChainConfig returns the active chain configuration.
49
func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
50
	return b.eth.blockchain.Config()
51 52
}

53
func (b *EthAPIBackend) CurrentBlock() *types.Block {
54 55 56
	return b.eth.blockchain.CurrentBlock()
}

57
func (b *EthAPIBackend) SetHead(number uint64) {
58
	b.eth.protocolManager.downloader.Cancel()
59 60 61
	b.eth.blockchain.SetHead(number)
}

62
func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
63
	// Pending block is only known by the miner
64
	if number == rpc.PendingBlockNumber {
65
		block := b.eth.miner.PendingBlock()
66
		return block.Header(), nil
67 68
	}
	// Otherwise resolve and return the block
69
	if number == rpc.LatestBlockNumber {
70
		return b.eth.blockchain.CurrentBlock().Header(), nil
71
	}
72
	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
	if blockNr, ok := blockNrOrHash.Number(); ok {
		return b.HeaderByNumber(ctx, blockNr)
	}
	if hash, ok := blockNrOrHash.Hash(); ok {
		header := b.eth.blockchain.GetHeaderByHash(hash)
		if header == nil {
			return nil, errors.New("header for hash not found")
		}
		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
			return nil, errors.New("hash is not currently canonical")
		}
		return header, nil
	}
	return nil, errors.New("invalid arguments; neither block nor hash specified")
}

92 93 94 95
func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
	return b.eth.blockchain.GetHeaderByHash(hash), nil
}

96
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
97
	// Pending block is only known by the miner
98
	if number == rpc.PendingBlockNumber {
99
		block := b.eth.miner.PendingBlock()
100 101 102
		return block, nil
	}
	// Otherwise resolve and return the block
103
	if number == rpc.LatestBlockNumber {
104 105
		return b.eth.blockchain.CurrentBlock(), nil
	}
106
	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
107 108
}

109 110 111 112
func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
	return b.eth.blockchain.GetBlockByHash(hash), nil
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
	if blockNr, ok := blockNrOrHash.Number(); ok {
		return b.BlockByNumber(ctx, blockNr)
	}
	if hash, ok := blockNrOrHash.Hash(); ok {
		header := b.eth.blockchain.GetHeaderByHash(hash)
		if header == nil {
			return nil, errors.New("header for hash not found")
		}
		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
			return nil, errors.New("hash is not currently canonical")
		}
		block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
		if block == nil {
			return nil, errors.New("header found, but block body is missing")
		}
		return block, nil
	}
	return nil, errors.New("invalid arguments; neither block nor hash specified")
}

134
func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
135
	// Pending state is only known by the miner
136
	if number == rpc.PendingBlockNumber {
137
		block, state := b.eth.miner.Pending()
138
		return state, block.Header(), nil
139 140
	}
	// Otherwise resolve the block number and return its state
141
	header, err := b.HeaderByNumber(ctx, number)
142
	if err != nil {
143
		return nil, nil, err
144
	}
145 146 147
	if header == nil {
		return nil, nil, errors.New("header not found")
	}
148
	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
149
	return stateDb, header, err
150 151
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
	if blockNr, ok := blockNrOrHash.Number(); ok {
		return b.StateAndHeaderByNumber(ctx, blockNr)
	}
	if hash, ok := blockNrOrHash.Hash(); ok {
		header, err := b.HeaderByHash(ctx, hash)
		if err != nil {
			return nil, nil, err
		}
		if header == nil {
			return nil, nil, errors.New("header for hash not found")
		}
		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
			return nil, nil, errors.New("hash is not currently canonical")
		}
		stateDb, err := b.eth.BlockChain().StateAt(header.Root)
		return stateDb, header, err
	}
	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
}

173
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
174
	return b.eth.blockchain.GetReceiptsByHash(hash), nil
175 176
}

177
func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
178
	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
179 180 181 182 183 184 185 186 187 188
	if receipts == nil {
		return nil, nil
	}
	logs := make([][]*types.Log, len(receipts))
	for i, receipt := range receipts {
		logs[i] = receipt.Logs
	}
	return logs, nil
}

189
func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
190 191 192
	return b.eth.blockchain.GetTdByHash(blockHash)
}

193
func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
194
	state.SetBalance(msg.From(), math.MaxBig256)
195
	vmError := func() error { return nil }
196

197
	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
198
	return vm.NewEVM(context, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
199 200
}

201
func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
202 203 204
	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
}

205
func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
206 207 208
	return b.eth.BlockChain().SubscribeChainEvent(ch)
}

209
func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
210 211 212
	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
}

213
func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
214 215 216
	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
}

217
func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
218 219 220
	return b.eth.BlockChain().SubscribeLogsEvent(ch)
}

221
func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
222
	return b.eth.txPool.AddLocal(signedTx)
223 224
}

225
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
226 227 228 229
	pending, err := b.eth.txPool.Pending()
	if err != nil {
		return nil, err
	}
230
	var txs types.Transactions
231
	for _, batch := range pending {
232 233
		txs = append(txs, batch...)
	}
234
	return txs, nil
235 236
}

237
func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
238
	return b.eth.txPool.Get(hash)
239 240
}

241 242 243 244 245
func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
	return tx, blockHash, blockNumber, index, nil
}

246
func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
247
	return b.eth.txPool.Nonce(addr), nil
248 249
}

250
func (b *EthAPIBackend) Stats() (pending int, queued int) {
251 252 253
	return b.eth.txPool.Stats()
}

254
func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
255 256 257
	return b.eth.TxPool().Content()
}

258 259
func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
260 261
}

262
func (b *EthAPIBackend) Downloader() *downloader.Downloader {
263 264 265
	return b.eth.Downloader()
}

266
func (b *EthAPIBackend) ProtocolVersion() int {
267 268 269
	return b.eth.EthVersion()
}

270
func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
271
	return b.gpo.SuggestPrice(ctx)
272 273
}

274
func (b *EthAPIBackend) ChainDb() ethdb.Database {
275 276 277
	return b.eth.ChainDb()
}

278
func (b *EthAPIBackend) EventMux() *event.TypeMux {
279 280 281
	return b.eth.EventMux()
}

282
func (b *EthAPIBackend) AccountManager() *accounts.Manager {
283 284
	return b.eth.AccountManager()
}
285

286 287 288 289
func (b *EthAPIBackend) ExtRPCEnabled() bool {
	return b.extRPCEnabled
}

290 291 292 293
func (b *EthAPIBackend) RPCGasCap() *big.Int {
	return b.eth.config.RPCGasCap
}

294
func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
295 296
	sections, _, _ := b.eth.bloomIndexer.Sections()
	return params.BloomBitsBlocks, sections
297 298
}

299
func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
300 301
	for i := 0; i < bloomFilterThreads; i++ {
		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
302 303
	}
}