-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
242 lines (207 loc) · 6.91 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
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
package atomicslasher
import (
"fmt"
"sync"
asig "github.com/babylonlabs-io/babylon/crypto/schnorr-adaptor-signature"
bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
type SlashingPath int
const (
slashStakingTx SlashingPath = iota
slashUnbondingTx
)
type SlashingTxInfo struct {
path SlashingPath
StakingTxHash chainhash.Hash
SlashingMsgTx *wire.MsgTx
}
func NewSlashingTxInfo(path SlashingPath, stakingTxHash chainhash.Hash, slashingMsgTx *wire.MsgTx) *SlashingTxInfo {
return &SlashingTxInfo{
path: path,
StakingTxHash: stakingTxHash,
SlashingMsgTx: slashingMsgTx,
}
}
func (s *SlashingTxInfo) IsSlashStakingTx() bool {
return s.path == slashStakingTx
}
type TrackedDelegation struct {
StakingTxHash chainhash.Hash
SlashingTxHash chainhash.Hash
UnbondingSlashingTxHash chainhash.Hash
}
func NewTrackedBTCDelegation(btcDel *bstypes.BTCDelegationResponse) (*TrackedDelegation, error) {
stakingTxHash, _, err := bbn.NewBTCTxFromHex(btcDel.StakingTxHex)
if err != nil {
return nil, err
}
slashTx, err := bstypes.NewBTCSlashingTxFromHex(btcDel.SlashingTxHex)
if err != nil {
return nil, err
}
slashingTx, err := slashTx.ToMsgTx()
if err != nil {
return nil, err
}
unbondingSlashTx, err := bstypes.NewBTCSlashingTxFromHex(btcDel.UndelegationResponse.SlashingTxHex)
if err != nil {
return nil, err
}
unbondingSlashingTx, err := unbondingSlashTx.ToMsgTx()
if err != nil {
return nil, err
}
return &TrackedDelegation{
StakingTxHash: stakingTxHash.TxHash(),
SlashingTxHash: slashingTx.TxHash(),
UnbondingSlashingTxHash: unbondingSlashingTx.TxHash(),
}, nil
}
// BTCDelegationIndex is an index of BTC delegations that support efficient
// search of BTC delegations by using staking/slashing/unbondingslashing tx
type BTCDelegationIndex struct {
sync.Mutex
// staking tx hash -> tracked BTC delegation
delMap map[chainhash.Hash]*TrackedDelegation
// slashing tx hash -> staking tx hash of the corresponding BTC delegation
slashingTxMap map[chainhash.Hash]chainhash.Hash
// unbonding slashing tx hash -> staking tx hash of the corresponding BTC delegation
unbondingSlashingTxMap map[chainhash.Hash]chainhash.Hash
}
// NewBTCDelegationIndex returns an empty BTC delegation index
func NewBTCDelegationIndex() *BTCDelegationIndex {
return &BTCDelegationIndex{
delMap: make(map[chainhash.Hash]*TrackedDelegation),
slashingTxMap: make(map[chainhash.Hash]chainhash.Hash),
unbondingSlashingTxMap: make(map[chainhash.Hash]chainhash.Hash),
}
}
// Get returns the tracked delegation for the given staking tx hash or nil if not found.
func (bdi *BTCDelegationIndex) Get(stakingTxHash chainhash.Hash) *TrackedDelegation {
bdi.Lock()
defer bdi.Unlock()
del, ok := bdi.delMap[stakingTxHash]
if !ok {
return nil
}
return del
}
func (bdi *BTCDelegationIndex) Add(trackedDel *TrackedDelegation) {
bdi.Lock()
defer bdi.Unlock()
// ensure the BTC delegation is not known yet
if _, ok := bdi.delMap[trackedDel.StakingTxHash]; ok {
return
}
// at this point, the BTC delegation is not known, add it
bdi.delMap[trackedDel.StakingTxHash] = trackedDel
// track slashing tx and unbonding slashing tx
bdi.slashingTxMap[trackedDel.SlashingTxHash] = trackedDel.StakingTxHash
bdi.unbondingSlashingTxMap[trackedDel.UnbondingSlashingTxHash] = trackedDel.StakingTxHash
}
func (bdi *BTCDelegationIndex) Remove(stakingTxHash chainhash.Hash) {
bdi.Lock()
defer bdi.Unlock()
del, ok := bdi.delMap[stakingTxHash]
if !ok {
return
}
delete(bdi.delMap, stakingTxHash)
delete(bdi.slashingTxMap, del.SlashingTxHash)
delete(bdi.unbondingSlashingTxMap, del.UnbondingSlashingTxHash)
}
func (bdi *BTCDelegationIndex) FindSlashedBTCDelegation(txHash chainhash.Hash) (*TrackedDelegation, SlashingPath) {
bdi.Lock()
defer bdi.Unlock()
if stakingTxHash, ok := bdi.slashingTxMap[txHash]; ok {
return bdi.delMap[stakingTxHash], slashStakingTx
}
if stakingTxHash, ok := bdi.unbondingSlashingTxMap[txHash]; ok {
return bdi.delMap[stakingTxHash], slashUnbondingTx
}
return nil, -1
}
// parseSlashingTxWitness is a private helper function for extracting
// the PK/signature pairs of covenant members and the index of the
// slashed finality provider who signs the slashing tx
// TODO: fuzz test
func parseSlashingTxWitness(
witnessStack wire.TxWitness,
covPKs []bbn.BIP340PubKey,
fpPKs []bbn.BIP340PubKey,
) (map[string]*bbn.BIP340Signature, int, *bbn.BIP340PubKey, error) {
// sort covenant PKs and finality provider PKs as per the tx script structure
orderedCovPKs := bbn.SortBIP340PKs(covPKs)
orderedfpPKs := bbn.SortBIP340PKs(fpPKs)
// decode covenant signatures
covSigMap := make(map[string]*bbn.BIP340Signature)
covWitnessStack := witnessStack[0:len(orderedCovPKs)]
for i := range covWitnessStack {
if len(covWitnessStack[i]) == 0 {
// this covenant member does not sign, skip
continue
}
sig, err := bbn.NewBIP340Signature(covWitnessStack[i])
if err != nil {
return nil, 0, nil, err
}
covSigMap[covPKs[i].MarshalHex()] = sig
}
// decode finality provider signatures
var fpIdx int
var fpPK *bbn.BIP340PubKey
fpWitnessStack := witnessStack[len(orderedCovPKs) : len(orderedCovPKs)+len(orderedfpPKs)]
for i := range fpWitnessStack {
if len(fpWitnessStack[i]) != 0 {
fpIdx = i
fpPK = &fpPKs[i]
break
}
}
return covSigMap, fpIdx, fpPK, nil
}
// TODO: fuzz test
func tryExtractFPSK(
covSigMap map[string]*bbn.BIP340Signature,
fpIdx int,
fpPK *bbn.BIP340PubKey,
covASigLists []*bstypes.CovenantAdaptorSignatures,
) (*btcec.PrivateKey, error) {
// try to recover
for _, covASigList := range covASigLists {
// find the covenant Schnorr signature in the map
covSchnorrSig, ok := covSigMap[covASigList.CovPk.MarshalHex()]
if !ok {
// this covenant member does not sign adaptor signature
continue
}
// find the covenant adaptor signature
covSchnorrBTCSig, err := covSchnorrSig.ToBTCSig()
if err != nil {
return nil, err
}
covAdaptorSigBytes := covASigList.AdaptorSigs[fpIdx]
covAdaptorSig, err := asig.NewAdaptorSignatureFromBytes(covAdaptorSigBytes)
if err != nil {
return nil, err
}
// try to recover the finality provider SK
fpSK := covAdaptorSig.Recover(covSchnorrBTCSig).ToBTCSK()
actualfpPK := bbn.NewBIP340PubKeyFromBTCPK(fpSK.PubKey())
if !fpPK.Equals(actualfpPK) {
// this covenant member must have colluded with finality provider and
// signed a new Schnorr signature. try the next covenant member
// TODO: decide what to do here
continue
}
// successfully extracted finality provider's SK, return
return fpSK, nil
}
// at this point, all signed covenant members must have colluded and
// signed Schnorr signatures for the slashing tx
return nil, fmt.Errorf("failed to extract finality provider's SK")
}