github.com/core-coin/go-core/v2@v2.1.9/xcb/api_backend.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package xcb 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 24 "github.com/core-coin/go-core/v2/xcb/energyprice" 25 26 "github.com/core-coin/go-core/v2/xcbdb" 27 28 "github.com/core-coin/go-core/v2/accounts" 29 "github.com/core-coin/go-core/v2/common" 30 "github.com/core-coin/go-core/v2/consensus" 31 "github.com/core-coin/go-core/v2/core" 32 "github.com/core-coin/go-core/v2/core/bloombits" 33 "github.com/core-coin/go-core/v2/core/rawdb" 34 "github.com/core-coin/go-core/v2/core/state" 35 "github.com/core-coin/go-core/v2/core/types" 36 "github.com/core-coin/go-core/v2/core/vm" 37 "github.com/core-coin/go-core/v2/event" 38 "github.com/core-coin/go-core/v2/miner" 39 "github.com/core-coin/go-core/v2/params" 40 "github.com/core-coin/go-core/v2/rpc" 41 "github.com/core-coin/go-core/v2/xcb/downloader" 42 ) 43 44 // XcbAPIBackend implements xcbapi.Backend for full nodes 45 type XcbAPIBackend struct { 46 extRPCEnabled bool 47 xcb *Core 48 gpo *energyprice.Oracle 49 } 50 51 // ChainConfig returns the active chain configuration. 52 func (b *XcbAPIBackend) ChainConfig() *params.ChainConfig { 53 return b.xcb.blockchain.Config() 54 } 55 56 func (b *XcbAPIBackend) CurrentBlock() *types.Block { 57 return b.xcb.blockchain.CurrentBlock() 58 } 59 60 func (b *XcbAPIBackend) SetHead(number uint64) { 61 b.xcb.protocolManager.downloader.Cancel() 62 b.xcb.blockchain.SetHead(number) 63 } 64 65 func (b *XcbAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 66 // Pending block is only known by the miner 67 if number == rpc.PendingBlockNumber { 68 block := b.xcb.miner.PendingBlock() 69 return block.Header(), nil 70 } 71 // Otherwise resolve and return the block 72 if number == rpc.LatestBlockNumber { 73 return b.xcb.blockchain.CurrentBlock().Header(), nil 74 } 75 return b.xcb.blockchain.GetHeaderByNumber(uint64(number)), nil 76 } 77 78 func (b *XcbAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 79 if blockNr, ok := blockNrOrHash.Number(); ok { 80 return b.HeaderByNumber(ctx, blockNr) 81 } 82 if hash, ok := blockNrOrHash.Hash(); ok { 83 header := b.xcb.blockchain.GetHeaderByHash(hash) 84 if header == nil { 85 return nil, errors.New("header for hash not found") 86 } 87 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 88 return nil, errors.New("hash is not currently canonical") 89 } 90 return header, nil 91 } 92 return nil, errors.New("invalid arguments; neither block nor hash specified") 93 } 94 95 func (b *XcbAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 96 return b.xcb.blockchain.GetHeaderByHash(hash), nil 97 } 98 99 func (b *XcbAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 100 // Pending block is only known by the miner 101 if number == rpc.PendingBlockNumber { 102 block := b.xcb.miner.PendingBlock() 103 return block, nil 104 } 105 // Otherwise resolve and return the block 106 if number == rpc.LatestBlockNumber { 107 return b.xcb.blockchain.CurrentBlock(), nil 108 } 109 return b.xcb.blockchain.GetBlockByNumber(uint64(number)), nil 110 } 111 112 func (b *XcbAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 113 return b.xcb.blockchain.GetBlockByHash(hash), nil 114 } 115 116 func (b *XcbAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 117 if blockNr, ok := blockNrOrHash.Number(); ok { 118 return b.BlockByNumber(ctx, blockNr) 119 } 120 if hash, ok := blockNrOrHash.Hash(); ok { 121 header := b.xcb.blockchain.GetHeaderByHash(hash) 122 if header == nil { 123 return nil, errors.New("header for hash not found") 124 } 125 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 126 return nil, errors.New("hash is not currently canonical") 127 } 128 block := b.xcb.blockchain.GetBlock(hash, header.Number.Uint64()) 129 if block == nil { 130 return nil, errors.New("header found, but block body is missing") 131 } 132 return block, nil 133 } 134 return nil, errors.New("invalid arguments; neither block nor hash specified") 135 } 136 137 func (b *XcbAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 138 // Pending state is only known by the miner 139 if number == rpc.PendingBlockNumber { 140 block, state := b.xcb.miner.Pending() 141 return state, block.Header(), nil 142 } 143 // Otherwise resolve the block number and return its state 144 header, err := b.HeaderByNumber(ctx, number) 145 if err != nil { 146 return nil, nil, err 147 } 148 if header == nil { 149 return nil, nil, errors.New("header not found") 150 } 151 stateDb, err := b.xcb.BlockChain().StateAt(header.Root) 152 return stateDb, header, err 153 } 154 155 func (b *XcbAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 156 if blockNr, ok := blockNrOrHash.Number(); ok { 157 return b.StateAndHeaderByNumber(ctx, blockNr) 158 } 159 if hash, ok := blockNrOrHash.Hash(); ok { 160 header, err := b.HeaderByHash(ctx, hash) 161 if err != nil { 162 return nil, nil, err 163 } 164 if header == nil { 165 return nil, nil, errors.New("header for hash not found") 166 } 167 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 168 return nil, nil, errors.New("hash is not currently canonical") 169 } 170 stateDb, err := b.xcb.BlockChain().StateAt(header.Root) 171 return stateDb, header, err 172 } 173 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 174 } 175 176 func (b *XcbAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 177 return b.xcb.blockchain.GetReceiptsByHash(hash), nil 178 } 179 180 func (b *XcbAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 181 receipts := b.xcb.blockchain.GetReceiptsByHash(hash) 182 if receipts == nil { 183 return nil, nil 184 } 185 logs := make([][]*types.Log, len(receipts)) 186 for i, receipt := range receipts { 187 logs[i] = receipt.Logs 188 } 189 return logs, nil 190 } 191 192 func (b *XcbAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 193 return b.xcb.blockchain.GetTdByHash(hash) 194 } 195 196 func (b *XcbAPIBackend) GetCVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.CVM, func() error, error) { 197 vmError := func() error { return nil } 198 199 txContext := core.NewCVMTxContext(msg) 200 context := core.NewCVMBlockContext(header, b.xcb.BlockChain(), nil) 201 return vm.NewCVM(context, txContext, state, b.xcb.blockchain.Config(), *b.xcb.blockchain.GetVMConfig()), vmError, nil 202 } 203 204 func (b *XcbAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 205 return b.xcb.BlockChain().SubscribeRemovedLogsEvent(ch) 206 } 207 208 func (b *XcbAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 209 return b.xcb.miner.SubscribePendingLogs(ch) 210 } 211 212 func (b *XcbAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 213 return b.xcb.BlockChain().SubscribeChainEvent(ch) 214 } 215 216 func (b *XcbAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 217 return b.xcb.BlockChain().SubscribeChainHeadEvent(ch) 218 } 219 220 func (b *XcbAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 221 return b.xcb.BlockChain().SubscribeChainSideEvent(ch) 222 } 223 224 func (b *XcbAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 225 return b.xcb.BlockChain().SubscribeLogsEvent(ch) 226 } 227 228 func (b *XcbAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 229 return b.xcb.txPool.AddLocal(signedTx) 230 } 231 232 func (b *XcbAPIBackend) GetPoolTransactions() (types.Transactions, error) { 233 pending, err := b.xcb.txPool.Pending() 234 if err != nil { 235 return nil, err 236 } 237 var txs types.Transactions 238 for _, batch := range pending { 239 txs = append(txs, batch...) 240 } 241 return txs, nil 242 } 243 244 func (b *XcbAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 245 return b.xcb.txPool.Get(hash) 246 } 247 248 func (b *XcbAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 249 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.xcb.ChainDb(), txHash) 250 return tx, blockHash, blockNumber, index, nil 251 } 252 253 func (b *XcbAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 254 return b.xcb.txPool.Nonce(addr), nil 255 } 256 257 func (b *XcbAPIBackend) Stats() (pending int, queued int) { 258 return b.xcb.txPool.Stats() 259 } 260 261 func (b *XcbAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 262 return b.xcb.TxPool().Content() 263 } 264 265 func (b *XcbAPIBackend) TxPool() *core.TxPool { 266 return b.xcb.TxPool() 267 } 268 269 func (b *XcbAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 270 return b.xcb.TxPool().SubscribeNewTxsEvent(ch) 271 } 272 273 func (b *XcbAPIBackend) Downloader() *downloader.Downloader { 274 return b.xcb.Downloader() 275 } 276 277 func (b *XcbAPIBackend) ProtocolVersion() int { 278 return b.xcb.XcbVersion() 279 } 280 281 func (b *XcbAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 282 return b.gpo.SuggestPrice(ctx) 283 } 284 285 func (b *XcbAPIBackend) ChainDb() xcbdb.Database { 286 return b.xcb.ChainDb() 287 } 288 289 func (b *XcbAPIBackend) EventMux() *event.TypeMux { 290 return b.xcb.EventMux() 291 } 292 293 func (b *XcbAPIBackend) AccountManager() *accounts.Manager { 294 return b.xcb.AccountManager() 295 } 296 297 func (b *XcbAPIBackend) ExtRPCEnabled() bool { 298 return b.extRPCEnabled 299 } 300 301 func (b *XcbAPIBackend) RPCEnergyCap() uint64 { 302 return b.xcb.config.RPCEnergyCap 303 } 304 305 func (b *XcbAPIBackend) RPCTxFeeCap() float64 { 306 return b.xcb.config.RPCTxFeeCap 307 } 308 309 func (b *XcbAPIBackend) BloomStatus() (uint64, uint64) { 310 sections, _, _ := b.xcb.bloomIndexer.Sections() 311 return params.BloomBitsBlocks, sections 312 } 313 314 func (b *XcbAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 315 for i := 0; i < bloomFilterThreads; i++ { 316 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.xcb.bloomRequests) 317 } 318 } 319 320 func (b *XcbAPIBackend) Engine() consensus.Engine { 321 return b.xcb.engine 322 } 323 324 func (b *XcbAPIBackend) CurrentHeader() *types.Header { 325 return b.xcb.blockchain.CurrentHeader() 326 } 327 328 func (b *XcbAPIBackend) Miner() *miner.Miner { 329 return b.xcb.Miner() 330 } 331 332 func (b *XcbAPIBackend) StartMining(threads int) error { 333 return b.xcb.StartMining(threads) 334 }