graphql.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. // Copyright 2019 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 graphql provides a GraphQL interface to Ethereum node data.
  17. package graphql
  18. import (
  19. "context"
  20. "errors"
  21. "fmt"
  22. "math/big"
  23. "strconv"
  24. "github.com/ethereum/go-ethereum"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/hexutil"
  27. "github.com/ethereum/go-ethereum/common/math"
  28. "github.com/ethereum/go-ethereum/consensus/misc"
  29. "github.com/ethereum/go-ethereum/core/state"
  30. "github.com/ethereum/go-ethereum/core/types"
  31. "github.com/ethereum/go-ethereum/eth/filters"
  32. "github.com/ethereum/go-ethereum/internal/ethapi"
  33. "github.com/ethereum/go-ethereum/rlp"
  34. "github.com/ethereum/go-ethereum/rpc"
  35. )
  36. var (
  37. errBlockInvariant = errors.New("block objects must be instantiated with at least one of num or hash")
  38. )
  39. type Long int64
  40. // ImplementsGraphQLType returns true if Long implements the provided GraphQL type.
  41. func (b Long) ImplementsGraphQLType(name string) bool { return name == "Long" }
  42. // UnmarshalGraphQL unmarshals the provided GraphQL query data.
  43. func (b *Long) UnmarshalGraphQL(input interface{}) error {
  44. var err error
  45. switch input := input.(type) {
  46. case string:
  47. // uncomment to support hex values
  48. //if strings.HasPrefix(input, "0x") {
  49. // // apply leniency and support hex representations of longs.
  50. // value, err := hexutil.DecodeUint64(input)
  51. // *b = Long(value)
  52. // return err
  53. //} else {
  54. value, err := strconv.ParseInt(input, 10, 64)
  55. *b = Long(value)
  56. return err
  57. //}
  58. case int32:
  59. *b = Long(input)
  60. case int64:
  61. *b = Long(input)
  62. case float64:
  63. *b = Long(input)
  64. default:
  65. err = fmt.Errorf("unexpected type %T for Long", input)
  66. }
  67. return err
  68. }
  69. // Account represents an Ethereum account at a particular block.
  70. type Account struct {
  71. r *Resolver
  72. address common.Address
  73. blockNrOrHash rpc.BlockNumberOrHash
  74. }
  75. // getState fetches the StateDB object for an account.
  76. func (a *Account) getState(ctx context.Context) (*state.StateDB, error) {
  77. state, _, err := a.r.backend.StateAndHeaderByNumberOrHash(ctx, a.blockNrOrHash)
  78. return state, err
  79. }
  80. func (a *Account) Address(ctx context.Context) (common.Address, error) {
  81. return a.address, nil
  82. }
  83. func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
  84. state, err := a.getState(ctx)
  85. if err != nil {
  86. return hexutil.Big{}, err
  87. }
  88. balance := state.GetBalance(a.address)
  89. if balance == nil {
  90. return hexutil.Big{}, fmt.Errorf("failed to load balance %x", a.address)
  91. }
  92. return hexutil.Big(*balance), nil
  93. }
  94. func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
  95. // Ask transaction pool for the nonce which includes pending transactions
  96. if blockNr, ok := a.blockNrOrHash.Number(); ok && blockNr == rpc.PendingBlockNumber {
  97. nonce, err := a.r.backend.GetPoolNonce(ctx, a.address)
  98. if err != nil {
  99. return 0, err
  100. }
  101. return hexutil.Uint64(nonce), nil
  102. }
  103. state, err := a.getState(ctx)
  104. if err != nil {
  105. return 0, err
  106. }
  107. return hexutil.Uint64(state.GetNonce(a.address)), nil
  108. }
  109. func (a *Account) Code(ctx context.Context) (hexutil.Bytes, error) {
  110. state, err := a.getState(ctx)
  111. if err != nil {
  112. return hexutil.Bytes{}, err
  113. }
  114. return state.GetCode(a.address), nil
  115. }
  116. func (a *Account) Storage(ctx context.Context, args struct{ Slot common.Hash }) (common.Hash, error) {
  117. state, err := a.getState(ctx)
  118. if err != nil {
  119. return common.Hash{}, err
  120. }
  121. return state.GetState(a.address, args.Slot), nil
  122. }
  123. // Log represents an individual log message. All arguments are mandatory.
  124. type Log struct {
  125. r *Resolver
  126. transaction *Transaction
  127. log *types.Log
  128. }
  129. func (l *Log) Transaction(ctx context.Context) *Transaction {
  130. return l.transaction
  131. }
  132. func (l *Log) Account(ctx context.Context, args BlockNumberArgs) *Account {
  133. return &Account{
  134. r: l.r,
  135. address: l.log.Address,
  136. blockNrOrHash: args.NumberOrLatest(),
  137. }
  138. }
  139. func (l *Log) Index(ctx context.Context) int32 {
  140. return int32(l.log.Index)
  141. }
  142. func (l *Log) Topics(ctx context.Context) []common.Hash {
  143. return l.log.Topics
  144. }
  145. func (l *Log) Data(ctx context.Context) hexutil.Bytes {
  146. return l.log.Data
  147. }
  148. // AccessTuple represents EIP-2930
  149. type AccessTuple struct {
  150. address common.Address
  151. storageKeys []common.Hash
  152. }
  153. func (at *AccessTuple) Address(ctx context.Context) common.Address {
  154. return at.address
  155. }
  156. func (at *AccessTuple) StorageKeys(ctx context.Context) []common.Hash {
  157. return at.storageKeys
  158. }
  159. // Transaction represents an Ethereum transaction.
  160. // backend and hash are mandatory; all others will be fetched when required.
  161. type Transaction struct {
  162. r *Resolver
  163. hash common.Hash
  164. tx *types.Transaction
  165. block *Block
  166. index uint64
  167. }
  168. // resolve returns the internal transaction object, fetching it if needed.
  169. func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) {
  170. if t.tx == nil {
  171. // Try to return an already finalized transaction
  172. tx, blockHash, _, index, err := t.r.backend.GetTransaction(ctx, t.hash)
  173. if err == nil && tx != nil {
  174. t.tx = tx
  175. blockNrOrHash := rpc.BlockNumberOrHashWithHash(blockHash, false)
  176. t.block = &Block{
  177. r: t.r,
  178. numberOrHash: &blockNrOrHash,
  179. }
  180. t.index = index
  181. return t.tx, nil
  182. }
  183. // No finalized transaction, try to retrieve it from the pool
  184. t.tx = t.r.backend.GetPoolTransaction(t.hash)
  185. }
  186. return t.tx, nil
  187. }
  188. func (t *Transaction) Hash(ctx context.Context) common.Hash {
  189. return t.hash
  190. }
  191. func (t *Transaction) InputData(ctx context.Context) (hexutil.Bytes, error) {
  192. tx, err := t.resolve(ctx)
  193. if err != nil || tx == nil {
  194. return hexutil.Bytes{}, err
  195. }
  196. return tx.Data(), nil
  197. }
  198. func (t *Transaction) Gas(ctx context.Context) (hexutil.Uint64, error) {
  199. tx, err := t.resolve(ctx)
  200. if err != nil || tx == nil {
  201. return 0, err
  202. }
  203. return hexutil.Uint64(tx.Gas()), nil
  204. }
  205. func (t *Transaction) GasPrice(ctx context.Context) (hexutil.Big, error) {
  206. tx, err := t.resolve(ctx)
  207. if err != nil || tx == nil {
  208. return hexutil.Big{}, err
  209. }
  210. switch tx.Type() {
  211. case types.AccessListTxType:
  212. return hexutil.Big(*tx.GasPrice()), nil
  213. case types.DynamicFeeTxType:
  214. if t.block != nil {
  215. if baseFee, _ := t.block.BaseFeePerGas(ctx); baseFee != nil {
  216. // price = min(tip, gasFeeCap - baseFee) + baseFee
  217. return (hexutil.Big)(*math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee.ToInt()), tx.GasFeeCap())), nil
  218. }
  219. }
  220. return hexutil.Big(*tx.GasPrice()), nil
  221. default:
  222. return hexutil.Big(*tx.GasPrice()), nil
  223. }
  224. }
  225. func (t *Transaction) EffectiveGasPrice(ctx context.Context) (*hexutil.Big, error) {
  226. tx, err := t.resolve(ctx)
  227. if err != nil || tx == nil {
  228. return nil, err
  229. }
  230. // Pending tx
  231. if t.block == nil {
  232. return nil, nil
  233. }
  234. header, err := t.block.resolveHeader(ctx)
  235. if err != nil || header == nil {
  236. return nil, err
  237. }
  238. if header.BaseFee == nil {
  239. return (*hexutil.Big)(tx.GasPrice()), nil
  240. }
  241. return (*hexutil.Big)(math.BigMin(new(big.Int).Add(tx.GasTipCap(), header.BaseFee), tx.GasFeeCap())), nil
  242. }
  243. func (t *Transaction) MaxFeePerGas(ctx context.Context) (*hexutil.Big, error) {
  244. tx, err := t.resolve(ctx)
  245. if err != nil || tx == nil {
  246. return nil, err
  247. }
  248. switch tx.Type() {
  249. case types.AccessListTxType:
  250. return nil, nil
  251. case types.DynamicFeeTxType:
  252. return (*hexutil.Big)(tx.GasFeeCap()), nil
  253. default:
  254. return nil, nil
  255. }
  256. }
  257. func (t *Transaction) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) {
  258. tx, err := t.resolve(ctx)
  259. if err != nil || tx == nil {
  260. return nil, err
  261. }
  262. switch tx.Type() {
  263. case types.AccessListTxType:
  264. return nil, nil
  265. case types.DynamicFeeTxType:
  266. return (*hexutil.Big)(tx.GasTipCap()), nil
  267. default:
  268. return nil, nil
  269. }
  270. }
  271. func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) {
  272. tx, err := t.resolve(ctx)
  273. if err != nil || tx == nil {
  274. return nil, err
  275. }
  276. // Pending tx
  277. if t.block == nil {
  278. return nil, nil
  279. }
  280. header, err := t.block.resolveHeader(ctx)
  281. if err != nil || header == nil {
  282. return nil, err
  283. }
  284. if header.BaseFee == nil {
  285. return (*hexutil.Big)(tx.GasPrice()), nil
  286. }
  287. tip, err := tx.EffectiveGasTip(header.BaseFee)
  288. if err != nil {
  289. return nil, err
  290. }
  291. return (*hexutil.Big)(tip), nil
  292. }
  293. func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
  294. tx, err := t.resolve(ctx)
  295. if err != nil || tx == nil {
  296. return hexutil.Big{}, err
  297. }
  298. if tx.Value() == nil {
  299. return hexutil.Big{}, fmt.Errorf("invalid transaction value %x", t.hash)
  300. }
  301. return hexutil.Big(*tx.Value()), nil
  302. }
  303. func (t *Transaction) Nonce(ctx context.Context) (hexutil.Uint64, error) {
  304. tx, err := t.resolve(ctx)
  305. if err != nil || tx == nil {
  306. return 0, err
  307. }
  308. return hexutil.Uint64(tx.Nonce()), nil
  309. }
  310. func (t *Transaction) To(ctx context.Context, args BlockNumberArgs) (*Account, error) {
  311. tx, err := t.resolve(ctx)
  312. if err != nil || tx == nil {
  313. return nil, err
  314. }
  315. to := tx.To()
  316. if to == nil {
  317. return nil, nil
  318. }
  319. return &Account{
  320. r: t.r,
  321. address: *to,
  322. blockNrOrHash: args.NumberOrLatest(),
  323. }, nil
  324. }
  325. func (t *Transaction) From(ctx context.Context, args BlockNumberArgs) (*Account, error) {
  326. tx, err := t.resolve(ctx)
  327. if err != nil || tx == nil {
  328. return nil, err
  329. }
  330. signer := types.LatestSigner(t.r.backend.ChainConfig())
  331. from, _ := types.Sender(signer, tx)
  332. return &Account{
  333. r: t.r,
  334. address: from,
  335. blockNrOrHash: args.NumberOrLatest(),
  336. }, nil
  337. }
  338. func (t *Transaction) Block(ctx context.Context) (*Block, error) {
  339. if _, err := t.resolve(ctx); err != nil {
  340. return nil, err
  341. }
  342. return t.block, nil
  343. }
  344. func (t *Transaction) Index(ctx context.Context) (*int32, error) {
  345. if _, err := t.resolve(ctx); err != nil {
  346. return nil, err
  347. }
  348. if t.block == nil {
  349. return nil, nil
  350. }
  351. index := int32(t.index)
  352. return &index, nil
  353. }
  354. // getReceipt returns the receipt associated with this transaction, if any.
  355. func (t *Transaction) getReceipt(ctx context.Context) (*types.Receipt, error) {
  356. if _, err := t.resolve(ctx); err != nil {
  357. return nil, err
  358. }
  359. if t.block == nil {
  360. return nil, nil
  361. }
  362. receipts, err := t.block.resolveReceipts(ctx)
  363. if err != nil {
  364. return nil, err
  365. }
  366. return receipts[t.index], nil
  367. }
  368. func (t *Transaction) Status(ctx context.Context) (*Long, error) {
  369. receipt, err := t.getReceipt(ctx)
  370. if err != nil || receipt == nil {
  371. return nil, err
  372. }
  373. if len(receipt.PostState) != 0 {
  374. return nil, nil
  375. }
  376. ret := Long(receipt.Status)
  377. return &ret, nil
  378. }
  379. func (t *Transaction) GasUsed(ctx context.Context) (*Long, error) {
  380. receipt, err := t.getReceipt(ctx)
  381. if err != nil || receipt == nil {
  382. return nil, err
  383. }
  384. ret := Long(receipt.GasUsed)
  385. return &ret, nil
  386. }
  387. func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*Long, error) {
  388. receipt, err := t.getReceipt(ctx)
  389. if err != nil || receipt == nil {
  390. return nil, err
  391. }
  392. ret := Long(receipt.CumulativeGasUsed)
  393. return &ret, nil
  394. }
  395. func (t *Transaction) CreatedContract(ctx context.Context, args BlockNumberArgs) (*Account, error) {
  396. receipt, err := t.getReceipt(ctx)
  397. if err != nil || receipt == nil || receipt.ContractAddress == (common.Address{}) {
  398. return nil, err
  399. }
  400. return &Account{
  401. r: t.r,
  402. address: receipt.ContractAddress,
  403. blockNrOrHash: args.NumberOrLatest(),
  404. }, nil
  405. }
  406. func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) {
  407. if _, err := t.resolve(ctx); err != nil {
  408. return nil, err
  409. }
  410. if t.block == nil {
  411. return nil, nil
  412. }
  413. if _, ok := t.block.numberOrHash.Hash(); !ok {
  414. header, err := t.r.backend.HeaderByNumberOrHash(ctx, *t.block.numberOrHash)
  415. if err != nil {
  416. return nil, err
  417. }
  418. hash := header.Hash()
  419. t.block.numberOrHash.BlockHash = &hash
  420. }
  421. return t.getLogs(ctx)
  422. }
  423. // getLogs returns log objects for the given tx.
  424. // Assumes block hash is resolved.
  425. func (t *Transaction) getLogs(ctx context.Context) (*[]*Log, error) {
  426. var (
  427. hash, _ = t.block.numberOrHash.Hash()
  428. filter = t.r.filterSystem.NewBlockFilter(hash, nil, nil)
  429. logs, err = filter.Logs(ctx)
  430. )
  431. if err != nil {
  432. return nil, err
  433. }
  434. ret := make([]*Log, 0, len(logs))
  435. for _, log := range logs {
  436. ret = append(ret, &Log{
  437. r: t.r,
  438. transaction: t,
  439. log: log,
  440. })
  441. }
  442. return &ret, nil
  443. }
  444. func (t *Transaction) Type(ctx context.Context) (*int32, error) {
  445. tx, err := t.resolve(ctx)
  446. if err != nil {
  447. return nil, err
  448. }
  449. txType := int32(tx.Type())
  450. return &txType, nil
  451. }
  452. func (t *Transaction) AccessList(ctx context.Context) (*[]*AccessTuple, error) {
  453. tx, err := t.resolve(ctx)
  454. if err != nil || tx == nil {
  455. return nil, err
  456. }
  457. accessList := tx.AccessList()
  458. ret := make([]*AccessTuple, 0, len(accessList))
  459. for _, al := range accessList {
  460. ret = append(ret, &AccessTuple{
  461. address: al.Address,
  462. storageKeys: al.StorageKeys,
  463. })
  464. }
  465. return &ret, nil
  466. }
  467. func (t *Transaction) R(ctx context.Context) (hexutil.Big, error) {
  468. tx, err := t.resolve(ctx)
  469. if err != nil || tx == nil {
  470. return hexutil.Big{}, err
  471. }
  472. _, r, _ := tx.RawSignatureValues()
  473. return hexutil.Big(*r), nil
  474. }
  475. func (t *Transaction) S(ctx context.Context) (hexutil.Big, error) {
  476. tx, err := t.resolve(ctx)
  477. if err != nil || tx == nil {
  478. return hexutil.Big{}, err
  479. }
  480. _, _, s := tx.RawSignatureValues()
  481. return hexutil.Big(*s), nil
  482. }
  483. func (t *Transaction) V(ctx context.Context) (hexutil.Big, error) {
  484. tx, err := t.resolve(ctx)
  485. if err != nil || tx == nil {
  486. return hexutil.Big{}, err
  487. }
  488. v, _, _ := tx.RawSignatureValues()
  489. return hexutil.Big(*v), nil
  490. }
  491. func (t *Transaction) Raw(ctx context.Context) (hexutil.Bytes, error) {
  492. tx, err := t.resolve(ctx)
  493. if err != nil || tx == nil {
  494. return hexutil.Bytes{}, err
  495. }
  496. return tx.MarshalBinary()
  497. }
  498. func (t *Transaction) RawReceipt(ctx context.Context) (hexutil.Bytes, error) {
  499. receipt, err := t.getReceipt(ctx)
  500. if err != nil || receipt == nil {
  501. return hexutil.Bytes{}, err
  502. }
  503. return receipt.MarshalBinary()
  504. }
  505. type BlockType int
  506. // Block represents an Ethereum block.
  507. // backend, and numberOrHash are mandatory. All other fields are lazily fetched
  508. // when required.
  509. type Block struct {
  510. r *Resolver
  511. numberOrHash *rpc.BlockNumberOrHash
  512. hash common.Hash
  513. header *types.Header
  514. block *types.Block
  515. receipts []*types.Receipt
  516. }
  517. // resolve returns the internal Block object representing this block, fetching
  518. // it if necessary.
  519. func (b *Block) resolve(ctx context.Context) (*types.Block, error) {
  520. if b.block != nil {
  521. return b.block, nil
  522. }
  523. if b.numberOrHash == nil {
  524. latest := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
  525. b.numberOrHash = &latest
  526. }
  527. var err error
  528. b.block, err = b.r.backend.BlockByNumberOrHash(ctx, *b.numberOrHash)
  529. if b.block != nil && b.header == nil {
  530. b.header = b.block.Header()
  531. if hash, ok := b.numberOrHash.Hash(); ok {
  532. b.hash = hash
  533. }
  534. }
  535. return b.block, err
  536. }
  537. // resolveHeader returns the internal Header object for this block, fetching it
  538. // if necessary. Call this function instead of `resolve` unless you need the
  539. // additional data (transactions and uncles).
  540. func (b *Block) resolveHeader(ctx context.Context) (*types.Header, error) {
  541. if b.numberOrHash == nil && b.hash == (common.Hash{}) {
  542. return nil, errBlockInvariant
  543. }
  544. var err error
  545. if b.header == nil {
  546. if b.hash != (common.Hash{}) {
  547. b.header, err = b.r.backend.HeaderByHash(ctx, b.hash)
  548. } else {
  549. b.header, err = b.r.backend.HeaderByNumberOrHash(ctx, *b.numberOrHash)
  550. }
  551. }
  552. return b.header, err
  553. }
  554. // resolveReceipts returns the list of receipts for this block, fetching them
  555. // if necessary.
  556. func (b *Block) resolveReceipts(ctx context.Context) ([]*types.Receipt, error) {
  557. if b.receipts == nil {
  558. hash := b.hash
  559. if hash == (common.Hash{}) {
  560. header, err := b.resolveHeader(ctx)
  561. if err != nil {
  562. return nil, err
  563. }
  564. hash = header.Hash()
  565. }
  566. receipts, err := b.r.backend.GetReceipts(ctx, hash)
  567. if err != nil {
  568. return nil, err
  569. }
  570. b.receipts = receipts
  571. }
  572. return b.receipts, nil
  573. }
  574. func (b *Block) Number(ctx context.Context) (Long, error) {
  575. header, err := b.resolveHeader(ctx)
  576. if err != nil {
  577. return 0, err
  578. }
  579. return Long(header.Number.Uint64()), nil
  580. }
  581. func (b *Block) Hash(ctx context.Context) (common.Hash, error) {
  582. if b.hash == (common.Hash{}) {
  583. header, err := b.resolveHeader(ctx)
  584. if err != nil {
  585. return common.Hash{}, err
  586. }
  587. b.hash = header.Hash()
  588. }
  589. return b.hash, nil
  590. }
  591. func (b *Block) GasLimit(ctx context.Context) (Long, error) {
  592. header, err := b.resolveHeader(ctx)
  593. if err != nil {
  594. return 0, err
  595. }
  596. return Long(header.GasLimit), nil
  597. }
  598. func (b *Block) GasUsed(ctx context.Context) (Long, error) {
  599. header, err := b.resolveHeader(ctx)
  600. if err != nil {
  601. return 0, err
  602. }
  603. return Long(header.GasUsed), nil
  604. }
  605. func (b *Block) BaseFeePerGas(ctx context.Context) (*hexutil.Big, error) {
  606. header, err := b.resolveHeader(ctx)
  607. if err != nil {
  608. return nil, err
  609. }
  610. if header.BaseFee == nil {
  611. return nil, nil
  612. }
  613. return (*hexutil.Big)(header.BaseFee), nil
  614. }
  615. func (b *Block) NextBaseFeePerGas(ctx context.Context) (*hexutil.Big, error) {
  616. header, err := b.resolveHeader(ctx)
  617. if err != nil {
  618. return nil, err
  619. }
  620. chaincfg := b.r.backend.ChainConfig()
  621. if header.BaseFee == nil {
  622. // Make sure next block doesn't enable EIP-1559
  623. if !chaincfg.IsLondon(new(big.Int).Add(header.Number, common.Big1)) {
  624. return nil, nil
  625. }
  626. }
  627. nextBaseFee := misc.CalcBaseFee(chaincfg, header)
  628. return (*hexutil.Big)(nextBaseFee), nil
  629. }
  630. func (b *Block) Parent(ctx context.Context) (*Block, error) {
  631. if _, err := b.resolveHeader(ctx); err != nil {
  632. return nil, err
  633. }
  634. if b.header == nil || b.header.Number.Uint64() < 1 {
  635. return nil, nil
  636. }
  637. num := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(b.header.Number.Uint64() - 1))
  638. return &Block{
  639. r: b.r,
  640. numberOrHash: &num,
  641. hash: b.header.ParentHash,
  642. }, nil
  643. }
  644. func (b *Block) Difficulty(ctx context.Context) (hexutil.Big, error) {
  645. header, err := b.resolveHeader(ctx)
  646. if err != nil {
  647. return hexutil.Big{}, err
  648. }
  649. return hexutil.Big(*header.Difficulty), nil
  650. }
  651. func (b *Block) Timestamp(ctx context.Context) (hexutil.Uint64, error) {
  652. header, err := b.resolveHeader(ctx)
  653. if err != nil {
  654. return 0, err
  655. }
  656. return hexutil.Uint64(header.Time), nil
  657. }
  658. func (b *Block) Nonce(ctx context.Context) (hexutil.Bytes, error) {
  659. header, err := b.resolveHeader(ctx)
  660. if err != nil {
  661. return hexutil.Bytes{}, err
  662. }
  663. return header.Nonce[:], nil
  664. }
  665. func (b *Block) MixHash(ctx context.Context) (common.Hash, error) {
  666. header, err := b.resolveHeader(ctx)
  667. if err != nil {
  668. return common.Hash{}, err
  669. }
  670. return header.MixDigest, nil
  671. }
  672. func (b *Block) TransactionsRoot(ctx context.Context) (common.Hash, error) {
  673. header, err := b.resolveHeader(ctx)
  674. if err != nil {
  675. return common.Hash{}, err
  676. }
  677. return header.TxHash, nil
  678. }
  679. func (b *Block) StateRoot(ctx context.Context) (common.Hash, error) {
  680. header, err := b.resolveHeader(ctx)
  681. if err != nil {
  682. return common.Hash{}, err
  683. }
  684. return header.Root, nil
  685. }
  686. func (b *Block) ReceiptsRoot(ctx context.Context) (common.Hash, error) {
  687. header, err := b.resolveHeader(ctx)
  688. if err != nil {
  689. return common.Hash{}, err
  690. }
  691. return header.ReceiptHash, nil
  692. }
  693. func (b *Block) OmmerHash(ctx context.Context) (common.Hash, error) {
  694. header, err := b.resolveHeader(ctx)
  695. if err != nil {
  696. return common.Hash{}, err
  697. }
  698. return header.UncleHash, nil
  699. }
  700. func (b *Block) OmmerCount(ctx context.Context) (*int32, error) {
  701. block, err := b.resolve(ctx)
  702. if err != nil || block == nil {
  703. return nil, err
  704. }
  705. count := int32(len(block.Uncles()))
  706. return &count, err
  707. }
  708. func (b *Block) Ommers(ctx context.Context) (*[]*Block, error) {
  709. block, err := b.resolve(ctx)
  710. if err != nil || block == nil {
  711. return nil, err
  712. }
  713. ret := make([]*Block, 0, len(block.Uncles()))
  714. for _, uncle := range block.Uncles() {
  715. blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
  716. ret = append(ret, &Block{
  717. r: b.r,
  718. numberOrHash: &blockNumberOrHash,
  719. header: uncle,
  720. })
  721. }
  722. return &ret, nil
  723. }
  724. func (b *Block) ExtraData(ctx context.Context) (hexutil.Bytes, error) {
  725. header, err := b.resolveHeader(ctx)
  726. if err != nil {
  727. return hexutil.Bytes{}, err
  728. }
  729. return header.Extra, nil
  730. }
  731. func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) {
  732. header, err := b.resolveHeader(ctx)
  733. if err != nil {
  734. return hexutil.Bytes{}, err
  735. }
  736. return header.Bloom.Bytes(), nil
  737. }
  738. func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
  739. h := b.hash
  740. if h == (common.Hash{}) {
  741. header, err := b.resolveHeader(ctx)
  742. if err != nil {
  743. return hexutil.Big{}, err
  744. }
  745. h = header.Hash()
  746. }
  747. td := b.r.backend.GetTd(ctx, h)
  748. if td == nil {
  749. return hexutil.Big{}, fmt.Errorf("total difficulty not found %x", b.hash)
  750. }
  751. return hexutil.Big(*td), nil
  752. }
  753. func (b *Block) RawHeader(ctx context.Context) (hexutil.Bytes, error) {
  754. header, err := b.resolveHeader(ctx)
  755. if err != nil {
  756. return hexutil.Bytes{}, err
  757. }
  758. return rlp.EncodeToBytes(header)
  759. }
  760. func (b *Block) Raw(ctx context.Context) (hexutil.Bytes, error) {
  761. block, err := b.resolve(ctx)
  762. if err != nil {
  763. return hexutil.Bytes{}, err
  764. }
  765. return rlp.EncodeToBytes(block)
  766. }
  767. // BlockNumberArgs encapsulates arguments to accessors that specify a block number.
  768. type BlockNumberArgs struct {
  769. // TODO: Ideally we could use input unions to allow the query to specify the
  770. // block parameter by hash, block number, or tag but input unions aren't part of the
  771. // standard GraphQL schema SDL yet, see: https://github.com/graphql/graphql-spec/issues/488
  772. Block *hexutil.Uint64
  773. }
  774. // NumberOr returns the provided block number argument, or the "current" block number or hash if none
  775. // was provided.
  776. func (a BlockNumberArgs) NumberOr(current rpc.BlockNumberOrHash) rpc.BlockNumberOrHash {
  777. if a.Block != nil {
  778. blockNr := rpc.BlockNumber(*a.Block)
  779. return rpc.BlockNumberOrHashWithNumber(blockNr)
  780. }
  781. return current
  782. }
  783. // NumberOrLatest returns the provided block number argument, or the "latest" block number if none
  784. // was provided.
  785. func (a BlockNumberArgs) NumberOrLatest() rpc.BlockNumberOrHash {
  786. return a.NumberOr(rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
  787. }
  788. func (b *Block) Miner(ctx context.Context, args BlockNumberArgs) (*Account, error) {
  789. header, err := b.resolveHeader(ctx)
  790. if err != nil {
  791. return nil, err
  792. }
  793. return &Account{
  794. r: b.r,
  795. address: header.Coinbase,
  796. blockNrOrHash: args.NumberOrLatest(),
  797. }, nil
  798. }
  799. func (b *Block) TransactionCount(ctx context.Context) (*int32, error) {
  800. block, err := b.resolve(ctx)
  801. if err != nil || block == nil {
  802. return nil, err
  803. }
  804. count := int32(len(block.Transactions()))
  805. return &count, err
  806. }
  807. func (b *Block) Transactions(ctx context.Context) (*[]*Transaction, error) {
  808. block, err := b.resolve(ctx)
  809. if err != nil || block == nil {
  810. return nil, err
  811. }
  812. ret := make([]*Transaction, 0, len(block.Transactions()))
  813. for i, tx := range block.Transactions() {
  814. ret = append(ret, &Transaction{
  815. r: b.r,
  816. hash: tx.Hash(),
  817. tx: tx,
  818. block: b,
  819. index: uint64(i),
  820. })
  821. }
  822. return &ret, nil
  823. }
  824. func (b *Block) TransactionAt(ctx context.Context, args struct{ Index int32 }) (*Transaction, error) {
  825. block, err := b.resolve(ctx)
  826. if err != nil || block == nil {
  827. return nil, err
  828. }
  829. txs := block.Transactions()
  830. if args.Index < 0 || int(args.Index) >= len(txs) {
  831. return nil, nil
  832. }
  833. tx := txs[args.Index]
  834. return &Transaction{
  835. r: b.r,
  836. hash: tx.Hash(),
  837. tx: tx,
  838. block: b,
  839. index: uint64(args.Index),
  840. }, nil
  841. }
  842. func (b *Block) OmmerAt(ctx context.Context, args struct{ Index int32 }) (*Block, error) {
  843. block, err := b.resolve(ctx)
  844. if err != nil || block == nil {
  845. return nil, err
  846. }
  847. uncles := block.Uncles()
  848. if args.Index < 0 || int(args.Index) >= len(uncles) {
  849. return nil, nil
  850. }
  851. uncle := uncles[args.Index]
  852. blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
  853. return &Block{
  854. r: b.r,
  855. numberOrHash: &blockNumberOrHash,
  856. header: uncle,
  857. }, nil
  858. }
  859. // BlockFilterCriteria encapsulates criteria passed to a `logs` accessor inside
  860. // a block.
  861. type BlockFilterCriteria struct {
  862. Addresses *[]common.Address // restricts matches to events created by specific contracts
  863. // The Topic list restricts matches to particular event topics. Each event has a list
  864. // of topics. Topics matches a prefix of that list. An empty element slice matches any
  865. // topic. Non-empty elements represent an alternative that matches any of the
  866. // contained topics.
  867. //
  868. // Examples:
  869. // {} or nil matches any topic list
  870. // {{A}} matches topic A in first position
  871. // {{}, {B}} matches any topic in first position, B in second position
  872. // {{A}, {B}} matches topic A in first position, B in second position
  873. // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
  874. Topics *[][]common.Hash
  875. }
  876. // runFilter accepts a filter and executes it, returning all its results as
  877. // `Log` objects.
  878. func runFilter(ctx context.Context, r *Resolver, filter *filters.Filter) ([]*Log, error) {
  879. logs, err := filter.Logs(ctx)
  880. if err != nil || logs == nil {
  881. return nil, err
  882. }
  883. ret := make([]*Log, 0, len(logs))
  884. for _, log := range logs {
  885. ret = append(ret, &Log{
  886. r: r,
  887. transaction: &Transaction{r: r, hash: log.TxHash},
  888. log: log,
  889. })
  890. }
  891. return ret, nil
  892. }
  893. func (b *Block) Logs(ctx context.Context, args struct{ Filter BlockFilterCriteria }) ([]*Log, error) {
  894. var addresses []common.Address
  895. if args.Filter.Addresses != nil {
  896. addresses = *args.Filter.Addresses
  897. }
  898. var topics [][]common.Hash
  899. if args.Filter.Topics != nil {
  900. topics = *args.Filter.Topics
  901. }
  902. hash := b.hash
  903. if hash == (common.Hash{}) {
  904. header, err := b.resolveHeader(ctx)
  905. if err != nil {
  906. return nil, err
  907. }
  908. hash = header.Hash()
  909. }
  910. // Construct the range filter
  911. filter := b.r.filterSystem.NewBlockFilter(hash, addresses, topics)
  912. // Run the filter and return all the logs
  913. return runFilter(ctx, b.r, filter)
  914. }
  915. func (b *Block) Account(ctx context.Context, args struct {
  916. Address common.Address
  917. }) (*Account, error) {
  918. if b.numberOrHash == nil {
  919. _, err := b.resolveHeader(ctx)
  920. if err != nil {
  921. return nil, err
  922. }
  923. }
  924. return &Account{
  925. r: b.r,
  926. address: args.Address,
  927. blockNrOrHash: *b.numberOrHash,
  928. }, nil
  929. }
  930. // CallData encapsulates arguments to `call` or `estimateGas`.
  931. // All arguments are optional.
  932. type CallData struct {
  933. From *common.Address // The Ethereum address the call is from.
  934. To *common.Address // The Ethereum address the call is to.
  935. Gas *hexutil.Uint64 // The amount of gas provided for the call.
  936. GasPrice *hexutil.Big // The price of each unit of gas, in wei.
  937. MaxFeePerGas *hexutil.Big // The max price of each unit of gas, in wei (1559).
  938. MaxPriorityFeePerGas *hexutil.Big // The max tip of each unit of gas, in wei (1559).
  939. Value *hexutil.Big // The value sent along with the call.
  940. Data *hexutil.Bytes // Any data sent with the call.
  941. }
  942. // CallResult encapsulates the result of an invocation of the `call` accessor.
  943. type CallResult struct {
  944. data hexutil.Bytes // The return data from the call
  945. gasUsed Long // The amount of gas used
  946. status Long // The return status of the call - 0 for failure or 1 for success.
  947. }
  948. func (c *CallResult) Data() hexutil.Bytes {
  949. return c.data
  950. }
  951. func (c *CallResult) GasUsed() Long {
  952. return c.gasUsed
  953. }
  954. func (c *CallResult) Status() Long {
  955. return c.status
  956. }
  957. func (b *Block) Call(ctx context.Context, args struct {
  958. Data ethapi.TransactionArgs
  959. }) (*CallResult, error) {
  960. if b.numberOrHash == nil {
  961. _, err := b.resolve(ctx)
  962. if err != nil {
  963. return nil, err
  964. }
  965. }
  966. result, err := ethapi.DoCall(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCEVMTimeout(), b.r.backend.RPCGasCap())
  967. if err != nil {
  968. return nil, err
  969. }
  970. status := Long(1)
  971. if result.Failed() {
  972. status = 0
  973. }
  974. return &CallResult{
  975. data: result.ReturnData,
  976. gasUsed: Long(result.UsedGas),
  977. status: status,
  978. }, nil
  979. }
  980. func (b *Block) EstimateGas(ctx context.Context, args struct {
  981. Data ethapi.TransactionArgs
  982. }) (Long, error) {
  983. if b.numberOrHash == nil {
  984. _, err := b.resolveHeader(ctx)
  985. if err != nil {
  986. return 0, err
  987. }
  988. }
  989. gas, err := ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, b.r.backend.RPCGasCap())
  990. return Long(gas), err
  991. }
  992. type Pending struct {
  993. r *Resolver
  994. }
  995. func (p *Pending) TransactionCount(ctx context.Context) (int32, error) {
  996. txs, err := p.r.backend.GetPoolTransactions()
  997. return int32(len(txs)), err
  998. }
  999. func (p *Pending) Transactions(ctx context.Context) (*[]*Transaction, error) {
  1000. txs, err := p.r.backend.GetPoolTransactions()
  1001. if err != nil {
  1002. return nil, err
  1003. }
  1004. ret := make([]*Transaction, 0, len(txs))
  1005. for i, tx := range txs {
  1006. ret = append(ret, &Transaction{
  1007. r: p.r,
  1008. hash: tx.Hash(),
  1009. tx: tx,
  1010. index: uint64(i),
  1011. })
  1012. }
  1013. return &ret, nil
  1014. }
  1015. func (p *Pending) Account(ctx context.Context, args struct {
  1016. Address common.Address
  1017. }) *Account {
  1018. pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
  1019. return &Account{
  1020. r: p.r,
  1021. address: args.Address,
  1022. blockNrOrHash: pendingBlockNr,
  1023. }
  1024. }
  1025. func (p *Pending) Call(ctx context.Context, args struct {
  1026. Data ethapi.TransactionArgs
  1027. }) (*CallResult, error) {
  1028. pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
  1029. result, err := ethapi.DoCall(ctx, p.r.backend, args.Data, pendingBlockNr, nil, p.r.backend.RPCEVMTimeout(), p.r.backend.RPCGasCap())
  1030. if err != nil {
  1031. return nil, err
  1032. }
  1033. status := Long(1)
  1034. if result.Failed() {
  1035. status = 0
  1036. }
  1037. return &CallResult{
  1038. data: result.ReturnData,
  1039. gasUsed: Long(result.UsedGas),
  1040. status: status,
  1041. }, nil
  1042. }
  1043. func (p *Pending) EstimateGas(ctx context.Context, args struct {
  1044. Data ethapi.TransactionArgs
  1045. }) (Long, error) {
  1046. pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
  1047. gas, err := ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, pendingBlockNr, p.r.backend.RPCGasCap())
  1048. return Long(gas), err
  1049. }
  1050. // Resolver is the top-level object in the GraphQL hierarchy.
  1051. type Resolver struct {
  1052. backend ethapi.Backend
  1053. filterSystem *filters.FilterSystem
  1054. }
  1055. func (r *Resolver) Block(ctx context.Context, args struct {
  1056. Number *Long
  1057. Hash *common.Hash
  1058. }) (*Block, error) {
  1059. var block *Block
  1060. if args.Number != nil {
  1061. if *args.Number < 0 {
  1062. return nil, nil
  1063. }
  1064. number := rpc.BlockNumber(*args.Number)
  1065. numberOrHash := rpc.BlockNumberOrHashWithNumber(number)
  1066. block = &Block{
  1067. r: r,
  1068. numberOrHash: &numberOrHash,
  1069. }
  1070. } else if args.Hash != nil {
  1071. numberOrHash := rpc.BlockNumberOrHashWithHash(*args.Hash, false)
  1072. block = &Block{
  1073. r: r,
  1074. numberOrHash: &numberOrHash,
  1075. }
  1076. } else {
  1077. numberOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
  1078. block = &Block{
  1079. r: r,
  1080. numberOrHash: &numberOrHash,
  1081. }
  1082. }
  1083. // Resolve the header, return nil if it doesn't exist.
  1084. // Note we don't resolve block directly here since it will require an
  1085. // additional network request for light client.
  1086. h, err := block.resolveHeader(ctx)
  1087. if err != nil {
  1088. return nil, err
  1089. } else if h == nil {
  1090. return nil, nil
  1091. }
  1092. return block, nil
  1093. }
  1094. func (r *Resolver) Blocks(ctx context.Context, args struct {
  1095. From *Long
  1096. To *Long
  1097. }) ([]*Block, error) {
  1098. from := rpc.BlockNumber(*args.From)
  1099. var to rpc.BlockNumber
  1100. if args.To != nil {
  1101. to = rpc.BlockNumber(*args.To)
  1102. } else {
  1103. to = rpc.BlockNumber(r.backend.CurrentBlock().Number().Int64())
  1104. }
  1105. if to < from {
  1106. return []*Block{}, nil
  1107. }
  1108. ret := make([]*Block, 0, to-from+1)
  1109. for i := from; i <= to; i++ {
  1110. numberOrHash := rpc.BlockNumberOrHashWithNumber(i)
  1111. block := &Block{
  1112. r: r,
  1113. numberOrHash: &numberOrHash,
  1114. }
  1115. // Resolve the header to check for existence.
  1116. // Note we don't resolve block directly here since it will require an
  1117. // additional network request for light client.
  1118. h, err := block.resolveHeader(ctx)
  1119. if err != nil {
  1120. return nil, err
  1121. } else if h == nil {
  1122. // Blocks after must be non-existent too, break.
  1123. break
  1124. }
  1125. ret = append(ret, block)
  1126. }
  1127. return ret, nil
  1128. }
  1129. func (r *Resolver) Pending(ctx context.Context) *Pending {
  1130. return &Pending{r}
  1131. }
  1132. func (r *Resolver) Transaction(ctx context.Context, args struct{ Hash common.Hash }) (*Transaction, error) {
  1133. tx := &Transaction{
  1134. r: r,
  1135. hash: args.Hash,
  1136. }
  1137. // Resolve the transaction; if it doesn't exist, return nil.
  1138. t, err := tx.resolve(ctx)
  1139. if err != nil {
  1140. return nil, err
  1141. } else if t == nil {
  1142. return nil, nil
  1143. }
  1144. return tx, nil
  1145. }
  1146. func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data hexutil.Bytes }) (common.Hash, error) {
  1147. tx := new(types.Transaction)
  1148. if err := tx.UnmarshalBinary(args.Data); err != nil {
  1149. return common.Hash{}, err
  1150. }
  1151. hash, err := ethapi.SubmitTransaction(ctx, r.backend, tx)
  1152. return hash, err
  1153. }
  1154. // FilterCriteria encapsulates the arguments to `logs` on the root resolver object.
  1155. type FilterCriteria struct {
  1156. FromBlock *hexutil.Uint64 // beginning of the queried range, nil means genesis block
  1157. ToBlock *hexutil.Uint64 // end of the range, nil means latest block
  1158. Addresses *[]common.Address // restricts matches to events created by specific contracts
  1159. // The Topic list restricts matches to particular event topics. Each event has a list
  1160. // of topics. Topics matches a prefix of that list. An empty element slice matches any
  1161. // topic. Non-empty elements represent an alternative that matches any of the
  1162. // contained topics.
  1163. //
  1164. // Examples:
  1165. // {} or nil matches any topic list
  1166. // {{A}} matches topic A in first position
  1167. // {{}, {B}} matches any topic in first position, B in second position
  1168. // {{A}, {B}} matches topic A in first position, B in second position
  1169. // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
  1170. Topics *[][]common.Hash
  1171. }
  1172. func (r *Resolver) Logs(ctx context.Context, args struct{ Filter FilterCriteria }) ([]*Log, error) {
  1173. // Convert the RPC block numbers into internal representations
  1174. begin := rpc.LatestBlockNumber.Int64()
  1175. if args.Filter.FromBlock != nil {
  1176. begin = int64(*args.Filter.FromBlock)
  1177. }
  1178. end := rpc.LatestBlockNumber.Int64()
  1179. if args.Filter.ToBlock != nil {
  1180. end = int64(*args.Filter.ToBlock)
  1181. }
  1182. var addresses []common.Address
  1183. if args.Filter.Addresses != nil {
  1184. addresses = *args.Filter.Addresses
  1185. }
  1186. var topics [][]common.Hash
  1187. if args.Filter.Topics != nil {
  1188. topics = *args.Filter.Topics
  1189. }
  1190. // Construct the range filter
  1191. filter := r.filterSystem.NewRangeFilter(begin, end, addresses, topics)
  1192. return runFilter(ctx, r, filter)
  1193. }
  1194. func (r *Resolver) GasPrice(ctx context.Context) (hexutil.Big, error) {
  1195. tipcap, err := r.backend.SuggestGasTipCap(ctx)
  1196. if err != nil {
  1197. return hexutil.Big{}, err
  1198. }
  1199. if head := r.backend.CurrentHeader(); head.BaseFee != nil {
  1200. tipcap.Add(tipcap, head.BaseFee)
  1201. }
  1202. return (hexutil.Big)(*tipcap), nil
  1203. }
  1204. func (r *Resolver) MaxPriorityFeePerGas(ctx context.Context) (hexutil.Big, error) {
  1205. tipcap, err := r.backend.SuggestGasTipCap(ctx)
  1206. if err != nil {
  1207. return hexutil.Big{}, err
  1208. }
  1209. return (hexutil.Big)(*tipcap), nil
  1210. }
  1211. func (r *Resolver) ChainID(ctx context.Context) (hexutil.Big, error) {
  1212. header, _ := r.backend.HeaderByNumber(context.Background(), rpc.LatestBlockNumber)
  1213. if header != nil && r.backend.ChainConfig().IsEthPoWFork(header.Number) {
  1214. return hexutil.Big(*r.backend.ChainConfig().ChainID_ALT), nil
  1215. }
  1216. return hexutil.Big(*r.backend.ChainConfig().ChainID), nil
  1217. }
  1218. // SyncState represents the synchronisation status returned from the `syncing` accessor.
  1219. type SyncState struct {
  1220. progress ethereum.SyncProgress
  1221. }
  1222. func (s *SyncState) StartingBlock() hexutil.Uint64 {
  1223. return hexutil.Uint64(s.progress.StartingBlock)
  1224. }
  1225. func (s *SyncState) CurrentBlock() hexutil.Uint64 {
  1226. return hexutil.Uint64(s.progress.CurrentBlock)
  1227. }
  1228. func (s *SyncState) HighestBlock() hexutil.Uint64 {
  1229. return hexutil.Uint64(s.progress.HighestBlock)
  1230. }
  1231. func (s *SyncState) SyncedAccounts() hexutil.Uint64 {
  1232. return hexutil.Uint64(s.progress.SyncedAccounts)
  1233. }
  1234. func (s *SyncState) SyncedAccountBytes() hexutil.Uint64 {
  1235. return hexutil.Uint64(s.progress.SyncedAccountBytes)
  1236. }
  1237. func (s *SyncState) SyncedBytecodes() hexutil.Uint64 {
  1238. return hexutil.Uint64(s.progress.SyncedBytecodes)
  1239. }
  1240. func (s *SyncState) SyncedBytecodeBytes() hexutil.Uint64 {
  1241. return hexutil.Uint64(s.progress.SyncedBytecodeBytes)
  1242. }
  1243. func (s *SyncState) SyncedStorage() hexutil.Uint64 {
  1244. return hexutil.Uint64(s.progress.SyncedStorage)
  1245. }
  1246. func (s *SyncState) SyncedStorageBytes() hexutil.Uint64 {
  1247. return hexutil.Uint64(s.progress.SyncedStorageBytes)
  1248. }
  1249. func (s *SyncState) HealedTrienodes() hexutil.Uint64 {
  1250. return hexutil.Uint64(s.progress.HealedTrienodes)
  1251. }
  1252. func (s *SyncState) HealedTrienodeBytes() hexutil.Uint64 {
  1253. return hexutil.Uint64(s.progress.HealedTrienodeBytes)
  1254. }
  1255. func (s *SyncState) HealedBytecodes() hexutil.Uint64 {
  1256. return hexutil.Uint64(s.progress.HealedBytecodes)
  1257. }
  1258. func (s *SyncState) HealedBytecodeBytes() hexutil.Uint64 {
  1259. return hexutil.Uint64(s.progress.HealedBytecodeBytes)
  1260. }
  1261. func (s *SyncState) HealingTrienodes() hexutil.Uint64 {
  1262. return hexutil.Uint64(s.progress.HealingTrienodes)
  1263. }
  1264. func (s *SyncState) HealingBytecode() hexutil.Uint64 {
  1265. return hexutil.Uint64(s.progress.HealingBytecode)
  1266. }
  1267. // Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not
  1268. // yet received the latest block headers from its pears. In case it is synchronizing:
  1269. // - startingBlock: block number this node started to synchronise from
  1270. // - currentBlock: block number this node is currently importing
  1271. // - highestBlock: block number of the highest block header this node has received from peers
  1272. // - syncedAccounts: number of accounts downloaded
  1273. // - syncedAccountBytes: number of account trie bytes persisted to disk
  1274. // - syncedBytecodes: number of bytecodes downloaded
  1275. // - syncedBytecodeBytes: number of bytecode bytes downloaded
  1276. // - syncedStorage: number of storage slots downloaded
  1277. // - syncedStorageBytes: number of storage trie bytes persisted to disk
  1278. // - healedTrienodes: number of state trie nodes downloaded
  1279. // - healedTrienodeBytes: number of state trie bytes persisted to disk
  1280. // - healedBytecodes: number of bytecodes downloaded
  1281. // - healedBytecodeBytes: number of bytecodes persisted to disk
  1282. // - healingTrienodes: number of state trie nodes pending
  1283. // - healingBytecode: number of bytecodes pending
  1284. func (r *Resolver) Syncing() (*SyncState, error) {
  1285. progress := r.backend.SyncProgress()
  1286. // Return not syncing if the synchronisation already completed
  1287. if progress.CurrentBlock >= progress.HighestBlock {
  1288. return nil, nil
  1289. }
  1290. // Otherwise gather the block sync stats
  1291. return &SyncState{progress}, nil
  1292. }