Skip to content

Commit

Permalink
feat: version for storage (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored May 3, 2024
1 parent a8f3686 commit a76ccd3
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/declarations/satellite/satellite.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface AssetNoContent {
encodings: Array<[string, AssetEncodingNoContent]>;
headers: Array<[string, string]>;
created_at: bigint;
version: [] | [bigint];
}
export interface AuthenticationConfig {
internet_identity: [] | [AuthenticationConfigInternetIdentity];
Expand All @@ -47,6 +48,7 @@ export type ControllerScope = { Write: null } | { Admin: null };
export interface CustomDomain {
updated_at: bigint;
created_at: bigint;
version: [] | [bigint];
bn_id: [] | [string];
}
export interface DelDoc {
Expand Down
4 changes: 3 additions & 1 deletion src/declarations/satellite/satellite.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const idlFactory = ({ IDL }) => {
updated_at: IDL.Nat64,
encodings: IDL.Vec(IDL.Tuple(IDL.Text, AssetEncodingNoContent)),
headers: IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text)),
created_at: IDL.Nat64
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64)
});
const AuthenticationConfigInternetIdentity = IDL.Record({
derivation_origin: IDL.Opt(IDL.Text)
Expand Down Expand Up @@ -147,6 +148,7 @@ export const idlFactory = ({ IDL }) => {
const CustomDomain = IDL.Record({
updated_at: IDL.Nat64,
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64),
bn_id: IDL.Opt(IDL.Text)
});
const ListResults_1 = IDL.Record({
Expand Down
4 changes: 3 additions & 1 deletion src/declarations/satellite/satellite.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const idlFactory = ({ IDL }) => {
updated_at: IDL.Nat64,
encodings: IDL.Vec(IDL.Tuple(IDL.Text, AssetEncodingNoContent)),
headers: IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text)),
created_at: IDL.Nat64
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64)
});
const AuthenticationConfigInternetIdentity = IDL.Record({
derivation_origin: IDL.Opt(IDL.Text)
Expand Down Expand Up @@ -147,6 +148,7 @@ export const idlFactory = ({ IDL }) => {
const CustomDomain = IDL.Record({
updated_at: IDL.Nat64,
created_at: IDL.Nat64,
version: IDL.Opt(IDL.Nat64),
bn_id: IDL.Opt(IDL.Text)
});
const ListResults_1 = IDL.Record({
Expand Down
2 changes: 2 additions & 0 deletions src/libs/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AssetNoContent = record {
encodings : vec record { text; AssetEncodingNoContent };
headers : vec record { text; text };
created_at : nat64;
version : opt nat64;
};
type AuthenticationConfig = record {
internet_identity : opt AuthenticationConfigInternetIdentity;
Expand All @@ -41,6 +42,7 @@ type ControllerScope = variant { Write; Admin };
type CustomDomain = record {
updated_at : nat64;
created_at : nat64;
version : opt nat64;
bn_id : opt text;
};
type DelDoc = record { version : opt nat64 };
Expand Down
8 changes: 1 addition & 7 deletions src/libs/satellite/src/storage/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ pub fn build_asset_response(
&rewrite_source,
);

let Asset {
key,
headers: _,
encodings: _,
created_at: _,
updated_at: _,
} = &asset;
let Asset { key, .. } = &asset;

match headers {
Ok(headers) => {
Expand Down
1 change: 1 addition & 0 deletions src/libs/satellite/src/storage/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl From<&Asset> for AssetNoContent {
.collect(),
created_at: asset.created_at,
updated_at: asset.updated_at,
version: asset.version,
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/libs/satellite/src/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use crate::msg::{
use crate::rules::constants::DEFAULT_ASSETS_COLLECTIONS;
use candid::Principal;
use ic_cdk::api::time;
use junobuild_shared::constants::INITIAL_VERSION;
use junobuild_shared::controllers::is_controller;
use junobuild_shared::types::state::Controllers;
use junobuild_shared::types::state::{Controllers, Timestamp, Version};
use junobuild_shared::utils::principal_not_equal;
use std::collections::HashMap;

Expand Down Expand Up @@ -764,11 +765,13 @@ fn commit_chunks(
encodings: HashMap::new(),
created_at: now,
updated_at: now,
version: Some(INITIAL_VERSION),
};

if let Some(existing_asset) = current {
asset.encodings = existing_asset.encodings.clone();
asset.created_at = existing_asset.created_at;
asset.version = Some(existing_asset.version.unwrap_or_default() + 1);
}

let encoding_type = get_encoding_type(&batch.encoding_type)?;
Expand Down Expand Up @@ -876,17 +879,23 @@ fn set_state_domain_impl(domain_name: &DomainName, bn_id: &Option<String>) {

let now = time();

let created_at: u64 = match domain {
let created_at: Timestamp = match domain.clone() {
None => now,
Some(domain) => domain.created_at,
};

let updated_at: u64 = now;
let version: Version = match domain {
None => INITIAL_VERSION,
Some(domain) => domain.version.unwrap_or_default() + 1,
};

let updated_at: Timestamp = now;

let custom_domain = CustomDomain {
bn_id: bn_id.to_owned(),
created_at,
updated_at,
version: Some(version),
};

insert_state_domain(domain_name, &custom_domain);
Expand Down
9 changes: 6 additions & 3 deletions src/libs/satellite/src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub mod store {
use crate::types::core::{Blob, CollectionKey};
use candid::CandidType;
use ic_certification::Hash;
use junobuild_shared::types::state::{Timestamp, UserId};
use junobuild_shared::types::state::{Timestamp, UserId, Version};
use serde::{Deserialize, Serialize};
use std::clone::Clone;
use std::collections::HashMap;
Expand Down Expand Up @@ -110,6 +110,7 @@ pub mod store {
pub encodings: HashMap<EncodingType, AssetEncoding>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub version: Option<Version>,
}

#[derive(CandidType, Serialize, Deserialize, Clone)]
Expand All @@ -130,7 +131,7 @@ pub mod store {
pub mod interface {
use candid::{CandidType, Deserialize};
use ic_certification::Hash;
use junobuild_shared::types::state::Timestamp;
use junobuild_shared::types::state::{Timestamp, Version};
use serde::Serialize;

use crate::storage::http::types::HeaderField;
Expand Down Expand Up @@ -179,6 +180,7 @@ pub mod interface {
pub encodings: Vec<(EncodingType, AssetEncodingNoContent)>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub version: Option<Version>,
}

#[derive(CandidType, Deserialize, Clone)]
Expand Down Expand Up @@ -266,7 +268,7 @@ pub mod http_request {
pub mod domain {
use crate::types::core::DomainName;
use candid::CandidType;
use junobuild_shared::types::state::Timestamp;
use junobuild_shared::types::state::{Timestamp, Version};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand All @@ -277,5 +279,6 @@ pub mod domain {
pub bn_id: Option<String>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
pub version: Option<Version>,
}
}
38 changes: 26 additions & 12 deletions src/libs/satellite/src/storage/well_known/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::storage::http::types::HeaderField;
use crate::storage::types::store::{Asset, AssetEncoding, AssetKey};
use ic_cdk::api::time;
use ic_cdk::id;
use junobuild_shared::constants::INITIAL_VERSION;
use junobuild_shared::types::state::{Timestamp, Version};
use std::collections::HashMap;

pub fn map_custom_domains_asset(custom_domains: &String, existing_asset: Option<Asset>) -> Asset {
Expand All @@ -20,22 +22,28 @@ pub fn map_custom_domains_asset(custom_domains: &String, existing_asset: Option<
description: None,
};

let created_at: Timestamp = match existing_asset.clone() {
None => now,
Some(existing_asset) => existing_asset.created_at,
};

let version: Version = match existing_asset {
None => INITIAL_VERSION,
Some(existing_asset) => existing_asset.version.unwrap_or_default() + 1,
};

let mut asset: Asset = Asset {
key,
headers: Vec::from([HeaderField(
"content-type".to_string(),
"application/octet-stream".to_string(),
)]),
encodings: HashMap::new(),
created_at: now,
created_at,
updated_at: now,
version: Some(version),
};

match existing_asset {
None => (),
Some(existing_asset) => asset.created_at = existing_asset.created_at,
}

let chunks = Vec::from([custom_domains.as_bytes().to_vec()]);

asset.encodings.insert(
Expand All @@ -61,22 +69,28 @@ pub fn map_alternative_origins_asset(
description: None,
};

let created_at: Timestamp = match existing_asset.clone() {
None => now,
Some(existing_asset) => existing_asset.created_at,
};

let version: Version = match existing_asset {
None => INITIAL_VERSION,
Some(existing_asset) => existing_asset.version.unwrap_or_default() + 1,
};

let mut asset: Asset = Asset {
key,
headers: Vec::from([HeaderField(
"content-type".to_string(),
"application/json".to_string(),
)]),
encodings: HashMap::new(),
created_at: now,
created_at,
updated_at: now,
version: Some(version),
};

match existing_asset {
None => (),
Some(existing_asset) => asset.created_at = existing_asset.created_at,
}

let chunks = Vec::from([alternative_origins.as_bytes().to_vec()]);

asset.encodings.insert(
Expand Down
2 changes: 2 additions & 0 deletions src/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AssetNoContent = record {
encodings : vec record { text; AssetEncodingNoContent };
headers : vec record { text; text };
created_at : nat64;
version : opt nat64;
};
type AuthenticationConfig = record {
internet_identity : opt AuthenticationConfigInternetIdentity;
Expand All @@ -43,6 +44,7 @@ type ControllerScope = variant { Write; Admin };
type CustomDomain = record {
updated_at : nat64;
created_at : nat64;
version : opt nat64;
bn_id : opt text;
};
type DelDoc = record { version : opt nat64 };
Expand Down
4 changes: 3 additions & 1 deletion src/tests/satellite.custom-domains.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { _SERVICE as SatelliteActor } from '$declarations/satellite/satelli
import { idlFactory as idlFactorSatellite } from '$declarations/satellite/satellite.factory.did';
import { AnonymousIdentity } from '@dfinity/agent';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { toNullable } from '@dfinity/utils';
import { fromNullable, toNullable } from '@dfinity/utils';
import { PocketIc, type Actor } from '@hadronous/pic';
import { afterAll, beforeAll, describe, expect, inject } from 'vitest';
import { ADMIN_ERROR_MSG } from './constants/satellite-tests.constants';
Expand Down Expand Up @@ -76,13 +76,15 @@ describe('Satellite custom domains', () => {
expect(results[0][1].updated_at).toBeGreaterThan(0n);
expect(results[0][1].created_at).not.toBeUndefined();
expect(results[0][1].created_at).toBeGreaterThan(0n);
expect(fromNullable(results[0][1].version) ?? 0n).toBeGreaterThan(0n);

expect(results[1][0]).toEqual('test2.com');
expect(results[1][1].bn_id).toEqual([]);
expect(results[1][1].updated_at).not.toBeUndefined();
expect(results[1][1].updated_at).toBeGreaterThan(0n);
expect(results[1][1].created_at).not.toBeUndefined();
expect(results[1][1].created_at).toBeGreaterThan(0n);
expect(fromNullable(results[1][1].version) ?? 0n).toBeGreaterThan(0n);
});

it('should expose /.well-known/ic-domains', async () => {
Expand Down
Loading

0 comments on commit a76ccd3

Please sign in to comment.