api_backend.go 7.34 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
	"github.com/ethereum/go-ethereum/core/rawdb"
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
	eth *Ethereum
43
	gpo *gasprice.Oracle
44 45
}

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

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

100
func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
101
	return b.eth.blockchain.GetBlockByHash(hash), nil
102 103
}

104
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
105 106 107 108
	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
		return rawdb.ReadReceipts(b.eth.chainDb, hash, *number), nil
	}
	return nil, nil
109 110
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

191
func (b *EthAPIBackend) SubscribeTxPreEvent(ch chan<- core.TxsPreEvent) event.Subscription {
192 193 194
	return b.eth.TxPool().SubscribeTxPreEvent(ch)
}

195
func (b *EthAPIBackend) Downloader() *downloader.Downloader {
196 197 198
	return b.eth.Downloader()
}

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

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

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

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

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

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

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