Skip to content

Commit

Permalink
refactor consumer reward tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Sep 25, 2024
1 parent 197bb3c commit c2413b4
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 66 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/action_rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func CreateSubmitChangeRewardDenomsProposalActionGen() *rapid.Generator[SubmitCh
Chain: GetChainIDGen().Draw(t, "Chain"),
From: GetValidatorIDGen().Draw(t, "From"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
Denom: rapid.String().Draw(t, "Denom"),
Denoms: rapid.SliceOf(rapid.String()).Draw(t, "Denoms"),
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2536,14 +2536,14 @@ func (tr Chain) registerRepresentative(

type SubmitChangeRewardDenomsProposalAction struct {
Chain ChainID
Denom string
Denoms []string
Deposit uint
From ValidatorID
}

func (tr Chain) submitChangeRewardDenomsProposal(action SubmitChangeRewardDenomsProposalAction, verbose bool) {
changeRewMsg := types.MsgChangeRewardDenoms{
DenomsToAdd: []string{action.Denom},
DenomsToAdd: action.Denoms,
DenomsToRemove: []string{"stake"},
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
}
Expand Down Expand Up @@ -2604,7 +2604,7 @@ func (tr Chain) submitChangeRewardDenomsLegacyProposal(action SubmitChangeReward
ChangeRewardDenomsProposal: types.ChangeRewardDenomsProposal{
Title: "Change reward denoms",
Description: "Change reward denoms",
DenomsToAdd: []string{action.Denom},
DenomsToAdd: action.Denoms,
DenomsToRemove: []string{"stake"},
},
Deposit: fmt.Sprint(action.Deposit) + `stake`,
Expand Down Expand Up @@ -3378,7 +3378,7 @@ func (tr Chain) transferIbcToken(
fmt.Sprint(action.Amount)+`stake`,
action.Memo,
action.From,
string(tr.testConfig.chainConfigs[action.Chain].ConsumerId),
string(tr.testConfig.chainConfigs[action.Chain].ChainId),
tr.getValidatorHome(action.Chain, action.From),
tr.getValidatorNode(action.Chain, action.From),
gas,
Expand Down
11 changes: 3 additions & 8 deletions tests/e2e/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ func (tr Chain) GetRewards(chain ChainID, modelState Rewards) Rewards {
currentBlock = 1
}
for k := range modelState.IsRewarded {
receivedRewards[k] = tr.target.GetReward(chain, k, nextBlock, modelState.IsNativeDenom) > tr.target.GetReward(chain, k, currentBlock, modelState.IsNativeDenom)
receivedRewards[k] = tr.target.GetReward(chain, k, nextBlock, modelState.Denom) > tr.target.GetReward(chain, k, currentBlock, modelState.Denom)
}

return Rewards{IsRewarded: receivedRewards, IsIncrementalReward: modelState.IsIncrementalReward, IsNativeDenom: modelState.IsNativeDenom}
return Rewards{IsRewarded: receivedRewards, IsIncrementalReward: modelState.IsIncrementalReward, Denom: modelState.Denom}
}

func (tr Chain) GetConsumerAddresses(chain ChainID, modelState map[ValidatorID]string) map[ValidatorID]string {
Expand Down Expand Up @@ -253,7 +253,7 @@ func (tr Commands) GetBlockHeight(chain ChainID) uint {
return uint(blockHeight)
}

func (tr Commands) GetReward(chain ChainID, validator ValidatorID, blockHeight uint, isNativeDenom bool) float64 {
func (tr Commands) GetReward(chain ChainID, validator ValidatorID, blockHeight uint, denom string) float64 {
valCfg := tr.validatorConfigs[validator]
delAddresss := valCfg.DelAddress
if chain != ChainID("provi") {
Expand Down Expand Up @@ -286,11 +286,6 @@ func (tr Commands) GetReward(chain ChainID, validator ValidatorID, blockHeight u
log.Fatal("failed getting rewards: ", err, "\n", string(bz))
}

denom := "stake"
if !isNativeDenom {
denom = "ibc"
}

denomCondition := fmt.Sprintf(`total.#(%%"*%s*")`, denom)
amount := strings.Split(gjson.Get(string(bz), denomCondition).String(), denom)[0]

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/state_rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func GetRewardsGen() *rapid.Generator[Rewards] {
return rapid.Custom(func(t *rapid.T) Rewards {
return Rewards{
IsIncrementalReward: rapid.Bool().Draw(t, "IsIncrementalReward"),
IsNativeDenom: rapid.Bool().Draw(t, "IsNativeDenom"),
IsRewarded: rapid.MapOf(GetValidatorIDGen(), rapid.Bool()).Draw(t, "IsRewarded"),
// Denom: rapid.Str,
IsRewarded: rapid.MapOf(GetValidatorIDGen(), rapid.Bool()).Draw(t, "IsRewarded"),
}
})
}
Expand Down
86 changes: 61 additions & 25 deletions tests/e2e/steps_democracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

const consumerRewardDenom = "ibc/3C3D7B3BE4ECC85A0E5B52A3AEC3B7DFC2AA9CA47C37821E57020D6807043BE9"
var consumerRewardDenoms = []string{
"ibc/3C3D7B3BE4ECC85A0E5B52A3AEC3B7DFC2AA9CA47C37821E57020D6807043BE9", // transfer channel-1
"ibc/D549749C93524DA1831A4B3C850DFC1BA9060261BEDFB224B3B0B4744CD77A70", // transfer channel-2
}

func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool) []Step {
return []Step{
Expand All @@ -25,7 +28,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
ValidatorID("carol"): false,
},
IsIncrementalReward: true,
IsNativeDenom: true,
Denom: "stake",
},
// Check that delegating on gov-consumer does not change validator powers
ValPowers: &map[ValidatorID]uint{
Expand Down Expand Up @@ -64,7 +67,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
ValidatorID("carol"): true,
},
IsIncrementalReward: true,
IsNativeDenom: true,
Denom: "stake",
},
},
},
Expand Down Expand Up @@ -139,7 +142,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
ValidatorID("carol"): false,
},
IsIncrementalReward: false,
IsNativeDenom: false,
Denom: consumerRewardDenoms[0],
},
// Check that the denom is not registered on provider chain
RegisteredConsumerRewardDenoms: &[]string{},
Expand All @@ -149,7 +152,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
{
Action: SubmitChangeRewardDenomsProposalAction{
Chain: ChainID("provi"),
Denom: consumerRewardDenom,
Denoms: consumerRewardDenoms,
Deposit: 10000001,
From: ValidatorID("bob"),
},
Expand All @@ -170,13 +173,35 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
State: State{
ChainID("provi"): ChainState{
// Check that the denom is registered on provider chain
RegisteredConsumerRewardDenoms: &[]string{consumerRewardDenom},
RegisteredConsumerRewardDenoms: &consumerRewardDenoms,
},
},
},
// Relay pending consumer rewards sent via the transfer channel-1
{
Action: RelayRewardPacketsToProviderAction{
ConsumerChain: ChainID(consumerName),
ProviderChain: ChainID("provi"),
Port: "transfer",
Channel: 1,
},
State: State{
ChainID("provi"): ChainState{
Rewards: &Rewards{
// expectRegisteredRewardDistribution == true
// expect rewards to be distributed since IBC denoms are registered
// and transfer channel-1 is associated to the consumer id
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): expectRegisteredRewardDistribution,
ValidatorID("bob"): expectRegisteredRewardDistribution,
ValidatorID("carol"): expectRegisteredRewardDistribution,
},
IsIncrementalReward: false,
Denom: consumerRewardDenoms[0],
},
},
},
},
// Check that consumer rewards are only distributed
// if they come from a consumer client associated with a consumer id.
//
// Create a second consumer client on the provider
{
Action: CreateIbcClientAction{
Expand All @@ -185,7 +210,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
},
State: State{},
},
// Create a second IBC connection between the 2nd consumer client
// Create a new IBC connection between the 2nd consumer client
// and the existing provider client on the consumer
{
Action: AddIbcConnectionAction{
Expand All @@ -196,7 +221,7 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
},
State: State{},
},
// Create a second IBC transfer channel
// Create IBC transfer channel-2
{
Action: AddIbcChannelAction{
ChainA: ChainID("provi"),
Expand All @@ -210,20 +235,20 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
State: State{},
},
// Transfer tokens from the consumer to the consumer reward pool
// of the provider via the 2nd transfer channel
// of the provider via the transfer channel-2
{
Action: TransferIbcTokenAction{
Chain: ChainID(consumerName),
From: ValidatorID("carol"),
DstAddr: "cosmos1ap0mh6xzfn8943urr84q6ae7zfnar48am2erhd", // consumer reward pool address
Amount: 1000000,
Channel: 2, // new transfer channel
Memo: "consumer chain rewards distribution", // use old memo
Channel: 2,
Memo: "consumer chain rewards distribution", // no consumer Id in memo
},
State: State{},
},
// Relay the transfer packet from the second transfer
// channel and check that tokens are not distributed
// Relay the transfer packets from channel-2
// and check that tokens are not distributed
// since the packet isn't associated to a consumer id
{
Action: RelayRewardPacketsToProviderAction{
Expand All @@ -234,40 +259,51 @@ func stepsDemocracy(consumerName string, expectRegisteredRewardDistribution bool
},
State: State{
ChainID("provi"): ChainState{

Rewards: &Rewards{
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): false,
ValidatorID("bob"): false,
ValidatorID("carol"): false,
},
IsIncrementalReward: false,
IsNativeDenom: false,
IsIncrementalReward: true,
Denom: "stake",
},
},
},
},
// Relay pending consumer rewards sent via the 1st transfer channel
// and check that they are distributed to the validators.
// Transfer tokens from the consumer to the consumer reward pool
// of the provider via the transfer channel-2 using the correct memo
// to identify the consumer
{
Action: TransferIbcTokenAction{
Chain: ChainID(consumerName),
From: ValidatorID("carol"),
DstAddr: "cosmos1ap0mh6xzfn8943urr84q6ae7zfnar48am2erhd", // consumer reward pool address
Amount: 1000000,
Channel: 2,
Memo: `{"provider":{"consumerId":"0","chainId":"democ","memo":"ICS rewards"}}`,
},
State: State{},
},
// Relay the transfer packets from channel-2
// and check that tokens are distributed
{
Action: RelayRewardPacketsToProviderAction{
ConsumerChain: ChainID(consumerName),
ProviderChain: ChainID("provi"),
Port: "transfer",
Channel: 1,
Channel: 2,
},
State: State{
ChainID("provi"): ChainState{
Rewards: &Rewards{
// expectRegisteredRewardDistribution == true
// expect "provi" validators to get rewards from "democ"
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): expectRegisteredRewardDistribution,
ValidatorID("bob"): expectRegisteredRewardDistribution,
ValidatorID("carol"): expectRegisteredRewardDistribution,
},
IsIncrementalReward: false,
IsNativeDenom: false,
Denom: consumerRewardDenoms[1],
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions tests/e2e/steps_inactive_vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func stepsInactiveProviderValidators() []Step {
ValidatorID("carol"): 300000000,
},
Rewards: &Rewards{
IsNativeDenom: true, // check for rewards in the provider denom
IsIncrementalReward: true, // we need to get incremental rewards
Denom: "stake", // check for rewards in the provider denom
IsIncrementalReward: true, // we need to get incremental rewards
// if we would look at total rewards, alice would trivially also get rewards,
// because she gets rewards in the first block due to being in the genesis
IsRewarded: map[ValidatorID]bool{
Expand Down Expand Up @@ -100,8 +100,8 @@ func stepsInactiveProviderValidators() []Step {
},
// check that bob and carol get rewards, but alice does not
Rewards: &Rewards{
IsNativeDenom: true, // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards since block 1
Denom: "stake", // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards since block 1
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): false,
ValidatorID("bob"): true,
Expand Down Expand Up @@ -143,8 +143,8 @@ func stepsInactiveProviderValidators() []Step {
State: State{
ChainID("provi"): ChainState{
Rewards: &Rewards{
IsNativeDenom: true, // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards for currently produced blocks only
Denom: "stake", // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards for currently produced blocks only
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): true, // alice is participating right now, so gets rewards
ValidatorID("bob"): false, // bob does not get rewards since he is not participating in consensus
Expand Down Expand Up @@ -176,8 +176,8 @@ func stepsInactiveProviderValidators() []Step {
},
// check that between two blocks now, alice does not get rewarded with the native denom
Rewards: &Rewards{
IsNativeDenom: true, // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards for currently produced blocks only
Denom: "stake", // check for rewards in the provider denom
IsIncrementalReward: true, // check rewards for currently produced blocks only
IsRewarded: map[ValidatorID]bool{
ValidatorID("alice"): false,
ValidatorID("bob"): true,
Expand Down Expand Up @@ -787,8 +787,8 @@ func stepsInactiveValsWithTopN() []Step {
ValidatorID("carol"): 300000000,
},
Rewards: &Rewards{
IsNativeDenom: true, // check for rewards in the provider denom
IsIncrementalReward: true, // we need to get incremental rewards
Denom: "stake", // check for rewards in the provider denom
IsIncrementalReward: true, // we need to get incremental rewards
// if we would look at total rewards, alice would trivially also get rewards,
// because she gets rewards in the first block due to being in the genesis
IsRewarded: map[ValidatorID]bool{
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (tr *TestRunner) Run() error {
}

tr.result.Passed()
// err = tr.teardownEnvironment()
err = tr.teardownEnvironment()
fmt.Printf("==========================================\n")
return err
}
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/testlib/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ChainCommands interface {
GetProposal(chain ChainID, proposal uint) Proposal
GetParam(chain ChainID, param Param) string
GetProviderAddressFromConsumer(consumerChain ChainID, validator ValidatorID) string
GetReward(chain ChainID, validator ValidatorID, blockHeight uint, isNativeDenom bool) float64
GetReward(chain ChainID, validator ValidatorID, blockHeight uint, denom string) float64
GetRegisteredConsumerRewardDenoms(chain ChainID) []string
GetSlashMeter() int64
GetPendingPacketQueueSize(chain ChainID) uint
Expand Down Expand Up @@ -339,9 +339,9 @@ type Rewards struct {
// if true it will calculate if the validator/delegator is rewarded between 2 successive blocks,
// otherwise it will calculate if it received any rewards since the 1st block
IsIncrementalReward bool
// if true checks rewards for "stake" token, otherwise checks rewards from
// other chains (e.g. false is used to check if provider received rewards from a consumer chain)
IsNativeDenom bool
// The reward denom to be checked. This can be either the native "stake" denom or
// a denom from other chains (e.g. if provider received rewards from a consumer chain)
Denom string
}

type ParamsProposal struct {
Expand Down
Loading

0 comments on commit c2413b4

Please sign in to comment.