Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genesis port script v0.34.0 #4023

Merged
merged 29 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0d88bc9
genesis port script
Apr 2, 2019
9aa7005
add command
Apr 2, 2019
e20d778
various fixes
Apr 2, 2019
4c549f2
works
fedekunze Apr 3, 2019
62bca4d
cleanup and move to script
fedekunze Apr 3, 2019
f6887b0
typo
fedekunze Apr 3, 2019
8915f47
check args
fedekunze Apr 3, 2019
1dab733
rename files and fix scripts
fedekunze Apr 3, 2019
ddec599
command to run
fedekunze Apr 3, 2019
4d726bc
address comments from review
fedekunze Apr 3, 2019
5e4f838
use flags
fedekunze Apr 3, 2019
2c99fb9
changelog
fedekunze Apr 4, 2019
d88b6d2
Update .pending/improvements/gaia/4018-create-genesis-
cwgoes Apr 4, 2019
2f3012a
update value and validation test
fedekunze Apr 4, 2019
39d7ce0
Merge branch 'fedekunze/4018-genesis-scritp' of https://github.com/co…
fedekunze Apr 4, 2019
9df1f1b
add chain-id
fedekunze Apr 4, 2019
50c58c6
Merge branch 'release/v0.34.0' into fedekunze/4018-genesis-scritp
alexanderbez Apr 4, 2019
b55d40a
New python script
alessio Apr 4, 2019
31952f1
delete go files
fedekunze Apr 4, 2019
4a3b178
numbers to strings
fedekunze Apr 4, 2019
9c6a515
add crisis constant fee
fedekunze Apr 5, 2019
afe9952
Exit with error on stderr
alessio Apr 7, 2019
6e01099
Refactoring, slightly more modular design
alessio Apr 7, 2019
1b56d64
Fix indentation
alessio Apr 8, 2019
34a9b36
Remove unnecessary variable
alessio Apr 8, 2019
0fd205c
Minor reformatting
alexanderbez Apr 8, 2019
1581756
Minor reformatting
alexanderbez Apr 8, 2019
c4de703
Rename file
alexanderbez Apr 8, 2019
5c85697
Update contrib/export/v0.33.x-to-v0.34.0.py
cwgoes Apr 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions scripts/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package export
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

app "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
tmtypes "github.com/tendermint/tendermint/types"
)

// GenesisFile defines the Gaia genesis format
type GenesisFile struct {
GenesisTime string `json:"genesis_time"`
ChainID string `json:"chain_id"`
ConsensusParams *tmtypes.ConsensusParams `json:"consensus_params"`
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
AppHash string `json:"app_hash"`
AppState app.GenesisState `json:"app_state"`
}

// NewGenesisFile builds a default GenesisDoc and creates a GenesisFile from it
func NewGenesisFile(cdc *codec.Codec, path string) (GenesisFile, error) {

genDoc, err := importGenesis(path)
if err != nil {
return GenesisFile{}, err
}

var appState app.GenesisState
if genDoc.AppState == nil {
appState = app.GenesisState{}
} else {
if err = cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil {
return GenesisFile{}, err
}
}

return GenesisFile{
GenesisTime: genDoc.GenesisTime.String(),
ChainID: genDoc.ChainID,
ConsensusParams: genDoc.ConsensusParams,
AppHash: genDoc.AppHash.String(),
AppState: appState,
}, nil
}

// ValidateInputs validates each of the parameters used by
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
func ValidateInputs(path, chainID, genesisTime string) error {
if chainID = strings.Trim(chainID, " "); chainID == "" {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("chain-id cannot be blank")
}
_, err := time.Parse(time.RFC3339, genesisTime)
if err != nil {
return err
}

if ext := filepath.Ext(path); ext != ".json" {
return fmt.Errorf("%s is not a JSON file", path)
}

if _, err = os.Stat(path); err != nil {
return err
}
return nil
}

