Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alerts for failed contract resolutions #313

Merged
merged 2 commits into from Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 24 additions & 11 deletions host/contracts/actions.go
Expand Up @@ -10,7 +10,6 @@ import (
"go.sia.tech/core/types"
"go.sia.tech/hostd/alerts"
"go.uber.org/zap"
"lukechampine.com/frand"
)

// An action determines what lifecycle event should be performed on a contract.
Expand Down Expand Up @@ -99,6 +98,25 @@ func (cm *ContractManager) handleContractAction(id types.FileContractID, height
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 {
Expand Down Expand Up @@ -178,6 +196,7 @@ func (cm *ContractManager) handleContractAction(id types.FileContractID, height
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
}

Expand All @@ -199,6 +218,7 @@ func (cm *ContractManager) handleContractAction(id types.FileContractID, height
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()
Expand All @@ -219,8 +239,10 @@ func (cm *ContractManager) handleContractAction(id types.FileContractID, height
} 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 {
Expand Down Expand Up @@ -254,16 +276,7 @@ func (cm *ContractManager) handleContractAction(id types.FileContractID, height
if err := cm.store.ExpireContract(id, ContractStatusFailed); err != nil {
log.Error("failed to set contract status", zap.Error(err))
}
cm.alerts.Register(alerts.Alert{
ID: frand.Entropy256(),
Severity: alerts.SeverityWarning,
Message: "Contract failed without storage proof",
Data: map[string]any{
"contractID": id,
"blockHeight": height,
},
Timestamp: time.Now(),
})
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))
Expand Down