Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PoCW Economics
POCW_ENABLED=false
POCW_TRANSFER_FEE=21000
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Thumbs.db
**/*.rs.bk
Cargo.lock

# Environment
.env

# Logs
*.log
logs/
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ cargo test --all
#### 1. Start Validator

```bash
# Set environment variables
# Using an env file (auto-loads .env from working directory)
./target/release/setu-validator

# Or specify a custom env file
./target/release/setu-validator --env-file config/prod.env

# Or set environment variables directly
export VALIDATOR_ID=validator-1
export VALIDATOR_HTTP_PORT=8080
export VALIDATOR_P2P_PORT=9000
export VALIDATOR_DB_PATH=/tmp/setu/validator

# Start validator
./target/release/setu-validator
```

Expand Down
5 changes: 4 additions & 1 deletion setu-validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ Validator node for the Setu network. Responsible for verifying events, maintaini
# Start validator with default config
cargo run -p setu-validator

# With environment variables
# With an env file (auto-loads .env from working directory, or use --env-file)
cargo run -p setu-validator -- --env-file .env

# With inline environment variables
VALIDATOR_ID=validator_1 \
VALIDATOR_PORT=8001 \
IS_LEADER=true \
Expand Down
43 changes: 43 additions & 0 deletions setu-validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,51 @@ fn load_key_info(key_file: &str) -> anyhow::Result<(String, Vec<u8>, Vec<u8>)> {
Ok((account_address, public_key, signature))
}

/// Load environment variables from a dotenv file.
/// Priority: `--env-file <path>` flag, then `.env` in the working directory.
/// Each line should be KEY=VALUE (comments and blank lines are skipped).
fn load_env_file() {
let args: Vec<String> = std::env::args().collect();
let explicit = args.windows(2)
.find(|w| w[0] == "--env-file")
.map(|w| w[1].clone());

let path = match explicit {
Some(p) => p,
None => {
// Auto-load .env from working directory if it exists
if std::path::Path::new(".env").exists() {
".env".to_string()
} else {
return;
}
}
};

let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
eprintln!("Failed to read env file {}: {}", path, e);
std::process::exit(1);
}
};

for line in content.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some((key, value)) = line.split_once('=') {
std::env::set_var(key.trim(), value.trim());
}
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Load .env automatically, or from --env-file <path>
load_env_file();

// Initialize tracing with more detailed output
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
Expand Down