Skip to content

Commit

Permalink
feat(network): check no gentx are present in the initial genesis (#…
Browse files Browse the repository at this point in the history
…2646)

* gentxs counter

* gentx check

* lint

* fix errors

* lint 2

* fix test
  • Loading branch information
lumtis committed Jul 26, 2022
1 parent 8d8a9e1 commit d3eb838
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
10 changes: 9 additions & 1 deletion ignite/pkg/cosmosutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type (
BondDenom string `json:"bond_denom"`
} `json:"params"`
} `json:"staking"`
Genutil struct {
GenTxs []struct{} `json:"gen_txs"`
} `json:"genutil"`
} `json:"app_state"`
}

Expand All @@ -59,7 +62,7 @@ type (
GenesisField func(fields)
)

// HasAccount check if account exist into the genesis account
// HasAccount checks if account exist into the genesis account
func (g Genesis) HasAccount(address string) bool {
for _, account := range g.Accounts {
if account == address {
Expand All @@ -69,6 +72,11 @@ func (g Genesis) HasAccount(address string) bool {
return false
}

// GenTxCount returns the number of gentxs inside the genesis
func (cg ChainGenesis) GenTxCount() int {
return len(cg.AppState.Genutil.GenTxs)
}

// WithKeyValue sets key and value field to genesis file
func WithKeyValue(key, value string) GenesisField {
return func(f fields) {
Expand Down
35 changes: 35 additions & 0 deletions ignite/pkg/cosmosutil/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func TestParseChainGenesis(t *testing.T) {
Address string `json:"address"`
}{{Address: "cosmos1dd246yq6z5vzjz9gh8cff46pll75yyl8ygndsj"}}
genesis1.AppState.Staking.Params.BondDenom = "stake"
genesis1.AppState.Genutil.GenTxs = []struct{}{{}} // 1 gentx

genesis2 := cosmosutil.ChainGenesis{ChainID: "earth-1"}
genesis2.AppState.Auth.Accounts = []struct {
Address string `json:"address"`
}{{Address: "cosmos1mmlqwyqk7neqegffp99q86eckpm4pjah3ytlpa"}}
genesis2.AppState.Staking.Params.BondDenom = "stake"
genesis2.AppState.Genutil.GenTxs = []struct{}{{}} // 1 gentx

tests := []struct {
name string
Expand Down Expand Up @@ -339,3 +341,36 @@ func TestUpdateGenesis(t *testing.T) {
})
}
}

func TestChainGenesis_GenTxCount(t *testing.T) {
// create a genesis with 10 gentx
testChainGenesis := cosmosutil.ChainGenesis{}
for i := 0; i < 10; i++ {
testChainGenesis.AppState.Genutil.GenTxs = append(
testChainGenesis.AppState.Genutil.GenTxs,
struct{}{},
)
}

tests := []struct {
name string
chainGenesis cosmosutil.ChainGenesis
expected int
}{
{
name: "should return 0 for initialized chain genesis",
chainGenesis: cosmosutil.ChainGenesis{},
expected: 0,
},
{
name: "should return the number of gentxs",
chainGenesis: testChainGenesis,
expected: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.EqualValues(t, tt.expected, tt.chainGenesis.GenTxCount())
})
}
}
24 changes: 21 additions & 3 deletions ignite/services/network/networkchain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package networkchain

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -94,8 +95,8 @@ func (c *Chain) initGenesis(ctx context.Context) error {

}

// check the genesis is valid
if err := c.checkGenesis(ctx); err != nil {
// check the initial genesis is valid
if err := c.checkInitialGenesis(ctx); err != nil {
return err
}

Expand All @@ -104,13 +105,30 @@ func (c *Chain) initGenesis(ctx context.Context) error {
}

// checkGenesis checks the stored genesis is valid
func (c *Chain) checkGenesis(ctx context.Context) error {
func (c *Chain) checkInitialGenesis(ctx context.Context) error {
// perform static analysis of the chain with the validate-genesis command.
chainCmd, err := c.chain.Commands(ctx)
if err != nil {
return err
}

// the chain initial genesis should not contain gentx, gentxs should be added through requests
genesisPath, err := c.chain.GenesisPath()
if err != nil {
return err
}
genesisFile, err := os.ReadFile(genesisPath)
if err != nil {
return err
}
chainGenesis, err := cosmosutil.ParseChainGenesis(genesisFile)
if err != nil {
return err
}
if chainGenesis.GenTxCount() > 0 {
return errors.New("the initial genesis for the chain should not contain gentx")
}

return chainCmd.ValidateGenesis(ctx)

// TODO: static analysis of the genesis with validate-genesis doesn't check the full validity of the genesis
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/network/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestPublish(t *testing.T) {
var (
account = testutil.NewTestAccount(t, testutil.TestAccountName)
customGenesisChainID = "test-custom-1"
customGenesisHash = "72a80a32e33513cd74423354502cef035e96b0bff59c754646b453b201d12d07"
customGenesisHash = "61da86775013bd18d6a019b533eedf1304b778fe8005090a0a0223720adfd8eb"
gts = startGenesisTestServer(cosmosutil.ChainGenesis{ChainID: customGenesisChainID})
suite, network = newSuite(account)
)
Expand Down

0 comments on commit d3eb838

Please sign in to comment.