Skip to content

Commit

Permalink
Buck review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Apr 18, 2024
1 parent 1d65ccc commit b4c1c96
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 39 deletions.
8 changes: 4 additions & 4 deletions client/asset/bch/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (w *bchSPVWallet) ListSinceBlock(start, end, syncHeight int32) ([]btcjson.L
func (w *bchSPVWallet) GetTransactions(startBlock, endBlock int32, _ string, cancel <-chan struct{}) (*btcwallet.GetTransactionsResult, error) {
startID := wallet.NewBlockIdentifierFromHeight(startBlock)
endID := wallet.NewBlockIdentifierFromHeight(endBlock)
ltcGTR, err := w.Wallet.GetTransactions(startID, endID, cancel)
bchGTR, err := w.Wallet.GetTransactions(startID, endID, cancel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -377,11 +377,11 @@ func (w *bchSPVWallet) GetTransactions(startBlock, endBlock int32, _ string, can
}

btcGTR := &btcwallet.GetTransactionsResult{
MinedTransactions: make([]btcwallet.Block, len(ltcGTR.MinedTransactions)),
UnminedTransactions: convertTxs(ltcGTR.UnminedTransactions),
MinedTransactions: make([]btcwallet.Block, len(bchGTR.MinedTransactions)),
UnminedTransactions: convertTxs(bchGTR.UnminedTransactions),
}

for i, block := range ltcGTR.MinedTransactions {
for i, block := range bchGTR.MinedTransactions {
blockHash := chainhash.Hash(*block.Hash)
btcGTR.MinedTransactions[i] = btcwallet.Block{
Hash: &blockHash,
Expand Down
14 changes: 7 additions & 7 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ func (btc *baseWallet) Info() *asset.WalletInfo {
}

func (btc *baseWallet) txHistoryDBPath(walletID string) string {
return filepath.Join(btc.walletDir, fmt.Sprintf("txhistory-%s.db", walletID))
return filepath.Join(btc.walletDir, fmt.Sprintf("txhistorydb-%s", walletID))
}

// findExistingAddressBasedTxHistoryDB finds the path of a tx history db that
Expand All @@ -1414,7 +1414,7 @@ func (btc *baseWallet) findExistingAddressBasedTxHistoryDB() (string, error) {
return "", fmt.Errorf("error reading wallet directory: %w", err)
}

pattern := regexp.MustCompile(`^txhistory-(.+)\.db$`)
pattern := regexp.MustCompile(`^txhistorydb-(.+)$`)

for _, entry := range entries {
if !entry.IsDir() {
Expand All @@ -1439,7 +1439,7 @@ func (btc *baseWallet) findExistingAddressBasedTxHistoryDB() (string, error) {
return "", nil
}

func (btc *baseWallet) startTxHistoryDB() error {
func (btc *baseWallet) startTxHistoryDB(ctx context.Context) error {
var dbPath string
fingerPrint, err := btc.node.fingerprint()
if err == nil && fingerPrint != "" {
Expand All @@ -1466,7 +1466,7 @@ func (btc *baseWallet) startTxHistoryDB() error {

btc.log.Debugf("Using tx history db at %s", dbPath)

db, err := NewBadgerTxDB(dbPath, btc.log)
db, err := NewBadgerTxDB(ctx, dbPath, btc.log)
if err != nil {
return fmt.Errorf("error opening tx history db: %w", err)
}
Expand Down Expand Up @@ -1572,7 +1572,7 @@ func (btc *intermediaryWallet) Connect(ctx context.Context) (*sync.WaitGroup, er
return nil, err
}

err = btc.startTxHistoryDB()
err = btc.startTxHistoryDB(baseCtx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1604,9 +1604,9 @@ func (btc *intermediaryWallet) Connect(ctx context.Context) (*sync.WaitGroup, er
}
}()

btc.tipMtx.Lock()
btc.tipMtx.RLock()
tip := btc.currentTip
btc.tipMtx.Unlock()
btc.tipMtx.RUnlock()
go btc.syncTxHistory(uint64(tip.Height))

return baseWg, nil
Expand Down
5 changes: 3 additions & 2 deletions client/asset/btc/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,9 @@ func (wc *rpcClient) listTransactionsSinceBlock(blockHeight int32) ([]*ListTrans
if err != nil {
return nil, fmt.Errorf("getBlockHash error: %w", err)
}

result := new(listTransactionsResult)
result := new(struct {
Transactions []btcjson.ListTransactionsResult `json:"transactions"`
})
err = wc.call(methodListSinceBlock, anylist{blockHash.String()}, result)
if err != nil {
return nil, fmt.Errorf("listtransactions error: %w", err)
Expand Down
13 changes: 7 additions & 6 deletions client/asset/btc/txdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type BadgerTxDB struct {
seq *badger.Sequence

running atomic.Bool
wg *sync.WaitGroup
wg sync.WaitGroup
ctx context.Context
die context.CancelFunc
}
Expand All @@ -93,7 +93,7 @@ func (log *badgerLoggerWrapper) Warningf(s string, a ...interface{}) {
log.Warnf(s, a...)
}

func NewBadgerTxDB(filePath string, log dex.Logger) (*BadgerTxDB, error) {
func NewBadgerTxDB(ctx context.Context, filePath string, log dex.Logger) (*BadgerTxDB, error) {
// If memory use is a concern, could try
// .WithValueLogLoadingMode(options.FileIO) // default options.MemoryMap
// .WithMaxTableSize(sz int64); // bytes, default 6MB
Expand All @@ -118,13 +118,12 @@ func NewBadgerTxDB(filePath string, log dex.Logger) (*BadgerTxDB, error) {
return nil, err
}

ctx, die := context.WithCancel(context.Background())
ctx, die := context.WithCancel(ctx)

badgerDB := &BadgerTxDB{
DB: db,
log: log,
seq: seq,
wg: new(sync.WaitGroup),
die: die,
}
badgerDB.running.Store(true)
Expand All @@ -142,6 +141,9 @@ func NewBadgerTxDB(filePath string, log dex.Logger) (*BadgerTxDB, error) {
log.Errorf("garbage collection error: %v", err)
}
case <-ctx.Done():
if err := seq.Release(); err != nil {
log.Errorf("error releasing sequence: %v", err)
}
return
}
}
Expand All @@ -153,11 +155,10 @@ func NewBadgerTxDB(filePath string, log dex.Logger) (*BadgerTxDB, error) {
// badgerDB returns ErrConflict when a read happening in a update (read/write)
// transaction is stale. This function retries updates multiple times in
// case of conflicts.
func handleConflictWithBackoff(update func() error) error {
func handleConflictWithBackoff(update func() error) (err error) {
maxRetries := 10
sleepTime := 5 * time.Millisecond

var err error
for i := 0; i < maxRetries; i++ {
sleepTime *= 2
err = update()
Expand Down
11 changes: 9 additions & 2 deletions client/asset/btc/txdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package btc

import (
"context"
"encoding/hex"
"errors"
"reflect"
Expand All @@ -18,7 +19,10 @@ func TestTxDB(t *testing.T) {
tempDir := t.TempDir()
tLogger := dex.StdOutLogger("TXDB", dex.LevelTrace)

txHistoryStore, err := NewBadgerTxDB(tempDir, tLogger)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

txHistoryStore, err := NewBadgerTxDB(ctx, tempDir, tLogger)
if err != nil {
t.Fatalf("error creating tx history store: %v", err)
}
Expand Down Expand Up @@ -185,7 +189,10 @@ func TestSetAndGetLastQuery(t *testing.T) {
tempDir := t.TempDir()
tLogger := dex.StdOutLogger("TXDB", dex.LevelTrace)

txHistoryStore, err := NewBadgerTxDB(tempDir, tLogger)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

txHistoryStore, err := NewBadgerTxDB(ctx, tempDir, tLogger)
if err != nil {
t.Fatalf("error creating tx history store: %v", err)
}
Expand Down
5 changes: 0 additions & 5 deletions client/asset/btc/wallettypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package btc

import (
"decred.org/dcrdex/dex"
"github.com/btcsuite/btcd/btcjson"
)

// GetBalancesResult models a successful response from the getbalances request.
Expand Down Expand Up @@ -164,7 +163,3 @@ type listDescriptorsResult struct {
Next int64 `json:"next"` // next index to addresses generation; only set for ranged descriptors
} `json:"descriptors"`
}

type listTransactionsResult struct {
Transactions []btcjson.ListTransactionsResult `json:"transactions"`
}
8 changes: 4 additions & 4 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ func (dcr *ExchangeWallet) Info() *asset.WalletInfo {
// }

func (dcr *ExchangeWallet) txHistoryDBPath(walletID string) string {
return filepath.Join(dcr.walletDir, fmt.Sprintf("txhistory-%s.db", walletID))
return filepath.Join(dcr.walletDir, fmt.Sprintf("txhistorydb-%s", walletID))
}

// findExistingAddressBasedTxHistoryDB finds the path of a tx history db that
Expand All @@ -922,7 +922,7 @@ func (dcr *ExchangeWallet) findExistingAddressBasedTxHistoryDB() (string, error)
return "", fmt.Errorf("error reading wallet directory: %w", err)
}

pattern := regexp.MustCompile(`^txhistory-(.+)\.db$`)
pattern := regexp.MustCompile(`^txhistorydb-(.+)$`)

for _, entry := range entries {
if !entry.IsDir() {
Expand Down Expand Up @@ -978,7 +978,7 @@ func (dcr *ExchangeWallet) startTxHistoryDB(ctx context.Context) error {

dcr.log.Debugf("Using tx history db at %s", dbPath)

db, err := btc.NewBadgerTxDB(dbPath, dcr.log)
db, err := btc.NewBadgerTxDB(ctx, dbPath, dcr.log)
if err != nil {
return fmt.Errorf("error opening tx history db: %w", err)
}
Expand Down Expand Up @@ -4449,7 +4449,7 @@ func (dcr *ExchangeWallet) RefundBond(ctx context.Context, ver uint16, coinID, s
LockTime: uint64(lockTime),
BondID: pkhPush,
}
dcr.addTxToHistory(asset.RedeemBond, txHash, amt, amt-uint64(refundAmt), bondInfo, nil, true)
dcr.addTxToHistory(asset.RedeemBond, redeemHash, amt, amt-uint64(refundAmt), bondInfo, nil, true)
return newOutput(redeemHash, 0, uint64(refundAmt), wire.TxTreeRegular), nil

/* If we need to find the actual unspent bond transaction for any of:
Expand Down
2 changes: 1 addition & 1 deletion client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ func (w *ETHWallet) Connect(ctx context.Context) (_ *sync.WaitGroup, err error)
}
}

w.txDB, err = newBadgerTxDB(filepath.Join(w.dir, "tx.db"), w.log.SubLogger("TXDB"))
w.txDB, err = newBadgerTxDB(ctx, filepath.Join(w.dir, "txhistorydb"), w.log.SubLogger("TXDB"))
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4442,8 +4442,11 @@ func testConfirmRedemption(t *testing.T, assetID uint32) {
}
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

tempDir := t.TempDir()
txDB, err := newBadgerTxDB(filepath.Join(tempDir, "tx.db"), tLogger)
txDB, err := newBadgerTxDB(ctx, filepath.Join(tempDir, "tx.db"), tLogger)
if err != nil {
t.Fatalf("error creating tx db: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions client/asset/eth/txdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ type badgerTxDB struct {
*badger.DB
log dex.Logger
running atomic.Bool
wg *sync.WaitGroup
wg sync.WaitGroup
die context.CancelFunc
}

Expand All @@ -227,7 +227,7 @@ func (log *badgerLoggerWrapper) Warningf(s string, a ...interface{}) {
log.Warnf(s, a...)
}

func newBadgerTxDB(filePath string, log dex.Logger) (*badgerTxDB, error) {
func newBadgerTxDB(ctx context.Context, filePath string, log dex.Logger) (*badgerTxDB, error) {
// If memory use is a concern, could try
// .WithValueLogLoadingMode(options.FileIO) // default options.MemoryMap
// .WithMaxTableSize(sz int64); // bytes, default 6MB
Expand All @@ -248,12 +248,11 @@ func newBadgerTxDB(filePath string, log dex.Logger) (*badgerTxDB, error) {
}

wg := new(sync.WaitGroup)
ctx, die := context.WithCancel(context.Background())
ctx, die := context.WithCancel(ctx)

txDB := &badgerTxDB{
DB: db,
log: log,
wg: wg,
die: die,
}

Expand Down
15 changes: 12 additions & 3 deletions client/asset/eth/txdb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth

import (
"context"
"encoding/hex"
"reflect"
"testing"
Expand All @@ -17,7 +18,10 @@ func TestTxDB(t *testing.T) {
tempDir := t.TempDir()
tLogger := dex.StdOutLogger("TXDB", dex.LevelTrace)

txHistoryStore, err := newBadgerTxDB(tempDir, tLogger)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

txHistoryStore, err := newBadgerTxDB(ctx, tempDir, tLogger)
if err != nil {
t.Fatalf("error creating tx history store: %v", err)
}
Expand Down Expand Up @@ -175,7 +179,9 @@ func TestTxDB(t *testing.T) {

txHistoryStore.close()

txHistoryStore, err = newBadgerTxDB(tempDir, dex.StdOutLogger("TXDB", dex.LevelTrace))
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
txHistoryStore, err = newBadgerTxDB(ctx, tempDir, dex.StdOutLogger("TXDB", dex.LevelTrace))
if err != nil {
t.Fatalf("error creating tx history store: %v", err)
}
Expand Down Expand Up @@ -395,7 +401,10 @@ func TestTxDBUpgrade(t *testing.T) {
t.Fatalf("error closing badger db: %v", err)
}

txHistoryStore, err := newBadgerTxDB(dir, tLogger)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

txHistoryStore, err := newBadgerTxDB(ctx, dir, tLogger)
if err != nil {
t.Fatalf("error creating tx history store: %v", err)
}
Expand Down

0 comments on commit b4c1c96

Please sign in to comment.