Skip to content

Commit

Permalink
chore: replace ByteN with ByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 17, 2024
1 parent 5cf8ae4 commit f034d4c
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/ic_oss/src/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytes::{Bytes, BytesMut};
use candid::{CandidType, Principal};
use ic_agent::Agent;
use ic_oss_types::{bucket::*, crc32, file::*, folder::*, format_error, ByteN};
use ic_oss_types::{bucket::*, crc32, file::*, folder::*, format_error};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use sha3::{Digest, Sha3_256};
use std::{collections::BTreeSet, sync::Arc};
use tokio::io::AsyncRead;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Client {
.await?
}

pub async fn get_file_info_by_hash(&self, hash: ByteN<32>) -> Result<FileInfo, String> {
pub async fn get_file_info_by_hash(&self, hash: ByteArray<32>) -> Result<FileInfo, String> {
query_call(
&self.agent,
&self.bucket,
Expand Down Expand Up @@ -350,7 +350,7 @@ impl Client {
stream: T,
id: u32,
size: Option<u64>,
hash: Option<ByteN<32>>,
hash: Option<ByteArray<32>>,
exclude_chunks: &BTreeSet<u32>,
on_progress: F,
) -> UploadFileChunksResult
Expand Down
8 changes: 4 additions & 4 deletions src/ic_oss/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use candid::{Nat, Principal};
use ic_agent::Agent;
use ic_oss_types::{bucket::Token, cluster::*, ByteN};
use serde_bytes::ByteBuf;
use ic_oss_types::{bucket::Token, cluster::*};
use serde_bytes::{ByteArray, ByteBuf};
use std::{
collections::{BTreeMap, BTreeSet},
sync::Arc,
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Client {
query_call(&self.agent, &self.cluster, "get_cluster_info", ()).await?
}

pub async fn get_bucket_wasm(&self, hash: ByteN<32>) -> Result<WasmInfo, String> {
pub async fn get_bucket_wasm(&self, hash: ByteArray<32>) -> Result<WasmInfo, String> {
query_call(&self.agent, &self.cluster, "get_bucket_wasm", (hash,)).await?
}

Expand Down Expand Up @@ -92,7 +92,7 @@ impl Client {
pub async fn admin_add_wasm(
&self,
args: AddWasmInput,
force_prev_hash: Option<ByteN<32>>,
force_prev_hash: Option<ByteArray<32>>,
) -> Result<(), String> {
update_call(
&self.agent,
Expand Down
6 changes: 3 additions & 3 deletions src/ic_oss_bucket/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use ic_oss_types::{
bucket::BucketInfo,
file::{FileChunk, FileInfo},
folder::{FolderInfo, FolderName},
format_error, ByteN,
format_error,
};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};

use crate::{permission, store, SECONDS};

Expand Down Expand Up @@ -99,7 +99,7 @@ fn get_file_info(id: u32, access_token: Option<ByteBuf>) -> Result<FileInfo, Str

#[ic_cdk::query]
fn get_file_info_by_hash(
hash: ByteN<32>,
hash: ByteArray<32>,
access_token: Option<ByteBuf>,
) -> Result<FileInfo, String> {
let id = store::fs::get_file_id(&hash).ok_or("file not found")?;
Expand Down
4 changes: 2 additions & 2 deletions src/ic_oss_bucket/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use candid::Principal;
use ic_cdk::api::management_canister::main::CanisterStatusResponse;
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use std::collections::BTreeSet;

mod api_admin;
Expand All @@ -12,7 +12,7 @@ mod permission;
mod store;

use api_init::CanisterArgs;
use ic_oss_types::{bucket::*, file::*, folder::*, ByteN};
use ic_oss_types::{bucket::*, file::*, folder::*};

const MILLISECONDS: u64 = 1_000_000;
const SECONDS: u64 = 1_000_000_000;
Expand Down
26 changes: 13 additions & 13 deletions src/ic_oss_bucket/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ic_oss_types::{
},
folder::{FolderInfo, FolderName, UpdateFolderInput},
permission::Policies,
ByteN, MapValue,
MapValue,
};
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct Bucket {
// used to verify the request token signed with SECP256K1
pub trusted_ecdsa_pub_keys: Vec<ByteBuf>,
// used to verify the request token signed with ED25519
pub trusted_eddsa_pub_keys: Vec<ByteN<32>>,
pub trusted_eddsa_pub_keys: Vec<ByteArray<32>>,
}

impl Default for Bucket {
Expand Down Expand Up @@ -232,8 +232,8 @@ pub struct FileMetadata {
pub created_at: u64, // unix timestamp in milliseconds
pub updated_at: u64, // unix timestamp in milliseconds
pub chunks: u32,
pub status: i8, // -1: archived; 0: readable and writable; 1: readonly
pub hash: Option<ByteN<32>>, // recommend sha3 256
pub status: i8, // -1: archived; 0: readable and writable; 1: readonly
pub hash: Option<ByteArray<32>>, // recommend sha3 256
pub dek: Option<ByteBuf>, // // Data Encryption Key that encrypted by BYOK or vetKey in COSE_Encrypt0
pub custom: Option<MapValue>, // custom metadata
pub ex: Option<MapValue>, // External Resource, ER indicates that the file is an external resource.
Expand Down Expand Up @@ -947,7 +947,7 @@ pub mod fs {
Err(format!("file hash conflict, {}", prev))?;
}

m.insert(hash.0, id);
m.insert(hash, id);
Ok::<(), String>(())
})?;
}
Expand Down Expand Up @@ -1094,13 +1094,13 @@ pub mod fs {
HASHS.with(|r| {
let mut hm = r.borrow_mut();
if let Some(ref hash) = file.hash {
if let Some(prev) = hm.get(&hash.0) {
if let Some(prev) = hm.get(hash) {
Err(format!("file hash conflict, {}", prev))?;
}
hm.insert(hash.0, change.id);
hm.insert(*hash, change.id);
}
if let Some(prev_hash) = prev_hash {
hm.remove(&prev_hash.0);
hm.remove(&prev_hash);
}
Ok::<(), String>(())
})?;
Expand Down Expand Up @@ -1283,7 +1283,7 @@ pub mod fs {

m.remove(&id);
if let Some(hash) = file.hash {
HASHS.with(|r| r.borrow_mut().remove(&hash.0));
HASHS.with(|r| r.borrow_mut().remove(&hash));
}
FS_CHUNKS_STORE.with(|r| {
let mut fs_data = r.borrow_mut();
Expand Down Expand Up @@ -1321,7 +1321,7 @@ pub mod fs {
removed.push(id);
folder.files.remove(&id);
if let Some(hash) = file.hash {
HASHS.with(|r| r.borrow_mut().remove(&hash.0));
HASHS.with(|r| r.borrow_mut().remove(&hash));
}

for i in 0..file.chunks {
Expand Down Expand Up @@ -1379,7 +1379,7 @@ mod test {

let f1 = fs::add_file(FileMetadata {
name: "f1.bin".to_string(),
hash: Some(ByteN::from([1u8; 32])),
hash: Some(ByteArray::from([1u8; 32])),
..Default::default()
})
.unwrap();
Expand All @@ -1404,14 +1404,14 @@ mod test {

assert!(fs::add_file(FileMetadata {
name: "f2.bin".to_string(),
hash: Some(ByteN::from([1u8; 32])),
hash: Some(ByteArray::from([1u8; 32])),
..Default::default()
})
.is_err());

let f2 = fs::add_file(FileMetadata {
name: "f2.bin".to_string(),
hash: Some(ByteN::from([2u8; 32])),
hash: Some(ByteArray::from([2u8; 32])),
..Default::default()
})
.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_can/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use candid::Principal;
use ciborium::{from_reader, into_writer};
use ic_oss_types::{file::*, ByteN};
use ic_oss_types::file::*;
use ic_stable_structures::{storable::Bound, Storable};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteArray;
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -94,7 +95,7 @@ pub struct FileMetadata {
pub created_at: u64, // unix timestamp in milliseconds
pub updated_at: u64, // unix timestamp in milliseconds
pub chunks: u32,
pub hash: Option<ByteN<32>>, // recommend sha3 256
pub hash: Option<ByteArray<32>>, // recommend sha3 256
}

impl Storable for FileMetadata {
Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_cli/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::prelude::*;
use ic_oss_types::{file::*, format_error, ByteN};
use ic_oss_types::{file::*, format_error};
use serde_bytes::ByteArray;
use sha3::{Digest, Sha3_256};
use tokio::io::AsyncReadExt;
use tokio::{time, time::Duration};
Expand Down Expand Up @@ -28,7 +29,7 @@ pub async fn upload_file(
mime_db::lookup(file).unwrap_or("application/octet-stream")
};

let hash: Option<ByteN<32>> = if enable_hash_index {
let hash: Option<ByteArray<32>> = if enable_hash_index {
let fs = tokio::fs::File::open(&file_path)
.await
.map_err(format_error)?;
Expand Down
25 changes: 12 additions & 13 deletions src/ic_oss_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use ic_oss_types::{
cluster::AddWasmInput,
file::{MoveInput, CHUNK_SIZE},
folder::CreateFolderInput,
format_error, ByteN,
format_error,
};
use ring::{rand, signature::Ed25519KeyPair};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use sha3::{Digest, Sha3_256};
use std::{
io::SeekFrom,
Expand Down Expand Up @@ -306,16 +306,15 @@ async fn main() -> Result<(), String> {
let cli = cli.cluster(identity, ic, cluster).await?;
let wasm = std::fs::read(path).map_err(format_error)?;
let prev_hash = prev_hash.as_ref().map(|s| parse_file_hash(s)).transpose()?;
cli
.admin_add_wasm(
AddWasmInput {
wasm: ByteBuf::from(wasm),
description: description.to_owned(),
},
prev_hash,
)
.await
.map_err(format_error)?;
cli.admin_add_wasm(
AddWasmInput {
wasm: ByteBuf::from(wasm),
description: description.to_owned(),
},
prev_hash,
)
.await
.map_err(format_error)?;
return Ok(());
}

Expand Down Expand Up @@ -584,7 +583,7 @@ where
Ok(())
}

fn parse_file_hash(s: &str) -> Result<ByteN<32>, String> {
fn parse_file_hash(s: &str) -> Result<ByteArray<32>, String> {
let s = s.replace("\\", "");
let data = hex::decode(s.strip_prefix("0x").unwrap_or(&s)).map_err(format_error)?;
let hash: [u8; 32] = data.try_into().map_err(format_error)?;
Expand Down
15 changes: 7 additions & 8 deletions src/ic_oss_cluster/src/api_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use ic_oss_types::{
cluster::{AddWasmInput, DeployWasmInput},
format_error,
permission::Policies,
ByteN,
};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use std::collections::BTreeSet;
use std::time::Duration;

Expand Down Expand Up @@ -82,7 +81,7 @@ async fn admin_detach_policies(args: Token) -> Result<(), String> {
#[ic_cdk::update(guard = "is_controller_or_manager")]
async fn admin_add_wasm(
args: AddWasmInput,
force_prev_hash: Option<ByteN<32>>,
force_prev_hash: Option<ByteArray<32>>,
) -> Result<(), String> {
store::wasm::add_wasm(
ic_cdk::caller(),
Expand All @@ -96,7 +95,7 @@ async fn admin_add_wasm(
#[ic_cdk::update]
async fn validate_admin_add_wasm(
args: AddWasmInput,
force_prev_hash: Option<ByteN<32>>,
force_prev_hash: Option<ByteArray<32>>,
) -> Result<(), String> {
store::wasm::add_wasm(
ic_cdk::caller(),
Expand All @@ -110,7 +109,7 @@ async fn validate_admin_add_wasm(
#[ic_cdk::update(guard = "is_controller")]
async fn admin_deploy_bucket(
args: DeployWasmInput,
ignore_prev_hash: Option<ByteN<32>>,
ignore_prev_hash: Option<ByteArray<32>>,
) -> Result<(), String> {
let (info,) = canister_info(CanisterInfoRequest {
canister_id: args.canister,
Expand Down Expand Up @@ -138,7 +137,7 @@ async fn admin_deploy_bucket(
} else {
Default::default()
};
let prev_hash = ByteN::from(prev_hash);
let prev_hash = ByteArray::from(prev_hash);
let (hash, wasm) = if let Some(ignore_prev_hash) = ignore_prev_hash {
if ignore_prev_hash != prev_hash {
Err(format!(
Expand Down Expand Up @@ -187,7 +186,7 @@ async fn admin_deploy_bucket(
#[ic_cdk::update]
async fn validate_admin_deploy_bucket(
args: DeployWasmInput,
ignore_prev_hash: Option<ByteN<32>>,
ignore_prev_hash: Option<ByteArray<32>>,
) -> Result<(), String> {
let (info,) = canister_info(CanisterInfoRequest {
canister_id: args.canister,
Expand All @@ -209,7 +208,7 @@ async fn validate_admin_deploy_bucket(
} else {
Default::default()
};
let prev_hash = ByteN::from(prev_hash);
let prev_hash = ByteArray::from(prev_hash);
if let Some(ignore_prev_hash) = ignore_prev_hash {
if ignore_prev_hash != prev_hash {
Err(format!(
Expand Down
5 changes: 3 additions & 2 deletions src/ic_oss_cluster/src/api_query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use candid::{Nat, Principal};
use ic_oss_types::{
cluster::{BucketDeploymentInfo, ClusterInfo, WasmInfo},
nat_to_u64, ByteN,
nat_to_u64,
};
use serde_bytes::ByteArray;
use std::collections::BTreeMap;

use crate::{is_controller_or_manager, store};
Expand All @@ -13,7 +14,7 @@ fn get_cluster_info() -> Result<ClusterInfo, String> {
}

#[ic_cdk::query]
fn get_bucket_wasm(hash: ByteN<32>) -> Result<WasmInfo, String> {
fn get_bucket_wasm(hash: ByteArray<32>) -> Result<WasmInfo, String> {
store::wasm::get_wasm(&hash)
.map(|w| WasmInfo {
created_at: w.created_at,
Expand Down
3 changes: 1 addition & 2 deletions src/ic_oss_cluster/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use candid::{Nat, Principal};
use ic_oss_types::{
bucket::Token,
cluster::{AddWasmInput, BucketDeploymentInfo, ClusterInfo, DeployWasmInput, WasmInfo},
ByteN,
};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use std::collections::{BTreeMap, BTreeSet};

mod api_admin;
Expand Down
Loading

0 comments on commit f034d4c

Please sign in to comment.