Skip to content

Commit

Permalink
sig and serialization improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed May 14, 2022
1 parent e03d543 commit 87c6af1
Show file tree
Hide file tree
Showing 19 changed files with 530 additions and 487 deletions.
2 changes: 1 addition & 1 deletion client/asset/bch/bch.go
Expand Up @@ -231,7 +231,7 @@ func translateTx(btcTx *wire.MsgTx) (*bchwire.MsgTx, error) {
return nil, err
}

bchTx := bchwire.NewMsgTx(bchwire.TxVersion)
bchTx := new(bchwire.MsgTx)
err = bchTx.Deserialize(bytes.NewBuffer(txB))
if err != nil {
return nil, err
Expand Down
72 changes: 29 additions & 43 deletions client/asset/btc/btc.go
Expand Up @@ -276,6 +276,9 @@ type BTCCloneCFG struct {
// TxSizeCalculator is an optional function that will be used to calculate
// the size of a transaction.
TxSizeCalculator func(*wire.MsgTx) uint64
// TxVersion is an optional function that returns a version to use for
// new transactions.
TxVersion func() int32
}

// outPoint is the hash and output index of a transaction output.
Expand Down Expand Up @@ -611,6 +614,7 @@ type baseWallet struct {
calcTxSize func(*wire.MsgTx) uint64
hashTx func(*wire.MsgTx) *chainhash.Hash
stringifyAddress func(btcutil.Address) string
txVersion func() int32

tipMtx sync.RWMutex
currentTip *block
Expand Down Expand Up @@ -869,6 +873,11 @@ func newUnconnectedWallet(cfg *BTCCloneCFG, walletCfg *WalletConfig) (*baseWalle
addressStringer = stringifyAddress
}

txVersion := cfg.TxVersion
if txVersion == nil {
txVersion = func() int32 { return wire.TxVersion }
}

w := &baseWallet{
symbol: cfg.Symbol,
chainParams: cfg.ChainParams,
Expand All @@ -895,6 +904,7 @@ func newUnconnectedWallet(cfg *BTCCloneCFG, walletCfg *WalletConfig) (*baseWalle
hashTx: txHasher,
stringifyAddress: addressStringer,
calcTxSize: txSizeCalculator,
txVersion: txVersion,
}

if w.estimateFee == nil {
Expand Down Expand Up @@ -1004,11 +1014,16 @@ func (btc *baseWallet) IsDust(txOut *wire.TxOut, minRelayTxFee uint64) bool {
// getBlockchainInfoResult models the data returned from the getblockchaininfo
// command.
type getBlockchainInfoResult struct {
Blocks int64 `json:"blocks"`
Headers int64 `json:"headers"`
BestBlockHash string `json:"bestblockhash"`
InitialBlockDownload *bool `json:"initialblockdownload"`
InitialBlockDownloadComplete *bool `json:"initial_block_download_complete"`
Blocks int64 `json:"blocks"`
Headers int64 `json:"headers"`
BestBlockHash string `json:"bestblockhash"`
// InitialBlockDownload will be true if the node is still in the initial
// block download mode.
InitialBlockDownload *bool `json:"initialblockdownload"`
// InitialBlockDownloadComplete will be true if this node has completed its
// initial block download and is expected to be synced to the network.
// ZCash uses this terminology instead of initialblockdownload.
InitialBlockDownloadComplete *bool `json:"initial_block_download_complete"`
}

func (r *getBlockchainInfoResult) syncing() bool {
Expand Down Expand Up @@ -1965,7 +1980,7 @@ func (btc *baseWallet) Locked() bool {

// fundedTx creates and returns a new MsgTx with the provided coins as inputs.
func (btc *baseWallet) fundedTx(coins asset.Coins) (*wire.MsgTx, uint64, []outPoint, error) {
baseTx := wire.NewMsgTx(wire.TxVersion)
baseTx := wire.NewMsgTx(btc.txVersion())
var totalIn uint64
// Add the funding utxos.
pts := make([]outPoint, 0, len(coins))
Expand Down Expand Up @@ -2141,7 +2156,7 @@ func (btc *baseWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, ui
// Redeem sends the redemption transaction, completing the atomic swap.
func (btc *baseWallet) Redeem(form *asset.RedeemForm) ([]dex.Bytes, asset.Coin, uint64, error) {
// Create a transaction that spends the referenced contract.
msgTx := wire.NewMsgTx(wire.TxVersion)
msgTx := wire.NewMsgTx(btc.txVersion())
var totalIn uint64
var contracts [][]byte
var addresses []btcutil.Address
Expand Down Expand Up @@ -2423,41 +2438,6 @@ func (btc *baseWallet) AuditContract(coinID, contract, txData dex.Bytes, rebroad
}, nil
}

// AuditContract retrieves information about a swap contract from the provided
// txData. The extracted information would be used to audit the counter-party's
// contract during a swap. Since this wallet is backed by a full node, txData
// may be empty to attempt retrieval of the transaction output from the network.
func (btc *ExchangeWalletFullNode) AuditContract(coinID, contract, txData dex.Bytes, rebroadcast bool) (*asset.AuditInfo, error) {
if len(txData) != 0 {
return btc.baseWallet.AuditContract(coinID, contract, txData, rebroadcast)
}

full, ok := btc.node.(*rpcClient)
if !ok {
return nil, fmt.Errorf("wallet backend not a *rpcClient: %T", btc.node)
}

// In the off chance that the transaction is in mempool or txindex is
// enabled, try getrawtransaction.
txHash, _, err := decodeCoinID(coinID)
if err != nil {
return nil, err
}
tx, err := full.GetRawTransaction(txHash)
if err != nil {
btc.log.Warnf("Contract transaction %v could not be retrieved: %v", txHash, err)
// Try with gettxout in the baseWallet method.
return btc.baseWallet.AuditContract(coinID, contract, txData, rebroadcast)
}

txData, err = btc.serializeTx(tx) // if error, we'll just pass nil and let it try
if err != nil {
return nil, err
}

return btc.baseWallet.AuditContract(coinID, contract, txData, rebroadcast)
}

// LocktimeExpired returns true if the specified contract's locktime has
// expired, making it possible to issue a Refund.
func (btc *baseWallet) LocktimeExpired(contract dex.Bytes) (bool, time.Time, error) {
Expand Down Expand Up @@ -2773,7 +2753,7 @@ func (btc *baseWallet) refundTx(txHash *chainhash.Hash, vout uint32, contract de

// Create the transaction that spends the contract.
feeRate := btc.targetFeeRateWithFallback(2, feeSuggestion) // meh level urgency
msgTx := wire.NewMsgTx(wire.TxVersion)
msgTx := wire.NewMsgTx(btc.txVersion())
msgTx.LockTime = uint32(lockTime)
prevOut := wire.NewOutPoint(txHash, vout)
txIn := wire.NewTxIn(prevOut, []byte{}, nil)
Expand Down Expand Up @@ -3609,6 +3589,12 @@ func (btc *baseWallet) wireBytes(tx *wire.MsgTx) []byte {
return buf.Bytes()
}

// GetBestBlockHeight is exported for use by clone wallets. Not part of the
// asset.Wallet interface.
func (btc *baseWallet) GetBestBlockHeight() (int32, error) {
return btc.node.getBestBlockHeight()
}

// Convert the BTC value to satoshi.
func toSatoshi(v float64) uint64 {
return uint64(math.Round(v * conventionalConversionFactor))
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/rpcclient.go
Expand Up @@ -729,7 +729,7 @@ func msgTxFromHex(txHex string) (*wire.MsgTx, error) {

// msgTxFromBytes creates a wire.MsgTx by deserializing the transaction.
func msgTxFromBytes(txB []byte) (*wire.MsgTx, error) {
msgTx := wire.NewMsgTx(wire.TxVersion)
msgTx := new(wire.MsgTx)
if err := msgTx.Deserialize(bytes.NewReader(txB)); err != nil {
return nil, err
}
Expand Down

0 comments on commit 87c6af1

Please sign in to comment.