Skip to content

Commit 3ba64bd

Browse files
committed
fixed panicing env checks, added debug logs
1 parent 2e720c1 commit 3ba64bd

File tree

14 files changed

+54
-61
lines changed

14 files changed

+54
-61
lines changed

Cargo.lock

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dkn-oracle"
33
description = "Dria Knowledge Network: Oracle Node"
4-
version = "0.1.4"
4+
version = "0.1.5"
55
edition = "2021"
66
license = "Apache-2.0"
77
readme = "README.md"
@@ -24,7 +24,6 @@ lazy_static = "1.5.0"
2424
ollama-workflows = { git = "https://github.com/andthattoo/ollama-workflows" }
2525

2626
# errors & logging & env
27-
color-eyre = { version = "0.6.3", default-features = false }
2827
env_logger = "0.11.5"
2928
eyre = "0.6.12"
3029
log = "0.4.22"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The CLI provides several methods to interact with the oracle contracts.
4949

5050
### Registration
5151

52-
To serve oracle requests, you must first register as your desired oracle type, i.e. `generator` or `validator`. These are handled by the registration commands `register` and `unregister` which accepts multiple arguments to register at once. You can then see your registrations with `registrations` command.
52+
To serve oracle requests, you **MUST** first register as your desired oracle type, i.e. `generator` or `validator`. These are handled by the registration commands `register` and `unregister` which accepts multiple arguments to register at once. You can then see your registrations with `registrations` command.
5353

5454
Here is an example:
5555

