Skip to content

Commit

Permalink
electrum: proper log output of tx watcher
Browse files Browse the repository at this point in the history
To optimise the log output of the transaction watcher,
only output to the required debug log.
Also, swap.ErrDoesNotExist is now handled in the callback.
  • Loading branch information
YusukeShimizu committed May 31, 2024
1 parent d1f6a89 commit f2ee818
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 14 deletions.
18 changes: 14 additions & 4 deletions electrum/block_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package electrum

import (
"context"
"errors"
"sync"

"github.com/elementsproject/peerswap/log"
"github.com/elementsproject/peerswap/swap"
)

type BlocKHeight uint32
Expand Down Expand Up @@ -55,13 +57,21 @@ func (h *liquidBlockHeaderSubscriber) Update(ctx context.Context, blockHeight Bl
defer h.mu.Unlock()
for _, observer := range h.txObservers {
callbacked, err := observer.Callback(ctx, blockHeight)
if callbacked && err == nil {
// callbacked and no error, remove observer
h.Deregister(observer)
if callbacked {
if err == nil || errors.Is(err, swap.ErrSwapDoesNotExist) {
// callbacked and no error, remove observer
h.Deregister(observer)
}
}
if err != nil {
if err != nil && !errors.Is(err, swap.ErrSwapDoesNotExist) {
log.Infof("Error in callback: %v", err)
}
}
return nil
}

func (h *liquidBlockHeaderSubscriber) Count() int {
h.mu.Lock()
defer h.mu.Unlock()
return len(h.txObservers)
}
66 changes: 64 additions & 2 deletions electrum/block_subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (
)

type testtxo struct {
swapid swap.SwapId
swapid swap.SwapId
getSwapID func() swap.SwapId
callback func(context.Context, BlocKHeight) (bool, error)
}

func (t *testtxo) GetSwapID() swap.SwapId {
if t.getSwapID != nil {
return t.getSwapID()
}
return t.swapid
}

func (t *testtxo) Callback(context.Context, BlocKHeight) (bool, error) {
func (t *testtxo) Callback(ctx context.Context, b BlocKHeight) (bool, error) {
if t.callback != nil {
return t.callback(ctx, b)
}
return true, nil
}

Expand Down Expand Up @@ -51,3 +59,57 @@ func TestConcurrentUpdate(t *testing.T) {
t.Errorf("Expected length %d, but got %d", 0, len(h.txObservers))
}
}

func Test_liquidBlockHeaderSubscriber_Update(t *testing.T) {
t.Parallel()
var blockHeight BlocKHeight = 10
tests := map[string]struct {
txObservers []TXObserver
count int
wantErr bool
}{
"no observers": {
txObservers: nil,
},
"observers": {
txObservers: []TXObserver{
&testtxo{},
&testtxo{},
},
},
"swap does not exists": {
txObservers: []TXObserver{
&testtxo{
callback: func(context.Context, BlocKHeight) (bool, error) {
return true, swap.ErrSwapDoesNotExist
},
},
},
},
"error in callback": {
txObservers: []TXObserver{
&testtxo{
callback: func(context.Context, BlocKHeight) (bool, error) {
return true, swap.ErrEventRejected
},
},
},
count: 1,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
h := &liquidBlockHeaderSubscriber{
txObservers: tt.txObservers,
}
if err := h.Update(context.Background(), blockHeight); (err != nil) != tt.wantErr {
t.Errorf("liquidBlockHeaderSubscriber.Update() error = %v, wantErr %v", err, tt.wantErr)
}
if len(h.txObservers) != tt.count {
t.Errorf("Expected length %d, but got %d", tt.count, len(h.txObservers))
}
})
}
}
13 changes: 7 additions & 6 deletions electrum/tx_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/checksum0/go-electrum/electrum"
"github.com/elementsproject/peerswap/log"
"github.com/elementsproject/peerswap/onchain"
"github.com/elementsproject/peerswap/swap"
)
Expand Down Expand Up @@ -93,11 +94,11 @@ func (o *observeOpeningTX) Callback(ctx context.Context, currentHeight BlocKHeig
}
rawTx, err := o.electrumClient.GetRawTransaction(ctx, o.txID.String())
if err != nil {
return false, fmt.Errorf("failed to get raw transaction: %w", err)
log.Debugf("failed to get raw transaction: %s", o.txID.String())
return false, nil
}
if !(currentHeight.Height() >= getHeight(hs, o.txID).Height()+uint32(onchain.LiquidConfs)-1) {
return false, fmt.Errorf("not enough confirmations for opening transaction. txhash: %s.height: %d, current: %d",
o.txID.String(), getHeight(hs, o.txID).Height(), currentHeight.Height())
return false, nil
}
return true, o.cb(o.swapID.String(), rawTx, nil)
}
Expand Down Expand Up @@ -139,11 +140,11 @@ func (o *observeCSVTX) Callback(ctx context.Context, currentHeight BlocKHeight)
return false, fmt.Errorf("failed to get history: %w", err)
}
if !(getHeight(hs, o.txID).Confirmed()) {
return false, fmt.Errorf("the transaction is unconfirmed")
log.Debugf("the transaction is unconfirmed. txhash: %s", o.txID.String())
return false, nil
}
if !(currentHeight.Height() >= getHeight(hs, o.txID).Height()+uint32(onchain.LiquidCsv-1)) {
return false, fmt.Errorf("not enough confirmations for csv transaction. txhash: %s.height: %d, current: %d",
o.txID.String(), getHeight(hs, o.txID).Height(), currentHeight.Height())
return false, nil
}
return true, o.cb(o.swapID.String())
}
2 changes: 1 addition & 1 deletion lwk/electrumtxwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *electrumTxWatcher) StartWatchingTxs() error {
r.mu.Lock()
r.blockHeight = electrum.BlocKHeight(blockHeader.Height)
r.mu.Unlock()
log.Infof("New block received. block height:%d", r.blockHeight)
log.Debugf("New block received. block height:%d", r.blockHeight)
err = r.subscriber.Update(ctx, r.blockHeight)
if err != nil {
log.Infof("Error notifying tx observers: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion lwk/lwkwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type SatPerKVByte float64
func SatPerKVByteFromFeeBTCPerKb(feeBTCPerKb float64) SatPerKVByte {
s := SatPerKVByte(feeBTCPerKb * math.Pow10(btcToSatoshiExp) / kb)
if s < minimumSatPerByte {
log.Infof("using minimum fee: %v.", minimumSatPerByte)
log.Debugf("Using minimum fee rate of %v sat/kw",
minimumSatPerByte)
return minimumSatPerByte
}
return s
Expand Down

0 comments on commit f2ee818

Please sign in to comment.