Skip to content

Commit

Permalink
Democracy chain integration tests, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Sep 9, 2022
1 parent 96905b7 commit 4a66fb5
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 27 deletions.
36 changes: 29 additions & 7 deletions tests/integration/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os/exec"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -704,6 +705,27 @@ func (tr TestRun) relayPackets(
}
}

type relayRewardPacketsToProviderAction struct {
consumerChain chainID
providerChain chainID
port string
channel uint
}

func (tr TestRun) relayRewardPacketsToProvider(
action relayRewardPacketsToProviderAction,
verbose bool,
) {
blockPerDistribution, _ := strconv.ParseUint(strings.Trim(tr.getParam(action.consumerChain, Param{Subspace: "ccvconsumer", Key: "BlocksPerDistributionTransmission"}), "\""), 10, 64)
currentBlock := uint64(tr.getBlockHeight(action.consumerChain))
if currentBlock <= blockPerDistribution {
tr.waitBlocks(action.consumerChain, uint(blockPerDistribution-currentBlock+1), 60*time.Second)
}

tr.relayPackets(relayPacketsAction{chain: action.consumerChain, port: action.port, channel: action.channel}, verbose)
tr.waitBlocks(action.providerChain, 1, 10*time.Second)
}

type delegateTokensAction struct {
chain chainID
from validatorID
Expand Down Expand Up @@ -896,18 +918,18 @@ func (tr TestRun) unjailValidator(action unjailValidatorAction, verbose bool) {
}
}

type registerRepresentAction struct {
chain chainID
represents []validatorID
stakes []uint
type registerRepresentativeAction struct {
chain chainID
representatives []validatorID
stakes []uint
}

func (tr TestRun) registerRepresent(
action registerRepresentAction,
func (tr TestRun) registerRepresentative(
action registerRepresentativeAction,
verbose bool,
) {
var wg sync.WaitGroup
for i, val := range action.represents {
for i, val := range action.representatives {
wg.Add(1)
stake := action.stakes[i]
go func(val validatorID, stake uint) {
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (tr TestRun) runStep(step Step, verbose bool) {
tr.transferChannelComplete(action, verbose)
case relayPacketsAction:
tr.relayPackets(action, verbose)
case relayRewardPacketsToProviderAction:
tr.relayRewardPacketsToProvider(action, verbose)
case delegateTokensAction:
tr.delegateTokens(action, verbose)
case unbondTokensAction:
Expand All @@ -75,8 +77,8 @@ func (tr TestRun) runStep(step Step, verbose bool) {
tr.invokeDowntimeSlash(action, verbose)
case unjailValidatorAction:
tr.unjailValidator(action, verbose)
case registerRepresentAction:
tr.registerRepresent(action, verbose)
case registerRepresentativeAction:
tr.registerRepresentative(action, verbose)
default:
log.Fatalf(fmt.Sprintf(`unknown action: %#v`, action))
}
Expand Down
80 changes: 69 additions & 11 deletions tests/integration/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
type State map[chainID]ChainState

type ChainState struct {
ValBalances *map[validatorID]uint
Proposals *map[uint]Proposal
ValPowers *map[validatorID]uint
RepresentPowers *map[validatorID]uint
Params *[]Param
ValBalances *map[validatorID]uint
Proposals *map[uint]Proposal
ValPowers *map[validatorID]uint
RepresentativePowers *map[validatorID]uint
Params *[]Param
Rewards *Rewards
}

type Proposal interface {
Expand All @@ -44,6 +45,16 @@ type ConsumerProposal struct {
Status string
}

type Rewards struct {
IsRewarded map[validatorID]bool
//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
}

func (p ConsumerProposal) isProposal() {}

type ParamsProposal struct {
Expand Down Expand Up @@ -90,16 +101,21 @@ func (tr TestRun) getChainState(chain chainID, modelState ChainState) ChainState
chainState.ValPowers = &powers
}

if modelState.RepresentPowers != nil {
representPowers := tr.getRepresentPowers(chain, *modelState.RepresentPowers)
chainState.RepresentPowers = &representPowers
if modelState.RepresentativePowers != nil {
representPowers := tr.getRepresentativePowers(chain, *modelState.RepresentativePowers)
chainState.RepresentativePowers = &representPowers
}

if modelState.Params != nil {
params := tr.getParams(chain, *modelState.Params)
chainState.Params = &params
}

if modelState.Rewards != nil {
rewards := tr.getRewards(chain, *modelState.Rewards)
chainState.Rewards = &rewards
}

return chainState
}

Expand Down Expand Up @@ -169,10 +185,10 @@ func (tr TestRun) getValPowers(chain chainID, modelState map[validatorID]uint) m
return actualState
}

func (tr TestRun) getRepresentPowers(chain chainID, modelState map[validatorID]uint) map[validatorID]uint {
func (tr TestRun) getRepresentativePowers(chain chainID, modelState map[validatorID]uint) map[validatorID]uint {
actualState := map[validatorID]uint{}
for k := range modelState {
actualState[k] = tr.getRepresentPower(chain, k)
actualState[k] = tr.getRepresentativePower(chain, k)
}

return actualState
Expand All @@ -187,6 +203,48 @@ func (tr TestRun) getParams(chain chainID, modelState []Param) []Param {
return actualState
}

func (tr TestRun) getRewards(chain chainID, modelState Rewards) Rewards {
receivedRewards := map[validatorID]bool{}

currentBlock := tr.getBlockHeight(chain)
tr.waitBlocks(chain, 1, 10*time.Second)
nextBlock := tr.getBlockHeight(chain)
tr.waitBlocks(chain, 1, 10*time.Second)

if !modelState.IsIncrementalReward {
currentBlock = 1
}
for k := range modelState.IsRewarded {
receivedRewards[k] = tr.getReward(chain, k, nextBlock, modelState.IsNativeDenom) > tr.getReward(chain, k, currentBlock, modelState.IsNativeDenom)
}

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

func (tr TestRun) getReward(chain chainID, validator validatorID, blockHeight uint, isNativeDenom bool) float64 {
//#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",
tr.validatorConfigs[validator].delAddress,

`--height`, fmt.Sprint(blockHeight),
`--node`, tr.getValidatorNode(chain, tr.getDefaultValidator(chain)),
`-o`, `json`,
).CombinedOutput()

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

denomCondition := `total.#(denom!="stake").amount`
if isNativeDenom {
denomCondition = `total.#(denom=="stake").amount`
}

return gjson.Get(string(bz), denomCondition).Float()
}

func (tr TestRun) getBalance(chain chainID, validator validatorID) uint {
//#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments.
bz, err := exec.Command("docker", "exec", tr.containerConfig.instanceName, tr.chainConfigs[chain].binaryName,
Expand Down Expand Up @@ -341,7 +399,7 @@ func (tr TestRun) getValPower(chain chainID, validator validatorID) uint {
return 0
}

func (tr TestRun) getRepresentPower(chain chainID, validator validatorID) uint {
func (tr TestRun) getRepresentativePower(chain chainID, validator validatorID) uint {
//#nosec G204 -- Bypass linter warning for spawning subprocess with cmd arguments.
bz, err := exec.Command("docker", "exec", tr.containerConfig.instanceName, tr.chainConfigs[chain].binaryName,

Expand Down
Loading

0 comments on commit 4a66fb5

Please sign in to comment.