github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/cmd/geth/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  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  var customGenesisTests = []struct {
    27  	genesis string
    28  	query   string
    29  	result  string
    30  }{
    31  	// Plain genesis file without anything extra
    32  	{
    33  		genesis: `{
    34  			"alloc"      : {},
    35  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    36  			"difficulty" : "0x20000",
    37  			"extraData"  : "",
    38  			"gasLimit"   : "0x2fefd8",
    39  			"nonce"      : "0x0000000000000042",
    40  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    41  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    42  			"timestamp"  : "0x00"
    43  			"config"     : {"isQuorum":false}
    44  		}`,
    45  		query:  "eth.getBlock(0).nonce",
    46  		result: "0x0000000000000042",
    47  	},
    48  	// Genesis file with an empty chain configuration (ensure missing fields work)
    49  	{
    50  		genesis: `{
    51  			"alloc"      : {},
    52  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    53  			"difficulty" : "0x20000",
    54  			"extraData"  : "",
    55  			"gasLimit"   : "0x2fefd8",
    56  			"nonce"      : "0x0000000000000042",
    57  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    58  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    59  			"timestamp"  : "0x00",
    60  			"config"     : {"isQuorum":false }
    61  		}`,
    62  		query:  "eth.getBlock(0).nonce",
    63  		result: "0x0000000000000042",
    64  	},
    65  	// Genesis file with specific chain configurations
    66  	{
    67  		genesis: `{
    68  			"alloc"      : {},
    69  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    70  			"difficulty" : "0x20000",
    71  			"extraData"  : "",
    72  			"gasLimit"   : "0x2fefd8",
    73  			"nonce"      : "0x0000000000000042",
    74  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    75  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    76  			"timestamp"  : "0x00",
    77  			"config"     : {
    78  				"homesteadBlock" : 314,
    79  				"daoForkBlock"   : 141,
    80  				"daoForkSupport" : true,
    81  				"isQuorum" : false
    82  
    83  			},
    84  		}`,
    85  		query:  "eth.getBlock(0).nonce",
    86  		result: "0x0000000000000042",
    87  	},
    88  }
    89  
    90  // Tests that initializing Geth with a custom genesis block and chain definitions
    91  // work properly.
    92  func TestCustomGenesis(t *testing.T) {
    93  	for i, tt := range customGenesisTests {
    94  		// Create a temporary data directory to use and inspect later
    95  		datadir := tmpdir(t)
    96  		defer os.RemoveAll(datadir)
    97  
    98  		// Initialize the data directory with the custom genesis block
    99  		json := filepath.Join(datadir, "genesis.json")
   100  		if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
   101  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
   102  		}
   103  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
   104  
   105  		// Query the custom genesis block
   106  		geth := runGeth(t,
   107  			"--datadir", datadir, "--maxpeers", "0", "--port", "0",
   108  			"--nodiscover", "--nat", "none", "--ipcdisable",
   109  			"--exec", tt.query, "console")
   110  		geth.ExpectRegexp(tt.result)
   111  		geth.ExpectExit()
   112  	}
   113  }