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