forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
291 lines (262 loc) · 7.15 KB
/
lib.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![allow(
clippy::large_enum_variant,
clippy::result_large_err,
clippy::too_many_arguments,
clippy::type_complexity
)]
#![deny(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]
use {
self::{
arguments::Arguments,
blocktime::Blocktime,
decimal::Decimal,
deserialize_from_str::DeserializeFromStr,
index::BitcoinCoreRpcResultExt,
inscriptions::{
inscription_id,
media::{self, ImageRendering, Media},
teleburn, ParsedEnvelope,
},
into_usize::IntoUsize,
representation::Representation,
settings::Settings,
subcommand::{Subcommand, SubcommandResult},
tally::Tally,
},
anyhow::{anyhow, bail, ensure, Context, Error},
bip39::Mnemonic,
bitcoin::{
address::{Address, NetworkUnchecked},
blockdata::{
constants::{DIFFCHANGE_INTERVAL, MAX_SCRIPT_ELEMENT_SIZE, SUBSIDY_HALVING_INTERVAL},
locktime::absolute::LockTime,
},
consensus::{self, Decodable, Encodable},
hash_types::{BlockHash, TxMerkleNode},
hashes::Hash,
script, Amount, Block, Network, OutPoint, Script, ScriptBuf, Sequence, Transaction, TxIn,
TxOut, Txid, Witness,
},
bitcoincore_rpc::{Client, RpcApi},
chrono::{DateTime, TimeZone, Utc},
ciborium::Value,
clap::{ArgGroup, Parser},
html_escaper::{Escape, Trusted},
http::HeaderMap,
lazy_static::lazy_static,
ordinals::{
varint, Charm, Edict, Epoch, Etching, Height, Pile, Rarity, Rune, RuneId, Runestone, Sat,
SatPoint, SpacedRune, Terms,
},
regex::Regex,
reqwest::Url,
serde::{Deserialize, Deserializer, Serialize},
serde_with::{DeserializeFromStr, SerializeDisplay},
std::{
cmp::{self, Reverse},
collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
env,
fmt::{self, Display, Formatter},
fs,
io::{self, Cursor, Read},
mem,
net::ToSocketAddrs,
path::{Path, PathBuf},
process::{self, Command, Stdio},
str::FromStr,
sync::{
atomic::{self, AtomicBool},
Arc, Mutex,
},
thread,
time::{Duration, Instant, SystemTime},
},
sysinfo::System,
tokio::{runtime::Runtime, task},
};
pub use self::{
chain::Chain,
fee_rate::FeeRate,
index::{Index, RuneEntry},
inscriptions::{Envelope, Inscription, InscriptionId},
object::Object,
options::Options,
wallet::transaction_builder::{Target, TransactionBuilder},
};
#[cfg(test)]
#[macro_use]
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;
pub mod chain;
mod decimal;
mod deserialize_from_str;
mod fee_rate;
pub mod index;
mod inscriptions;
mod into_usize;
mod object;
pub mod options;
pub mod outgoing;
mod re;
mod representation;
pub mod runes;
mod settings;
pub mod subcommand;
mod tally;
pub mod templates;
pub mod wallet;
type Result<T = (), E = Error> = std::result::Result<T, E>;
const TARGET_POSTAGE: Amount = Amount::from_sat(10_000);
static SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
static LISTENERS: Mutex<Vec<axum_server::Handle>> = Mutex::new(Vec::new());
static INDEXER: Mutex<Option<thread::JoinHandle<()>>> = Mutex::new(None);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn fund_raw_transaction(
client: &Client,
fee_rate: FeeRate,
unfunded_transaction: &Transaction,
) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
{
unfunded_transaction.version.consensus_encode(&mut buffer)?;
unfunded_transaction.input.consensus_encode(&mut buffer)?;
unfunded_transaction.output.consensus_encode(&mut buffer)?;
unfunded_transaction
.lock_time
.consensus_encode(&mut buffer)?;
}
Ok(
client
.fund_raw_transaction(
&buffer,
Some(&bitcoincore_rpc::json::FundRawTransactionOptions {
// NB. This is `fundrawtransaction`'s `feeRate`, which is fee per kvB
// and *not* fee per vB. So, we multiply the fee rate given by the user
// by 1000.
fee_rate: Some(Amount::from_sat((fee_rate.n() * 1000.0).ceil() as u64)),
change_position: Some(unfunded_transaction.output.len().try_into()?),
..default()
}),
Some(false),
)
.map_err(|err| {
if matches!(
err,
bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { code: -6, .. }
))
) {
anyhow!("not enough cardinal utxos")
} else {
err.into()
}
})?
.hex,
)
}
pub fn timestamp(seconds: u64) -> DateTime<Utc> {
Utc
.timestamp_opt(seconds.try_into().unwrap_or(i64::MAX), 0)
.unwrap()
}
fn target_as_block_hash(target: bitcoin::Target) -> BlockHash {
BlockHash::from_raw_hash(Hash::from_byte_array(target.to_le_bytes()))
}
fn unbound_outpoint() -> OutPoint {
OutPoint {
txid: Hash::all_zeros(),
vout: 0,
}
}
fn uncheck(address: &Address) -> Address<NetworkUnchecked> {
address.to_string().parse().unwrap()
}
fn default<T: Default>() -> T {
Default::default()
}
pub fn parse_ord_server_args(args: &str) -> (Settings, subcommand::server::Server) {
match Arguments::try_parse_from(args.split_whitespace()) {
Ok(arguments) => match arguments.subcommand {
Subcommand::Server(server) => (
Settings::merge(
arguments.options,
vec![("INTEGRATION_TEST".into(), "1".into())]
.into_iter()
.collect(),
)
.unwrap(),
server,
),
subcommand => panic!("unexpected subcommand: {subcommand:?}"),
},
Err(err) => panic!("error parsing arguments: {err}"),
}
}
fn gracefully_shutdown_indexer() {
if let Some(indexer) = INDEXER.lock().unwrap().take() {
// We explicitly set this to true to notify the thread to not take on new work
SHUTTING_DOWN.store(true, atomic::Ordering::Relaxed);
log::info!("Waiting for index thread to finish...");
if indexer.join().is_err() {
log::warn!("Index thread panicked; join failed");
}
}
}
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.");
LISTENERS
.lock()
.unwrap()
.iter()
.for_each(|handle| handle.graceful_shutdown(Some(Duration::from_millis(100))));
gracefully_shutdown_indexer();
})
.expect("Error setting <CTRL-C> handler");
let args = Arguments::parse();
let minify = args.options.minify;
match args.run() {
Err(err) => {
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());
}
gracefully_shutdown_indexer();
process::exit(1);
}
Ok(output) => {
if let Some(output) = output {
output.print_json(minify);
}
gracefully_shutdown_indexer();
}
}
}