github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/types/genesis_test.go (about)

     1  package types
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/tendermint/tendermint/crypto/ed25519"
    12  	tmjson "github.com/tendermint/tendermint/libs/json"
    13  	tmtime "github.com/tendermint/tendermint/types/time"
    14  )
    15  
    16  func TestGenesisBad(t *testing.T) {
    17  	// test some bad ones from raw json
    18  	testCases := [][]byte{
    19  		{},              // empty
    20  		{1, 1, 1, 1, 1}, // junk
    21  		[]byte(`{}`),    // empty
    22  		[]byte(`{"chain_id":"mychain","validators":[{}]}`),   // invalid validator
    23  		[]byte(`{"chain_id":"chain","initial_height":"-1"}`), // negative initial height
    24  		// missing pub_key type
    25  		[]byte(
    26  			`{"validators":[{"pub_key":{"value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`,
    27  		),
    28  		// missing chain_id
    29  		[]byte(
    30  			`{"validators":[` +
    31  				`{"pub_key":{` +
    32  				`"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="` +
    33  				`},"power":"10","name":""}` +
    34  				`]}`,
    35  		),
    36  		// too big chain_id
    37  		[]byte(
    38  			`{"chain_id": "Lorem ipsum dolor sit amet, consectetuer adipiscing", "validators": [` +
    39  				`{"pub_key":{` +
    40  				`"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="` +
    41  				`},"power":"10","name":""}` +
    42  				`]}`,
    43  		),
    44  		// wrong address
    45  		[]byte(
    46  			`{"chain_id":"mychain", "validators":[` +
    47  				`{"address": "A", "pub_key":{` +
    48  				`"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="` +
    49  				`},"power":"10","name":""}` +
    50  				`]}`,
    51  		),
    52  	}
    53  
    54  	for _, testCase := range testCases {
    55  		_, err := GenesisDocFromJSON(testCase)
    56  		assert.Error(t, err, "expected error for empty genDoc json")
    57  	}
    58  }
    59  
    60  func TestGenesisGood(t *testing.T) {
    61  	// test a good one by raw json
    62  	genDocBytes := []byte(
    63  		`{
    64  			"genesis_time": "0001-01-01T00:00:00Z",
    65  			"chain_id": "test-chain-QDKdJr",
    66  			"initial_height": "1000",
    67  			"consensus_params": null,
    68  			"validators": [{
    69  				"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},
    70  				"power":"10",
    71  				"name":""
    72  			}],
    73  			"app_hash":"",
    74  			"app_state":{"account_owner": "Bob"}
    75  		}`,
    76  	)
    77  	_, err := GenesisDocFromJSON(genDocBytes)
    78  	assert.NoError(t, err, "expected no error for good genDoc json")
    79  
    80  	pubkey := ed25519.GenPrivKey().PubKey()
    81  	// create a base gendoc from struct
    82  	baseGenDoc := &GenesisDoc{
    83  		ChainID:    "abc",
    84  		Validators: []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
    85  	}
    86  	genDocBytes, err = tmjson.Marshal(baseGenDoc)
    87  	assert.NoError(t, err, "error marshalling genDoc")
    88  
    89  	// test base gendoc and check consensus params were filled
    90  	genDoc, err := GenesisDocFromJSON(genDocBytes)
    91  	assert.NoError(t, err, "expected no error for valid genDoc json")
    92  	assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
    93  
    94  	// check validator's address is filled
    95  	assert.NotNil(t, genDoc.Validators[0].Address, "expected validator's address to be filled in")
    96  
    97  	// create json with consensus params filled
    98  	genDocBytes, err = tmjson.Marshal(genDoc)
    99  	assert.NoError(t, err, "error marshalling genDoc")
   100  	genDoc, err = GenesisDocFromJSON(genDocBytes)
   101  	assert.NoError(t, err, "expected no error for valid genDoc json")
   102  
   103  	// test with invalid consensus params
   104  	genDoc.ConsensusParams.Block.MaxBytes = 0
   105  	genDocBytes, err = tmjson.Marshal(genDoc)
   106  	assert.NoError(t, err, "error marshalling genDoc")
   107  	_, err = GenesisDocFromJSON(genDocBytes)
   108  	assert.Error(t, err, "expected error for genDoc json with block size of 0")
   109  
   110  	// Genesis doc from raw json
   111  	missingValidatorsTestCases := [][]byte{
   112  		[]byte(`{"chain_id":"mychain"}`),                   // missing validators
   113  		[]byte(`{"chain_id":"mychain","validators":[]}`),   // missing validators
   114  		[]byte(`{"chain_id":"mychain","validators":null}`), // nil validator
   115  		[]byte(`{"chain_id":"mychain"}`),                   // missing validators
   116  	}
   117  
   118  	for _, tc := range missingValidatorsTestCases {
   119  		_, err := GenesisDocFromJSON(tc)
   120  		assert.NoError(t, err)
   121  	}
   122  }
   123  
   124  func TestGenesisSaveAs(t *testing.T) {
   125  	tmpfile, err := ioutil.TempFile("", "genesis")
   126  	require.NoError(t, err)
   127  	defer os.Remove(tmpfile.Name())
   128  
   129  	genDoc := randomGenesisDoc()
   130  
   131  	// save
   132  	err = genDoc.SaveAs(tmpfile.Name())
   133  	require.NoError(t, err)
   134  	stat, err := tmpfile.Stat()
   135  	require.NoError(t, err)
   136  	if err != nil && stat.Size() <= 0 {
   137  		t.Fatalf("SaveAs failed to write any bytes to %v", tmpfile.Name())
   138  	}
   139  
   140  	err = tmpfile.Close()
   141  	require.NoError(t, err)
   142  
   143  	// load
   144  	genDoc2, err := GenesisDocFromFile(tmpfile.Name())
   145  	require.NoError(t, err)
   146  	assert.EqualValues(t, genDoc2, genDoc)
   147  	assert.Equal(t, genDoc2.Validators, genDoc.Validators)
   148  }
   149  
   150  func TestGenesisValidatorHash(t *testing.T) {
   151  	genDoc := randomGenesisDoc()
   152  	assert.NotEmpty(t, genDoc.ValidatorHash())
   153  }
   154  
   155  func randomGenesisDoc() *GenesisDoc {
   156  	pubkey := ed25519.GenPrivKey().PubKey()
   157  	return &GenesisDoc{
   158  		GenesisTime:     tmtime.Now(),
   159  		ChainID:         "abc",
   160  		InitialHeight:   1000,
   161  		Validators:      []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
   162  		ConsensusParams: DefaultConsensusParams(),
   163  		AppHash:         []byte{1, 2, 3},
   164  	}
   165  }