-
Notifications
You must be signed in to change notification settings - Fork 12
/
actions.go
288 lines (271 loc) · 12.3 KB
/
actions.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package contracts
import (
"encoding/json"
"fmt"
"sync/atomic"
"time"
rhp2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"go.sia.tech/hostd/alerts"
"go.uber.org/zap"
)
// An action determines what lifecycle event should be performed on a contract.
const (
ActionBroadcastFormation = "formation"
ActionReject = "reject"
ActionBroadcastFinalRevision = "revision"
ActionBroadcastResolution = "resolve"
ActionExpire = "expire"
)
func (cm *ContractManager) buildStorageProof(id types.FileContractID, filesize uint64, index uint64, log *zap.Logger) (types.StorageProof, error) {
if filesize == 0 {
return types.StorageProof{
ParentID: id,
}, nil
}
sectorIndex := index / rhp2.LeavesPerSector
segmentIndex := index % rhp2.LeavesPerSector
roots, err := cm.getSectorRoots(id)
if err != nil {
return types.StorageProof{}, fmt.Errorf("failed to get sector roots: %w", err)
} else if uint64(len(roots)) < sectorIndex {
log.Error("failed to build storage proof. invalid root index", zap.Uint64("sectorIndex", sectorIndex), zap.Uint64("segmentIndex", segmentIndex), zap.Int("rootsLength", len(roots)))
return types.StorageProof{}, fmt.Errorf("invalid root index")
}
root := roots[sectorIndex]
sector, err := cm.storage.Read(root)
if err != nil {
log.Error("failed to build storage proof. unable to read sector data", zap.Error(err), zap.Stringer("sectorRoot", root))
return types.StorageProof{}, fmt.Errorf("failed to read sector data")
}
segmentProof := rhp2.ConvertProofOrdering(rhp2.BuildProof(sector, segmentIndex, segmentIndex+1, nil), segmentIndex)
sectorProof := rhp2.ConvertProofOrdering(rhp2.BuildSectorRangeProof(roots, sectorIndex, sectorIndex+1), sectorIndex)
sp := types.StorageProof{
ParentID: id,
Proof: append(segmentProof, sectorProof...),
}
copy(sp.Leaf[:], sector[segmentIndex*rhp2.LeafSize:])
return sp, nil
}
// processActions performs lifecycle actions on contracts. Triggered by a
// consensus change, changes are processed in the order they were received.
func (cm *ContractManager) processActions() {
for {
select {
case height := <-cm.processQueue:
err := func() error {
done, err := cm.tg.Add()
if err != nil {
return nil
}
defer done()
err = cm.store.ContractAction(height, cm.handleContractAction)
if err != nil {
return fmt.Errorf("failed to process contract actions: %w", err)
} else if err = cm.store.ExpireContractSectors(height); err != nil {
return fmt.Errorf("failed to expire contract sectors: %w", err)
}
return nil
}()
if err != nil {
cm.log.Panic("failed to process contract actions", zap.Error(err), zap.Stack("stack"))
}
atomic.StoreUint64(&cm.blockHeight, height)
case <-cm.tg.Done():
return
}
}
}
// handleContractAction performs a lifecycle action on a contract.
func (cm *ContractManager) handleContractAction(id types.FileContractID, height uint64, action string) {
log := cm.log.Named("lifecycle").With(zap.String("contractID", id.String()), zap.Uint64("height", height), zap.String("action", action))
contract, err := cm.store.Contract(id)
if err != nil {
log.Error("failed to get contract", zap.Error(err))
return
}
log = log.With(zap.Uint64("revisionNumber", contract.Revision.RevisionNumber), zap.Uint64("size", contract.Revision.Filesize), zap.Stringer("merkleRoot", contract.Revision.FileMerkleRoot), zap.Uint64("scanHeight", cm.chain.TipState().Index.Height))
log.Debug("performing contract action", zap.Uint64("negotiationHeight", contract.NegotiationHeight), zap.Uint64("windowStart", contract.Revision.WindowStart), zap.Uint64("windowEnd", contract.Revision.WindowEnd))
start := time.Now()
cs := cm.chain.TipState()
// helper to register a contract alert
registerContractAlert := func(severity alerts.Severity, message string, err error) {
data := map[string]any{
"contractID": id,
"blockHeight": height,
}
if err != nil {
data["error"] = err.Error()
}
cm.alerts.Register(alerts.Alert{
ID: types.Hash256(id),
Severity: severity,
Message: message,
Data: data,
Timestamp: time.Now(),
})
}
switch action {
case ActionBroadcastFormation:
if (height-contract.NegotiationHeight)%3 != 0 {
// debounce formation broadcasts to prevent spamming
log.Debug("skipping rebroadcast", zap.Uint64("negotiationHeight", contract.NegotiationHeight))
return
}
formationSet, err := cm.store.ContractFormationSet(id)
if err != nil {
log.Error("failed to get formation set", zap.Error(err))
return
} else if err := cm.tpool.AcceptTransactionSet(formationSet); err != nil {
log.Error("failed to broadcast formation transaction", zap.Error(err))
return
}
log.Info("rebroadcast formation transaction", zap.String("transactionID", formationSet[len(formationSet)-1].ID().String()))
case ActionBroadcastFinalRevision:
if (contract.Revision.WindowStart-height)%3 != 0 {
// debounce final revision broadcasts to prevent spamming
log.Debug("skipping revision", zap.Uint64("windowStart", contract.Revision.WindowStart))
return
}
revisionTxn := types.Transaction{
FileContractRevisions: []types.FileContractRevision{contract.Revision},
Signatures: []types.TransactionSignature{
{
ParentID: types.Hash256(contract.Revision.ParentID),
CoveredFields: types.CoveredFields{FileContractRevisions: []uint64{0}},
Signature: contract.RenterSignature[:],
},
{
ParentID: types.Hash256(contract.Revision.ParentID),
CoveredFields: types.CoveredFields{FileContractRevisions: []uint64{0}},
Signature: contract.HostSignature[:],
PublicKeyIndex: 1,
},
},
}
fee := cm.tpool.RecommendedFee().Mul64(1000)
revisionTxn.MinerFees = append(revisionTxn.MinerFees, fee)
toSign, discard, err := cm.wallet.FundTransaction(&revisionTxn, fee)
if err != nil {
log.Error("failed to fund revision transaction", zap.Error(err))
return
}
defer discard()
if err := cm.wallet.SignTransaction(cs, &revisionTxn, toSign, types.CoveredFields{WholeTransaction: true}); err != nil {
log.Error("failed to sign revision transaction", zap.Error(err))
return
} else if err := cm.tpool.AcceptTransactionSet([]types.Transaction{revisionTxn}); err != nil {
log.Error("failed to broadcast revision transaction", zap.Error(err))
return
}
log.Info("broadcast final revision", zap.Uint64("revisionNumber", contract.Revision.RevisionNumber), zap.String("transactionID", revisionTxn.ID().String()))
case ActionBroadcastResolution:
if (height-contract.Revision.WindowStart)%3 != 0 {
// debounce resolution broadcasts to prevent spamming
log.Debug("skipping resolution", zap.Uint64("windowStart", contract.Revision.WindowStart))
return
}
validPayout, missedPayout := contract.Revision.ValidHostPayout(), contract.Revision.MissedHostPayout()
if missedPayout.Cmp(validPayout) >= 0 {
log.Info("skipping storage proof, no benefit to host", zap.String("validPayout", validPayout.ExactString()), zap.String("missedPayout", missedPayout.ExactString()))
return
}
// get the block before the proof window starts
windowStart, err := cm.chain.IndexAtHeight(contract.Revision.WindowStart - 1)
if err != nil {
log.Error("failed to get chain index at height", zap.Uint64("height", contract.Revision.WindowStart-1), zap.Error(err))
return
}
// get the proof leaf index
leafIndex := cs.StorageProofLeafIndex(contract.Revision.Filesize, windowStart.ID, contract.Revision.ParentID)
sp, err := cm.buildStorageProof(contract.Revision.ParentID, contract.Revision.Filesize, leafIndex, log.Named("buildStorageProof"))
if err != nil {
log.Error("failed to build storage proof", zap.Error(err))
registerContractAlert(alerts.SeverityError, "Failed to build storage proof", err)
return
}
// TODO: consider cost of broadcasting the proof
fee := cm.tpool.RecommendedFee().Mul64(1000)
resolutionTxnSet := []types.Transaction{
{
// intermediate funding transaction is required by siad because
// transactions with storage proofs cannot have change outputs
SiacoinOutputs: []types.SiacoinOutput{
{Address: cm.wallet.Address(), Value: fee},
},
},
{
MinerFees: []types.Currency{fee},
StorageProofs: []types.StorageProof{sp},
},
}
intermediateToSign, discard, err := cm.wallet.FundTransaction(&resolutionTxnSet[0], fee)
if err != nil {
log.Error("failed to fund resolution transaction", zap.Error(err))
registerContractAlert(alerts.SeverityError, "Failed to fund resolution transaction", err)
return
}
defer discard()
// add the intermediate output to the proof transaction
resolutionTxnSet[1].SiacoinInputs = append(resolutionTxnSet[1].SiacoinInputs, types.SiacoinInput{
ParentID: resolutionTxnSet[0].SiacoinOutputID(0),
UnlockConditions: cm.wallet.UnlockConditions(),
})
proofToSign := []types.Hash256{types.Hash256(resolutionTxnSet[1].SiacoinInputs[0].ParentID)}
start = time.Now()
if err := cm.wallet.SignTransaction(cs, &resolutionTxnSet[0], intermediateToSign, types.CoveredFields{WholeTransaction: true}); err != nil { // sign the intermediate transaction
log.Error("failed to sign resolution intermediate transaction", zap.Error(err))
return
} else if err := cm.wallet.SignTransaction(cs, &resolutionTxnSet[1], proofToSign, types.CoveredFields{WholeTransaction: true}); err != nil { // sign the proof transaction
log.Error("failed to sign resolution transaction", zap.Error(err))
return
} else if err := cm.tpool.AcceptTransactionSet(resolutionTxnSet); err != nil { // broadcast the transaction set
buf, _ := json.Marshal(resolutionTxnSet)
log.Error("failed to broadcast resolution transaction set", zap.Error(err), zap.ByteString("transactionSet", buf))
registerContractAlert(alerts.SeverityError, "Failed to broadcast resolution transaction set", err)
return
}
cm.alerts.Dismiss(types.Hash256(id)) // dismiss any previous failure alerts
log.Info("broadcast storage proof", zap.String("transactionID", resolutionTxnSet[1].ID().String()), zap.Duration("elapsed", time.Since(start)))
case ActionReject:
if err := cm.store.ExpireContract(id, ContractStatusRejected); err != nil {
log.Error("failed to set contract status", zap.Error(err))
}
log.Info("contract rejected", zap.Uint64("negotiationHeight", contract.NegotiationHeight))
case ActionExpire:
validPayout, missedPayout := contract.Revision.ValidHostPayout(), contract.Revision.MissedHostPayout()
switch {
case !contract.FormationConfirmed:
// if the contract was never confirmed, nothing was ever lost or
// gained
if err := cm.store.ExpireContract(id, ContractStatusRejected); err != nil {
log.Error("failed to set contract status", zap.Error(err))
}
case validPayout.Cmp(missedPayout) <= 0 || contract.ResolutionHeight != 0:
// if the host valid payout is less than or equal to the missed
// payout or if a resolution was confirmed, the contract was
// successful
if err := cm.store.ExpireContract(id, ContractStatusSuccessful); err != nil {
log.Error("failed to set contract status", zap.Error(err))
}
payout := validPayout
if contract.ResolutionHeight != 0 {
payout = missedPayout
}
log.Info("contract successful", zap.String("payout", payout.ExactString()))
case validPayout.Cmp(missedPayout) > 0 && contract.ResolutionHeight == 0:
// if the host valid payout is greater than the missed payout and a
// proof was not broadcast, the contract failed
if err := cm.store.ExpireContract(id, ContractStatusFailed); err != nil {
log.Error("failed to set contract status", zap.Error(err))
}
registerContractAlert(alerts.SeverityError, "Contract failed without storage proof", nil)
log.Error("contract failed, revenue lost", zap.Uint64("windowStart", contract.Revision.WindowStart), zap.Uint64("windowEnd", contract.Revision.WindowEnd), zap.String("validPayout", validPayout.ExactString()), zap.String("missedPayout", missedPayout.ExactString()))
default:
log.Panic("unrecognized contract state", zap.Stack("stack"), zap.String("validPayout", validPayout.ExactString()), zap.String("missedPayout", missedPayout.ExactString()), zap.Uint64("resolutionHeight", contract.ResolutionHeight), zap.Bool("formationConfirmed", contract.FormationConfirmed))
}
default:
log.Panic("unrecognized contract action", zap.Stack("stack"))
}
log.Debug("contract action completed", zap.Duration("elapsed", time.Since(start)))
}