github.com/calmw/ethereum@v0.1.1/cmd/evm/t8n_test.go (about)

     1  // Copyright 2021 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  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"reflect"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/calmw/ethereum/cmd/evm/internal/t8ntool"
    28  	"github.com/calmw/ethereum/internal/cmdtest"
    29  	"github.com/docker/docker/pkg/reexec"
    30  )
    31  
    32  func TestMain(m *testing.M) {
    33  	// Run the app if we've been exec'd as "ethkey-test" in runEthkey.
    34  	reexec.Register("evm-test", func() {
    35  		if err := app.Run(os.Args); err != nil {
    36  			fmt.Fprintln(os.Stderr, err)
    37  			os.Exit(1)
    38  		}
    39  		os.Exit(0)
    40  	})
    41  	// check if we have been reexec'd
    42  	if reexec.Init() {
    43  		return
    44  	}
    45  	os.Exit(m.Run())
    46  }
    47  
    48  type testT8n struct {
    49  	*cmdtest.TestCmd
    50  }
    51  
    52  type t8nInput struct {
    53  	inAlloc  string
    54  	inTxs    string
    55  	inEnv    string
    56  	stFork   string
    57  	stReward string
    58  }
    59  
    60  func (args *t8nInput) get(base string) []string {
    61  	var out []string
    62  	if opt := args.inAlloc; opt != "" {
    63  		out = append(out, "--input.alloc")
    64  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    65  	}
    66  	if opt := args.inTxs; opt != "" {
    67  		out = append(out, "--input.txs")
    68  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    69  	}
    70  	if opt := args.inEnv; opt != "" {
    71  		out = append(out, "--input.env")
    72  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    73  	}
    74  	if opt := args.stFork; opt != "" {
    75  		out = append(out, "--state.fork", opt)
    76  	}
    77  	if opt := args.stReward; opt != "" {
    78  		out = append(out, "--state.reward", opt)
    79  	}
    80  	return out
    81  }
    82  
    83  type t8nOutput struct {
    84  	alloc  bool
    85  	result bool
    86  	body   bool
    87  }
    88  
    89  func (args *t8nOutput) get() (out []string) {
    90  	if args.body {
    91  		out = append(out, "--output.body", "stdout")
    92  	} else {
    93  		out = append(out, "--output.body", "") // empty means ignore
    94  	}
    95  	if args.result {
    96  		out = append(out, "--output.result", "stdout")
    97  	} else {
    98  		out = append(out, "--output.result", "")
    99  	}
   100  	if args.alloc {
   101  		out = append(out, "--output.alloc", "stdout")
   102  	} else {
   103  		out = append(out, "--output.alloc", "")
   104  	}
   105  	return out
   106  }
   107  
   108  func TestT8n(t *testing.T) {
   109  	tt := new(testT8n)
   110  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
   111  	for i, tc := range []struct {
   112  		base        string
   113  		input       t8nInput
   114  		output      t8nOutput
   115  		expExitCode int
   116  		expOut      string
   117  	}{
   118  		{ // Test exit (3) on bad config
   119  			base: "./testdata/1",
   120  			input: t8nInput{
   121  				"alloc.json", "txs.json", "env.json", "Frontier+1346", "",
   122  			},
   123  			output:      t8nOutput{alloc: true, result: true},
   124  			expExitCode: 3,
   125  		},
   126  		{
   127  			base: "./testdata/1",
   128  			input: t8nInput{
   129  				"alloc.json", "txs.json", "env.json", "Byzantium", "",
   130  			},
   131  			output: t8nOutput{alloc: true, result: true},
   132  			expOut: "exp.json",
   133  		},
   134  		{ // blockhash test
   135  			base: "./testdata/3",
   136  			input: t8nInput{
   137  				"alloc.json", "txs.json", "env.json", "Berlin", "",
   138  			},
   139  			output: t8nOutput{alloc: true, result: true},
   140  			expOut: "exp.json",
   141  		},
   142  		{ // missing blockhash test
   143  			base: "./testdata/4",
   144  			input: t8nInput{
   145  				"alloc.json", "txs.json", "env.json", "Berlin", "",
   146  			},
   147  			output:      t8nOutput{alloc: true, result: true},
   148  			expExitCode: 4,
   149  		},
   150  		{ // Uncle test
   151  			base: "./testdata/5",
   152  			input: t8nInput{
   153  				"alloc.json", "txs.json", "env.json", "Byzantium", "0x80",
   154  			},
   155  			output: t8nOutput{alloc: true, result: true},
   156  			expOut: "exp.json",
   157  		},
   158  		{ // Sign json transactions
   159  			base: "./testdata/13",
   160  			input: t8nInput{
   161  				"alloc.json", "txs.json", "env.json", "London", "",
   162  			},
   163  			output: t8nOutput{body: true},
   164  			expOut: "exp.json",
   165  		},
   166  		{ // Already signed transactions
   167  			base: "./testdata/13",
   168  			input: t8nInput{
   169  				"alloc.json", "signed_txs.rlp", "env.json", "London", "",
   170  			},
   171  			output: t8nOutput{result: true},
   172  			expOut: "exp2.json",
   173  		},
   174  		{ // Difficulty calculation - no uncles
   175  			base: "./testdata/14",
   176  			input: t8nInput{
   177  				"alloc.json", "txs.json", "env.json", "London", "",
   178  			},
   179  			output: t8nOutput{result: true},
   180  			expOut: "exp.json",
   181  		},
   182  		{ // Difficulty calculation - with uncles
   183  			base: "./testdata/14",
   184  			input: t8nInput{
   185  				"alloc.json", "txs.json", "env.uncles.json", "London", "",
   186  			},
   187  			output: t8nOutput{result: true},
   188  			expOut: "exp2.json",
   189  		},
   190  		{ // Difficulty calculation - with ommers + Berlin
   191  			base: "./testdata/14",
   192  			input: t8nInput{
   193  				"alloc.json", "txs.json", "env.uncles.json", "Berlin", "",
   194  			},
   195  			output: t8nOutput{result: true},
   196  			expOut: "exp_berlin.json",
   197  		},
   198  		{ // Difficulty calculation on arrow glacier
   199  			base: "./testdata/19",
   200  			input: t8nInput{
   201  				"alloc.json", "txs.json", "env.json", "London", "",
   202  			},
   203  			output: t8nOutput{result: true},
   204  			expOut: "exp_london.json",
   205  		},
   206  		{ // Difficulty calculation on arrow glacier
   207  			base: "./testdata/19",
   208  			input: t8nInput{
   209  				"alloc.json", "txs.json", "env.json", "ArrowGlacier", "",
   210  			},
   211  			output: t8nOutput{result: true},
   212  			expOut: "exp_arrowglacier.json",
   213  		},
   214  		{ // Difficulty calculation on gray glacier
   215  			base: "./testdata/19",
   216  			input: t8nInput{
   217  				"alloc.json", "txs.json", "env.json", "GrayGlacier", "",
   218  			},
   219  			output: t8nOutput{result: true},
   220  			expOut: "exp_grayglacier.json",
   221  		},
   222  		{ // Sign unprotected (pre-EIP155) transaction
   223  			base: "./testdata/23",
   224  			input: t8nInput{
   225  				"alloc.json", "txs.json", "env.json", "Berlin", "",
   226  			},
   227  			output: t8nOutput{result: true},
   228  			expOut: "exp.json",
   229  		},
   230  		{ // Test post-merge transition
   231  			base: "./testdata/24",
   232  			input: t8nInput{
   233  				"alloc.json", "txs.json", "env.json", "Merge", "",
   234  			},
   235  			output: t8nOutput{alloc: true, result: true},
   236  			expOut: "exp.json",
   237  		},
   238  		{ // Test post-merge transition where input is missing random
   239  			base: "./testdata/24",
   240  			input: t8nInput{
   241  				"alloc.json", "txs.json", "env-missingrandom.json", "Merge", "",
   242  			},
   243  			output:      t8nOutput{alloc: false, result: false},
   244  			expExitCode: 3,
   245  		},
   246  		{ // Test base fee calculation
   247  			base: "./testdata/25",
   248  			input: t8nInput{
   249  				"alloc.json", "txs.json", "env.json", "Merge", "",
   250  			},
   251  			output: t8nOutput{alloc: true, result: true},
   252  			expOut: "exp.json",
   253  		},
   254  		{ // Test withdrawals transition
   255  			base: "./testdata/26",
   256  			input: t8nInput{
   257  				"alloc.json", "txs.json", "env.json", "Shanghai", "",
   258  			},
   259  			output: t8nOutput{alloc: true, result: true},
   260  			expOut: "exp.json",
   261  		},
   262  	} {
   263  		args := []string{"t8n"}
   264  		args = append(args, tc.output.get()...)
   265  		args = append(args, tc.input.get(tc.base)...)
   266  		var qArgs []string // quoted args for debugging purposes
   267  		for _, arg := range args {
   268  			if len(arg) == 0 {
   269  				qArgs = append(qArgs, `""`)
   270  			} else {
   271  				qArgs = append(qArgs, arg)
   272  			}
   273  		}
   274  		tt.Logf("args: %v\n", strings.Join(qArgs, " "))
   275  		tt.Run("evm-test", args...)
   276  		// Compare the expected output, if provided
   277  		if tc.expOut != "" {
   278  			file := fmt.Sprintf("%v/%v", tc.base, tc.expOut)
   279  			want, err := os.ReadFile(file)
   280  			if err != nil {
   281  				t.Fatalf("test %d: could not read expected output: %v", i, err)
   282  			}
   283  			have := tt.Output()
   284  			ok, err := cmpJson(have, want)
   285  			switch {
   286  			case err != nil:
   287  				t.Fatalf("test %d, file %v: json parsing failed: %v", i, file, err)
   288  			case !ok:
   289  				t.Fatalf("test %d, file %v: output wrong, have \n%v\nwant\n%v\n", i, file, string(have), string(want))
   290  			}
   291  		}
   292  		tt.WaitExit()
   293  		if have, want := tt.ExitStatus(), tc.expExitCode; have != want {
   294  			t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want)
   295  		}
   296  	}
   297  }
   298  
   299  type t9nInput struct {
   300  	inTxs  string
   301  	stFork string
   302  }
   303  
   304  func (args *t9nInput) get(base string) []string {
   305  	var out []string
   306  	if opt := args.inTxs; opt != "" {
   307  		out = append(out, "--input.txs")
   308  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   309  	}
   310  	if opt := args.stFork; opt != "" {
   311  		out = append(out, "--state.fork", opt)
   312  	}
   313  	return out
   314  }
   315  
   316  func TestT9n(t *testing.T) {
   317  	tt := new(testT8n)
   318  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
   319  	for i, tc := range []struct {
   320  		base        string
   321  		input       t9nInput
   322  		expExitCode int
   323  		expOut      string
   324  	}{
   325  		{ // London txs on homestead
   326  			base: "./testdata/15",
   327  			input: t9nInput{
   328  				inTxs:  "signed_txs.rlp",
   329  				stFork: "Homestead",
   330  			},
   331  			expOut: "exp.json",
   332  		},
   333  		{ // London txs on London
   334  			base: "./testdata/15",
   335  			input: t9nInput{
   336  				inTxs:  "signed_txs.rlp",
   337  				stFork: "London",
   338  			},
   339  			expOut: "exp2.json",
   340  		},
   341  		{ // An RLP list (a blockheader really)
   342  			base: "./testdata/15",
   343  			input: t9nInput{
   344  				inTxs:  "blockheader.rlp",
   345  				stFork: "London",
   346  			},
   347  			expOut: "exp3.json",
   348  		},
   349  		{ // Transactions with too low gas
   350  			base: "./testdata/16",
   351  			input: t9nInput{
   352  				inTxs:  "signed_txs.rlp",
   353  				stFork: "London",
   354  			},
   355  			expOut: "exp.json",
   356  		},
   357  		{ // Transactions with value exceeding 256 bits
   358  			base: "./testdata/17",
   359  			input: t9nInput{
   360  				inTxs:  "signed_txs.rlp",
   361  				stFork: "London",
   362  			},
   363  			expOut: "exp.json",
   364  		},
   365  		{ // Invalid RLP
   366  			base: "./testdata/18",
   367  			input: t9nInput{
   368  				inTxs:  "invalid.rlp",
   369  				stFork: "London",
   370  			},
   371  			expExitCode: t8ntool.ErrorIO,
   372  		},
   373  	} {
   374  		args := []string{"t9n"}
   375  		args = append(args, tc.input.get(tc.base)...)
   376  
   377  		tt.Run("evm-test", args...)
   378  		tt.Logf("args:\n go run . %v\n", strings.Join(args, " "))
   379  		// Compare the expected output, if provided
   380  		if tc.expOut != "" {
   381  			want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut))
   382  			if err != nil {
   383  				t.Fatalf("test %d: could not read expected output: %v", i, err)
   384  			}
   385  			have := tt.Output()
   386  			ok, err := cmpJson(have, want)
   387  			switch {
   388  			case err != nil:
   389  				t.Logf(string(have))
   390  				t.Fatalf("test %d, json parsing failed: %v", i, err)
   391  			case !ok:
   392  				t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
   393  			}
   394  		}
   395  		tt.WaitExit()
   396  		if have, want := tt.ExitStatus(), tc.expExitCode; have != want {
   397  			t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want)
   398  		}
   399  	}
   400  }
   401  
   402  type b11rInput struct {
   403  	inEnv         string
   404  	inOmmersRlp   string
   405  	inWithdrawals string
   406  	inTxsRlp      string
   407  	inClique      string
   408  	ethash        bool
   409  	ethashMode    string
   410  	ethashDir     string
   411  }
   412  
   413  func (args *b11rInput) get(base string) []string {
   414  	var out []string
   415  	if opt := args.inEnv; opt != "" {
   416  		out = append(out, "--input.header")
   417  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   418  	}
   419  	if opt := args.inOmmersRlp; opt != "" {
   420  		out = append(out, "--input.ommers")
   421  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   422  	}
   423  	if opt := args.inWithdrawals; opt != "" {
   424  		out = append(out, "--input.withdrawals")
   425  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   426  	}
   427  	if opt := args.inTxsRlp; opt != "" {
   428  		out = append(out, "--input.txs")
   429  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   430  	}
   431  	if opt := args.inClique; opt != "" {
   432  		out = append(out, "--seal.clique")
   433  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   434  	}
   435  	if args.ethash {
   436  		out = append(out, "--seal.ethash")
   437  	}
   438  	if opt := args.ethashMode; opt != "" {
   439  		out = append(out, "--seal.ethash.mode")
   440  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   441  	}
   442  	if opt := args.ethashDir; opt != "" {
   443  		out = append(out, "--seal.ethash.dir")
   444  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   445  	}
   446  	out = append(out, "--output.block")
   447  	out = append(out, "stdout")
   448  	return out
   449  }
   450  
   451  func TestB11r(t *testing.T) {
   452  	tt := new(testT8n)
   453  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
   454  	for i, tc := range []struct {
   455  		base        string
   456  		input       b11rInput
   457  		expExitCode int
   458  		expOut      string
   459  	}{
   460  		{ // unsealed block
   461  			base: "./testdata/20",
   462  			input: b11rInput{
   463  				inEnv:       "header.json",
   464  				inOmmersRlp: "ommers.json",
   465  				inTxsRlp:    "txs.rlp",
   466  			},
   467  			expOut: "exp.json",
   468  		},
   469  		{ // ethash test seal
   470  			base: "./testdata/21",
   471  			input: b11rInput{
   472  				inEnv:       "header.json",
   473  				inOmmersRlp: "ommers.json",
   474  				inTxsRlp:    "txs.rlp",
   475  			},
   476  			expOut: "exp.json",
   477  		},
   478  		{ // clique test seal
   479  			base: "./testdata/21",
   480  			input: b11rInput{
   481  				inEnv:       "header.json",
   482  				inOmmersRlp: "ommers.json",
   483  				inTxsRlp:    "txs.rlp",
   484  				inClique:    "clique.json",
   485  			},
   486  			expOut: "exp-clique.json",
   487  		},
   488  		{ // block with ommers
   489  			base: "./testdata/22",
   490  			input: b11rInput{
   491  				inEnv:       "header.json",
   492  				inOmmersRlp: "ommers.json",
   493  				inTxsRlp:    "txs.rlp",
   494  			},
   495  			expOut: "exp.json",
   496  		},
   497  		{ // block with withdrawals
   498  			base: "./testdata/27",
   499  			input: b11rInput{
   500  				inEnv:         "header.json",
   501  				inOmmersRlp:   "ommers.json",
   502  				inWithdrawals: "withdrawals.json",
   503  				inTxsRlp:      "txs.rlp",
   504  			},
   505  			expOut: "exp.json",
   506  		},
   507  	} {
   508  		args := []string{"b11r"}
   509  		args = append(args, tc.input.get(tc.base)...)
   510  
   511  		tt.Run("evm-test", args...)
   512  		tt.Logf("args:\n go run . %v\n", strings.Join(args, " "))
   513  		// Compare the expected output, if provided
   514  		if tc.expOut != "" {
   515  			want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut))
   516  			if err != nil {
   517  				t.Fatalf("test %d: could not read expected output: %v", i, err)
   518  			}
   519  			have := tt.Output()
   520  			ok, err := cmpJson(have, want)
   521  			switch {
   522  			case err != nil:
   523  				t.Logf(string(have))
   524  				t.Fatalf("test %d, json parsing failed: %v", i, err)
   525  			case !ok:
   526  				t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
   527  			}
   528  		}
   529  		tt.WaitExit()
   530  		if have, want := tt.ExitStatus(), tc.expExitCode; have != want {
   531  			t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want)
   532  		}
   533  	}
   534  }
   535  
   536  // cmpJson compares the JSON in two byte slices.
   537  func cmpJson(a, b []byte) (bool, error) {
   538  	var j, j2 interface{}
   539  	if err := json.Unmarshal(a, &j); err != nil {
   540  		return false, err
   541  	}
   542  	if err := json.Unmarshal(b, &j2); err != nil {
   543  		return false, err
   544  	}
   545  	return reflect.DeepEqual(j2, j), nil
   546  }