github.com/theQRL/go-zond@v0.2.1/zond/tracers/internal/tracetest/prestate_test.go (about)

     1  // Copyright 2021 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 tracetest
    18  
    19  import (
    20  	"encoding/json"
    21  	"math/big"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/theQRL/go-zond/common"
    28  	"github.com/theQRL/go-zond/core"
    29  	"github.com/theQRL/go-zond/core/rawdb"
    30  	"github.com/theQRL/go-zond/core/types"
    31  	"github.com/theQRL/go-zond/core/vm"
    32  	"github.com/theQRL/go-zond/tests"
    33  	"github.com/theQRL/go-zond/zond/tracers"
    34  )
    35  
    36  // prestateTrace is the result of a prestateTrace run.
    37  type prestateTrace = map[common.Address]*account
    38  
    39  type account struct {
    40  	Balance string                      `json:"balance"`
    41  	Code    string                      `json:"code"`
    42  	Nonce   uint64                      `json:"nonce"`
    43  	Storage map[common.Hash]common.Hash `json:"storage"`
    44  }
    45  
    46  // testcase defines a single test to check the stateDiff tracer against.
    47  type testcase struct {
    48  	Genesis      *core.Genesis   `json:"genesis"`
    49  	Context      *callContext    `json:"context"`
    50  	Input        string          `json:"input"`
    51  	TracerConfig json.RawMessage `json:"tracerConfig"`
    52  	Result       interface{}     `json:"result"`
    53  }
    54  
    55  func TestPrestateTracer(t *testing.T) {
    56  	testPrestateDiffTracer("prestateTracer", "prestate_tracer", t)
    57  }
    58  
    59  // TODO(now.youtrack.cloud/issue/TGZ-13)
    60  func TestPrestateWithDiffModeTracer(t *testing.T) {
    61  	testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
    62  }
    63  
    64  func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
    65  	files, err := os.ReadDir(filepath.Join("testdata", dirPath))
    66  	if err != nil {
    67  		t.Fatalf("failed to retrieve tracer test suite: %v", err)
    68  	}
    69  	for _, file := range files {
    70  		if !strings.HasSuffix(file.Name(), ".json") {
    71  			continue
    72  		}
    73  		file := file // capture range variable
    74  		t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) {
    75  			t.Parallel()
    76  
    77  			var (
    78  				test = new(testcase)
    79  				tx   = new(types.Transaction)
    80  			)
    81  			// Call tracer test found, read if from disk
    82  			if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil {
    83  				t.Fatalf("failed to read testcase: %v", err)
    84  			} else if err := json.Unmarshal(blob, test); err != nil {
    85  				t.Fatalf("failed to parse testcase: %v", err)
    86  			}
    87  			if err := tx.UnmarshalBinary(common.FromHex(test.Input)); err != nil {
    88  				t.Fatalf("failed to parse testcase input: %v", err)
    89  			}
    90  			// Configure a blockchain with the given prestate
    91  			var (
    92  				signer    = types.MakeSigner(test.Genesis.Config)
    93  				origin, _ = signer.Sender(tx)
    94  				txContext = vm.TxContext{
    95  					Origin:   origin,
    96  					GasPrice: tx.GasPrice(),
    97  				}
    98  				context = vm.BlockContext{
    99  					CanTransfer: core.CanTransfer,
   100  					Transfer:    core.Transfer,
   101  					Coinbase:    test.Context.Miner,
   102  					BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)),
   103  					Time:        uint64(test.Context.Time),
   104  					GasLimit:    uint64(test.Context.GasLimit),
   105  					BaseFee:     (*big.Int)(test.Context.BaseFee),
   106  				}
   107  				triedb, _, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false, rawdb.HashScheme)
   108  			)
   109  			defer triedb.Close()
   110  
   111  			tracer, err := tracers.DefaultDirectory.New(tracerName, new(tracers.Context), test.TracerConfig)
   112  			if err != nil {
   113  				t.Fatalf("failed to create call tracer: %v", err)
   114  			}
   115  			zvm := vm.NewZVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Tracer: tracer})
   116  			msg, err := core.TransactionToMessage(tx, signer, nil)
   117  			if err != nil {
   118  				t.Fatalf("failed to prepare transaction for tracing: %v", err)
   119  			}
   120  			st := core.NewStateTransition(zvm, msg, new(core.GasPool).AddGas(tx.Gas()))
   121  			if _, err = st.TransitionDb(); err != nil {
   122  				t.Fatalf("failed to execute transaction: %v", err)
   123  			}
   124  			// Retrieve the trace result and compare against the expected
   125  			res, err := tracer.GetResult()
   126  			if err != nil {
   127  				t.Fatalf("failed to retrieve trace result: %v", err)
   128  			}
   129  			want, err := json.Marshal(test.Result)
   130  			if err != nil {
   131  				t.Fatalf("failed to marshal test: %v", err)
   132  			}
   133  			if string(want) != string(res) {
   134  				t.Fatalf("trace mismatch\n have: %v\n want: %v\n", string(res), string(want))
   135  			}
   136  		})
   137  	}
   138  }