Skip to content

Commit

Permalink
Fix merkle root display, display available balances, bump utoipa to 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Nov 19, 2024
1 parent d8adb3c commit 3716ae0
Show file tree
Hide file tree
Showing 17 changed files with 417 additions and 264 deletions.
108 changes: 53 additions & 55 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tonic-reflection = "0.12.2"
tracing = "0.1.40"
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3.18", features = ["json"] }
utoipa = "4.2.3"
utoipa = "5.2.0"

[dev-dependencies.bip300301]
git = "https://github.com/Ash-L2L/bip300301.git"
Expand Down
2 changes: 1 addition & 1 deletion app/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl BottomPanel {
/// Updates values
fn update(&mut self, app: &App) {
self.balance = match app.wallet.get_balance() {
Ok(balance) => Some(Some(balance)),
Ok(balance) => Some(Some(balance.total)),
Err(err) => {
let err = anyhow::Error::from(err);
tracing::error!("Failed to update balance: {err:#}");
Expand Down
17 changes: 8 additions & 9 deletions app/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use jsonrpsee::{
use thunder::{
node,
types::{Address, PointedOutput, Txid},
wallet,
wallet::{self, Balance},
};
use thunder_app_rpc_api::RpcServer;

Expand Down Expand Up @@ -47,11 +47,8 @@ fn convert_wallet_err(err: wallet::Error) -> ErrorObject<'static> {

#[async_trait]
impl RpcServer for RpcServerImpl {
async fn balance(&self) -> RpcResult<u64> {
match self.app.wallet.get_balance() {
Ok(balance) => Ok(balance.to_sat()),
Err(err) => Err(convert_wallet_err(err)),
}
async fn balance(&self) -> RpcResult<Balance> {
self.app.wallet.get_balance().map_err(convert_wallet_err)
}

async fn create_deposit(
Expand Down Expand Up @@ -167,11 +164,13 @@ impl RpcServer for RpcServerImpl {
.map_err(convert_wallet_err)
}

async fn sidechain_wealth(&self) -> RpcResult<bitcoin::Amount> {
self.app
async fn sidechain_wealth_sats(&self) -> RpcResult<u64> {
let sidechain_wealth = self
.app
.node
.get_sidechain_wealth()
.map_err(convert_node_err)
.map_err(convert_node_err)?;
Ok(sidechain_wealth.to_sat())
}

async fn stop(&self) {
Expand Down
11 changes: 9 additions & 2 deletions app/tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ async fn regtest_test() -> anyhow::Result<()> {
// Verify that there are no deposits on Thunder
{
let balance = thunderd_client.balance().await?;
anyhow::ensure!(balance == 0, "Expected 0 balance, but got {balance}");
anyhow::ensure!(
balance.total == bitcoin::Amount::ZERO,
"Expected 0 balance, but got {}",
balance.total
);
}
// Mine a BMM block to process the deposit
let () = mine_thunder_block(
Expand All @@ -363,7 +367,10 @@ async fn regtest_test() -> anyhow::Result<()> {
// Verify that the deposit was successful
{
let balance = thunderd_client.balance().await?;
anyhow::ensure!(balance > 0, "Expected positive balance");
anyhow::ensure!(
balance.total > bitcoin::Amount::ZERO,
"Expected positive balance"
);
}

/* Clean up */
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde_json = "1.0.113"
thunder = { path = "../lib" }
thunder_app_rpc_api = { path = "../rpc-api" }
tokio = "1.29.1"
utoipa = "4.2.3"
utoipa = "5.2.0"

[lib]
name = "thunder_app_cli_lib"
Expand Down
5 changes: 3 additions & 2 deletions cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Cli {
let res = match self.command {
Command::Balance => {
let balance = rpc_client.balance().await?;
format!("{balance}")
serde_json::to_string_pretty(&balance)?
}
Command::ConnectPeer { addr } => {
let () = rpc_client.connect_peer(addr).await?;
Expand Down Expand Up @@ -151,7 +151,8 @@ impl Cli {
String::default()
}
Command::SidechainWealth => {
let sidechain_wealth = rpc_client.sidechain_wealth().await?;
let sidechain_wealth =
rpc_client.sidechain_wealth_sats().await?;
format!("{sidechain_wealth}")
}
Command::Stop => {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tokio-stream = { version = "0.1.15", features = ["sync"] }
tokio-util = { version = "0.7.10", features = ["rt"] }
tonic = "0.12.3"
tracing = "0.1.40"
utoipa = "4.2.3"
utoipa = { version = "5.2.0", features = ["non_strict_integers"] }

[lib]
name = "thunder"
Expand Down
22 changes: 3 additions & 19 deletions lib/types/address.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeAs, DisplayFromStr};
use utoipa::ToSchema;

#[derive(Debug, thiserror::Error)]
pub enum AddressParseError {
Expand All @@ -11,8 +12,9 @@ pub enum AddressParseError {
}

#[derive(
BorshDeserialize, BorshSerialize, Clone, Copy, Eq, PartialEq, Hash,
BorshDeserialize, BorshSerialize, Clone, Copy, Eq, Hash, PartialEq, ToSchema,
)]
#[schema(value_type = String)]
pub struct Address(pub [u8; 20]);

impl Address {
Expand Down Expand Up @@ -78,21 +80,3 @@ impl Serialize for Address {
}
}
}

impl utoipa::PartialSchema for Address {
fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
let obj = utoipa::openapi::Object::with_type(
utoipa::openapi::SchemaType::String,
);
utoipa::openapi::RefOr::T(utoipa::openapi::Schema::Object(obj))
}
}

impl utoipa::ToSchema<'static> for Address {
fn schema() -> (
&'static str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
("Address", <Address as utoipa::PartialSchema>::schema())
}
}
33 changes: 11 additions & 22 deletions lib/types/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl std::fmt::Debug for BlockHash {
PartialOrd,
Serialize,
)]
pub struct MerkleRoot(Hash);
pub struct MerkleRoot(#[serde(with = "serde_hexstr_human_readable")] Hash);

impl From<Hash> for MerkleRoot {
fn from(other: Hash) -> Self {
Expand Down Expand Up @@ -105,22 +105,15 @@ impl std::fmt::Debug for MerkleRoot {

impl utoipa::PartialSchema for MerkleRoot {
fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
let obj = utoipa::openapi::Object::with_type(
utoipa::openapi::SchemaType::String,
);
let obj =
utoipa::openapi::Object::with_type(utoipa::openapi::Type::String);
utoipa::openapi::RefOr::T(utoipa::openapi::Schema::Object(obj))
}
}

impl utoipa::ToSchema<'static> for MerkleRoot {
fn schema() -> (
&'static str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
(
"MerkleRoot",
<MerkleRoot as utoipa::PartialSchema>::schema(),
)
impl utoipa::ToSchema for MerkleRoot {
fn name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("MerkleRoot")
}
}

Expand Down Expand Up @@ -186,19 +179,15 @@ impl FromStr for Txid {

impl utoipa::PartialSchema for Txid {
fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
let obj = utoipa::openapi::Object::with_type(
utoipa::openapi::SchemaType::String,
);
let obj =
utoipa::openapi::Object::with_type(utoipa::openapi::Type::String);
utoipa::openapi::RefOr::T(utoipa::openapi::Schema::Object(obj))
}
}

impl utoipa::ToSchema<'static> for Txid {
fn schema() -> (
&'static str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
("Txid", <Txid as utoipa::PartialSchema>::schema())
impl utoipa::ToSchema for Txid {
fn name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("Txid")
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use thiserror::Error;
mod address;
pub mod hashes;
pub mod proto;
pub mod schema;
mod transaction;

pub use address::Address;
Expand Down
36 changes: 36 additions & 0 deletions lib/types/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Schemas for OpenAPI
use utoipa::{
openapi::{self, RefOr, Schema},
PartialSchema, ToSchema,
};

pub struct BitcoinAddr;

impl PartialSchema for BitcoinAddr {
fn schema() -> RefOr<Schema> {
let obj = utoipa::openapi::Object::with_type(openapi::Type::String);
RefOr::T(Schema::Object(obj))
}
}

impl ToSchema for BitcoinAddr {
fn name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("bitcoin.Address")
}
}

pub struct BitcoinOutPoint;

impl PartialSchema for BitcoinOutPoint {
fn schema() -> RefOr<Schema> {
let obj = utoipa::openapi::Object::new();
RefOr::T(Schema::Object(obj))
}
}

impl ToSchema for BitcoinOutPoint {
fn name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("bitcoin.OutPoint")
}
}
Loading

0 comments on commit 3716ae0

Please sign in to comment.