-
Notifications
You must be signed in to change notification settings - Fork 10
/
wasm_genesis_helper.go
115 lines (105 loc) · 2.78 KB
/
wasm_genesis_helper.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
package types
import (
"bytes"
"crypto/sha256"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Copy pasted from Wasmd - https://github.com/CosmWasm/wasmd/blob/master/x/wasm/client/cli/genesis_msg.go
type codeMeta struct {
CodeID uint64 `json:"code_id"`
Info types.CodeInfo `json:"info"`
}
func getAllCodes(state *types.GenesisState) ([]codeMeta, error) {
all := make([]codeMeta, len(state.Codes))
for i, c := range state.Codes {
all[i] = codeMeta{
CodeID: c.CodeID,
Info: c.CodeInfo,
}
}
// add inflight
seq := codeSeqValue(state)
for _, m := range state.GenMsgs {
if msg := m.GetStoreCode(); msg != nil {
var accessConfig types.AccessConfig
if msg.InstantiatePermission != nil {
accessConfig = *msg.InstantiatePermission
} else {
// default
creator, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, fmt.Errorf("sender: %s", err)
}
accessConfig = state.Params.InstantiateDefaultPermission.With(creator)
}
hash := sha256.Sum256(msg.WASMByteCode)
all = append(all, codeMeta{
CodeID: seq,
Info: types.CodeInfo{
CodeHash: hash[:],
Creator: msg.Sender,
InstantiateConfig: accessConfig,
},
})
seq++
}
}
return all, nil
}
type contractMeta struct {
ContractAddress string `json:"contract_address"`
Info types.ContractInfo `json:"info"`
}
func getAllContracts(state *types.GenesisState) []contractMeta {
all := make([]contractMeta, len(state.Contracts))
for i, c := range state.Contracts {
all[i] = contractMeta{
ContractAddress: c.ContractAddress,
Info: c.ContractInfo,
}
}
// add inflight
seq := contractSeqValue(state)
for _, m := range state.GenMsgs {
if msg := m.GetInstantiateContract(); msg != nil {
all = append(all, contractMeta{
ContractAddress: keeper.BuildContractAddress(msg.CodeID, seq).String(),
Info: types.ContractInfo{
CodeID: msg.CodeID,
Creator: msg.Sender,
Admin: msg.Admin,
Label: msg.Label,
},
})
seq++
}
}
return all
}
// contractSeqValue reads the contract sequence from the genesis or
// returns default start value used in the keeper
func contractSeqValue(state *types.GenesisState) uint64 {
var seq uint64 = 1
for _, s := range state.Sequences {
if bytes.Equal(s.IDKey, types.KeyLastInstanceID) {
seq = s.Value
break
}
}
return seq
}
// codeSeqValue reads the code sequence from the genesis or
// returns default start value used in the keeper
func codeSeqValue(state *types.GenesisState) uint64 {
var seq uint64 = 1
for _, s := range state.Sequences {
if bytes.Equal(s.IDKey, types.KeyLastCodeID) {
seq = s.Value
break
}
}
return seq
}