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