mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-15 11:46:11 +00:00
genesis: add support for arbitrary initial height (#5191)
Adds a genesis parameter `initial_height` which specifies the initial block height, as well as ABCI `RequestInitChain.InitialHeight` to pass it to the ABCI application, and `State.InitialHeight` to keep track of the initial height throughout the code. Fixes #2543, based on [RFC-002](https://github.com/tendermint/spec/pull/119). Spec changes in https://github.com/tendermint/spec/pull/135.
This commit is contained in:
+2
-4
@@ -65,10 +65,8 @@ func (b *Block) ValidateBasic() error {
|
||||
if b.LastCommit == nil {
|
||||
return errors.New("nil LastCommit")
|
||||
}
|
||||
if b.Header.Height > 1 {
|
||||
if err := b.LastCommit.ValidateBasic(); err != nil {
|
||||
return fmt.Errorf("wrong LastCommit: %v", err)
|
||||
}
|
||||
if err := b.LastCommit.ValidateBasic(); err != nil {
|
||||
return fmt.Errorf("wrong LastCommit: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
|
||||
|
||||
@@ -39,6 +39,7 @@ type GenesisValidator struct {
|
||||
type GenesisDoc struct {
|
||||
GenesisTime time.Time `json:"genesis_time"`
|
||||
ChainID string `json:"chain_id"`
|
||||
InitialHeight int64 `json:"initial_height"`
|
||||
ConsensusParams *tmproto.ConsensusParams `json:"consensus_params,omitempty"`
|
||||
Validators []GenesisValidator `json:"validators,omitempty"`
|
||||
AppHash tmbytes.HexBytes `json:"app_hash"`
|
||||
@@ -73,6 +74,12 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
|
||||
if len(genDoc.ChainID) > MaxChainIDLen {
|
||||
return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
|
||||
}
|
||||
if genDoc.InitialHeight < 0 {
|
||||
return fmt.Errorf("initial_height cannot be negative (got %v)", genDoc.InitialHeight)
|
||||
}
|
||||
if genDoc.InitialHeight == 0 {
|
||||
genDoc.InitialHeight = 1
|
||||
}
|
||||
|
||||
if genDoc.ConsensusParams == nil {
|
||||
genDoc.ConsensusParams = DefaultConsensusParams()
|
||||
|
||||
+18
-9
@@ -19,7 +19,8 @@ func TestGenesisBad(t *testing.T) {
|
||||
{}, // empty
|
||||
{1, 1, 1, 1, 1}, // junk
|
||||
[]byte(`{}`), // empty
|
||||
[]byte(`{"chain_id":"mychain","validators":[{}]}`), // invalid validator
|
||||
[]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":""}]}`,
|
||||
@@ -59,11 +60,19 @@ func TestGenesisBad(t *testing.T) {
|
||||
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","consensus_params":null,"validators":[` +
|
||||
`{"pub_key":{` +
|
||||
`"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="` +
|
||||
`},"power":"10","name":""}` +
|
||||
`],"app_hash":"","app_state":{"account_owner": "Bob"}}`,
|
||||
`{
|
||||
"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")
|
||||
@@ -133,9 +142,7 @@ func TestGenesisSaveAs(t *testing.T) {
|
||||
// load
|
||||
genDoc2, err := GenesisDocFromFile(tmpfile.Name())
|
||||
require.NoError(t, err)
|
||||
|
||||
// fails to unknown reason
|
||||
// assert.EqualValues(t, genDoc2, genDoc)
|
||||
assert.EqualValues(t, genDoc2, genDoc)
|
||||
assert.Equal(t, genDoc2.Validators, genDoc.Validators)
|
||||
}
|
||||
|
||||
@@ -149,7 +156,9 @@ func randomGenesisDoc() *GenesisDoc {
|
||||
return &GenesisDoc{
|
||||
GenesisTime: tmtime.Now(),
|
||||
ChainID: "abc",
|
||||
InitialHeight: 1000,
|
||||
Validators: []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
|
||||
ConsensusParams: DefaultConsensusParams(),
|
||||
AppHash: []byte{1, 2, 3},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user