github.com/ConsenSys/Quorum@v20.10.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  	"strings"
    24  	"testing"
    25  
    26  	"github.com/cespare/cp"
    27  )
    28  
    29  var customGenesisTests = []struct {
    30  	genesis string
    31  	query   string
    32  	result  string
    33  }{
    34  	// Plain genesis file without anything extra
    35  	{
    36  		genesis: `{
    37  			"alloc"      : {},
    38  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    39  			"difficulty" : "0x20000",
    40  			"extraData"  : "",
    41  			"gasLimit"   : "0x2fefd8",
    42  			"nonce"      : "0x0000000000000042",
    43  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    44  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    45  			"timestamp"  : "0x00"
    46  			"config"     : {"isQuorum":false}
    47  		}`,
    48  		query:  "eth.getBlock(0).nonce",
    49  		result: "0x0000000000000042",
    50  	},
    51  	// Genesis file with an empty chain configuration (ensure missing fields work)
    52  	{
    53  		genesis: `{
    54  			"alloc"      : {},
    55  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    56  			"difficulty" : "0x20000",
    57  			"extraData"  : "",
    58  			"gasLimit"   : "0x2fefd8",
    59  			"nonce"      : "0x0000000000000042",
    60  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    61  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    62  			"timestamp"  : "0x00",
    63  			"config"     : {"isQuorum":false }
    64  		}`,
    65  		query:  "eth.getBlock(0).nonce",
    66  		result: "0x0000000000000042",
    67  	},
    68  	// Genesis file with specific chain configurations
    69  	{
    70  		genesis: `{
    71  			"alloc"      : {},
    72  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    73  			"difficulty" : "0x20000",
    74  			"extraData"  : "",
    75  			"gasLimit"   : "0x2fefd8",
    76  			"nonce"      : "0x0000000000000042",
    77  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    78  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    79  			"timestamp"  : "0x00",
    80  			"config"     : {
    81  				"homesteadBlock" : 314,
    82  				"daoForkBlock"   : 141,
    83  				"daoForkSupport" : true,
    84  				"isQuorum" : false
    85  
    86  			},
    87  		}`,
    88  		query:  "eth.getBlock(0).nonce",
    89  		result: "0x0000000000000042",
    90  	},
    91  }
    92  
    93  // Tests that initializing Geth with a custom genesis block and chain definitions
    94  // work properly.
    95  func TestCustomGenesis(t *testing.T) {
    96  	defer SetResetPrivateConfig("ignore")()
    97  	for i, tt := range customGenesisTests {
    98  		// Create a temporary data directory to use and inspect later
    99  		datadir := tmpdir(t)
   100  		defer os.RemoveAll(datadir)
   101  
   102  		// copy the node key and static-nodes.json so that geth can start with the raft consensus
   103  		gethDir := filepath.Join(datadir, "geth")
   104  		sourceNodeKey := filepath.Join("testdata", "geth")
   105  		if err := cp.CopyAll(gethDir, sourceNodeKey); err != nil {
   106  			t.Fatal(err)
   107  		}
   108  
   109  		// Initialize the data directory with the custom genesis block
   110  		json := filepath.Join(datadir, "genesis.json")
   111  		if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
   112  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
   113  		}
   114  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
   115  
   116  		// Query the custom genesis block
   117  		geth := runGeth(t,
   118  			"--datadir", datadir, "--maxpeers", "0", "--port", "0",
   119  			"--nodiscover", "--nat", "none", "--ipcdisable",
   120  			"--raft",
   121  			"--exec", tt.query, "console")
   122  		geth.ExpectRegexp(tt.result)
   123  		geth.ExpectExit()
   124  	}
   125  }
   126  
   127  func TestCustomGenesisUpgradeWithPrivacyEnhancementsBlock(t *testing.T) {
   128  	defer SetResetPrivateConfig("ignore")()
   129  	// Create a temporary data directory to use and inspect later
   130  	datadir := tmpdir(t)
   131  	defer os.RemoveAll(datadir)
   132  
   133  	// copy the node key and static-nodes.json so that geth can start with the raft consensus
   134  	gethDir := filepath.Join(datadir, "geth")
   135  	sourceNodeKey := filepath.Join("testdata", "geth")
   136  	if err := cp.CopyAll(gethDir, sourceNodeKey); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	genesisContentWithoutPrivacyEnhancements :=
   141  		`{
   142  			"alloc"      : {},
   143  			"coinbase"   : "0x0000000000000000000000000000000000000000",
   144  			"difficulty" : "0x20000",
   145  			"extraData"  : "",
   146  			"gasLimit"   : "0x2fefd8",
   147  			"nonce"      : "0x0000000000000042",
   148  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
   149  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
   150  			"timestamp"  : "0x00",
   151  			"config"     : {
   152  				"homesteadBlock" : 314,
   153  				"daoForkBlock"   : 141,
   154  				"daoForkSupport" : true,
   155  				"isQuorum" : false
   156  			}
   157  		}`
   158  
   159  	// Initialize the data directory with the custom genesis block
   160  	json := filepath.Join(datadir, "genesis.json")
   161  	if err := ioutil.WriteFile(json, []byte(genesisContentWithoutPrivacyEnhancements), 0600); err != nil {
   162  		t.Fatalf("failed to write genesis file: %v", err)
   163  	}
   164  	geth := runGeth(t, "--datadir", datadir, "init", json)
   165  	geth.WaitExit()
   166  
   167  	genesisContentWithPrivacyEnhancements :=
   168  		`{
   169  			"alloc"      : {},
   170  			"coinbase"   : "0x0000000000000000000000000000000000000000",
   171  			"difficulty" : "0x20000",
   172  			"extraData"  : "",
   173  			"gasLimit"   : "0x2fefd8",
   174  			"nonce"      : "0x0000000000000042",
   175  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
   176  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
   177  			"timestamp"  : "0x00",
   178  			"config"     : {
   179  				"homesteadBlock" : 314,
   180  				"daoForkBlock"   : 141,
   181  				"privacyEnhancementsBlock"   : 1000,
   182  				"daoForkSupport" : true,
   183  				"isQuorum" : false
   184  			}
   185  		}`
   186  
   187  	if err := ioutil.WriteFile(json, []byte(genesisContentWithPrivacyEnhancements), 0600); err != nil {
   188  		t.Fatalf("failed to write genesis file: %v", err)
   189  	}
   190  	geth = runGeth(t, "--datadir", datadir, "init", json)
   191  	geth.WaitExit()
   192  
   193  	expectedText := "Privacy enhancements have been enabled from block height 1000. Please ensure your privacy manager is upgraded and supports privacy enhancements"
   194  
   195  	result := strings.TrimSpace(geth.StderrText())
   196  	if !strings.Contains(result, expectedText) {
   197  		geth.Fatalf("bad stderr text. want '%s', got '%s'", expectedText, result)
   198  	}
   199  
   200  	// start quorum - it should fail the transaction manager PrivacyEnhancements feature validation
   201  	geth = runGeth(t,
   202  		"--datadir", datadir, "--maxpeers", "0", "--port", "0",
   203  		"--nodiscover", "--nat", "none", "--ipcdisable",
   204  		"--raft", "console")
   205  	geth.ExpectRegexp("Cannot start quorum with privacy enhancements enabled while the transaction manager does not support it")
   206  	geth.ExpectExit()
   207  }