github.com/citrino-network/citrino-cli@v0.0.0-20211030184609-316d7bfc332c/cmd/evm/t8n_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/pkg/reexec"
    12  	"github.com/citrino-network/citrino-cli/cmd/evm/internal/t8ntool"
    13  	"github.com/citrino-network/citrino-cli/internal/cmdtest"
    14  )
    15  
    16  func TestMain(m *testing.M) {
    17  	// Run the app if we've been exec'd as "ethkey-test" in runEthkey.
    18  	reexec.Register("evm-test", func() {
    19  		if err := app.Run(os.Args); err != nil {
    20  			fmt.Fprintln(os.Stderr, err)
    21  			os.Exit(1)
    22  		}
    23  		os.Exit(0)
    24  	})
    25  	// check if we have been reexec'd
    26  	if reexec.Init() {
    27  		return
    28  	}
    29  	os.Exit(m.Run())
    30  }
    31  
    32  type testT8n struct {
    33  	*cmdtest.TestCmd
    34  }
    35  
    36  type t8nInput struct {
    37  	inAlloc  string
    38  	inTxs    string
    39  	inEnv    string
    40  	stFork   string
    41  	stReward string
    42  }
    43  
    44  func (args *t8nInput) get(base string) []string {
    45  	var out []string
    46  	if opt := args.inAlloc; opt != "" {
    47  		out = append(out, "--input.alloc")
    48  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    49  	}
    50  	if opt := args.inTxs; opt != "" {
    51  		out = append(out, "--input.txs")
    52  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    53  	}
    54  	if opt := args.inEnv; opt != "" {
    55  		out = append(out, "--input.env")
    56  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
    57  	}
    58  	if opt := args.stFork; opt != "" {
    59  		out = append(out, "--state.fork", opt)
    60  	}
    61  	if opt := args.stReward; opt != "" {
    62  		out = append(out, "--state.reward", opt)
    63  	}
    64  	return out
    65  }
    66  
    67  type t8nOutput struct {
    68  	alloc  bool
    69  	result bool
    70  	body   bool
    71  }
    72  
    73  func (args *t8nOutput) get() (out []string) {
    74  	if args.body {
    75  		out = append(out, "--output.body", "stdout")
    76  	} else {
    77  		out = append(out, "--output.body", "") // empty means ignore
    78  	}
    79  	if args.result {
    80  		out = append(out, "--output.result", "stdout")
    81  	} else {
    82  		out = append(out, "--output.result", "")
    83  	}
    84  	if args.alloc {
    85  		out = append(out, "--output.alloc", "stdout")
    86  	} else {
    87  		out = append(out, "--output.alloc", "")
    88  	}
    89  	return out
    90  }
    91  
    92  func TestT8n(t *testing.T) {
    93  	tt := new(testT8n)
    94  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
    95  	for i, tc := range []struct {
    96  		base        string
    97  		input       t8nInput
    98  		output      t8nOutput
    99  		expExitCode int
   100  		expOut      string
   101  	}{
   102  		{ // Test exit (3) on bad config
   103  			base: "./testdata/1",
   104  			input: t8nInput{
   105  				"alloc.json", "txs.json", "env.json", "Frontier+1346", "",
   106  			},
   107  			output:      t8nOutput{alloc: true, result: true},
   108  			expExitCode: 3,
   109  		},
   110  		{
   111  			base: "./testdata/1",
   112  			input: t8nInput{
   113  				"alloc.json", "txs.json", "env.json", "Byzantium", "",
   114  			},
   115  			output: t8nOutput{alloc: true, result: true},
   116  			expOut: "exp.json",
   117  		},
   118  		{ // blockhash test
   119  			base: "./testdata/3",
   120  			input: t8nInput{
   121  				"alloc.json", "txs.json", "env.json", "Berlin", "",
   122  			},
   123  			output: t8nOutput{alloc: true, result: true},
   124  			expOut: "exp.json",
   125  		},
   126  		{ // missing blockhash test
   127  			base: "./testdata/4",
   128  			input: t8nInput{
   129  				"alloc.json", "txs.json", "env.json", "Berlin", "",
   130  			},
   131  			output:      t8nOutput{alloc: true, result: true},
   132  			expExitCode: 4,
   133  		},
   134  		{ // Ommer test
   135  			base: "./testdata/5",
   136  			input: t8nInput{
   137  				"alloc.json", "txs.json", "env.json", "Byzantium", "0x80",
   138  			},
   139  			output: t8nOutput{alloc: true, result: true},
   140  			expOut: "exp.json",
   141  		},
   142  		{ // Sign json transactions
   143  			base: "./testdata/13",
   144  			input: t8nInput{
   145  				"alloc.json", "txs.json", "env.json", "London", "",
   146  			},
   147  			output: t8nOutput{body: true},
   148  			expOut: "exp.json",
   149  		},
   150  		{ // Already signed transactions
   151  			base: "./testdata/13",
   152  			input: t8nInput{
   153  				"alloc.json", "signed_txs.rlp", "env.json", "London", "",
   154  			},
   155  			output: t8nOutput{result: true},
   156  			expOut: "exp2.json",
   157  		},
   158  		{ // Difficulty calculation - no uncles
   159  			base: "./testdata/14",
   160  			input: t8nInput{
   161  				"alloc.json", "txs.json", "env.json", "London", "",
   162  			},
   163  			output: t8nOutput{result: true},
   164  			expOut: "exp.json",
   165  		},
   166  		{ // Difficulty calculation - with uncles
   167  			base: "./testdata/14",
   168  			input: t8nInput{
   169  				"alloc.json", "txs.json", "env.uncles.json", "London", "",
   170  			},
   171  			output: t8nOutput{result: true},
   172  			expOut: "exp2.json",
   173  		},
   174  		{ // Difficulty calculation - with uncles + Berlin
   175  			base: "./testdata/14",
   176  			input: t8nInput{
   177  				"alloc.json", "txs.json", "env.uncles.json", "Berlin", "",
   178  			},
   179  			output: t8nOutput{result: true},
   180  			expOut: "exp_berlin.json",
   181  		},
   182  		{ // Difficulty calculation on arrow glacier
   183  			base: "./testdata/19",
   184  			input: t8nInput{
   185  				"alloc.json", "txs.json", "env.json", "London", "",
   186  			},
   187  			output: t8nOutput{result: true},
   188  			expOut: "exp_london.json",
   189  		},
   190  		{ // Difficulty calculation on arrow glacier
   191  			base: "./testdata/19",
   192  			input: t8nInput{
   193  				"alloc.json", "txs.json", "env.json", "ArrowGlacier", "",
   194  			},
   195  			output: t8nOutput{result: true},
   196  			expOut: "exp_arrowglacier.json",
   197  		},
   198  	} {
   199  
   200  		args := []string{"t8n"}
   201  		args = append(args, tc.output.get()...)
   202  		args = append(args, tc.input.get(tc.base)...)
   203  		var qArgs []string // quoted args for debugging purposes
   204  		for _, arg := range args {
   205  			if len(arg) == 0 {
   206  				qArgs = append(qArgs, `""`)
   207  			} else {
   208  				qArgs = append(qArgs, arg)
   209  			}
   210  		}
   211  		tt.Logf("args: %v\n", strings.Join(qArgs, " "))
   212  		tt.Run("evm-test", args...)
   213  		// Compare the expected output, if provided
   214  		if tc.expOut != "" {
   215  			want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut))
   216  			if err != nil {
   217  				t.Fatalf("test %d: could not read expected output: %v", i, err)
   218  			}
   219  			have := tt.Output()
   220  			ok, err := cmpJson(have, want)
   221  			switch {
   222  			case err != nil:
   223  				t.Fatalf("test %d, json parsing failed: %v", i, err)
   224  			case !ok:
   225  				t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
   226  			}
   227  		}
   228  		tt.WaitExit()
   229  		if have, want := tt.ExitStatus(), tc.expExitCode; have != want {
   230  			t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want)
   231  		}
   232  	}
   233  }
   234  
   235  type t9nInput struct {
   236  	inTxs  string
   237  	stFork string
   238  }
   239  
   240  func (args *t9nInput) get(base string) []string {
   241  	var out []string
   242  	if opt := args.inTxs; opt != "" {
   243  		out = append(out, "--input.txs")
   244  		out = append(out, fmt.Sprintf("%v/%v", base, opt))
   245  	}
   246  	if opt := args.stFork; opt != "" {
   247  		out = append(out, "--state.fork", opt)
   248  	}
   249  	return out
   250  }
   251  
   252  func TestT9n(t *testing.T) {
   253  	tt := new(testT8n)
   254  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
   255  	for i, tc := range []struct {
   256  		base        string
   257  		input       t9nInput
   258  		expExitCode int
   259  		expOut      string
   260  	}{
   261  		{ // London txs on homestead
   262  			base: "./testdata/15",
   263  			input: t9nInput{
   264  				inTxs:  "signed_txs.rlp",
   265  				stFork: "Homestead",
   266  			},
   267  			expOut: "exp.json",
   268  		},
   269  		{ // London txs on London
   270  			base: "./testdata/15",
   271  			input: t9nInput{
   272  				inTxs:  "signed_txs.rlp",
   273  				stFork: "London",
   274  			},
   275  			expOut: "exp2.json",
   276  		},
   277  		{ // An RLP list (a blockheader really)
   278  			base: "./testdata/15",
   279  			input: t9nInput{
   280  				inTxs:  "blockheader.rlp",
   281  				stFork: "London",
   282  			},
   283  			expOut: "exp3.json",
   284  		},
   285  		{ // Transactions with too low gas
   286  			base: "./testdata/16",
   287  			input: t9nInput{
   288  				inTxs:  "signed_txs.rlp",
   289  				stFork: "London",
   290  			},
   291  			expOut: "exp.json",
   292  		},
   293  		{ // Transactions with value exceeding 256 bits
   294  			base: "./testdata/17",
   295  			input: t9nInput{
   296  				inTxs:  "signed_txs.rlp",
   297  				stFork: "London",
   298  			},
   299  			expOut: "exp.json",
   300  		},
   301  		{ // Invalid RLP
   302  			base: "./testdata/18",
   303  			input: t9nInput{
   304  				inTxs:  "invalid.rlp",
   305  				stFork: "London",
   306  			},
   307  			expExitCode: t8ntool.ErrorIO,
   308  		},
   309  	} {
   310  
   311  		args := []string{"t9n"}
   312  		args = append(args, tc.input.get(tc.base)...)
   313  
   314  		tt.Run("evm-test", args...)
   315  		tt.Logf("args:\n go run . %v\n", strings.Join(args, " "))
   316  		// Compare the expected output, if provided
   317  		if tc.expOut != "" {
   318  			want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut))
   319  			if err != nil {
   320  				t.Fatalf("test %d: could not read expected output: %v", i, err)
   321  			}
   322  			have := tt.Output()
   323  			ok, err := cmpJson(have, want)
   324  			switch {
   325  			case err != nil:
   326  				t.Logf(string(have))
   327  				t.Fatalf("test %d, json parsing failed: %v", i, err)
   328  			case !ok:
   329  				t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
   330  			}
   331  		}
   332  		tt.WaitExit()
   333  		if have, want := tt.ExitStatus(), tc.expExitCode; have != want {
   334  			t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want)
   335  		}
   336  	}
   337  }
   338  
   339  // cmpJson compares the JSON in two byte slices.
   340  func cmpJson(a, b []byte) (bool, error) {
   341  	var j, j2 interface{}
   342  	if err := json.Unmarshal(a, &j); err != nil {
   343  		return false, err
   344  	}
   345  	if err := json.Unmarshal(b, &j2); err != nil {
   346  		return false, err
   347  	}
   348  	return reflect.DeepEqual(j2, j), nil
   349  }