Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"adding mine block to test server" #544

Merged
merged 30 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
64b06bf
"adding mine block to test server"
raphjaph Sep 20, 2022
7dbc148
Implement generate_to_address
casey Sep 20, 2022
2439b8b
Add TODO
casey Sep 20, 2022
b79f549
Use Blocks struct
casey Sep 20, 2022
e07072e
Use BitcoinRpcServerHandle::url
casey Sep 20, 2022
a52199e
Check height endpoint text in function
casey Sep 20, 2022
d0ae807
Remove TODO
casey Sep 20, 2022
97af02a
Fix imports
casey Sep 20, 2022
d1c8110
converted some tests and wrote helper functions
raphjaph Sep 21, 2022
5c79037
rebase on top of master
raphjaph Sep 22, 2022
f1ec43e
ignore return value
raphjaph Sep 22, 2022
9c0807c
lets see what happens here
raphjaph Sep 22, 2022
277b7c5
some formatting stuff
raphjaph Sep 22, 2022
828998f
stuff
raphjaph Sep 22, 2022
6a856ab
Shutdown TestServers in background
casey Sep 22, 2022
9f06dd8
Remove old redb
casey Sep 22, 2022
125486e
Remove serial_test
casey Sep 22, 2022
3ed1b3e
tweak
casey Sep 22, 2022
e987fd8
Add mine block function
casey Sep 22, 2022
1bdd4a9
Remove loop
casey Sep 22, 2022
2444963
Add report-test-time recipe
casey Sep 22, 2022
7205ed8
Use Checksum to make tests strategy
casey Sep 22, 2022
c0b84b2
Get rid of LISTENERS static
casey Sep 22, 2022
497351a
Reuse macro
casey Sep 22, 2022
411a3be
Revert "Get rid of LISTENERS static"
casey Sep 22, 2022
44763d6
Sort members
casey Sep 22, 2022
95a70aa
Remove note
casey Sep 22, 2022
7661562
Always sync before making a request
casey Sep 22, 2022
fc2cde7
Rename methods
casey Sep 22, 2022
78261d0
Remove old height tests
casey Sep 22, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 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 @@ -26,7 +26,7 @@ http = "0.2.6"
log = "0.4.14"
mime_guess = "2.0.4"
rayon = "1.5.1"
redb = "0.6.0"
redb = { version = "0.6.0", git = "https://github.com/casey/redb.git", branch = "add-write-lock" }
rust-embed = "6.4.0"
rustls = "0.20.6"
rustls-acme = { version = "0.5.0", features = ["axum"] }
Expand Down
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ test-deploy:
. [email protected]:ord
ssh [email protected] 'cd ord && ./deploy/setup'

report-test-time:
cargo +nightly test --bin ord -- -Z unstable-options --report-time

