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