Skip to content

Commit

Permalink
Allow pausing and resuming etchings (ordinals#3374)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Mar 31, 2024
1 parent 07bc59b commit 06e124a
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ urlencoding = "2.1.3"
[dev-dependencies]
criterion = "0.5.1"
executable-path = "1.0.0"
nix = "0.28.0"
nix = { version = "0.28.0", features = ["signal"] }
pretty_assertions = "1.2.1"
reqwest = { version = "0.11.10", features = ["blocking", "brotli", "json"] }
mockcore = { path = "crates/mockcore" }
Expand Down
13 changes: 0 additions & 13 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,6 @@ pub(crate) mod testing;

const SCHEMA_VERSION: u64 = 24;

macro_rules! define_table {
($name:ident, $key:ty, $value:ty) => {
const $name: TableDefinition<$key, $value> = TableDefinition::new(stringify!($name));
};
}

macro_rules! define_multimap_table {
($name:ident, $key:ty, $value:ty) => {
const $name: MultimapTableDefinition<$key, $value> =
MultimapTableDefinition::new(stringify!($name));
};
}

define_multimap_table! { SATPOINT_TO_SEQUENCE_NUMBER, &SatPointValue, u32 }
define_multimap_table! { SAT_TO_SEQUENCE_NUMBER, u64, u32 }
define_multimap_table! { SEQUENCE_NUMBER_TO_CHILDREN, u32, u32 }
Expand Down
13 changes: 2 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ mod test;
#[cfg(test)]
use self::test::*;

macro_rules! tprintln {
($($arg:tt)*) => {
if cfg!(test) {
eprint!("==> ");
eprintln!($($arg)*);
}
};
}

pub mod api;
pub mod arguments;
mod blocktime;
Expand All @@ -117,6 +108,7 @@ mod fee_rate;
pub mod index;
mod inscriptions;
mod into_usize;
mod macros;
mod object;
pub mod options;
pub mod outgoing;
Expand Down Expand Up @@ -241,13 +233,12 @@ fn gracefully_shutdown_indexer() {

pub fn main() {
env_logger::init();

ctrlc::set_handler(move || {
if SHUTTING_DOWN.fetch_or(true, atomic::Ordering::Relaxed) {
process::exit(1);
}

println!("Shutting down gracefully. Press <CTRL-C> again to shutdown immediately.");
eprintln!("Shutting down gracefully. Press <CTRL-C> again to shutdown immediately.");

LISTENERS
.lock()
Expand Down
51 changes: 51 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[macro_export]
macro_rules! define_table {
($name:ident, $key:ty, $value:ty) => {
const $name: TableDefinition<$key, $value> = TableDefinition::new(stringify!($name));
};
}

#[macro_export]
macro_rules! define_multimap_table {
($name:ident, $key:ty, $value:ty) => {
const $name: MultimapTableDefinition<$key, $value> =
MultimapTableDefinition::new(stringify!($name));
};
}

#[macro_export]
macro_rules! tprintln {
($($arg:tt)*) => {
if cfg!(test) {
eprint!("==> ");
eprintln!($($arg)*);
}
};
}

#[macro_export]
macro_rules! assert_regex_match {
($value:expr, $pattern:expr $(,)?) => {
let regex = Regex::new(&format!("^(?s){}$", $pattern)).unwrap();
let string = $value.to_string();

if !regex.is_match(string.as_ref()) {
eprintln!("Regex did not match:");
pretty_assert_eq!(regex.as_str(), string);
}
};
}

#[macro_export]
macro_rules! assert_matches {
($expression:expr, $( $pattern:pat_param )|+ $( if $guard:expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => {}
left => panic!(
"assertion failed: (left ~= right)\n left: `{:?}`\n right: `{}`",
left,
stringify!($($pattern)|+ $(if $guard)?)
),
}
}
}
4 changes: 4 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod mint;
pub mod outputs;
pub mod receive;
pub mod restore;
pub mod resume;
pub mod sats;
pub mod send;
mod shared_args;
Expand Down Expand Up @@ -57,6 +58,8 @@ pub(crate) enum Subcommand {
Receive(receive::Receive),
#[command(about = "Restore wallet")]
Restore(restore::Restore),
#[command(about = "Resume pending etchings")]
Resume,
#[command(about = "List wallet satoshis")]
Sats(sats::Sats),
#[command(about = "Send sat or inscription")]
Expand Down Expand Up @@ -99,6 +102,7 @@ impl WalletCommand {
Subcommand::Inscriptions => inscriptions::run(wallet),
Subcommand::Mint(mint) => mint.run(wallet),
Subcommand::Receive(receive) => receive.run(wallet),
Subcommand::Resume => resume::run(wallet),
Subcommand::Sats(sats) => sats.run(wallet),
Subcommand::Send(send) => send.run(wallet),
Subcommand::Transactions(transactions) => transactions.run(wallet),
Expand Down
5 changes: 5 additions & 0 deletions src/subcommand/wallet/batch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ impl Batch {
fn check_etching(wallet: &Wallet, etching: &batch::Etching) -> Result {
let rune = etching.rune.rune;

ensure!(
wallet.load_etching(rune)?.is_none(),
"rune `{rune}` has pending etching, resume with `ord wallet resume`"
);

ensure!(!rune.is_reserved(), "rune `{rune}` is reserved");

ensure!(
Expand Down
18 changes: 18 additions & 0 deletions src/subcommand/wallet/resume.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ResumeOutput {
pub etchings: Vec<batch::Output>,
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let outputs: Result<Vec<batch::Output>> = wallet
.pending_etchings()?
.into_iter()
.map(|(rune, entry)| {
wallet.wait_for_maturation(&rune, entry.commit, entry.reveal, entry.output)
})
.collect();

outputs.map(|etchings| Some(Box::new(ResumeOutput { etchings }) as Box<dyn Output>))
}
25 changes: 0 additions & 25 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,6 @@ pub(crate) use {
unindent::Unindent,
};

macro_rules! assert_regex_match {
($value:expr, $pattern:expr $(,)?) => {
let regex = Regex::new(&format!("^(?s){}$", $pattern)).unwrap();
let string = $value.to_string();

if !regex.is_match(string.as_ref()) {
eprintln!("Regex did not match:");
pretty_assert_eq!(regex.as_str(), string);
}
};
}

macro_rules! assert_matches {
($expression:expr, $( $pattern:pat_param )|+ $( if $guard:expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => {}
left => panic!(
"assertion failed: (left ~= right)\n left: `{:?}`\n right: `{}`",
left,
stringify!($($pattern)|+ $(if $guard)?)
),
}
}
}

pub(crate) fn txid(n: u64) -> Txid {
let hex = format!("{n:x}");

Expand Down
Loading

0 comments on commit 06e124a

Please sign in to comment.