status:
ssh [email protected] systemctl status bitcoind
ssh [email protected] systemctl status ord
Expand Down
4 changes: 3 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Index {
Ok(database) => database,
Err(redb::Error::Io(error)) if error.kind() == io::ErrorKind::NotFound => unsafe {
Database::builder()
.set_write_strategy(WriteStrategy::TwoPhase)
.set_write_strategy(WriteStrategy::Checksum)
.create(&database_path, options.max_index_size().0)?
},
Err(error) => return Err(error.into()),
Expand Down Expand Up @@ -540,6 +540,8 @@ mod tests {
fn height_limit() {
let bitcoin_rpc_server = BitcoinRpcServer::spawn();

bitcoin_rpc_server.mine_block();

let tempdir = TempDir::new().unwrap();

let cookie_file = tempdir.path().join("cookie");
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use {
axum::{extract, http::StatusCode, response::Html, response::IntoResponse, routing::get, Router},
axum_server::Handle,
bdk::{
blockchain::rpc::{Auth, RpcBlockchain, RpcConfig},
blockchain::rpc::{RpcBlockchain, RpcConfig},
blockchain::{Blockchain, ConfigurableBlockchain},
database::SqliteDatabase,
keys::bip39::{Language, Mnemonic},
Expand Down
2 changes: 1 addition & 1 deletion src/purse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, bdk::blockchain::rpc::Auth};

#[derive(Debug)]
pub(crate) struct Purse {
Expand Down
7 changes: 6 additions & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ impl Subcommand {
Self::List(list) => list.run(options),
Self::Parse(parse) => parse.run(),
Self::Range(range) => range.run(),
Self::Server(server) => server.run(options),
Self::Server(server) => {
let index = Arc::new(Index::open(&options)?);
let handle = Handle::new();
LISTENERS.lock().unwrap().push(handle.clone());
server.run(options, index, handle)
}
Self::Supply => supply::run(),
Self::Traits(traits) => traits.run(),
Self::Wallet(wallet) => wallet.run(options),
Expand Down
188 changes: 173 additions & 15 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use {
},
serde::{de, Deserializer},
std::cmp::Ordering,
std::str,
tokio_stream::StreamExt,
};

Expand Down Expand Up @@ -89,10 +90,8 @@ pub(crate) struct Server {
}

impl Server {
pub(crate) fn run(self, options: Options) -> Result {
pub(crate) fn run(self, options: Options, index: Arc<Index>, handle: Handle) -> Result {
Runtime::new()?.block_on(async {
let index = Arc::new(Index::open(&options)?);

let clone = index.clone();
thread::spawn(move || loop {
if let Err(error) = clone.index() {
Expand Down Expand Up @@ -125,10 +124,6 @@ impl Server {
.allow_origin(Any),
);

let handle = Handle::new();

LISTENERS.lock().unwrap().push(handle.clone());

let (http_result, https_result) = tokio::join!(
self.spawn(&router, &handle, None)?,
self.spawn(&router, &handle, self.acceptor(&options)?)?
Expand Down Expand Up @@ -494,16 +489,17 @@ mod tests {
use {super::*, std::net::TcpListener, tempfile::TempDir};

struct TestServer {
#[allow(unused)]
bitcoin_rpc_server_handle: BitcoinRpcServerHandle,
bitcoin_rpc_server: BitcoinRpcServerHandle,
index: Arc<Index>,
ord_server_handle: Handle,
port: u16,
#[allow(unused)]
tempdir: TempDir,
port: u16,
}

impl TestServer {
fn new() -> Self {
let bitcoin_rpc_server_handle = BitcoinRpcServer::spawn();
let bitcoin_rpc_server = BitcoinRpcServer::spawn();

let tempdir = TempDir::new().unwrap();

Expand All @@ -518,14 +514,21 @@ mod tests {
.port();

let (options, server) = parse_server_args(&format!(
"ord --chain regtest --rpc-url http://127.0.0.1:{} --cookie-file {} --data-dir {} server --http-port {} --address 127.0.0.1",
bitcoin_rpc_server_handle.port,
"ord --chain regtest --rpc-url {} --cookie-file {} --data-dir {} server --http-port {} --address 127.0.0.1",
bitcoin_rpc_server.url(),
cookiefile.to_str().unwrap(),
tempdir.path().to_str().unwrap(),
port,
));

thread::spawn(|| server.run(options).unwrap());
let index = Arc::new(Index::open(&options).unwrap());
let ord_server_handle = Handle::new();

{
let index = index.clone();
let ord_server_handle = ord_server_handle.clone();
thread::spawn(|| server.run(options, index, ord_server_handle).unwrap());
}

for i in 0.. {
match reqwest::blocking::get(&format!("http://127.0.0.1:{port}/status")) {
Expand All @@ -541,15 +544,40 @@ mod tests {
}

Self {
bitcoin_rpc_server_handle,
bitcoin_rpc_server,
index,
ord_server_handle,
port,
tempdir,
}
}

fn get(&self, url: &str) -> reqwest::blocking::Response {
self.index.index().unwrap();
reqwest::blocking::get(self.join_url(url)).unwrap()
}

fn join_url(&self, url: &str) -> String {
format!("http://127.0.0.1:{}/{url}", self.port)
}

fn assert_response(&self, path: &str, status: StatusCode, expected_response: &str) {
let response = self.get(path);
assert_eq!(response.status(), status);
assert_eq!(response.text().unwrap(), expected_response);
}

fn assert_response_regex(&self, path: &str, status: StatusCode, regex: &'static str) {
let response = self.get(path);
assert_eq!(response.status(), status);
assert_regex_match!(response.text().unwrap(), regex);
}
}

impl Drop for TestServer {
fn drop(&mut self) {
self.ord_server_handle.shutdown();
}
}

fn parse_server_args(args: &str) -> (Options, Server) {
Expand Down Expand Up @@ -794,4 +822,134 @@ mod tests {
"/ordinal/0"
);
}

#[test]
fn status() {
TestServer::new().assert_response("status", StatusCode::OK, "OK");
}

#[test]
fn height_endpoint() {
TestServer::new().assert_response("height", StatusCode::OK, "0");
}

#[test]
fn height_updates() {
let test_server = TestServer::new();

let response = test_server.get("height");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.text().unwrap(), "0");

test_server.bitcoin_rpc_server.mine_block();

let response = test_server.get("height");

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.text().unwrap(), "1");
}

#[test]
fn range_end_before_range_start_returns_400() {
TestServer::new().assert_response(
"range/1/0/",
StatusCode::BAD_REQUEST,
"Range Start Greater Than Range End",
);
}

#[test]
fn invalid_range_start_returns_400() {
TestServer::new().assert_response(
"range/=/0",
StatusCode::BAD_REQUEST,
"Invalid URL: invalid digit found in string",
);
}

#[test]
fn invalid_range_end_returns_400() {
TestServer::new().assert_response(
"range/0/=",
StatusCode::BAD_REQUEST,
"Invalid URL: invalid digit found in string",
);
}

#[test]
fn empty_range_returns_400() {
TestServer::new().assert_response("range/0/0", StatusCode::BAD_REQUEST, "Empty Range");
}

#[test]
fn range() {
TestServer::new().assert_response_regex(
"range/0/1",
StatusCode::OK,
r".*<title>Ordinal range \[0,1\)</title>.*<h1>Ordinal range \[0,1\)</h1>
<dl>
<dt>size</dt><dd>1</dd>
<dt>first</dt><dd><a href=/ordinal/0 class=mythic>0</a></dd>
</dl>.*",
);
}
#[test]
fn ordinal_number() {
TestServer::new().assert_response_regex("ordinal/0", StatusCode::OK, ".*<h1>Ordinal 0</h1>.*");
}

#[test]
fn ordinal_decimal() {
TestServer::new().assert_response_regex(
"ordinal/0.0",
StatusCode::OK,
".*<h1>Ordinal 0</h1>.*",
);
}

#[test]
fn ordinal_degree() {
TestServer::new().assert_response_regex(
"ordinal/0°0′0″0‴",
StatusCode::OK,
".*<h1>Ordinal 0</h1>.*",
);
}

#[test]
fn ordinal_name() {
TestServer::new().assert_response_regex(
"ordinal/nvtdijuwxlp",
StatusCode::OK,
".*<h1>Ordinal 0</h1>.*",
);
}

#[test]
fn ordinal() {
TestServer::new().assert_response_regex(
"ordinal/0",
StatusCode::OK,
".*<title>0°0′0″0‴</title>.*<h1>Ordinal 0</h1>.*",
);
}

#[test]
fn ordinal_out_of_range() {
TestServer::new().assert_response(
"ordinal/2099999997690000",
StatusCode::BAD_REQUEST,
"Invalid URL: Invalid ordinal",
);
}

#[test]
fn invalid_outpoint_hash_returns_400() {
TestServer::new().assert_response(
"output/foo:0",
StatusCode::BAD_REQUEST,
"Invalid URL: error parsing TXID: odd hex string length 3",
);
}
}
Loading