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.

169 lines
5.2 KiB

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