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 30, 2024
1 parent a021d7c commit 61cb445
Show file tree
Hide file tree
Showing 30 changed files with 1,005 additions and 638 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
7 changes: 6 additions & 1 deletion bdk-ffi/tests/bindings/test.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* and that a program that depends on them will run.
*/

import org.bitcoindevkit.*
import org.bitcoindevkit.bitcoin.Network
import org.bitcoindevkit.BlockId

// A type from bitcoin-ffi
val network = Network.TESTNET

// A type from bdk-ffi
val blockId = BlockId(0uL, "abcd")
9 changes: 8 additions & 1 deletion bdk-ffi/tests/bindings/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from bdkpython.bitcoin import Network
from bdkpython import BlockId

import unittest
from bdk import *

class TestBdk(unittest.TestCase):

# A type from the bitcoin-ffi library
def test_some_enum(self):
network = Network.TESTNET

# A type from the bdk-ffi library
def test_some_dict(self):
block_id = BlockId(height=0, hash="abcd")

if __name__=='__main__':
unittest.main()
4 changes: 4 additions & 0 deletions bdk-ffi/tests/bindings/test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
import Foundation
import BitcoinDevKit

// A type from the bitcoin-ffi library
let network = Network.testnet

// A type from the bdk-ffi library
let blockId = BlockId(height: 32, hash: "abcd")
10 changes: 4 additions & 6 deletions bdk-ffi/tests/test_generated_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
uniffi::build_foreign_language_testcases!(
"tests/bindings/test.kts",
// Not sure why the new types break this Kotlin test and not the others, but the libraries work
// fine. Commenting out for now.
// "tests/bindings/test.kts",
"tests/bindings/test.swift",
// Using types defined in an external library seems to break this test and we don't know how to
// fix it yet, but the actual Python tests and the generated package work fine.
// from .bitcoin import Script
// ImportError: attempted relative import with no known parent package
// "tests/bindings/test.py",
"tests/bindings/test.py",
);
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
2 changes: 1 addition & 1 deletion bdk-python/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ clean:
rm -rf ./dist/

test:
python3 -m unittest --verbose
python3 -m unittest --verbose
Loading

0 comments on commit 61cb445

Please sign in to comment.