-
Notifications
You must be signed in to change notification settings - Fork 74
/
registered_query.go
38 lines (31 loc) · 1.27 KB
/
registered_query.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
package types
import (
"fmt"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func (q *RegisteredQuery) GetOwnerAddress() (creator sdk.AccAddress, err error) {
creator, err = sdk.AccAddressFromBech32(q.Owner)
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to decode owner address: %s", q.Owner)
}
return creator, nil
}
// ValidateRemoval checks whether the caller is authorized to remove the query in current
// circumstances. Valid cases are:
// 1. owner removes query at any time;
// 2. anyone removes query if there's been q.SubmitTimeout blocks since last result submission
// height and query registration height.
func (q *RegisteredQuery) ValidateRemoval(ctx sdk.Context, caller string) error {
if q.GetOwner() == caller {
return nil // query owner is authorized to remove their queries at any time
}
registrationTimeoutBlock := q.RegisteredAtHeight + q.SubmitTimeout
submitTimeoutBlock := q.LastSubmittedResultLocalHeight + q.SubmitTimeout
currentBlock := uint64(ctx.BlockHeader().Height)
if currentBlock <= registrationTimeoutBlock || currentBlock <= submitTimeoutBlock {
return fmt.Errorf("only owner can remove a query within its service period")
}
return nil
}