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