/
key.go
103 lines (90 loc) · 4.49 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
package types
import (
"github.com/cosmos/cosmos-sdk/types/address"
)
const (
// ModuleName is the name of the module
ModuleName = "oracle"
// 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 (
// 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{0x1}
// 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{0x2}
CurrentEpochKey = []byte{0x3}
CurrentPricesKey = []byte{0x4}
CurrentHoldersKey = []byte{0x5}
)
// 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 Claim) []byte {
var detailsHash []byte
if details != nil {
detailsHash = details.ClaimHash()
} else {
panic("No claim without details!")
}
claimTypeLen := len([]byte{byte(details.GetType())})
epochBz := UInt64Bytes(details.GetEpoch())
key := make([]byte, len(OracleClaimKey)+claimTypeLen+address.Len+len(epochBz)+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())
copy(key[len(OracleClaimKey)+claimTypeLen+address.Len:], epochBz)
copy(key[len(OracleClaimKey)+claimTypeLen+address.Len+len(epochBz):], 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(epoch uint64, details Claim) []byte {
var claimHash []byte
if details != nil {
claimHash = details.ClaimHash()
} else {
panic("No claim without details!")
}
key := make([]byte, len(OracleAttestationKey)+len(UInt64Bytes(0))+len(claimHash))
copy(key[0:], OracleAttestationKey)
copy(key[len(OracleAttestationKey):], UInt64Bytes(epoch))
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(epoch uint64, claimHash []byte) []byte {
key := make([]byte, len(OracleAttestationKey)+len(UInt64Bytes(0))+len(claimHash))
copy(key[0:], OracleAttestationKey)
copy(key[len(OracleAttestationKey):], UInt64Bytes(epoch))
copy(key[len(OracleAttestationKey)+len(UInt64Bytes(0)):], claimHash)
return key
}