github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/geth/genesis_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"io/ioutil"
    16  	"os"
    17  	"path/filepath"
    18  	"testing"
    19  )
    20  
    21  var customGenesisTests = []struct {
    22  	genesis string
    23  	query   string
    24  	result  string
    25  }{
    26  	// Plain genesis file without anything extra
    27  	{
    28  		genesis: `{
    29  			"alloc"      : {},
    30  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    31  			"difficulty" : "0x00001",
    32  			"extraData"  : "",
    33  			"gasLimit"   : "0x2fefd8",
    34  			"nonce"      : "0x0000000000000000",
    35  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    36  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    37  			"timestamp"  : "0x00"
    38  		}`,
    39  		query:  "eth.getBlock(0).nonce",
    40  		result: "0x0000000000000000",
    41  	},
    42  	// Genesis file with an empty chain configuration (ensure missing fields work)
    43  	{
    44  		genesis: `{
    45  			"alloc"      : {},
    46  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    47  			"difficulty" : "0x00001",
    48  			"extraData"  : "",
    49  			"gasLimit"   : "0x2fefd8",
    50  			"nonce"      : "0x0000000000000000",
    51  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    52  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    53  			"timestamp"  : "0x00",
    54  			"config"     : {}
    55  		}`,
    56  		query:  "eth.getBlock(0).nonce",
    57  		result: "0x0000000000000000",
    58  	},
    59  	// Genesis file with specific chain configurations
    60  	{
    61  		genesis: `{
    62  			"alloc"      : {},
    63  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    64  			"difficulty" : "0x00001",
    65  			"extraData"  : "",
    66  			"gasLimit"   : "0x2fefd8",
    67  			"nonce"      : "0x0000000000000000",
    68  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    69  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    70  			"timestamp"  : "0x00",
    71  			"config"     : {
    72  				"homesteadBlock" : 314,
    73  				"daoForkBlock"   : 141,
    74  				"daoForkSupport" : true
    75  			},
    76  		}`,
    77  		query:  "eth.getBlock(0).nonce",
    78  		result: "0x0000000000000000",
    79  	},
    80  }
    81  
    82  // Tests that initializing Geth with a custom genesis block and chain definitions
    83  // work properly.
    84  func TestCustomGenesis(t *testing.T) {
    85  	for i, tt := range customGenesisTests {
    86  		// Create a temporary data directory to use and inspect later
    87  		datadir := tmpdir(t)
    88  		defer os.RemoveAll(datadir)
    89  
    90  		// Initialize the data directory with the custom genesis block
    91  		json := filepath.Join(datadir, "genesis.json")
    92  		if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
    93  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
    94  		}
    95  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
    96  
    97  		// Query the custom genesis block
    98  		geth := runGeth(t,
    99  			"--datadir", datadir, "--maxpeers", "0", "--port", "0",
   100  			"--nodiscover", "--nat", "none", "--ipcdisable",
   101  			"--exec", tt.query, "console")
   102  		geth.ExpectRegexp(tt.result)
   103  		geth.ExpectExit()
   104  	}
   105  }