Skip to content

Commit

Permalink
feat: add difficulty as configurable env var
Browse files Browse the repository at this point in the history
  • Loading branch information
DefiCake committed Dec 1, 2023
1 parent 30b53b3 commit 2b0876c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
Faucet App
===
# Faucet App

[![build](https://github.com/FuelLabs/faucet/actions/workflows/ci.yml/badge.svg)](https://github.com/FuelLabs/faucet/actions/workflows/ci.yml)
[![discord](https://img.shields.io/badge/chat%20on-discord-orange?&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/xfpK4Pe)

A simple faucet app for dispensing tokens on a fuel network. It uses Google captcha for spam resistance
without requiring any social media based identification.

## Configuration

The faucet makes use of environment variables for configuration.

| Environment Variable | Description |
|----------------------|-------------------------------------------------------------------------|
| RUST_LOG | EnvFilter configuration for adjusting logging granularity. |
| HUMAN_LOGGING | If false, logs will be output as machine readable JSON. |
| CAPTCHA_SECRET | The secret key used for enabling Google captcha authentication. |
| CAPTCHA_KEY | The website key used for enabling Google captcha authentication. |
| WALLET_SECRET_KEY | A hex formatted string of the wallet private key that owns some tokens. |
| FUEL_NODE_URL | The GraphQL endpoint for connecting to fuel-core. |
| Environment Variable | Description |
| -------------------- | ----------------------------------------------------------------------------------------------- |
| RUST_LOG | EnvFilter configuration for adjusting logging granularity. |
| HUMAN_LOGGING | If false, logs will be output as machine readable JSON. |
| CAPTCHA_SECRET | The secret key used for enabling Google captcha authentication. |
| CAPTCHA_KEY | The website key used for enabling Google captcha authentication. |
| WALLET_SECRET_KEY | A hex formatted string of the wallet private key that owns some tokens. |
| FUEL_NODE_URL | The GraphQL endpoint for connecting to fuel-core. |
| PUBLIC_FUEL_NODE_URL | The public GraphQL endpoint for connecting to fuel-core. Ex.: https://node.fuel.network/graphql |
| SERVICE_PORT | The port the service will listen for http connections on. |
| DISPENSE_AMOUNT | Dispense amount on each faucet |
| MIN_GAS_PRICE | The minimum gas price to use in each transfer |
| SERVICE_PORT | The port the service will listen for http connections on. |
| DISPENSE_AMOUNT | Dispense amount on each faucet |
| MIN_GAS_PRICE | The minimum gas price to use in each transfer |
| POW_DIFFICULTY | Number of leading zeroes that a valid proof of work hash must have |

## Build and Run

Expand Down
7 changes: 6 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::constants::{
CAPTCHA_KEY, CAPTCHA_SECRET, DEFAULT_FAUCET_DISPENSE_AMOUNT, DEFAULT_NODE_URL, DEFAULT_PORT,
DISPENSE_AMOUNT, FAUCET_ASSET_ID, FUEL_NODE_URL, HUMAN_LOGGING, LOG_FILTER, MIN_GAS_PRICE,
PUBLIC_FUEL_NODE_URL, SERVICE_PORT, TIMEOUT_SECONDS, WALLET_SECRET_KEY,
PUBLIC_FUEL_NODE_URL, SERVICE_PORT, TIMEOUT_SECONDS, WALLET_SECRET_KEY, POW_DIFFICULTY,
};
use fuels_core::types::AssetId;
use secrecy::Secret;
Expand All @@ -21,6 +21,7 @@ pub struct Config {
pub dispense_asset_id: AssetId,
pub min_gas_price: u64,
pub timeout: u64,
pub pow_difficulty: u8,
}

impl Default for Config {
Expand Down Expand Up @@ -52,6 +53,10 @@ impl Default for Config {
.unwrap_or_else(|_| "30".to_string())
.parse::<u64>()
.expect("expected a valid integer for TIMEOUT_SECONDS"),
pow_difficulty: env::var(POW_DIFFICULTY)
.unwrap_or_else(|_| "6".to_owned())
.parse::<u8>()
.expect("expected a valid integer [0, 255] for POW_DIFFICULTY"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const DEFAULT_PORT: u16 = 3000;

pub const MIN_GAS_PRICE: &str = "MIN_GAS_PRICE";
pub const TIMEOUT_SECONDS: &str = "TIMEOUT_SECONDS";
pub const POW_DIFFICULTY: &str = "POW_DIFFICULTY";

// HTTP config

Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ pub async fn start_server(
.sum::<u64>();
info!("Faucet Account: {:#x}", Address::from(wallet.address()));
info!("Faucet Balance: {}", balance);


let pow_difficulty = service_config.pow_difficulty;
let sessions = Arc::new(Mutex::new(SessionMap::new()));


// setup routes
let app = Router::new()
Expand Down Expand Up @@ -204,6 +206,7 @@ pub async fn start_server(
.timeout(Duration::from_secs(60))
.layer(TraceLayer::new_for_http())
.layer(Extension(sessions.clone()))
.layer(Extension(Arc::new(pow_difficulty)))
.into_inner(),
);

Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct CreateSessionInput {
pub struct CreateSessionResponse {
pub status: String,
pub salt: String,
pub difficulty: u8,
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ impl IntoResponse for CreateSessionError {
pub async fn create_session(
Json(input): Json<CreateSessionInput>,
Extension(sessions): Extension<Arc<Mutex<SessionMap>>>,
Extension(pow_difficulty): Extension<Arc<u8>>,
) -> Result<CreateSessionResponse, CreateSessionError> {
// parse deposit address
let address = if let Ok(address) = Address::from_str(input.address.as_str()) {
Expand Down Expand Up @@ -356,6 +357,7 @@ pub async fn create_session(
Ok(CreateSessionResponse {
status: "Success".to_owned(),
salt: hex::encode(salt.as_bytes()),
difficulty: *pow_difficulty
})
}

Expand Down

0 comments on commit 2b0876c

Please sign in to comment.