Skip to content

Commit

Permalink
feat(labels): add labels package to handle labeling on-chain transact…
Browse files Browse the repository at this point in the history
…ions

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
  • Loading branch information
YusukeShimizu committed Feb 14, 2024
1 parent eea02b3 commit ba1fd4e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
36 changes: 36 additions & 0 deletions labels/labels.go
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 16 additions & 8 deletions swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit ba1fd4e

Please sign in to comment.