src/commands/token.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub async fn display_rewards(node: &DriaOracle) -> Result<()> {
2222

2323
log::info!("Claimable rewards:");
2424
log::info!("{} ", allowance);
25-
2625
if allowance.amount.is_zero() {
2726
log::warn!("You have no claimable rewards!");
2827
}

src/compute/handlers/generation.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,45 @@ pub async fn handle_generation(
2323
task_id: U256,
2424
protocol: FixedBytes<32>,
2525
) -> Result<Option<TransactionReceipt>> {
26+
log::info!("Handling generation task {}", task_id);
27+
28+
// check if we have validated anyways
29+
log::debug!("Checking existing generation esponses");
2630
let responses = node.get_task_responses(task_id).await?;
2731
if responses.iter().any(|r| r.responder == node.address()) {
2832
log::debug!("Already responded to {} with generation", task_id);
2933
return Ok(None);
3034
}
3135

36+
// fetch the request from contract
37+
log::debug!("Fetching the task request");
3238
let request = node
3339
.get_task_request(task_id)
3440
.await
3541
.wrap_err("could not get task")?;
3642

3743
// choose model based on the request
44+
log::debug!("Choosing model to use");
3845
let models_string = bytes_to_string(&request.models)?;
3946
let (_, model) = models.get_any_matching_model_from_csv(&models_string)?;
4047
log::debug!("Using model: {} from {}", model, models_string);
4148

4249
// execute task
50+
log::debug!("Executing the workflow");
4351
let protocol_string = bytes32_to_string(&protocol)?;
4452
let executor = Executor::new(model);
4553
let (output, metadata) = executor
4654
.execute_raw(&request.input, &protocol_string)
4755
.await?;
4856

4957
// do the Arweave trick for large inputs
50-
let arweave = Arweave::new_from_env()?;
58+
log::debug!("Uploading to Arweave if required");
59+
let arweave = Arweave::new_from_env().wrap_err("could not create Arweave instance")?;
5160
let output = arweave.put_if_large(output).await?;
5261
let metadata = arweave.put_if_large(metadata).await?;
5362

5463
// mine nonce
64+
log::debug!("Mining nonce for task");
5565
let nonce = mine_nonce(
5666
request.parameters.difficulty,
5767
&request.requester,
@@ -62,8 +72,9 @@ pub async fn handle_generation(
6272
.nonce;
6373

6474
// respond
65-
let tx_hash = node
75+
log::debug!("Responding with generation");
76+
let tx_receipt = node
6677
.respond_generation(task_id, output, metadata, nonce)
6778
.await?;
68-
Ok(Some(tx_hash))
79+
Ok(Some(tx_receipt))
6980
}

src/compute/handlers/validation.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloy::{
44
primitives::{utils::parse_ether, Bytes, U256},
55
rpc::types::TransactionReceipt,
66
};
7-
use eyre::{eyre, Result};
7+
use eyre::{eyre, Context, Result};
88

99
/// Handles a validation request.
1010
#[allow(unused)]
@@ -13,7 +13,10 @@ pub async fn handle_validation(
1313
models: &ModelConfig,
1414
task_id: U256,
1515
) -> Result<Option<TransactionReceipt>> {
16+
log::info!("Handling validation task {}", task_id);
17+
1618
// check if already responded as generator, because we cant validate our own answer
19+
log::debug!("Checking if we are a generator for this task");
1720
let responses = node.get_task_responses(task_id).await?;
1821
if responses.iter().any(|r| r.responder == node.address()) {
1922
log::debug!(
@@ -24,23 +27,29 @@ pub async fn handle_validation(
2427
}
2528

2629
// check if we have validated anyways
30+
log::debug!("Checking if we have validated already");
2731
let validations = node.get_task_validations(task_id).await?;
2832
if validations.iter().any(|v| v.validator == node.address()) {
2933
return Err(eyre!("Already validated {}", task_id));
3034
}
3135

32-
let request = node.get_task_request(task_id).await?;
36+
log::debug!("Fetching the task request");
37+
let request = node
38+
.get_task_request(task_id)
39+
.await
40+
.wrap_err("could not get task")?;
3341

3442
// FIXME: will add validation prompt here
43+
log::debug!("Validating the task");
3544
let scores = (0..request.parameters.numGenerations)
3645
.map(|_| parse_ether("1.0").unwrap())
3746
.collect::<Vec<_>>();
38-
3947
// FIXME: metadata is empty for now, as dummy data
4048
// FIXME: can add Arweave trick for metadata here
4149
let metadata = Bytes::default();
4250

4351
// mine nonce
52+
log::debug!("Mining nonce for task");
4453
let nonce = mine_nonce(
4554
request.parameters.difficulty,
4655
&request.requester,
@@ -50,8 +59,10 @@ pub async fn handle_validation(
5059
)
5160
.nonce;
5261

53-
let tx_hash = node
62+
// respond
63+
log::debug!("Responding with validation");
64+
let tx_receipt = node
5465
.respond_validation(task_id, scores, metadata, nonce)
5566
.await?;
56-
Ok(Some(tx_hash))
67+
Ok(Some(tx_receipt))
5768
}

src/compute/nonce.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub fn mine_nonce(
2323
// target is (2^256-1) / 2^difficulty
2424
let target = U256::MAX >> difficulty;
2525

26-
log::debug!("Mining nonce for task {}", task_id);
2726
loop {
2827
// encode packed
2928
let mut message = Vec::new();

src/configurations/mod.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use alloy::{
33
transports::http::reqwest::Url,
44
};
55

6-
use color_eyre::Section;
76
use eyre::{Context, Result};
87
use std::env;
98

@@ -18,10 +17,7 @@ pub struct DriaOracleConfig {
1817

1918
impl Default for DriaOracleConfig {
2019
fn default() -> Self {
21-
Self::new_from_env()
22-
.and_then(Self::enable_color_eyre)
23-
.unwrap()
24-
.enable_logs()
20+
Self::new_from_env().unwrap().enable_logs()
2521
}
2622
}
2723

@@ -40,14 +36,10 @@ impl DriaOracleConfig {
4036
/// - `SECRET_KEY`
4137
/// - `RPC_URL`
4238
pub fn new_from_env() -> Result<Self> {
43-
dotenvy::dotenv()?;
4439
// parse private key
45-
let private_key_hex = env::var("SECRET_KEY")
46-
.wrap_err("SECRET_KEY is not set")
47-
.suggestion("SECRET_KEY must be within .env.")?;
48-
let secret_key = B256::from_hex(private_key_hex)
49-
.wrap_err("could not hex-decode secret key")
50-
.suggestion("SECRET_KEY must be within .env and be hexadecimals.")?;
40+
let private_key_hex = env::var("SECRET_KEY").wrap_err("SECRET_KEY is not set")?;
41+
let secret_key =
42+
B256::from_hex(private_key_hex).wrap_err("could not hex-decode secret key")?;
5143

5244
// parse rpc url
5345
let rpc_url_env = env::var("RPC_URL").wrap_err("RPC_URL is not set")?;
@@ -89,14 +81,6 @@ impl DriaOracleConfig {
8981
self
9082
}
9183

92-
/// Enables colored `eyre` error reports.
93-
pub fn enable_color_eyre(self) -> Result<Self> {
94-
if let Err(e) = color_eyre::install() {
95-
log::error!("Error during color_eyre::install: {}", e);
96-
}
97-
Ok(self)
98-
}
99-
10084
/// Change the signer with a new one with the given secret key.
10185
pub fn with_secret_key(&mut self, secret_key: &B256) -> Result<&mut Self> {
10286
let signer =

src/data/arweave.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct Arweave {
5050
/// - https://gateway.irys.xyz
5151
/// - https://node1.bundlr.network
5252
base_url: Url,
53-
/// Reqwest client
53+
/// Reqwest client.
5454
client: Client,
5555
/// Byte limit for the data to be considered for Arweave.
5656
///
@@ -64,7 +64,7 @@ impl Arweave {
6464
pub fn new(base_url: &str, wallet: &str, byte_limit: usize) -> Result<Self> {
6565
Ok(Self {
6666
wallet: PathBuf::from(wallet),
67-
base_url: Url::parse(base_url)?,
67+
base_url: Url::parse(base_url).wrap_err("could not parse base URL")?,
6868
client: Client::new(),
6969
byte_limit,
7070
})
@@ -73,18 +73,19 @@ impl Arweave {
7373
/// Creates a new Arweave instance from the environment variables.
7474
///
7575
/// Required environment variables:
76+
///
7677
/// - `ARWEAVE_WALLET_PATH`
7778
/// - `ARWEAVE_BASE_URL`
7879
/// - `ARWEAVE_BYTE_LIMIT`
7980
///
8081
/// All these variables have defaults if they are missing.
8182
pub fn new_from_env() -> Result<Self> {
82-
dotenvy::dotenv()?;
8383
let wallet = env::var("ARWEAVE_WALLET_PATH").unwrap_or(DEFAULT_WALLET_PATH.to_string());
8484
let base_url = env::var("ARWEAVE_BASE_URL").unwrap_or(DEFAULT_BASE_URL.to_string());
8585
let byte_limit = env::var("ARWEAVE_BYTE_LIMIT")
8686
.unwrap_or(DEFAULT_BYTE_LIMIT.to_string())
87-
.parse::<usize>()?;
87+
.parse::<usize>()
88+
.wrap_err("could not parse ARWEAVE_BYTE_LIMIT")?;
8889

8990
Self::new(&base_url, &wallet, byte_limit)
9091
}
@@ -95,7 +96,9 @@ impl Arweave {
9596
/// we want to use hexadecimals to read them easily on-chain.
9697
#[inline(always)]
9798
pub fn base64_to_hex(key: &str) -> Result<String> {
98-
let decoded_key = BASE64_URL_SAFE_NO_PAD.decode(key.as_bytes())?;
99+
let decoded_key = BASE64_URL_SAFE_NO_PAD
100+
.decode(key.as_bytes())
101+
.wrap_err("could not decode base64 url")?;
99102
Ok(hex::encode(decoded_key))
100103
}
101104

@@ -105,7 +108,7 @@ impl Arweave {
105108
/// we want to use hexadecimals to read them easily on-chain.
106109
#[inline(always)]
107110
pub fn hex_to_base64(key: &str) -> Result<String> {
108-
let decoded_key = hex::decode(key)?;
111+
let decoded_key = hex::decode(key).wrap_err("could not decode hexadecimals")?;
109112
Ok(BASE64_URL_SAFE_NO_PAD.encode(&decoded_key))
110113
}
111114

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use eyre::{Context, Result};
44
async fn main() -> Result<()> {
55
let dotenv_result = dotenvy::dotenv();
66
env_logger::try_init().wrap_err("could not initialize env_logger")?;
7-
color_eyre::install().wrap_err("could not install color_eyre")?;
87
if let Err(err) = dotenv_result {
98
log::warn!("Could not load .env file: {}", err);
109
}

tests/oracle_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use ollama_workflows::Model;
88

99
#[tokio::test]
1010
async fn test_oracle() -> Result<()> {
11+
dotenvy::dotenv().unwrap();
12+
1113
// task setup
1214
let difficulty = 1;
1315
let models = string_to_bytes(Model::GPT4Turbo.to_string());

tests/request_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use ollama_workflows::Model;
1111

1212
#[tokio::test]
1313
async fn test_request() -> Result<()> {
14+
dotenvy::dotenv().unwrap();
15+
1416
// task setup
1517
let difficulty = 1;
1618
let num_gens = 1;

tests/swan_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use ollama_workflows::Model;
1414

1515
#[tokio::test]
1616
async fn test_swan() -> Result<()> {
17+
dotenvy::dotenv().unwrap();
18+
1719
// task setup
1820
let difficulty = 1;
1921
let models = string_to_bytes(Model::GPT4Turbo.to_string());

tests/weth_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use eyre::Result;
1010

1111
#[tokio::test]
1212
async fn test_weth_transfer() -> Result<()> {
13+
dotenvy::dotenv().unwrap();
14+
1315
// amount of WETH that will be transferred
1416
let amount = parse_ether("100")?;
1517

0 commit comments

Comments
 (0)