Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ordinals/ord into fix-charms
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Dec 1, 2023
2 parents 7c978a7 + 733b74d commit 9b653bc
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 21 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ copyright = "The Ord Maintainers"
maintainer = "The Ord Maintainers"

[workspace]
members = [".", "test-bitcoincore-rpc"]
members = [".", "test-bitcoincore-rpc", "crates/*"]

[dependencies]
anyhow = { version = "1.0.56", features = ["backtrace"] }
Expand Down
9 changes: 9 additions & 0 deletions crates/audit-cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "audit-cache"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
colored = "2.0.4"
reqwest = { version = "0.11.22", features = ["blocking"] }
88 changes: 88 additions & 0 deletions crates/audit-cache/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use {
colored::Colorize,
reqwest::{blocking::get, StatusCode},
std::process,
};

const ENDPOINTS: &[(&str, StatusCode, &str)] = &[
// PNG content is cached
(
"/content/6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i0",
StatusCode::OK,
"HIT",
),
// HTML content is cached
(
"/content/114c5c87c4d0a7facb2b4bf515a4ad385182c076a5cfcc2982bf2df103ec0fffi0",
StatusCode::OK,
"HIT",
),
// content respopnses that aren't found aren't cached
(
"/content/6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i1",
StatusCode::NOT_FOUND,
"BYPASS",
),
// HTML previews are cached
(
"/preview/114c5c87c4d0a7facb2b4bf515a4ad385182c076a5cfcc2982bf2df103ec0fffi0",
StatusCode::OK,
"HIT",
),
// non-HTML previews are not cached
(
"/preview/6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i0",
StatusCode::OK,
"BYPASS",
),
("/static/index.css", StatusCode::OK, "HIT"),
("/static/index.js", StatusCode::OK, "HIT"),
("/sat/FOO", StatusCode::BAD_REQUEST, "HIT"),
("/", StatusCode::OK, "BYPASS"),
("/blockheight", StatusCode::OK, "BYPASS"),
];

fn main() {
eprint!("Warming up the cache");

for (endpoint, expected_status_code, _expected_cache_status) in ENDPOINTS {
let response = get(format!("https://ordinals.com{endpoint}")).unwrap();

assert_eq!(response.status(), *expected_status_code);

eprint!(".");
}

eprintln!();

let mut failures = 0;

for (endpoint, expected_status_code, expected_cache_status) in ENDPOINTS {
eprint!("GET {endpoint}");

let response = get(format!("https://ordinals.com{endpoint}")).unwrap();

let status_code = response.status();

eprint!(" {}", status_code.as_u16());

assert_eq!(response.status(), *expected_status_code);

let cache_status = response.headers().get("cf-cache-status").unwrap();

let pass = cache_status == expected_cache_status;

if pass {
eprintln!(" {}", cache_status.to_str().unwrap().green());
} else {
eprintln!(" {}", cache_status.to_str().unwrap().red());
}

failures += u32::from(!pass);
}

if failures > 0 {
eprintln!("{failures} failures");
process::exit(1);
}
}
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,6 @@ convert-logo-to-favicon:

update-mdbook-theme:
curl https://raw.githubusercontent.com/rust-lang/mdBook/v0.4.35/src/theme/index.hbs > docs/theme/index.hbs

audit-cache:
cargo run --package audit-cache
52 changes: 39 additions & 13 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,37 +901,63 @@ impl Index {
Ok(runic)
}

