github.com/Night-mk/quorum@v21.1.0+incompatible/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 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/core/rawdb" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/core/vm" 30 "github.com/ethereum/go-ethereum/eth/filters" 31 "github.com/ethereum/go-ethereum/internal/ethapi" 32 "github.com/ethereum/go-ethereum/private" 33 "github.com/ethereum/go-ethereum/rlp" 34 "github.com/ethereum/go-ethereum/rpc" 35 ) 36 37 var ( 38 errBlockInvariant = errors.New("block objects must be instantiated with at least one of num or hash") 39 ) 40 41 // Account represents an Ethereum account at a particular block. 42 type Account struct { 43 backend ethapi.Backend 44 address common.Address 45 blockNrOrHash rpc.BlockNumberOrHash 46 } 47 48 // getState fetches the StateDB object for an account. 49 func (a *Account) getState(ctx context.Context) (vm.MinimalApiState, error) { 50 stat, _, err := a.backend.StateAndHeaderByNumberOrHash(ctx, a.blockNrOrHash) 51 return stat, err 52 } 53 54 func (a *Account) Address(ctx context.Context) (common.Address, error) { 55 return a.address, nil 56 } 57 58 func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) { 59 state, err := a.getState(ctx) 60 if err != nil { 61 return hexutil.Big{}, err 62 } 63 return hexutil.Big(*state.GetBalance(a.address)), nil 64 } 65 66 func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) { 67 state, err := a.getState(ctx) 68 if err != nil { 69 return 0, err 70 } 71 return hexutil.Uint64(state.GetNonce(a.address)), nil 72 } 73 74 func (a *Account) Code(ctx context.Context) (hexutil.Bytes, error) { 75 state, err := a.getState(ctx) 76 if err != nil { 77 return hexutil.Bytes{}, err 78 } 79 return hexutil.Bytes(state.GetCode(a.address)), nil 80 } 81 82 func (a *Account) Storage(ctx context.Context, args struct{ Slot common.Hash }) (common.Hash, error) { 83 state, err := a.getState(ctx) 84 if err != nil { 85 return common.Hash{}, err 86 } 87 return state.GetState(a.address, args.Slot), nil 88 } 89 90 // Log represents an individual log message. All arguments are mandatory. 91 type Log struct { 92 backend ethapi.Backend 93 transaction *Transaction 94 log *types.Log 95 } 96 97 func (l *Log) Transaction(ctx context.Context) *Transaction { 98 return l.transaction 99 } 100 101 func (l *Log) Account(ctx context.Context, args BlockNumberArgs) *Account { 102 return &Account{ 103 backend: l.backend, 104 address: l.log.Address, 105 blockNrOrHash: args.NumberOrLatest(), 106 } 107 } 108 109 func (l *Log) Index(ctx context.Context) int32 { 110 return int32(l.log.Index) 111 } 112 113 func (l *Log) Topics(ctx context.Context) []common.Hash { 114 return l.log.Topics 115 } 116 117 func (l *Log) Data(ctx context.Context) hexutil.Bytes { 118 return hexutil.Bytes(l.log.Data) 119 } 120 121 // Transaction represents an Ethereum transaction. 122 // backend and hash are mandatory; all others will be fetched when required. 123 type Transaction struct { 124 backend ethapi.Backend 125 hash common.Hash 126 tx *types.Transaction 127 block *Block 128 index uint64 129 } 130 131 // resolve returns the internal transaction object, fetching it if needed. 132 func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) { 133 if t.tx == nil { 134 tx, blockHash, _, index := rawdb.ReadTransaction(t.backend.ChainDb(), t.hash) 135 if tx != nil { 136 t.tx = tx 137 blockNrOrHash := rpc.BlockNumberOrHashWithHash(blockHash, false) 138 t.block = &Block{ 139 backend: t.backend, 140 numberOrHash: &blockNrOrHash, 141 } 142 t.index = index 143 } else { 144 t.tx = t.backend.GetPoolTransaction(t.hash) 145 } 146 } 147 return t.tx, nil 148 } 149 150 func (t *Transaction) Hash(ctx context.Context) common.Hash { 151 return t.hash 152 } 153 154 func (t *Transaction) InputData(ctx context.Context) (hexutil.Bytes, error) { 155 tx, err := t.resolve(ctx) 156 if err != nil || tx == nil { 157 return hexutil.Bytes{}, err 158 } 159 return hexutil.Bytes(tx.Data()), nil 160 } 161 162 func (t *Transaction) Gas(ctx context.Context) (hexutil.Uint64, error) { 163 tx, err := t.resolve(ctx) 164 if err != nil || tx == nil { 165 return 0, err 166 } 167 return hexutil.Uint64(tx.Gas()), nil 168 } 169 170 func (t *Transaction) GasPrice(ctx context.Context) (hexutil.Big, error) { 171 tx, err := t.resolve(ctx) 172 if err != nil || tx == nil { 173 return hexutil.Big{}, err 174 } 175 return hexutil.Big(*tx.GasPrice()), nil 176 } 177 178 func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) { 179 tx, err := t.resolve(ctx) 180 if err != nil || tx == nil { 181 return hexutil.Big{}, err 182 } 183 return hexutil.Big(*tx.Value()), nil 184 } 185 186 func (t *Transaction) Nonce(ctx context.Context) (hexutil.Uint64, error) { 187 tx, err := t.resolve(ctx) 188 if err != nil || tx == nil { 189 return 0, err 190 } 191 return hexutil.Uint64(tx.Nonce()), nil 192 } 193 194 func (t *Transaction) To(ctx context.Context, args BlockNumberArgs) (*Account, error) { 195 tx, err := t.resolve(ctx) 196 if err != nil || tx == nil { 197 return nil, err 198 } 199 to := tx.To() 200 if to == nil { 201 return nil, nil 202 } 203 return &Account{ 204 backend: t.backend, 205 address: *to, 206 blockNrOrHash: args.NumberOrLatest(), 207 }, nil 208 } 209 210 func (t *Transaction) From(ctx context.Context, args BlockNumberArgs) (*Account, error) { 211 tx, err := t.resolve(ctx) 212 if err != nil || tx == nil { 213 return nil, err 214 } 215 var signer types.Signer = types.HomesteadSigner{} 216 if tx.Protected() { 217 signer = types.NewEIP155Signer(tx.ChainId()) 218 } 219 from, _ := types.Sender(signer, tx) 220 221 return &Account{ 222 backend: t.backend, 223 address: from, 224 blockNrOrHash: args.NumberOrLatest(), 225 }, nil 226 } 227 228 func (t *Transaction) Block(ctx context.Context) (*Block, error) { 229 if _, err := t.resolve(ctx); err != nil { 230 return nil, err 231 } 232 return t.block, nil 233 } 234 235 func (t *Transaction) Index(ctx context.Context) (*int32, error) { 236 if _, err := t.resolve(ctx); err != nil { 237 return nil, err 238 } 239 if t.block == nil { 240 return nil, nil 241 } 242 index := int32(t.index) 243 return &index, nil 244 } 245 246 // getReceipt returns the receipt associated with this transaction, if any. 247 func (t *Transaction) getReceipt(ctx context.Context) (*types.Receipt, error) { 248 if _, err := t.resolve(ctx); err != nil { 249 return nil, err 250 } 251 if t.block == nil { 252 return nil, nil 253 } 254 receipts, err := t.block.resolveReceipts(ctx) 255 if err != nil { 256 return nil, err 257 } 258 return receipts[t.index], nil 259 } 260 261 func (t *Transaction) Status(ctx context.Context) (*hexutil.Uint64, error) { 262 receipt, err := t.getReceipt(ctx) 263 if err != nil || receipt == nil { 264 return nil, err 265 } 266 ret := hexutil.Uint64(receipt.Status) 267 return &ret, nil 268 } 269 270 func (t *Transaction) GasUsed(ctx context.Context) (*hexutil.Uint64, error) { 271 receipt, err := t.getReceipt(ctx) 272 if err != nil || receipt == nil { 273 return nil, err 274 } 275 ret := hexutil.Uint64(receipt.GasUsed) 276 return &ret, nil 277 } 278 279 func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*hexutil.Uint64, error) { 280 receipt, err := t.getReceipt(ctx) 281 if err != nil || receipt == nil { 282 return nil, err 283 } 284 ret := hexutil.Uint64(receipt.CumulativeGasUsed) 285 return &ret, nil 286 } 287 288 func (t *Transaction) CreatedContract(ctx context.Context, args BlockNumberArgs) (*Account, error) { 289 receipt, err := t.getReceipt(ctx) 290 if err != nil || receipt == nil || receipt.ContractAddress == (common.Address{}) { 291 return nil, err 292 } 293 return &Account{ 294 backend: t.backend, 295 address: receipt.ContractAddress, 296 blockNrOrHash: args.NumberOrLatest(), 297 }, nil 298 } 299 300 func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) { 301 receipt, err := t.getReceipt(ctx) 302 if err != nil || receipt == nil { 303 return nil, err 304 } 305 ret := make([]*Log, 0, len(receipt.Logs)) 306 for _, log := range receipt.Logs { 307 ret = append(ret, &Log{ 308 backend: t.backend, 309 transaction: t, 310 log: log, 311 }) 312 } 313 return &ret, nil 314 } 315 316 func (t *Transaction) R(ctx context.Context) (hexutil.Big, error) { 317 tx, err := t.resolve(ctx) 318 if err != nil || tx == nil { 319 return hexutil.Big{}, err 320 } 321 _, r, _ := tx.RawSignatureValues() 322 return hexutil.Big(*r), nil 323 } 324 325 func (t *Transaction) S(ctx context.Context) (hexutil.Big, error) { 326 tx, err := t.resolve(ctx) 327 if err != nil || tx == nil { 328 return hexutil.Big{}, err 329 } 330 _, _, s := tx.RawSignatureValues() 331 return hexutil.Big(*s), nil 332 } 333 334 func (t *Transaction) V(ctx context.Context) (hexutil.Big, error) { 335 tx, err := t.resolve(ctx) 336 if err != nil || tx == nil { 337 return hexutil.Big{}, err 338 } 339 v, _, _ := tx.RawSignatureValues() 340 return hexutil.Big(*v), nil 341 } 342 343 // Quorum 344 func (t *Transaction) IsPrivate(ctx context.Context) (*bool, error) { 345 ret := false 346 tx, err := t.resolve(ctx) 347 if err != nil || tx == nil { 348 return &ret, err 349 } 350 ret = tx.IsPrivate() 351 return &ret, nil 352 } 353 354 func (t *Transaction) PrivateInputData(ctx context.Context) (*hexutil.Bytes, error) { 355 tx, err := t.resolve(ctx) 356 if err != nil || tx == nil { 357 return &hexutil.Bytes{}, err 358 } 359 if tx.IsPrivate() { 360 _, _, privateInputData, _, err := private.P.Receive(common.BytesToEncryptedPayloadHash(tx.Data())) 361 if err != nil || tx == nil { 362 return &hexutil.Bytes{}, err 363 } 364 ret := hexutil.Bytes(privateInputData) 365 return &ret, nil 366 } 367 return &hexutil.Bytes{}, nil 368 } 369 370 // END QUORUM 371 372 type BlockType int 373 374 // Block represents an Ethereum block. 375 // backend, and numberOrHash are mandatory. All other fields are lazily fetched 376 // when required. 377 type Block struct { 378 backend ethapi.Backend 379 numberOrHash *rpc.BlockNumberOrHash 380 hash common.Hash 381 header *types.Header 382 block *types.Block 383 receipts []*types.Receipt 384 } 385 386 // resolve returns the internal Block object representing this block, fetching 387 // it if necessary. 388 func (b *Block) resolve(ctx context.Context) (*types.Block, error) { 389 if b.block != nil { 390 return b.block, nil 391 } 392 if b.numberOrHash == nil { 393 latest := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) 394 b.numberOrHash = &latest 395 } 396 var err error 397 b.block, err = b.backend.BlockByNumberOrHash(ctx, *b.numberOrHash) 398 if b.block != nil && b.header == nil { 399 b.header = b.block.Header() 400 if hash, ok := b.numberOrHash.Hash(); ok { 401 b.hash = hash 402 } 403 } 404 return b.block, err 405 } 406 407 // resolveHeader returns the internal Header object for this block, fetching it 408 // if necessary. Call this function instead of `resolve` unless you need the 409 // additional data (transactions and uncles). 410 func (b *Block) resolveHeader(ctx context.Context) (*types.Header, error) { 411 if b.numberOrHash == nil && b.hash == (common.Hash{}) { 412 return nil, errBlockInvariant 413 } 414 var err error 415 if b.header == nil { 416 if b.hash != (common.Hash{}) { 417 b.header, err = b.backend.HeaderByHash(ctx, b.hash) 418 } else { 419 b.header, err = b.backend.HeaderByNumberOrHash(ctx, *b.numberOrHash) 420 } 421 } 422 return b.header, err 423 } 424 425 // resolveReceipts returns the list of receipts for this block, fetching them 426 // if necessary. 427 func (b *Block) resolveReceipts(ctx context.Context) ([]*types.Receipt, error) { 428 if b.receipts == nil { 429 hash := b.hash 430 if hash == (common.Hash{}) { 431 header, err := b.resolveHeader(ctx) 432 if err != nil { 433 return nil, err 434 } 435 hash = header.Hash() 436 } 437 receipts, err := b.backend.GetReceipts(ctx, hash) 438 if err != nil { 439 return nil, err 440 } 441 b.receipts = []*types.Receipt(receipts) 442 } 443 return b.receipts, nil 444 } 445 446 func (b *Block) Number(ctx context.Context) (hexutil.Uint64, error) { 447 header, err := b.resolveHeader(ctx) 448 if err != nil { 449 return 0, err 450 } 451 452 return hexutil.Uint64(header.Number.Uint64()), nil 453 } 454 455 func (b *Block) Hash(ctx context.Context) (common.Hash, error) { 456 if b.hash == (common.Hash{}) { 457 header, err := b.resolveHeader(ctx) 458 if err != nil { 459 return common.Hash{}, err 460 } 461 b.hash = header.Hash() 462 } 463 return b.hash, nil 464 } 465 466 func (b *Block) GasLimit(ctx context.Context) (hexutil.Uint64, error) { 467 header, err := b.resolveHeader(ctx) 468 if err != nil { 469 return 0, err 470 } 471 return hexutil.Uint64(header.GasLimit), nil 472 } 473 474 func (b *Block) GasUsed(ctx context.Context) (hexutil.Uint64, error) { 475 header, err := b.resolveHeader(ctx) 476 if err != nil { 477 return 0, err 478 } 479 return hexutil.Uint64(header.GasUsed), nil 480 } 481 482 func (b *Block) Parent(ctx context.Context) (*Block, error) { 483 // If the block header hasn't been fetched, and we'll need it, fetch it. 484 if b.numberOrHash == nil && b.header == nil { 485 if _, err := b.resolveHeader(ctx); err != nil { 486 return nil, err 487 } 488 } 489 if b.header != nil && b.header.Number.Uint64() > 0 { 490 num := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(b.header.Number.Uint64() - 1)) 491 return &Block{ 492 backend: b.backend, 493 numberOrHash: &num, 494 hash: b.header.ParentHash, 495 }, nil 496 } 497 return nil, nil 498 } 499 500 func (b *Block) Difficulty(ctx context.Context) (hexutil.Big, error) { 501 header, err := b.resolveHeader(ctx) 502 if err != nil { 503 return hexutil.Big{}, err 504 } 505 return hexutil.Big(*header.Difficulty), nil 506 } 507 508 func (b *Block) Timestamp(ctx context.Context) (hexutil.Uint64, error) { 509 header, err := b.resolveHeader(ctx) 510 if err != nil { 511 return 0, err 512 } 513 return hexutil.Uint64(header.Time), nil 514 } 515 516 func (b *Block) Nonce(ctx context.Context) (hexutil.Bytes, error) { 517 header, err := b.resolveHeader(ctx) 518 if err != nil { 519 return hexutil.Bytes{}, err 520 } 521 return hexutil.Bytes(header.Nonce[:]), nil 522 } 523 524 func (b *Block) MixHash(ctx context.Context) (common.Hash, error) { 525 header, err := b.resolveHeader(ctx) 526 if err != nil { 527 return common.Hash{}, err 528 } 529 return header.MixDigest, nil 530 } 531 532 func (b *Block) TransactionsRoot(ctx context.Context) (common.Hash, error) { 533 header, err := b.resolveHeader(ctx) 534 if err != nil { 535 return common.Hash{}, err 536 } 537 return header.TxHash, nil 538 } 539 540 func (b *Block) StateRoot(ctx context.Context) (common.Hash, error) { 541 header, err := b.resolveHeader(ctx) 542 if err != nil { 543 return common.Hash{}, err 544 } 545 return header.Root, nil 546 } 547 548 func (b *Block) ReceiptsRoot(ctx context.Context) (common.Hash, error) { 549 header, err := b.resolveHeader(ctx) 550 if err != nil { 551 return common.Hash{}, err 552 } 553 return header.ReceiptHash, nil 554 } 555 556 func (b *Block) OmmerHash(ctx context.Context) (common.Hash, error) { 557 header, err := b.resolveHeader(ctx) 558 if err != nil { 559 return common.Hash{}, err 560 } 561 return header.UncleHash, nil 562 } 563 564 func (b *Block) OmmerCount(ctx context.Context) (*int32, error) { 565 block, err := b.resolve(ctx) 566 if err != nil || block == nil { 567 return nil, err 568 } 569 count := int32(len(block.Uncles())) 570 return &count, err 571 } 572 573 func (b *Block) Ommers(ctx context.Context) (*[]*Block, error) { 574 block, err := b.resolve(ctx) 575 if err != nil || block == nil { 576 return nil, err 577 } 578 ret := make([]*Block, 0, len(block.Uncles())) 579 for _, uncle := range block.Uncles() { 580 blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false) 581 ret = append(ret, &Block{ 582 backend: b.backend, 583 numberOrHash: &blockNumberOrHash, 584 header: uncle, 585 }) 586 } 587 return &ret, nil 588 } 589 590 func (b *Block) ExtraData(ctx context.Context) (hexutil.Bytes, error) { 591 header, err := b.resolveHeader(ctx) 592 if err != nil { 593 return hexutil.Bytes{}, err 594 } 595 return hexutil.Bytes(header.Extra), nil 596 } 597 598 func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) { 599 header, err := b.resolveHeader(ctx) 600 if err != nil { 601 return hexutil.Bytes{}, err 602 } 603 return hexutil.Bytes(header.Bloom.Bytes()), nil 604 } 605 606 func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) { 607 h := b.hash 608 if h == (common.Hash{}) { 609 header, err := b.resolveHeader(ctx) 610 if err != nil { 611 return hexutil.Big{}, err 612 } 613 h = header.Hash() 614 } 615 return hexutil.Big(*b.backend.GetTd(h)), nil 616 } 617 618 // BlockNumberArgs encapsulates arguments to accessors that specify a block number. 619 type BlockNumberArgs struct { 620 // TODO: Ideally we could use input unions to allow the query to specify the 621 // block parameter by hash, block number, or tag but input unions aren't part of the 622 // standard GraphQL schema SDL yet, see: https://github.com/graphql/graphql-spec/issues/488 623 Block *hexutil.Uint64 624 } 625 626 // NumberOr returns the provided block number argument, or the "current" block number or hash if none 627 // was provided. 628 func (a BlockNumberArgs) NumberOr(current rpc.BlockNumberOrHash) rpc.BlockNumberOrHash { 629 if a.Block != nil { 630 blockNr := rpc.BlockNumber(*a.Block) 631 return rpc.BlockNumberOrHashWithNumber(blockNr) 632 } 633 return current 634 } 635 636 // NumberOrLatest returns the provided block number argument, or the "latest" block number if none 637 // was provided. 638 func (a BlockNumberArgs) NumberOrLatest() rpc.BlockNumberOrHash { 639 return a.NumberOr(rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)) 640 } 641 642 func (b *Block) Miner(ctx context.Context, args BlockNumberArgs) (*Account, error) { 643 header, err := b.resolveHeader(ctx) 644 if err != nil { 645 return nil, err 646 } 647 return &Account{ 648 backend: b.backend, 649 address: header.Coinbase, 650 blockNrOrHash: args.NumberOrLatest(), 651 }, nil 652 } 653 654 func (b *Block) TransactionCount(ctx context.Context) (*int32, error) { 655 block, err := b.resolve(ctx) 656 if err != nil || block == nil { 657 return nil, err 658 } 659 count := int32(len(block.Transactions())) 660 return &count, err 661 } 662 663 func (b *Block) Transactions(ctx context.Context) (*[]*Transaction, error) { 664 block, err := b.resolve(ctx) 665 if err != nil || block == nil { 666 return nil, err 667 } 668 ret := make([]*Transaction, 0, len(block.Transactions())) 669 for i, tx := range block.Transactions() { 670 ret = append(ret, &Transaction{ 671 backend: b.backend, 672 hash: tx.Hash(), 673 tx: tx, 674 block: b, 675 index: uint64(i), 676 }) 677 } 678 return &ret, nil 679 } 680 681 func (b *Block) TransactionAt(ctx context.Context, args struct{ Index int32 }) (*Transaction, error) { 682 block, err := b.resolve(ctx) 683 if err != nil || block == nil { 684 return nil, err 685 } 686 txs := block.Transactions() 687 if args.Index < 0 || int(args.Index) >= len(txs) { 688 return nil, nil 689 } 690 tx := txs[args.Index] 691 return &Transaction{ 692 backend: b.backend, 693 hash: tx.Hash(), 694 tx: tx, 695 block: b, 696 index: uint64(args.Index), 697 }, nil 698 } 699 700 func (b *Block) OmmerAt(ctx context.Context, args struct{ Index int32 }) (*Block, error) { 701 block, err := b.resolve(ctx) 702 if err != nil || block == nil { 703 return nil, err 704 } 705 uncles := block.Uncles() 706 if args.Index < 0 || int(args.Index) >= len(uncles) { 707 return nil, nil 708 } 709 uncle := uncles[args.Index] 710 blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false) 711 return &Block{ 712 backend: b.backend, 713 numberOrHash: &blockNumberOrHash, 714 header: uncle, 715 }, nil 716 } 717 718 // BlockFilterCriteria encapsulates criteria passed to a `logs` accessor inside 719 // a block. 720 type BlockFilterCriteria struct { 721 Addresses *[]common.Address // restricts matches to events created by specific contracts 722 723 // The Topic list restricts matches to particular event topics. Each event has a list 724 // of topics. Topics matches a prefix of that list. An empty element slice matches any 725 // topic. Non-empty elements represent an alternative that matches any of the 726 // contained topics. 727 // 728 // Examples: 729 // {} or nil matches any topic list 730 // {{A}} matches topic A in first position 731 // {{}, {B}} matches any topic in first position, B in second position 732 // {{A}, {B}} matches topic A in first position, B in second position 733 // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position 734 Topics *[][]common.Hash 735 } 736 737 // runFilter accepts a filter and executes it, returning all its results as 738 // `Log` objects. 739 func runFilter(ctx context.Context, be ethapi.Backend, filter *filters.Filter) ([]*Log, error) { 740 logs, err := filter.Logs(ctx) 741 if err != nil || logs == nil { 742 return nil, err 743 } 744 ret := make([]*Log, 0, len(logs)) 745 for _, log := range logs { 746 ret = append(ret, &Log{ 747 backend: be, 748 transaction: &Transaction{backend: be, hash: log.TxHash}, 749 log: log, 750 }) 751 } 752 return ret, nil 753 } 754 755 func (b *Block) Logs(ctx context.Context, args struct{ Filter BlockFilterCriteria }) ([]*Log, error) { 756 var addresses []common.Address 757 if args.Filter.Addresses != nil { 758 addresses = *args.Filter.Addresses 759 } 760 var topics [][]common.Hash 761 if args.Filter.Topics != nil { 762 topics = *args.Filter.Topics 763 } 764 hash := b.hash 765 if hash == (common.Hash{}) { 766 header, err := b.resolveHeader(ctx) 767 if err != nil { 768 return nil, err 769 } 770 hash = header.Hash() 771 } 772 // Construct the range filter 773 filter := filters.NewBlockFilter(b.backend, hash, addresses, topics) 774 775 // Run the filter and return all the logs 776 return runFilter(ctx, b.backend, filter) 777 } 778 779 func (b *Block) Account(ctx context.Context, args struct { 780 Address common.Address 781 }) (*Account, error) { 782 if b.numberOrHash == nil { 783 _, err := b.resolveHeader(ctx) 784 if err != nil { 785 return nil, err 786 } 787 } 788 return &Account{ 789 backend: b.backend, 790 address: args.Address, 791 blockNrOrHash: *b.numberOrHash, 792 }, nil 793 } 794 795 // CallData encapsulates arguments to `call` or `estimateGas`. 796 // All arguments are optional. 797 type CallData struct { 798 From *common.Address // The Ethereum address the call is from. 799 To *common.Address // The Ethereum address the call is to. 800 Gas *hexutil.Uint64 // The amount of gas provided for the call. 801 GasPrice *hexutil.Big // The price of each unit of gas, in wei. 802 Value *hexutil.Big // The value sent along with the call. 803 Data *hexutil.Bytes // Any data sent with the call. 804 } 805 806 // CallResult encapsulates the result of an invocation of the `call` accessor. 807 type CallResult struct { 808 data hexutil.Bytes // The return data from the call 809 gasUsed hexutil.Uint64 // The amount of gas used 810 status hexutil.Uint64 // The return status of the call - 0 for failure or 1 for success. 811 } 812 813 func (c *CallResult) Data() hexutil.Bytes { 814 return c.data 815 } 816 817 func (c *CallResult) GasUsed() hexutil.Uint64 { 818 return c.gasUsed 819 } 820 821 func (c *CallResult) Status() hexutil.Uint64 { 822 return c.status 823 } 824 825 func (b *Block) Call(ctx context.Context, args struct { 826 Data ethapi.CallArgs 827 }) (*CallResult, error) { 828 if b.numberOrHash == nil { 829 _, err := b.resolve(ctx) 830 if err != nil { 831 return nil, err 832 } 833 } 834 835 // Quorum - replaced the default 5s time out with the value passed in vm.calltimeout 836 result, gas, failed, err := ethapi.DoCall(ctx, b.backend, args.Data, *b.numberOrHash, nil, vm.Config{}, b.backend.CallTimeOut(), b.backend.RPCGasCap()) 837 status := hexutil.Uint64(1) 838 if failed { 839 status = 0 840 } 841 return &CallResult{ 842 data: hexutil.Bytes(result), 843 gasUsed: hexutil.Uint64(gas), 844 status: status, 845 }, err 846 } 847 848 func (b *Block) EstimateGas(ctx context.Context, args struct { 849 Data ethapi.CallArgs 850 }) (hexutil.Uint64, error) { 851 if b.numberOrHash == nil { 852 _, err := b.resolveHeader(ctx) 853 if err != nil { 854 return hexutil.Uint64(0), err 855 } 856 } 857 gas, err := ethapi.DoEstimateGas(ctx, b.backend, args.Data, *b.numberOrHash, b.backend.RPCGasCap()) 858 return gas, err 859 } 860 861 type Pending struct { 862 backend ethapi.Backend 863 } 864 865 func (p *Pending) TransactionCount(ctx context.Context) (int32, error) { 866 txs, err := p.backend.GetPoolTransactions() 867 return int32(len(txs)), err 868 } 869 870 func (p *Pending) Transactions(ctx context.Context) (*[]*Transaction, error) { 871 txs, err := p.backend.GetPoolTransactions() 872 if err != nil { 873 return nil, err 874 } 875 ret := make([]*Transaction, 0, len(txs)) 876 for i, tx := range txs { 877 ret = append(ret, &Transaction{ 878 backend: p.backend, 879 hash: tx.Hash(), 880 tx: tx, 881 index: uint64(i), 882 }) 883 } 884 return &ret, nil 885 } 886 887 func (p *Pending) Account(ctx context.Context, args struct { 888 Address common.Address 889 }) *Account { 890 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 891 return &Account{ 892 backend: p.backend, 893 address: args.Address, 894 blockNrOrHash: pendingBlockNr, 895 } 896 } 897 898 func (p *Pending) Call(ctx context.Context, args struct { 899 Data ethapi.CallArgs 900 }) (*CallResult, error) { 901 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 902 903 // Quorum - replaced the default 5s time out with the value passed in vm.calltimeout 904 result, gas, failed, err := ethapi.DoCall(ctx, p.backend, args.Data, pendingBlockNr, nil, vm.Config{}, p.backend.CallTimeOut(), p.backend.RPCGasCap()) 905 status := hexutil.Uint64(1) 906 if failed { 907 status = 0 908 } 909 return &CallResult{ 910 data: hexutil.Bytes(result), 911 gasUsed: hexutil.Uint64(gas), 912 status: status, 913 }, err 914 } 915 916 func (p *Pending) EstimateGas(ctx context.Context, args struct { 917 Data ethapi.CallArgs 918 }) (hexutil.Uint64, error) { 919 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 920 return ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap()) 921 } 922 923 // Resolver is the top-level object in the GraphQL hierarchy. 924 type Resolver struct { 925 backend ethapi.Backend 926 } 927 928 func (r *Resolver) Block(ctx context.Context, args struct { 929 Number *hexutil.Uint64 930 Hash *common.Hash 931 }) (*Block, error) { 932 var block *Block 933 if args.Number != nil { 934 number := rpc.BlockNumber(uint64(*args.Number)) 935 numberOrHash := rpc.BlockNumberOrHashWithNumber(number) 936 block = &Block{ 937 backend: r.backend, 938 numberOrHash: &numberOrHash, 939 } 940 } else if args.Hash != nil { 941 numberOrHash := rpc.BlockNumberOrHashWithHash(*args.Hash, false) 942 block = &Block{ 943 backend: r.backend, 944 numberOrHash: &numberOrHash, 945 } 946 } else { 947 numberOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) 948 block = &Block{ 949 backend: r.backend, 950 numberOrHash: &numberOrHash, 951 } 952 } 953 // Resolve the header, return nil if it doesn't exist. 954 // Note we don't resolve block directly here since it will require an 955 // additional network request for light client. 956 h, err := block.resolveHeader(ctx) 957 if err != nil { 958 return nil, err 959 } else if h == nil { 960 return nil, nil 961 } 962 return block, nil 963 } 964 965 func (r *Resolver) Blocks(ctx context.Context, args struct { 966 From hexutil.Uint64 967 To *hexutil.Uint64 968 }) ([]*Block, error) { 969 from := rpc.BlockNumber(args.From) 970 971 var to rpc.BlockNumber 972 if args.To != nil { 973 to = rpc.BlockNumber(*args.To) 974 } else { 975 to = rpc.BlockNumber(r.backend.CurrentBlock().Number().Int64()) 976 } 977 if to < from { 978 return []*Block{}, nil 979 } 980 ret := make([]*Block, 0, to-from+1) 981 for i := from; i <= to; i++ { 982 numberOrHash := rpc.BlockNumberOrHashWithNumber(i) 983 ret = append(ret, &Block{ 984 backend: r.backend, 985 numberOrHash: &numberOrHash, 986 }) 987 } 988 return ret, nil 989 } 990 991 func (r *Resolver) Pending(ctx context.Context) *Pending { 992 return &Pending{r.backend} 993 } 994 995 func (r *Resolver) Transaction(ctx context.Context, args struct{ Hash common.Hash }) (*Transaction, error) { 996 tx := &Transaction{ 997 backend: r.backend, 998 hash: args.Hash, 999 } 1000 // Resolve the transaction; if it doesn't exist, return nil. 1001 t, err := tx.resolve(ctx) 1002 if err != nil { 1003 return nil, err 1004 } else if t == nil { 1005 return nil, nil 1006 } 1007 return tx, nil 1008 } 1009 1010 func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data hexutil.Bytes }) (common.Hash, error) { 1011 tx := new(types.Transaction) 1012 if err := rlp.DecodeBytes(args.Data, tx); err != nil { 1013 return common.Hash{}, err 1014 } 1015 hash, err := ethapi.SubmitTransaction(ctx, r.backend, tx, "", nil, true) 1016 return hash, err 1017 } 1018 1019 // FilterCriteria encapsulates the arguments to `logs` on the root resolver object. 1020 type FilterCriteria struct { 1021 FromBlock *hexutil.Uint64 // beginning of the queried range, nil means genesis block 1022 ToBlock *hexutil.Uint64 // end of the range, nil means latest block 1023 Addresses *[]common.Address // restricts matches to events created by specific contracts 1024 1025 // The Topic list restricts matches to particular event topics. Each event has a list 1026 // of topics. Topics matches a prefix of that list. An empty element slice matches any 1027 // topic. Non-empty elements represent an alternative that matches any of the 1028 // contained topics. 1029 // 1030 // Examples: 1031 // {} or nil matches any topic list 1032 // {{A}} matches topic A in first position 1033 // {{}, {B}} matches any topic in first position, B in second position 1034 // {{A}, {B}} matches topic A in first position, B in second position 1035 // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position 1036 Topics *[][]common.Hash 1037 } 1038 1039 func (r *Resolver) Logs(ctx context.Context, args struct{ Filter FilterCriteria }) ([]*Log, error) { 1040 // Convert the RPC block numbers into internal representations 1041 begin := rpc.LatestBlockNumber.Int64() 1042 if args.Filter.FromBlock != nil { 1043 begin = int64(*args.Filter.FromBlock) 1044 } 1045 end := rpc.LatestBlockNumber.Int64() 1046 if args.Filter.ToBlock != nil { 1047 end = int64(*args.Filter.ToBlock) 1048 } 1049 var addresses []common.Address 1050 if args.Filter.Addresses != nil { 1051 addresses = *args.Filter.Addresses 1052 } 1053 var topics [][]common.Hash 1054 if args.Filter.Topics != nil { 1055 topics = *args.Filter.Topics 1056 } 1057 // Construct the range filter 1058 filter := filters.NewRangeFilter(filters.Backend(r.backend), begin, end, addresses, topics) 1059 return runFilter(ctx, r.backend, filter) 1060 } 1061 1062 func (r *Resolver) GasPrice(ctx context.Context) (hexutil.Big, error) { 1063 price, err := r.backend.SuggestPrice(ctx) 1064 return hexutil.Big(*price), err 1065 } 1066 1067 func (r *Resolver) ProtocolVersion(ctx context.Context) (int32, error) { 1068 return int32(r.backend.ProtocolVersion()), nil 1069 } 1070 1071 // SyncState represents the synchronisation status returned from the `syncing` accessor. 1072 type SyncState struct { 1073 progress ethereum.SyncProgress 1074 } 1075 1076 func (s *SyncState) StartingBlock() hexutil.Uint64 { 1077 return hexutil.Uint64(s.progress.StartingBlock) 1078 } 1079 1080 func (s *SyncState) CurrentBlock() hexutil.Uint64 { 1081 return hexutil.Uint64(s.progress.CurrentBlock) 1082 } 1083 1084 func (s *SyncState) HighestBlock() hexutil.Uint64 { 1085 return hexutil.Uint64(s.progress.HighestBlock) 1086 } 1087 1088 func (s *SyncState) PulledStates() *hexutil.Uint64 { 1089 ret := hexutil.Uint64(s.progress.PulledStates) 1090 return &ret 1091 } 1092 1093 func (s *SyncState) KnownStates() *hexutil.Uint64 { 1094 ret := hexutil.Uint64(s.progress.KnownStates) 1095 return &ret 1096 } 1097 1098 // Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not 1099 // yet received the latest block headers from its pears. In case it is synchronizing: 1100 // - startingBlock: block number this node started to synchronise from 1101 // - currentBlock: block number this node is currently importing 1102 // - highestBlock: block number of the highest block header this node has received from peers 1103 // - pulledStates: number of state entries processed until now 1104 // - knownStates: number of known state entries that still need to be pulled 1105 func (r *Resolver) Syncing() (*SyncState, error) { 1106 progress := r.backend.Downloader().Progress() 1107 1108 // Return not syncing if the synchronisation already completed 1109 if progress.CurrentBlock >= progress.HighestBlock { 1110 return nil, nil 1111 } 1112 // Otherwise gather the block sync stats 1113 return &SyncState{progress}, nil 1114 }