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.

164 lines
5.0 KiB

  1. package types
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. tmjson "github.com/tendermint/tendermint/libs/json"
  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 TestGenesisGood(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. "consensus_params": null,
  61. "validators": [{
  62. "pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},
  63. "power":"10",
  64. "name":""
  65. }],
  66. "app_hash":"",
  67. "app_state":{"account_owner": "Bob"}
  68. }`,
  69. )
  70. _, err := GenesisDocFromJSON(genDocBytes)
  71. assert.NoError(t, err, "expected no error for good genDoc json")
  72. pubkey := ed25519.GenPrivKey().PubKey()
  73. // create a base gendoc from struct
  74. baseGenDoc := &GenesisDoc{
  75. ChainID: "abc",
  76. Validators: []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
  77. }
  78. genDocBytes, err = tmjson.Marshal(baseGenDoc)
  79. assert.NoError(t, err, "error marshaling genDoc")
  80. // test base gendoc and check consensus params were filled
  81. genDoc, err := GenesisDocFromJSON(genDocBytes)
  82. assert.NoError(t, err, "expected no error for valid genDoc json")
  83. assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
  84. // check validator's address is filled
  85. assert.NotNil(t, genDoc.Validators[0].Address, "expected validator's address to be filled in")
  86. // create json with consensus params filled
  87. genDocBytes, err = tmjson.Marshal(genDoc)
  88. assert.NoError(t, err, "error marshaling genDoc")
  89. genDoc, err = GenesisDocFromJSON(genDocBytes)
  90. assert.NoError(t, err, "expected no error for valid genDoc json")
  91. // test with invalid consensus params
  92. genDoc.ConsensusParams.Block.MaxBytes = 0
  93. genDocBytes, err = tmjson.Marshal(genDoc)
  94. assert.NoError(t, err, "error marshaling genDoc")
  95. _, err = GenesisDocFromJSON(genDocBytes)
  96. assert.Error(t, err, "expected error for genDoc json with block size of 0")
  97. // Genesis doc from raw json
  98. missingValidatorsTestCases := [][]byte{
  99. []byte(`{"chain_id":"mychain"}`), // missing validators
  100. []byte(`{"chain_id":"mychain","validators":[]}`), // missing validators
  101. []byte(`{"chain_id":"mychain","validators":null}`), // nil validator
  102. []byte(`{"chain_id":"mychain"}`), // missing validators
  103. }
  104. for _, tc := range missingValidatorsTestCases {
  105. _, err := GenesisDocFromJSON(tc)
  106. assert.NoError(t, err)
  107. }
  108. }
  109. func TestGenesisSaveAs(t *testing.T) {
  110. tmpfile, err := os.CreateTemp("", "genesis")
  111. require.NoError(t, err)
  112. defer os.Remove(tmpfile.Name())
  113. genDoc := randomGenesisDoc()
  114. // save
  115. err = genDoc.SaveAs(tmpfile.Name())
  116. require.NoError(t, err)
  117. stat, err := tmpfile.Stat()
  118. require.NoError(t, err)
  119. if err != nil && stat.Size() <= 0 {
  120. t.Fatalf("SaveAs failed to write any bytes to %v", tmpfile.Name())
  121. }
  122. err = tmpfile.Close()
  123. require.NoError(t, err)
  124. // load
  125. genDoc2, err := GenesisDocFromFile(tmpfile.Name())
  126. require.NoError(t, err)
  127. assert.EqualValues(t, genDoc2, genDoc)
  128. assert.Equal(t, genDoc2.Validators, genDoc.Validators)
  129. }
  130. func TestGenesisValidatorHash(t *testing.T) {
  131. genDoc := randomGenesisDoc()
  132. assert.NotEmpty(t, genDoc.ValidatorHash())
  133. }
  134. func randomGenesisDoc() *GenesisDoc {
  135. pubkey := ed25519.GenPrivKey().PubKey()
  136. return &GenesisDoc{
  137. GenesisTime: tmtime.Now(),
  138. ChainID: "abc",
  139. InitialHeight: 1000,
  140. Validators: []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
  141. ConsensusParams: DefaultConsensusParams(),
  142. AppHash: []byte{1, 2, 3},
  143. }
  144. }