/
proposal_handler_router.go
87 lines (72 loc) · 2.58 KB
/
proposal_handler_router.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package keeper
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fastock/fastock-chain/x/gov/types"
)
var (
_ ProposalHandlerRouter = (*proposalHandlerRouter)(nil)
)
// ProposalHandlerRouter defines the interface of managing the proposal handler of proposal
type ProposalHandlerRouter interface {
AddRoute(r string, mp ProposalHandler) (mpr ProposalHandlerRouter)
HasRoute(r string) bool
GetRoute(path string) (h ProposalHandler)
Seal()
}
// ProposalHandler defines the interface handler in different periods of proposal
type ProposalHandler interface {
GetMinDeposit(ctx sdk.Context, content types.Content) sdk.SysCoins
GetMaxDepositPeriod(ctx sdk.Context, content types.Content) time.Duration
GetVotingPeriod(ctx sdk.Context, content types.Content) time.Duration
CheckMsgSubmitProposal(ctx sdk.Context, msg types.MsgSubmitProposal) sdk.Error
AfterSubmitProposalHandler(ctx sdk.Context, proposal types.Proposal)
VoteHandler(ctx sdk.Context, proposal types.Proposal, vote types.Vote) (string, sdk.Error)
AfterDepositPeriodPassed(ctx sdk.Context, proposal types.Proposal)
RejectedHandler(ctx sdk.Context, content types.Content)
}
type proposalHandlerRouter struct {
routes map[string]ProposalHandler
sealed bool
}
// nolint
func NewProposalHandlerRouter() ProposalHandlerRouter {
return &proposalHandlerRouter{
routes: make(map[string]ProposalHandler),
}
}
// Seal seals the router which prohibits any subsequent route handlers to be
// added. Seal will panic if called more than once.
func (phr *proposalHandlerRouter) Seal() {
if phr.sealed {
panic("router already sealed")
}
phr.sealed = true
}
// AddRoute adds a governance handler for a given path. It returns the Router
// so AddRoute calls can be linked. It will panic if the router is sealed.
func (phr *proposalHandlerRouter) AddRoute(path string, mp ProposalHandler) ProposalHandlerRouter {
if phr.sealed {
panic("router sealed; cannot add route handler")
}
if !isAlphaNumeric(path) {
panic("route expressions can only contain alphanumeric characters")
}
if phr.HasRoute(path) {
panic(fmt.Sprintf("route %s has already been initialized", path))
}
phr.routes[path] = mp
return phr
}
// HasRoute returns true if the router has a path registered or false otherwise.
func (phr *proposalHandlerRouter) HasRoute(path string) bool {
return phr.routes[path] != nil
}
// GetRoute returns a Handler for a given path.
func (phr *proposalHandlerRouter) GetRoute(path string) ProposalHandler {
if !phr.HasRoute(path) {
panic(fmt.Sprintf("route \"%s\" does not exist", path))
}
return phr.routes[path]
}