Skip to content

Commit 101cb28

Browse files
feat: include overflow stats in stats event (#3)
1 parent c05e7f6 commit 101cb28

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

client/schema.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const (
2222

2323
// Event schema versions, only increment when the schema changes
2424
const (
25-
activeEventVersion int = 0
26-
unbondingEventVersion int = 0
27-
withdrawEventVersion int = 0
28-
expiredEventVersion int = 0
29-
statsEventVersion int = 0
30-
btcInfoEventVersion int = 0
31-
confirmedInfoEventVersion int = 0
25+
ActiveEventVersion int = 0
26+
UnbondingEventVersion int = 0
27+
WithdrawEventVersion int = 0
28+
ExpiredEventVersion int = 0
29+
StatsEventVersion int = 1
30+
BtcInfoEventVersion int = 0
31+
ConfirmedInfoEventVersion int = 0
3232
)
3333

3434
type EventType int
@@ -74,7 +74,7 @@ func NewActiveStakingEvent(
7474
isOverflow bool,
7575
) ActiveStakingEvent {
7676
return ActiveStakingEvent{
77-
SchemaVersion: activeEventVersion,
77+
SchemaVersion: ActiveEventVersion,
7878
EventType: ActiveStakingEventType,
7979
StakingTxHashHex: stakingTxHashHex,
8080
StakerPkHex: stakerPkHex,
@@ -119,7 +119,7 @@ func NewUnbondingStakingEvent(
119119
unbondingTxHashHex string,
120120
) UnbondingStakingEvent {
121121
return UnbondingStakingEvent{
122-
SchemaVersion: unbondingEventVersion,
122+
SchemaVersion: UnbondingEventVersion,
123123
EventType: UnbondingStakingEventType,
124124
StakingTxHashHex: stakingTxHashHex,
125125
UnbondingStartHeight: unbondingStartHeight,
@@ -147,7 +147,7 @@ func (e WithdrawStakingEvent) GetStakingTxHashHex() string {
147147

148148
func NewWithdrawStakingEvent(stakingTxHashHex string) WithdrawStakingEvent {
149149
return WithdrawStakingEvent{
150-
SchemaVersion: withdrawEventVersion,
150+
SchemaVersion: WithdrawEventVersion,
151151
EventType: WithdrawStakingEventType,
152152
StakingTxHashHex: stakingTxHashHex,
153153
}
@@ -170,7 +170,7 @@ func (e ExpiredStakingEvent) GetStakingTxHashHex() string {
170170

171171
func NewExpiredStakingEvent(stakingTxHashHex string, txType string) ExpiredStakingEvent {
172172
return ExpiredStakingEvent{
173-
SchemaVersion: expiredEventVersion,
173+
SchemaVersion: ExpiredEventVersion,
174174
EventType: ExpiredStakingEventType,
175175
StakingTxHashHex: stakingTxHashHex,
176176
TxType: txType,
@@ -185,6 +185,7 @@ type StatsEvent struct {
185185
FinalityProviderPkHex string `json:"finality_provider_pk_hex"`
186186
StakingValue uint64 `json:"staking_value"`
187187
State string `json:"state"`
188+
IsOverflow bool `json:"is_overflow"`
188189
}
189190

190191
func (e StatsEvent) GetEventType() EventType {
@@ -201,15 +202,17 @@ func NewStatsEvent(
201202
finalityProviderPkHex string,
202203
stakingValue uint64,
203204
state string,
205+
isOverflow bool,
204206
) StatsEvent {
205207
return StatsEvent{
206-
SchemaVersion: statsEventVersion,
208+
SchemaVersion: StatsEventVersion,
207209
EventType: StatsEventType,
208210
StakingTxHashHex: stakingTxHashHex,
209211
StakerPkHex: stakerPkHex,
210212
FinalityProviderPkHex: finalityProviderPkHex,
211213
StakingValue: stakingValue,
212214
State: state,
215+
IsOverflow: isOverflow,
213216
}
214217
}
215218

@@ -232,7 +235,7 @@ func (e BtcInfoEvent) GetStakingTxHashHex() string {
232235

233236
func NewBtcInfoEvent(height, confirmedTvl, unconfirmedTvl uint64) BtcInfoEvent {
234237
return BtcInfoEvent{
235-
SchemaVersion: btcInfoEventVersion,
238+
SchemaVersion: BtcInfoEventVersion,
236239
EventType: BtcInfoEventType,
237240
Height: height,
238241
ConfirmedTvl: confirmedTvl,
@@ -258,7 +261,7 @@ func (e ConfirmedInfoEvent) GetStakingTxHashHex() string {
258261

259262
func NewConfirmedInfoEvent(height, tvl uint64) ConfirmedInfoEvent {
260263
return ConfirmedInfoEvent{
261-
SchemaVersion: confirmedInfoEventVersion,
264+
SchemaVersion: ConfirmedInfoEventVersion,
262265
EventType: ConfirmedInfoEventType,
263266
Height: height,
264267
Tvl: tvl,

queuemngr/queue_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ func (qc *QueueManager) Start() error {
7777
return nil
7878
}
7979

80-
func PushEvent[T any](qc *QueueManager, ev T) error {
80+
func PushEvent[T any](queueClient client.QueueClient, ev T) error {
8181
jsonBytes, err := json.Marshal(ev)
8282
if err != nil {
8383
return err
8484
}
8585
messageBody := string(jsonBytes)
8686

87-
err = qc.StakingQueue.SendMessage(context.TODO(), messageBody)
87+
err = queueClient.SendMessage(context.TODO(), messageBody)
8888
if err != nil {
8989
return fmt.Errorf("failed to push event: %w", err)
9090
}

tests/e2e_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestSchemaVersionBackwardsCompatibility(t *testing.T) {
104104
stakingEventReceivedChan, err := queueManager.StakingQueue.ReceiveMessages()
105105
require.NoError(t, err)
106106

107-
err = queuemngr.PushEvent(queueManager, event)
107+
err = queuemngr.PushEvent(queueManager.StakingQueue, event)
108108
require.NoError(t, err)
109109
receivedEv := <-stakingEventReceivedChan
110110
var stakingEv client.ActiveStakingEvent
@@ -193,6 +193,32 @@ func TestWithdrawEvent(t *testing.T) {
193193
}
194194
}
195195

196+
func TestStatsEvent(t *testing.T) {
197+
numEvents := 3
198+
statsEvents := buildNStatsEvents(mockStakerHash, numEvents)
199+
queueCfg := config.DefaultQueueConfig()
200+
201+
testServer := setupTestQueueConsumer(t, queueCfg)
202+
defer testServer.Stop(t)
203+
204+
queueManager := testServer.QueueManager
205+
206+
eventReceivedChan, err := queueManager.StatsQueue.ReceiveMessages()
207+
require.NoError(t, err)
208+
209+
for _, ev := range statsEvents {
210+
err = queuemngr.PushEvent(queueManager.StatsQueue, ev)
211+
require.NoError(t, err)
212+
213+
receivedEv := <-eventReceivedChan
214+
var statsEv client.StatsEvent
215+
err := json.Unmarshal([]byte(receivedEv.Body), &statsEv)
216+
require.NoError(t, err)
217+
require.Equal(t, ev, &statsEv)
218+
require.Equal(t, 1, statsEv.SchemaVersion)
219+
}
220+
}
221+
196222
func TestExpiryEvent(t *testing.T) {
197223
numExpiryEvents := 3
198224
expiryEvents := buildNExpiryEvents(numExpiryEvents)

tests/setup.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ func buildNExpiryEvents(numOfEvent int) []*client.ExpiredStakingEvent {
149149
return expiryEvents
150150
}
151151

152+
func buildNStatsEvents(stakerHash string, numOfEvent int) []*client.StatsEvent {
153+
var statsEvents []*client.StatsEvent
154+
for i := 0; i < numOfEvent; i++ {
155+
activeStakingEvent := client.NewStatsEvent(
156+
"0x1234567890abcdef"+fmt.Sprint(i),
157+
stakerHash,
158+
"0xabcdef1234567890"+fmt.Sprint(i),
159+
1+uint64(i),
160+
"active",
161+
false,
162+
)
163+
164+
statsEvents = append(statsEvents, &activeStakingEvent)
165+
}
166+
return statsEvents
167+
}
168+
152169
func buildNBtcInfoEvents(numOfEvent int) []*client.BtcInfoEvent {
153170
var btcInfoEvents []*client.BtcInfoEvent
154171
for i := 0; i < numOfEvent; i++ {

0 commit comments

Comments
 (0)