/
records.go
83 lines (74 loc) · 3.05 KB
/
records.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
package types
import (
fmt "fmt"
)
// Confirm there are no duplicate delegation record Ids and that the amounts are not nil
func ValidateDelegationRecordGenesis(delegationRecords []DelegationRecord) error {
ids := map[uint64]bool{}
for _, delegationRecord := range delegationRecords {
if delegationRecord.NativeAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized native amount in delegation record %d", delegationRecord.Id)
}
if _, ok := ids[delegationRecord.Id]; ok {
return ErrInvalidGenesisRecords.Wrapf("duplicate delegation record %d", delegationRecord.Id)
}
ids[delegationRecord.Id] = true
}
return nil
}
// Confirm there are no duplicate unbonding record Ids and that the amounts are not nil
func ValidateUnbondingRecordGenesis(unbondingRecords []UnbondingRecord) error {
ids := map[uint64]bool{}
for _, unbondingRecord := range unbondingRecords {
if unbondingRecord.NativeAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized native amount in unbonding record %d", unbondingRecord.Id)
}
if unbondingRecord.StTokenAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized sttoken amount in unbonding record %d", unbondingRecord.Id)
}
if _, ok := ids[unbondingRecord.Id]; ok {
return ErrInvalidGenesisRecords.Wrapf("duplicate unbonding record %d", unbondingRecord.Id)
}
ids[unbondingRecord.Id] = true
}
return nil
}
// Confirm there are no duplicate slash record Ids and that the amounts are not nil
func ValidateRedemptionRecordGenesis(redemptionRecords []RedemptionRecord) error {
ids := map[string]bool{}
for _, redemptionRecord := range redemptionRecords {
idKey := fmt.Sprintf("%d-%s", redemptionRecord.UnbondingRecordId, redemptionRecord.Redeemer)
if redemptionRecord.NativeAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized native amount in redemption record %s", idKey)
}
if redemptionRecord.StTokenAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized sttoken amount in redemption record %s", idKey)
}
if _, ok := ids[idKey]; ok {
return ErrInvalidGenesisRecords.Wrapf("duplicate redemption record %s", idKey)
}
ids[idKey] = true
}
return nil
}
// Confirm there are no duplicate slash record Ids and that the amounts are not nil
func ValidateSlashRecordGenesis(slashRecords []SlashRecord) error {
ids := map[uint64]bool{}
for _, slashRecord := range slashRecords {
if slashRecord.NativeAmount.IsNil() {
return ErrInvalidGenesisRecords.Wrapf("uninitialized native amount in slash record %d", slashRecord.Id)
}
if _, ok := ids[slashRecord.Id]; ok {
return ErrInvalidGenesisRecords.Wrapf("duplicate slash record %d", slashRecord.Id)
}
ids[slashRecord.Id] = true
}
return nil
}
// Returns a RedemptionRecordResponse, which is a RedemptionRecord with the unbonding time
func NewRedemptionRecordResponse(redemptionRecord RedemptionRecord, unbondingTime uint64) RedemptionRecordResponse {
return RedemptionRecordResponse{
RedemptionRecord: &redemptionRecord,
UnbondingCompletionTimeSeconds: unbondingTime,
}
}