From 3af268ad195ffde0e82fe48e943f6dad12e06d79 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Sun, 19 Mar 2023 21:30:56 -0700 Subject: [PATCH 01/42] copy Felipe PR 9 to new branch off master Co-authored-by: felipelincoln --- .gitignore | 1 + src/index/updater.rs | 18 ++++++++++++++++-- src/index/updater/inscription_updater.rs | 5 +++++ src/lib.rs | 17 ++++++++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 26154489a5..ee4580c20f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ /target /test-times.txt /tmp +inscription_satpoint.txt diff --git a/src/index/updater.rs b/src/index/updater.rs index 5d56e4e9ab..2965399501 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,6 +102,8 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); + let mut prev_block: Option = None; + loop { let block = match rx.recv() { Ok(block) => block, @@ -113,10 +115,22 @@ impl Updater { &mut outpoint_sender, &mut value_receiver, &mut wtx, - block, + &block, &mut value_cache, )?; + if let Some(prev) = prev_block { + log::info!( + target: "new_inscription_satpoint", + "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", + &block.header.block_hash(), + &prev.header.block_hash(), + &block.txdata.len(), + ); + } + + prev_block = Some(block); + if let Some(progress_bar) = &mut progress_bar { progress_bar.inc(1); @@ -333,7 +347,7 @@ impl Updater { outpoint_sender: &mut Sender, value_receiver: &mut Receiver, wtx: &mut WriteTransaction, - block: BlockData, + block: &BlockData, value_cache: &mut HashMap, ) -> Result<()> { // If value_receiver still has values something went wrong with the last block diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 575ccf7ca7..4b5ae5d028 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -239,6 +239,11 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { self.satpoint_to_id.insert(&new_satpoint, &inscription_id)?; self.id_to_satpoint.insert(&inscription_id, &new_satpoint)?; + let inscription_id = InscriptionId::load(inscription_id); + let satpoint = SatPoint::load(new_satpoint); + + log::info!(target: "new_inscription_satpoint", "{}, {}, {}", self.height, satpoint, inscription_id); + Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index ca7229fe56..1e762acd0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,8 +54,8 @@ use { env, ffi::OsString, fmt::{self, Display, Formatter}, - fs::{self, File}, - io, + fs::{self, File, OpenOptions}, + io::{self, Write}, net::{TcpListener, ToSocketAddrs}, ops::{Add, AddAssign, Sub}, path::{Path, PathBuf}, @@ -144,7 +144,18 @@ fn timestamp(seconds: u32) -> DateTime { const INTERRUPT_LIMIT: u64 = 5; pub fn main() { - env_logger::init(); + let inscription_satpoint_logs_file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("inscription_satpoint.txt") + .unwrap(); + + env_logger::builder() + .filter(Some("new_inscription_satpoint"), log::LevelFilter::Info) + .target(env_logger::Target::Pipe(Box::new(inscription_satpoint_logs_file))) + .format(|buf, record| writeln!(buf, "{}", record.args())) + .init(); ctrlc::set_handler(move || { LISTENERS From dbeb372a563b882fea7700d37ffd97b7e77927a3 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Wed, 22 Mar 2023 00:04:12 -0700 Subject: [PATCH 02/42] fix block hash logging --- src/index/updater.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 2965399501..a9208a699d 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,7 +102,12 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block: Option = None; + + let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { + Some(index.client.get_block_hash(prev_height).unwrap()) + } else { + None + }; loop { let block = match rx.recv() { @@ -119,17 +124,17 @@ impl Updater { &mut value_cache, )?; - if let Some(prev) = prev_block { + if let Some(prev) = prev_block_hash { log::info!( target: "new_inscription_satpoint", "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", &block.header.block_hash(), - &prev.header.block_hash(), + &prev, &block.txdata.len(), ); } - prev_block = Some(block); + prev_block_hash = Some(block.header.block_hash()); if let Some(progress_bar) = &mut progress_bar { progress_bar.inc(1); From 0d54a536c4046952465966c6126b98dcd9f310bc Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 24 Mar 2023 14:47:03 -0700 Subject: [PATCH 03/42] Miscellaneous design improvements (#1968) * blue text only on hover in navbar * white text in navbar * add missing semicolon in css * change navbar background to black * switch search button from text to symbol * improve navbar submit element style --- src/index.rs | 1 - src/subcommand/server.rs | 4 +-- src/subcommand/wallet/transaction_builder.rs | 1 - src/templates.rs | 2 +- static/index.css | 28 +++++++++++++++----- templates/page.html | 2 +- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/index.rs b/src/index.rs index e4c02d853c..eb79d8f2a4 100644 --- a/src/index.rs +++ b/src/index.rs @@ -568,7 +568,6 @@ impl Index { .open_table(SATPOINT_TO_INSCRIPTION_ID)?, outpoint, )? - .into_iter() .map(|(_satpoint, inscription_id)| inscription_id) .collect(), ) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 63adafb300..9f79a777a3 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -768,7 +768,7 @@ impl Server { .get_inscription_by_id(inscription_id)? .ok_or_not_found(|| format!("inscription {inscription_id}"))?; - return match inscription.media() { + match inscription.media() { Media::Audio => Ok(PreviewAudioHtml { inscription_id }.into_response()), Media::Iframe => Ok( Self::content_response(inscription) @@ -809,7 +809,7 @@ impl Server { } Media::Unknown => Ok(PreviewUnknownHtml.into_response()), Media::Video => Ok(PreviewVideoHtml { inscription_id }.into_response()), - }; + } } async fn inscription( diff --git a/src/subcommand/wallet/transaction_builder.rs b/src/subcommand/wallet/transaction_builder.rs index aed340be39..11f73d4f65 100644 --- a/src/subcommand/wallet/transaction_builder.rs +++ b/src/subcommand/wallet/transaction_builder.rs @@ -427,7 +427,6 @@ impl TransactionBuilder { version: 1, lock_time: PackedLockTime::ZERO, input: (0..inputs) - .into_iter() .map(|_| TxIn { previous_output: OutPoint::null(), script_sig: Script::new(), diff --git a/src/templates.rs b/src/templates.rs index f4344309e6..7a8858a869 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -137,7 +137,7 @@ mod tests { rare.txt
- +
diff --git a/static/index.css b/static/index.css index a8faeb6616..36a1551793 100644 --- a/static/index.css +++ b/static/index.css @@ -4,8 +4,8 @@ --dark-fg: #98a3ad; --epic: darkorchid; --legendary: gold; - --light-bg: #292c2f; - --light-fg: #a1adb8; + --light-bg: #000000; + --light-fg: #ffffff; --link: #4169e1; --mythic: #f2a900; --rare: cornflowerblue; @@ -48,10 +48,6 @@ a:hover { text-decoration: underline; } -a:visited { - color: var(--link); -} - dt { font-weight: bold; margin-top: 0.5rem; @@ -70,6 +66,15 @@ nav > :first-child { font-weight: bold; } +nav a:hover { + color: var(--link); + text-decoration: none; +} + +nav a { + color: var(--light-fg); +} + form { display: flex; flex-grow: 1; @@ -87,6 +92,17 @@ input[type=text] { min-width: 0; } +input[type=submit] { + background: none; + border: none; + color: var(--light-fg); + cursor: pointer; + font-weight: bold; + font: inherit; + outline: inherit; + padding: 0; +} + dl { overflow-wrap: break-word; } diff --git a/templates/page.html b/templates/page.html index 8e5a25ed1e..6d1faccff3 100644 --- a/templates/page.html +++ b/templates/page.html @@ -25,7 +25,7 @@ %% }
- +
From a7d403fdaa2398cef64963ff2c598463ed1376ed Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 24 Mar 2023 15:15:56 -0700 Subject: [PATCH 04/42] insignificant change --- src/index/updater.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index a9208a699d..7fc080ee6e 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,7 +102,6 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { Some(index.client.get_block_hash(prev_height).unwrap()) } else { From 936ae983d2d7756ab27ae6c3233bb35e6cf2daa1 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 15:23:59 -0300 Subject: [PATCH 05/42] add `witness` cli command --- src/inscription.rs | 4 ++++ src/subcommand.rs | 4 ++++ src/subcommand/witness.rs | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/subcommand/witness.rs diff --git a/src/inscription.rs b/src/inscription.rs index d0fba77016..56e021cb4d 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -32,6 +32,10 @@ impl Inscription { InscriptionParser::parse(&tx.input.get(0)?.witness).ok() } + pub(crate) fn from_witness(witness: &Witness) -> Option { + InscriptionParser::parse(witness).ok() + } + pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { let path = path.as_ref(); diff --git a/src/subcommand.rs b/src/subcommand.rs index cc1848b252..985c9d2a60 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -12,6 +12,7 @@ pub mod subsidy; pub mod supply; pub mod traits; pub mod wallet; +pub mod witness; fn print_json(output: impl Serialize) -> Result { serde_json::to_writer_pretty(io::stdout(), &output)?; @@ -45,6 +46,8 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), + #[clap(about = "Extract inscription data from witness")] + Witness(witness::Witness), } impl Subcommand { @@ -67,6 +70,7 @@ impl Subcommand { Self::Supply => supply::run(), Self::Traits(traits) => traits.run(), Self::Wallet(wallet) => wallet.run(options), + Self::Witness(witness) => witness.run(options), } } } diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs new file mode 100644 index 0000000000..7df0f18430 --- /dev/null +++ b/src/subcommand/witness.rs @@ -0,0 +1,44 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Witness { + #[clap(help = "The inscription script is the second data on witness array")] + pub(crate) inscription_script: String, + #[clap(long, help = "Display only the inscription's body")] + pub(crate) only_body: bool, + #[clap(long, help = "Display only the inscription's content type")] + pub(crate) only_content_type: bool, + #[clap(long, help = "[To be implemented]")] + pub(crate) only_parent: bool, +} + +impl Witness { + pub(crate) fn run(self, _options: Options) -> Result { + let data = self.inscription_script.as_str(); + if data.len() % 2 == 1 { return Err(anyhow!("Odd length hex string")) }; + + let result; + + let witness = bitcoin::Witness::from_vec(vec![vec![], hex::decode(data).unwrap(), vec![]]); + if let Some(inscription) = Inscription::from_witness(&witness){ + let body = inscription.body().unwrap_or(&[]); + let content_type = inscription.content_type().unwrap_or(""); + + if self.only_body { + result = format!("{:?}", body); + } else if self.only_content_type { + result = format!("{}", content_type); + } else if self.only_parent { + return Err(anyhow!("--only-parent not implemented yet")); + } else { + result = format!("{{\"body\": {:?}, \"content_type\": \"{}\"}}", body, content_type); + } + } else { + let data_shortened = data[..(data.len().min(12))].to_string() + if data.len() > 12 { "..." } else {""}; + return Err(anyhow!("Failed to parse witness data {}", data_shortened)); + } + + println!("{}", result); + Ok(()) + } +} \ No newline at end of file From 8e2d5c76a4567afac3c721e318a818476536e773 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:32:04 -0300 Subject: [PATCH 06/42] change logic to use inscription id --- src/inscription.rs | 4 --- src/subcommand.rs | 2 +- src/subcommand/witness.rs | 47 +++++++++------------------------ src/subcommand/witness/ctype.rs | 25 ++++++++++++++++++ src/subcommand/witness/file.rs | 31 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 src/subcommand/witness/ctype.rs create mode 100644 src/subcommand/witness/file.rs diff --git a/src/inscription.rs b/src/inscription.rs index 56e021cb4d..d0fba77016 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -32,10 +32,6 @@ impl Inscription { InscriptionParser::parse(&tx.input.get(0)?.witness).ok() } - pub(crate) fn from_witness(witness: &Witness) -> Option { - InscriptionParser::parse(witness).ok() - } - pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { let path = path.as_ref(); diff --git a/src/subcommand.rs b/src/subcommand.rs index 985c9d2a60..26b9d93b38 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -46,7 +46,7 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), - #[clap(about = "Extract inscription data from witness")] + #[clap(subcommand, about = "Extract inscription data from witness")] Witness(witness::Witness), } diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 7df0f18430..07dcef0363 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -1,44 +1,21 @@ use super::*; +pub(crate) mod ctype; +pub(crate) mod file; + #[derive(Debug, Parser)] -pub(crate) struct Witness { - #[clap(help = "The inscription script is the second data on witness array")] - pub(crate) inscription_script: String, - #[clap(long, help = "Display only the inscription's body")] - pub(crate) only_body: bool, - #[clap(long, help = "Display only the inscription's content type")] - pub(crate) only_content_type: bool, - #[clap(long, help = "[To be implemented]")] - pub(crate) only_parent: bool, +pub(crate) enum Witness { + #[clap(about = "Display the content type from an inscription")] + Ctype(ctype::Ctype), + #[clap(about = "Write the inscription content to a file")] + File(file::File) } impl Witness { - pub(crate) fn run(self, _options: Options) -> Result { - let data = self.inscription_script.as_str(); - if data.len() % 2 == 1 { return Err(anyhow!("Odd length hex string")) }; - - let result; - - let witness = bitcoin::Witness::from_vec(vec![vec![], hex::decode(data).unwrap(), vec![]]); - if let Some(inscription) = Inscription::from_witness(&witness){ - let body = inscription.body().unwrap_or(&[]); - let content_type = inscription.content_type().unwrap_or(""); - - if self.only_body { - result = format!("{:?}", body); - } else if self.only_content_type { - result = format!("{}", content_type); - } else if self.only_parent { - return Err(anyhow!("--only-parent not implemented yet")); - } else { - result = format!("{{\"body\": {:?}, \"content_type\": \"{}\"}}", body, content_type); - } - } else { - let data_shortened = data[..(data.len().min(12))].to_string() + if data.len() > 12 { "..." } else {""}; - return Err(anyhow!("Failed to parse witness data {}", data_shortened)); + pub(crate) fn run(self, options: Options) -> Result { + match self { + Self::Ctype(ctype) => ctype.run(options), + Self::File(file) => file.run(options), } - - println!("{}", result); - Ok(()) } } \ No newline at end of file diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs new file mode 100644 index 0000000000..144c92a82d --- /dev/null +++ b/src/subcommand/witness/ctype.rs @@ -0,0 +1,25 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Ctype { + #[clap(help = "The inscription to extract the content type.")] + inscription_id: String, +} + +impl Ctype { + pub(crate) fn run(self, options: Options) -> Result { + let inscription_id = InscriptionId::from_str(self.inscription_id.as_str())?; + + let index = Index::open(&options)?; + index.update()?; + + if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + println!("{}", inscription.content_type().unwrap()); + + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); + } + + Ok(()) + } +} \ No newline at end of file diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs new file mode 100644 index 0000000000..bd96d1438c --- /dev/null +++ b/src/subcommand/witness/file.rs @@ -0,0 +1,31 @@ +use std::io::Write; + +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct File { + #[clap(help = "The inscription to extract the content.")] + inscription_id: String, + #[clap(help = "The name of the file to be created with the inscription content.")] + filename: String, +} + +impl File { + pub(crate) fn run(self, options: Options) -> Result { + let inscription_id = InscriptionId::from_str(self.inscription_id.as_str())?; + + let index = Index::open(&options)?; + index.update()?; + + if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + let content_bytes = inscription.body().unwrap(); + let mut file = fs::File::create(self.filename)?; + file.write_all(content_bytes)?; + + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); + } + + Ok(()) + } +} \ No newline at end of file From 3a367efcc3c5c7f80a08ab48c49194130ab2ffdd Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:40:56 -0300 Subject: [PATCH 07/42] update witness cli about --- src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 26b9d93b38..655a6b25fa 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -46,7 +46,7 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), - #[clap(subcommand, about = "Extract inscription data from witness")] + #[clap(subcommand, about = "Extract witness content from inscription")] Witness(witness::Witness), } From 162b18f314e2d0f4ca2e10c3351cd40ab83d862d Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:42:47 -0300 Subject: [PATCH 08/42] format --- src/subcommand/witness.rs | 2 +- src/subcommand/witness/ctype.rs | 6 +++--- src/subcommand/witness/file.rs | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 07dcef0363..8112a8967e 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -18,4 +18,4 @@ impl Witness { Self::File(file) => file.run(options), } } -} \ No newline at end of file +} diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 144c92a82d..3e9a2666b5 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -2,8 +2,8 @@ use super::*; #[derive(Debug, Parser)] pub(crate) struct Ctype { - #[clap(help = "The inscription to extract the content type.")] - inscription_id: String, + #[clap(help = "The inscription to extract the content type.")] + inscription_id: String, } impl Ctype { @@ -22,4 +22,4 @@ impl Ctype { Ok(()) } -} \ No newline at end of file +} diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index bd96d1438c..ce23a47a6a 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -4,10 +4,10 @@ use super::*; #[derive(Debug, Parser)] pub(crate) struct File { - #[clap(help = "The inscription to extract the content.")] - inscription_id: String, - #[clap(help = "The name of the file to be created with the inscription content.")] - filename: String, + #[clap(help = "The inscription to extract the content.")] + inscription_id: String, + #[clap(help = "The name of the file to be created with the inscription content.")] + filename: String, } impl File { @@ -28,4 +28,4 @@ impl File { Ok(()) } -} \ No newline at end of file +} From 66db0bf24669e24888cf328c0af212846b6dbb87 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 24 Mar 2023 21:38:01 -0700 Subject: [PATCH 09/42] run cargo fmt --- src/index/updater.rs | 11 ++++++----- src/lib.rs | 4 +++- src/subcommand/witness.rs | 2 +- src/subcommand/witness/ctype.rs | 9 ++++----- src/subcommand/witness/file.rs | 3 +-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 7fc080ee6e..5653a29563 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,11 +102,12 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { - Some(index.client.get_block_hash(prev_height).unwrap()) - } else { - None - }; + let mut prev_block_hash: Option = + if let Some(prev_height) = self.height.checked_sub(1) { + Some(index.client.get_block_hash(prev_height).unwrap()) + } else { + None + }; loop { let block = match rx.recv() { diff --git a/src/lib.rs b/src/lib.rs index 1e762acd0f..37ce5f10c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,7 +153,9 @@ pub fn main() { env_logger::builder() .filter(Some("new_inscription_satpoint"), log::LevelFilter::Info) - .target(env_logger::Target::Pipe(Box::new(inscription_satpoint_logs_file))) + .target(env_logger::Target::Pipe(Box::new( + inscription_satpoint_logs_file, + ))) .format(|buf, record| writeln!(buf, "{}", record.args())) .init(); diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 8112a8967e..fec8cd0cef 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -8,7 +8,7 @@ pub(crate) enum Witness { #[clap(about = "Display the content type from an inscription")] Ctype(ctype::Ctype), #[clap(about = "Write the inscription content to a file")] - File(file::File) + File(file::File), } impl Witness { diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 3e9a2666b5..2e10cab431 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -13,11 +13,10 @@ impl Ctype { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ - println!("{}", inscription.content_type().unwrap()); - - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); + if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { + println!("{}", inscription.content_type().unwrap()); + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); } Ok(()) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index ce23a47a6a..4bc3874a96 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -17,11 +17,10 @@ impl File { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { let content_bytes = inscription.body().unwrap(); let mut file = fs::File::create(self.filename)?; file.write_all(content_bytes)?; - } else { return Err(anyhow!("Inscription {} not found", inscription_id)); } From 6d1d5d80f610ab0d50cb767e36c2162bfdb1c5ae Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Sat, 25 Mar 2023 12:41:43 -0300 Subject: [PATCH 10/42] improve code readability Co-authored-by: Clarke Plumo --- src/subcommand/witness/ctype.rs | 10 +++++----- src/subcommand/witness/file.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 2e10cab431..d99b3916d6 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -13,11 +13,11 @@ impl Ctype { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { - println!("{}", inscription.content_type().unwrap()); - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); - } + let inscription = index + .get_inscription_by_id(inscription_id)? + .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; + + println!("{}", inscription.content_type().unwrap()); Ok(()) } diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index 4bc3874a96..dbd03ccda1 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -17,13 +17,13 @@ impl File { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { - let content_bytes = inscription.body().unwrap(); - let mut file = fs::File::create(self.filename)?; - file.write_all(content_bytes)?; - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); - } + let inscription = index + .get_inscription_by_id(inscription_id)? + .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; + + let content_bytes = inscription.body().unwrap(); + let mut file = fs::File::create(self.filename)?; + file.write_all(content_bytes)?; Ok(()) } From e00c5c0a28241d567f6d1911d55ebf5f6995a952 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Mon, 27 Mar 2023 13:47:08 -0300 Subject: [PATCH 11/42] print file size --- src/subcommand/witness/file.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index dbd03ccda1..260e35812a 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -25,6 +25,8 @@ impl File { let mut file = fs::File::create(self.filename)?; file.write_all(content_bytes)?; + println!("{}", file.metadata()?.len()); + Ok(()) } } From 936ca6fcb56dfcaebfc01a396e45a1bd09b27bf1 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Mon, 27 Mar 2023 14:07:51 -0300 Subject: [PATCH 12/42] skip write if file is empty --- src/subcommand/witness/file.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index 260e35812a..006957b6b4 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -21,9 +21,10 @@ impl File { .get_inscription_by_id(inscription_id)? .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; - let content_bytes = inscription.body().unwrap(); let mut file = fs::File::create(self.filename)?; - file.write_all(content_bytes)?; + if let Some(content_bytes) = inscription.body() { + file.write_all(content_bytes)?; + } println!("{}", file.metadata()?.len()); From a3145343d660c1f42e89584aff362c915604b039 Mon Sep 17 00:00:00 2001 From: gmart7t2 <49558347+gmart7t2@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:19:25 -0300 Subject: [PATCH 13/42] Label change and receive addresses correctly (#1847) Co-authored-by: Greg Martin Co-authored-by: raphjaph --- src/subcommand/wallet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index 0404f2ad24..bd15307786 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -136,7 +136,7 @@ fn derive_and_import_descriptor( active: Some(true), range: None, next_index: None, - internal: Some(!change), + internal: Some(change), label: None, })?; From 92157e53bdf6768610b2f08005877f2dde306726 Mon Sep 17 00:00:00 2001 From: gmart7t2 <49558347+gmart7t2@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:29:58 -0300 Subject: [PATCH 14/42] Mandatory fee rate for inscribe (#1897) Co-authored-by: Greg Martin Co-authored-by: raphjaph --- docs/src/guides/inscriptions.md | 2 +- src/subcommand/wallet/inscribe.rs | 6 +-- tests/lib.rs | 2 +- tests/wallet/inscribe.rs | 72 +++++++++++++++++-------------- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/docs/src/guides/inscriptions.md b/docs/src/guides/inscriptions.md index c8824b5077..e9fe3e2993 100644 --- a/docs/src/guides/inscriptions.md +++ b/docs/src/guides/inscriptions.md @@ -166,7 +166,7 @@ Creating Inscriptions To create an inscription with the contents of `FILE`, run: ``` -ord wallet inscribe FILE +ord wallet inscribe --fee-rate FEE_RATE FILE ``` Ord will output two transactions IDs, one for the commit transaction, and one diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index d9b537f82d..1df5c15061 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -30,11 +30,7 @@ struct Output { pub(crate) struct Inscribe { #[clap(long, help = "Inscribe ")] pub(crate) satpoint: Option, - #[clap( - long, - default_value = "1.0", - help = "Use fee rate of sats/vB" - )] + #[clap(long, help = "Use fee rate of sats/vB")] pub(crate) fee_rate: FeeRate, #[clap( long, diff --git a/tests/lib.rs b/tests/lib.rs index 710e6b2a88..0639c15570 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -48,7 +48,7 @@ struct Inscribe { fn inscribe(rpc_server: &test_bitcoincore_rpc::Handle) -> Inscribe { rpc_server.mine_blocks(1); - let output = CommandBuilder::new("wallet inscribe foo.txt") + let output = CommandBuilder::new("wallet inscribe --fee-rate 1 foo.txt") .write("foo.txt", "FOO") .rpc_server(rpc_server) .output(); diff --git a/tests/wallet/inscribe.rs b/tests/wallet/inscribe.rs index 16b486037e..50ecca7dda 100644 --- a/tests/wallet/inscribe.rs +++ b/tests/wallet/inscribe.rs @@ -42,7 +42,7 @@ fn inscribe_works_with_huge_expensive_inscriptions() { fn inscribe_fails_if_bitcoin_core_is_too_old() { let rpc_server = test_bitcoincore_rpc::builder().version(230000).build(); - CommandBuilder::new("wallet inscribe hello.txt") + CommandBuilder::new("wallet inscribe hello.txt --fee-rate 1") .write("hello.txt", "HELLOWORLD") .expected_exit_code(1) .expected_stderr("error: Bitcoin Core 24.0.0 or newer required, current version is 23.0.0\n") @@ -58,7 +58,7 @@ fn inscribe_no_backup() { create_wallet(&rpc_server); assert_eq!(rpc_server.descriptors().len(), 2); - CommandBuilder::new("wallet inscribe hello.txt --no-backup") + CommandBuilder::new("wallet inscribe hello.txt --no-backup --fee-rate 1") .write("hello.txt", "HELLOWORLD") .rpc_server(&rpc_server) .output::(); @@ -72,7 +72,7 @@ fn inscribe_unknown_file_extension() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("wallet inscribe pepe.xyz") + CommandBuilder::new("wallet inscribe pepe.xyz --fee-rate 1") .write("pepe.xyz", [1; 520]) .rpc_server(&rpc_server) .expected_exit_code(1) @@ -88,7 +88,7 @@ fn inscribe_exceeds_chain_limit() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("--chain signet wallet inscribe degenerate.png") + CommandBuilder::new("--chain signet wallet inscribe degenerate.png --fee-rate 1") .write("degenerate.png", [1; 1025]) .rpc_server(&rpc_server) .expected_exit_code(1) @@ -106,7 +106,7 @@ fn regtest_has_no_content_size_limit() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("--chain regtest wallet inscribe degenerate.png") + CommandBuilder::new("--chain regtest wallet inscribe degenerate.png --fee-rate 1") .write("degenerate.png", [1; 1025]) .rpc_server(&rpc_server) .stdout_regex(".*") @@ -121,7 +121,7 @@ fn mainnet_has_no_content_size_limit() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("wallet inscribe degenerate.png") + CommandBuilder::new("wallet inscribe degenerate.png --fee-rate 1") .write("degenerate.png", [1; 1025]) .rpc_server(&rpc_server) .stdout_regex(".*") @@ -136,7 +136,7 @@ fn inscribe_does_not_use_inscribed_sats_as_cardinal_utxos() { rpc_server.mine_blocks_with_subsidy(1, 100); CommandBuilder::new( - "wallet inscribe degenerate.png" + "wallet inscribe degenerate.png --fee-rate 1" ) .rpc_server(&rpc_server) .write("degenerate.png", [1; 100]) @@ -156,12 +156,14 @@ fn refuse_to_reinscribe_sats() { rpc_server.mine_blocks_with_subsidy(1, 100); - CommandBuilder::new(format!("wallet inscribe --satpoint {reveal}:0:0 hello.txt")) - .write("hello.txt", "HELLOWORLD") - .rpc_server(&rpc_server) - .expected_exit_code(1) - .expected_stderr(format!("error: sat at {reveal}:0:0 already inscribed\n")) - .run(); + CommandBuilder::new(format!( + "wallet inscribe --satpoint {reveal}:0:0 hello.txt --fee-rate 1" + )) + .write("hello.txt", "HELLOWORLD") + .rpc_server(&rpc_server) + .expected_exit_code(1) + .expected_stderr(format!("error: sat at {reveal}:0:0 already inscribed\n")) + .run(); } #[test] @@ -181,7 +183,7 @@ fn refuse_to_inscribe_already_inscribed_utxo() { }; CommandBuilder::new(format!( - "wallet inscribe --satpoint {output}:55555 hello.txt" + "wallet inscribe --satpoint {output}:55555 hello.txt --fee-rate 1" )) .write("hello.txt", "HELLOWORLD") .rpc_server(&rpc_server) @@ -198,11 +200,12 @@ fn inscribe_with_optional_satpoint_arg() { create_wallet(&rpc_server); let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid(); - let Inscribe { inscription, .. } = - CommandBuilder::new(format!("wallet inscribe foo.txt --satpoint {txid}:0:0")) - .write("foo.txt", "FOO") - .rpc_server(&rpc_server) - .output(); + let Inscribe { inscription, .. } = CommandBuilder::new(format!( + "wallet inscribe foo.txt --satpoint {txid}:0:0 --fee-rate 1" + )) + .write("foo.txt", "FOO") + .rpc_server(&rpc_server) + .output(); rpc_server.mine_blocks(1); @@ -262,10 +265,12 @@ fn inscribe_with_commit_fee_rate() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("--index-sats wallet inscribe degenerate.png --commit-fee-rate 2.0") - .write("degenerate.png", [1; 520]) - .rpc_server(&rpc_server) - .output::(); + CommandBuilder::new( + "--index-sats wallet inscribe degenerate.png --commit-fee-rate 2.0 --fee-rate 1", + ) + .write("degenerate.png", [1; 520]) + .rpc_server(&rpc_server) + .output::(); let tx1 = &rpc_server.mempool()[0]; let mut fee = 0; @@ -307,7 +312,7 @@ fn inscribe_with_wallet_named_foo() { rpc_server.mine_blocks(1); - CommandBuilder::new("--wallet foo wallet inscribe degenerate.png") + CommandBuilder::new("--wallet foo wallet inscribe degenerate.png --fee-rate 1") .write("degenerate.png", [1; 520]) .rpc_server(&rpc_server) .output::(); @@ -319,14 +324,14 @@ fn inscribe_with_dry_run_flag() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - CommandBuilder::new("wallet inscribe --dry-run degenerate.png") + CommandBuilder::new("wallet inscribe --dry-run degenerate.png --fee-rate 1") .write("degenerate.png", [1; 520]) .rpc_server(&rpc_server) .output::(); assert!(rpc_server.mempool().is_empty()); - CommandBuilder::new("wallet inscribe degenerate.png") + CommandBuilder::new("wallet inscribe degenerate.png --fee-rate 1") .write("degenerate.png", [1; 520]) .rpc_server(&rpc_server) .output::(); @@ -340,11 +345,12 @@ fn inscribe_with_dry_run_flag_fees_inscrease() { create_wallet(&rpc_server); rpc_server.mine_blocks(1); - let total_fee_dry_run = CommandBuilder::new("wallet inscribe --dry-run degenerate.png") - .write("degenerate.png", [1; 520]) - .rpc_server(&rpc_server) - .output::() - .fees; + let total_fee_dry_run = + CommandBuilder::new("wallet inscribe --dry-run degenerate.png --fee-rate 1") + .write("degenerate.png", [1; 520]) + .rpc_server(&rpc_server) + .output::() + .fees; let total_fee_normal = CommandBuilder::new("wallet inscribe --dry-run degenerate.png --fee-rate 1.1") @@ -368,7 +374,7 @@ fn inscribe_to_specific_destination() { .address; let txid = CommandBuilder::new(format!( - "wallet inscribe --destination {destination} degenerate.png" + "wallet inscribe --destination {destination} degenerate.png --fee-rate 1" )) .write("degenerate.png", [1; 520]) .rpc_server(&rpc_server) @@ -390,7 +396,7 @@ fn inscribe_with_no_limit() { rpc_server.mine_blocks(1); let four_megger = std::iter::repeat(0).take(4_000_000).collect::>(); - CommandBuilder::new("wallet inscribe --no-limit degenerate.png") + CommandBuilder::new("wallet inscribe --no-limit degenerate.png --fee-rate 1") .write("degenerate.png", four_megger) .rpc_server(&rpc_server); } From ab2f178eb1941f29e99f9c0ba8e5340b73bb8363 Mon Sep 17 00:00:00 2001 From: gmart7t2 <49558347+gmart7t2@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:08:26 -0300 Subject: [PATCH 15/42] Add `ord wallet cardinals` command to list the cardinal outputs (#1904) Co-authored-by: Greg Martin Co-authored-by: raphjaph --- src/subcommand/wallet.rs | 6 ++++- src/subcommand/wallet/cardinals.rs | 37 ++++++++++++++++++++++++++++++ tests/wallet.rs | 1 + tests/wallet/cardinals.rs | 23 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/subcommand/wallet/cardinals.rs create mode 100644 tests/wallet/cardinals.rs diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index bd15307786..914805d5af 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -15,6 +15,7 @@ use { }; pub mod balance; +pub mod cardinals; pub mod create; pub(crate) mod inscribe; pub mod inscriptions; @@ -46,8 +47,10 @@ pub(crate) enum Wallet { Send(send::Send), #[clap(about = "See wallet transactions")] Transactions(transactions::Transactions), - #[clap(about = "List wallet outputs")] + #[clap(about = "List all unspent outputs in wallet")] Outputs, + #[clap(about = "List unspent cardinal outputs in wallet")] + Cardinals, } impl Wallet { @@ -63,6 +66,7 @@ impl Wallet { Self::Send(send) => send.run(options), Self::Transactions(transactions) => transactions.run(options), Self::Outputs => outputs::run(options), + Self::Cardinals => cardinals::run(options), } } } diff --git a/src/subcommand/wallet/cardinals.rs b/src/subcommand/wallet/cardinals.rs new file mode 100644 index 0000000000..32076229a1 --- /dev/null +++ b/src/subcommand/wallet/cardinals.rs @@ -0,0 +1,37 @@ +use {super::*, crate::wallet::Wallet, std::collections::BTreeSet}; + +#[derive(Serialize, Deserialize)] +pub struct Cardinal { + pub output: OutPoint, + pub amount: u64, +} + +pub(crate) fn run(options: Options) -> Result { + let index = Index::open(&options)?; + index.update()?; + + let inscribed_utxos = index + .get_inscriptions(None)? + .keys() + .map(|satpoint| satpoint.outpoint) + .collect::>(); + + let cardinal_utxos = index + .get_unspent_outputs(Wallet::load(&options)?)? + .iter() + .filter_map(|(output, amount)| { + if inscribed_utxos.contains(output) { + None + } else { + Some(Cardinal { + output: *output, + amount: amount.to_sat(), + }) + } + }) + .collect::>(); + + print_json(cardinal_utxos)?; + + Ok(()) +} diff --git a/tests/wallet.rs b/tests/wallet.rs index 31dc1d96a0..3e04064183 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -1,6 +1,7 @@ use super::*; mod balance; +mod cardinals; mod create; mod inscribe; mod inscriptions; diff --git a/tests/wallet/cardinals.rs b/tests/wallet/cardinals.rs new file mode 100644 index 0000000000..19053f4389 --- /dev/null +++ b/tests/wallet/cardinals.rs @@ -0,0 +1,23 @@ +use { + super::*, + ord::subcommand::wallet::{cardinals::Cardinal, outputs::Output}, +}; + +#[test] +fn cardinals() { + let rpc_server = test_bitcoincore_rpc::spawn(); + create_wallet(&rpc_server); + + // this creates 2 more cardinal outputs and one inscribed output + inscribe(&rpc_server); + + let all_outputs = CommandBuilder::new("wallet outputs") + .rpc_server(&rpc_server) + .output::>(); + + let cardinal_outputs = CommandBuilder::new("wallet cardinals") + .rpc_server(&rpc_server) + .output::>(); + + assert_eq!(all_outputs.len() - cardinal_outputs.len(), 1); +} From ccd10bda6d47b6520a91184b22f6276f65ac1fd2 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Tue, 4 Apr 2023 23:29:35 -0700 Subject: [PATCH 16/42] fix inscription_satpoint.txt format, use localhost by default --- src/index/updater.rs | 2 +- src/index/updater/inscription_updater.rs | 2 +- src/subcommand/server.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 5653a29563..372ec5d178 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -127,7 +127,7 @@ impl Updater { if let Some(prev) = prev_block_hash { log::info!( target: "new_inscription_satpoint", - "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", + "{{\"block_hash\":\"{}\",\"prev_block_hash\":\"{}\",\"tx_count\":{}}}", &block.header.block_hash(), &prev, &block.txdata.len(), diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 4b5ae5d028..a7b27dbcbd 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -242,7 +242,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { let inscription_id = InscriptionId::load(inscription_id); let satpoint = SatPoint::load(new_satpoint); - log::info!(target: "new_inscription_satpoint", "{}, {}, {}", self.height, satpoint, inscription_id); + log::info!(target: "new_inscription_satpoint", "{},{},{}", self.height, satpoint, inscription_id); Ok(()) } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 63adafb300..0ef4b8236d 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -91,7 +91,7 @@ impl Display for StaticHtml { pub(crate) struct Server { #[clap( long, - default_value = "0.0.0.0", + default_value = "127.0.0.1", help = "Listen on
for incoming requests." )] address: String, From da64c039a3e6334cc46d0925e757d8e35137eb06 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Wed, 5 Apr 2023 11:31:20 -0700 Subject: [PATCH 17/42] fix indexer to look for inscriptions in all tx inputs --- src/inscription.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/inscription.rs b/src/inscription.rs index d0fba77016..525a23c1eb 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -29,7 +29,17 @@ impl Inscription { } pub(crate) fn from_transaction(tx: &Transaction) -> Option { - InscriptionParser::parse(&tx.input.get(0)?.witness).ok() + for input in &tx.input { + if let Some(inscription) = Inscription::from_tx_input(input) { + return Some(inscription); + } + } + + None + } + + pub(crate) fn from_tx_input(tx_in: &TxIn) -> Option { + InscriptionParser::parse(&tx_in.witness).ok() } pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { From 6f03ec1ac76b39c5664a892d3af944e20b07e7b3 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Sun, 19 Mar 2023 21:30:56 -0700 Subject: [PATCH 18/42] copy Felipe PR 9 to new branch off master Co-authored-by: felipelincoln --- .gitignore | 1 + src/index/updater.rs | 18 ++++++++++++++++-- src/index/updater/inscription_updater.rs | 5 +++++ src/lib.rs | 17 ++++++++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 26154489a5..ee4580c20f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ /target /test-times.txt /tmp +inscription_satpoint.txt diff --git a/src/index/updater.rs b/src/index/updater.rs index 5d56e4e9ab..2965399501 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,6 +102,8 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); + let mut prev_block: Option = None; + loop { let block = match rx.recv() { Ok(block) => block, @@ -113,10 +115,22 @@ impl Updater { &mut outpoint_sender, &mut value_receiver, &mut wtx, - block, + &block, &mut value_cache, )?; + if let Some(prev) = prev_block { + log::info!( + target: "new_inscription_satpoint", + "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", + &block.header.block_hash(), + &prev.header.block_hash(), + &block.txdata.len(), + ); + } + + prev_block = Some(block); + if let Some(progress_bar) = &mut progress_bar { progress_bar.inc(1); @@ -333,7 +347,7 @@ impl Updater { outpoint_sender: &mut Sender, value_receiver: &mut Receiver, wtx: &mut WriteTransaction, - block: BlockData, + block: &BlockData, value_cache: &mut HashMap, ) -> Result<()> { // If value_receiver still has values something went wrong with the last block diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 575ccf7ca7..4b5ae5d028 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -239,6 +239,11 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { self.satpoint_to_id.insert(&new_satpoint, &inscription_id)?; self.id_to_satpoint.insert(&inscription_id, &new_satpoint)?; + let inscription_id = InscriptionId::load(inscription_id); + let satpoint = SatPoint::load(new_satpoint); + + log::info!(target: "new_inscription_satpoint", "{}, {}, {}", self.height, satpoint, inscription_id); + Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index ca7229fe56..1e762acd0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,8 +54,8 @@ use { env, ffi::OsString, fmt::{self, Display, Formatter}, - fs::{self, File}, - io, + fs::{self, File, OpenOptions}, + io::{self, Write}, net::{TcpListener, ToSocketAddrs}, ops::{Add, AddAssign, Sub}, path::{Path, PathBuf}, @@ -144,7 +144,18 @@ fn timestamp(seconds: u32) -> DateTime { const INTERRUPT_LIMIT: u64 = 5; pub fn main() { - env_logger::init(); + let inscription_satpoint_logs_file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("inscription_satpoint.txt") + .unwrap(); + + env_logger::builder() + .filter(Some("new_inscription_satpoint"), log::LevelFilter::Info) + .target(env_logger::Target::Pipe(Box::new(inscription_satpoint_logs_file))) + .format(|buf, record| writeln!(buf, "{}", record.args())) + .init(); ctrlc::set_handler(move || { LISTENERS From 484afcda4ecbb1acb9b3ba4eb05a55d055e0fbc2 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Wed, 22 Mar 2023 00:04:12 -0700 Subject: [PATCH 19/42] fix block hash logging --- src/index/updater.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 2965399501..a9208a699d 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,7 +102,12 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block: Option = None; + + let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { + Some(index.client.get_block_hash(prev_height).unwrap()) + } else { + None + }; loop { let block = match rx.recv() { @@ -119,17 +124,17 @@ impl Updater { &mut value_cache, )?; - if let Some(prev) = prev_block { + if let Some(prev) = prev_block_hash { log::info!( target: "new_inscription_satpoint", "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", &block.header.block_hash(), - &prev.header.block_hash(), + &prev, &block.txdata.len(), ); } - prev_block = Some(block); + prev_block_hash = Some(block.header.block_hash()); if let Some(progress_bar) = &mut progress_bar { progress_bar.inc(1); From 4fbb880b59066194a6981a7ce9f81f343dbf96be Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 24 Mar 2023 15:15:56 -0700 Subject: [PATCH 20/42] insignificant change --- src/index/updater.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index a9208a699d..7fc080ee6e 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,7 +102,6 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { Some(index.client.get_block_hash(prev_height).unwrap()) } else { From aebf0518222752c73d32bad733c57fdcf94fc533 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 15:23:59 -0300 Subject: [PATCH 21/42] add `witness` cli command --- src/inscription.rs | 4 ++++ src/subcommand.rs | 4 ++++ src/subcommand/witness.rs | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/subcommand/witness.rs diff --git a/src/inscription.rs b/src/inscription.rs index d0fba77016..56e021cb4d 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -32,6 +32,10 @@ impl Inscription { InscriptionParser::parse(&tx.input.get(0)?.witness).ok() } + pub(crate) fn from_witness(witness: &Witness) -> Option { + InscriptionParser::parse(witness).ok() + } + pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { let path = path.as_ref(); diff --git a/src/subcommand.rs b/src/subcommand.rs index cc1848b252..985c9d2a60 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -12,6 +12,7 @@ pub mod subsidy; pub mod supply; pub mod traits; pub mod wallet; +pub mod witness; fn print_json(output: impl Serialize) -> Result { serde_json::to_writer_pretty(io::stdout(), &output)?; @@ -45,6 +46,8 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), + #[clap(about = "Extract inscription data from witness")] + Witness(witness::Witness), } impl Subcommand { @@ -67,6 +70,7 @@ impl Subcommand { Self::Supply => supply::run(), Self::Traits(traits) => traits.run(), Self::Wallet(wallet) => wallet.run(options), + Self::Witness(witness) => witness.run(options), } } } diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs new file mode 100644 index 0000000000..7df0f18430 --- /dev/null +++ b/src/subcommand/witness.rs @@ -0,0 +1,44 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Witness { + #[clap(help = "The inscription script is the second data on witness array")] + pub(crate) inscription_script: String, + #[clap(long, help = "Display only the inscription's body")] + pub(crate) only_body: bool, + #[clap(long, help = "Display only the inscription's content type")] + pub(crate) only_content_type: bool, + #[clap(long, help = "[To be implemented]")] + pub(crate) only_parent: bool, +} + +impl Witness { + pub(crate) fn run(self, _options: Options) -> Result { + let data = self.inscription_script.as_str(); + if data.len() % 2 == 1 { return Err(anyhow!("Odd length hex string")) }; + + let result; + + let witness = bitcoin::Witness::from_vec(vec![vec![], hex::decode(data).unwrap(), vec![]]); + if let Some(inscription) = Inscription::from_witness(&witness){ + let body = inscription.body().unwrap_or(&[]); + let content_type = inscription.content_type().unwrap_or(""); + + if self.only_body { + result = format!("{:?}", body); + } else if self.only_content_type { + result = format!("{}", content_type); + } else if self.only_parent { + return Err(anyhow!("--only-parent not implemented yet")); + } else { + result = format!("{{\"body\": {:?}, \"content_type\": \"{}\"}}", body, content_type); + } + } else { + let data_shortened = data[..(data.len().min(12))].to_string() + if data.len() > 12 { "..." } else {""}; + return Err(anyhow!("Failed to parse witness data {}", data_shortened)); + } + + println!("{}", result); + Ok(()) + } +} \ No newline at end of file From 052bb96325375e0fc878c5538610e13d8a9e1980 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:32:04 -0300 Subject: [PATCH 22/42] change logic to use inscription id --- src/inscription.rs | 4 --- src/subcommand.rs | 2 +- src/subcommand/witness.rs | 47 +++++++++------------------------ src/subcommand/witness/ctype.rs | 25 ++++++++++++++++++ src/subcommand/witness/file.rs | 31 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 src/subcommand/witness/ctype.rs create mode 100644 src/subcommand/witness/file.rs diff --git a/src/inscription.rs b/src/inscription.rs index 56e021cb4d..d0fba77016 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -32,10 +32,6 @@ impl Inscription { InscriptionParser::parse(&tx.input.get(0)?.witness).ok() } - pub(crate) fn from_witness(witness: &Witness) -> Option { - InscriptionParser::parse(witness).ok() - } - pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { let path = path.as_ref(); diff --git a/src/subcommand.rs b/src/subcommand.rs index 985c9d2a60..26b9d93b38 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -46,7 +46,7 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), - #[clap(about = "Extract inscription data from witness")] + #[clap(subcommand, about = "Extract inscription data from witness")] Witness(witness::Witness), } diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 7df0f18430..07dcef0363 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -1,44 +1,21 @@ use super::*; +pub(crate) mod ctype; +pub(crate) mod file; + #[derive(Debug, Parser)] -pub(crate) struct Witness { - #[clap(help = "The inscription script is the second data on witness array")] - pub(crate) inscription_script: String, - #[clap(long, help = "Display only the inscription's body")] - pub(crate) only_body: bool, - #[clap(long, help = "Display only the inscription's content type")] - pub(crate) only_content_type: bool, - #[clap(long, help = "[To be implemented]")] - pub(crate) only_parent: bool, +pub(crate) enum Witness { + #[clap(about = "Display the content type from an inscription")] + Ctype(ctype::Ctype), + #[clap(about = "Write the inscription content to a file")] + File(file::File) } impl Witness { - pub(crate) fn run(self, _options: Options) -> Result { - let data = self.inscription_script.as_str(); - if data.len() % 2 == 1 { return Err(anyhow!("Odd length hex string")) }; - - let result; - - let witness = bitcoin::Witness::from_vec(vec![vec![], hex::decode(data).unwrap(), vec![]]); - if let Some(inscription) = Inscription::from_witness(&witness){ - let body = inscription.body().unwrap_or(&[]); - let content_type = inscription.content_type().unwrap_or(""); - - if self.only_body { - result = format!("{:?}", body); - } else if self.only_content_type { - result = format!("{}", content_type); - } else if self.only_parent { - return Err(anyhow!("--only-parent not implemented yet")); - } else { - result = format!("{{\"body\": {:?}, \"content_type\": \"{}\"}}", body, content_type); - } - } else { - let data_shortened = data[..(data.len().min(12))].to_string() + if data.len() > 12 { "..." } else {""}; - return Err(anyhow!("Failed to parse witness data {}", data_shortened)); + pub(crate) fn run(self, options: Options) -> Result { + match self { + Self::Ctype(ctype) => ctype.run(options), + Self::File(file) => file.run(options), } - - println!("{}", result); - Ok(()) } } \ No newline at end of file diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs new file mode 100644 index 0000000000..144c92a82d --- /dev/null +++ b/src/subcommand/witness/ctype.rs @@ -0,0 +1,25 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Ctype { + #[clap(help = "The inscription to extract the content type.")] + inscription_id: String, +} + +impl Ctype { + pub(crate) fn run(self, options: Options) -> Result { + let inscription_id = InscriptionId::from_str(self.inscription_id.as_str())?; + + let index = Index::open(&options)?; + index.update()?; + + if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + println!("{}", inscription.content_type().unwrap()); + + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); + } + + Ok(()) + } +} \ No newline at end of file diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs new file mode 100644 index 0000000000..bd96d1438c --- /dev/null +++ b/src/subcommand/witness/file.rs @@ -0,0 +1,31 @@ +use std::io::Write; + +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct File { + #[clap(help = "The inscription to extract the content.")] + inscription_id: String, + #[clap(help = "The name of the file to be created with the inscription content.")] + filename: String, +} + +impl File { + pub(crate) fn run(self, options: Options) -> Result { + let inscription_id = InscriptionId::from_str(self.inscription_id.as_str())?; + + let index = Index::open(&options)?; + index.update()?; + + if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + let content_bytes = inscription.body().unwrap(); + let mut file = fs::File::create(self.filename)?; + file.write_all(content_bytes)?; + + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); + } + + Ok(()) + } +} \ No newline at end of file From 5ad357b8169a0d694292964611801c494aa413fe Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:40:56 -0300 Subject: [PATCH 23/42] update witness cli about --- src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 26b9d93b38..655a6b25fa 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -46,7 +46,7 @@ pub(crate) enum Subcommand { Traits(traits::Traits), #[clap(subcommand, about = "Wallet commands")] Wallet(wallet::Wallet), - #[clap(subcommand, about = "Extract inscription data from witness")] + #[clap(subcommand, about = "Extract witness content from inscription")] Witness(witness::Witness), } From a6df036211d79d39e60c708bc8e7c06471965abc Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 24 Mar 2023 21:42:47 -0300 Subject: [PATCH 24/42] format --- src/subcommand/witness.rs | 2 +- src/subcommand/witness/ctype.rs | 6 +++--- src/subcommand/witness/file.rs | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 07dcef0363..8112a8967e 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -18,4 +18,4 @@ impl Witness { Self::File(file) => file.run(options), } } -} \ No newline at end of file +} diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 144c92a82d..3e9a2666b5 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -2,8 +2,8 @@ use super::*; #[derive(Debug, Parser)] pub(crate) struct Ctype { - #[clap(help = "The inscription to extract the content type.")] - inscription_id: String, + #[clap(help = "The inscription to extract the content type.")] + inscription_id: String, } impl Ctype { @@ -22,4 +22,4 @@ impl Ctype { Ok(()) } -} \ No newline at end of file +} diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index bd96d1438c..ce23a47a6a 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -4,10 +4,10 @@ use super::*; #[derive(Debug, Parser)] pub(crate) struct File { - #[clap(help = "The inscription to extract the content.")] - inscription_id: String, - #[clap(help = "The name of the file to be created with the inscription content.")] - filename: String, + #[clap(help = "The inscription to extract the content.")] + inscription_id: String, + #[clap(help = "The name of the file to be created with the inscription content.")] + filename: String, } impl File { @@ -28,4 +28,4 @@ impl File { Ok(()) } -} \ No newline at end of file +} From 36e009e7c3bb78d383fc86f285e1d9f5d9ca8dfd Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 24 Mar 2023 21:38:01 -0700 Subject: [PATCH 25/42] run cargo fmt --- src/index/updater.rs | 11 ++++++----- src/lib.rs | 4 +++- src/subcommand/witness.rs | 2 +- src/subcommand/witness/ctype.rs | 9 ++++----- src/subcommand/witness/file.rs | 3 +-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 7fc080ee6e..5653a29563 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,11 +102,12 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block_hash: Option = if let Some(prev_height) = self.height.checked_sub(1) { - Some(index.client.get_block_hash(prev_height).unwrap()) - } else { - None - }; + let mut prev_block_hash: Option = + if let Some(prev_height) = self.height.checked_sub(1) { + Some(index.client.get_block_hash(prev_height).unwrap()) + } else { + None + }; loop { let block = match rx.recv() { diff --git a/src/lib.rs b/src/lib.rs index 1e762acd0f..37ce5f10c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,7 +153,9 @@ pub fn main() { env_logger::builder() .filter(Some("new_inscription_satpoint"), log::LevelFilter::Info) - .target(env_logger::Target::Pipe(Box::new(inscription_satpoint_logs_file))) + .target(env_logger::Target::Pipe(Box::new( + inscription_satpoint_logs_file, + ))) .format(|buf, record| writeln!(buf, "{}", record.args())) .init(); diff --git a/src/subcommand/witness.rs b/src/subcommand/witness.rs index 8112a8967e..fec8cd0cef 100644 --- a/src/subcommand/witness.rs +++ b/src/subcommand/witness.rs @@ -8,7 +8,7 @@ pub(crate) enum Witness { #[clap(about = "Display the content type from an inscription")] Ctype(ctype::Ctype), #[clap(about = "Write the inscription content to a file")] - File(file::File) + File(file::File), } impl Witness { diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 3e9a2666b5..2e10cab431 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -13,11 +13,10 @@ impl Ctype { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ - println!("{}", inscription.content_type().unwrap()); - - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); + if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { + println!("{}", inscription.content_type().unwrap()); + } else { + return Err(anyhow!("Inscription {} not found", inscription_id)); } Ok(()) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index ce23a47a6a..4bc3874a96 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -17,11 +17,10 @@ impl File { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)?{ + if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { let content_bytes = inscription.body().unwrap(); let mut file = fs::File::create(self.filename)?; file.write_all(content_bytes)?; - } else { return Err(anyhow!("Inscription {} not found", inscription_id)); } From 2b3c7f593f6d4591ecb1837ae96d2e289b5806d8 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Sat, 25 Mar 2023 12:41:43 -0300 Subject: [PATCH 26/42] improve code readability Co-authored-by: Clarke Plumo --- src/subcommand/witness/ctype.rs | 10 +++++----- src/subcommand/witness/file.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/subcommand/witness/ctype.rs b/src/subcommand/witness/ctype.rs index 2e10cab431..d99b3916d6 100644 --- a/src/subcommand/witness/ctype.rs +++ b/src/subcommand/witness/ctype.rs @@ -13,11 +13,11 @@ impl Ctype { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { - println!("{}", inscription.content_type().unwrap()); - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); - } + let inscription = index + .get_inscription_by_id(inscription_id)? + .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; + + println!("{}", inscription.content_type().unwrap()); Ok(()) } diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index 4bc3874a96..dbd03ccda1 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -17,13 +17,13 @@ impl File { let index = Index::open(&options)?; index.update()?; - if let Some(inscription) = index.get_inscription_by_id(inscription_id)? { - let content_bytes = inscription.body().unwrap(); - let mut file = fs::File::create(self.filename)?; - file.write_all(content_bytes)?; - } else { - return Err(anyhow!("Inscription {} not found", inscription_id)); - } + let inscription = index + .get_inscription_by_id(inscription_id)? + .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; + + let content_bytes = inscription.body().unwrap(); + let mut file = fs::File::create(self.filename)?; + file.write_all(content_bytes)?; Ok(()) } From 21fed5d43993c7cac2a03a3d0d5f574c0df571dc Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Mon, 27 Mar 2023 13:47:08 -0300 Subject: [PATCH 27/42] print file size --- src/subcommand/witness/file.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index dbd03ccda1..260e35812a 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -25,6 +25,8 @@ impl File { let mut file = fs::File::create(self.filename)?; file.write_all(content_bytes)?; + println!("{}", file.metadata()?.len()); + Ok(()) } } From a808347b5d3e112982fa6d409c9bf9559559b0cc Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Mon, 27 Mar 2023 14:07:51 -0300 Subject: [PATCH 28/42] skip write if file is empty --- src/subcommand/witness/file.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/subcommand/witness/file.rs b/src/subcommand/witness/file.rs index 260e35812a..006957b6b4 100644 --- a/src/subcommand/witness/file.rs +++ b/src/subcommand/witness/file.rs @@ -21,9 +21,10 @@ impl File { .get_inscription_by_id(inscription_id)? .ok_or_else(|| anyhow!("Inscription {} not found", inscription_id))?; - let content_bytes = inscription.body().unwrap(); let mut file = fs::File::create(self.filename)?; - file.write_all(content_bytes)?; + if let Some(content_bytes) = inscription.body() { + file.write_all(content_bytes)?; + } println!("{}", file.metadata()?.len()); From a67e864682d6911abc9f2ec9a711f4f322e3fb00 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Tue, 4 Apr 2023 23:29:35 -0700 Subject: [PATCH 29/42] fix inscription_satpoint.txt format, use localhost by default --- src/index/updater.rs | 2 +- src/index/updater/inscription_updater.rs | 2 +- src/subcommand/server.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 5653a29563..372ec5d178 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -127,7 +127,7 @@ impl Updater { if let Some(prev) = prev_block_hash { log::info!( target: "new_inscription_satpoint", - "{{ block_hash: {}, prev_block_hash:{}, tx_count:{} }}", + "{{\"block_hash\":\"{}\",\"prev_block_hash\":\"{}\",\"tx_count\":{}}}", &block.header.block_hash(), &prev, &block.txdata.len(), diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 4b5ae5d028..a7b27dbcbd 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -242,7 +242,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { let inscription_id = InscriptionId::load(inscription_id); let satpoint = SatPoint::load(new_satpoint); - log::info!(target: "new_inscription_satpoint", "{}, {}, {}", self.height, satpoint, inscription_id); + log::info!(target: "new_inscription_satpoint", "{},{},{}", self.height, satpoint, inscription_id); Ok(()) } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 9f79a777a3..d208d71b49 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -91,7 +91,7 @@ impl Display for StaticHtml { pub(crate) struct Server { #[clap( long, - default_value = "0.0.0.0", + default_value = "127.0.0.1", help = "Listen on
for incoming requests." )] address: String, From 816519a6cc3123ccffbb56dd426bb904dd8c3203 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Wed, 5 Apr 2023 11:31:20 -0700 Subject: [PATCH 30/42] fix indexer to look for inscriptions in all tx inputs --- src/inscription.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/inscription.rs b/src/inscription.rs index d0fba77016..525a23c1eb 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -29,7 +29,17 @@ impl Inscription { } pub(crate) fn from_transaction(tx: &Transaction) -> Option { - InscriptionParser::parse(&tx.input.get(0)?.witness).ok() + for input in &tx.input { + if let Some(inscription) = Inscription::from_tx_input(input) { + return Some(inscription); + } + } + + None + } + + pub(crate) fn from_tx_input(tx_in: &TxIn) -> Option { + InscriptionParser::parse(&tx_in.witness).ok() } pub(crate) fn from_file(chain: Chain, path: impl AsRef) -> Result { From df62dfc7f33d72f568a6d220fcf6786936784496 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 7 Apr 2023 00:16:27 -0700 Subject: [PATCH 31/42] include content_type, body, and file size in log file for new inscriptions --- src/index/updater/inscription_updater.rs | 35 +++++++++++++++++++++++- src/inscription.rs | 15 +++++----- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index a7b27dbcbd..6099fec6c2 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -4,6 +4,7 @@ pub(super) struct Flotsam { inscription_id: InscriptionId, offset: u64, origin: Origin, + tx_in: Option, } enum Origin { @@ -87,6 +88,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { offset: input_value + old_satpoint.offset, inscription_id, origin: Origin::Old(old_satpoint), + tx_in: None, }); } @@ -115,6 +117,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { inscription_id: txid.into(), offset: 0, origin: Origin::New(input_value - tx.output.iter().map(|txout| txout.value).sum::()), + tx_in: tx.input.get(0).cloned(), }); }; @@ -242,7 +245,37 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { let inscription_id = InscriptionId::load(inscription_id); let satpoint = SatPoint::load(new_satpoint); - log::info!(target: "new_inscription_satpoint", "{},{},{}", self.height, satpoint, inscription_id); + if let Some(tx_in) = flotsam.tx_in { + let inscription = Inscription::from_tx_input(&tx_in).unwrap(); + if let (Some(content_type), Some(body)) = (inscription.content_type(), inscription.body()) { + log::info!( + target: "new_inscription_satpoint", + "{},{},{},{},{},{}", + self.height, + satpoint, + inscription_id, + content_type, + hex::encode(body), + body.len() + ); + } else { + log::info!( + target: "new_inscription_satpoint", + "{},{},{}", + self.height, + satpoint, + inscription_id + ); + } + } else { + log::info!( + target: "new_inscription_satpoint", + "{},{},{}", + self.height, + satpoint, + inscription_id + ); + } Ok(()) } diff --git a/src/inscription.rs b/src/inscription.rs index 525a23c1eb..17669961f9 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -29,13 +29,14 @@ impl Inscription { } pub(crate) fn from_transaction(tx: &Transaction) -> Option { - for input in &tx.input { - if let Some(inscription) = Inscription::from_tx_input(input) { - return Some(inscription); - } - } - - None + InscriptionParser::parse(&tx.input.get(0)?.witness).ok() + // for input in &tx.input { + // if let Some(inscription) = Inscription::from_tx_input(input) { + // return Some(inscription); + // } + // } + + // None } pub(crate) fn from_tx_input(tx_in: &TxIn) -> Option { From 2d661acb6a1ba2d01803c92b223a806414914ee4 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 7 Apr 2023 10:07:11 -0700 Subject: [PATCH 32/42] remove body data from log file --- src/index/updater/inscription_updater.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 6099fec6c2..755fd6cd80 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -250,12 +250,12 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { if let (Some(content_type), Some(body)) = (inscription.content_type(), inscription.body()) { log::info!( target: "new_inscription_satpoint", - "{},{},{},{},{},{}", + "{},{},{},{},{}", self.height, satpoint, inscription_id, content_type, - hex::encode(body), + // hex::encode(body), body.len() ); } else { From 0b6c338b24f74f1e506811b336edf451eb26b9c5 Mon Sep 17 00:00:00 2001 From: ericatallah Date: Fri, 7 Apr 2023 13:53:40 -0700 Subject: [PATCH 33/42] fix log file to handle empty inscriptions, add test --- src/index/updater/inscription_updater.rs | 32 +++++++++--------------- src/inscription.rs | 11 ++++++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 755fd6cd80..f3917435b1 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -247,26 +247,18 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { if let Some(tx_in) = flotsam.tx_in { let inscription = Inscription::from_tx_input(&tx_in).unwrap(); - if let (Some(content_type), Some(body)) = (inscription.content_type(), inscription.body()) { - log::info!( - target: "new_inscription_satpoint", - "{},{},{},{},{}", - self.height, - satpoint, - inscription_id, - content_type, - // hex::encode(body), - body.len() - ); - } else { - log::info!( - target: "new_inscription_satpoint", - "{},{},{}", - self.height, - satpoint, - inscription_id - ); - } + let content_type = inscription.content_type().unwrap_or(""); + let content_len = inscription.body().map_or(0, |body| body.len()); + + log::info!( + target: "new_inscription_satpoint", + "{},{},{},{},{}", + self.height, + satpoint, + inscription_id, + content_type, + content_len, + ); } else { log::info!( target: "new_inscription_satpoint", diff --git a/src/inscription.rs b/src/inscription.rs index 17669961f9..4d32b12f23 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -400,6 +400,17 @@ mod tests { ); } + #[test] + fn no_content_type_and_no_body() { + assert_eq!( + InscriptionParser::parse(&envelope(&[b"ord"])), + Ok(Inscription { + content_type: None, + body: None, + }), + ); + } + #[test] fn valid_body_in_multiple_pushes() { assert_eq!( From 2569624d7bb1da5bb7dd9a7fd3ea95639bdabe62 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Tue, 11 Apr 2023 12:04:25 -0300 Subject: [PATCH 34/42] add inscription number to log --- src/index/updater/inscription_updater.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index f3917435b1..56dd114b88 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -196,6 +196,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { new_satpoint: SatPoint, ) -> Result { let inscription_id = flotsam.inscription_id.store(); + let inscription_number = self.next_number; match flotsam.origin { Origin::Old(old_satpoint) => { @@ -252,20 +253,22 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { log::info!( target: "new_inscription_satpoint", - "{},{},{},{},{}", + "{},{},{},{},{},{}", self.height, satpoint, inscription_id, + inscription_number, content_type, content_len, ); } else { log::info!( target: "new_inscription_satpoint", - "{},{},{}", + "{},{},{},{}", self.height, satpoint, - inscription_id + inscription_id, + inscription_number, ); } From 86b81815a4ec0f50267617483831c8a3ac53b233 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 13 Apr 2023 12:17:34 -0700 Subject: [PATCH 35/42] add block height to inscription_satpoint.txt log --- src/index/updater.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index 372ec5d178..ee25d5ae5c 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -127,7 +127,8 @@ impl Updater { if let Some(prev) = prev_block_hash { log::info!( target: "new_inscription_satpoint", - "{{\"block_hash\":\"{}\",\"prev_block_hash\":\"{}\",\"tx_count\":{}}}", + "{{\"height\":{},\"block_hash\":\"{}\",\"prev_block_hash\":\"{}\",\"tx_count\":{}}}", + &self.height, &block.header.block_hash(), &prev, &block.txdata.len(), From 5f68337fa8429a1f2db9c062d3a328511a715ee7 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 13 Apr 2023 14:39:43 -0700 Subject: [PATCH 36/42] fix off by 1 bug for height, clean up prev hash --- src/index/updater.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/index/updater.rs b/src/index/updater.rs index ee25d5ae5c..ad109378f1 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -102,12 +102,6 @@ impl Updater { let mut uncommitted = 0; let mut value_cache = HashMap::new(); - let mut prev_block_hash: Option = - if let Some(prev_height) = self.height.checked_sub(1) { - Some(index.client.get_block_hash(prev_height).unwrap()) - } else { - None - }; loop { let block = match rx.recv() { @@ -124,19 +118,17 @@ impl Updater { &mut value_cache, )?; - if let Some(prev) = prev_block_hash { + if self.height.checked_sub(1).is_some() { log::info!( target: "new_inscription_satpoint", "{{\"height\":{},\"block_hash\":\"{}\",\"prev_block_hash\":\"{}\",\"tx_count\":{}}}", - &self.height, + &self.height - 1, &block.header.block_hash(), - &prev, + &block.header.prev_blockhash, &block.txdata.len(), ); } - prev_block_hash = Some(block.header.block_hash()); - if let Some(progress_bar) = &mut progress_bar { progress_bar.inc(1); From 711553e41623f0230b8c44d98ffe9017266f9f9b Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 14 Apr 2023 01:27:37 -0300 Subject: [PATCH 37/42] create inscription-data endpoint --- src/subcommand/server.rs | 68 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index d208d71b49..2d68973ebb 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -17,7 +17,7 @@ use { http::{header, HeaderMap, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Redirect, Response}, routing::get, - Router, TypedHeader, + Router, TypedHeader, Json, }, axum_server::Handle, rust_embed::RustEmbed, @@ -87,6 +87,18 @@ impl Display for StaticHtml { } } +#[derive(Serialize, Deserialize)] +struct InscriptionData{ + id: InscriptionId, + owner_address: Address, + location: SatPoint, + genesis_height: u64, + genesis_transaction: Txid, + creator_address: Address, + content_length: usize, + content_type: String, +} + #[derive(Debug, Parser)] pub(crate) struct Server { #[clap( @@ -154,6 +166,7 @@ impl Server { .route("/feed.xml", get(Self::feed)) .route("/input/:block/:transaction/:input", get(Self::input)) .route("/inscription/:inscription_id", get(Self::inscription)) + .route("/inscription-data/:inscription_id", get(Self::inscription_data)) .route("/inscriptions", get(Self::inscriptions)) .route("/inscriptions/:from", get(Self::inscriptions_from)) .route("/install.sh", get(Self::install_script)) @@ -812,6 +825,59 @@ impl Server { } } + async fn inscription_data( + Extension(config): Extension>, + Extension(index): Extension>, + Path(DeserializeFromStr(inscription_id)): Path> + ) -> ServerResult> { + let entry = index + .get_inscription_entry(inscription_id)? + .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + + let inscription = index + .get_inscription_by_id(inscription_id)? + .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + + let satpoint = index + .get_inscription_satpoint_by_id(inscription_id)? + .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + + let output = index + .get_transaction(satpoint.outpoint.txid)? + .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .output + .into_iter() + .nth(satpoint.outpoint.vout.try_into().unwrap()) + .ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?; + + let creation_output = index + .get_transaction(inscription_id.txid)? + .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .output + .into_iter() + .nth(satpoint.outpoint.vout.try_into().unwrap()) + .ok_or_not_found(|| format!("inscription {inscription_id} creation transaction output"))?; + + let owner_address = config.chain + .address_from_script(&output.script_pubkey).ok().unwrap(); + + let creator_address = config.chain + .address_from_script(&creation_output.script_pubkey).ok().unwrap(); + + let insc = InscriptionData { + id: inscription_id, + owner_address, + genesis_height: entry.height, + genesis_transaction: inscription_id.txid, + creator_address, + location: satpoint, + content_length: inscription.content_length().unwrap_or(0), + content_type: inscription.content_type().unwrap_or("").to_string(), + }; + + Ok(Json(insc)) + } + async fn inscription( Extension(page_config): Extension>, Extension(index): Extension>, From 63dcb38ca7530e06eeffffb1891badd4bad55662 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 14 Apr 2023 01:28:44 -0300 Subject: [PATCH 38/42] cargo fmt --- src/subcommand/server.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 2d68973ebb..eb4c530e8f 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -17,7 +17,7 @@ use { http::{header, HeaderMap, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Redirect, Response}, routing::get, - Router, TypedHeader, Json, + Json, Router, TypedHeader, }, axum_server::Handle, rust_embed::RustEmbed, @@ -88,7 +88,7 @@ impl Display for StaticHtml { } #[derive(Serialize, Deserialize)] -struct InscriptionData{ +struct InscriptionData { id: InscriptionId, owner_address: Address, location: SatPoint, @@ -166,7 +166,10 @@ impl Server { .route("/feed.xml", get(Self::feed)) .route("/input/:block/:transaction/:input", get(Self::input)) .route("/inscription/:inscription_id", get(Self::inscription)) - .route("/inscription-data/:inscription_id", get(Self::inscription_data)) + .route( + "/inscription-data/:inscription_id", + get(Self::inscription_data), + ) .route("/inscriptions", get(Self::inscriptions)) .route("/inscriptions/:from", get(Self::inscriptions_from)) .route("/install.sh", get(Self::install_script)) @@ -828,7 +831,7 @@ impl Server { async fn inscription_data( Extension(config): Extension>, Extension(index): Extension>, - Path(DeserializeFromStr(inscription_id)): Path> + Path(DeserializeFromStr(inscription_id)): Path>, ) -> ServerResult> { let entry = index .get_inscription_entry(inscription_id)? @@ -858,11 +861,17 @@ impl Server { .nth(satpoint.outpoint.vout.try_into().unwrap()) .ok_or_not_found(|| format!("inscription {inscription_id} creation transaction output"))?; - let owner_address = config.chain - .address_from_script(&output.script_pubkey).ok().unwrap(); + let owner_address = config + .chain + .address_from_script(&output.script_pubkey) + .ok() + .unwrap(); - let creator_address = config.chain - .address_from_script(&creation_output.script_pubkey).ok().unwrap(); + let creator_address = config + .chain + .address_from_script(&creation_output.script_pubkey) + .ok() + .unwrap(); let insc = InscriptionData { id: inscription_id, From 7f47614888bc45f7ef84d2b9373ee335500bd626 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 14 Apr 2023 02:01:04 -0300 Subject: [PATCH 39/42] add inscription number --- src/subcommand/server.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index eb4c530e8f..a15d93b70f 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -89,6 +89,7 @@ impl Display for StaticHtml { #[derive(Serialize, Deserialize)] struct InscriptionData { + number: u64, id: InscriptionId, owner_address: Address, location: SatPoint, @@ -874,6 +875,7 @@ impl Server { .unwrap(); let insc = InscriptionData { + number: entry.number, id: inscription_id, owner_address, genesis_height: entry.height, From c65fd220442b2c5cb8d4e297ba3f2ef245fa5aca Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 14 Apr 2023 03:33:58 -0300 Subject: [PATCH 40/42] fix creator address --- src/subcommand/server.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index a15d93b70f..3d7fb55834 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -854,13 +854,22 @@ impl Server { .nth(satpoint.outpoint.vout.try_into().unwrap()) .ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?; - let creation_output = index + let previous_outpoint = index .get_transaction(inscription_id.txid)? .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .input + .into_iter() + .nth(inscription_id.index.try_into().unwrap()) + .ok_or_not_found(|| format!("inscription {inscription_id} creation transaction output"))? + .previous_output; + + let previous_output = index + .get_transaction(previous_outpoint.txid)? + .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? .output .into_iter() - .nth(satpoint.outpoint.vout.try_into().unwrap()) - .ok_or_not_found(|| format!("inscription {inscription_id} creation transaction output"))?; + .nth(previous_outpoint.vout.try_into().unwrap()) + .ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?; let owner_address = config .chain @@ -870,7 +879,7 @@ impl Server { let creator_address = config .chain - .address_from_script(&creation_output.script_pubkey) + .address_from_script(&previous_output.script_pubkey) .ok() .unwrap(); From e4cfb1238492c91603bd65a527551ae1d0f596f0 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Fri, 14 Apr 2023 05:11:56 -0300 Subject: [PATCH 41/42] fix inscription number on log --- src/index/updater/inscription_updater.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 56dd114b88..56309308dd 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -196,7 +196,6 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { new_satpoint: SatPoint, ) -> Result { let inscription_id = flotsam.inscription_id.store(); - let inscription_number = self.next_number; match flotsam.origin { Origin::Old(old_satpoint) => { @@ -245,6 +244,8 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { let inscription_id = InscriptionId::load(inscription_id); let satpoint = SatPoint::load(new_satpoint); + let inscription_entry = self.id_to_entry.get(&inscription_id.store())?.unwrap(); + let inscription_number = InscriptionEntry::load(inscription_entry.value()).number; if let Some(tx_in) = flotsam.tx_in { let inscription = Inscription::from_tx_input(&tx_in).unwrap(); From d30c857904ec1791e708eee2062820cd4e210533 Mon Sep 17 00:00:00 2001 From: Felipe Lincoln Date: Tue, 18 Apr 2023 23:40:33 -0300 Subject: [PATCH 42/42] update inscriptions data endpoint --- src/subcommand/server.rs | 94 +++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 3d7fb55834..cee9d0070e 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -12,11 +12,11 @@ use { }, axum::{ body, - extract::{Extension, Path, Query}, + extract::{Extension, Path, Query, RawBody}, headers::UserAgent, http::{header, HeaderMap, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Redirect, Response}, - routing::get, + routing::{get, post}, Json, Router, TypedHeader, }, axum_server::Handle, @@ -27,7 +27,7 @@ use { caches::DirCache, AcmeConfig, }, - std::{cmp::Ordering, str}, + std::{cmp::Ordering, str, collections::HashMap}, tokio_stream::StreamExt, tower_http::{ compression::CompressionLayer, @@ -87,7 +87,7 @@ impl Display for StaticHtml { } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] struct InscriptionData { number: u64, id: InscriptionId, @@ -100,6 +100,16 @@ struct InscriptionData { content_type: String, } +#[derive(Serialize)] +struct InscriptionsDataResult { + inscriptions: HashMap, +} + +#[derive(Serialize, Deserialize, Debug)] +struct InscriptionsDataRequest { + ids: Vec, +} + #[derive(Debug, Parser)] pub(crate) struct Server { #[clap( @@ -167,10 +177,7 @@ impl Server { .route("/feed.xml", get(Self::feed)) .route("/input/:block/:transaction/:input", get(Self::input)) .route("/inscription/:inscription_id", get(Self::inscription)) - .route( - "/inscription-data/:inscription_id", - get(Self::inscription_data), - ) + .route("/inscriptions-data/", post(Self::inscriptions_data)) .route("/inscriptions", get(Self::inscriptions)) .route("/inscriptions/:from", get(Self::inscriptions_from)) .route("/install.sh", get(Self::install_script)) @@ -829,61 +836,86 @@ impl Server { } } - async fn inscription_data( - Extension(config): Extension>, + async fn inscriptions_data( + Extension(page_config): Extension>, Extension(index): Extension>, - Path(DeserializeFromStr(inscription_id)): Path>, - ) -> ServerResult> { - let entry = index - .get_inscription_entry(inscription_id)? - .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + RawBody(body): RawBody, + ) -> ServerResult> { + let body_bytes = hyper::body::to_bytes(body).await.unwrap().to_vec(); + let body_string = String::from_utf8(body_bytes).unwrap(); + let request_body = serde_json::from_str::(&body_string).unwrap(); + + let inscriptions = request_body.ids; + let mut data_map: HashMap = HashMap::new(); + + for inscription_id in inscriptions.into_iter() { + match Server::get_inscription_data(&index, page_config.chain, inscription_id).await { + Ok(inscription_data) => { + data_map.insert(inscription_id, inscription_data); + } + Err(_) => { + // skip broken inscriptions + continue; + } + } + } - let inscription = index - .get_inscription_by_id(inscription_id)? - .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + let inscription_data_result = InscriptionsDataResult { + inscriptions: data_map, + }; + + Ok(Json(inscription_data_result)) + } + + async fn get_inscription_data( + index: &Arc, + chain: Chain, + inscription_id: InscriptionId, + ) -> Result { + let entry = index.get_inscription_entry(inscription_id)?.unwrap(); + + let inscription = index.get_inscription_by_id(inscription_id)?.unwrap(); let satpoint = index .get_inscription_satpoint_by_id(inscription_id)? - .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + .unwrap(); let output = index .get_transaction(satpoint.outpoint.txid)? - .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .unwrap() .output .into_iter() .nth(satpoint.outpoint.vout.try_into().unwrap()) - .ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?; + .unwrap(); let previous_outpoint = index .get_transaction(inscription_id.txid)? - .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .unwrap() .input .into_iter() .nth(inscription_id.index.try_into().unwrap()) - .ok_or_not_found(|| format!("inscription {inscription_id} creation transaction output"))? + .unwrap() .previous_output; let previous_output = index .get_transaction(previous_outpoint.txid)? - .ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))? + .unwrap() .output .into_iter() .nth(previous_outpoint.vout.try_into().unwrap()) - .ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?; + .unwrap(); - let owner_address = config - .chain + let owner_address = chain .address_from_script(&output.script_pubkey) .ok() .unwrap(); - let creator_address = config - .chain + let creator_address = chain .address_from_script(&previous_output.script_pubkey) .ok() .unwrap(); - let insc = InscriptionData { + let inscription_data = InscriptionData { number: entry.number, id: inscription_id, owner_address, @@ -895,7 +927,7 @@ impl Server { content_type: inscription.content_type().unwrap_or("").to_string(), }; - Ok(Json(insc)) + Ok(inscription_data) } async fn inscription(