github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/gov/keeper/proposal_handler_router.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  
     9  	"github.com/fibonacci-chain/fbc/x/gov/types"
    10  )
    11  
    12  var (
    13  	_ ProposalHandlerRouter = (*proposalHandlerRouter)(nil)
    14  )
    15  
    16  // ProposalHandlerRouter defines the interface of managing the proposal handler of proposal
    17  type ProposalHandlerRouter interface {
    18  	AddRoute(r string, mp ProposalHandler) (mpr ProposalHandlerRouter)
    19  	HasRoute(r string) bool
    20  	GetRoute(path string) (h ProposalHandler)
    21  	Seal()
    22  }
    23  
    24  // ProposalHandler defines the interface handler in different periods of proposal
    25  type ProposalHandler interface {
    26  	GetMinDeposit(ctx sdk.Context, content types.Content) sdk.SysCoins
    27  	GetMaxDepositPeriod(ctx sdk.Context, content types.Content) time.Duration
    28  	GetVotingPeriod(ctx sdk.Context, content types.Content) time.Duration
    29  	CheckMsgSubmitProposal(ctx sdk.Context, msg types.MsgSubmitProposal) sdk.Error
    30  	AfterSubmitProposalHandler(ctx sdk.Context, proposal types.Proposal)
    31  	VoteHandler(ctx sdk.Context, proposal types.Proposal, vote types.Vote) (string, sdk.Error)
    32  	AfterDepositPeriodPassed(ctx sdk.Context, proposal types.Proposal)
    33  	RejectedHandler(ctx sdk.Context, content types.Content)
    34  }
    35  
    36  type proposalHandlerRouter struct {
    37  	routes map[string]ProposalHandler
    38  	sealed bool
    39  }
    40  
    41  // nolint
    42  func NewProposalHandlerRouter() ProposalHandlerRouter {
    43  	return &proposalHandlerRouter{
    44  		routes: make(map[string]ProposalHandler),
    45  	}
    46  }
    47  
    48  // Seal seals the router which prohibits any subsequent route handlers to be
    49  // added. Seal will panic if called more than once.
    50  func (phr *proposalHandlerRouter) Seal() {
    51  	if phr.sealed {
    52  		panic("router already sealed")
    53  	}
    54  	phr.sealed = true
    55  }
    56  
    57  // AddRoute adds a governance handler for a given path. It returns the Router
    58  // so AddRoute calls can be linked. It will panic if the router is sealed.
    59  func (phr *proposalHandlerRouter) AddRoute(path string, mp ProposalHandler) ProposalHandlerRouter {
    60  	if phr.sealed {
    61  		panic("router sealed; cannot add route handler")
    62  	}
    63  
    64  	if !isAlphaNumeric(path) {
    65  		panic("route expressions can only contain alphanumeric characters")
    66  	}
    67  	if phr.HasRoute(path) {
    68  		panic(fmt.Sprintf("route %s has already been initialized", path))
    69  	}
    70  
    71  	phr.routes[path] = mp
    72  	return phr
    73  }
    74  
    75  // HasRoute returns true if the router has a path registered or false otherwise.
    76  func (phr *proposalHandlerRouter) HasRoute(path string) bool {
    77  	return phr.routes[path] != nil
    78  }
    79  
    80  // GetRoute returns a Handler for a given path.
    81  func (phr *proposalHandlerRouter) GetRoute(path string) ProposalHandler {
    82  	if !phr.HasRoute(path) {
    83  		panic(fmt.Sprintf("route \"%s\" does not exist", path))
    84  	}
    85  
    86  	return phr.routes[path]
    87  }