Skip to content

Commit

Permalink
Merge branch 'master' into ord-env-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 28, 2024
2 parents b5535c3 + b8ff6cc commit 4a75ec4
Show file tree
Hide file tree
Showing 38 changed files with 1,799 additions and 1,325 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,3 @@ jobs:

- name: Test
run: cargo test --all

core:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install Rust Toolchain Components
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- uses: Swatinem/rust-cache@v2

- name: Install Bitcoin Core
run: ./bin/install-bitcoin-core-linux

- name: Test
run: cargo test --all -- --ignored
1 change: 1 addition & 0 deletions crates/test-bitcoincore-rpc/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;

#[derive(Debug)]
pub(crate) struct State {
pub(crate) blocks: BTreeMap<BlockHash, Block>,
pub(crate) change_addresses: Vec<Address>,
Expand Down
7 changes: 4 additions & 3 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Summary
- [Explorer](guides/explorer.md)
- [Wallet](guides/wallet.md)
- [Batch Inscribing](guides/batch-inscribing.md)
- [Sat Hunting](guides/sat-hunting.md)
- [Teleburning](guides/teleburning.md)
- [Collecting](guides/collecting.md)
- [Sparrow Wallet](guides/collecting/sparrow-wallet.md)
- [Testing](guides/testing.md)
- [Moderation](guides/moderation.md)
- [Reindexing](guides/reindexing.md)
- [Sat Hunting](guides/sat-hunting.md)
- [Settings](guides/settings.md)
- [Teleburning](guides/teleburning.md)
- [Testing](guides/testing.md)
- [Bounties](bounties.md)
- [Bounty 0: 100,000 sats Claimed!](bounty/0.md)
- [Bounty 1: 200,000 sats Claimed!](bounty/1.md)
Expand Down
27 changes: 27 additions & 0 deletions docs/src/guides/settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Settings
========

`ord` can be configured with command line options, environment variables, a
configuration file, and default values.

When multiple sources configure the same thing, precedence is in order of
command line options, then environment variables, then the configuration file,
and finally default values.

The path to the configuration can be given with `--config <CONFIG_PATH>`. `ord`
will error if `<CONFIG_PATH>` doesn't exist. The path to a configuration
directory can be given with `--config-dir <CONFIG_DIR_PATH>`, in which case the
config path is `<CONFIG_DIR_PATH>/ord.yaml`. It is not an error if
`<CONFIG_DIR_PATH>/ord.yaml` does not exist, and `ord` will use a configuration
with default values.

All settings can be configured with command line options, but not all settings
can yet be configured with environmnet variables or a configuration file.

`ord`'s configuration can be viewd as JSON with `ord settings`.

| setting | CLI | environment variable | default value |
| --- | --- | --- | --- |
| bitcoin RPC password | `--bitcoin-rpc-pass <PASSWORD>` | `ORD_BITCOIN_RPC_PASS` | none |
| bitcoin RPC username | `--bitcoin-rpc-user <USERNAME>` | `ORD_BITCOIN_RPC_USER` | none |
| chain | `--chain <CHAIN>` | `ORD_CHAIN` | mainnet |
28 changes: 27 additions & 1 deletion src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ pub(crate) struct Arguments {

impl Arguments {
pub(crate) fn run(self) -> SubcommandResult {
self.subcommand.run(self.options)
let mut env: BTreeMap<String, String> = BTreeMap::new();

for (var, value) in env::vars_os() {
let Some(var) = var.to_str() else {
continue;
};

let Some(key) = var.strip_prefix("ORD_") else {
continue;
};

env.insert(
key.into(),
value.into_string().map_err(|value| {
anyhow!(
"environment variable `{var}` not valid unicode: `{}`",
value.to_string_lossy()
)
})?,
);
}

let config = self.options.config()?;

self
.subcommand
.run(Settings::new(self.options, env, config)?)
}
}
31 changes: 31 additions & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,34 @@ impl Display for Chain {
)
}
}

impl FromStr for Chain {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mainnet" => Ok(Self::Mainnet),
"regtest" => Ok(Self::Regtest),
"signet" => Ok(Self::Signet),
"testnet" => Ok(Self::Testnet),
_ => bail!("invalid chain `{s}`"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from_str() {
assert_eq!("mainnet".parse::<Chain>().unwrap(), Chain::Mainnet);
assert_eq!("regtest".parse::<Chain>().unwrap(), Chain::Regtest);
assert_eq!("signet".parse::<Chain>().unwrap(), Chain::Signet);
assert_eq!("testnet".parse::<Chain>().unwrap(), Chain::Testnet);
assert_eq!(
"foo".parse::<Chain>().unwrap_err().to_string(),
"invalid chain `foo`"
);
}
}
30 changes: 3 additions & 27 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
use super::*;

#[derive(Deserialize, Default, PartialEq, Debug)]
#[derive(Deserialize, Default, PartialEq, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct Config {
pub(crate) hidden: HashSet<InscriptionId>,
pub(crate) bitcoin_rpc_pass: Option<String>,
pub(crate) bitcoin_rpc_user: Option<String>,
}

impl Config {
pub(crate) fn is_hidden(&self, inscription_id: InscriptionId) -> bool {
self.hidden.contains(&inscription_id)
}
pub(crate) chain: Option<Chain>,
pub(crate) hidden: Option<HashSet<InscriptionId>>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn inscriptions_can_be_hidden() {
let a = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi0"
.parse::<InscriptionId>()
.unwrap();

let b = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi1"
.parse::<InscriptionId>()
.unwrap();

let config = Config {
hidden: iter::once(a).collect(),
..Default::default()
};

assert!(config.is_hidden(a));
assert!(!config.is_hidden(b));
}

#[test]
fn example_config_file_is_valid() {
let _: Config = serde_yaml::from_reader(File::open("ord.yaml").unwrap()).unwrap();
Expand Down
Loading

0 comments on commit 4a75ec4

Please sign in to comment.