forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.rs
144 lines (131 loc) Β· 3.34 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#![allow(clippy::too_many_arguments)]
use {
self::{
arguments::Arguments,
blocktime::Blocktime,
bytes::Bytes,
degree::Degree,
epoch::Epoch,
height::Height,
index::{Index, List},
options::Options,
ordinal::Ordinal,
purse::Purse,
rarity::Rarity,
sat_point::SatPoint,
subcommand::Subcommand,
},
anyhow::{anyhow, bail, Context, Error},
axum::{extract, http::StatusCode, response::Html, response::IntoResponse, routing::get, Router},
axum_server::Handle,
bdk::{
blockchain::rpc::{Auth, RpcBlockchain, RpcConfig},
blockchain::{Blockchain, ConfigurableBlockchain},
database::SqliteDatabase,
keys::bip39::{Language, Mnemonic},
template::Bip84,
wallet::{signer::SignOptions, AddressIndex::LastUnused},
wallet::{wallet_name_from_descriptor, SyncOptions},
FeeRate, KeychainKind, LocalUtxo,
},
bitcoin::{
blockdata::{constants::COIN_VALUE, transaction::TxOut},
consensus::{Decodable, Encodable},
hash_types::BlockHash,
hashes::{sha256d, Hash},
secp256k1::{
rand::{self},
Secp256k1,
},
util::psbt::PartiallySignedTransaction,
Address, Block, Network, OutPoint, Transaction, Txid,
},
chrono::{DateTime, NaiveDateTime, Utc},
clap::Parser,
derive_more::{Display, FromStr},
redb::{Database, ReadableTable, Table, TableDefinition, WriteTransaction},
serde::{Deserialize, Serialize},
std::{
cmp::Ordering,
collections::VecDeque,
env,
fmt::{self, Display, Formatter},
fs, io,
net::ToSocketAddrs,
ops::{Add, AddAssign, Deref, Mul, Sub},
path::{Path, PathBuf},
process,
str::FromStr,
sync::{
atomic::{self, AtomicU64},
Arc, Mutex,
},
thread,
time::{Duration, Instant},
},
tokio::{runtime::Runtime, task},
tower_http::cors::{Any, CorsLayer},
};
#[cfg(test)]
use regex::Regex;
#[cfg(test)]
macro_rules! assert_regex_match {
($string:expr, $pattern:expr $(,)?) => {
let pattern: &'static str = $pattern;
let regex = Regex::new(&format!("^(?s){}$", pattern)).unwrap();
let string = $string;
if !regex.is_match(string.as_ref()) {
panic!(
"Regex:\n\n{}\n\nβ¦did not match string:\n\n{}",
regex, string
);
}
};
}
mod arguments;
mod blocktime;
mod bytes;
mod degree;
mod epoch;
mod height;
mod index;
mod options;
mod ordinal;
mod purse;
mod rarity;
mod sat_point;
mod subcommand;
type Result<T = (), E = Error> = std::result::Result<T, E>;
const PERIOD_BLOCKS: u64 = 2016;
const CYCLE_EPOCHS: u64 = 6;
static INTERRUPTS: AtomicU64 = AtomicU64::new(0);
static LISTENERS: Mutex<Vec<Handle>> = Mutex::new(Vec::new());
fn main() {
env_logger::init();
ctrlc::set_handler(move || {
LISTENERS
.lock()
.unwrap()
.iter()
.for_each(|handle| handle.graceful_shutdown(Some(Duration::from_millis(100))));
let interrupts = INTERRUPTS.fetch_add(1, atomic::Ordering::Relaxed);
if interrupts > 5 {
process::exit(1);
}
})
.expect("Error setting ctrl-c handler");
if let Err(err) = Arguments::parse().run() {
eprintln!("error: {}", err);
err
.chain()
.skip(1)
.for_each(|cause| eprintln!("because: {}", cause));
if env::var_os("RUST_BACKTRACE")
.map(|val| val == "1")
.unwrap_or_default()
{
eprintln!("{}", err.backtrace());
}
process::exit(1);
}
}