-
Notifications
You must be signed in to change notification settings - Fork 414
/
keys.go
108 lines (91 loc) · 3.87 KB
/
keys.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// ModuleName is the name of the contract module
ModuleName = "wasm"
// StoreKey is the string store representation
StoreKey = ModuleName
// TStoreKey is the string transient store representation
TStoreKey = "transient_" + ModuleName
// QuerierRoute is the querier route for the wasm module
QuerierRoute = ModuleName
// RouterKey is the msg router key for the wasm module
RouterKey = ModuleName
)
// nolint
var (
CodeKeyPrefix = []byte{0x01}
ContractKeyPrefix = []byte{0x02}
ContractStorePrefix = []byte{0x03}
SequenceKeyPrefix = []byte{0x04}
ContractCodeHistoryElementPrefix = []byte{0x05}
ContractByCodeIDAndCreatedSecondaryIndexPrefix = []byte{0x06}
PinnedCodeIndexPrefix = []byte{0x07}
TXCounterPrefix = []byte{0x08}
KeyLastCodeID = append(SequenceKeyPrefix, []byte("lastCodeId")...)
KeyLastInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...)
)
// GetCodeKey constructs the key for retreiving the ID for the WASM code
func GetCodeKey(codeID uint64) []byte {
contractIDBz := sdk.Uint64ToBigEndian(codeID)
return append(CodeKeyPrefix, contractIDBz...)
}
// GetContractAddressKey returns the key for the WASM contract instance
func GetContractAddressKey(addr sdk.AccAddress) []byte {
return append(ContractKeyPrefix, addr...)
}
// GetContractStorePrefix returns the store prefix for the WASM contract instance
func GetContractStorePrefix(addr sdk.AccAddress) []byte {
return append(ContractStorePrefix, addr...)
}
// GetContractByCreatedSecondaryIndexKey returns the key for the secondary index:
// `<prefix><codeID><created/last-migrated><contractAddr>`
func GetContractByCreatedSecondaryIndexKey(contractAddr sdk.AccAddress, c ContractCodeHistoryEntry) []byte {
prefix := GetContractByCodeIDSecondaryIndexPrefix(c.CodeID)
prefixLen := len(prefix)
r := make([]byte, prefixLen+AbsoluteTxPositionLen+ContractAddrLen)
copy(r[0:], prefix)
copy(r[prefixLen:], c.Updated.Bytes())
copy(r[prefixLen+AbsoluteTxPositionLen:], contractAddr)
return r
}
// GetContractByCodeIDSecondaryIndexPrefix returns the prefix for the second index: `<prefix><codeID>`
func GetContractByCodeIDSecondaryIndexPrefix(codeID uint64) []byte {
prefixLen := len(ContractByCodeIDAndCreatedSecondaryIndexPrefix)
const codeIDLen = 8
r := make([]byte, prefixLen+codeIDLen)
copy(r[0:], ContractByCodeIDAndCreatedSecondaryIndexPrefix)
copy(r[prefixLen:], sdk.Uint64ToBigEndian(codeID))
return r
}
// GetContractCodeHistoryElementKey returns the key a contract code history entry: `<prefix><contractAddr><position>`
func GetContractCodeHistoryElementKey(contractAddr sdk.AccAddress, pos uint64) []byte {
prefix := GetContractCodeHistoryElementPrefix(contractAddr)
prefixLen := len(prefix)
r := make([]byte, prefixLen+8)
copy(r[0:], prefix)
copy(r[prefixLen:], sdk.Uint64ToBigEndian(pos))
return r
}
// GetContractCodeHistoryElementPrefix returns the key prefix for a contract code history entry: `<prefix><contractAddr>`
func GetContractCodeHistoryElementPrefix(contractAddr sdk.AccAddress) []byte {
prefixLen := len(ContractCodeHistoryElementPrefix)
r := make([]byte, prefixLen+ContractAddrLen)
copy(r[0:], ContractCodeHistoryElementPrefix)
copy(r[prefixLen:], contractAddr)
return r
}
// GetPinnedCodeIndexPrefix returns the key prefix for a code id pinned into the wasmvm cache
func GetPinnedCodeIndexPrefix(codeID uint64) []byte {
prefixLen := len(PinnedCodeIndexPrefix)
r := make([]byte, prefixLen+8)
copy(r[0:], PinnedCodeIndexPrefix)
copy(r[prefixLen:], sdk.Uint64ToBigEndian(codeID))
return r
}
// ParsePinnedCodeIndex converts the serialized code ID back.
func ParsePinnedCodeIndex(s []byte) uint64 {
return sdk.BigEndianToUint64(s)
}