retesteth_copypaste.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/hexutil"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. )
  23. // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
  24. type RPCTransaction struct {
  25. BlockHash common.Hash `json:"blockHash"`
  26. BlockNumber *hexutil.Big `json:"blockNumber"`
  27. From common.Address `json:"from"`
  28. Gas hexutil.Uint64 `json:"gas"`
  29. GasPrice *hexutil.Big `json:"gasPrice"`
  30. Hash common.Hash `json:"hash"`
  31. Input hexutil.Bytes `json:"input"`
  32. Nonce hexutil.Uint64 `json:"nonce"`
  33. To *common.Address `json:"to"`
  34. TransactionIndex hexutil.Uint `json:"transactionIndex"`
  35. Value *hexutil.Big `json:"value"`
  36. V *hexutil.Big `json:"v"`
  37. R *hexutil.Big `json:"r"`
  38. S *hexutil.Big `json:"s"`
  39. }
  40. // newRPCTransaction returns a transaction that will serialize to the RPC
  41. // representation, with the given location metadata set (if available).
  42. func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction {
  43. var signer types.Signer = types.FrontierSigner{}
  44. if tx.Protected() {
  45. signer = types.NewEIP155Signer(tx.ChainId())
  46. }
  47. from, _ := types.Sender(signer, tx)
  48. v, r, s := tx.RawSignatureValues()
  49. result := &RPCTransaction{
  50. From: from,
  51. Gas: hexutil.Uint64(tx.Gas()),
  52. GasPrice: (*hexutil.Big)(tx.GasPrice()),
  53. Hash: tx.Hash(),
  54. Input: hexutil.Bytes(tx.Data()),
  55. Nonce: hexutil.Uint64(tx.Nonce()),
  56. To: tx.To(),
  57. Value: (*hexutil.Big)(tx.Value()),
  58. V: (*hexutil.Big)(v),
  59. R: (*hexutil.Big)(r),
  60. S: (*hexutil.Big)(s),
  61. }
  62. if blockHash != (common.Hash{}) {
  63. result.BlockHash = blockHash
  64. result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
  65. result.TransactionIndex = hexutil.Uint(index)
  66. }
  67. return result
  68. }
  69. // newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
  70. func newRPCTransactionFromBlockIndex(b *types.Block, index uint64) *RPCTransaction {
  71. txs := b.Transactions()
  72. if index >= uint64(len(txs)) {
  73. return nil
  74. }
  75. return newRPCTransaction(txs[index], b.Hash(), b.NumberU64(), index)
  76. }
  77. // newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
  78. func newRPCTransactionFromBlockHash(b *types.Block, hash common.Hash) *RPCTransaction {
  79. for idx, tx := range b.Transactions() {
  80. if tx.Hash() == hash {
  81. return newRPCTransactionFromBlockIndex(b, uint64(idx))
  82. }
  83. }
  84. return nil
  85. }
  86. // RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
  87. // returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
  88. // transaction hashes.
  89. func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
  90. head := b.Header() // copies the header once
  91. fields := map[string]interface{}{
  92. "number": (*hexutil.Big)(head.Number),
  93. "hash": b.Hash(),
  94. "parentHash": head.ParentHash,
  95. "nonce": head.Nonce,
  96. "mixHash": head.MixDigest,
  97. "sha3Uncles": head.UncleHash,
  98. "logsBloom": head.Bloom,
  99. "stateRoot": head.Root,
  100. "miner": head.Coinbase,
  101. "difficulty": (*hexutil.Big)(head.Difficulty),
  102. "extraData": hexutil.Bytes(head.Extra),
  103. "size": hexutil.Uint64(b.Size()),
  104. "gasLimit": hexutil.Uint64(head.GasLimit),
  105. "gasUsed": hexutil.Uint64(head.GasUsed),
  106. "timestamp": hexutil.Uint64(head.Time),
  107. "transactionsRoot": head.TxHash,
  108. "receiptsRoot": head.ReceiptHash,
  109. }
  110. if inclTx {
  111. formatTx := func(tx *types.Transaction) (interface{}, error) {
  112. return tx.Hash(), nil
  113. }
  114. if fullTx {
  115. formatTx = func(tx *types.Transaction) (interface{}, error) {
  116. return newRPCTransactionFromBlockHash(b, tx.Hash()), nil
  117. }
  118. }
  119. txs := b.Transactions()
  120. transactions := make([]interface{}, len(txs))
  121. var err error
  122. for i, tx := range txs {
  123. if transactions[i], err = formatTx(tx); err != nil {
  124. return nil, err
  125. }
  126. }
  127. fields["transactions"] = transactions
  128. }
  129. uncles := b.Uncles()
  130. uncleHashes := make([]common.Hash, len(uncles))
  131. for i, uncle := range uncles {
  132. uncleHashes[i] = uncle.Hash()
  133. }
  134. fields["uncles"] = uncleHashes
  135. return fields, nil
  136. }