-
Notifications
You must be signed in to change notification settings - Fork 585
/
utils.go
61 lines (49 loc) · 2.3 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ibctesting
import (
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
abci "github.com/cometbft/cometbft/abci/types"
tmtypes "github.com/cometbft/cometbft/types"
)
// ApplyValSetChanges takes in tmtypes.ValidatorSet and []abci.ValidatorUpdate and will return a new tmtypes.ValidatorSet which has the
// provided validator updates applied to the provided validator set.
func ApplyValSetChanges(tb testing.TB, valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) *tmtypes.ValidatorSet {
tb.Helper()
updates, err := tmtypes.PB2TM.ValidatorUpdates(valUpdates)
require.NoError(tb, err)
// must copy since validator set will mutate with UpdateWithChangeSet
newVals := valSet.Copy()
err = newVals.UpdateWithChangeSet(updates)
require.NoError(tb, err)
return newVals
}
// VoteAndCheckProposalStatus votes on a gov proposal, checks if the proposal has passed, and returns an error if it has not with the failure reason.
func VoteAndCheckProposalStatus(endpoint *Endpoint, proposalID uint64) error {
// vote on proposal
ctx := endpoint.Chain.GetContext()
require.NoError(endpoint.Chain.TB, endpoint.Chain.GetSimApp().GovKeeper.AddVote(ctx, proposalID, endpoint.Chain.SenderAccount.GetAddress(), govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), ""))
// fast forward the chain context to end the voting period
params, err := endpoint.Chain.GetSimApp().GovKeeper.Params.Get(ctx)
require.NoError(endpoint.Chain.TB, err)
endpoint.Chain.Coordinator.IncrementTimeBy(*params.VotingPeriod + *params.MaxDepositPeriod)
endpoint.Chain.NextBlock()
// check if proposal passed or failed on msg execution
// we need to grab the context again since the previous context is no longer valid as the chain header time has been incremented
p, err := endpoint.Chain.GetSimApp().GovKeeper.Proposals.Get(endpoint.Chain.GetContext(), proposalID)
require.NoError(endpoint.Chain.TB, err)
if p.Status != govtypesv1.StatusPassed {
return fmt.Errorf("proposal failed: %s", p.FailedReason)
}
return nil
}
// GenerateString generates a random string of the given length in bytes
func GenerateString(length uint) string {
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = charset[rand.Intn(len(charset))]
}
return string(bytes)
}