github.com/theQRL/go-zond@v0.2.1/cmd/gzond/genesis_test.go (about)

     1  // Copyright 2016 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  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strconv"
    24  	"testing"
    25  )
    26  
    27  var customGenesisTests = []struct {
    28  	genesis string
    29  	query   string
    30  	result  string
    31  }{
    32  	// Genesis file with an empty chain configuration (ensure missing fields work)
    33  	{
    34  		genesis: `{
    35  			"alloc"      : {},
    36  			"coinbase"   : "Z0000000000000000000000000000000000000000",
    37  			"extraData"  : "0x0000000000001338",
    38  			"gasLimit"   : "0x2fefd8",
    39  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    40  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    41  			"timestamp"  : "0x00",
    42  			"config"     : {}
    43  		}`,
    44  		query:  "zond.getBlock(0).extraData",
    45  		result: "0x0000000000001338",
    46  	},
    47  	// Genesis file with specific chain configurations
    48  	{
    49  		genesis: `{
    50  			"alloc"      : {},
    51  			"coinbase"   : "Z0000000000000000000000000000000000000000",
    52  			"extraData"  : "0x0000000000001339",
    53  			"gasLimit"   : "0x2fefd8",
    54  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    55  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    56  			"timestamp"  : "0x00",
    57  			"config"     : {}
    58  		}`,
    59  		query:  "zond.getBlock(0).extraData",
    60  		result: "0x0000000000001339",
    61  	},
    62  }
    63  
    64  // Tests that initializing Gzond with a custom genesis block and chain definitions
    65  // work properly.
    66  func TestCustomGenesis(t *testing.T) {
    67  	t.Parallel()
    68  	for i, tt := range customGenesisTests {
    69  		// Create a temporary data directory to use and inspect later
    70  		datadir := t.TempDir()
    71  
    72  		// Initialize the data directory with the custom genesis block
    73  		json := filepath.Join(datadir, "genesis.json")
    74  		if err := os.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
    75  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
    76  		}
    77  		runGzond(t, "--datadir", datadir, "init", json).WaitExit()
    78  
    79  		// Query the custom genesis block
    80  		gzond := runGzond(t, "--networkid", "1337", "--syncmode=full", "--cache", "16",
    81  			"--datadir", datadir, "--maxpeers", "0", "--port", "0", "--authrpc.port", "0",
    82  			"--nodiscover", "--nat", "none", "--ipcdisable",
    83  			"--exec", tt.query, "console")
    84  		gzond.ExpectRegexp(tt.result)
    85  		gzond.ExpectExit()
    86  	}
    87  }
    88  
    89  // TestCustomBackend that the backend selection and detection (leveldb vs pebble) works properly.
    90  func TestCustomBackend(t *testing.T) {
    91  	t.Parallel()
    92  	// Test pebble, but only on 64-bit platforms
    93  	if strconv.IntSize != 64 {
    94  		t.Skip("Custom backends are only available on 64-bit platform")
    95  	}
    96  	genesis := `{
    97  		"alloc"      : {},
    98  		"coinbase"   : "Z0000000000000000000000000000000000000000",
    99  		"extraData"  : "0x0000000000001338",
   100  		"gasLimit"   : "0x2fefd8",
   101  		"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
   102  		"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
   103  		"timestamp"  : "0x00",
   104  		"config"     : {}
   105  	}`
   106  	type backendTest struct {
   107  		initArgs   []string
   108  		initExpect string
   109  		execArgs   []string
   110  		execExpect string
   111  	}
   112  	testfunc := func(t *testing.T, tt backendTest) error {
   113  		// Create a temporary data directory to use and inspect later
   114  		datadir := t.TempDir()
   115  
   116  		// Initialize the data directory with the custom genesis block
   117  		json := filepath.Join(datadir, "genesis.json")
   118  		if err := os.WriteFile(json, []byte(genesis), 0600); err != nil {
   119  			return fmt.Errorf("failed to write genesis file: %v", err)
   120  		}
   121  		{ // Init
   122  			args := append(tt.initArgs, "--datadir", datadir, "init", json)
   123  			gzond := runGzond(t, args...)
   124  			gzond.ExpectRegexp(tt.initExpect)
   125  			gzond.ExpectExit()
   126  		}
   127  		{ // Exec + query
   128  			args := append(tt.execArgs, "--networkid", "1337", "--syncmode=full", "--cache", "16",
   129  				"--datadir", datadir, "--maxpeers", "0", "--port", "0", "--authrpc.port", "0",
   130  				"--nodiscover", "--nat", "none", "--ipcdisable",
   131  				"--exec", "zond.getBlock(0).extraData", "console")
   132  			gzond := runGzond(t, args...)
   133  			gzond.ExpectRegexp(tt.execExpect)
   134  			gzond.ExpectExit()
   135  		}
   136  		return nil
   137  	}
   138  	for i, tt := range []backendTest{
   139  		{ // When not specified, it should default to pebble
   140  			execArgs:   []string{"--db.engine", "pebble"},
   141  			execExpect: "0x0000000000001338",
   142  		},
   143  		{ // Explicit leveldb
   144  			initArgs:   []string{"--db.engine", "leveldb"},
   145  			execArgs:   []string{"--db.engine", "leveldb"},
   146  			execExpect: "0x0000000000001338",
   147  		},
   148  		{ // Explicit leveldb first, then autodiscover
   149  			initArgs:   []string{"--db.engine", "leveldb"},
   150  			execExpect: "0x0000000000001338",
   151  		},
   152  		{ // Explicit pebble
   153  			initArgs:   []string{"--db.engine", "pebble"},
   154  			execArgs:   []string{"--db.engine", "pebble"},
   155  			execExpect: "0x0000000000001338",
   156  		},
   157  		{ // Explicit pebble, then auto-discover
   158  			initArgs:   []string{"--db.engine", "pebble"},
   159  			execExpect: "0x0000000000001338",
   160  		},
   161  		{ // Can't start pebble on top of leveldb
   162  			initArgs:   []string{"--db.engine", "leveldb"},
   163  			execArgs:   []string{"--db.engine", "pebble"},
   164  			execExpect: `Fatal: Could not open database: db.engine choice was pebble but found pre-existing leveldb database in specified data directory`,
   165  		},
   166  		{ // Can't start leveldb on top of pebble
   167  			initArgs:   []string{"--db.engine", "pebble"},
   168  			execArgs:   []string{"--db.engine", "leveldb"},
   169  			execExpect: `Fatal: Could not open database: db.engine choice was leveldb but found pre-existing pebble database in specified data directory`,
   170  		},
   171  		{ // Reject invalid backend choice
   172  			initArgs:   []string{"--db.engine", "mssql"},
   173  			initExpect: `Fatal: Invalid choice for db.engine 'mssql', allowed 'leveldb' or 'pebble'`,
   174  			// Since the init fails, this will return the (default) mainnet genesis
   175  			// block nonce
   176  			execExpect: `0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa`,
   177  		},
   178  	} {
   179  		if err := testfunc(t, tt); err != nil {
   180  			t.Fatalf("test %d-leveldb: %v", i, err)
   181  		}
   182  	}
   183  }