api_backend.go 7.3 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 22 23 24
	"math/big"

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

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

45
// ChainConfig returns the active chain configuration.
46
func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
47 48 49
	return b.eth.chainConfig
}

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

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

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

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

76
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
77 78
	// Pending block is only known by the miner
	if blockNr == rpc.PendingBlockNumber {
79
		block := b.eth.miner.PendingBlock()
80 81 82 83 84 85 86 87 88
		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
}

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

104
func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
105
	return b.eth.blockchain.GetBlockByHash(hash), nil
106 107
}

108
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
109
	return b.eth.blockchain.GetReceiptsByHash(hash), nil
110 111
}

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

124
func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
125 126 127
	return b.eth.blockchain.GetTdByHash(blockHash)
}

128
func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
129
	state.SetBalance(msg.From(), math.MaxBig256)
130
	vmError := func() error { return nil }
131

132
	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
133
	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
134 135
}

136
func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
137 138 139
	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
}

140
func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
141 142 143
	return b.eth.BlockChain().SubscribeChainEvent(ch)
}

144
func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
145 146 147
	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
}

148
func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
149 150 151
	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
}

152
func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
153 154 155
	return b.eth.BlockChain().SubscribeLogsEvent(ch)
}

156
func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
157
	return b.eth.txPool.AddLocal(signedTx)
158 159
}

160
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
161 162 163 164
	pending, err := b.eth.txPool.Pending()
	if err != nil {
		return nil, err
	}
165
	var txs types.Transactions
166
	for _, batch := range pending {
167 168
		txs = append(txs, batch...)
	}
169
	return txs, nil
170 171
}

172
func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
173
	return b.eth.txPool.Get(hash)
174 175
}

176
func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
177 178 179
	return b.eth.txPool.State().GetNonce(addr), nil
}

180
func (b *EthAPIBackend) Stats() (pending int, queued int) {
181 182 183
	return b.eth.txPool.Stats()
}

184
func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
185 186 187
	return b.eth.TxPool().Content()
}

188 189
func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
190 191
}

192
func (b *EthAPIBackend) Downloader() *downloader.Downloader {
193 194 195
	return b.eth.Downloader()
}

196
func (b *EthAPIBackend) ProtocolVersion() int {
197 198 199
	return b.eth.EthVersion()
}

200
func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
201
	return b.gpo.SuggestPrice(ctx)
202 203
}

204
func (b *EthAPIBackend) ChainDb() ethdb.Database {
205 206 207
	return b.eth.ChainDb()
}

208
func (b *EthAPIBackend) EventMux() *event.TypeMux {
209 210 211
	return b.eth.EventMux()
}

212
func (b *EthAPIBackend) AccountManager() *accounts.Manager {
213 214
	return b.eth.AccountManager()
}
215

216
func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
217 218
	sections, _, _ := b.eth.bloomIndexer.Sections()
	return params.BloomBitsBlocks, sections
219 220
}

221
func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
222 223
	for i := 0; i < bloomFilterThreads; i++ {
		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
224 225
	}
}