github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/quickchain/genesis_test.go (about)

     1  
     2  package main
     3  
     4  import (
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  var customGenesisTests = []struct {
    12  	genesis string
    13  	query   string
    14  	result  string
    15  }{
    16  	// Plain genesis file without anything extra
    17  	{
    18  		genesis: `{
    19  			"alloc"      : {},
    20  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    21  			"difficulty" : "0x20000",
    22  			"extraData"  : "",
    23  			"gasLimit"   : "0x2fefd8",
    24  			"nonce"      : "0x0000000000000042",
    25  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    26  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    27  			"timestamp"  : "0x00"
    28  		}`,
    29  		query:  "eth.getBlock(0).nonce",
    30  		result: "0x0000000000000042",
    31  	},
    32  	// Genesis file with an empty chain configuration (ensure missing fields work)
    33  	{
    34  		genesis: `{
    35  			"alloc"      : {},
    36  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    37  			"difficulty" : "0x20000",
    38  			"extraData"  : "",
    39  			"gasLimit"   : "0x2fefd8",
    40  			"nonce"      : "0x0000000000000042",
    41  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    42  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    43  			"timestamp"  : "0x00",
    44  			"config"     : {}
    45  		}`,
    46  		query:  "eth.getBlock(0).nonce",
    47  		result: "0x0000000000000042",
    48  	},
    49  	// Genesis file with specific chain configurations
    50  	{
    51  		genesis: `{
    52  			"alloc"      : {},
    53  			"coinbase"   : "0x0000000000000000000000000000000000000000",
    54  			"difficulty" : "0x20000",
    55  			"extraData"  : "",
    56  			"gasLimit"   : "0x2fefd8",
    57  			"nonce"      : "0x0000000000000042",
    58  			"mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    59  			"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    60  			"timestamp"  : "0x00",
    61  			"config"     : {
    62  				"homesteadBlock" : 314,
    63  				"daoForkBlock"   : 141,
    64  				"daoForkSupport" : true
    65  			},
    66  		}`,
    67  		query:  "eth.getBlock(0).nonce",
    68  		result: "0x0000000000000042",
    69  	},
    70  }
    71  
    72  // Tests that initializing Geth with a custom genesis block and chain definitions
    73  // work properly.
    74  func TestCustomGenesis(t *testing.T) {
    75  	for i, tt := range customGenesisTests {
    76  		// Create a temporary data directory to use and inspect later
    77  		datadir := tmpdir(t)
    78  		defer os.RemoveAll(datadir)
    79  
    80  		// Initialize the data directory with the custom genesis block
    81  		json := filepath.Join(datadir, "genesis.json")
    82  		if err := ioutil.WriteFile(json, []byte(tt.genesis), 0600); err != nil {
    83  			t.Fatalf("test %d: failed to write genesis file: %v", i, err)
    84  		}
    85  		runGeth(t, "--datadir", datadir, "init", json).WaitExit()
    86  
    87  		// Query the custom genesis block
    88  		geth := runGeth(t,
    89  			"--datadir", datadir, "--maxpeers", "0", "--port", "0",
    90  			"--nodiscover", "--nat", "none", "--ipcdisable",
    91  			"--exec", tt.query, "console")
    92  		geth.ExpectRegexp(tt.result)
    93  		geth.ExpectExit()
    94  	}
    95  }