-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
272 lines (223 loc) · 11.3 KB
/
key.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package types
import (
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
)
const (
// ModuleName is the name of the module
ModuleName = "gravity"
// StoreKey to be used when creating the KVStore
StoreKey = ModuleName
// RouterKey is the module name router key
RouterKey = ModuleName
// QuerierRoute to be used for querierer msgs
QuerierRoute = ModuleName
)
var (
// EthAddressKey indexes cosmos validator account addresses
// i.e. cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn
EthAddressKey = []byte{0x1}
// ValsetRequestKey indexes valset requests by nonce
ValsetRequestKey = []byte{0x2}
// ValsetConfirmKey indexes valset confirmations by nonce and the validator account address
// i.e cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn
ValsetConfirmKey = []byte{0x3}
// OracleClaimKey Claim details by nonce and validator address
// i.e. cosmosvaloper1ahx7f8wyertuus9r20284ej0asrs085case3kn
// A claim is named more intuitively than an Attestation, it is literally
// a validator making a claim to have seen something happen. Claims are
// attached to attestations which can be thought of as 'the event' that
// will eventually be executed.
OracleClaimKey = []byte{0x4}
// OracleAttestationKey attestation details by nonce and validator address
// i.e. cosmosvaloper1ahx7f8wyertuus9r20284ej0asrs085case3kn
// An attestation can be thought of as the 'event to be executed' while
// the Claims are an individual validator saying that they saw an event
// occur the Attestation is 'the event' that multiple claims vote on and
// eventually executes
OracleAttestationKey = []byte{0x5}
// OutgoingTXPoolKey indexes the last nonce for the outgoing tx pool
OutgoingTXPoolKey = []byte{0x6}
// DenomiatorPrefix indexes token contract addresses from ETH on gravity
DenomiatorPrefix = []byte{0x8}
// SecondIndexOutgoingTXFeeKey indexes fee amounts by token contract address
SecondIndexOutgoingTXFeeKey = []byte{0x9}
// OutgoingTXBatchKey indexes outgoing tx batches under a nonce and token address
OutgoingTXBatchKey = []byte{0xa}
// OutgoingTXBatchBlockKey indexes outgoing tx batches under a block height and token address
OutgoingTXBatchBlockKey = []byte{0xb}
// BatchConfirmKey indexes validator confirmations by token contract address
BatchConfirmKey = []byte{0xe1}
// SecondIndexNonceByClaimKey indexes latest nonce for a given claim type
SecondIndexNonceByClaimKey = []byte{0xf}
// LastEventNonceByValidatorKey indexes lateset event nonce by validator
LastEventNonceByValidatorKey = []byte{0xf1}
// LastObservedEventNonceKey indexes the latest event nonce
LastObservedEventNonceKey = []byte{0xf2}
// SequenceKeyPrefix indexes different txids
SequenceKeyPrefix = []byte{0x7}
// KeyLastTXPoolID indexes the lastTxPoolID
KeyLastTXPoolID = append(SequenceKeyPrefix, []byte("lastTxPoolId")...)
// KeyLastOutgoingBatchID indexes the lastBatchID
KeyLastOutgoingBatchID = append(SequenceKeyPrefix, []byte("lastBatchId")...)
// KeyOrchestratorAddress indexes the validator keys for an orchestrator
KeyOrchestratorAddress = []byte{0xe8}
// KeyOutgoingLogicCall indexes the outgoing logic calls
KeyOutgoingLogicCall = []byte{0xde}
// KeyOutgoingLogicConfirm indexes the outgoing logic confirms
KeyOutgoingLogicConfirm = []byte{0xae}
// LastObservedEthereumBlockHeightKey indexes the latest Ethereum block height
LastObservedEthereumBlockHeightKey = []byte{0xf9}
// DenomToERC20Key prefixes the index of Cosmos originated asset denoms to ERC20s
DenomToERC20Key = []byte{0xf3}
// ERC20ToDenomKey prefixes the index of Cosmos originated assets ERC20s to denoms
ERC20ToDenomKey = []byte{0xf4}
// LastSlashedValsetNonce indexes the latest slashed valset nonce
LastSlashedValsetNonce = []byte{0xf5}
// LatestValsetNonce indexes the latest valset nonce
LatestValsetNonce = []byte{0xf6}
// LastSlashedBatchBlock indexes the latest slashed batch block height
LastSlashedBatchBlock = []byte{0xf7}
// LastUnBondingBlockHeight indexes the last validator unbonding block height
LastUnBondingBlockHeight = []byte{0xf8}
)
// GetOrchestratorAddressKey returns the following key format
// prefix
// [0xe8][cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn]
func GetOrchestratorAddressKey(orc sdk.AccAddress) []byte {
return append(KeyOrchestratorAddress, orc.Bytes()...)
}
// GetEthAddressKey returns the following key format
// prefix cosmos-validator
// [0x0][cosmosvaloper1ahx7f8wyertuus9r20284ej0asrs085case3kn]
func GetEthAddressKey(validator sdk.AccAddress) []byte {
return append(EthAddressKey, validator.Bytes()...)
}
// GetValsetKey returns the following key format
// prefix nonce
// [0x0][0 0 0 0 0 0 0 1]
func GetValsetKey(nonce uint64) []byte {
return append(ValsetRequestKey, UInt64Bytes(nonce)...)
}
// GetValsetConfirmKey returns the following key format
// prefix nonce validator-address
// [0x0][0 0 0 0 0 0 0 1][cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn]
// MARK finish-batches: this is where the key is created in the old (presumed working) code
func GetValsetConfirmKey(nonce uint64, validator sdk.AccAddress) []byte {
return append(ValsetConfirmKey, append(UInt64Bytes(nonce), validator.Bytes()...)...)
}
// GetClaimKey returns the following key format
// prefix type cosmos-validator-address nonce attestation-details-hash
// [0x0][0 0 0 1][cosmosvaloper1ahx7f8wyertuus9r20284ej0asrs085case3kn][0 0 0 0 0 0 0 1][fd1af8cec6c67fcf156f1b61fdf91ebc04d05484d007436e75342fc05bbff35a]
// The Claim hash identifies a unique event, for example it would have a event nonce, a sender and a receiver. Or an event nonce and a batch nonce. But
// the Claim is stored indexed with the claimer key to make sure that it is unique.
func GetClaimKey(details EthereumClaim) []byte {
var detailsHash []byte
if details != nil {
detailsHash = details.ClaimHash()
} else {
panic("No claim without details!")
}
claimTypeLen := len([]byte{byte(details.GetType())})
nonceBz := UInt64Bytes(details.GetEventNonce())
key := make([]byte, len(OracleClaimKey)+claimTypeLen+sdk.AddrLen+len(nonceBz)+len(detailsHash))
copy(key[0:], OracleClaimKey)
copy(key[len(OracleClaimKey):], []byte{byte(details.GetType())})
// TODO this is the delegate address, should be stored by the valaddress
copy(key[len(OracleClaimKey)+claimTypeLen:], details.GetClaimer().Bytes())
copy(key[len(OracleClaimKey)+claimTypeLen+sdk.AddrLen:], nonceBz)
copy(key[len(OracleClaimKey)+claimTypeLen+sdk.AddrLen+len(nonceBz):], detailsHash)
return key
}
// GetAttestationKey returns the following key format
// prefix nonce claim-details-hash
// [0x5][0 0 0 0 0 0 0 1][fd1af8cec6c67fcf156f1b61fdf91ebc04d05484d007436e75342fc05bbff35a]
// An attestation is an event multiple people are voting on, this function needs the claim
// details because each Attestation is aggregating all claims of a specific event, lets say
// validator X and validator y where making different claims about the same event nonce
// Note that the claim hash does NOT include the claimer address and only identifies an event
func GetAttestationKey(eventNonce uint64, claimHash []byte) []byte {
key := make([]byte, len(OracleAttestationKey)+len(UInt64Bytes(0))+len(claimHash))
copy(key[0:], OracleAttestationKey)
copy(key[len(OracleAttestationKey):], UInt64Bytes(eventNonce))
copy(key[len(OracleAttestationKey)+len(UInt64Bytes(0)):], claimHash)
return key
}
// GetAttestationKeyWithHash returns the following key format
// prefix nonce claim-details-hash
// [0x5][0 0 0 0 0 0 0 1][fd1af8cec6c67fcf156f1b61fdf91ebc04d05484d007436e75342fc05bbff35a]
// An attestation is an event multiple people are voting on, this function needs the claim
// details because each Attestation is aggregating all claims of a specific event, lets say
// validator X and validator y where making different claims about the same event nonce
// Note that the claim hash does NOT include the claimer address and only identifies an event
func GetAttestationKeyWithHash(eventNonce uint64, claimHash []byte) []byte {
key := make([]byte, len(OracleAttestationKey)+len(UInt64Bytes(0))+len(claimHash))
copy(key[0:], OracleAttestationKey)
copy(key[len(OracleAttestationKey):], UInt64Bytes(eventNonce))
copy(key[len(OracleAttestationKey)+len(UInt64Bytes(0)):], claimHash)
return key
}
// GetOutgoingTxPoolKey returns the following key format
// prefix id
// [0x6][0 0 0 0 0 0 0 1]
func GetOutgoingTxPoolKey(id uint64) []byte {
return append(OutgoingTXPoolKey, sdk.Uint64ToBigEndian(id)...)
}
// GetOutgoingTxBatchKey returns the following key format
// prefix nonce eth-contract-address
// [0xa][0 0 0 0 0 0 0 1][0xc783df8a850f42e7F7e57013759C285caa701eB6]
func GetOutgoingTxBatchKey(tokenContract string, nonce uint64) []byte {
return append(append(OutgoingTXBatchKey, []byte(tokenContract)...), UInt64Bytes(nonce)...)
}
// GetOutgoingTxBatchBlockKey returns the following key format
// prefix blockheight
// [0xb][0 0 0 0 2 1 4 3]
func GetOutgoingTxBatchBlockKey(block uint64) []byte {
return append(OutgoingTXBatchBlockKey, UInt64Bytes(block)...)
}
// GetBatchConfirmKey returns the following key format
// prefix eth-contract-address BatchNonce Validator-address
// [0xe1][0xc783df8a850f42e7F7e57013759C285caa701eB6][0 0 0 0 0 0 0 1][cosmosvaloper1ahx7f8wyertuus9r20284ej0asrs085case3kn]
// TODO this should be a sdk.AccAddress
func GetBatchConfirmKey(tokenContract string, batchNonce uint64, validator sdk.AccAddress) []byte {
a := append(UInt64Bytes(batchNonce), validator.Bytes()...)
b := append([]byte(tokenContract), a...)
c := append(BatchConfirmKey, b...)
return c
}
// GetFeeSecondIndexKey returns the following key format
// prefix eth-contract-address fee_amount
// [0x9][0xc783df8a850f42e7F7e57013759C285caa701eB6][1000000000]
func GetFeeSecondIndexKey(fee ERC20Token) []byte {
r := make([]byte, 1+ETHContractAddressLen+32)
// sdkInts have a size limit of 255 bits or 32 bytes
// therefore this will never panic and is always safe
amount := make([]byte, 32)
amount = fee.Amount.BigInt().FillBytes(amount)
// TODO this won't ever work fix it
copy(r[0:], SecondIndexOutgoingTXFeeKey)
copy(r[len(SecondIndexOutgoingTXFeeKey):], []byte(fee.Contract))
copy(r[len(SecondIndexOutgoingTXFeeKey)+len(fee.Contract):], amount)
return r
}
// GetLastEventNonceByValidatorKey indexes lateset event nonce by validator
// GetLastEventNonceByValidatorKey returns the following key format
// prefix cosmos-validator
// [0x0][cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn]
func GetLastEventNonceByValidatorKey(validator sdk.AccAddress) []byte {
return append(LastEventNonceByValidatorKey, validator.Bytes()...)
}
func GetDenomToERC20Key(denom string) []byte {
return append(DenomToERC20Key, []byte(denom)...)
}
func GetERC20ToDenomKey(erc20 string) []byte {
return append(ERC20ToDenomKey, []byte(erc20)...)
}
func GetOutgoingLogicCallKey(invalidationId []byte, invalidationNonce uint64) []byte {
a := append(KeyOutgoingLogicCall, invalidationId...)
return append(a, UInt64Bytes(invalidationNonce)...)
}
func GetLogicConfirmKey(invalidationId []byte, invalidationNonce uint64, validator sdk.AccAddress) []byte {
interm := append(KeyOutgoingLogicConfirm, invalidationId...)
interm = append(interm, UInt64Bytes(invalidationNonce)...)
return append(interm, validator.Bytes()...)
}