Skip to content

Commit

Permalink
e2e: refactor democracy tests and related actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Mar 11, 2024
1 parent 4a2a951 commit 8fdf5cf
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 133 deletions.
15 changes: 6 additions & 9 deletions tests/e2e/action_rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,12 @@ func GetSubmitConsumerRemovalProposalActionGen() *rapid.Generator[SubmitConsumer
})
}

func GetSubmitParamChangeProposalActionGen() *rapid.Generator[SubmitParamChangeLegacyProposalAction] {
return rapid.Custom(func(t *rapid.T) SubmitParamChangeLegacyProposalAction {
return SubmitParamChangeLegacyProposalAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
From: GetValidatorIDGen().Draw(t, "From"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
Subspace: rapid.String().Draw(t, "Subspace"),
Key: rapid.String().Draw(t, "Key"),
Value: rapid.String().Draw(t, "Value"), // could make this more generic in the future, since Value takes interfaces
func GetSubmitParamChangeProposalActionGen() *rapid.Generator[SubmitLegacyTextProposalAction] {
return rapid.Custom(func(t *rapid.T) SubmitLegacyTextProposalAction {
return SubmitLegacyTextProposalAction{
Chain: GetChainIDGen().Draw(t, "Chain"),
From: GetValidatorIDGen().Draw(t, "From"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
}
})
}
Expand Down
127 changes: 63 additions & 64 deletions tests/e2e/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,66 +390,24 @@ func (tr TestConfig) submitConsumerRemovalProposal(
tr.waitBlocks(ChainID("provi"), 2, 20*time.Second)
}

type SubmitParamChangeLegacyProposalAction struct {
Chain ChainID
From ValidatorID
Deposit uint
Subspace string
Key string
Value interface{}
}

type paramChangeProposalJSON struct {
Title string `json:"title"`
Summary string `json:"summary"`
Description string `json:"description"`
Changes []paramChangeJSON `json:"changes"`
Deposit string `json:"deposit"`
}

type paramChangeJSON struct {
Subspace string `json:"subspace"`
Key string `json:"key"`
Value interface{} `json:"value"`
type SubmitLegacyTextProposalAction struct {
Chain ChainID
From ValidatorID
Deposit uint
}

func (tr TestConfig) submitParamChangeProposal(
action SubmitParamChangeLegacyProposalAction,
func (tr TestConfig) submitLegacyTextProposal(
action SubmitLegacyTextProposalAction,
target ExecutionTarget,
verbose bool,
) {
prop := paramChangeProposalJSON{
Title: "Legacy Param change",
Summary: "Changing legacy module params",
Description: "Changing legacy module params",
Changes: []paramChangeJSON{{Subspace: action.Subspace, Key: action.Key, Value: action.Value}},
Deposit: fmt.Sprint(action.Deposit) + `stake`,
}

bz, err := json.Marshal(prop)
if err != nil {
log.Fatal(err)
}

jsonStr := string(bz)
if strings.Contains(jsonStr, "'") {
log.Fatal("prop json contains single quote")
}

//#nosec G204 -- bypass unsafe quoting warning (no production code)
bz, err = target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/params-proposal.json"),
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}

cmd := target.ExecCommand(
tr.chainConfigs[action.Chain].BinaryName,

"tx", "gov", "submit-legacy-proposal", "param-change", "/params-proposal.json",

"tx", "gov", "submit-legacy-proposal",
"--type", "Text",
"--title", "Test Proposal",
"--description", "testing",
"--deposit", fmt.Sprintf("%dstake", action.Deposit),
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
Expand All @@ -459,13 +417,17 @@ func (tr TestConfig) submitParamChangeProposal(
`-y`,
)

bz, err = cmd.CombinedOutput()
if verbose {
fmt.Println("submitLegacyTextProposal cmd:", cmd.String())
}

bz, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}

// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(action.Chain, 2, 60*time.Second)
tr.waitBlocks(action.Chain, 2, 10*time.Second)
}

type VoteGovProposalAction struct {
Expand Down Expand Up @@ -1883,6 +1845,19 @@ func (tr TestConfig) registerRepresentative(
target ExecutionTarget,
verbose bool,
) {
fileTempl := `{
"pubkey": %s,
"amount": "%s",
"moniker": "%s",
"identity": "",
"website": "",
"security": "",
"details": "",
"commission-rate": "0.1",
"commission-max-rate": "0.2",
"commission-max-change-rate": "0.01",
"min-self-delegation": "1"
}`
var wg sync.WaitGroup
for i, val := range action.Representatives {
wg.Add(1)
Expand All @@ -1900,22 +1875,46 @@ func (tr TestConfig) registerRepresentative(
log.Fatal(err, "\n", string(bzPubKey))
}

bz, err := target.ExecCommand(tr.chainConfigs[action.Chain].BinaryName,
fileContent := fmt.Sprintf(fileTempl, string(bzPubKey), fmt.Sprint(stake)+"stake", fmt.Sprint(val))
fileName := fmt.Sprintf("%s_democracy_representative.json", val)
file, err := os.CreateTemp("", fileName)
if err != nil {
panic(fmt.Sprintf("failed writing ccv consumer file : %v", err))
}
defer file.Close()
err = os.WriteFile(file.Name(), []byte(fileContent), 0600)
if err != nil {
log.Fatalf("Failed writing consumer genesis to file: %v", err)
}

containerInstance := tr.containerConfig.InstanceName
targetFile := fmt.Sprintf("/tmp/%s", fileName)
sourceFile := file.Name()
//#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments.
copyCmd := exec.Command("docker", "cp", sourceFile,
fmt.Sprintf("%s:%s", containerInstance, targetFile))
writeResult, err := copyCmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(writeResult))
}

cmd := target.ExecCommand(tr.chainConfigs[action.Chain].BinaryName,
"tx", "staking", "create-validator",
`--amount`, fmt.Sprint(stake)+"stake",
`--pubkey`, string(bzPubKey),
`--moniker`, fmt.Sprint(val),
`--commission-rate`, "0.1",
`--commission-max-rate`, "0.2",
`--commission-max-change-rate`, "0.01",
`--min-self-delegation`, "1",
targetFile,
`--from`, `validator`+fmt.Sprint(val),
`--chain-id`, string(tr.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, val),
`--node`, tr.getValidatorNode(action.Chain, val),
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()
)

if verbose {
fmt.Println("register representative cmd:", cmd.String())
fmt.Println("Tx json:", fileContent)
}

bz, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func UnmarshalMapToActionType(rawAction json.RawMessage, actionTypeString string
if err == nil {
return a, nil
}
case "main.SubmitParamChangeLegacyProposalAction":
var a SubmitParamChangeLegacyProposalAction
case "main.SubmitLegacyTextProposalAction":
var a SubmitLegacyTextProposalAction
err := json.Unmarshal(rawAction, &a)
if err == nil {
return a, nil
Expand Down
29 changes: 20 additions & 9 deletions tests/e2e/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,18 @@ func (tr TestConfig) getReward(chain ChainID, validator ValidatorID, blockHeight
}

//#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments.
bz, err := exec.Command("docker", "exec", tr.containerConfig.InstanceName, tr.chainConfigs[chain].BinaryName,

"query", "distribution", "rewards",
delAddresss,

cmd := exec.Command("docker", "exec", tr.containerConfig.InstanceName, tr.chainConfigs[chain].BinaryName,
"query", "distribution", "delegation-total-rewards",
"--delegator-address", delAddresss,
`--height`, fmt.Sprint(blockHeight),
`--node`, tr.getQueryNode(chain),
`-o`, `json`,
).CombinedOutput()
)

bz, err := cmd.CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
log.Fatal("failed getting rewards: ", err, "\n", string(bz))
}

denomCondition := `total.#(denom!="stake").amount`
Expand Down Expand Up @@ -473,14 +474,24 @@ func (tr TestConfig) getProposal(chain ChainID, proposal uint) Proposal {
StopTime: int(stopTime.Milliseconds()),
}
case "/cosmos.params.v1beta1.ParameterChangeProposal":
// deprecated for most modules
// deprecated for all modules, keeping for posterity
return ParamsProposal{
Deposit: uint(deposit),
Status: status,
Subspace: rawContent.Get("changes.0.subspace").String(),
Key: rawContent.Get("changes.0.key").String(),
Value: rawContent.Get("changes.0.value").String(),
}
case "cosmos-sdk/TextProposal":
title := rawContent.Get("title").String()
description := rawContent.Get("description").String()

return TextProposal{
Deposit: uint(deposit),
Status: status,
Title: title,
Description: description,
}
}

log.Fatal("unknown proposal type", string(bz))
Expand Down Expand Up @@ -590,7 +601,7 @@ func (tr TestConfig) getValStakedTokens(chain ChainID, validator ValidatorID) ui
log.Fatal(err, "\n", string(bz))
}

amount := gjson.Get(string(bz), `tokens`)
amount := gjson.Get(string(bz), `validator.tokens`)

return uint(amount.Uint())
}
Expand Down
44 changes: 21 additions & 23 deletions tests/e2e/steps_democracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func stepsDemocracy(consumerName string) []Step {
IsIncrementalReward: true,
IsNativeDenom: true,
},
// Check that delegating on gov-consumer does not change validator powers
ValPowers: &map[ValidatorID]uint{
ValidatorID("alice"): 511,
ValidatorID("bob"): 500,
ValidatorID("carol"): 500,
},
},
},
},
Expand Down Expand Up @@ -69,29 +75,24 @@ func stepsDemocracy(consumerName string) []Step {
},
{
// whitelisted legacy proposal can only handle ibctransfer.SendEnabled/ReceiveEnabled
Action: SubmitParamChangeLegacyProposalAction{
Chain: ChainID(consumerName),
From: ValidatorID("alice"),
Deposit: 10000001,
Subspace: "transfer",
Key: "SendEnabled",
Value: true,
Action: SubmitLegacyTextProposalAction{
Chain: ChainID(consumerName),
From: ValidatorID("alice"),
Deposit: 10000001,
},
State: State{
ChainID(consumerName): ChainState{
ValBalances: &map[ValidatorID]uint{
ValidatorID("alice"): 9889999998,
ValidatorID("bob"): 9960000001,
},
// Check that the "SendEnabled" transfer parameter is set to false
Params: &([]Param{{Subspace: "transfer", Key: "SendEnabled", Value: "false"}}),
// confirm the
Proposals: &map[uint]Proposal{
1: ParamsProposal{
Deposit: 10000001,
Status: strconv.Itoa(int(gov.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD)),
Subspace: "transfer",
Key: "SendEnabled",
Value: "true",
1: TextProposal{
Deposit: 10000001,
Status: strconv.Itoa(int(gov.ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD)),
Title: "Test Proposal",
Description: "testing",
},
},
},
Expand All @@ -114,16 +115,13 @@ func stepsDemocracy(consumerName string) []Step {
},
// Check that the prop passed
Proposals: &map[uint]Proposal{
1: ParamsProposal{
Deposit: 10000001,
Status: strconv.Itoa(int(gov.ProposalStatus_PROPOSAL_STATUS_PASSED)),
Subspace: "transfer",
Key: "SendEnabled",
Value: "true",
1: TextProposal{
Deposit: 10000001,
Status: strconv.Itoa(int(gov.ProposalStatus_PROPOSAL_STATUS_PASSED)),
Title: "Test Proposal",
Description: "testing",
},
},
// Check that the parameter is changed on gov-consumer chain
Params: &([]Param{{Subspace: "transfer", Key: "SendEnabled", Value: "true"}}),
},
},
},
Expand Down
Loading

0 comments on commit 8fdf5cf

Please sign in to comment.