-
Notifications
You must be signed in to change notification settings - Fork 402
/
genesis.go
122 lines (104 loc) · 3.85 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
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
package keeper
import (
abci "github.com/cometbft/cometbft/abci/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
// ValidatorSetSource is a subset of the staking keeper
type ValidatorSetSource interface {
ApplyAndReturnValidatorSetUpdates(sdk.Context) (updates []abci.ValidatorUpdate, err error)
}
// InitGenesis sets supply information for genesis.
//
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]abci.ValidatorUpdate, error) {
contractKeeper := NewGovPermissionKeeper(keeper)
err := keeper.SetParams(ctx, data.Params)
if err != nil {
return nil, errorsmod.Wrapf(err, "set params")
}
var maxCodeID uint64
for i, code := range data.Codes {
err := keeper.importCode(ctx, code.CodeID, code.CodeInfo, code.CodeBytes)
if err != nil {
return nil, errorsmod.Wrapf(err, "code %d with id: %d", i, code.CodeID)
}
if code.CodeID > maxCodeID {
maxCodeID = code.CodeID
}
if code.Pinned {
if err := contractKeeper.PinCode(ctx, code.CodeID); err != nil {
return nil, errorsmod.Wrapf(err, "contract number %d", i)
}
}
}
var maxContractID int
for i, contract := range data.Contracts {
contractAddr, err := sdk.AccAddressFromBech32(contract.ContractAddress)
if err != nil {
return nil, errorsmod.Wrapf(err, "address in contract number %d", i)
}
err = keeper.importContract(ctx, contractAddr, &contract.ContractInfo, contract.ContractState, contract.ContractCodeHistory) //nolint:gosec
if err != nil {
return nil, errorsmod.Wrapf(err, "contract number %d", i)
}
maxContractID = i + 1 // not ideal but max(contractID) is not persisted otherwise
}
for i, seq := range data.Sequences {
err := keeper.importAutoIncrementID(ctx, seq.IDKey, seq.Value)
if err != nil {
return nil, errorsmod.Wrapf(err, "sequence number %d", i)
}
}
// sanity check seq values
seqVal := keeper.PeekAutoIncrementID(ctx, types.KeySequenceCodeID)
if seqVal <= maxCodeID {
return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceCodeID), seqVal, maxCodeID)
}
seqVal = keeper.PeekAutoIncrementID(ctx, types.KeySequenceInstanceID)
if seqVal <= uint64(maxContractID) {
return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceInstanceID), seqVal, maxContractID)
}
return nil, nil
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState {
var genState types.GenesisState
genState.Params = keeper.GetParams(ctx)
keeper.IterateCodeInfos(ctx, func(codeID uint64, info types.CodeInfo) bool {
bytecode, err := keeper.GetByteCode(ctx, codeID)
if err != nil {
panic(err)
}
genState.Codes = append(genState.Codes, types.Code{
CodeID: codeID,
CodeInfo: info,
CodeBytes: bytecode,
Pinned: keeper.IsPinnedCode(ctx, codeID),
})
return false
})
keeper.IterateContractInfo(ctx, func(addr sdk.AccAddress, contract types.ContractInfo) bool {
var state []types.Model
keeper.IterateContractState(ctx, addr, func(key, value []byte) bool {
state = append(state, types.Model{Key: key, Value: value})
return false
})
contractCodeHistory := keeper.GetContractHistory(ctx, addr)
genState.Contracts = append(genState.Contracts, types.Contract{
ContractAddress: addr.String(),
ContractInfo: contract,
ContractState: state,
ContractCodeHistory: contractCodeHistory,
})
return false
})
for _, k := range [][]byte{types.KeySequenceCodeID, types.KeySequenceInstanceID} {
genState.Sequences = append(genState.Sequences, types.Sequence{
IDKey: k,
Value: keeper.PeekAutoIncrementID(ctx, k),
})
}
return &genState
}