github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/api_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 intprotocol 18 19 import ( 20 "context" 21 "math/big" 22 23 "github.com/intfoundation/intchain/accounts" 24 "github.com/intfoundation/intchain/common" 25 "github.com/intfoundation/intchain/common/math" 26 "github.com/intfoundation/intchain/consensus" 27 "github.com/intfoundation/intchain/core" 28 "github.com/intfoundation/intchain/core/bloombits" 29 "github.com/intfoundation/intchain/core/state" 30 "github.com/intfoundation/intchain/core/types" 31 "github.com/intfoundation/intchain/core/vm" 32 "github.com/intfoundation/intchain/event" 33 "github.com/intfoundation/intchain/intdb" 34 "github.com/intfoundation/intchain/intprotocol/downloader" 35 "github.com/intfoundation/intchain/intprotocol/gasprice" 36 "github.com/intfoundation/intchain/params" 37 "github.com/intfoundation/intchain/rpc" 38 ) 39 40 // EthApiBackend implements intapi.Backend for full nodes 41 type EthApiBackend struct { 42 eth *IntChain 43 gpo *gasprice.Oracle 44 //apiBridge intapi.InnerAPIBridge 45 crossChainHelper core.CrossChainHelper 46 } 47 48 func (b *EthApiBackend) ChainConfig() *params.ChainConfig { 49 return b.eth.chainConfig 50 } 51 52 func (b *EthApiBackend) CurrentBlock() *types.Block { 53 return b.eth.blockchain.CurrentBlock() 54 } 55 56 func (b *EthApiBackend) SetHead(number uint64) { 57 b.eth.protocolManager.downloader.Cancel() 58 b.eth.blockchain.SetHead(number) 59 } 60 61 func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 62 // Pending block is only known by the miner 63 if blockNr == rpc.PendingBlockNumber { 64 block := b.eth.miner.PendingBlock() 65 return block.Header(), nil 66 } 67 // Otherwise resolve and return the block 68 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 func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 75 // Pending block is only known by the miner 76 if blockNr == rpc.PendingBlockNumber { 77 block := b.eth.miner.PendingBlock() 78 return block, nil 79 } 80 // Otherwise resolve and return the block 81 if blockNr == rpc.LatestBlockNumber { 82 return b.eth.blockchain.CurrentBlock(), nil 83 } 84 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 85 } 86 87 func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 88 // Pending state is only known by the miner 89 if blockNr == rpc.PendingBlockNumber { 90 block, state := b.eth.miner.Pending() 91 return state, block.Header(), nil 92 } 93 // Otherwise resolve the block number and return its state 94 header, err := b.HeaderByNumber(ctx, blockNr) 95 if header == nil || err != nil { 96 return nil, nil, err 97 } 98 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 99 return stateDb, header, err 100 } 101 102 func (b *EthApiBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { 103 return b.eth.blockchain.GetBlockByHash(hash), nil 104 } 105 106 func (b *EthApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 107 return b.eth.blockchain.GetReceiptsByHash(hash), nil 108 } 109 110 func (b *EthApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 111 receipts := b.eth.blockchain.GetReceiptsByHash(hash) 112 if receipts == nil { 113 return nil, nil 114 } 115 logs := make([][]*types.Log, len(receipts)) 116 for i, receipt := range receipts { 117 logs[i] = receipt.Logs 118 } 119 return logs, nil 120 } 121 122 func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int { 123 return b.eth.blockchain.GetTdByHash(blockHash) 124 } 125 126 func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) { 127 state.SetBalance(msg.From(), math.MaxBig256) 128 vmError := func() error { return nil } 129 130 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 131 return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil 132 } 133 134 func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 135 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 136 } 137 138 func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 139 return b.eth.BlockChain().SubscribeChainEvent(ch) 140 } 141 142 func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 143 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 144 } 145 146 func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 147 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 148 } 149 150 func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 151 return b.eth.BlockChain().SubscribeLogsEvent(ch) 152 } 153 154 func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 155 return b.eth.txPool.AddLocal(signedTx) 156 } 157 158 func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) { 159 pending, err := b.eth.txPool.Pending() 160 if err != nil { 161 return nil, err 162 } 163 var txs types.Transactions 164 for _, batch := range pending { 165 txs = append(txs, batch...) 166 } 167 return txs, nil 168 } 169 170 func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 171 return b.eth.txPool.Get(hash) 172 } 173 174 func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 175 return b.eth.txPool.State().GetNonce(addr), nil 176 } 177 178 func (b *EthApiBackend) Stats() (pending int, queued int) { 179 return b.eth.txPool.Stats() 180 } 181 182 func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 183 return b.eth.TxPool().Content() 184 } 185 186 func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription { 187 return b.eth.TxPool().SubscribeTxPreEvent(ch) 188 } 189 190 func (b *EthApiBackend) Downloader() *downloader.Downloader { 191 return b.eth.Downloader() 192 } 193 194 func (b *EthApiBackend) ProtocolVersion() int { 195 return b.eth.EthVersion() 196 } 197 198 func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 199 return b.gpo.SuggestPrice(ctx) 200 } 201 202 func (b *EthApiBackend) ChainDb() intdb.Database { 203 return b.eth.ChainDb() 204 } 205 206 func (b *EthApiBackend) EventMux() *event.TypeMux { 207 return b.eth.EventMux() 208 } 209 210 func (b *EthApiBackend) AccountManager() *accounts.Manager { 211 return b.eth.AccountManager() 212 } 213 214 func (b *EthApiBackend) BloomStatus() (uint64, uint64) { 215 sections, _, _ := b.eth.bloomIndexer.Sections() 216 return params.BloomBitsBlocks, sections 217 } 218 219 func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 220 for i := 0; i < bloomFilterThreads; i++ { 221 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 222 } 223 } 224 225 //func (b *EthApiBackend) SetInnerAPIBridge(inBridge intapi.InnerAPIBridge) { 226 // b.apiBridge = inBridge 227 //} 228 // 229 //func (b *EthApiBackend) GetInnerAPIBridge() intapi.InnerAPIBridge { 230 // return b.apiBridge 231 //} 232 233 func (b *EthApiBackend) GetCrossChainHelper() core.CrossChainHelper { 234 return b.crossChainHelper 235 } 236 237 func (b *EthApiBackend) BroadcastTX3ProofData(proofData *types.TX3ProofData) { 238 b.eth.protocolManager.BroadcastTX3ProofData(proofData.Header.Hash(), proofData) 239 } 240 241 func (b *EthApiBackend) Engine() consensus.Engine { 242 return b.eth.Engine() 243 }