Skip to content

Commit

Permalink
chappjc review followup 1
Browse files Browse the repository at this point in the history
Refactor initialization and connection. Move nearly all instantiation
to connect. There is an assumption here that the caller will never
try to use an unconnected wallet to do anything. Upon inspection, I
think this is how we've designed things, but it's worth mentioning
since calling some methods before connecting will definitely panic
now.

Log btcwallet and neutrino output to a rotating log file.

Combine loadChainClient and startWallet functions into a single
(*spvWallet).startWallet method.
  • Loading branch information
buck54321 committed Oct 13, 2021
1 parent 11ab338 commit 1086370
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 201 deletions.
3 changes: 1 addition & 2 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (d *Driver) Create(params *asset.CreateWalletParams) error {
return fmt.Errorf("error parsing chain: %w", err)
}

return createSPVWallet(params.Pass, params.Seed, params.DataDir, chainParams)
return createSPVWallet(params.Pass, params.Seed, params.DataDir, params.Logger, chainParams)
}

// Open opens or connects to the BTC exchange wallet. Start the wallet with its
Expand Down Expand Up @@ -786,7 +786,6 @@ func (btc *ExchangeWallet) shutdown() {
delete(btc.findRedemptionQueue, contractOutpoint)
}
btc.findRedemptionMtx.Unlock()
btc.node.stop()
}

// getBlockchainInfoResult models the data returned from the getblockchaininfo
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (c *tRawRequester) RawRequest(_ context.Context, method string, params []js
const testBlocksPerBlockTimeOffset = 4

func generateTestBlockTime(blockHeight int64) time.Time {
return time.Unix(1e6, 0).Add(time.Duration(blockHeight) * maxBlockTimeOffset / testBlocksPerBlockTimeOffset)
return time.Unix(1e6, 0).Add(time.Duration(blockHeight) * maxFutureBlockTime / testBlocksPerBlockTimeOffset)
}

func (c *testData) addRawTx(blockHeight int64, tx *wire.MsgTx) (*chainhash.Hash, *wire.MsgBlock) {
Expand Down
23 changes: 17 additions & 6 deletions client/asset/btc/livetest/livetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ func Run(t *testing.T, cfg *Config) {
rig.backends["beta"], rig.connectionMasters["beta"] = tBackend(tCtx, t, cfg, "beta", "", tLogger.SubLogger("beta"), blkFunc)
rig.backends["gamma"], rig.connectionMasters["gamma"] = tBackend(tCtx, t, cfg, "alpha", "gamma", tLogger.SubLogger("gamma"), blkFunc)
defer rig.close()

// Unlock the wallet for use.
err := rig.alpha().Unlock(walletPassword)
if err != nil {
t.Fatalf("error unlocking gamma wallet: %v", err)
}

if cfg.SPV {
// The test expects beta and gamma to be unlocked.
if err := rig.beta().Unlock(walletPassword); err != nil {
t.Fatalf("beta Unlock error: %v", err)
}
if err := rig.gamma().Unlock(walletPassword); err != nil {
t.Fatalf("gamma Unlock error: %v", err)
}
}

var lots uint64 = 2
contractValue := lots * cfg.LotSize

Expand All @@ -184,12 +201,6 @@ func Run(t *testing.T, cfg *Config) {
name, float64(bal.Available)/1e8, float64(bal.Immature)/1e8, float64(bal.Locked)/1e8)
}

// Unlock the wallet for use.
err := rig.alpha().Unlock(walletPassword)
if err != nil {
t.Fatalf("error unlocking gamma wallet: %v", err)
}

ord := &asset.Order{
Value: contractValue * 3,
MaxSwapCount: lots * 3,
Expand Down
8 changes: 1 addition & 7 deletions client/asset/btc/livetest/regnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestWallet(t *testing.T) {
Pass: tPW, // match walletPassword in livetest.go -> Run
DataDir: cfg.DataDir,
Net: dex.Simnet,
Logger: logger,
})
if err != nil {
return fmt.Errorf("error creating SPV wallet: %w", err)
Expand Down Expand Up @@ -173,13 +174,6 @@ func TestWallet(t *testing.T) {
return nil, err
}

// The test expects beta and gamma to be unlocked.
if name == "beta" || name == "gamma" {
if err := w.Unlock(tPW); err != nil {
return nil, err
}
}

return w, nil
}

Expand Down
4 changes: 1 addition & 3 deletions client/asset/btc/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ func (wc *rpcClient) connect(ctx context.Context) error {
return nil
}

func (wc *rpcClient) stop() {}

// RawRequest passes the reqeuest to the wallet's RawRequester.
func (wc *rpcClient) RawRequest(method string, params []json.RawMessage) (json.RawMessage, error) {
return wc.requester.RawRequest(wc.ctx, method, params)
Expand Down Expand Up @@ -406,7 +404,7 @@ func (wc *rpcClient) walletLock() error {
}

// sendToAddress sends the amount to the address. feeRate is in units of
// atoms/byte.
// sats/byte.
func (wc *rpcClient) sendToAddress(address string, value, feeRate uint64, subtract bool) (*chainhash.Hash, error) {
var success bool
// 1e-5 = 1e-8 for satoshis * 1000 for kB.
Expand Down
Loading

0 comments on commit 1086370

Please sign in to comment.