Skip to content

Commit e7a7c41

Browse files
JuArceentropidelictaturosati
authored
chore: add LocalStack for local s3 (#647)
Co-authored-by: Mariano A. Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Santos Rosati <rosatisantos@gmail.com> Co-authored-by: Tatu <65305492+srosati@users.noreply.github.com>
1 parent f8ed41a commit e7a7c41

8 files changed

Lines changed: 72 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/**
1010
batcher/aligned/batch_inclusion_responses/*
1111
**/aligned_verification_data
1212
**/broadcast
13+
volume
1314

1415
nonce_*.bin

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ batcher_start: ./batcher/aligned-batcher/.env user_fund_payment_service
191191
@echo "Starting Batcher..."
192192
@cargo +nightly-2024-04-17 run --manifest-path ./batcher/aligned-batcher/Cargo.toml --release -- --config ./config-files/config-batcher.yaml --env-file ./batcher/aligned-batcher/.env
193193

194+
batcher_start_local: ./batcher/aligned-batcher/.env user_fund_payment_service
195+
@echo "Starting Batcher..."
196+
@$(MAKE) run_storage &
197+
@cargo +nightly-2024-04-17 run --manifest-path ./batcher/aligned-batcher/Cargo.toml --release -- --config ./config-files/config-batcher.yaml --env-file ./batcher/aligned-batcher/.env.dev
198+
194199
install_batcher:
195200
@cargo +nightly-2024-04-17 install --path batcher/aligned-batcher
196201

@@ -396,6 +401,11 @@ run_metrics: ## Run metrics using metrics-docker-compose.yaml
396401
@echo "Running metrics..."
397402
@docker-compose -f metrics-docker-compose.yaml up
398403

404+
__STORAGE__:
405+
run_storage: ## Run storage using storage-docker-compose.yaml
406+
@echo "Running storage..."
407+
@docker-compose -f storage-docker-compose.yaml up
408+
399409
__DEPLOYMENT__:
400410
deploy_aligned_contracts: ## Deploy Aligned Contracts
401411
@echo "Deploying Aligned Contracts..."

batcher/aligned-batcher/.env.dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AWS_SECRET_ACCESS_KEY=test
2+
AWS_REGION=us-east-2
3+
AWS_ACCESS_KEY_ID=test
4+
AWS_BUCKET_NAME=aligned.storage
5+
STORAGE_ENDPOINT=http://localhost:4566/aligned.storage
6+
LOCALSTACK_ENDPOINT_URL=http://localhost:4566
7+
RUST_LOG=info
8+
RUST_BACKTRACE=1

batcher/aligned-batcher/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ AWS_SECRET_ACCESS_KEY=<secret_access_key>
22
AWS_REGION=<region>
33
AWS_ACCESS_KEY_ID=<access_key_id>
44
AWS_BUCKET_NAME=<bucket_name>
5+
STORAGE_ENDPOINT=<storage_endpoint>
6+
LOCALSTACK_ENDPOINT_URL=<endpoint_url to use for localstack>
57
RUST_LOG=<log_level> # info, debug, error, warn, etc.

batcher/aligned-batcher/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl BatchState {
8787
pub struct Batcher {
8888
s3_client: S3Client,
8989
s3_bucket_name: String,
90+
storage_endpoint: String,
9091
eth_ws_provider: Provider<Ws>,
9192
payment_service: BatcherPaymentService,
9293
batch_state: Mutex<BatchState>,
@@ -102,10 +103,17 @@ pub struct Batcher {
102103
impl Batcher {
103104
pub async fn new(config_file: String) -> Self {
104105
dotenv().ok();
106+
107+
// https://docs.aws.amazon.com/sdk-for-rust/latest/dg/localstack.html
108+
let endpoint_url = env::var("LOCALSTACK_ENDPOINT_URL").ok();
109+
105110
let s3_bucket_name =
106111
env::var("AWS_BUCKET_NAME").expect("AWS_BUCKET_NAME not found in environment");
107112

108-
let s3_client = s3::create_client().await;
113+
let storage_endpoint =
114+
env::var("STORAGE_ENDPOINT").expect("STORAGE_ENDPOINT not found in environment");
115+
116+
let s3_client = s3::create_client(endpoint_url).await;
109117

110118
let config = ConfigFromYaml::new(config_file);
111119
let deployment_output =
@@ -147,6 +155,7 @@ impl Batcher {
147155
Self {
148156
s3_client,
149157
s3_bucket_name,
158+
storage_endpoint,
150159
eth_ws_provider,
151160
payment_service,
152161
batch_state: Mutex::new(BatchState::new()),
@@ -593,7 +602,7 @@ impl Batcher {
593602
info!("Batch sent to S3 with name: {}", file_name);
594603

595604
info!("Uploading batch to contract");
596-
let batch_data_pointer = "https://".to_owned() + &self.s3_bucket_name + "/" + &file_name;
605+
let batch_data_pointer: String = "".to_owned() + &self.storage_endpoint + "/" + &file_name;
597606

598607
let num_proofs_in_batch = leaves.len();
599608

batcher/aligned-batcher/src/s3/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ use aws_sdk_s3::error::SdkError;
44
use aws_sdk_s3::operation::put_object::{PutObjectError, PutObjectOutput};
55
use aws_sdk_s3::primitives::ByteStream;
66
use aws_sdk_s3::Client;
7+
use log::info;
78

8-
pub async fn create_client() -> Client {
9+
pub async fn create_client(endpoint_url: Option<String>) -> Client {
910
let region_provider = RegionProviderChain::default_provider().or_else("us-east-2");
10-
let config = aws_config::defaults(BehaviorVersion::latest())
11-
.region(region_provider)
12-
.load()
13-
.await;
14-
Client::new(&config)
11+
let mut config = aws_config::defaults(BehaviorVersion::latest()).region(region_provider);
12+
if let Some(endpoint_url) = &endpoint_url {
13+
info!("Using custom endpoint: {}", endpoint_url);
14+
config = config.endpoint_url(endpoint_url);
15+
}
16+
let config = config.load().await;
17+
18+
let mut s3_config_builder = aws_sdk_s3::config::Builder::from(&config);
19+
if endpoint_url.is_some() {
20+
info!("Forcing path style for custom endpoint");
21+
s3_config_builder = s3_config_builder.force_path_style(true);
22+
}
23+
Client::from_conf(s3_config_builder.build())
1524
}
1625

1726
pub async fn upload_object(

scripts/init-s3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import boto3
2+
3+
s3_client = boto3.client(
4+
"s3",
5+
endpoint_url=f"http://localhost:4566",
6+
aws_access_key_id="test",
7+
aws_secret_access_key="test"
8+
)
9+
10+
s3_client.create_bucket(Bucket="aligned.storage")

storage-docker-compose.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
localstack:
3+
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
4+
image: localstack/localstack:s3-latest
5+
ports:
6+
- "127.0.0.1:4566:4566" # LocalStack Gateway
7+
environment:
8+
- DEBUG=${DEBUG:-0}
9+
- DEFAULT_REGION=us-east-2
10+
- AWS_ACCESS_KEY_ID=test
11+
- AWS_SECRET_ACCESS_KEY=test
12+
volumes:
13+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
14+
- "/var/run/docker.sock:/var/run/docker.sock"
15+
- "./scripts/init-s3.py:/etc/localstack/init/ready.d/init-s3.py" # bucket initialization script

0 commit comments

Comments
 (0)