-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
54 lines (46 loc) · 1.66 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
package random
import (
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furynet/furymod/modules/random/keeper"
"github.com/furynet/furymod/modules/random/types"
)
// InitGenesis stores the genesis state
func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) {
if err := types.ValidateGenesis(data); err != nil {
panic(fmt.Errorf("failed to initialize random genesis state: %s", err.Error()))
}
for height, requests := range data.PendingRandomRequests {
for _, request := range requests.Requests {
h, _ := strconv.ParseInt(height, 10, 64)
k.EnqueueRandomRequest(ctx, h, types.GenerateRequestID(request), request)
}
}
}
// ExportGenesis outputs the genesis state
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
pendingRequests := make(map[string]types.Requests)
k.IterateRandomRequestQueue(ctx, func(height int64, reqID []byte, request types.Request) bool {
leftHeight := fmt.Sprintf("%d", height)
heightRequests, ok := pendingRequests[leftHeight]
if ok {
heightRequests.Requests = append(heightRequests.Requests, request)
} else {
heightRequests = types.Requests{
Requests: []types.Request{request},
}
}
pendingRequests[leftHeight] = heightRequests
return false
})
return &types.GenesisState{PendingRandomRequests: pendingRequests}
}
func PrepForZeroHeightGenesis(ctx sdk.Context, k keeper.Keeper) {
k.IterateRandomRequestQueue(ctx, func(height int64, reqID []byte, request types.Request) bool {
leftHeight := height - ctx.BlockHeight() + 1
k.DequeueRandomRequest(ctx, height, reqID)
k.EnqueueRandomRequest(ctx, leftHeight, reqID, request)
return false
})
}