Merge pull request #1559: Inconsistent Genesis Generation Key Order

This commit is contained in:
Aleksandr Bezobchuk 2018-07-05 14:07:03 -04:00
parent 1d12985e56
commit 51fdc9db2f
4 changed files with 25 additions and 27 deletions

View File

@ -29,20 +29,24 @@ func CoolAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState jso
if err != nil {
return
}
key := "cool"
value := json.RawMessage(`{
"trend": "ice-cold"
}`)
appState, err = server.AppendJSON(cdc, appState, key, value)
appState, err = server.InsertKeyJSON(cdc, appState, key, value)
if err != nil {
return
}
key = "pow"
value = json.RawMessage(`{
"difficulty": 1,
"count": 0
}`)
appState, err = server.AppendJSON(cdc, appState, key, value)
appState, err = server.InsertKeyJSON(cdc, appState, key, value)
return
}

View File

@ -332,32 +332,20 @@ func readOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey {
return privValidator.GetPubKey()
}
// create the genesis file
// writeGenesisFile creates and writes the genesis configuration to disk. An
// error is returned if building or writing the configuration to file fails.
func writeGenesisFile(cdc *wire.Codec, genesisFile, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error {
genDoc := tmtypes.GenesisDoc{
ChainID: chainID,
Validators: validators,
ChainID: chainID,
Validators: validators,
AppStateJSON: appState,
}
if err := genDoc.ValidateAndComplete(); err != nil {
return err
}
if err := genDoc.SaveAs(genesisFile); err != nil {
return err
}
return addAppStateToGenesis(cdc, genesisFile, appState)
}
// Add one line to the genesis file
func addAppStateToGenesis(cdc *wire.Codec, genesisConfigPath string, appState json.RawMessage) error {
bz, err := ioutil.ReadFile(genesisConfigPath)
if err != nil {
return err
}
out, err := AppendJSON(cdc, bz, "app_state", appState)
if err != nil {
return err
}
return ioutil.WriteFile(genesisConfigPath, out, 0600)
return genDoc.SaveAs(genesisFile)
}
//_____________________________________________________________________

View File

@ -123,15 +123,21 @@ func AddCommands(
//___________________________________________________________________________________
// append a new json field to existing json message
func AppendJSON(cdc *wire.Codec, baseJSON []byte, key string, value json.RawMessage) (appended []byte, err error) {
// InsertKeyJSON inserts a new JSON field/key with a given value to an existing
// JSON message. An error is returned if any serialization operation fails.
//
// NOTE: The ordering of the keys returned as the resulting JSON message is
// non-deterministic, so the client should not rely on key ordering.
func InsertKeyJSON(cdc *wire.Codec, baseJSON []byte, key string, value json.RawMessage) ([]byte, error) {
var jsonMap map[string]json.RawMessage
err = cdc.UnmarshalJSON(baseJSON, &jsonMap)
if err != nil {
if err := cdc.UnmarshalJSON(baseJSON, &jsonMap); err != nil {
return nil, err
}
jsonMap[key] = value
bz, err := wire.MarshalJSONIndent(cdc, jsonMap)
return json.RawMessage(bz), err
}

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestAppendJSON(t *testing.T) {
func TestInsertKeyJSON(t *testing.T) {
cdc := wire.NewCodec()
foo := map[string]string{"foo": "foofoo"}
@ -24,7 +24,7 @@ func TestAppendJSON(t *testing.T) {
barRaw := json.RawMessage(bz)
// make the append
appBz, err := AppendJSON(cdc, fooRaw, "barOuter", barRaw)
appBz, err := InsertKeyJSON(cdc, fooRaw, "barOuter", barRaw)
require.NoError(t, err)
// test the append