api_backend.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. package les
  17. import (
  18. "context"
  19. "errors"
  20. "math/big"
  21. "time"
  22. "github.com/ethereum/go-ethereum/accounts"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/consensus"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/bloombits"
  27. "github.com/ethereum/go-ethereum/core/mps"
  28. "github.com/ethereum/go-ethereum/core/rawdb"
  29. "github.com/ethereum/go-ethereum/core/state"
  30. "github.com/ethereum/go-ethereum/core/types"
  31. "github.com/ethereum/go-ethereum/core/vm"
  32. "github.com/ethereum/go-ethereum/eth/downloader"
  33. "github.com/ethereum/go-ethereum/eth/gasprice"
  34. "github.com/ethereum/go-ethereum/eth/tracers"
  35. "github.com/ethereum/go-ethereum/ethdb"
  36. "github.com/ethereum/go-ethereum/event"
  37. "github.com/ethereum/go-ethereum/internal/ethapi"
  38. "github.com/ethereum/go-ethereum/light"
  39. "github.com/ethereum/go-ethereum/params"
  40. "github.com/ethereum/go-ethereum/rpc"
  41. "github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto"
  42. )
  43. type LesApiBackend struct {
  44. extRPCEnabled bool
  45. allowUnprotectedTxs bool
  46. eth *LightEthereum
  47. gpo *gasprice.Oracle
  48. }
  49. var _ ethapi.Backend = &LesApiBackend{}
  50. var _ tracers.Backend = &LesApiBackend{}
  51. func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
  52. return b.eth.chainConfig
  53. }
  54. func (b *LesApiBackend) PSMR() mps.PrivateStateMetadataResolver {
  55. panic("not supported")
  56. }
  57. func (b *LesApiBackend) CurrentBlock() *types.Block {
  58. return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
  59. }
  60. func (b *LesApiBackend) SetHead(number uint64) {
  61. b.eth.handler.downloader.Cancel()
  62. b.eth.blockchain.SetHead(number)
  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. 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.eth.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. func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  90. return b.eth.blockchain.GetHeaderByHash(hash), nil
  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. func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  100. return b.eth.blockchain.GetBlockByHash(ctx, hash)
  101. }
  102. func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
  103. if blockNr, ok := blockNrOrHash.Number(); ok {
  104. return b.BlockByNumber(ctx, blockNr)
  105. }
  106. if hash, ok := blockNrOrHash.Hash(); ok {
  107. block, err := b.BlockByHash(ctx, hash)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if block == nil {
  112. return nil, errors.New("header found, but block body is missing")
  113. }
  114. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(block.NumberU64()) != hash {
  115. return nil, errors.New("hash is not currently canonical")
  116. }
  117. return block, nil
  118. }
  119. return nil, errors.New("invalid arguments; neither block nor hash specified")
  120. }
  121. func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) {
  122. header, err := b.HeaderByNumber(ctx, number)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. if header == nil {
  127. return nil, nil, errors.New("header not found")
  128. }
  129. return light.NewState(ctx, header, b.eth.odr), header, nil
  130. }
  131. func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (vm.MinimalApiState, *types.Header, error) {
  132. if blockNr, ok := blockNrOrHash.Number(); ok {
  133. return b.StateAndHeaderByNumber(ctx, blockNr)
  134. }
  135. if hash, ok := blockNrOrHash.Hash(); ok {
  136. header := b.eth.blockchain.GetHeaderByHash(hash)
  137. if header == nil {
  138. return nil, nil, errors.New("header for hash not found")
  139. }
  140. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  141. return nil, nil, errors.New("hash is not currently canonical")
  142. }
  143. return light.NewState(ctx, header, b.eth.odr), header, nil
  144. }
  145. return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
  146. }
  147. func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
  148. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  149. return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
  150. }
  151. return nil, nil
  152. }
  153. func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
  154. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  155. return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
  156. }
  157. return nil, nil
  158. }
  159. func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
  160. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  161. return b.eth.blockchain.GetTdOdr(ctx, hash, *number)
  162. }
  163. return nil
  164. }
  165. func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, apiState vm.MinimalApiState, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
  166. statedb := apiState.(*state.StateDB)
  167. if vmConfig == nil {
  168. vmConfig = new(vm.Config)
  169. }
  170. txContext := core.NewEVMTxContext(msg)
  171. context := core.NewEVMBlockContext(header, b.eth.blockchain, nil)
  172. return vm.NewEVM(context, txContext, statedb, statedb, b.eth.chainConfig, *vmConfig), statedb.Error, nil
  173. }
  174. func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  175. return b.eth.txPool.Add(ctx, signedTx)
  176. }
  177. func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
  178. b.eth.txPool.RemoveTx(txHash)
  179. }
  180. func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
  181. return b.eth.txPool.GetTransactions()
  182. }
  183. func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
  184. return b.eth.txPool.GetTransaction(txHash)
  185. }
  186. func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
  187. return light.GetTransaction(ctx, b.eth.odr, txHash)
  188. }
  189. func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  190. return b.eth.txPool.GetNonce(ctx, addr)
  191. }
  192. func (b *LesApiBackend) Stats() (pending int, queued int) {
  193. return b.eth.txPool.Stats(), 0
  194. }
  195. func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  196. return b.eth.txPool.Content()
  197. }
  198. func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
  199. return b.eth.txPool.SubscribeNewTxsEvent(ch)
  200. }
  201. func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  202. return b.eth.blockchain.SubscribeChainEvent(ch)
  203. }
  204. func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  205. return b.eth.blockchain.SubscribeChainHeadEvent(ch)
  206. }
  207. func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  208. return b.eth.blockchain.SubscribeChainSideEvent(ch)
  209. }
  210. func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  211. return b.eth.blockchain.SubscribeLogsEvent(ch)
  212. }
  213. func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
  214. return event.NewSubscription(func(quit <-chan struct{}) error {
  215. <-quit
  216. return nil
  217. })
  218. }
  219. func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  220. return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
  221. }
  222. func (b *LesApiBackend) Downloader() *downloader.Downloader {
  223. return b.eth.Downloader()
  224. }
  225. func (b *LesApiBackend) ProtocolVersion() int {
  226. return b.eth.LesVersion() + 10000
  227. }
  228. func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  229. if !b.ChainConfig().IsQuorum || b.ChainConfig().IsGasPriceEnabled(b.eth.BlockChain().CurrentHeader().Number) {
  230. return b.gpo.SuggestPrice(ctx)
  231. } else {
  232. return big.NewInt(0), nil
  233. }
  234. }
  235. func (b *LesApiBackend) ChainDb() ethdb.Database {
  236. return b.eth.chainDb
  237. }
  238. func (b *LesApiBackend) AccountManager() *accounts.Manager {
  239. return b.eth.accountManager
  240. }
  241. func (b *LesApiBackend) ExtRPCEnabled() bool {
  242. return b.extRPCEnabled
  243. }
  244. func (b *LesApiBackend) UnprotectedAllowed() bool {
  245. return b.allowUnprotectedTxs
  246. }
  247. func (b *LesApiBackend) RPCGasCap() uint64 {
  248. return b.eth.config.RPCGasCap
  249. }
  250. func (b *LesApiBackend) RPCTxFeeCap() float64 {
  251. return b.eth.config.RPCTxFeeCap
  252. }
  253. func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
  254. if b.eth.bloomIndexer == nil {
  255. return 0, 0
  256. }
  257. sections, _, _ := b.eth.bloomIndexer.Sections()
  258. return params.BloomBitsBlocksClient, sections
  259. }
  260. func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  261. for i := 0; i < bloomFilterThreads; i++ {
  262. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  263. }
  264. }
  265. func (b *LesApiBackend) Engine() consensus.Engine {
  266. return b.eth.engine
  267. }
  268. func (b *LesApiBackend) CurrentHeader() *types.Header {
  269. return b.eth.blockchain.CurrentHeader()
  270. }
  271. func (b *LesApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, mps.PrivateStateRepository, error) {
  272. return b.eth.stateAtBlock(ctx, block, reexec)
  273. }
  274. func (b *LesApiBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, *state.StateDB, mps.PrivateStateRepository, error) {
  275. return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
  276. }
  277. // Quorum
  278. func (b *LesApiBackend) GetBlockchain() *core.BlockChain {
  279. return nil
  280. }
  281. func (b *LesApiBackend) SupportsMultitenancy(rpcCtx context.Context) (*proto.PreAuthenticatedAuthenticationToken, bool) {
  282. authToken := rpc.PreauthenticatedTokenFromContext(rpcCtx)
  283. if authToken != nil && b.eth.config.MultiTenantEnabled() {
  284. return authToken, true
  285. }
  286. return nil, false
  287. }
  288. func (b *LesApiBackend) AccountExtraDataStateGetterByNumber(ctx context.Context, number rpc.BlockNumber) (vm.AccountExtraDataStateGetter, error) {
  289. s, _, err := b.StateAndHeaderByNumber(ctx, number)
  290. return s, err
  291. }
  292. func (b *LesApiBackend) IsPrivacyMarkerTransactionCreationEnabled() bool {
  293. return b.eth.config.QuorumChainConfig.PrivacyMarkerEnabled()
  294. }
  295. func (b *LesApiBackend) CallTimeOut() time.Duration {
  296. return b.eth.config.EVMCallTimeOut
  297. }
  298. // End Quorum