Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Apr 3, 2022
1 parent bae9942 commit 3809d44
Show file tree
Hide file tree
Showing 22 changed files with 983 additions and 127 deletions.
49 changes: 40 additions & 9 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ type BTCCloneCFG struct {
// FeeEstimator provides a way to get fees given an RawRequest-enabled
// client and a confirmation target.
FeeEstimator func(RawRequester, uint64) (uint64, error)
// OmitAddressType causes the address type (bech32, legacy) to be omitted
// from calls to getnewaddress.
OmitAddressType bool
// LegacySignTxRPC causes the RPC client to use the signrawtransaction
// endpoint instead of the signrawtransactionwithwallet endpoint.
LegacySignTxRPC bool
// BooleanGetBlockRPC causes the RPC client to use a boolean second argument
// for the getblock endpoint, instead of Bitcoin's numeric.
BooleanGetBlockRPC bool
// SingularWallet signals that the node software supports only one wallet,
// so the RPC endpoint does not have a /wallet/{walletname} path.
SingularWallet bool
}

// outPoint is the hash and output index of a transaction output.
Expand Down Expand Up @@ -425,12 +437,16 @@ func readRPCWalletConfig(settings map[string]string, symbol string, net dex.Netw

// parseRPCWalletConfig parses a *RPCWalletConfig from the settings map and
// creates the unconnected *rpcclient.Client.
func parseRPCWalletConfig(settings map[string]string, symbol string, net dex.Network, ports dexbtc.NetPorts) (*RPCWalletConfig, *rpcclient.Client, error) {
func parseRPCWalletConfig(settings map[string]string, symbol string, net dex.Network, ports dexbtc.NetPorts, singularWallet bool) (*RPCWalletConfig, *rpcclient.Client, error) {
cfg, err := readRPCWalletConfig(settings, symbol, net, ports)
if err != nil {
return nil, nil, err
}
endpoint := cfg.RPCBind + "/wallet/" + cfg.WalletName
endpoint := cfg.RPCBind
if !singularWallet {
endpoint += "/wallet/" + cfg.WalletName
}

cl, err := rpcclient.New(&rpcclient.ConnConfig{
HTTPPostMode: true,
DisableTLS: true,
Expand Down Expand Up @@ -552,7 +568,7 @@ type baseWallet struct {
useSplitTx bool
useLegacyBalance bool
segwit bool
legacyRawFeeLimit bool
legacyRawFeeLimit bool // wut dis?
signNonSegwit TxInSigner
estimateFee func(RawRequester, uint64) (uint64, error)
decodeAddr dexbtc.AddressDecoder
Expand Down Expand Up @@ -699,7 +715,7 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, net dex.Network) (ass
// conjunction with ReadCloneParams, to create a ExchangeWallet for other assets
// with minimal coding.
func BTCCloneWallet(cfg *BTCCloneCFG) (*ExchangeWalletFullNode, error) {
clientCfg, client, err := parseRPCWalletConfig(cfg.WalletCFG.Settings, cfg.Symbol, cfg.Network, cfg.Ports)
clientCfg, client, err := parseRPCWalletConfig(cfg.WalletCFG.Settings, cfg.Symbol, cfg.Network, cfg.Ports, cfg.SingularWallet)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -730,8 +746,19 @@ func newRPCWallet(requester RawRequesterWithContext, cfg *BTCCloneCFG, walletCon
if err != nil {
return nil, err
}
btc.node = newRPCClient(requester, cfg.Segwit, btc.decodeAddr, cfg.ArglessChangeAddrRPC,
cfg.LegacyRawFeeLimit, cfg.MinNetworkVersion, cfg.Logger.SubLogger("RPC"), cfg.ChainParams)
btc.node = newRPCClient(&rpcCore{
requester: requester,
segwit: cfg.Segwit,
decodeAddr: btc.decodeAddr,
arglessChangeAddrRPC: cfg.ArglessChangeAddrRPC,
legacyRawSends: cfg.LegacyRawFeeLimit,
minNetworkVersion: cfg.MinNetworkVersion,
log: cfg.Logger.SubLogger("RPC"),
chainParams: cfg.ChainParams,
omitAddressType: cfg.OmitAddressType,
legacySignTx: cfg.LegacySignTxRPC,
booleanGetBlock: cfg.BooleanGetBlockRPC,
})
return &ExchangeWalletFullNode{btc}, nil
}

Expand All @@ -747,11 +774,14 @@ func newUnconnectedWallet(cfg *BTCCloneCFG, walletCfg *WalletConfig) (*baseWalle

// If set in the user config, the fee rate limit will be in units of BTC/KB.
// Convert to sats/byte & error if value is smaller than smallest unit.
feesLimitPerByte := uint64(defaultFeeRateLimit)
feesLimitPerByte := cfg.DefaultFeeRateLimit
if feesLimitPerByte == 0 {
feesLimitPerByte = defaultFeeRateLimit
}
if walletCfg.FeeRateLimit > 0 {
feesLimitPerByte = toSatoshi(walletCfg.FeeRateLimit / 1000)
if feesLimitPerByte == 0 {
return nil, fmt.Errorf("Fee rate limit is smaller than smallest unit: %v",
return nil, fmt.Errorf("fee rate limit is smaller than smallest unit: %v",
walletCfg.FeeRateLimit)
}
}
Expand Down Expand Up @@ -3343,6 +3373,7 @@ func (btc *baseWallet) spendableUTXOs(confs uint32) ([]*compositeUTXO, map[outPo
if err != nil {
return nil, nil, 0, err
}

utxos, utxoMap, sum, err := convertUnspent(confs, unspents, btc.chainParams)
if err != nil {
return nil, nil, 0, err
Expand All @@ -3367,7 +3398,7 @@ func convertUnspent(confs uint32, unspents []*ListUnspentResult, chainParams *ch
utxos := make([]*compositeUTXO, 0, len(unspents))
utxoMap := make(map[outPoint]*compositeUTXO, len(unspents))
for _, txout := range unspents {
if txout.Confirmations >= confs && txout.Safe && txout.Spendable {
if txout.Confirmations >= confs && txout.Safe() && txout.Spendable {
txHash, err := chainhash.NewHashFromStr(txout.TxID)
if err != nil {
return nil, nil, 0, fmt.Errorf("error decoding txid in ListUnspentResult: %w", err)
Expand Down
30 changes: 16 additions & 14 deletions client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ var (
feeSuggestion uint64 = 10
)

func boolPtr(v bool) *bool {
return &v
}

func btcAddr(segwit bool) btcutil.Address {
var addr btcutil.Address
if segwit {
Expand Down Expand Up @@ -599,8 +603,6 @@ func tNewWallet(segwit bool, walletType string) (*ExchangeWalletFullNode, *testD
Segwit: segwit,
}

// rpcClient := newRPCClient(requester, segwit, nil, false, minNetworkVersion, dex.StdOutLogger("RPCTEST", dex.LevelTrace), &chaincfg.MainNetParams)

var wallet *ExchangeWalletFullNode
var err error
switch walletType {
Expand Down Expand Up @@ -739,7 +741,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
ScriptPubKey: tP2PKH,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents = append(unspents, littleUTXO)
node.listUnspent = unspents
Expand Down Expand Up @@ -799,7 +801,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
ScriptPubKey: tP2PKH,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents = append(unspents, lottaUTXO)
littleUTXO.Confirmations = 1
Expand Down Expand Up @@ -864,7 +866,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
}

// Fund a little bit, with unsafe littleUTXO.
littleUTXO.Safe = false
littleUTXO.SafePtr = boolPtr(false)
littleUTXO.Confirmations = 0
node.listUnspent = unspents
spendables, _, err := wallet.FundOrder(ord)
Expand All @@ -880,7 +882,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
}

// Now with safe confirmed littleUTXO.
littleUTXO.Safe = true
littleUTXO.SafePtr = boolPtr(true)
littleUTXO.Confirmations = 2
node.listUnspent = unspents
spendables, _, err = wallet.FundOrder(ord)
Expand Down Expand Up @@ -1156,7 +1158,7 @@ func testFundingCoins(t *testing.T, segwit bool, walletType string) {
ScriptPubKey: tP2PKH,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
Amount: 1,
}
unspents := []*ListUnspentResult{p2pkhUnspent}
Expand Down Expand Up @@ -1300,7 +1302,7 @@ func TestFundEdges(t *testing.T) {
ScriptPubKey: tP2PKH,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents := []*ListUnspentResult{p2pkhUnspent}
node.listUnspent = unspents
Expand Down Expand Up @@ -1387,7 +1389,7 @@ func TestFundEdges(t *testing.T) {
RedeemScript: p2shRedeem,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
p2pkhUnspent.Amount = float64(halfSwap+backingFees-1) / 1e8
unspents = []*ListUnspentResult{p2pkhUnspent, p2shUnspent}
Expand Down Expand Up @@ -1418,7 +1420,7 @@ func TestFundEdges(t *testing.T) {
ScriptPubKey: p2wpkhPkScript,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents = []*ListUnspentResult{p2wpkhUnspent}
node.listUnspent = unspents
Expand Down Expand Up @@ -1452,7 +1454,7 @@ func TestFundEdges(t *testing.T) {
RedeemScript: p2wpkhRedeemScript,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents = []*ListUnspentResult{p2wpshUnspent}
node.listUnspent = unspents
Expand Down Expand Up @@ -1519,7 +1521,7 @@ func TestFundEdgesSegwit(t *testing.T) {
ScriptPubKey: tP2WPKH,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents := []*ListUnspentResult{p2wpkhUnspent}
node.listUnspent = unspents
Expand Down Expand Up @@ -2353,7 +2355,7 @@ func testSender(t *testing.T, senderType tSenderType, segwit bool, walletType st
Confirmations: 1,
Vout: vout,
ScriptPubKey: pkScript,
Safe: true,
SafePtr: boolPtr(true),
Spendable: true,
}}
node.listUnspent = unspents
Expand Down Expand Up @@ -2709,7 +2711,7 @@ func testPreSwap(t *testing.T, segwit bool, walletType string) {
ScriptPubKey: pkScript,
Spendable: true,
Solvable: true,
Safe: true,
SafePtr: boolPtr(true),
}
unspents := []*ListUnspentResult{unspent}

Expand Down

0 comments on commit 3809d44

Please sign in to comment.