Skip to content

Commit

Permalink
test: fix tests for new bitcoin-ffi types
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 28, 2024
1 parent 6d5da2b commit 796eaf7
Show file tree
Hide file tree
Showing 25 changed files with 969 additions and 621 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
import kotlin.test.AfterTest
import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import org.junit.Test
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.runner.RunWith
import java.io.File
import kotlin.test.AfterTest
import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith
import org.rustbitcoin.bitcoin.Network

@RunWith(AndroidJUnit4::class)
class OfflineDescriptorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import kotlin.test.assertFalse
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.runner.RunWith
import java.io.File
import kotlin.test.AfterTest
import java.io.File
import org.rustbitcoin.bitcoin.Network

@RunWith(AndroidJUnit4::class)
class OfflineWalletTest {
Expand Down
10 changes: 0 additions & 10 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,6 @@ interface LoadWithPersistError {
CouldNotLoad();
};

[Error]
interface ParseAmountError {
OutOfRange();
TooPrecise();
MissingDigits();
InputTooLarge();
InvalidCharacter(string error_message);
OtherParseAmountErr();
};

[Error]
interface PersistenceError {
Write(string error_message);
Expand Down
41 changes: 2 additions & 39 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bdk_wallet::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk_wallet::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk_wallet::chain;
use bdk_wallet::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk_wallet::chain::rusqlite::Error as BdkSqliteError;
use bdk_wallet::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
Expand All @@ -24,7 +25,6 @@ use bdk_wallet::CreateWithPersistError as BdkCreateWithPersistError;
use bdk_wallet::LoadWithPersistError as BdkLoadWithPersistError;
use bitcoin_internals::hex::display::DisplayHex;

use bdk_wallet::chain;
use std::convert::TryInto;

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -407,7 +407,7 @@ pub enum RequestBuilderError {
RequestAlreadyConsumed,
}

// #[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error)]
pub enum LoadWithPersistError {
#[error("sqlite persistence error: {error_message}")]
Persist { error_message: String },
Expand All @@ -419,28 +419,6 @@ pub enum LoadWithPersistError {
CouldNotLoad,
}

#[derive(Debug, thiserror::Error)]
// pub enum ParseAmountError {
// #[error("amount out of range")]
// OutOfRange,
//
// #[error("amount has a too high precision")]
// TooPrecise,
//
// #[error("the input has too few digits")]
// MissingDigits,
//
// #[error("the input is too large")]
// InputTooLarge,
//
// #[error("invalid character: {error_message}")]
// InvalidCharacter { error_message: String },
//
// // Has to handle non-exhaustive
// #[error("unknown parse amount error")]
// OtherParseAmountErr,
// }

#[derive(Debug, thiserror::Error)]
pub enum PersistenceError {
#[error("writing to persistence error: {error_message}")]
Expand Down Expand Up @@ -1088,21 +1066,6 @@ impl From<BdkLoadWithPersistError<chain::rusqlite::Error>> for LoadWithPersistEr
}
}

impl From<BdkParseAmountError> for ParseAmountError {
fn from(error: BdkParseAmountError) -> Self {
match error {
BdkParseAmountError::OutOfRange(_) => ParseAmountError::OutOfRange,
BdkParseAmountError::TooPrecise(_) => ParseAmountError::TooPrecise,
BdkParseAmountError::MissingDigits(_) => ParseAmountError::MissingDigits,
BdkParseAmountError::InputTooLarge(_) => ParseAmountError::InputTooLarge,
BdkParseAmountError::InvalidCharacter(c) => ParseAmountError::InvalidCharacter {
error_message: c.to_string(),
},
_ => ParseAmountError::OtherParseAmountErr,
}
}
}

impl From<std::io::Error> for PersistenceError {
fn from(error: std::io::Error) -> Self {
PersistenceError::Write {
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use bdk_wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bdk_wallet::Update as BdkUpdate;
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;
use bdk_wallet::Update as BdkUpdate;

use std::sync::{Arc, Mutex};

Expand Down
7 changes: 4 additions & 3 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::{Amount, FeeRate, OutPoint, Psbt, Transaction};
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateTxError, CreateWithPersistError,
Expand All @@ -8,6 +8,9 @@ use crate::store::Connection;
use crate::types::{AddressInfo, Balance, CanonicalTx, LocalOutput, ScriptAmount};
use crate::types::{FullScanRequestBuilder, SyncRequestBuilder, Update};

use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::amount::Amount as BdkAmount;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
Expand All @@ -18,8 +21,6 @@ use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::PersistedWallet;
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, SignOptions};
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use std::borrow::BorrowMut;
use std::collections::HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bitcoindevkit

import kotlin.test.Test
import org.rustbitcoin.bitcoin.Network

private const val SIGNET_ELECTRUM_URL = "ssl://mempool.space:60602"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.bitcoindevkit

import org.rustbitcoin.bitcoin.Script
import kotlin.test.Test
import org.rustbitcoin.bitcoin.Script
import org.rustbitcoin.bitcoin.Network

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bitcoindevkit

import kotlin.test.Test
import org.rustbitcoin.bitcoin.Network

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.bitcoindevkit

import java.io.File
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount


private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.bitcoindevkit

import java.io.File
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Amount
import org.rustbitcoin.bitcoin.Network

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.bitcoindevkit

import kotlin.test.Test
import kotlin.test.assertEquals
import org.rustbitcoin.bitcoin.Network

class OfflineDescriptorTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.bitcoindevkit

import kotlin.test.Test
import kotlin.test.assertEquals
import org.rustbitcoin.bitcoin.Network

class OfflinePersistenceTest {
private val persistenceFilePath = run {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.bitcoindevkit

import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import java.io.File
import org.rustbitcoin.bitcoin.Network

class OfflineWalletTest {
private val descriptor: Descriptor = Descriptor(
Expand Down
65 changes: 38 additions & 27 deletions bdk-python/tests/test_live_tx_builder.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import bdkpython as bdk
from bdkpython import Descriptor
from bdkpython import KeychainKind
from bdkpython import FeeRate
from bdkpython import Wallet
from bdkpython import EsploraClient
from bdkpython import ScriptAmount
from bdkpython import FullScanRequest
from bdkpython import Address
from bdkpython import Psbt
from bdkpython import TxBuilder
from bdkpython.bitcoin import Network
from bdkpython.bitcoin import Amount
import unittest
import os

SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"

descriptor: bdk.Descriptor = bdk.Descriptor(
descriptor: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
bdk.Network.TESTNET
Network.TESTNET
)
change_descriptor: bdk.Descriptor = bdk.Descriptor(
change_descriptor: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/1/*)",
bdk.Network.TESTNET
Network.TESTNET
)

class LiveTxBuilderTest(unittest.TestCase):
Expand All @@ -21,19 +32,19 @@ def tearDown(self) -> None:
os.remove("./bdk_persistence.sqlite")

def test_tx_builder(self):
descriptor: bdk.Descriptor = bdk.Descriptor(
descriptor: Descriptor = Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
bdk.Network.SIGNET
Network.SIGNET
)
connection: bdk.Connection = bdk.Connection.new_in_memory()
wallet: bdk.Wallet = bdk.Wallet(
wallet: Wallet = Wallet(
descriptor,
change_descriptor,
bdk.Network.SIGNET,
Network.SIGNET,
connection
)
esplora_client: bdk.EsploraClient = bdk.EsploraClient(url = SIGNET_ESPLORA_URL)
full_scan_request: bdk.FullScanRequest = wallet.start_full_scan().build()
esplora_client: EsploraClient = EsploraClient(url = SIGNET_ESPLORA_URL)
full_scan_request: FullScanRequest = wallet.start_full_scan().build()
update = esplora_client.full_scan(
full_scan_request=full_scan_request,
stop_gap=10,
Expand All @@ -44,28 +55,28 @@ def test_tx_builder(self):
self.assertGreater(
wallet.balance().total.to_sat(),
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address} and try again."
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(KeychainKind.EXTERNAL).address} and try again."
)

recipient = bdk.Address(
recipient = Address(
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
network=bdk.Network.SIGNET
network=Network.SIGNET
)

psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=bdk.Amount.from_sat(4200)).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).finish(wallet)
psbt = TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=Amount.from_sat(4200)).fee_rate(fee_rate=FeeRate.from_sat_per_vb(2)).finish(wallet)

self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")

def complex_tx_builder(self):
connection: bdk.Connection = bdk.Connection.new_in_memory()
wallet: bdk.Wallet = bdk.Wallet(
wallet: Wallet = Wallet(
descriptor,
change_descriptor,
bdk.Network.SIGNET,
Network.SIGNET,
connection
)
esplora_client: bdk.EsploraClient = bdk.EsploraClient(url = SIGNET_ESPLORA_URL)
full_scan_request: bdk.FullScanRequest = wallet.start_full_scan().build()
esplora_client: EsploraClient = EsploraClient(url = SIGNET_ESPLORA_URL)
full_scan_request: FullScanRequest = wallet.start_full_scan().build()
update = esplora_client.full_scan(
full_scan_request=full_scan_request,
stop_gap=10,
Expand All @@ -76,23 +87,23 @@ def complex_tx_builder(self):
self.assertGreater(
wallet.balance().total.to_sat(),
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address} and try again."
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(KeychainKind.EXTERNAL).address} and try again."
)

recipient1 = bdk.Address(
recipient1 = Address(
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
network=bdk.Network.SIGNET
network=Network.SIGNET
)
recipient2 = bdk.Address(
recipient2 = Address(
address="tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6",
network=bdk.Network.SIGNET
network=Network.SIGNET
)
all_recipients = list(
bdk.ScriptAmount(recipient1.script_pubkey, 4200),
bdk.ScriptAmount(recipient2.script_pubkey, 4200)
ScriptAmount(recipient1.script_pubkey, 4200),
ScriptAmount(recipient2.script_pubkey, 4200)
)

psbt: bdk.Psbt = bdk.TxBuilder().set_recipients(all_recipients).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).enable_rbf().finish(wallet)
psbt: Psbt = TxBuilder().set_recipients(all_recipients).fee_rate(fee_rate=FeeRate.from_sat_per_vb(2)).enable_rbf().finish(wallet)
wallet.sign(psbt)

self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")
Expand Down
Loading

0 comments on commit 796eaf7

Please sign in to comment.