Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return txids #417

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitmask-core"
version = "0.7.0-beta.7"
version = "0.7.0-beta.8"
authors = [
"Jose Diego Robles <[email protected]>",
"Hunter Trujillo <[email protected]>",
Expand Down Expand Up @@ -91,6 +91,7 @@ strict_types = "1.6.3"
thiserror = "1.0"
tokio = { version = "1.33.0", features = ["macros", "sync"] }
zeroize = "1.6.0"
walkdir = "2.4.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
bdk = { version = "0.28.2", features = [
Expand Down
2 changes: 2 additions & 0 deletions lib/web/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,6 @@ export interface WalletData {
export interface FundVaultDetails {
assetsOutput?: string;
udasOutput?: string;
isFunded: boolean;
fundTxid: string;
}
4 changes: 2 additions & 2 deletions lib/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Francisco Calderón <[email protected]>"
],
"description": "Core functionality for the BitMask wallet",
"version": "0.7.0-beta.7",
"version": "0.7.0-beta.8",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions lib/web/rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ export interface RgbTransferResponse {
psbt: string;
/// Tapret Commitment (used to spend output)
commit: string;
/// Transfer Bitcoin L1 transaction id
txid: string;
}

export interface AcceptRequest {
Expand Down Expand Up @@ -757,6 +759,7 @@ export interface BatchRgbTransferItem {
status: TxStatus;
isAccept: boolean;
iface: string;
txid: string;
}

export interface RgbOfferRequest {
Expand Down
24 changes: 21 additions & 3 deletions src/bin/bitmaskd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use axum::{
use bitcoin_30::secp256k1::{ecdh::SharedSecret, PublicKey, SecretKey};
use bitmask_core::{
bitcoin::{save_mnemonic, sign_and_publish_psbt_file},
carbonado::{handle_file, server_retrieve, server_store, store},
carbonado::{handle_file, metrics::metrics_csv, server_retrieve, server_store, store},
constants::{
get_marketplace_nostr_key, get_marketplace_seed, get_network, get_udas_utxo, switch_network,
},
Expand Down Expand Up @@ -704,11 +704,27 @@ async fn send_coins(
Path((address, amount)): Path<(String, String)>,
) -> Result<impl IntoResponse, AppError> {
use bitmask_core::regtest::send_coins;

send_coins(&address, &amount);

Ok("Ok")
}

async fn json_metrics() -> Result<impl IntoResponse, AppError> {
use bitmask_core::carbonado::metrics::metrics;
let path = std::env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let dir = std::path::Path::new(&path);

Ok(Json(metrics(dir)?))
}

async fn csv_metrics() -> Result<impl IntoResponse, AppError> {
use bitmask_core::carbonado::metrics::metrics;
let path = std::env::var("CARBONADO_DIR").unwrap_or("/tmp/bitmaskd/carbonado".to_owned());
let dir = std::path::Path::new(&path);

Ok(metrics_csv(metrics(dir)?))
}

#[tokio::main]
async fn main() -> Result<()> {
if env::var("RUST_LOG").is_err() {
Expand Down Expand Up @@ -759,7 +775,9 @@ async fn main() -> Result<()> {
.route("/proxy/consignment/:id", get(rgb_proxy_consig_retrieve))
.route("/proxy/media-metadata", post(rgb_proxy_media_data_save))
.route("/proxy/media-metadata/:id", get(rgb_proxy_media_retrieve))
.route("/proxy/media/:id", get(rgb_proxy_metadata_retrieve));
.route("/proxy/media/:id", get(rgb_proxy_metadata_retrieve))
.route("/metrics.json", get(json_metrics))
.route("/metrics.csv", get(csv_metrics));

let network = get_network().await;
switch_network(&network).await?;
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ pub async fn fund_vault(
assets_output,
udas_output,
is_funded: true,
fund_txid: Some(asset_txid.to_hex()),
})
}

Expand Down Expand Up @@ -459,6 +460,7 @@ pub async fn get_assets_vault(
assets_output,
udas_output,
is_funded,
fund_txid: None,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/carbonado.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bitcoin_30::secp256k1::{PublicKey, SecretKey};
use crate::{carbonado::error::CarbonadoError, constants::NETWORK, info, structs::FileMetadata};

pub mod error;
pub mod metrics;

#[cfg(not(target_arch = "wasm32"))]
pub use server::{handle_file, retrieve, retrieve_metadata, server_retrieve, server_store, store};
Expand Down
204 changes: 204 additions & 0 deletions src/carbonado/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use std::{collections::BTreeMap, path::Path, time::SystemTime};

use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;

#[derive(Serialize, Deserialize, Default)]
pub struct MetricsResponse {
bytes: u64,
bytes_by_day: BTreeMap<String, u64>,
bitcoin_wallets_by_day: BTreeMap<String, usize>,
wallets_by_network: BTreeMap<String, usize>,
}

pub fn metrics(dir: &Path) -> Result<MetricsResponse> {
let mut response = MetricsResponse::default();

response.wallets_by_network.insert("bitcoin".to_string(), 0);
response.wallets_by_network.insert("testnet".to_string(), 0);
response.wallets_by_network.insert("signet".to_string(), 0);
response.wallets_by_network.insert("regtest".to_string(), 0);
response.wallets_by_network.insert("total".to_string(), 0);

let mut total_wallets = 0;
let mut day_prior = None;

for entry in WalkDir::new(dir) {
let entry = entry?;
let filename = entry.file_name().to_string_lossy().to_string();
let metadata = entry.metadata()?;
let day = metadata.created()?;
let day = round_system_time_to_day(day);

if metadata.is_file() {
response.bytes += metadata.len();

let bytes_total_day_prior = if let Some(dp) = &day_prior {
response.bytes_by_day.get(dp).unwrap_or(&0).to_owned()
} else {
0
};

let bitcoin_wallets_total_day_prior = if let Some(dp) = day_prior {
response
.bitcoin_wallets_by_day
.get(&dp)
.unwrap_or(&0)
.to_owned()
} else {
0
};

*response
.bytes_by_day
.entry(day.clone())
.or_insert(bytes_total_day_prior) += metadata.len();

if filename
== "bitcoin-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
{
*response
.wallets_by_network
.get_mut("bitcoin")
.unwrap_or(&mut 0) += 1;
*response
.bitcoin_wallets_by_day
.entry(day.clone())
.or_insert(bitcoin_wallets_total_day_prior) += 1;
}

if filename
== "testnet-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
{
*response
.wallets_by_network
.get_mut("testnet")
.unwrap_or(&mut 0) += 1;
}

if filename
== "signet-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
{
*response
.wallets_by_network
.get_mut("signet")
.unwrap_or(&mut 0) += 1;
}

if filename
== "regtest-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
{
*response
.wallets_by_network
.get_mut("regtest")
.unwrap_or(&mut 0) += 1;
}

if filename == "bitcoin-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
|| filename == "testnet-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
|| filename == "signet-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
|| filename == "regtest-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15"
{
total_wallets += 1;
}
}

day_prior = Some(day);
}

*response
.wallets_by_network
.get_mut("total")
.unwrap_or(&mut 0) = total_wallets;

Ok(response)
}

pub fn metrics_csv(metrics: MetricsResponse) -> String {
let lines = vec![vec![
"Wallet",
"Wallet Count",
"Bytes Total",
"Day",
"Bitcoin Wallets by Day",
"Bytes by Day",
]];

for (day, bitcoin_wallets) in metrics.bitcoin_wallets_by_day {
let mut line = vec![];

if lines.len() == 1 {
line.push("Bitcoin".to_string());
line.push(
metrics
.wallets_by_network
.get("bitcoin")
.unwrap()
.to_string(),
);
line.push(metrics.bytes.to_string());
}

if lines.len() == 2 {
line.push("Tesnet".to_string());
line.push(
metrics
.wallets_by_network
.get("testnet")
.unwrap()
.to_string(),
);
line.push("".to_owned());
}

if lines.len() == 3 {
line.push("signet".to_string());
line.push(
metrics
.wallets_by_network
.get("signet")
.unwrap()
.to_string(),
);
line.push("".to_owned());
}

if lines.len() == 4 {
line.push("regtest".to_string());
line.push(
metrics
.wallets_by_network
.get("regtest")
.unwrap()
.to_string(),
);
line.push("".to_owned());
}

if lines.len() > 4 {
line.push("".to_owned());
line.push("".to_owned());
line.push("".to_owned());
}

line.push(day.clone());
line.push(bitcoin_wallets.to_string());
line.push(metrics.bytes_by_day.get(&day).unwrap().to_string())
}

let lines: Vec<String> = lines.iter().map(|line| line.join(",")).collect();
lines.join("\n")
}

fn round_system_time_to_day(system_time: SystemTime) -> String {
// Convert SystemTime to a Chrono DateTime
let datetime: DateTime<Utc> = system_time.into();

// Round down to the nearest day (00:00:00)
let rounded = datetime.date_naive().and_hms_opt(0, 0, 0).unwrap();

// Format the date as a string in "YYYY-MM-DD" format
rounded.format("%Y-%m-%d").to_string()
}
Loading