Skip to content

Commit

Permalink
feat(baliusd): load worker config from json file (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Oct 27, 2024
1 parent dc7c9a9 commit da1cffe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion balius-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Runtime {
&mut self,
id: &str,
wasm_path: impl AsRef<Path>,
config: Option<serde_json::Value>,
config: serde_json::Value,
) -> Result<(), Error> {
let component = wasmtime::component::Component::from_file(&self.engine, wasm_path)?;

Expand Down
2 changes: 1 addition & 1 deletion balius-runtime/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn faucet_claim() {
});

runtime
.register_worker("faucet", "tests/faucet.wasm", Some(config))
.register_worker("faucet", "tests/faucet.wasm", config)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion balius-runtime/tests/u5c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn faucet_claim() {
});

runtime
.register_worker("faucet", "tests/faucet.wasm", Some(config))
.register_worker("faucet", "tests/faucet.wasm", config)
.await
.unwrap();

Expand Down
6 changes: 1 addition & 5 deletions baliusd/example/baliusd.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ api_key = "dmtr_utxorpc1wgnnj0qcfj32zxsz2uc8d4g7uclm2s2w"
[[workers]]
name = "faucet"
module = "faucet.wasm"

[workers.config.validator]
ref_txo = { transaction_id = "f7d3837715680f3a170e99cd202b726842d97f82c05af8fcd18053c64e33ec4f", index = 0 }
hash = "ef7a1cebb2dc7de884ddf82f8fcbc91fe9750dcd8c12ec7643a99bbe"
address = "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x"
config = "faucet.json"
10 changes: 10 additions & 0 deletions baliusd/example/faucet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"validator": {
"ref_txo": {
"transaction_id": "f7d3837715680f3a170e99cd202b726842d97f82c05af8fcd18053c64e33ec4f",
"index": 0
},
"hash": "ef7a1cebb2dc7de884ddf82f8fcbc91fe9750dcd8c12ec7643a99bbe",
"address": "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x"
}
}
21 changes: 19 additions & 2 deletions baliusd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct WorkerConfig {
pub module: PathBuf,
pub since_slot: Option<u64>,
pub until_slot: Option<u64>,
pub config: Option<serde_json::Value>,
pub config: Option<PathBuf>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
Expand All @@ -36,6 +36,21 @@ pub struct Config {
pub logging: LoggingConfig,
}

fn load_worker_config(config_path: Option<PathBuf>) -> miette::Result<serde_json::Value> {
match config_path {
Some(path) => {
let config_str = std::fs::read_to_string(&path)
.into_diagnostic()
.context("reading worker config file")?;

serde_json::from_str::<serde_json::Value>(&config_str)
.into_diagnostic()
.context("parsing worker config as JSON")
}
None => Ok(serde_json::Value::Null),
}
}

#[tokio::main]
async fn main() -> miette::Result<()> {
let config: Config = boilerplate::load_config(&None)
Expand All @@ -60,8 +75,10 @@ async fn main() -> miette::Result<()> {
.context("setting up runtime")?;

for worker in config.workers {
let config = load_worker_config(worker.config)?;

runtime
.register_worker(&worker.name, worker.module, worker.config)
.register_worker(&worker.name, worker.module, config)
.await
.into_diagnostic()
.context("registering worker")?;
Expand Down

0 comments on commit da1cffe

Please sign in to comment.