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