github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/internal/tracetest/makeTest.js (about)

     1  // Copyright 2024 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  // makeTest generates a test for the configured tracer by running
    18  // a prestate reassembled and a call trace run, assembling all the
    19  // gathered information into a test case.
    20  var makeTest = function(tx, traceConfig) {
    21      // Generate the genesis block from the block, transaction and prestate data
    22      var block   = eth.getBlock(eth.getTransaction(tx).blockHash);
    23      var genesis = eth.getBlock(block.parentHash);
    24  
    25      delete genesis.gasUsed;
    26      delete genesis.logsBloom;
    27      delete genesis.parentHash;
    28      delete genesis.receiptsRoot;
    29      delete genesis.sha3Uncles;
    30      delete genesis.size;
    31      delete genesis.transactions;
    32      delete genesis.transactionsRoot;
    33      delete genesis.uncles;
    34      delete genesis.withdrawals;
    35      delete genesis.withdrawalsRoot;
    36      delete genesis.baseFeePerGas;
    37  
    38      genesis.gasLimit  = genesis.gasLimit.toString();
    39      genesis.number    = genesis.number.toString();
    40      genesis.timestamp = genesis.timestamp.toString();
    41  
    42      genesis.alloc = debug.traceTransaction(tx, {tracer: "prestateTracer"});
    43      for (var key in genesis.alloc) {
    44          var nonce = genesis.alloc[key].nonce;
    45          if (nonce) {
    46              genesis.alloc[key].nonce = nonce.toString();
    47          }
    48      }
    49      genesis.config = admin.nodeInfo.protocols.eth.config;
    50  
    51      // Generate the call trace and produce the test input
    52      var result = debug.traceTransaction(tx, traceConfig);
    53      delete result.time;
    54  
    55      var context = {
    56          number:     block.number.toString(),
    57          difficulty: block.difficulty,
    58          timestamp:  block.timestamp.toString(),
    59          gasLimit:   block.gasLimit.toString(),
    60          miner:      block.miner,
    61      };
    62      if (block.baseFeePerGas) {
    63          context.baseFeePerGas = block.baseFeePerGas.toString();
    64      }
    65  
    66      var data = {
    67          genesis: genesis,
    68          context: context,
    69          input:   eth.getRawTransaction(tx),
    70          result:  result,
    71      };
    72      if (traceConfig && traceConfig.tracerConfig) {
    73          data.tracerConfig = traceConfig.tracerConfig;
    74      }
    75  
    76      console.log(JSON.stringify(data, null, 2));
    77  }