You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.0 KiB

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