Skip to content

Commit

Permalink
fix: fix gas consumption issue for vote condition type
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybxyz committed Apr 14, 2022
1 parent dc3f40c commit 1afc2b6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
15 changes: 11 additions & 4 deletions x/claim/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ func (k Keeper) ValidateCondition(ctx sdk.Context, recipient sdk.AccAddress, ct

switch ct {
case types.ConditionTypeDeposit:
// Expect this condition to be executed with multiple messages (MsgDeposit + MsgClaim)
// in a single transaction because deposit request gets deleted at the begin blocker
if len(k.liquidityKeeper.GetDepositRequestsByDepositor(ctx, recipient)) != 0 {
ok = true
}

case types.ConditionTypeSwap:
// Expect this condition to be executed with multiple messages (MsgLimitOrder or MsgMarketOrder + MsgClaim)
// in a single transaction because order request gets deleted at the begin blocker after order lifespan
if len(k.liquidityKeeper.GetOrdersByOrderer(ctx, recipient)) != 0 {
ok = true
}
Expand All @@ -84,10 +88,13 @@ func (k Keeper) ValidateCondition(ctx sdk.Context, recipient sdk.AccAddress, ct
}

case types.ConditionTypeVote:
k.govKeeper.IterateAllVotes(ctx, func(vote govtypes.Vote) (stop bool) {
if vote.Voter == recipient.String() {
ok = true
return true
k.govKeeper.IterateProposals(ctx, func(proposal govtypes.Proposal) (stop bool) {
if proposal.Status == govtypes.StatusVotingPeriod {
_, found := k.govKeeper.GetVote(ctx, proposal.ProposalId, recipient)
if found {
ok = true
return true
}
}
return false
})
Expand Down
61 changes: 61 additions & 0 deletions x/claim/keeper/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,64 @@ func (s *KeeperTestSuite) TestClaim_Partial_TerminatAirdrop() {
feePool := s.app.DistrKeeper.GetFeePool(s.ctx)
s.Require().False(feePool.CommunityPool.IsZero())
}

func (s *KeeperTestSuite) TestSimulateGasUsage_VoteCondition() {
// Create an airdrop
sourceAddr := s.addr(0)
airdrop := s.createAirdrop(
1,
sourceAddr,
utils.ParseCoins("100000000000denom1"),
[]types.ConditionType{
types.ConditionTypeDeposit,
types.ConditionTypeSwap,
types.ConditionTypeLiquidStake,
types.ConditionTypeVote,
},
s.ctx.BlockTime(),
s.ctx.BlockTime().AddDate(0, 6, 0),
true,
)

// Submit governance proposals
s.createTextProposal(sourceAddr, "Text1", "Description")
s.createTextProposal(sourceAddr, "Text2", "Description")

recipients := []sdk.AccAddress{}
numRecipients := 10000

// Claim records for all recipients
for i := 1; i <= numRecipients; i++ {
recipient := s.addr(i)
recipients = append(recipients, recipient)

s.createClaimRecord(
airdrop.Id,
recipient,
utils.ParseCoins("1000000denom1"),
utils.ParseCoins("1000000denom1"),
[]types.ConditionType{},
)

_, found := s.keeper.GetClaimRecordByRecipient(s.ctx, airdrop.Id, recipient)
s.Require().True(found)
}

for _, recipient := range recipients[:5000] {
s.vote(recipient, 1, govtypes.OptionYes)
}

// Vote proposal and claim condition
for i, recipient := range recipients[5000:] {
gasConsumedBefore := s.ctx.GasMeter().GasConsumed()

s.vote(recipient, 2, govtypes.OptionYes)

_, err := s.keeper.Claim(s.ctx, types.NewMsgClaim(airdrop.Id, recipient, types.ConditionTypeVote))
s.Require().NoError(err)

gasConsumed := s.ctx.GasMeter().GasConsumed()
gasConsumed = gasConsumed - gasConsumedBefore
s.T().Logf("[%d] GasConsumed: %d\n", i+1, gasConsumed)
}
}
3 changes: 2 additions & 1 deletion x/claim/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type DistrKeeper interface {
}

type GovKeeper interface {
IterateAllVotes(ctx sdk.Context, cb func(vote govtypes.Vote) (stop bool))
IterateProposals(ctx sdk.Context, cb func(proposal govtypes.Proposal) (stop bool))
GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) (vote govtypes.Vote, found bool)
}

// LiquidityKeeper defines the expected interface needed to check the condition.
Expand Down

0 comments on commit 1afc2b6

Please sign in to comment.