api_backend.go 7.58 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 30 31 32 33 34 35
	"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"
36
	"github.com/ethereum/go-ethereum/params"
37
	"github.com/ethereum/go-ethereum/rpc"
38 39
)

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

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

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

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

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

74 75 76 77
func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
	return b.eth.blockchain.GetHeaderByHash(hash), nil
}

78
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
79 80
	// Pending block is only known by the miner
	if blockNr == rpc.PendingBlockNumber {
81
		block := b.eth.miner.PendingBlock()
82 83 84 85 86 87 88 89 90
		return block, nil
	}
	// Otherwise resolve and return the block
	if blockNr == rpc.LatestBlockNumber {
		return b.eth.blockchain.CurrentBlock(), nil
	}
	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
}

91
func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
92 93 94
	// Pending state is only known by the miner
	if blockNr == rpc.PendingBlockNumber {
		block, state := b.eth.miner.Pending()
95
		return state, block.Header(), nil
96 97
	}
	// Otherwise resolve the block number and return its state
98
	header, err := b.HeaderByNumber(ctx, blockNr)
99
	if err != nil {
100
		return nil, nil, err
101
	}
102 103 104
	if header == nil {
		return nil, nil, errors.New("header not found")
	}
105
	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
106
	return stateDb, header, err
107 108
}

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

113
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
114
	return b.eth.blockchain.GetReceiptsByHash(hash), nil
115 116
}

117
func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
118
	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
119 120 121 122 123 124 125 126 127 128
	if receipts == nil {
		return nil, nil
	}
	logs := make([][]*types.Log, len(receipts))
	for i, receipt := range receipts {
		logs[i] = receipt.Logs
	}
	return logs, nil
}

129
func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
130 131 132
	return b.eth.blockchain.GetTdByHash(blockHash)
}

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

137
	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
138
	return vm.NewEVM(context, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
139 140
}

141
func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
142 143 144
	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
}

145
func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
146 147 148
	return b.eth.BlockChain().SubscribeChainEvent(ch)
}

149
func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
150 151 152
	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
}

153
func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
154 155 156
	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
}

157
func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
158 159 160
	return b.eth.BlockChain().SubscribeLogsEvent(ch)
}

161
func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
162
	return b.eth.txPool.AddLocal(signedTx)
163 164
}

165
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
166 167 168 169
	pending, err := b.eth.txPool.Pending()
	if err != nil {
		return nil, err
	}
170
	var txs types.Transactions
171
	for _, batch := range pending {
172 173
		txs = append(txs, batch...)
	}
174
	return txs, nil
175 176
}

177
func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
178
	return b.eth.txPool.Get(hash)
179 180
}

181
func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
182 183 184
	return b.eth.txPool.State().GetNonce(addr), nil
}

185
func (b *EthAPIBackend) Stats() (pending int, queued int) {
186 187 188
	return b.eth.txPool.Stats()
}

189
func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
190 191 192
	return b.eth.TxPool().Content()
}

193 194
func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
195 196
}

197
func (b *EthAPIBackend) Downloader() *downloader.Downloader {
198 199 200
	return b.eth.Downloader()
}

201
func (b *EthAPIBackend) ProtocolVersion() int {
202 203 204
	return b.eth.EthVersion()
}

205
func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
206
	return b.gpo.SuggestPrice(ctx)
207 208
}

209
func (b *EthAPIBackend) ChainDb() ethdb.Database {
210 211 212
	return b.eth.ChainDb()
}

213
func (b *EthAPIBackend) EventMux() *event.TypeMux {
214 215 216
	return b.eth.EventMux()
}

217
func (b *EthAPIBackend) AccountManager() *accounts.Manager {
218 219
	return b.eth.AccountManager()
}
220

221 222 223 224
func (b *EthAPIBackend) ExtRPCEnabled() bool {
	return b.extRPCEnabled
}

225 226 227 228
func (b *EthAPIBackend) RPCGasCap() *big.Int {
	return b.eth.config.RPCGasCap
}

229
func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
230 231
	sections, _, _ := b.eth.bloomIndexer.Sections()
	return params.BloomBitsBlocks, sections
232 233
}

234
func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
235 236
	for i := 0; i < bloomFilterThreads; i++ {
		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
237 238
	}
}