// importGenesis imports genesis from JSON and completes missing fields
func importGenesis(path string) (genDoc *tmtypes.GenesisDoc, err error) {
genDoc, err = tmtypes.GenesisDocFromFile(path)
if err != nil {
return
}

err = genDoc.ValidateAndComplete()
if err != nil {
return
}
return
}

func defaultGenesisDoc(chainID string) (tmtypes.GenesisDoc, error) {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
genDoc := tmtypes.GenesisDoc{
ChainID: chainID,
}
err := (&genDoc).ValidateAndComplete()
if err != nil {
return genDoc, err
}
return genDoc, nil
}
72 changes: 72 additions & 0 deletions scripts/export/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package export
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

app "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"
)

var (
path = "./genesis.json"
chainID = "cosmos-zone"
genesisTime = "2019-02-11T12:00:00Z"
)

func TestNewGenesisFile(t *testing.T) {
cdc := app.MakeCodec()
genDoc, err := defaultGenesisDoc(chainID)
require.NoError(t, err)

output, err := cdc.MarshalJSONIndent(genDoc, "", " ")
require.NoError(t, err)

err = ioutil.WriteFile(path, output, 0644)
require.NoError(t, err)

genesisFile, err := NewGenesisFile(cdc, path)
require.NoError(t, err)
require.NotEqual(t, GenesisFile{}, genesisFile)
os.Remove(path)
}

func TestDefaultGenesisDoc(t *testing.T) {
expectedGenDoc := tmtypes.GenesisDoc{ChainID: chainID}
genDoc, err := defaultGenesisDoc(chainID)
require.NoError(t, err)
require.NotEqual(t, expectedGenDoc, genDoc)

genDoc, err = defaultGenesisDoc("")
require.Error(t, err)
}

func TestImportGenesis(t *testing.T) {
genesis := tmtypes.GenesisDoc{ChainID: chainID}

output, err := json.Marshal(genesis)
require.NoError(t, err)

err = ioutil.WriteFile(path, output, 0644)
require.NoError(t, err)

genDoc, err := importGenesis(path)
require.NoError(t, err)
require.NotEqual(t, genesis, genDoc)
os.Remove(path)

// should fail with invalid genesis
genesis = tmtypes.GenesisDoc{}
output, err = json.Marshal(genesis)
require.NoError(t, err)

err = ioutil.WriteFile(path, output, 0644)
require.NoError(t, err)

genDoc, err = importGenesis(path)
require.Error(t, err)
os.Remove(path)
}
59 changes: 59 additions & 0 deletions scripts/export/v0_33_0/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"
"strings"

app "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/scripts/export"
)

// Command: go run main.go [path_to_old_genesis.json] [chain-id] [genesis-start-time] > [path_to_new_genesis.json]
func main() {
cdc := app.MakeCodec()

args := os.Args[1:]
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if len(args) != 3 {
panic(fmt.Errorf("please provide path, chain-id and genesis time"))
}

pathToGenesis := args[0]
chainID := args[1]
genesisTime := args[2]

err := export.ValidateInputs(pathToGenesis, chainID, genesisTime)
if err != nil {
panic(err)
}

genesis, err := export.NewGenesisFile(cdc, pathToGenesis)
if err != nil {
panic(err)
}

genesis.ChainID = strings.Trim(chainID, " ")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
genesis.GenesisTime = genesisTime

// proposal #1 updates
genesis.AppState.MintData.Params.BlocksPerYear = 4855015

// proposal #2 updates
genesis.ConsensusParams.Block.MaxGas = 200000
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
genesis.ConsensusParams.Block.MaxBytes = 2000000

// enable transfers
genesis.AppState.BankData.SendEnabled = true
genesis.AppState.DistrData.WithdrawAddrEnabled = true

err = app.GaiaValidateGenesisState(genesis.AppState)
if err != nil {
panic(err)
}

genesisJSON, err := cdc.MarshalJSONIndent(genesis, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(genesisJSON))
}