-
Notifications
You must be signed in to change notification settings - Fork 113
/
vote_router.go
106 lines (84 loc) · 2.84 KB
/
vote_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package types
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/axelarnetwork/axelar-core/x/vote/exported"
)
// VoteRouter implements a Vote router based on module name.
type VoteRouter interface {
AddHandler(module string, handler exported.VoteHandler) VoteRouter
HasHandler(module string) bool
GetHandler(module string) exported.VoteHandler
Seal()
}
var _ VoteRouter = (*router)(nil)
type router struct {
routes map[string]exported.VoteHandler
sealed bool
}
// NewRouter creates a new Router interface instance
func NewRouter() VoteRouter {
return &router{
routes: make(map[string]exported.VoteHandler),
}
}
// Seal prevents additional route handlers from being added to the router.
func (r *router) Seal() {
r.sealed = true
}
// AddHandler registers a vote handler for a given path and returns the handler.
// Panics if the router is sealed, module is an empty string, or if the module has been registered already.
func (r *router) AddHandler(module string, handler exported.VoteHandler) VoteRouter {
if r.sealed {
panic("cannot add handler (router sealed)")
}
if module == "" {
panic("module name cannot be an empty string")
}
if r.HasHandler(module) {
panic(fmt.Sprintf("handler for module %s has already been registered", module))
}
r.routes[module] = handler
return r
}
// HasHandler returns true if the router has an handler registered for the given module
func (r *router) HasHandler(module string) bool {
return r.routes[module] != nil
}
// GetHandler returns a Handler for a given module.
func (r *router) GetHandler(module string) exported.VoteHandler {
if !r.HasHandler(module) {
panic(fmt.Sprintf("handler for module \"%s\" not registered", module))
}
return handlerWrapper{r.routes[module]}
}
type handlerWrapper struct {
handler exported.VoteHandler
}
func (w handlerWrapper) IsFalsyResult(result codec.ProtoMarshaler) bool {
return w.handler.IsFalsyResult(result)
}
func (w handlerWrapper) HandleResult(ctx sdk.Context, result codec.ProtoMarshaler) error {
return cache(w.handler.HandleResult)(ctx, result)
}
func (w handlerWrapper) HandleFailedPoll(ctx sdk.Context, poll exported.Poll) error {
return cache(w.handler.HandleFailedPoll)(ctx, poll)
}
func (w handlerWrapper) HandleCompletedPoll(ctx sdk.Context, poll exported.Poll) error {
return cache(w.handler.HandleCompletedPoll)(ctx, poll)
}
func (w handlerWrapper) HandleExpiredPoll(ctx sdk.Context, poll exported.Poll) error {
return cache(w.handler.HandleExpiredPoll)(ctx, poll)
}
func cache[T any](f func(ctx sdk.Context, x T) error) func(ctx sdk.Context, x T) error {
return func(ctx sdk.Context, x T) error {
cachedCtx, writeCache := ctx.CacheContext()
if err := f(cachedCtx, x); err != nil {
return err
}
writeCache()
ctx.EventManager().EmitEvents(cachedCtx.EventManager().Events())
return nil
}
}