-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.go
141 lines (120 loc) · 3.74 KB
/
handler.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package chain
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types"
"github.com/stafihub/rtoken-relay-core/common/core"
"github.com/stafihub/rtoken-relay-core/common/log"
)
const msgLimit = 4096
type Handler struct {
conn *Connection
minUnDelegateAmount types.Int //skip unbond, if less than this on multisg addresse
router *core.Router
msgChan chan *core.Message
log log.Logger
stopChan <-chan struct{}
sysErrChan chan<- error
}
func NewHandler(conn *Connection, minUnDelegateAmount types.Int, log log.Logger, stopChan <-chan struct{}, sysErrChan chan<- error) *Handler {
return &Handler{
conn: conn,
minUnDelegateAmount: minUnDelegateAmount,
msgChan: make(chan *core.Message, msgLimit),
log: log,
stopChan: stopChan,
sysErrChan: sysErrChan,
}
}
func (h *Handler) setRouter(r *core.Router) {
h.router = r
}
func (h *Handler) start() error {
go h.msgHandler()
return nil
}
// resolve msg from other chains
func (h *Handler) HandleMessage(m *core.Message) {
h.queueMessage(m)
}
func (h *Handler) queueMessage(m *core.Message) {
h.msgChan <- m
}
func (h *Handler) msgHandler() {
for {
select {
case <-h.stopChan:
h.log.Info("msgHandler receive stopChan, will stop")
return
case msg := <-h.msgChan:
err := h.handleMessage(msg)
if err != nil {
h.sysErrChan <- fmt.Errorf("resolveMessage process failed.err: %s, msg: %+v", err, msg)
return
}
}
}
}
// resolve msg from other chains
func (h *Handler) handleMessage(m *core.Message) error {
switch m.Reason {
case core.ReasonEraPoolUpdatedEvent:
return h.handleEraPoolUpdatedEvent(m)
case core.ReasonBondReportedEvent:
return h.handleBondReportedEvent(m)
case core.ReasonActiveReportedEvent:
return h.handleActiveReportedEvent(m)
case core.ReasonRParamsChangedEvent:
return h.handleRParamsChangedEvent(m)
case core.ReasonRValidatorUpdatedEvent:
return h.handleRValidatorUpdatedEvent(m)
case core.ReasonRValidatorAddedEvent:
return h.handleRValidatorAddedEvent(m)
case core.ReasonInitPoolEvent:
return h.handleInitPoolEvent(m)
case core.ReasonRemovePoolEvent:
return h.handleRemovePoolEvent(m)
default:
return fmt.Errorf("message reason unsupported reason: %s", m.Reason)
}
}
// update rparams
func (h *Handler) handleRParamsChangedEvent(m *core.Message) error {
h.log.Info("handleRParamsChangedEvent", "m", m)
eventRParamsChanged, ok := m.Content.(core.EventRParamsChanged)
if !ok {
return fmt.Errorf("EventRParamsChanged cast failed, %+v", m)
}
leastBond, err := types.ParseCoinNormalized(eventRParamsChanged.LeastBond)
if err != nil {
return err
}
h.conn.RParams.eraSeconds = int64(eventRParamsChanged.EraSeconds)
h.conn.RParams.leastBond = leastBond
h.conn.RParams.offset = int64(eventRParamsChanged.Offset)
for _, c := range h.conn.poolClients {
err := c.SetGasPrice(eventRParamsChanged.GasPrice)
if err != nil {
return fmt.Errorf("setGasPrice failed, err: %s", err)
}
}
return nil
}
// only support ica pool
func (h *Handler) handleInitPoolEvent(m *core.Message) error {
h.log.Info("handleInitPoolEvent", "m", m)
eventInitPool, ok := m.Content.(core.EventInitPool)
if !ok {
return fmt.Errorf("EventInitPool cast failed, %+v", m)
}
return h.conn.AddIcaPool(eventInitPool.PoolAddress, eventInitPool.WithdrawalAddress, eventInitPool.HostChannelId, eventInitPool.Validators)
}
// support multisig/ica pool
func (h *Handler) handleRemovePoolEvent(m *core.Message) error {
h.log.Info("handleRemovePoolEvent", "m", m)
eventRemovePool, ok := m.Content.(core.EventRemovePool)
if !ok {
return fmt.Errorf("EventRemovePool cast failed, %+v", m)
}
h.conn.RemovePool(eventRemovePool.PoolAddress)
return nil
}