Skip to content

Commit

Permalink
Add TryRejectVotedAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Abliazimov committed Sep 6, 2024
1 parent 31d05da commit b46916f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
23 changes: 11 additions & 12 deletions warden/x/act/keeper/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/warden-protocol/wardenprotocol/shield"
"github.com/warden-protocol/wardenprotocol/shield/ast"
"github.com/warden-protocol/wardenprotocol/shield/object"
"github.com/warden-protocol/wardenprotocol/warden/x/act/cosmoshield"
Expand Down Expand Up @@ -58,17 +57,8 @@ func (votes ActionRejectedVotesEnv) Get(name string) (object.Object, bool) {
// TryExecuteVotedAction checks if the action's expression is satisfied and stores the
// result in the database.
func (k Keeper) TryExecuteVotedAction(ctx context.Context, act *types.Action) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
approved, err := act.Rule.Eval(ctx, ActionApprovedVotesEnv(act.Votes))

evaluateVotes := func(env shield.Environment) (bool, error) {
ready, err := act.Rule.Eval(ctx, env)
if err != nil {
return false, fmt.Errorf("failed to evaluate votes: %w", err)
}
return ready, nil
}

approved, err := evaluateVotes(ActionApprovedVotesEnv(act.Votes))
if err != nil {
return err
}
Expand All @@ -77,7 +67,16 @@ func (k Keeper) TryExecuteVotedAction(ctx context.Context, act *types.Action) er
return k.executeAction(ctx, act)
}

rejected, err := evaluateVotes(ActionRejectedVotesEnv(act.Votes))
return nil
}

// TryRejectVotedAction checks if the action's reject expression is satisfied and updates its
// status revoked.
func (k Keeper) TryRejectVotedAction(ctx context.Context, act *types.Action) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)

rejected, err := act.Rule.Eval(ctx, ActionRejectedVotesEnv(act.Votes))

if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions warden/x/act/keeper/msg_server_vote_for_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ func (k msgServer) VoteForAction(goCtx context.Context, msg *types.MsgVoteForAct
return nil, err
}

if err := k.TryExecuteVotedAction(ctx, &act); err != nil {
return nil, err
if msg.VoteType == types.ActionVoteType_VOTE_TYPE_APPROVED {
if err := k.TryExecuteVotedAction(ctx, &act); err != nil {
return nil, err
}
} else if msg.VoteType == types.ActionVoteType_VOTE_TYPE_REJECTED {
if err := k.TryRejectVotedAction(ctx, &act); err != nil {
return nil, err
}
}

return &types.MsgVoteForActionResponse{Status: act.Status.String()}, nil
Expand Down

0 comments on commit b46916f

Please sign in to comment.