Skip to content

Commit

Permalink
add /getassetmedia + /createutxos size testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbus committed Jul 4, 2024
1 parent 2b32668 commit 554bd87
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,15 @@ pub(crate) struct MakerInitResponse {
#[derive(Deserialize, Serialize)]
pub(crate) struct Media {
pub(crate) file_path: String,
pub(crate) digest: String,
pub(crate) mime: String,
}

impl From<RgbLibMedia> for Media {
fn from(value: RgbLibMedia) -> Self {
Self {
file_path: value.file_path,
digest: value.digest,
mime: value.mime,
}
}
Expand Down
68 changes: 66 additions & 2 deletions src/test/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ const NODE1_PEER_PORT: u16 = 9821;
async fn issue() {
initialize();

let amt = 5000;
let file_path = "README.md";

let test_dir_node1 = format!("{TEST_DIR_BASE}node1");
let (node1_addr, _) = start_node(&test_dir_node1, NODE1_PEER_PORT, false).await;

fund_and_create_utxos(node1_addr).await;

let asset_cfa = issue_asset_cfa(node1_addr).await;
// check /createutxos size parameter
let unspents_1 = list_unspents(node1_addr).await;
create_utxos(node1_addr, false, Some(1), Some(amt)).await;
let unspents_2 = list_unspents(node1_addr).await;
assert_eq!(unspents_1.len(), unspents_2.len() - 1);
assert!(!unspents_1.iter().any(|u| u.utxo.btc_amount == amt as u64));
assert!(unspents_2.iter().any(|u| u.utxo.btc_amount == amt as u64));

// issue assets
let asset_cfa = issue_asset_cfa(node1_addr, Some(file_path)).await;
let asset_nia = issue_asset_nia(node1_addr).await;
let asset_uda = issue_asset_uda(node1_addr).await;
let asset_uda = issue_asset_uda(node1_addr, Some(file_path)).await;

// check /listassets
let assets = list_assets(node1_addr).await;
let assets_cfa = assets.cfa.unwrap();
let assets_nia = assets.nia.unwrap();
Expand All @@ -31,4 +44,55 @@ async fn issue() {
assert_eq!(cfa_asset.asset_id, asset_cfa.asset_id);
assert_eq!(nia_asset.asset_id, asset_nia.asset_id);
assert_eq!(uda_asset.asset_id, asset_uda.asset_id);

// check /getassetmedia
let mut buf_reader = tokio::io::BufReader::new(tokio::fs::File::open(file_path).await.unwrap());
let mut file_bytes = Vec::new();
buf_reader.read_to_end(&mut file_bytes).await.unwrap();

let cfa_digest = &cfa_asset.media.as_ref().unwrap().digest;
let cfa_media_hex = get_asset_media(node1_addr, cfa_digest).await;
let cfa_media_bytes = hex_str_to_vec(&cfa_media_hex).unwrap();
assert_eq!(cfa_media_bytes, file_bytes);

let uda_digest = &uda_asset
.token
.as_ref()
.unwrap()
.media
.as_ref()
.unwrap()
.digest;
let uda_media_hex = get_asset_media(node1_addr, uda_digest).await;
let uda_media_bytes = hex_str_to_vec(&uda_media_hex).unwrap();
assert_eq!(uda_media_bytes, file_bytes);

// check /getassetmedia invalid digest errors
let payload = GetAssetMediaRequest { digest: s!("a") };
let res = reqwest::Client::new()
.post(format!("http://{}/getassetmedia", node1_addr))
.json(&payload)
.send()
.await
.unwrap();
check_response_is_nok(
res,
reqwest::StatusCode::BAD_REQUEST,
"Invalid media digest",
)
.await;

let payload = GetAssetMediaRequest { digest: s!("") };
let res = reqwest::Client::new()
.post(format!("http://{}/getassetmedia", node1_addr))
.json(&payload)
.send()
.await
.unwrap();
check_response_is_nok(
res,
reqwest::StatusCode::INTERNAL_SERVER_ERROR,
"IO error: Is a directory (os error 21)",
)
.await;
}
86 changes: 60 additions & 26 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::process::{Command, Stdio};
use std::str::FromStr;
use std::sync::{Once, RwLock};
use time::OffsetDateTime;
use tokio::io::AsyncReadExt;
use tracing_test::traced_test;

use crate::error::APIErrorResponse;
Expand All @@ -18,19 +19,19 @@ use crate::routes::{
BackupRequest, BtcBalanceResponse, ChangePasswordRequest, Channel, CloseChannelRequest,
ConnectPeerRequest, CreateUtxosRequest, DecodeLNInvoiceRequest, DecodeLNInvoiceResponse,
DecodeRGBInvoiceRequest, DecodeRGBInvoiceResponse, DisconnectPeerRequest, EmptyResponse,
HTLCStatus, InitRequest, InitResponse, InvoiceStatus, InvoiceStatusRequest,
InvoiceStatusResponse, IssueAssetCFARequest, IssueAssetCFAResponse, IssueAssetNIARequest,
IssueAssetNIAResponse, IssueAssetUDARequest, IssueAssetUDAResponse, KeysendRequest,
KeysendResponse, LNInvoiceRequest, LNInvoiceResponse, ListAssetsRequest, ListAssetsResponse,
ListChannelsResponse, ListPaymentsResponse, ListPeersResponse, ListSwapsResponse,
ListTransactionsResponse, ListTransfersRequest, ListTransfersResponse, ListUnspentsResponse,
MakerExecuteRequest, MakerInitRequest, MakerInitResponse, NetworkInfoResponse,
NodeInfoResponse, OpenChannelRequest, OpenChannelResponse, Payment, Peer, RestoreRequest,
RgbInvoiceRequest, RgbInvoiceResponse, SendAssetRequest, SendAssetResponse, SendBtcRequest,
SendBtcResponse, SendPaymentRequest, SendPaymentResponse, SwapStatus, TakerRequest,
Transaction, Transfer, UnlockRequest, Unspent,
GetAssetMediaRequest, GetAssetMediaResponse, HTLCStatus, InitRequest, InitResponse,
InvoiceStatus, InvoiceStatusRequest, InvoiceStatusResponse, IssueAssetCFARequest,
IssueAssetCFAResponse, IssueAssetNIARequest, IssueAssetNIAResponse, IssueAssetUDARequest,
IssueAssetUDAResponse, KeysendRequest, KeysendResponse, LNInvoiceRequest, LNInvoiceResponse,
ListAssetsRequest, ListAssetsResponse, ListChannelsResponse, ListPaymentsResponse,
ListPeersResponse, ListSwapsResponse, ListTransactionsResponse, ListTransfersRequest,
ListTransfersResponse, ListUnspentsResponse, MakerExecuteRequest, MakerInitRequest,
MakerInitResponse, NetworkInfoResponse, NodeInfoResponse, OpenChannelRequest,
OpenChannelResponse, Payment, Peer, RestoreRequest, RgbInvoiceRequest, RgbInvoiceResponse,
SendAssetRequest, SendAssetResponse, SendBtcRequest, SendBtcResponse, SendPaymentRequest,
SendPaymentResponse, SwapStatus, TakerRequest, Transaction, Transfer, UnlockRequest, Unspent,
};
use crate::utils::PROXY_ENDPOINT_REGTEST;
use crate::utils::{hex_str_to_vec, PROXY_ENDPOINT_REGTEST};

use super::*;

Expand Down Expand Up @@ -348,6 +349,37 @@ async fn connect_peer(node_address: SocketAddr, peer_pubkey: &str, peer_addr: &s
.unwrap();
}

async fn create_utxos(node_address: SocketAddr, up_to: bool, num: Option<u8>, size: Option<u32>) {
println!(
"creating{}{} UTXOs{} for node {node_address}",
if up_to { " up to" } else { "" },
if num.is_some() {
format!(" {}", num.unwrap())
} else {
s!("")
},
if size.is_some() {
format!(" of size {}", size.unwrap())
} else {
s!("")
},
);

let num = if num.is_some() { num } else { Some(10) };
let payload = CreateUtxosRequest { up_to, num, size };
let res = reqwest::Client::new()
.post(format!("http://{}/createutxos", node_address))
.json(&payload)
.send()
.await
.unwrap();
_check_response_is_ok(res)
.await
.json::<EmptyResponse>()
.await
.unwrap();
}

async fn decode_ln_invoice(node_address: SocketAddr, invoice: &str) -> DecodeLNInvoiceResponse {
println!("decoding LN invoice {invoice} for node {node_address}");
let payload = DecodeLNInvoiceRequest {
Expand Down Expand Up @@ -403,31 +435,33 @@ async fn disconnect_peer(node_address: SocketAddr, peer_pubkey: &str) {
}

async fn fund_and_create_utxos(node_address: SocketAddr) {
println!("funding wallet and creating UTXOs for node {node_address}");
println!("funding wallet for node {node_address}");
let addr = address(node_address).await;

_fund_wallet(addr);
mine(false);

create_utxos(node_address, false, Some(10), None).await;
mine(false);
}

let payload = CreateUtxosRequest {
up_to: false,
num: Some(10),
size: None,
async fn get_asset_media(node_address: SocketAddr, digest: &str) -> String {
println!("requesting media for digest {digest} from node {node_address}");
let payload = GetAssetMediaRequest {
digest: digest.to_string(),
};
let res = reqwest::Client::new()
.post(format!("http://{}/createutxos", node_address))
.post(format!("http://{}/getassetmedia", node_address))
.json(&payload)
.send()
.await
.unwrap();
_check_response_is_ok(res)
.await
.json::<EmptyResponse>()
.json::<GetAssetMediaResponse>()
.await
.unwrap();

mine(false);
.unwrap()
.bytes_hex
}

async fn invoice_status(node_address: SocketAddr, invoice: &str) -> InvoiceStatus {
Expand All @@ -449,14 +483,14 @@ async fn invoice_status(node_address: SocketAddr, invoice: &str) -> InvoiceStatu
.status
}

async fn issue_asset_cfa(node_address: SocketAddr) -> AssetCFA {
async fn issue_asset_cfa(node_address: SocketAddr, file_path: Option<&str>) -> AssetCFA {
println!("issuing CFA asset on node {node_address}");
let payload = IssueAssetCFARequest {
amounts: vec![2000],
name: s!("Collectible"),
details: None,
precision: 0,
file_path: None,
file_path: file_path.map(|fp| fp.to_string()),
};
let res = reqwest::Client::new()
.post(format!("http://{}/issueassetcfa", node_address))
Expand Down Expand Up @@ -494,14 +528,14 @@ async fn issue_asset_nia(node_address: SocketAddr) -> AssetNIA {
.asset
}

async fn issue_asset_uda(node_address: SocketAddr) -> AssetUDA {
async fn issue_asset_uda(node_address: SocketAddr, file_path: Option<&str>) -> AssetUDA {
println!("issuing UDA asset on node {node_address}");
let payload = IssueAssetUDARequest {
ticker: s!("UNI"),
name: s!("Unique"),
details: None,
precision: 0,
media_file_path: None,
media_file_path: file_path.map(|fp| fp.to_string()),
attachments_file_paths: vec![],
};
let res = reqwest::Client::new()
Expand Down

0 comments on commit 554bd87

Please sign in to comment.