Skip to content

Commit

Permalink
Lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Nov 30, 2023
1 parent 2a9c39b commit b533d1f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 151 deletions.
3 changes: 2 additions & 1 deletion app/consumer-democracy/proposals_whitelisting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

func TestDemocracyGovernanceWhitelistingKeys(t *testing.T) {
_, valUpdates, _ := testutil.CreateValidators(t, 4)
_, valUpdates, _, err := testutil.CreateValidators(4)
require.NoError(t, err)
ibctesting.DefaultTestingAppInit = icstestingutils.DemocracyConsumerAppIniter(valUpdates)
chain := ibctesting.NewTestChain(t, ibctesting.NewCoordinator(t, 0), "test")
paramKeeper := chain.App.(*appConsumer.App).ParamsKeeper
Expand Down
121 changes: 7 additions & 114 deletions tests/mbt/driver/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Expand Down Expand Up @@ -87,19 +86,10 @@ func (b *Driver) providerStakingKeeper() stakingkeeper.Keeper {
return *b.providerChain().App.(*appProvider.App).StakingKeeper
}

func (b *Driver) providerSlashingKeeper() slashingkeeper.Keeper {
return b.providerChain().App.(*appProvider.App).SlashingKeeper
}

func (b *Driver) consumerKeeper(chain ChainId) consumerkeeper.Keeper {
return b.chain(chain).App.(*appConsumer.App).ConsumerKeeper
}

// height returns the height of the current header of chain
func (s *Driver) height(chain ChainId) int64 {
return s.chain(chain).CurrentHeader.GetHeight()
}

// runningTime returns the timestamp of the current header of chain
func (s *Driver) runningTime(chain ChainId) time.Time {
testChain := s.chain(chain)
Expand All @@ -112,15 +102,6 @@ func (s *Driver) lastTime(chain ChainId) time.Time {
return testChain.LastHeader.Header.Time
}

func (s *Driver) allTimes() map[ChainId]time.Time {
chains := s.coordinator.Chains
times := make(map[ChainId]time.Time, len(chains))
for _, chain := range chains {
times[ChainId(chain.ChainID)] = chain.CurrentHeader.Time
}
return times
}

// delegator retrieves the address for the delegator account
func (s *Driver) delegator() sdk.AccAddress {
return s.providerChain().SenderAccount.GetAddress()
Expand All @@ -131,25 +112,12 @@ func (s *Driver) validator(i int64) sdk.ValAddress {
return sdk.ValAddress(s.validators[i].Address)
}

// consAddr returns the ConsAdd for the validator with id (ix) i
func (s *Driver) consAddr(i int64) sdk.ConsAddress {
return sdk.ConsAddress(s.validator(i))
}

// isJailed returns the jail status of validator with id (ix) i
func (s *Driver) isJailed(i int64) bool {
val, found := s.providerStakingKeeper().GetValidator(s.ctx(P), s.validator(i))

require.True(s.t, found, "GetValidator(%v) -> !found", s.validator(i))
return val.IsJailed()
}

// consumerPower returns the power on the consumer chain chain for
// validator with id (ix) i
func (s *Driver) consumerPower(i int64, chain ChainId) (int64, error) {
v, found := s.consumerKeeper(chain).GetCCValidator(s.ctx(chain), s.validator(i))
if !found {
return 0, fmt.Errorf("GetCCValidator(%v) -> !found", s.validator(i))
return 0, fmt.Errorf("validator %v not found", s.validator(i))
}
return v.Power, nil
}
Expand All @@ -159,41 +127,12 @@ func (s *Driver) consumerPower(i int64, chain ChainId) (int64, error) {
func (s *Driver) providerPower(i int64) (int64, error) {
v, found := s.providerStakingKeeper().GetValidator(s.ctx(P), s.validator(i))
if !found {
return 0, fmt.Errorf("Validator with id %v not found on provider!", i)
return 0, fmt.Errorf("validator with id %v not found on provider", i)
} else {
return v.BondedTokens().Int64(), nil
}
}

// delegation returns the number of delegated tokens in the delegation from
// the delegator account to the validator with id (ix) i
func (s *Driver) delegation(i int64) int64 {
d, found := s.providerStakingKeeper().GetDelegation(s.ctx(P), s.delegator(), s.validator(i))
require.True(s.t, found, "GetDelegation(%v) -> !found", s.validator(i))
return d.Shares.TruncateInt64()
}

// validatorStatus returns the validator status for validator with id (ix) i
// on the provider chain, and also whether the validator was found.
// If the validator was not found, the status is returned as Unbonded.
func (s *Driver) validatorStatus(i int64) (stakingtypes.BondStatus, bool) {
v, found := s.providerStakingKeeper().GetValidator(s.ctx(P), s.validator(i))
if !found {
return stakingtypes.Unbonded, false
}
return v.GetStatus(), true
}

// providerTokens returns the number of tokens that the validator with
// id (ix) i has delegated to it in total on the provider chain
func (s *Driver) providerTokens(i int64) int64 {
v, found := s.providerStakingKeeper().GetValidator(s.ctx(P), s.validator(i))
if !found {
return 0
}
return v.Tokens.Int64()
}

func (s *Driver) providerValidatorSet() []stakingtypes.Validator {
return s.providerStakingKeeper().GetAllValidators(s.ctx(P))
}
Expand All @@ -202,13 +141,6 @@ func (s *Driver) consumerValidatorSet(chain ChainId) []consumertypes.CrossChainV
return s.consumerKeeper(chain).GetAllCCValidator(s.ctx(chain))
}

// delegatorBalance returns the balance of the delegator account
func (s *Driver) delegatorBalance() int64 {
d := s.delegator()
bal := s.providerChain().App.(*appProvider.App).BankKeeper.GetBalance(s.ctx(P), d, sdk.DefaultBondDenom)
return bal.Amount.Int64()
}

// delegate delegates amt tokens to validator val
func (s *Driver) delegate(val, amt int64) {
providerStaking := s.providerStakingKeeper()
Expand Down Expand Up @@ -236,28 +168,6 @@ func (s *Driver) undelegate(val, amt int64) {
providerStaking.GetAllDelegations(s.ctx(P))
}

// consumerSlash simulates a slash event occurring on the consumer chain.
// It can be for a downtime or doublesign.
func (s *Driver) consumerSlash(val sdk.ConsAddress, h int64, isDowntime bool, chain ChainId) {
kind := stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN
if isDowntime {
kind = stakingtypes.Infraction_INFRACTION_DOWNTIME
}
ctx := s.ctx(chain)
before := len(ctx.EventManager().Events())
s.consumerKeeper(chain).SlashWithInfractionReason(ctx, val, h, 0, sdk.Dec{}, kind)
// consumer module emits packets on slash, so these must be collected.
evts := ctx.EventManager().Events()
packets := simibc.ParsePacketsFromEvents(evts[before:])
if len(packets) > 0 {
s.path(chain).Outboxes.AddPacket(string(chain), packets[0])
}
}

func (s *Driver) updateClient(chain ChainId) error {
return s.path(chain).UpdateClient(string(chain), false)
}

// packetQueue returns the queued packet sfrom sender to receiver,
// where either sender or receiver must be the provider.
func (s *Driver) packetQueue(sender, receiver ChainId) []simibc.Packet {
Expand All @@ -277,14 +187,6 @@ func (s *Driver) packetQueue(sender, receiver ChainId) []simibc.Packet {
}
}

func (s *Driver) getPacketsFromProviderToConsumer(consumer ChainId) []simibc.Packet {
return s.path(consumer).Outboxes.OutboxPackets[string(consumer)]
}

func (s *Driver) getPacketsFromConsumerToProvider(consumer ChainId) []simibc.Packet {
return s.path(consumer).Outboxes.OutboxPackets[P]
}

func (s *Driver) getStateString() string {
var state strings.Builder

Expand Down Expand Up @@ -466,12 +368,6 @@ func (s *Driver) setTime(chain ChainId, newTime time.Time) {
testChain.App.BeginBlock(abcitypes.RequestBeginBlock{Header: testChain.CurrentHeader})
}

func (s *Driver) setAllTimes(newTimes map[ChainId]time.Time) {
for chain, newTime := range newTimes {
s.setTime(chain, newTime)
}
}

// DeliverPacketToConsumer delivers a packet from the provider to the given consumer recipient.
// It updates the client before delivering the packet.
// Since the channel is ordered, the packet that is delivered is the first packet in the outbox.
Expand All @@ -491,26 +387,23 @@ func (s *Driver) DeliverPacketFromConsumer(sender ChainId, expectError bool) {
func (s *Driver) DeliverAcks() {
for _, chain := range s.runningConsumers() {
path := s.path(ChainId(chain.ChainId))
path.DeliverAcks(string(path.Path.EndpointA.Chain.ChainID), math.MaxInt)
path.DeliverAcks(string(path.Path.EndpointB.Chain.ChainID), math.MaxInt)
path.DeliverAcks(path.Path.EndpointA.Chain.ChainID, math.MaxInt)
path.DeliverAcks(path.Path.EndpointB.Chain.ChainID, math.MaxInt)
}
}

// stopConsumer stops a given consumer chain.
func (s *Driver) stopConsumer(chain ChainId) {
func (s *Driver) stopConsumer(chain ChainId) error {
// stop the consumer chain on the provider
s.providerKeeper().StopConsumerChain(s.providerCtx(), string(chain), true)
// delete the chain from the coordinator
delete(s.coordinator.Chains, string(chain))
// delete the path from the driver
delete(s.simibcs, chain)
return s.providerKeeper().StopConsumerChain(s.providerCtx(), string(chain), true)
}

// newDriver creates a new Driver object.
// It creates a new coordinator, but does not start any chains.
// The caller must call setupChains to start the chains and
// fully populate the Driver.
func newDriver(t *testing.T, validators []*cmttypes.Validator, valNames []string) *Driver {
t.Helper()
t.Log("Creating coordinator")
coordinator := ibctesting.NewCoordinator(t, 0) // start without chains, which we add later

Expand Down
21 changes: 14 additions & 7 deletions tests/mbt/driver/mbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestMBT(t *testing.T) {
}

func RunItfTrace(t *testing.T, path string) {
t.Helper()
t.Logf("🟡 Testing trace %s", path)

// Load trace
Expand Down Expand Up @@ -140,7 +141,8 @@ func RunItfTrace(t *testing.T, path string) {
}

// initialValSet has the right vals, but not yet the right powers
valSet, addressMap, signers := CreateValSet(t, initialValSet)
valSet, addressMap, signers, err := CreateValSet(initialValSet)
require.NoError(t, err, "Error creating validator set")

// get a slice of validators in the right order
nodes := make([]*cmttypes.Validator, len(valNames))
Expand Down Expand Up @@ -249,7 +251,10 @@ func RunItfTrace(t *testing.T, path string) {

// stop consumers
for _, consumer := range consumersToStop {
driver.providerKeeper().StopConsumerChain(driver.providerCtx(), consumer.Value.(string), true)
err := driver.stopConsumer(ChainId(consumer.Value.(string)))
if err != nil {
log.Fatalf("Error stopping consumer %v: %v", consumer, err)
}
}

// reset the times for the consumers that were not stopped or started just now
Expand All @@ -264,7 +269,7 @@ func RunItfTrace(t *testing.T, path string) {
if len(consumersToStart) > 0 && consumer.ChainId == consumersToStart[len(consumersToStart)-1].Value.(string) {
continue
}
consumerChainId := string(consumer.ChainId)
consumerChainId := consumer.ChainId

driver.path(ChainId(consumerChainId)).AddClientHeader(Provider, driver.providerHeader())
err := driver.path(ChainId(consumerChainId)).UpdateClient(consumerChainId, false)
Expand Down Expand Up @@ -363,7 +368,7 @@ func RunItfTrace(t *testing.T, path string) {
CompareValidatorSets(t, driver, currentModelState, actualRunningConsumers, index)

// check times - sanity check that the block times match the ones from the model
CompareTimes(t, driver, actualRunningConsumers, currentModelState, timeOffset)
CompareTimes(driver, actualRunningConsumers, currentModelState, timeOffset)

// check sent packets: we check that the package queues in the model and the system have the same length.
for _, consumer := range actualRunningConsumers {
Expand All @@ -380,14 +385,15 @@ func RunItfTrace(t *testing.T, path string) {
}

func CompareValidatorSets(t *testing.T, driver *Driver, currentModelState map[string]itf.Expr, consumers []string, index int) {
t.Helper()
modelValSet := ValidatorSet(currentModelState, "provider")
curValSet := driver.providerValidatorSet()

actualValSet := make(map[string]int64, len(curValSet))

for _, val := range curValSet {
valName := val.Description.Moniker
actualValSet[valName] = int64(val.Tokens.Int64())
actualValSet[valName] = val.Tokens.Int64()
}

require.NoError(t, CompareValSet(modelValSet, actualValSet), "Validator sets do not match")
Expand All @@ -411,7 +417,7 @@ func CompareValidatorSets(t *testing.T, driver *Driver, currentModelState map[st
providerVal, found := driver.providerStakingKeeper().GetValidatorByConsAddr(driver.providerCtx(), providerConsAddr.Address)
require.True(t, found, "Error getting provider validator")

consumerCurValSet[providerVal.GetMoniker()] = int64(val.Power)
consumerCurValSet[providerVal.GetMoniker()] = val.Power
}
for val, power := range modelValSet {
_ = val
Expand All @@ -432,6 +438,7 @@ func ComparePacketQueues(
consumer string,
timeOffset time.Time,
) {
t.Helper()
ComparePacketQueue(t, driver, currentModelState, Provider, consumer, timeOffset)
ComparePacketQueue(t, driver, currentModelState, consumer, Provider, timeOffset)
}
Expand All @@ -444,6 +451,7 @@ func ComparePacketQueue(
receiver string,
timeOffset time.Time,
) {
t.Helper()
modelSenderQueue := PacketQueue(currentModelState, sender, receiver)
actualSenderQueue := driver.packetQueue(ChainId(sender), ChainId(receiver))

Expand Down Expand Up @@ -477,7 +485,6 @@ func ComparePacketQueue(
// We only compare down to seconds, because the model and system will differ
// on the order of nanoseconds.
func CompareTimes(
t *testing.T,
driver *Driver,
consumers []string,
currentModelState map[string]itf.Expr,
Expand Down
Loading

0 comments on commit b533d1f

Please sign in to comment.