Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Jan 13, 2024
1 parent 0c36cb1 commit 7650cf3
Show file tree
Hide file tree
Showing 4 changed files with 8 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.

39 changes: 1 addition & 38 deletions src/canisters/frontend/ic-certified-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
types::*,
};
use asset_certification::types::{certification::AssetKey, rc_bytes::RcBytes};
use candid::{candid_method, Principal};
use candid::Principal;
use ic_cdk::api::{call::ManualReply, caller, data_certificate, set_certified_data, time, trap};
use ic_cdk::{query, update};
use serde_bytes::ByteBuf;
Expand All @@ -33,19 +33,16 @@ thread_local! {
}

#[query]
#[candid_method(query)]
fn api_version() -> u16 {
1
}

#[update(guard = "is_manager_or_controller")]
#[candid_method(update)]
fn authorize(other: Principal) {
STATE.with(|s| s.borrow_mut().grant_permission(other, &Permission::Commit))
}

#[update(guard = "is_manager_or_controller")]
#[candid_method(update)]
fn grant_permission(arg: GrantPermissionArguments) {
STATE.with(|s| {
s.borrow_mut()
Expand All @@ -54,7 +51,6 @@ fn grant_permission(arg: GrantPermissionArguments) {
}

#[update]
#[candid_method(update)]
async fn validate_grant_permission(arg: GrantPermissionArguments) -> Result<String, String> {
Ok(format!(
"grant {} permission to principal {}",
Expand All @@ -63,7 +59,6 @@ async fn validate_grant_permission(arg: GrantPermissionArguments) -> Result<Stri
}

#[update]
#[candid_method(update)]
async fn deauthorize(other: Principal) {
let check_access_result = if other == caller() {
// this isn't "ManagePermissions" because these legacy methods only
Expand All @@ -79,7 +74,6 @@ async fn deauthorize(other: Principal) {
}

#[update]
#[candid_method(update)]
async fn revoke_permission(arg: RevokePermissionArguments) {
let check_access_result = if arg.of_principal == caller() {
has_permission_or_is_controller(&arg.permission)
Expand All @@ -96,7 +90,6 @@ async fn revoke_permission(arg: RevokePermissionArguments) {
}

#[update]
#[candid_method(update)]
async fn validate_revoke_permission(arg: RevokePermissionArguments) -> Result<String, String> {
Ok(format!(
"revoke {} permission from principal {}",
Expand All @@ -105,32 +98,27 @@ async fn validate_revoke_permission(arg: RevokePermissionArguments) -> Result<St
}

#[update(manual_reply = true)]
#[candid_method(update)]
fn list_authorized() -> ManualReply<Vec<Principal>> {
STATE.with(|s| ManualReply::one(s.borrow().list_permitted(&Permission::Commit)))
}

#[update(manual_reply = true)]
#[candid_method(update)]
fn list_permitted(arg: ListPermittedArguments) -> ManualReply<Vec<Principal>> {
STATE.with(|s| ManualReply::one(s.borrow().list_permitted(&arg.permission)))
}

#[update(guard = "is_controller")]
#[candid_method(update)]
async fn take_ownership() {
let caller = ic_cdk::api::caller();
STATE.with(|s| s.borrow_mut().take_ownership(caller))
}

#[update]
#[candid_method(update)]
async fn validate_take_ownership() -> Result<String, String> {
Ok("revoke all permissions, then gives the caller Commit permissions".to_string())
}

#[query]
#[candid_method(query)]
fn retrieve(key: AssetKey) -> RcBytes {
STATE.with(|s| match s.borrow().retrieve(&key) {
Ok(bytes) => bytes,
Expand All @@ -139,7 +127,6 @@ fn retrieve(key: AssetKey) -> RcBytes {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn store(arg: StoreArg) {
STATE.with(move |s| {
if let Err(msg) = s.borrow_mut().store(arg, time()) {
Expand All @@ -150,7 +137,6 @@ fn store(arg: StoreArg) {
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn create_batch() -> CreateBatchResponse {
STATE.with(|s| match s.borrow_mut().create_batch(time()) {
Ok(batch_id) => CreateBatchResponse { batch_id },
Expand All @@ -159,7 +145,6 @@ fn create_batch() -> CreateBatchResponse {
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn create_chunk(arg: CreateChunkArg) -> CreateChunkResponse {
STATE.with(|s| match s.borrow_mut().create_chunk(arg, time()) {
Ok(chunk_id) => CreateChunkResponse { chunk_id },
Expand All @@ -168,7 +153,6 @@ fn create_chunk(arg: CreateChunkArg) -> CreateChunkResponse {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn create_asset(arg: CreateAssetArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().create_asset(arg) {
Expand All @@ -179,7 +163,6 @@ fn create_asset(arg: CreateAssetArguments) {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn set_asset_content(arg: SetAssetContentArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().set_asset_content(arg, time()) {
Expand All @@ -190,7 +173,6 @@ fn set_asset_content(arg: SetAssetContentArguments) {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn unset_asset_content(arg: UnsetAssetContentArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().unset_asset_content(arg) {
Expand All @@ -201,7 +183,6 @@ fn unset_asset_content(arg: UnsetAssetContentArguments) {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn delete_asset(arg: DeleteAssetArguments) {
STATE.with(|s| {
s.borrow_mut().delete_asset(arg);
Expand All @@ -210,7 +191,6 @@ fn delete_asset(arg: DeleteAssetArguments) {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn clear() {
STATE.with(|s| {
s.borrow_mut().clear();
Expand All @@ -219,7 +199,6 @@ fn clear() {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn commit_batch(arg: CommitBatchArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().commit_batch(arg, time()) {
Expand All @@ -230,7 +209,6 @@ fn commit_batch(arg: CommitBatchArguments) {
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn propose_commit_batch(arg: CommitBatchArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().propose_commit_batch(arg) {
Expand All @@ -240,7 +218,6 @@ fn propose_commit_batch(arg: CommitBatchArguments) {
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn compute_evidence(arg: ComputeEvidenceArguments) -> Option<ByteBuf> {
STATE.with(|s| match s.borrow_mut().compute_evidence(arg) {
Err(msg) => trap(&msg),
Expand All @@ -249,7 +226,6 @@ fn compute_evidence(arg: ComputeEvidenceArguments) -> Option<ByteBuf> {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn commit_proposed_batch(arg: CommitProposedBatchArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().commit_proposed_batch(arg, time()) {
Expand All @@ -260,13 +236,11 @@ fn commit_proposed_batch(arg: CommitProposedBatchArguments) {
}

#[update]
#[candid_method(update)]
fn validate_commit_proposed_batch(arg: CommitProposedBatchArguments) -> Result<String, String> {
STATE.with(|s| s.borrow_mut().validate_commit_proposed_batch(arg))
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn delete_batch(arg: DeleteBatchArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().delete_batch(arg) {
Expand All @@ -276,7 +250,6 @@ fn delete_batch(arg: DeleteBatchArguments) {
}

#[query]
#[candid_method(query)]
fn get(arg: GetArg) -> EncodedAsset {
STATE.with(|s| match s.borrow().get(arg) {
Ok(asset) => asset,
Expand All @@ -285,7 +258,6 @@ fn get(arg: GetArg) -> EncodedAsset {
}

#[query]
#[candid_method(query)]
fn get_chunk(arg: GetChunkArg) -> GetChunkResponse {
STATE.with(|s| match s.borrow().get_chunk(arg) {
Ok(content) => GetChunkResponse { content },
Expand All @@ -294,21 +266,18 @@ fn get_chunk(arg: GetChunkArg) -> GetChunkResponse {
}

#[query]
#[candid_method(query)]
fn list() -> Vec<AssetDetails> {
STATE.with(|s| s.borrow().list_assets())
}

#[query]
#[candid_method(query)]
fn certified_tree() -> CertifiedTree {
let certificate = data_certificate().unwrap_or_else(|| trap("no data certificate available"));

STATE.with(|s| s.borrow().certified_tree(&certificate))
}

#[query]
#[candid_method(query)]
fn http_request(req: HttpRequest) -> HttpResponse {
let certificate = data_certificate().unwrap_or_else(|| trap("no data certificate available"));

Expand All @@ -322,7 +291,6 @@ fn http_request(req: HttpRequest) -> HttpResponse {
}

#[query]
#[candid_method(query)]
fn http_request_streaming_callback(token: StreamingCallbackToken) -> StreamingCallbackHttpResponse {
STATE.with(|s| {
s.borrow()
Expand All @@ -332,7 +300,6 @@ fn http_request_streaming_callback(token: StreamingCallbackToken) -> StreamingCa
}

#[query]
#[candid_method(query)]
fn get_asset_properties(key: AssetKey) -> AssetProperties {
STATE.with(|s| {
s.borrow()
Expand All @@ -342,7 +309,6 @@ fn get_asset_properties(key: AssetKey) -> AssetProperties {
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn set_asset_properties(arg: SetAssetPropertiesArguments) {
STATE.with(|s| {
if let Err(msg) = s.borrow_mut().set_asset_properties(arg) {
Expand All @@ -352,19 +318,16 @@ fn set_asset_properties(arg: SetAssetPropertiesArguments) {
}

#[update(guard = "can_prepare")]
#[candid_method(update)]
fn get_configuration() -> ConfigurationResponse {
STATE.with(|s| s.borrow().get_configuration())
}

#[update(guard = "can_commit")]
#[candid_method(update)]
fn configure(arg: ConfigureArguments) {
STATE.with(|s| s.borrow_mut().configure(arg))
}

#[update]
#[candid_method(update)]
fn validate_configure(arg: ConfigureArguments) -> Result<String, String> {
Ok(format!("configure: {:?}", arg))
}
Expand Down
10 changes: 5 additions & 5 deletions src/canisters/frontend/ic-certified-assets/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ impl State {
self.assets.clear();
self.batches.clear();
self.chunks.clear();
self.next_batch_id = Nat::from(1);
self.next_chunk_id = Nat::from(1);
self.next_batch_id = Nat::from(1u8);
self.next_chunk_id = Nat::from(1u8);
}

pub fn has_permission(&self, principal: &Principal, permission: &Permission) -> bool {
Expand Down Expand Up @@ -566,7 +566,7 @@ impl State {
}
}
let batch_id = self.next_batch_id.clone();
self.next_batch_id += 1;
self.next_batch_id += 1u8;

self.batches.insert(
batch_id.clone(),
Expand Down Expand Up @@ -607,7 +607,7 @@ impl State {
batch.expires_at = Int::from(now + BATCH_EXPIRY_NANOS);

let chunk_id = self.next_chunk_id.clone();
self.next_chunk_id += 1;
self.next_chunk_id += 1u8;
batch.chunk_content_total_size += arg.content.as_ref().len();

self.chunks.insert(
Expand Down Expand Up @@ -1093,7 +1093,7 @@ impl From<StableState> for State {
prepare_principals,
manage_permissions_principals,
assets: stable_state.stable_assets,
next_batch_id: stable_state.next_batch_id.unwrap_or_else(|| Nat::from(1)),
next_batch_id: stable_state.next_batch_id.unwrap_or_else(|| Nat::from(1u8)),
configuration: stable_state.configuration.unwrap_or_default(),
..Self::default()
};
Expand Down
1 change: 1 addition & 0 deletions src/canisters/frontend/ic-frontend-canister/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ crate-type = ["cdylib"]
[dependencies]
ic-certified-assets = { path = "../ic-certified-assets" }
ic-cdk.workspace = true
candid.workspace = true

0 comments on commit 7650cf3

Please sign in to comment.