Skip to content

Commit b8ff6cc

Browse files
authored
Improve configuration (ordinals#3156)
1 parent a1f3f52 commit b8ff6cc

38 files changed

+1799
-1325
lines changed

.github/workflows/ci.yaml

-20
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,3 @@ jobs:
116116

117117
- name: Test
118118
run: cargo test --all
119-
120-
core:
121-
runs-on: ubuntu-latest
122-
123-
steps:
124-
- uses: actions/checkout@v2
125-
126-
- name: Install Rust Toolchain Components
127-
uses: actions-rs/toolchain@v1
128-
with:
129-
profile: minimal
130-
toolchain: stable
131-
132-
- uses: Swatinem/rust-cache@v2
133-
134-
- name: Install Bitcoin Core
135-
run: ./bin/install-bitcoin-core-linux
136-
137-
- name: Test
138-
run: cargo test --all -- --ignored

crates/test-bitcoincore-rpc/src/state.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22

3+
#[derive(Debug)]
34
pub(crate) struct State {
45
pub(crate) blocks: BTreeMap<BlockHash, Block>,
56
pub(crate) change_addresses: Vec<Address>,

docs/src/SUMMARY.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ Summary
1818
- [Explorer](guides/explorer.md)
1919
- [Wallet](guides/wallet.md)
2020
- [Batch Inscribing](guides/batch-inscribing.md)
21-
- [Sat Hunting](guides/sat-hunting.md)
22-
- [Teleburning](guides/teleburning.md)
2321
- [Collecting](guides/collecting.md)
2422
- [Sparrow Wallet](guides/collecting/sparrow-wallet.md)
25-
- [Testing](guides/testing.md)
2623
- [Moderation](guides/moderation.md)
2724
- [Reindexing](guides/reindexing.md)
25+
- [Sat Hunting](guides/sat-hunting.md)
26+
- [Settings](guides/settings.md)
27+
- [Teleburning](guides/teleburning.md)
28+
- [Testing](guides/testing.md)
2829
- [Bounties](bounties.md)
2930
- [Bounty 0: 100,000 sats Claimed!](bounty/0.md)
3031
- [Bounty 1: 200,000 sats Claimed!](bounty/1.md)

docs/src/guides/settings.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Settings
2+
========
3+
4+
`ord` can be configured with command line options, environment variables, a
5+
configuration file, and default values.
6+
7+
When multiple sources configure the same thing, precedence is in order of
8+
command line options, then environment variables, then the configuration file,
9+
and finally default values.
10+
11+
The path to the configuration can be given with `--config <CONFIG_PATH>`. `ord`
12+
will error if `<CONFIG_PATH>` doesn't exist. The path to a configuration
13+
directory can be given with `--config-dir <CONFIG_DIR_PATH>`, in which case the
14+
config path is `<CONFIG_DIR_PATH>/ord.yaml`. It is not an error if
15+
`<CONFIG_DIR_PATH>/ord.yaml` does not exist, and `ord` will use a configuration
16+
with default values.
17+
18+
All settings can be configured with command line options, but not all settings
19+
can yet be configured with environmnet variables or a configuration file.
20+
21+
`ord`'s configuration can be viewd as JSON with `ord settings`.
22+
23+
| setting | CLI | environment variable | default value |
24+
| --- | --- | --- | --- |
25+
| bitcoin RPC password | `--bitcoin-rpc-pass <PASSWORD>` | `ORD_BITCOIN_RPC_PASS` | none |
26+
| bitcoin RPC username | `--bitcoin-rpc-user <USERNAME>` | `ORD_BITCOIN_RPC_USER` | none |
27+
| chain | `--chain <CHAIN>` | `ORD_CHAIN` | mainnet |

src/arguments.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ pub(crate) struct Arguments {
2121

2222
impl Arguments {
2323
pub(crate) fn run(self) -> SubcommandResult {
24-
self.subcommand.run(self.options)
24+
let mut env: BTreeMap<String, String> = BTreeMap::new();
25+
26+
for (var, value) in env::vars_os() {
27+
let Some(var) = var.to_str() else {
28+
continue;
29+
};
30+
31+
let Some(key) = var.strip_prefix("ORD_") else {
32+
continue;
33+
};
34+
35+
env.insert(
36+
key.into(),
37+
value.into_string().map_err(|value| {
38+
anyhow!(
39+
"environment variable `{var}` not valid unicode: `{}`",
40+
value.to_string_lossy()
41+
)
42+
})?,
43+
);
44+
}
45+
46+
let config = self.options.config()?;
47+
48+
self
49+
.subcommand
50+
.run(Settings::new(self.options, env, config)?)
2551
}
2652
}

src/chain.rs

+31
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,34 @@ impl Display for Chain {
108108
)
109109
}
110110
}
111+
112+
impl FromStr for Chain {
113+
type Err = Error;
114+
115+
fn from_str(s: &str) -> Result<Self, Self::Err> {
116+
match s {
117+
"mainnet" => Ok(Self::Mainnet),
118+
"regtest" => Ok(Self::Regtest),
119+
"signet" => Ok(Self::Signet),
120+
"testnet" => Ok(Self::Testnet),
121+
_ => bail!("invalid chain `{s}`"),
122+
}
123+
}
124+
}
125+
126+
#[cfg(test)]
127+
mod tests {
128+
use super::*;
129+
130+
#[test]
131+
fn from_str() {
132+
assert_eq!("mainnet".parse::<Chain>().unwrap(), Chain::Mainnet);
133+
assert_eq!("regtest".parse::<Chain>().unwrap(), Chain::Regtest);
134+
assert_eq!("signet".parse::<Chain>().unwrap(), Chain::Signet);
135+
assert_eq!("testnet".parse::<Chain>().unwrap(), Chain::Testnet);
136+
assert_eq!(
137+
"foo".parse::<Chain>().unwrap_err().to_string(),
138+
"invalid chain `foo`"
139+
);
140+
}
141+
}

src/config.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
11
use super::*;
22

3-
#[derive(Deserialize, Default, PartialEq, Debug)]
3+
#[derive(Deserialize, Default, PartialEq, Debug, Clone)]
44
#[serde(deny_unknown_fields)]
55
pub(crate) struct Config {
6-
pub(crate) hidden: HashSet<InscriptionId>,
76
pub(crate) bitcoin_rpc_pass: Option<String>,
87
pub(crate) bitcoin_rpc_user: Option<String>,
9-
}
10-
11-
impl Config {
12-
pub(crate) fn is_hidden(&self, inscription_id: InscriptionId) -> bool {
13-
self.hidden.contains(&inscription_id)
14-
}
8+
pub(crate) chain: Option<Chain>,
9+
pub(crate) hidden: Option<HashSet<InscriptionId>>,
1510
}
1611

1712
#[cfg(test)]
1813
mod tests {
1914
use super::*;
2015

21-
#[test]
22-
fn inscriptions_can_be_hidden() {
23-
let a = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi0"
24-
.parse::<InscriptionId>()
25-
.unwrap();
26-
27-
let b = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi1"
28-
.parse::<InscriptionId>()
29-
.unwrap();
30-
31-
let config = Config {
32-
hidden: iter::once(a).collect(),
33-
..Default::default()
34-
};
35-
36-
assert!(config.is_hidden(a));
37-
assert!(!config.is_hidden(b));
38-
}
39-
4016
#[test]
4117
fn example_config_file_is_valid() {
4218
let _: Config = serde_yaml::from_reader(File::open("ord.yaml").unwrap()).unwrap();

0 commit comments

Comments
 (0)