Skip to content

Commit

Permalink
fix mixing of node consensus and operator keys
Browse files Browse the repository at this point in the history
mint additionnal for node so it can run the chain alone.
  • Loading branch information
tbruyelle committed Jan 17, 2025
1 parent 53156ad commit d5445ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func tallyGenesisCmd() *ffcli.Command {
numVals := fs.Int("numVals", 1, "number of validators")
numDels := fs.Int("numDels", 0, "number of delegators")
numGovs := fs.Int("numGovs", 0, "number of governors")
nodePubkey := fs.String("nodePubkey", "", "pubkey of the validator node that will run the genesis")
nodeAddr := fs.String("nodeAddr", "", "bech32 address of the validator node that will run the genesis")
nodeConsPubkey := fs.String("nodeConsPubkey", "", "consensus pubkey of the validator node that will run the genesis")
return &ffcli.Command{
Name: "tally-genesis",
ShortUsage: "govbox tally-genesis <genesis.json>",
Expand All @@ -85,10 +86,13 @@ Used to evaluate the performance of the governance tally.`,
if *numVals < 1 {
return fmt.Errorf("numVals must be greater than 0")
}
if *nodePubkey == "" {
return fmt.Errorf("nodePubkey flag must be provided")
if *nodeAddr == "" {
return fmt.Errorf("nodeAddr flag must be provided")
}
return tallyGenesis(ctx, fs.Arg(0), *nodePubkey, *numVals, *numDels, *numGovs)
if *nodeConsPubkey == "" {
return fmt.Errorf("nodeConsPubkey flag must be provided")
}
return tallyGenesis(ctx, fs.Arg(0), *nodeAddr, *nodeConsPubkey, *numVals, *numDels, *numGovs)
},
}
}
Expand Down
44 changes: 33 additions & 11 deletions tally_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func tallyGenesis(goCtx context.Context, genesisFile, nodePubkey string, numVals, numDels, numGovs int) error {
func tallyGenesis(goCtx context.Context, genesisFile, nodeAddr string, nodeConsPubkey string, numVals, numDels, numGovs int) error {
cmd.InitSDKConfig()
var (
addrs = sims.CreateRandomAccounts(numVals + numDels + numGovs)
Expand All @@ -54,12 +54,12 @@ func tallyGenesis(goCtx context.Context, genesisFile, nodePubkey string, numVals
)
// unmarshal node pubkey
var nodePK cryptotypes.PubKey
err := cdc.UnmarshalInterfaceJSON([]byte(nodePubkey), &nodePK)
err := cdc.UnmarshalInterfaceJSON([]byte(nodeConsPubkey), &nodePK)
if err != nil {
panic(err)
}
// Set validator node to the first validator
addrs[0] = sdk.AccAddress(nodePK.Address())
addrs[0] = sdk.MustAccAddressFromBech32(nodeAddr)
valAddrs[0] = sdk.ValAddress(addrs[0])

// create keepers and msgServers
Expand All @@ -81,15 +81,31 @@ func tallyGenesis(goCtx context.Context, genesisFile, nodePubkey string, numVals
gov.InitGenesis(ctx, ak, bk, gk, govGenesis)
govMsgServer := govkeeper.NewMsgServerImpl(gk)

// fill bank balances
amt := sdk.NewCoins(sdk.NewInt64Coin("uatone", 1_000_000_000_000))
// mint amt * number of accounts
err = bk.MintCoins(ctx, banktypes.ModuleName, amt.MulInt(sdk.NewInt(int64(len(addrs)))))
// fill all address bank balances with addrAmt
var (
addrAmt = sdk.NewInt(1_000_000_000_000)
addrBalance = sdk.NewCoins(sdk.NewCoin("uatone", addrAmt))
// mint amt * number of addresses
totalAddrAmt = addrAmt.Mul(sdk.NewInt(int64(len(addrs))))
// mint 3 x totalAddrAmt for the node so it can run the chain alone
nodeAmt = totalAddrAmt.Mul(sdk.NewInt(3))
nodeBalance = sdk.NewCoins(sdk.NewCoin("uatone", nodeAmt))
supplyAmt = totalAddrAmt.Add(nodeAmt)
supplyBalance = sdk.NewCoins(sdk.NewCoin("uatone", supplyAmt))
)
err = bk.MintCoins(ctx, banktypes.ModuleName, supplyBalance)
if err != nil {
panic(err)
}
// send amt to each account
for _, a := range addrs {
for i, a := range addrs {
var amt sdk.Coins
if i == 0 {
// first address is the node
amt = nodeBalance
} else {
amt = addrBalance
}
err := bk.SendCoinsFromModuleToAccount(ctx, banktypes.ModuleName, a, amt)
if err != nil {
panic(err)
Expand All @@ -104,16 +120,22 @@ func tallyGenesis(goCtx context.Context, genesisFile, nodePubkey string, numVals
MaxRate: sdk.MustNewDecFromStr("0.2"),
MaxChangeRate: sdk.MustNewDecFromStr("0.01"),
}
var pk cryptotypes.PubKey
var (
pk cryptotypes.PubKey
selfDelegation sdk.Coin
)
if i == 0 {
// first validator is our operator, use the pk from parameters
pk = nodePK
// use full node balance for self delegation so it got >67% of voting power
selfDelegation = sdk.NewCoin("uatone", nodeAmt)
} else {
// other validators get a random pk
pk = ed25519.GenPrivKey().PubKey()
selfDelegation = sdk.NewInt64Coin("uatone", 1_000_000)
}
msg, err := stakingtypes.NewMsgCreateValidator(valOpAddr, pk,
sdk.NewInt64Coin("uatone", 1_000_000), description, commissionRates,
selfDelegation, description, commissionRates,
sdk.NewInt(1))
if err != nil {
panic(err)
Expand Down Expand Up @@ -179,7 +201,7 @@ func tallyGenesis(goCtx context.Context, genesisFile, nodePubkey string, numVals
}

// create proposal
msg, err := govv1types.NewMsgSubmitProposal(nil, minDeposit, addrs[0].String(), "", "my prop", "")
msg, err := govv1types.NewMsgSubmitProposal(nil, minDeposit, addrs[1].String(), "", "my prop", "")
if err != nil {
panic(err)
}
Expand Down

0 comments on commit d5445ee

Please sign in to comment.