#[cfg(test)]
pub(crate) fn get_rune_balances(&self) -> Vec<(OutPoint, Vec<(RuneId, u128)>)> {
pub(crate) fn get_rune_balance_map(&self) -> Result<BTreeMap<Rune, BTreeMap<OutPoint, u128>>> {
let outpoint_balances = self.get_rune_balances()?;

let rtx = self.database.begin_read()?;

let rune_id_to_rune_entry = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;

let mut rune_balances: BTreeMap<Rune, BTreeMap<OutPoint, u128>> = BTreeMap::new();

for (outpoint, balances) in outpoint_balances {
for (rune_id, amount) in balances {
let rune = RuneEntry::load(
rune_id_to_rune_entry
.get(&rune_id.store())?
.unwrap()
.value(),
)
.rune;

*rune_balances
.entry(rune)
.or_default()
.entry(outpoint)
.or_default() += amount;
}
}

Ok(rune_balances)
}

pub(crate) fn get_rune_balances(&self) -> Result<Vec<(OutPoint, Vec<(RuneId, u128)>)>> {
let mut result = Vec::new();

for entry in self
.database
.begin_read()
.unwrap()
.open_table(OUTPOINT_TO_RUNE_BALANCES)
.unwrap()
.iter()
.unwrap()
.begin_read()?
.open_table(OUTPOINT_TO_RUNE_BALANCES)?
.iter()?
{
let (outpoint, balances_buffer) = entry.unwrap();
let (outpoint, balances_buffer) = entry?;
let outpoint = OutPoint::load(*outpoint.value());
let balances_buffer = balances_buffer.value();

let mut balances = Vec::new();
let mut i = 0;
while i < balances_buffer.len() {
let (id, length) = runes::varint::decode(&balances_buffer[i..]).unwrap();
let (id, length) = runes::varint::decode(&balances_buffer[i..])?;
i += length;
let (balance, length) = runes::varint::decode(&balances_buffer[i..]).unwrap();
let (balance, length) = runes::varint::decode(&balances_buffer[i..])?;
i += length;
balances.push((RuneId::try_from(id).unwrap(), balance));
balances.push((RuneId::try_from(id)?, balance));
}

result.push((outpoint, balances));
}

result
Ok(result)
}

pub(crate) fn block_header(&self, hash: BlockHash) -> Result<Option<Header>> {
Expand Down
2 changes: 1 addition & 1 deletion src/index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Context {

assert_eq!(runes, self.index.runes().unwrap());

assert_eq!(balances, self.index.get_rune_balances());
assert_eq!(balances, self.index.get_rune_balances().unwrap());

let mut outstanding: HashMap<RuneId, u128> = HashMap::new();

Expand Down
4 changes: 4 additions & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;

pub mod balances;
pub mod decode;
pub mod epochs;
pub mod find;
Expand All @@ -17,6 +18,8 @@ pub mod wallet;

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
#[command(about = "List all rune balances")]
Balances,
#[command(about = "Decode a transaction")]
Decode(decode::Decode),
#[command(about = "List the first satoshis of each reward epoch")]
Expand Down Expand Up @@ -50,6 +53,7 @@ pub(crate) enum Subcommand {
impl Subcommand {
pub(crate) fn run(self, options: Options) -> SubcommandResult {
match self {
Self::Balances => balances::run(options),
Self::Decode(decode) => decode.run(),
Self::Epochs => epochs::run(),
Self::Find(find) => find.run(options),
Expand Down
21 changes: 21 additions & 0 deletions src/subcommand/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub runes: BTreeMap<Rune, BTreeMap<OutPoint, u128>>,
}

pub(crate) fn run(options: Options) -> SubcommandResult {
let index = Index::open(&options)?;

ensure!(
index.has_rune_index(),
"`ord balances` requires index created with `--index-runes-pre-alpha-i-agree-to-get-rekt` flag",
);

index.update()?;

Ok(Box::new(Output {
runes: index.get_rune_balance_map()?,
}))
}
8 changes: 4 additions & 4 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2151,7 +2151,7 @@ mod tests {
);

assert_eq!(
server.index.get_rune_balances(),
server.index.get_rune_balances().unwrap(),
[(OutPoint { txid, vout: 0 }, vec![(id, u128::max_value())])]
);

Expand Down Expand Up @@ -2220,7 +2220,7 @@ mod tests {
);

assert_eq!(
server.index.get_rune_balances(),
server.index.get_rune_balances().unwrap(),
[(OutPoint { txid, vout: 0 }, vec![(id, u128::max_value())])]
);

Expand Down Expand Up @@ -2326,7 +2326,7 @@ mod tests {
);

assert_eq!(
server.index.get_rune_balances(),
server.index.get_rune_balances().unwrap(),
[(OutPoint { txid, vout: 0 }, vec![(id, u128::max_value())])]
);

Expand Down Expand Up @@ -2396,7 +2396,7 @@ mod tests {
let output = OutPoint { txid, vout: 0 };

assert_eq!(
server.index.get_rune_balances(),
server.index.get_rune_balances().unwrap(),
[(output, vec![(id, u128::max_value())])]
);

Expand Down
13 changes: 11 additions & 2 deletions test-bitcoincore-rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,19 @@ impl Api for Server {
.map(|txout| txout.value)
.sum::<u64>();

let (outpoint, input_value) = state
let mut utxos = state
.utxos
.clone()
.into_iter()
.map(|(outpoint, value)| (value, outpoint))
.collect::<Vec<(Amount, OutPoint)>>();

utxos.sort();
utxos.reverse();

let (input_value, outpoint) = utxos
.iter()
.find(|(outpoint, value)| value.to_sat() >= output_value && !state.locked.contains(outpoint))
.find(|(value, outpoint)| value.to_sat() >= output_value && !state.locked.contains(outpoint))
.ok_or_else(Self::not_found)?;

transaction.input.push(TxIn {
Expand Down
Loading

0 comments on commit 9b653bc

Please sign in to comment.