-
Notifications
You must be signed in to change notification settings - Fork 19
/
the_vesting_stats_should_be.go
148 lines (124 loc) · 4.28 KB
/
the_vesting_stats_should_be.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package steps
import (
"errors"
"fmt"
"strings"
"code.vegaprotocol.io/vega/core/integration/stubs"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/cucumber/godog"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
func TheVestingStatsShouldBe(broker *stubs.BrokerStub, epochStr string, table *godog.Table) error {
epoch, err := U64(epochStr)
if err != nil {
return fmt.Errorf("could not parse epoch: %w", err)
}
expectedVestingStatsStats, err := parseVestingStatsShouldBeTable(table)
if err != nil {
return fmt.Errorf("table is invalid: %w", err)
}
vestingStats := broker.VestingStats()
foundStreaksForEpoch := map[string]eventspb.PartyVestingStats{}
for _, stats := range vestingStats {
if stats.AtEpoch == epoch {
return compareVestingStats(expectedVestingStatsStats, foundStreaksForEpoch)
}
}
return fmt.Errorf("no vesting stats found for epoch %q", epochStr)
}
func parseVestingStatsShouldBeTable(table *godog.Table) (map[string]eventspb.PartyVestingStats, error) {
rows := StrictParseTable(table, []string{
"party",
"reward bonus multiplier",
}, []string{})
stats := map[string]eventspb.PartyVestingStats{}
for _, row := range rows {
specificRow := newVestingStatsShouldBeRow(row)
partyID := specificRow.Party()
_, alreadyRegistered := stats[partyID]
if alreadyRegistered {
return nil, fmt.Errorf("cannot have more than one expectation for party %q", partyID)
}
stats[partyID] = eventspb.PartyVestingStats{
PartyId: partyID,
RewardBonusMultiplier: specificRow.RewardBonusMultiplier(),
}
}
return stats, nil
}
func compareVestingStats(expectedVestingStats map[string]eventspb.PartyVestingStats, foundVestingStats map[string]eventspb.PartyVestingStats) error {
foundVestingStatsIDs := maps.Keys(expectedVestingStats)
expectedVestingStatsIDs := maps.Keys(expectedVestingStats)
slices.Sort(foundVestingStatsIDs)
slices.Sort(expectedVestingStatsIDs)
unexpectedParties := []string{}
partiesNotFound := []string{}
for _, expectedID := range expectedVestingStatsIDs {
if _, ok := foundVestingStats[expectedID]; !ok {
partiesNotFound = append(partiesNotFound, expectedID)
}
}
for _, foundID := range foundVestingStatsIDs {
if _, ok := expectedVestingStats[foundID]; !ok {
unexpectedParties = append(unexpectedParties, foundID)
}
}
var errStr string
if len(partiesNotFound) > 0 {
errStr = "parties not found: " + strings.Join(partiesNotFound, ", ")
}
if len(unexpectedParties) > 0 {
if errStr != "" {
errStr += ", and "
}
errStr += "unexpected parties: " + strings.Join(unexpectedParties, ", ")
}
if errStr != "" {
return errors.New(errStr)
}
for _, party := range expectedVestingStatsIDs {
foundActivityStreak := foundVestingStats[party]
expectedActivityStreak := expectedVestingStats[party]
if expectedActivityStreak.RewardBonusMultiplier != foundActivityStreak.RewardBonusMultiplier {
return formatDiff(
fmt.Sprintf("vesting stats did not match for party %q", party),
map[string]string{
"reward bonus multiplier": expectedActivityStreak.RewardBonusMultiplier,
},
map[string]string{
"reward bonus multiplier": foundActivityStreak.RewardBonusMultiplier,
},
)
}
}
return nil
}
type vestingStatsShouldBeRow struct {
row RowWrapper
}
func newVestingStatsShouldBeRow(r RowWrapper) vestingStatsShouldBeRow {
return vestingStatsShouldBeRow{
row: r,
}
}
func (r vestingStatsShouldBeRow) Party() string {
return r.row.MustStr("party")
}
func (r vestingStatsShouldBeRow) RewardBonusMultiplier() string {
return r.row.MustStr("reward bonus multiplier")
}