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

// Package ethapi implements the general Ethereum API functions.
package ethapi

import (
21
	"context"
22
	"math/big"
23
	"time"
24

25
	"github.com/ethereum/go-ethereum"
26 27
	"github.com/ethereum/go-ethereum/accounts"
	"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/state"
31 32
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/core/vm"
33
	"github.com/ethereum/go-ethereum/eth/filters"
34 35
	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/event"
36
	"github.com/ethereum/go-ethereum/params"
37 38 39 40 41 42
	"github.com/ethereum/go-ethereum/rpc"
)

// Backend interface provides the common API services (that are provided by
// both full and light clients) with access to necessary functions.
type Backend interface {
43
	// General Ethereum API
44 45
	SyncProgress() ethereum.SyncProgress

46
	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
47
	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
48 49
	ChainDb() ethdb.Database
	AccountManager() *accounts.Manager
50
	ExtRPCEnabled() bool
51 52 53 54
	RPCGasCap() uint64            // global gas cap for eth_call over rpc: DoS protection
	RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
	RPCTxFeeCap() float64         // global tx fee cap for all transaction related APIs
	UnprotectedAllowed() bool     // allows only for EIP155 transactions.
55

56
	// Blockchain API
57
	SetHead(number uint64)
58 59
	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
60
	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
61 62
	CurrentHeader() *types.Header
	CurrentBlock() *types.Block
63
	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
64
	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
65
	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
66
	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
67
	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
68
	PendingBlockAndReceipts() (*types.Block, types.Receipts)
69
	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
70
	GetTd(ctx context.Context, hash common.Hash) *big.Int
71
	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
72 73 74
	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
75

76
	// Transaction pool API
77
	SendTx(ctx context.Context, signedTx *types.Transaction) error
78
	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
79
	GetPoolTransactions() (types.Transactions, error)
80 81 82
	GetPoolTransaction(txHash common.Hash) *types.Transaction
	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
	Stats() (pending int, queued int)
83
	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
84
	TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
85
	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
86 87

	ChainConfig() *params.ChainConfig
88
	Engine() consensus.Engine
89 90 91 92

	// eth/filters needs to be initialized from this backend type, so methods needed by
	// it must also be included here.
	filters.Backend
93 94
}

95
func GetAPIs(apiBackend Backend) []rpc.API {
96
	nonceLock := new(AddrLocker)
97
	return []rpc.API{
98 99
		{
			Namespace: "eth",
100
			Service:   NewEthereumAPI(apiBackend),
101 102
		}, {
			Namespace: "eth",
103
			Service:   NewBlockChainAPI(apiBackend),
104 105
		}, {
			Namespace: "eth",
106
			Service:   NewTransactionAPI(apiBackend, nonceLock),
107 108
		}, {
			Namespace: "txpool",
109
			Service:   NewTxPoolAPI(apiBackend),
110 111
		}, {
			Namespace: "debug",
112
			Service:   NewDebugAPI(apiBackend),
113 114
		}, {
			Namespace: "eth",
115
			Service:   NewEthereumAccountAPI(apiBackend.AccountManager()),
116 117
		}, {
			Namespace: "personal",
118
			Service:   NewPersonalAccountAPI(apiBackend, nonceLock),
119 120 121
		},
	}
}