-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
91 lines (82 loc) · 2.94 KB
/
genesis.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/sanction"
)
// InitGenesis updates this keeper's store using the provided GenesisState.
func (k Keeper) InitGenesis(origCtx sdk.Context, genState *sanction.GenesisState) {
if genState == nil {
return
}
// We don't want the events from this, so use a context with a throw-away event manager.
ctx := origCtx.WithEventManager(sdk.NewEventManager())
if err := k.SetParams(ctx, genState.Params); err != nil {
panic(fmt.Errorf("error setting params: %w", err))
}
toSanction, err := toAccAddrs(genState.SanctionedAddresses)
if err != nil {
// toAccAddrs has enough context to the error, no need to add more.
panic(err)
}
err = k.SanctionAddresses(ctx, toSanction...)
if err != nil {
panic(fmt.Errorf("error sanctioning addresses: %w", err))
}
for i, entry := range genState.TemporaryEntries {
var addr sdk.AccAddress
addr, err = sdk.AccAddressFromBech32(entry.Address)
if err != nil {
panic(fmt.Errorf("invalid temp entry[%d]: invalid address: %w", i, err))
}
switch entry.Status {
case sanction.TEMP_STATUS_SANCTIONED:
err = k.AddTemporarySanction(ctx, entry.ProposalId, addr)
if err != nil {
panic(fmt.Errorf("error adding temp entry[%d]: sanction: %w", i, err))
}
case sanction.TEMP_STATUS_UNSANCTIONED:
err = k.AddTemporaryUnsanction(ctx, entry.ProposalId, addr)
if err != nil {
panic(fmt.Errorf("error adding temp entry[%d]: unsanction: %w", i, err))
}
default:
panic(fmt.Errorf("invalid temp entry[%d]: invalid status: %s", i, entry.Status))
}
}
}
// ExportGenesis reads this keeper's entire state and returns it as a GenesisState.
func (k Keeper) ExportGenesis(ctx sdk.Context) *sanction.GenesisState {
params := k.GetParams(ctx)
sanctionedAddrs := k.GetAllSanctionedAddresses(ctx)
tempEntries := k.GetAllTemporaryEntries(ctx)
return sanction.NewGenesisState(params, sanctionedAddrs, tempEntries)
}
// GetAllSanctionedAddresses gets the bech32 string of every account that is sanctioned.
// This is designed for use with ExportGenesis. See also IterateSanctionedAddresses.
func (k Keeper) GetAllSanctionedAddresses(ctx sdk.Context) []string {
var rv []string
k.IterateSanctionedAddresses(ctx, func(addr sdk.AccAddress) bool {
rv = append(rv, addr.String())
return false
})
return rv
}
// GetAllTemporaryEntries gets all the Temporary entries.
// This is designed for use with ExportGenesis. See also IterateTemporaryEntries.
func (k Keeper) GetAllTemporaryEntries(ctx sdk.Context) []*sanction.TemporaryEntry {
var rv []*sanction.TemporaryEntry
k.IterateTemporaryEntries(ctx, nil, func(addr sdk.AccAddress, id uint64, isSanction bool) bool {
status := sanction.TEMP_STATUS_SANCTIONED
if !isSanction {
status = sanction.TEMP_STATUS_UNSANCTIONED
}
rv = append(rv, &sanction.TemporaryEntry{
Address: addr.String(),
ProposalId: id,
Status: status,
})
return false
})
return rv
}