Skip to content

Commit

Permalink
labels: add labels package
Browse files Browse the repository at this point in the history
Add labels package to handle labeling on-chain transactions.
* Opening label
* claim by CSV label
* claim by cooperative close label
  • Loading branch information
YusukeShimizu committed Feb 15, 2024
1 parent eea02b3 commit 22c7144
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 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)
}
37 changes: 25 additions & 12 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,9 +195,10 @@ func (s *ClaimSwapTransactionWithPreimageAction) Execute(services *SwapServices,
return Event_OnRetry
}
swap.ClaimTxId = txId
err = wallet.LabelTransaction(txId, peerswapLabel)
err = wallet.LabelTransaction(txId, labels.ClaimByInvoice(swap.GetId().Short()))
if err != nil {
log.Infof("Error labeling trnasaction %v", err)
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByInvoice(swap.GetId().Short()), err)
}
}

Expand Down Expand Up @@ -254,9 +255,10 @@ 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().Short()))
if err != nil {
log.Infof("Error labeling trnasaction %v", err)
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.Opening(swap.GetId().Short()), err)
}
startingHeight, err := txWatcher.GetBlockHeight()
if err != nil {
Expand Down Expand Up @@ -446,9 +448,10 @@ 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().Short()))
if err != nil {
log.Infof("Error labeling trnasaction %v", err)
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByCsv(swap.GetId().Short()), err)
}
}

Expand Down Expand Up @@ -476,9 +479,10 @@ 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().Short()))
if err != nil {
log.Infof("Error labeling trnasaction %v", err)
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.ClaimByCoop(swap.GetId().Short()), err)
}
}

Expand Down Expand Up @@ -717,7 +721,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 +734,15 @@ 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().Short()))
if err != nil {
log.Infof("Error labeling transaction. txid: %s, label: %s, error: %v",
txId, labels.Opening(swap.GetId().Short()), err)
}

var retryTime time.Duration = 120 * time.Second
var interval time.Duration = 10 * time.Second
Expand Down
5 changes: 5 additions & 0 deletions swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func (s *SwapId) FromString(str string) error {
return nil
}

// Short returns a shortened version of the id suitable for use in observing.
func (s *SwapId) Short() string {
return s.String()[:6]
}

func ParseSwapIdFromString(str string) (*SwapId, error) {
data, err := hex.DecodeString(str)
if err != nil {
Expand Down

0 comments on commit 22c7144

Please sign in to comment.