Skip to content

Commit

Permalink
Merge pull request #26 from tablelandnetwork/bcalza/fixcid
Browse files Browse the repository at this point in the history
fixes cid generation
  • Loading branch information
brunocalza authored Jan 24, 2024
2 parents 1de90c8 + a5b2511 commit 2fb63a8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 43 deletions.
1 change: 1 addition & 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 @@ -49,6 +49,7 @@ hyper = { version = "0.14", features = [
jemallocator = "0.5.4"
log = "0.4.1"
multibase = "0.9"
multihash = "0.19.1"
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 @@ -34,6 +34,7 @@ hex = { workspace = true }
hyper = { workspace = true }
log = { workspace = true }
multibase = { workspace = true }
multihash = { workspace = true }
once_cell = { workspace = true }
openssl = { workspace = true }
regex = { workspace = true }
Expand Down
50 changes: 12 additions & 38 deletions lib/worker/src/routes/vaults.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use std::io::Cursor;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;

use crate::crypto;
use crate::db;
use crate::domain::Cid;
use crate::domain::Vault;
use crate::gcs::GcsClient;
use crate::web3storage::Web3StorageClient;
use std::str::FromStr;

use basin_evm::EVMClient;
use blockless_car::reader::new_v1;
use blockless_car::reader::CarReader;
use chrono::DateTime;

use ethers::types::Address;
Expand All @@ -28,8 +22,9 @@ use google_cloud_storage::http::resumable_upload_client::ResumableUploadClient;
use serde::Deserialize;
use serde::Serialize;
use sqlx::PgPool;
use w3s::writer::car;

use cid::Cid as RustCid;
use multihash::Multihash;
use std::borrow::BorrowMut;
use std::convert::Infallible;
use std::io::Write;
Expand Down Expand Up @@ -710,41 +705,20 @@ async fn upload_w3s_mock(
.await
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;

// replace "/" with "_" in filename to avoid messing up ipfs path
let _w3s_filename = filename.replace('/', "_");

let mut buffer = Vec::new();
//let mut writer = Cursor::new(&mut buffer);
// Create a new Car writer
let mut w3s_car = car::Car::new(
1,
Arc::new(Mutex::new(vec![car::single_file_to_directory_item(
filename, None,
)])),
None,
None,
&mut buffer,
);

let mut hasher = Keccak::v256();
while let Some(Ok(data)) = download_stream.next().await {
w3s_car
.write_all(data.as_ref())
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;
hasher.update(data.as_ref());
}

w3s_car
.flush()
.map_err(|e| basin_common::errors::Error::Upload(e.to_string()))?;
let mut output = [0u8; 32];
hasher.finalize(&mut output);

let mut reader = Cursor::new(&buffer);
let car_reader = new_v1(&mut reader).unwrap();
const SHA2_256: u64 = 0x12;
let digest = Multihash::<64>::wrap(SHA2_256, &output).unwrap();

let roots = car_reader.header().roots();
let result_root_cid = roots.last().ok_or(basin_common::errors::Error::Upload(
"w3s upload failed: no cids returned".to_string(),
))?;
let cid = result_root_cid.to_owned();
log::info!("uploaded file to w3s: {:?}", cid);
const DAG_PB: u64 = 0x70;
let cid = RustCid::new_v1(DAG_PB, digest);
log::info!("uploaded file: {:?}", cid);

Ok(cid.to_bytes())
}
4 changes: 2 additions & 2 deletions lib/worker/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ impl TestApp {
&self,
vault: &str,
timestamp: i64,
event_content: [u8; 256],
event_content: Vec<u8>,
) -> Response {
// calculating hash
let mut hasher = Keccak::v256();
hasher.update(&event_content[..256]);
hasher.update(&event_content[..]);
let mut output = [0u8; 32];
hasher.finalize(&mut output);

Expand Down
6 changes: 3 additions & 3 deletions lib/worker/tests/api/http.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::helpers::spawn_app;
use basin_evm::EVMClient;
use ethers::{
core::rand::{thread_rng, Rng},
core::rand::{distributions::Uniform, thread_rng, Rng},
signers::Signer,
};

Expand Down Expand Up @@ -229,8 +229,8 @@ async fn write_event() {
app.create_vault_with_cache("api.test", 10).await;

// creating random event
let mut event_content = [0u8; 256];
thread_rng().fill(&mut event_content);
let range = Uniform::from(0..20);
let event_content = thread_rng().sample_iter(&range).take(100).collect();

let response = app
.upload_event("api.test", 1701372646, event_content)
Expand Down

0 comments on commit 2fb63a8

Please sign in to comment.