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