Skip to content

Commit

Permalink
Add indexed to output JSON (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Jan 10, 2024
1 parent 6159347 commit 331dbb1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ impl Index {
Ok(true)
}

pub(crate) fn contains_output(&self, output: &OutPoint) -> Result<bool> {
Ok(
self
.database
.begin_read()?
.open_table(OUTPOINT_TO_VALUE)?
.get(&output.store())?
.is_some(),
)
}

pub(crate) fn has_rune_index(&self) -> bool {
self.index_runes
}
Expand Down
8 changes: 8 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ impl Server {
task::block_in_place(|| {
let list = index.list(outpoint)?;

let indexed;

let output = if outpoint == OutPoint::null() || outpoint == unbound_outpoint() {
let mut value = 0;

Expand All @@ -557,11 +559,15 @@ impl Server {
}
}

indexed = true;

TxOut {
value,
script_pubkey: ScriptBuf::new(),
}
} else {
indexed = index.contains_output(&outpoint)?;

index
.get_transaction(outpoint.txid)?
.ok_or_not_found(|| format!("output {outpoint}"))?
Expand All @@ -582,6 +588,7 @@ impl Server {
server_config.chain,
output,
inscriptions,
indexed,
runes
.into_iter()
.map(|(spaced_rune, pile)| (spaced_rune.rune, pile.amount))
Expand Down Expand Up @@ -2611,6 +2618,7 @@ mod tests {
address: None,
transaction: txid.to_string(),
sat_ranges: None,
indexed: true,
inscriptions: Vec::new(),
runes: vec![(Rune(RUNE), 340282366920938463463374607431768211455)]
.into_iter()
Expand Down
3 changes: 3 additions & 0 deletions src/templates/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct OutputJson {
pub address: Option<String>,
pub transaction: String,
pub sat_ranges: Option<Vec<(u64, u64)>>,
pub indexed: bool,
pub inscriptions: Vec<InscriptionId>,
pub runes: BTreeMap<Rune, u128>,
}
Expand All @@ -28,6 +29,7 @@ impl OutputJson {
chain: Chain,
output: TxOut,
inscriptions: Vec<InscriptionId>,
indexed: bool,
runes: BTreeMap<Rune, u128>,
) -> Self {
Self {
Expand All @@ -43,6 +45,7 @@ impl OutputJson {
Some(List::Unspent(ranges)) => Some(ranges),
_ => None,
},
indexed,
inscriptions,
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,29 @@ fn get_output() {
],
..Default::default()
});

rpc_server.mine_blocks(1);

let server = TestServer::spawn_with_server_args(
&rpc_server,
&["--index-sats"],
&["--no-sync", "--enable-json-api"],
);

let response = reqwest::blocking::Client::new()
.get(server.url().join(&format!("/output/{}:0", txid)).unwrap())
.header(reqwest::header::ACCEPT, "application/json")
.send()
.unwrap();

assert_eq!(response.status(), StatusCode::OK);

assert!(
!serde_json::from_str::<OutputJson>(&response.text().unwrap())
.unwrap()
.indexed
);

let server =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"]);

Expand All @@ -323,6 +344,7 @@ fn get_output() {
InscriptionId { txid, index: 1 },
InscriptionId { txid, index: 2 },
],
indexed: true,
runes: BTreeMap::new(),
}
);
Expand Down
1 change: 1 addition & 0 deletions tests/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ fn splitting_merged_inscriptions_is_possible() {
index: 2
},
],
indexed: true,
runes: BTreeMap::new(),
}
);
Expand Down

0 comments on commit 331dbb1

Please sign in to comment.