Skip to content

Commit

Permalink
fixes tests and some clean up [staging]
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
  • Loading branch information
brunocalza committed Feb 6, 2024
1 parent d485674 commit 7cb6f31
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 1,016 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jemallocator = "0.5.4"
log = "0.4.1"
multibase = "0.9"
multihash = "0.19.1"
multihash-codetable = { version = "0.1.1", features = ["digest", "sha2"] }
once_cell = "1.18.0"
openssl = { version = "0.10.57", features = ["vendored"] }
regex = "1.10.2"
Expand Down
1 change: 1 addition & 0 deletions lib/worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ hyper = { workspace = true }
log = { workspace = true }
multibase = { workspace = true }
multihash = { workspace = true }
multihash-codetable = { workspace = true }
once_cell = { workspace = true }
openssl = { workspace = true }
regex = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion lib/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod db;
pub mod domain;
pub mod gcs;
pub mod routes;
pub mod rpc;
pub mod startup;
pub mod utils;
pub mod web3storage;
145 changes: 64 additions & 81 deletions lib/worker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use basin_common::db;
use basin_evm::{testing::MockClient, BasinClient};
use basin_evm::BasinClient;
use basin_worker::gcs::GcsClient;
use basin_worker::rpc;
use basin_worker::web3storage::Web3StorageClient;
use basin_worker::web3storage::Web3StorageImpl;
use clap::error::ErrorKind;
use clap::{arg, CommandFactory, Parser, ValueEnum};
use ethers::{
Expand Down Expand Up @@ -122,25 +121,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
args.export_endpoint,
)
.await?;
let web3store_client = Web3StorageClient::new(args.basin_w3s_endpoint);

let listener = tokio::net::TcpListener::bind(&args.bind_address).await?;

match args.evm_type {
EvmType::Mem => {
rpc::listen(
MockClient::new().await?,
pg_pool,
gcs_client,
web3store_client,
listener,
)
.await
}
EvmType::Remote => {
let mut cmd = Cli::command();
let web3store_client = Web3StorageImpl::new(args.basin_w3s_endpoint);

let wallet_pk = match args.evm_wallet_pk {
let mut cmd = Cli::command();

let wallet_pk = match args.evm_wallet_pk {
Some(s) => s.replace("0x", ""),
None => cmd
.error(
Expand All @@ -149,26 +134,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.exit(),
};
let wallet_pk = match hex::decode(wallet_pk) {
Ok(v) => v,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid hex value for --evm-wallet-pk <EVM_WALLET_PK>",
)
.exit(),
};
let wallet = match LocalWallet::from_bytes(wallet_pk.as_slice()) {
Ok(w) => w,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid key value for --evm-wallet-pk <EVM_WALLET_PK>",
)
.exit(),
};
let wallet_pk = match hex::decode(wallet_pk) {
Ok(v) => v,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid hex value for --evm-wallet-pk <EVM_WALLET_PK>",
)
.exit(),
};
let wallet = match LocalWallet::from_bytes(wallet_pk.as_slice()) {
Ok(w) => w,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid key value for --evm-wallet-pk <EVM_WALLET_PK>",
)
.exit(),
};

let contract_address = match args.evm_contract_address {
let contract_address = match args.evm_contract_address {
Some(s) => s.replace("0x", ""),
None => cmd
.error(
Expand All @@ -177,47 +162,45 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.exit(),
};
let contract_address = match hex::decode(contract_address) {
Ok(v) => v,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid hex value for --evm-contract-address <EVM_CONTRACT_ADDRESS>",
)
.exit(),
};
let contract_address = match hex::decode(contract_address) {
Ok(v) => v,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid hex value for --evm-contract-address <EVM_CONTRACT_ADDRESS>",
)
.exit(),
};

let chain_id = match Chain::try_from(args.evm_chain_id) {
Ok(c) => c,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid chain ID value for --evm-chain-id <EVM_CHAIN_ID>",
)
.exit(),
};

let evm_client = BasinClient::new(
wallet,
Address::from_slice(contract_address.as_slice()),
args.evm_provider_url.as_str(),
chain_id,
)
.await?;

let chain_id = match Chain::try_from(args.evm_chain_id) {
Ok(c) => c,
Err(_) => cmd
.error(
ErrorKind::ValueValidation,
"invalid chain ID value for --evm-chain-id <EVM_CHAIN_ID>",
)
.exit(),
};
tokio::spawn(async move {
info!("HTTP API started");
let (_, server) = startup::start_http_server(
args.bind_health_address,
pool,
evm_client,
gcs_client,
web3store_client,
);
server.await;
});

let evm_client = BasinClient::new(
wallet,
Address::from_slice(contract_address.as_slice()),
args.evm_provider_url.as_str(),
chain_id,
)
.await?;

//let tcp_listener = TcpListener::bind(args.bind_health_address)?;
let p = pool.clone();
let c = evm_client.clone();
let gcs = gcs_client.clone();
let w3s = web3store_client.clone();
tokio::spawn(async move {
info!("HTTP API started");
let (_, server) =
startup::start_http_server(args.bind_health_address, p, c, gcs, w3s);
server.await;
});

rpc::listen(evm_client, pg_pool, gcs_client, web3store_client, listener).await
}
}
Ok(())
}
30 changes: 16 additions & 14 deletions lib/worker/src/routes/vaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,12 @@ pub struct CreateVaultResponse {
}

#[allow(clippy::too_many_arguments)]
pub async fn write_event(
pub async fn write_event<W: Web3StorageClient>(
path: String,
size: u64,
filename: String,
gcs_client: GcsClient,
w3s_client: Web3StorageClient,
w3s_client: W,
pool: PgPool,
params: WriteEventParams,
mut stream: impl Stream<Item = Result<impl warp::Buf, warp::Error>> + Unpin + Send + Sync,
Expand Down Expand Up @@ -530,7 +530,7 @@ pub async fn write_event(
}
}

let cid_bytes = match upload_w3s_mock(gcs_client, w3s_client, &filename).await {
let cid_bytes = match upload_w3s(gcs_client, w3s_client, &filename).await {
Ok(cid) => cid,
Err(err) => {
log::error!("{}", err);
Expand Down Expand Up @@ -629,12 +629,12 @@ async fn upload_stream(
Ok(output)
}

async fn upload_w3s_mock(
async fn upload_w3s<W: Web3StorageClient>(
gcs_client: GcsClient,
w3s_client: Web3StorageClient,
_w3s_client: W,
filename: &str,
) -> basin_common::errors::Result<Vec<u8>> {
let download_stream = gcs_client
let _download_stream = gcs_client
.inner
.download_streamed_object(
&GetObjectRequest {
Expand All @@ -647,14 +647,16 @@ async fn upload_w3s_mock(
.await
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;

let res = w3s_client
.upload(download_stream, filename)
.await
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;
// let res = w3s_client
// .upload(download_stream, filename)
// .await
// .map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;

let cid = Cid::from(res.root.clone())
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;
log::info!("uploaded file: {:?}", res.root);
// let cid = Cid::from(res.root.clone())
// .map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;
// log::info!("uploaded file: {:?}", res.root);

// Ok(cid.as_bytes())

Ok(cid.as_bytes())
Ok(Vec::new())
}
Loading

0 comments on commit 7cb6f31

Please sign in to comment.