github.com/core-coin/go-core/v2@v2.1.9/les/api_backend.go (about) 1 // Copyright 2016 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 les 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/light" 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 type LesApiBackend struct { 45 extRPCEnabled bool 46 xcb *LightCore 47 gpo *energyprice.Oracle 48 } 49 50 func (b *LesApiBackend) ChainConfig() *params.ChainConfig { 51 return b.xcb.chainConfig 52 } 53 54 func (b *LesApiBackend) CurrentBlock() *types.Block { 55 return types.NewBlockWithHeader(b.xcb.BlockChain().CurrentHeader()) 56 } 57 58 func (b *LesApiBackend) SetHead(number uint64) { 59 b.xcb.handler.downloader.Cancel() 60 b.xcb.blockchain.SetHead(number) 61 } 62 63 func (b *LesApiBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 64 if number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber { 65 return b.xcb.blockchain.CurrentHeader(), nil 66 } 67 return b.xcb.blockchain.GetHeaderByNumberOdr(ctx, uint64(number)) 68 } 69 70 func (b *LesApiBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 71 if blockNr, ok := blockNrOrHash.Number(); ok { 72 return b.HeaderByNumber(ctx, blockNr) 73 } 74 if hash, ok := blockNrOrHash.Hash(); ok { 75 header, err := b.HeaderByHash(ctx, hash) 76 if err != nil { 77 return nil, err 78 } 79 if header == nil { 80 return nil, errors.New("header for hash not found") 81 } 82 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 83 return nil, errors.New("hash is not currently canonical") 84 } 85 return header, nil 86 } 87 return nil, errors.New("invalid arguments; neither block nor hash specified") 88 } 89 90 func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 91 return b.xcb.blockchain.GetHeaderByHash(hash), nil 92 } 93 94 func (b *LesApiBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 95 header, err := b.HeaderByNumber(ctx, number) 96 if header == nil || err != nil { 97 return nil, err 98 } 99 return b.BlockByHash(ctx, header.Hash()) 100 } 101 102 func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 103 return b.xcb.blockchain.GetBlockByHash(ctx, hash) 104 } 105 106 func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 107 if blockNr, ok := blockNrOrHash.Number(); ok { 108 return b.BlockByNumber(ctx, blockNr) 109 } 110 if hash, ok := blockNrOrHash.Hash(); ok { 111 block, err := b.BlockByHash(ctx, hash) 112 if err != nil { 113 return nil, err 114 } 115 if block == nil { 116 return nil, errors.New("header found, but block body is missing") 117 } 118 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(block.NumberU64()) != hash { 119 return nil, errors.New("hash is not currently canonical") 120 } 121 return block, nil 122 } 123 return nil, errors.New("invalid arguments; neither block nor hash specified") 124 } 125 126 func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 127 header, err := b.HeaderByNumber(ctx, number) 128 if err != nil { 129 return nil, nil, err 130 } 131 if header == nil { 132 return nil, nil, errors.New("header not found") 133 } 134 return light.NewState(ctx, header, b.xcb.odr), header, nil 135 } 136 137 func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 138 if blockNr, ok := blockNrOrHash.Number(); ok { 139 return b.StateAndHeaderByNumber(ctx, blockNr) 140 } 141 if hash, ok := blockNrOrHash.Hash(); ok { 142 header := b.xcb.blockchain.GetHeaderByHash(hash) 143 if header == nil { 144 return nil, nil, errors.New("header for hash not found") 145 } 146 if blockNrOrHash.RequireCanonical && b.xcb.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 147 return nil, nil, errors.New("hash is not currently canonical") 148 } 149 return light.NewState(ctx, header, b.xcb.odr), header, nil 150 } 151 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 152 } 153 154 func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 155 if number := rawdb.ReadHeaderNumber(b.xcb.chainDb, hash); number != nil { 156 return light.GetBlockReceipts(ctx, b.xcb.odr, hash, *number) 157 } 158 return nil, nil 159 } 160 161 func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 162 if number := rawdb.ReadHeaderNumber(b.xcb.chainDb, hash); number != nil { 163 return light.GetBlockLogs(ctx, b.xcb.odr, hash, *number) 164 } 165 return nil, nil 166 } 167 168 func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 169 if number := rawdb.ReadHeaderNumber(b.xcb.chainDb, hash); number != nil { 170 return b.xcb.blockchain.GetTdOdr(ctx, hash, *number) 171 } 172 return nil 173 } 174 175 func (b *LesApiBackend) GetCVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.CVM, func() error, error) { 176 txContext := core.NewCVMTxContext(msg) 177 context := core.NewCVMBlockContext(header, b.xcb.blockchain, nil) 178 return vm.NewCVM(context, txContext, state, b.xcb.chainConfig, vm.Config{}), state.Error, nil 179 } 180 181 func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 182 return b.xcb.txPool.Add(ctx, signedTx) 183 } 184 185 func (b *LesApiBackend) RemoveTx(txHash common.Hash) { 186 b.xcb.txPool.RemoveTx(txHash) 187 } 188 189 func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) { 190 return b.xcb.txPool.GetTransactions() 191 } 192 193 func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction { 194 return b.xcb.txPool.GetTransaction(txHash) 195 } 196 197 func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 198 return light.GetTransaction(ctx, b.xcb.odr, txHash) 199 } 200 201 func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 202 return b.xcb.txPool.GetNonce(ctx, addr) 203 } 204 205 func (b *LesApiBackend) Stats() (pending int, queued int) { 206 return b.xcb.txPool.Stats(), 0 207 } 208 209 func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 210 return b.xcb.txPool.Content() 211 } 212 213 func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 214 return b.xcb.txPool.SubscribeNewTxsEvent(ch) 215 } 216 217 func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 218 return b.xcb.blockchain.SubscribeChainEvent(ch) 219 } 220 221 func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 222 return b.xcb.blockchain.SubscribeChainHeadEvent(ch) 223 } 224 225 func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 226 return b.xcb.blockchain.SubscribeChainSideEvent(ch) 227 } 228 229 func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 230 return b.xcb.blockchain.SubscribeLogsEvent(ch) 231 } 232 233 func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 234 return event.NewSubscription(func(quit <-chan struct{}) error { 235 <-quit 236 return nil 237 }) 238 } 239 240 func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 241 return b.xcb.blockchain.SubscribeRemovedLogsEvent(ch) 242 } 243 244 func (b *LesApiBackend) Downloader() *downloader.Downloader { 245 return b.xcb.Downloader() 246 } 247 248 func (b *LesApiBackend) ProtocolVersion() int { 249 return b.xcb.LesVersion() + 10000 250 } 251 252 func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 253 return b.gpo.SuggestPrice(ctx) 254 } 255 256 func (b *LesApiBackend) ChainDb() xcbdb.Database { 257 return b.xcb.chainDb 258 } 259 260 func (b *LesApiBackend) AccountManager() *accounts.Manager { 261 return b.xcb.accountManager 262 } 263 264 func (b *LesApiBackend) ExtRPCEnabled() bool { 265 return b.extRPCEnabled 266 } 267 268 func (b *LesApiBackend) RPCEnergyCap() uint64 { 269 return b.xcb.config.RPCEnergyCap 270 } 271 272 func (b *LesApiBackend) RPCTxFeeCap() float64 { 273 return b.xcb.config.RPCTxFeeCap 274 } 275 276 func (b *LesApiBackend) BloomStatus() (uint64, uint64) { 277 if b.xcb.bloomIndexer == nil { 278 return 0, 0 279 } 280 sections, _, _ := b.xcb.bloomIndexer.Sections() 281 return params.BloomBitsBlocksClient, sections 282 } 283 284 func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 285 for i := 0; i < bloomFilterThreads; i++ { 286 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.xcb.bloomRequests) 287 } 288 } 289 290 func (b *LesApiBackend) Engine() consensus.Engine { 291 return b.xcb.engine 292 } 293 294 func (b *LesApiBackend) CurrentHeader() *types.Header { 295 return b.xcb.blockchain.CurrentHeader() 296 }