From ba1fd4ea2734afd7996a7a87bdaca10fbe9549a0 Mon Sep 17 00:00:00 2001 From: bruwbird Date: Thu, 15 Feb 2024 08:34:05 +0900 Subject: [PATCH] feat(labels): add labels package to handle labeling on-chain transactions fix(actions.go): change label used for labeling opening transaction to use Opening label from labels package fix(actions.go): change label used for labeling claim by CSV transaction to use ClaimByCsv label from labels package fix(actions.go): change label used for labeling claim by cooperative close transaction to use ClaimByCoop label from labels package fix(actions.go): add label to opening transaction in ValidateTxAndPayClaimInvoiceAction --- labels/labels.go | 36 ++++++++++++++++++++++++++++++++++++ swap/actions.go | 24 ++++++++++++++++-------- 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 labels/labels.go diff --git a/labels/labels.go b/labels/labels.go new file mode 100644 index 00000000..ad795ca4 --- /dev/null +++ b/labels/labels.go @@ -0,0 +1,36 @@ +package labels + +import "fmt" + +const ( + // peerswapLabelPattern is the pattern that peerswap uses to label on-chain transactions. + peerswapLabelPattern = "peerswap -- %s(swap id=%s)" + // opening is the label used for the opening transaction. + opening = "Opening" + // claimByInvoice is the label used for the claim by invoice transaction. + claimByInvoice = "ClaimByInvoice" + // claimByCoop is the label used for the claim by cooperative close transaction. + claimByCoop = "ClaimByCoop" + // ClaimByCsv is the label used for the claim by CSV transaction. + claimByCsv = "ClaimByCsv" +) + +// Opening returns the label used for the opening transaction. +func Opening(swapID string) string { + return fmt.Sprintf(peerswapLabelPattern, opening, swapID) +} + +// ClaimByInvoice returns the label used for the claim by invoice transaction. +func ClaimByInvoice(swapID string) string { + return fmt.Sprintf(peerswapLabelPattern, claimByInvoice, swapID) +} + +// ClaimByCoop returns the label used for the claim by cooperative close transaction. +func ClaimByCoop(swapID string) string { + return fmt.Sprintf(peerswapLabelPattern, claimByCoop, swapID) +} + +// ClaimByCsv returns the label used for the claim by CSV transaction. +func ClaimByCsv(swapID string) string { + return fmt.Sprintf(peerswapLabelPattern, claimByCsv, swapID) +} diff --git a/swap/actions.go b/swap/actions.go index 046888c3..b9adb54b 100644 --- a/swap/actions.go +++ b/swap/actions.go @@ -13,14 +13,14 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/elementsproject/peerswap/isdev" + "github.com/elementsproject/peerswap/labels" "github.com/elementsproject/peerswap/lightning" "github.com/elementsproject/peerswap/messages" ) const ( - BitcoinCsv = 1008 - LiquidCsv = 60 - peerswapLabel = "peerswap" + BitcoinCsv = 1008 + LiquidCsv = 60 ) type CheckRequestWrapperAction struct { @@ -195,7 +195,7 @@ func (s *ClaimSwapTransactionWithPreimageAction) Execute(services *SwapServices, return Event_OnRetry } swap.ClaimTxId = txId - err = wallet.LabelTransaction(txId, peerswapLabel) + err = wallet.LabelTransaction(txId, labels.Opening(swap.GetId().String())) if err != nil { log.Infof("Error labeling trnasaction %v", err) } @@ -254,7 +254,7 @@ func (c *CreateAndBroadcastOpeningTransaction) Execute(services *SwapServices, s // todo: idempotent states return swap.HandleError(err) } - err = wallet.LabelTransaction(txId, peerswapLabel) + err = wallet.LabelTransaction(txId, labels.Opening(swap.GetId().String())) if err != nil { log.Infof("Error labeling trnasaction %v", err) } @@ -446,7 +446,7 @@ func (c *ClaimSwapTransactionWithCsv) Execute(services *SwapServices, swap *Swap return Event_OnRetry } swap.ClaimTxId = txId - err = wallet.LabelTransaction(txId, peerswapLabel) + err = wallet.LabelTransaction(txId, labels.ClaimByCsv(swap.GetId().String())) if err != nil { log.Infof("Error labeling trnasaction %v", err) } @@ -476,7 +476,7 @@ func (c *ClaimSwapTransactionCoop) Execute(services *SwapServices, swap *SwapDat return swap.HandleError(err) } swap.ClaimTxId = txId - err = wallet.LabelTransaction(txId, peerswapLabel) + err = wallet.LabelTransaction(txId, labels.ClaimByCoop(swap.GetId().String())) if err != nil { log.Infof("Error labeling trnasaction %v", err) } @@ -717,7 +717,7 @@ type ValidateTxAndPayClaimInvoiceAction struct{} func (p *ValidateTxAndPayClaimInvoiceAction) Execute(services *SwapServices, swap *SwapData) EventType { lc := services.lightning - onchain, _, validator, err := services.getOnChainServices(swap.GetChain()) + onchain, wallet, validator, err := services.getOnChainServices(swap.GetChain()) if err != nil { return swap.HandleError(err) } @@ -730,6 +730,14 @@ func (p *ValidateTxAndPayClaimInvoiceAction) Execute(services *SwapServices, swa if !ok { return swap.HandleError(errors.New("tx is not valid")) } + txId, err := validator.TxIdFromHex(swap.OpeningTxHex) + if err != nil { + return swap.HandleError(err) + } + err = wallet.LabelTransaction(txId, labels.Opening(swap.GetId().String())) + if err != nil { + log.Infof("Error labeling trnasaction %v", err) + } var retryTime time.Duration = 120 * time.Second var interval time.Duration = 10 * time.Second