-
Notifications
You must be signed in to change notification settings - Fork 2
/
begin_blocker.go
233 lines (202 loc) · 5.63 KB
/
begin_blocker.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package distribution
import (
"context"
"encoding/base64"
"fmt"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/bro-n-bro/spacebox-crawler/modules/utils"
"github.com/bro-n-bro/spacebox-crawler/types"
"github.com/bro-n-bro/spacebox/broker/model"
)
var (
base64KeyValidator = base64.StdEncoding.EncodeToString([]byte(distrtypes.AttributeKeyValidator))
base64KeyAmount = base64.StdEncoding.EncodeToString([]byte(sdk.AttributeKeyAmount))
)
func (m *Module) HandleBeginBlocker(ctx context.Context, eventsMap types.BlockerEvents, height int64) error {
if err := m.parseProposerRewardEvent(ctx, eventsMap, height); err != nil {
m.log.Error().
Err(err).
Str("handler", "HandleBeginBlocker").
Int64("height", height).
Msg("failed to parse proposer reward event")
return err
}
if err := m.parseCommissionEvent(ctx, eventsMap, height); err != nil {
m.log.Error().
Err(err).
Str("handler", "HandleBeginBlocker").
Int64("height", height).
Msg("failed to parse distribution commission event")
return err
}
if err := m.parseRewardsEvent(ctx, eventsMap, height); err != nil {
m.log.Error().
Err(err).
Str("handler", "HandleBeginBlocker").
Int64("height", height).
Msg("failed to parse distribution rewards event")
return err
}
return nil
}
// parseProposerRewardEvent parses proposer reward event.
func (m *Module) parseProposerRewardEvent(ctx context.Context, eventsMap types.BlockerEvents, height int64) error {
events, ok := eventsMap[distrtypes.EventTypeProposerReward]
if !ok {
return nil
}
var (
coin model.Coin
operatorAddress string
err error
)
for _, event := range events {
if len(event.Attributes) < 2 {
m.log.Warn().
Int64("height", height).
Str("func", "parseProposerRewardEvent").
Msg("not enough attributes in event")
continue
}
operatorAddress, coin, err = m.parseAttributes(event)
if err != nil {
m.log.Error().
Err(err).
Str("func", "parseProposerRewardEvent").
Int64("height", height).
Msg("failed to parse attributes")
return err
}
if err = m.broker.PublishProposerReward(ctx, model.ProposerReward{
Height: height,
OperatorAddress: operatorAddress,
Reward: coin,
}); err != nil {
m.log.Error().
Err(err).
Str("func", "parseProposerRewardEvent").
Int64("height", height).
Msg("error while publishing proposer reward")
return err
}
}
return nil
}
// parseCommissionEvent parses distribution commission event.
func (m *Module) parseCommissionEvent(ctx context.Context, eventsMap types.BlockerEvents, height int64) error {
events, ok := eventsMap[distrtypes.EventTypeCommission]
if !ok {
return nil
}
var (
operatorAddress string
coin model.Coin
err error
)
for _, event := range events {
if len(event.Attributes) < 2 {
m.log.Warn().
Str("event", distrtypes.EventTypeCommission).
Int64("height", height).
Msg("not enough attributes in event")
continue
}
operatorAddress, coin, err = m.parseAttributes(event)
if err != nil {
m.log.Error().
Err(err).
Str("func", "parseCommissionEvent").
Int64("height", height).
Msg("failed to parse attributes")
return err
}
if err = m.broker.PublishDistributionCommission(ctx, model.DistributionCommission{
Height: height,
OperatorAddress: operatorAddress,
Amount: coin,
}); err != nil {
m.log.Error().
Err(err).
Str("func", "parseCommissionEvent").
Int64("height", height).
Msg("error while publishing distribution commission")
return err
}
}
return nil
}
// parseRewardsEvent parses rewards event.
func (m *Module) parseRewardsEvent(ctx context.Context, eventsMap types.BlockerEvents, height int64) error {
events, ok := eventsMap[distrtypes.EventTypeRewards]
if !ok {
return nil
}
var (
operatorAddress string
coin model.Coin
err error
)
for _, event := range events {
if len(event.Attributes) < 2 {
m.log.Warn().
Int64("height", height).
Str("func", "parseRewardsEvent").
Msg("not enough attributes in event")
continue
}
operatorAddress, coin, err = m.parseAttributes(event)
if err != nil {
m.log.Error().
Err(err).
Str("func", "parseCommissionEvent").
Int64("height", height).
Msg("failed to parse attributes")
return err
}
if err = m.broker.PublishDistributionReward(ctx, model.DistributionReward{
Height: height,
OperatorAddress: operatorAddress,
Amount: coin,
}); err != nil {
m.log.Error().
Err(err).
Str("func", "parseRewardsEvent").
Int64("height", height).
Msg("error while publishing distribution reward")
return err
}
}
return nil
}
func (m *Module) parseAttributes(event abci.Event) (string, model.Coin, error) {
var (
coin model.Coin
validator string
)
for _, attr := range event.Attributes {
// try to decode value if needed
switch attr.Key {
case base64KeyValidator, base64KeyAmount:
var err error
attr.Value, err = utils.DecodeToString(attr.Value)
if err != nil {
return "", model.Coin{}, err
}
}
switch attr.Key {
case distrtypes.AttributeKeyValidator, base64KeyValidator:
validator = attr.Value
case sdk.AttributeKeyAmount, base64KeyAmount:
coins, err := utils.ParseCoinsFromString(attr.Value)
if err != nil {
return "", model.Coin{}, fmt.Errorf("failed to convert %q to coin: %w", attr.Value, err)
}
if len(coins) > 0 {
coin = m.tbM.MapCoin(coins[0])
}
}
}
return validator, coin, nil
}