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