Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Oct 18, 2023
1 parent d240886 commit 62474fc
Show file tree
Hide file tree
Showing 10 changed files with 1,558 additions and 144 deletions.
2 changes: 1 addition & 1 deletion client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,7 @@ func (btc *baseWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, ui
}

var locks []*UTxO
if swaps.LockChange {
if change != nil && swaps.LockChange {
// Lock the change output
btc.log.Debugf("locking change coin %s", change)
err = btc.node.lockUnspent(false, []*Output{change})
Expand Down
23 changes: 17 additions & 6 deletions client/asset/btc/coinmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ func (c *CoinManager) fund(keep uint64, minConfs uint32, lockUnspents bool,
if err != nil {
return nil, nil, nil, nil, 0, 0, fmt.Errorf("error getting spendable utxos: %w", err)
}

return c.fundWithUTXOs(utxos, avail, keep, lockUnspents, enough)
}

func (c *CoinManager) Fund(
keep uint64, minConfs uint32, lockUnspents bool, enough EnoughFunc,
keep uint64,
minConfs uint32,
lockUnspents bool,
enough EnoughFunc,
) (coins asset.Coins, fundingCoins map[OutPoint]*UTxO, spents []*Output, redeemScripts []dex.Bytes, size, sum uint64, err error) {

c.mtx.Lock()
Expand Down Expand Up @@ -334,6 +336,7 @@ func (c *CoinManager) spendableUTXOs(confs uint32) ([]*CompositeUTXO, map[OutPoi
if err != nil {
return nil, nil, 0, err
}

var relock []*Output
var i int
for _, utxo := range utxos {
Expand Down Expand Up @@ -583,9 +586,17 @@ func convertUnspent(confs uint32, unspents []*ListUnspentResult, chainParams *ch
return utxos, utxoMap, sum, nil
}

func TryFund(utxos []*CompositeUTXO,
enough EnoughFunc) (
sum, extra, size uint64, coins asset.Coins, fundingCoins map[OutPoint]*UTxO, redeemScripts []dex.Bytes, spents []*Output, err error) {
func TryFund(
utxos []*CompositeUTXO,
enough EnoughFunc,
) (
sum, extra, size uint64,
coins asset.Coins,
fundingCoins map[OutPoint]*UTxO,
redeemScripts []dex.Bytes,
spents []*Output,
err error,
) {

fundingCoins = make(map[OutPoint]*UTxO)

Expand Down Expand Up @@ -647,7 +658,7 @@ func TryFund(utxos []*CompositeUTXO,
if !tryUTXOs(1) {
if !tryUTXOs(0) {
return 0, 0, 0, nil, nil, nil, nil, fmt.Errorf("not enough to cover requested funds. "+
"%s available in %d UTXOs", amount(sum), len(coins))
"%d available in %d UTXOs", amount(sum), len(coins))
}
}

Expand Down
72 changes: 72 additions & 0 deletions client/asset/zec/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.

package zec

import (
"errors"
"fmt"
)

// Error codes here are used on the frontend.
const (
errNoFundsRequested = iota
errBalanceRetrieval
errInsufficientBalance
errUTxORetrieval
errLockUnspent
errFunding
)

// Error is an error code and a wrapped error.
type Error struct {
code int
err error
}

// Error returns the error string. Satisfies the error interface.
func (e *Error) Error() string {
return e.err.Error()
}

// Code returns the error code.
func (e *Error) Code() *int {
return &e.code
}

// Unwrap returns the underlying wrapped error.
func (e *Error) Unwrap() error {
return e.err
}

// newError is a constructor for a new Error.
func newError(code int, s string, a ...any) error {
return &Error{
code: code,
err: fmt.Errorf(s, a...), // s may contain a %w verb to wrap an error
}
}

// codedError converts the error to an Error with the specified code.
func codedError(code int, err error) error {
return &Error{
code: code,
err: err,
}
}

// errorHasCode checks whether the error is an Error and has the specified code.
func errorHasCode(err error, code int) bool {
var e *Error
return errors.As(err, &e) && e.code == code
}

// UnwrapErr returns the result of calling the Unwrap method on err,
// until it returns a non-wrapped error.
func UnwrapErr(err error) error {
InnerErr := errors.Unwrap(err)
if InnerErr == nil {
return err
}
return UnwrapErr(InnerErr)
}
30 changes: 18 additions & 12 deletions client/asset/zec/shielded_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,18 @@ type poolBalances struct {
Sapling uint64 `json:"sapling"`
}

type zBalancePools struct {
Orchard valZat `json:"orchard"`
Transparent valZat `json:"transparent"`
Sapling valZat `json:"sapling"`
}

type zAccountBalance struct {
Pools zBalancePools `json:"pools"`
}

func zGetBalanceForAccount(c rpcCaller, acct uint32, confs int) (*poolBalances, error) {
var res struct {
Pools struct {
Orchard valZat `json:"orchard"`
Transparent valZat `json:"transparent"`
Sapling valZat `json:"sapling"`
} `json:"pools"`
}
var res zAccountBalance
return &poolBalances{
Orchard: res.Pools.Orchard.ValueZat,
Transparent: res.Pools.Transparent.ValueZat,
Expand Down Expand Up @@ -139,6 +143,10 @@ func zSendMany(c rpcCaller, fromAddress string, recips []*zSendManyRecipient, pr
return operationID, c.CallRPC(methodZSendMany, []any{fromAddress, recips, minConf, fee, priv}, &operationID)
}

type opResult struct {
TxID string `json:"txid"`
}

type operationStatus struct {
ID string `json:"id"`
Status string `json:"status"` // "success", "failed", others?
Expand All @@ -147,11 +155,9 @@ type operationStatus struct {
Code int32 `json:"code"`
Message string `json:"message"`
} `json:"error" `
Result *struct {
TxID string `json:"txid"`
} `json:"result"`
ExecutionSeconds float64 `json:"execution_secs"`
Method string `json:"method"`
Result *opResult `json:"result"`
ExecutionSeconds float64 `json:"execution_secs"`
Method string `json:"method"`
Params struct {
FromAddress string `json:"fromaddress"`
Amounts []struct {
Expand Down
15 changes: 8 additions & 7 deletions client/asset/zec/transparent_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func lockUnspent(c rpcCaller, unlock bool, ops []*btc.Output) error {
}

func getTransaction(c rpcCaller, txHash *chainhash.Hash) (*dexzec.Tx, error) {
var txB dex.Bytes
if err := c.CallRPC("gettransaction", []any{txHash.String()}, &txB); err != nil {
var tx btc.GetTransactionResult
if err := c.CallRPC("gettransaction", []any{txHash.String()}, &tx); err != nil {
return nil, err
}
return dexzec.DeserializeTx(txB)
return dexzec.DeserializeTx(tx.Bytes)
}

func getRawTransaction(c rpcCaller, txHash *chainhash.Hash) ([]byte, error) {
Expand Down Expand Up @@ -135,6 +135,7 @@ func listLockUnspent(c rpcCaller, log dex.Logger) ([]*btc.RPCOutpoint, error) {
TxID: utxo.TxID,
Vout: utxo.Vout,
}}

err = c.CallRPC("lockunspent", []any{true, op}, &success)
if err != nil || !success {
log.Warnf("lockunspent(unlocking %v:%d): success = %v, err = %v",
Expand Down Expand Up @@ -230,15 +231,15 @@ func getRPCBlockHeader(c rpcCaller, blockHash *chainhash.Hash) (*btc.BlockHeader
}

func getWalletTransaction(c rpcCaller, txHash *chainhash.Hash) (*btc.GetTransactionResult, error) {
tx := new(btc.GetTransactionResult)
err := c.CallRPC("gettransaction", []any{txHash.String()}, tx)
var tx btc.GetTransactionResult
err := c.CallRPC("gettransaction", []any{txHash.String()}, &tx)
if err != nil {
if btc.IsTxNotFoundErr(err) {
return nil, asset.CoinNotFoundError
}
return nil, err
}
return tx, nil
return &tx, nil
}

func peerCount(c rpcCaller) (uint32, error) {
Expand All @@ -265,7 +266,7 @@ func getBlockHeight(c rpcCaller, blockHash *chainhash.Hash) (int32, error) {

func getBlock(c rpcCaller, h chainhash.Hash) (*dexzec.Block, error) {
var blkB dex.Bytes
err := c.CallRPC("getblock", []any{h.String(), 0}, &blkB)
err := c.CallRPC("getblock", []any{h.String(), int64(0)}, &blkB)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 62474fc

Please sign in to comment.