github.com/dominant-strategies/go-quai@v0.28.2/internal/quaiapi/backend.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // package quaiapi implements the general Quai API functions.
    18  package quaiapi
    19  
    20  import (
    21  	"context"
    22  	"math/big"
    23  
    24  	quai "github.com/dominant-strategies/go-quai"
    25  	"github.com/dominant-strategies/go-quai/common"
    26  	"github.com/dominant-strategies/go-quai/consensus"
    27  	"github.com/dominant-strategies/go-quai/core"
    28  	"github.com/dominant-strategies/go-quai/core/bloombits"
    29  	"github.com/dominant-strategies/go-quai/core/state"
    30  	"github.com/dominant-strategies/go-quai/core/types"
    31  	"github.com/dominant-strategies/go-quai/core/vm"
    32  	"github.com/dominant-strategies/go-quai/eth/downloader"
    33  	"github.com/dominant-strategies/go-quai/ethdb"
    34  	"github.com/dominant-strategies/go-quai/event"
    35  	"github.com/dominant-strategies/go-quai/params"
    36  	"github.com/dominant-strategies/go-quai/rpc"
    37  )
    38  
    39  // Backend interface provides the common API services (that are provided by
    40  // both full and light clients) with access to necessary functions.
    41  type Backend interface {
    42  	// General Quai API
    43  	SyncProgress() quai.SyncProgress
    44  	EventMux() *event.TypeMux
    45  
    46  	// General Quai API
    47  	Downloader() *downloader.Downloader
    48  	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
    49  	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
    50  	ChainDb() ethdb.Database
    51  	ExtRPCEnabled() bool
    52  	RPCGasCap() uint64    // global gas cap for eth_call over rpc: DoS protection
    53  	RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
    54  
    55  	// Blockchain API
    56  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    57  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    58  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    59  	CurrentHeader() *types.Header
    60  	CurrentBlock() *types.Block
    61  	CurrentLogEntropy() *big.Int
    62  	TotalLogS(header *types.Header) *big.Int
    63  	CalcOrder(header *types.Header) (*big.Int, int, error)
    64  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    65  	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
    66  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    67  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    68  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    69  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    70  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
    71  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    72  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    73  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    74  	WriteBlock(block *types.Block)
    75  	Append(header *types.Header, manifest types.BlockManifest, domPendingHeader *types.Header, domTerminus common.Hash, domOrigin bool, newInboundEtxs types.Transactions) (types.Transactions, bool, bool, error)
    76  	DownloadBlocksInManifest(hash common.Hash, manifest types.BlockManifest, entropy *big.Int)
    77  	ConstructLocalMinedBlock(header *types.Header) (*types.Block, error)
    78  	InsertBlock(ctx context.Context, block *types.Block) (int, error)
    79  	PendingBlock() *types.Block
    80  	SubRelayPendingHeader(pendingHeader types.PendingHeader, newEntropy *big.Int, location common.Location, subReorg bool, order int)
    81  	UpdateDom(oldTerminus common.Hash, pendingHeader types.PendingHeader, location common.Location)
    82  	RequestDomToAppendOrFetch(hash common.Hash, entropy *big.Int, order int)
    83  	NewGenesisPendingHeader(pendingHeader *types.Header)
    84  	GetPendingHeader() (*types.Header, error)
    85  	GetManifest(blockHash common.Hash) (types.BlockManifest, error)
    86  	GetSubManifest(slice common.Location, blockHash common.Hash) (types.BlockManifest, error)
    87  	AddPendingEtxs(pEtxs types.PendingEtxs) error
    88  	AddPendingEtxsRollup(pEtxsRollup types.PendingEtxsRollup) error
    89  	PendingBlockAndReceipts() (*types.Block, types.Receipts)
    90  	GenerateRecoveryPendingHeader(pendingHeader *types.Header, checkpointHashes types.Termini) error
    91  	GetPendingEtxsRollupFromSub(hash common.Hash, location common.Location) (types.PendingEtxsRollup, error)
    92  	GetPendingEtxsFromSub(hash common.Hash, location common.Location) (types.PendingEtxs, error)
    93  	SetSyncTarget(header *types.Header)
    94  	ProcessingState() bool
    95  
    96  	// Transaction pool API
    97  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    98  	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
    99  	GetPoolTransactions() (types.Transactions, error)
   100  	GetPoolTransaction(txHash common.Hash) *types.Transaction
   101  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
   102  	Stats() (pending int, queued int)
   103  	TxPoolContent() (map[common.InternalAddress]types.Transactions, map[common.InternalAddress]types.Transactions)
   104  	TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
   105  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
   106  
   107  	// Filter API
   108  	BloomStatus() (uint64, uint64)
   109  	GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
   110  	ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
   111  	SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
   112  	SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
   113  	SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
   114  	SubscribePendingHeaderEvent(ch chan<- *types.Header) event.Subscription
   115  
   116  	ChainConfig() *params.ChainConfig
   117  	Engine() consensus.Engine
   118  }
   119  
   120  func GetAPIs(apiBackend Backend) []rpc.API {
   121  	nodeCtx := common.NodeLocation.Context()
   122  	nonceLock := new(AddrLocker)
   123  	apis := []rpc.API{
   124  		{
   125  			Namespace: "eth",
   126  			Version:   "1.0",
   127  			Service:   NewPublicQuaiAPI(apiBackend),
   128  			Public:    true,
   129  		}, {
   130  			Namespace: "quai",
   131  			Version:   "1.0",
   132  			Service:   NewPublicQuaiAPI(apiBackend),
   133  			Public:    true,
   134  		}, {
   135  			Namespace: "eth",
   136  			Version:   "1.0",
   137  			Service:   NewPublicBlockChainAPI(apiBackend),
   138  			Public:    true,
   139  		}, {
   140  			Namespace: "quai",
   141  			Version:   "1.0",
   142  			Service:   NewPublicBlockChainQuaiAPI(apiBackend),
   143  			Public:    true,
   144  		}, {
   145  			Namespace: "debug",
   146  			Version:   "1.0",
   147  			Service:   NewPublicDebugAPI(apiBackend),
   148  			Public:    true,
   149  		}, {
   150  			Namespace: "debug",
   151  			Version:   "1.0",
   152  			Service:   NewPrivateDebugAPI(apiBackend),
   153  		},
   154  	}
   155  	if nodeCtx == common.ZONE_CTX {
   156  		apis = append(apis, rpc.API{
   157  			Namespace: "eth",
   158  			Version:   "1.0",
   159  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   160  			Public:    true,
   161  		})
   162  		apis = append(apis, rpc.API{
   163  			Namespace: "quai",
   164  			Version:   "1.0",
   165  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   166  			Public:    true,
   167  		})
   168  		apis = append(apis, rpc.API{
   169  			Namespace: "txpool",
   170  			Version:   "1.0",
   171  			Service:   NewPublicTxPoolAPI(apiBackend),
   172  			Public:    true,
   173  		})
   174  	}
   175  
   176  	return apis
   177  }