Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further dockerization support #12

Merged
merged 2 commits into from
Dec 27, 2023
Merged
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: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ RUN rustup component add cargo
# TODO: Hacky but it works
RUN mkdir -p ./src
RUN mkdir -p ./crates/postgres-docker-utils/src
RUN mkdir -p ./crates/fake-rpc/src

# Copy only Cargo.toml for better caching
COPY ./Cargo.toml ./Cargo.toml
COPY ./Cargo.lock ./Cargo.lock
COPY ./crates/postgres-docker-utils/Cargo.toml ./crates/postgres-docker-utils/Cargo.toml
COPY ./crates/fake-rpc/Cargo.toml ./crates/fake-rpc/Cargo.toml

RUN echo "fn main() {}" > ./src/main.rs
RUN echo "fn main() {}" > ./crates/postgres-docker-utils/src/main.rs
RUN echo "fn main() {}" > ./crates/fake-rpc/src/main.rs

# Prebuild dependencies
RUN cargo fetch
Expand Down
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ws_url = "ws://127.0.0.1:8545"
id = "1b908a34-5dc1-4d2d-a146-5eb46e975830"
chain_id = 31337
key_id = "d10607662a85424f02a33fb1e6d095bd0ac7154396ff09762e41f82ff2233aaa"
api_key = "G5CKNF3BTS2hRl60bpdYMNPqXvXsP-QZd2lrtmgctsnllwU9D3Z4D8gOt04M0QNH"

[server]
host = "127.0.0.1:3000"
Expand Down
11 changes: 10 additions & 1 deletion src/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ pub struct ApiKey {
}

impl ApiKey {
pub fn new(relayer_id: impl ToString) -> Self {
pub fn new(relayer_id: impl ToString, key: [u8; 32]) -> Self {
let relayer_id = relayer_id.to_string();

Self {
relayer_id,
api_key: key,
}
}

pub fn random(relayer_id: impl ToString) -> Self {
let relayer_id = relayer_id.to_string();

let mut api_key = [0u8; 32];
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::api_key::ApiKey;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Config {
Expand Down Expand Up @@ -65,6 +67,7 @@ pub struct PredefinedRelayer {
pub name: String,
pub key_id: String,
pub chain_id: u64,
pub api_key: ApiKey,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ impl Database {
Ok(items.into_iter().map(|(x,)| x as u64).collect())
}

pub async fn save_api_key(
pub async fn create_api_key(
&self,
relayer_id: &str,
api_key_hash: [u8; 32],
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ pub async fn create_relayer_api_key(
State(app): State<Arc<App>>,
Path(relayer_id): Path<String>,
) -> Result<Json<CreateApiKeyResponse>, ApiError> {
let api_key = ApiKey::new(&relayer_id);
let api_key = ApiKey::random(&relayer_id);

app.db
.save_api_key(&relayer_id, api_key.api_key_hash())
.create_api_key(&relayer_id, api_key.api_key_hash())
.await?;

Ok(Json(CreateApiKeyResponse { api_key }))
Expand Down
9 changes: 9 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ async fn initialize_predefined_values(
return Ok(());
};

tracing::warn!("Running with predefined values is not recommended in a production environment");

app.db
.create_network(
predefined.network.chain_id,
Expand Down Expand Up @@ -121,5 +123,12 @@ async fn initialize_predefined_values(
)
.await?;

app.db
.create_api_key(
&predefined.relayer.api_key.relayer_id,
predefined.relayer.api_key.api_key_hash(),
)
.await?;

Ok(())
}
3 changes: 3 additions & 0 deletions tests/common/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::time::Duration;

use ethers::utils::AnvilInstance;
use tx_sitter::api_key::ApiKey;
use tx_sitter::client::TxSitterClient;
use tx_sitter::config::{
Config, DatabaseConfig, KeysConfig, LocalKeysConfig, Predefined,
Expand Down Expand Up @@ -71,6 +72,8 @@ impl ServiceBuilder {
id: DEFAULT_RELAYER_ID.to_string(),
key_id: anvil_private_key,
chain_id: DEFAULT_ANVIL_CHAIN_ID,
// TODO: Use this key in tests
api_key: ApiKey::random(DEFAULT_RELAYER_ID),
},
}),
},
Expand Down