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