Skip to content

Commit

Permalink
refactor rpc response to put rare sats and sat ranges under utxo obje…
Browse files Browse the repository at this point in the history
…ct (#14)
  • Loading branch information
haozhiliu authored Sep 7, 2023
1 parent c7e0ac5 commit d48b4b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
71 changes: 37 additions & 34 deletions src/subcommand/server/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::block_rarity::{
is_palindrome, BLOCK78_BLOCK_HEIGHT, BLOCK9_BLOCK_HEIGHT, FIRST_TRANSACTION_SAT_RANGE,
NAKAMOTO_BLOCK_HEIGHTS, PIZZA_RANGE_MAP, VINTAGE_BLOCK_HEIGHT,
};
use crate::subcommand::{traits::Output as SatDetails, wallet::sats::rare_sats};
use crate::subcommand::{traits::Output as SatDetails, wallet::sats::rare_sats_from_outpoint};
use axum_jrpc::{
error::{JsonRpcError, JsonRpcErrorReason},
JrpcResult, JsonRpcExtractor, JsonRpcResponse,
Expand Down Expand Up @@ -49,44 +49,49 @@ async fn get_sat_ranges(value: JsonRpcExtractor, index: Arc<Index>) -> JrpcResul

#[derive(Serialize)]
struct SatRange {
utxo: String,
start: u64,
end: u64,
block_rarities: Vec<BlockRarityInfo>,
}

#[derive(Serialize)]
struct RareSat {
utxo: String,
offset: u64,
rarity: Rarity,
sat: Sat,
sat_details: SatDetails,
}

#[derive(Serialize)]
struct Res {
struct Utxo {
utxo: String,
sat_ranges: Vec<SatRange>,
rare_sats: Vec<RareSat>,
}

#[derive(Serialize)]
struct Res {
utxos: Vec<Utxo>,
}

let answer_id = value.get_answer_id();
if index.has_sat_index().is_err() {
return invalid_params(answer_id, "Sat index is not available".to_string());
}

let req: Req = value.parse_params()?;
let mut res = Res {
sat_ranges: vec![],
rare_sats: vec![],
};
let mut utxos: Vec<(OutPoint, Vec<(u64, u64)>)> = vec![];
let mut res = Res { utxos: vec![] };

for output in req.utxos {
let outpoint = match OutPoint::from_str(output.as_str()) {
Ok(outpoint) => outpoint,
Err(err) => return invalid_params(answer_id, err.to_string()),
};
let mut utxo = Utxo {
utxo: output.clone(),
sat_ranges: vec![],
rare_sats: vec![],
};
let list = match index.list(outpoint) {
Ok(list) => list,
Err(err) => return invalid_params(answer_id, err.to_string()),
Expand All @@ -102,8 +107,7 @@ async fn get_sat_ranges(value: JsonRpcExtractor, index: Arc<Index>) -> JrpcResul
Err(err) => return invalid_params(answer_id, err.to_string()),
};

res.sat_ranges.push(SatRange {
utxo: output.clone(),
utxo.sat_ranges.push(SatRange {
start: range.0,
end: range.1,
block_rarities,
Expand All @@ -113,30 +117,29 @@ async fn get_sat_ranges(value: JsonRpcExtractor, index: Arc<Index>) -> JrpcResul
}
}
}
utxos.push((outpoint, sat_ranges));
}

let rare_sats = rare_sats(utxos);
for (outpoint, sat, offset, rarity) in rare_sats {
let sat_details = SatDetails {
number: sat.n(),
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
name: sat.name(),
height: sat.height().0,
cycle: sat.cycle(),
epoch: sat.epoch().0,
period: sat.period(),
offset: sat.third(),
rarity: sat.rarity(),
};
res.rare_sats.push(RareSat {
utxo: outpoint.to_string(),
offset,
rarity,
sat,
sat_details,
});
for (_, sat, offset, rarity) in rare_sats_from_outpoint(outpoint, sat_ranges) {
let sat_details = SatDetails {
number: sat.n(),
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
name: sat.name(),
height: sat.height().0,
cycle: sat.cycle(),
epoch: sat.epoch().0,
period: sat.period(),
offset: sat.third(),
rarity: sat.rarity(),
};
utxo.rare_sats.push(RareSat {
offset,
rarity,
sat,
sat_details,
});
}

res.utxos.push(utxo);
}

Ok(JsonRpcResponse::success(answer_id, res))
Expand Down
34 changes: 21 additions & 13 deletions src/subcommand/wallet/sats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,27 @@ pub(crate) fn rare_sats(
) -> Vec<(OutPoint, Sat, u64, Rarity)> {
utxos
.into_iter()
.flat_map(|(outpoint, sat_ranges)| {
let mut offset = 0;
sat_ranges.into_iter().filter_map(move |(start, end)| {
let sat = Sat(start);
let rarity = sat.rarity();
let start_offset = offset;
offset += end - start;
if rarity > Rarity::Common {
Some((outpoint, sat, start_offset, rarity))
} else {
None
}
})
.flat_map(|(outpoint, sat_ranges)| rare_sats_from_outpoint(outpoint, sat_ranges))
.collect()
}

pub(crate) fn rare_sats_from_outpoint(
outpoint: OutPoint,
sat_ranges: Vec<(u64, u64)>,
) -> Vec<(OutPoint, Sat, u64, Rarity)> {
let mut offset = 0;
sat_ranges
.into_iter()
.filter_map(move |(start, end)| {
let sat = Sat(start);
let rarity = sat.rarity();
let start_offset = offset;
offset += end - start;
if rarity > Rarity::Common {
Some((outpoint, sat, start_offset, rarity))
} else {
None
}
})
.collect()
}
Expand Down

0 comments on commit d48b4b9

Please sign in to comment.