-
Notifications
You must be signed in to change notification settings - Fork 153
/
types.go
192 lines (159 loc) · 4.9 KB
/
types.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
package types
import (
"encoding/hex"
"fmt"
"github.com/babylonchain/babylon/btctxformatter"
"github.com/babylonchain/babylon/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Semantically valid checkpoint submission with:
// - valid submitter address
// - at least 2 parsed proof
// Modelling proofs as separate Proof1 and Proof2, as this is more explicit than
// []*ParsedProof.
type RawCheckpointSubmission struct {
Reporter sdk.AccAddress
Proof1 ParsedProof
Proof2 ParsedProof
CheckpointData btctxformatter.RawBtcCheckpoint
}
// SubmissionBtcInfo encapsualte important information about submission posistion
// on btc ledger
type SubmissionBtcInfo struct {
SubmissionKey SubmissionKey
// Depth of the oldest btc header of the submission
OldestBlockDepth uint64
// Depth of the youngest btc header of the submission
YoungestBlockDepth uint64
// Hash of the youngest btc header of the submission
YoungestBlockHash types.BTCHeaderHashBytes
// Index of the lowest index transaction in youngest submission block
YoungestBlockLowestTxIdx uint32
}
func NewRawCheckpointSubmission(
a sdk.AccAddress,
p1 ParsedProof,
p2 ParsedProof,
checkpointData btctxformatter.RawBtcCheckpoint,
) RawCheckpointSubmission {
r := RawCheckpointSubmission{
Reporter: a,
Proof1: p1,
Proof2: p2,
CheckpointData: checkpointData,
}
return r
}
func (s *RawCheckpointSubmission) GetProofs() []*ParsedProof {
return []*ParsedProof{&s.Proof1, &s.Proof2}
}
func (s *RawCheckpointSubmission) GetFirstBlockHash() types.BTCHeaderHashBytes {
return s.Proof1.BlockHash
}
func (s *RawCheckpointSubmission) GetSecondBlockHash() types.BTCHeaderHashBytes {
return s.Proof2.BlockHash
}
func (s *RawCheckpointSubmission) InOneBlock() bool {
fh := s.GetFirstBlockHash()
sh := s.GetSecondBlockHash()
return fh.Eq(&sh)
}
func toTransactionKey(p *ParsedProof) TransactionKey {
hashBytes := p.BlockHash
return TransactionKey{
Index: p.TransactionIdx,
Hash: &hashBytes,
}
}
func (rsc *RawCheckpointSubmission) GetSubmissionKey() SubmissionKey {
var keys []*TransactionKey
k1 := toTransactionKey(&rsc.Proof1)
keys = append(keys, &k1)
k2 := toTransactionKey(&rsc.Proof2)
keys = append(keys, &k2)
return SubmissionKey{
Key: keys,
}
}
func (rsc *RawCheckpointSubmission) GetSubmissionData(epochNum uint64, txsInfo []*TransactionInfo) SubmissionData {
return SubmissionData{
VigilanteAddresses: &CheckpointAddresses{
Reporter: rsc.Reporter.Bytes(),
Submitter: rsc.CheckpointData.SubmitterAddress,
},
TxsInfo: txsInfo,
Epoch: epochNum,
}
}
func (sk *SubmissionKey) GetKeyBlockHashes() []*types.BTCHeaderHashBytes {
var hashes []*types.BTCHeaderHashBytes
for _, k := range sk.Key {
h := k.Hash
hashes = append(hashes, h)
}
return hashes
}
func NewEmptyEpochData() EpochData {
return EpochData{
Key: []*SubmissionKey{},
Status: Submitted,
}
}
func (s *EpochData) AppendKey(k SubmissionKey) {
key := &k
s.Key = append(s.Key, key)
}
// HappenedAfter returns true if `this` submission happened after `that` submission
func (submission *SubmissionBtcInfo) HappenedAfter(parentEpochSubmission *SubmissionBtcInfo) bool {
return submission.OldestBlockDepth < parentEpochSubmission.YoungestBlockDepth
}
// SubmissionDepth return depth of the submission. Due to the fact that submissions
// are splitted between several btc blocks, in Babylon subbmission depth is the depth
// of the youngest btc block
func (submission *SubmissionBtcInfo) SubmissionDepth() uint64 {
return submission.YoungestBlockDepth
}
func (newSubmission *SubmissionBtcInfo) IsBetterThan(currentBestSubmission *SubmissionBtcInfo) bool {
if newSubmission.SubmissionDepth() > currentBestSubmission.SubmissionDepth() {
return true
}
if newSubmission.SubmissionDepth() < currentBestSubmission.SubmissionDepth() {
return false
}
// at this point we know that both submissions youngest part happens to be in
// the same block. To resolve the tie we need to take into account index of
// latest transaction of the submissions
return newSubmission.YoungestBlockLowestTxIdx < currentBestSubmission.YoungestBlockLowestTxIdx
}
func NewTransactionInfo(txKey *TransactionKey, txBytes []byte, proof []byte) *TransactionInfo {
return &TransactionInfo{
Key: txKey,
Transaction: txBytes,
Proof: proof,
}
}
func (ti *TransactionInfo) ValidateBasic() error {
if ti.Key == nil {
return fmt.Errorf("key in TransactionInfo is nil")
}
if ti.Transaction == nil {
return fmt.Errorf("transaction in TransactionInfo is nil")
}
if ti.Proof == nil {
return fmt.Errorf("proof in TransactionInfo is nil")
}
return nil
}
func NewSpvProofFromHexBytes(c codec.Codec, proof string) (*BTCSpvProof, error) {
bytes, err := hex.DecodeString(proof)
if err != nil {
return nil, err
}
var p BTCSpvProof
err = c.Unmarshal(bytes, &p)
if err != nil {
return nil, err
}
return &p, nil
}