Skip to content

Commit

Permalink
fix: fix setting key
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 27, 2024
1 parent 3b9b04f commit d8804db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ strip = true
opt-level = 's'

[workspace.package]
version = "0.5.1"
version = "0.5.2"
edition = "2021"
repository = "https://github.com/ldclabs/ic-cose"
keywords = ["config", "cbor", "canister", "icp", "encryption"]
Expand Down
22 changes: 15 additions & 7 deletions src/ic_cose_canister/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ impl SettingPathKey {
val.version,
)
}

pub fn v0(&self) -> SettingPathKey {
SettingPathKey(self.0.clone(), self.1, self.2.clone(), self.3.clone(), 0)
}
}

impl Storable for SettingPathKey {
Expand Down Expand Up @@ -714,7 +718,7 @@ pub mod ns {
return Ok(true);
}

let setting = SETTINGS_STORE.with_borrow(|m| m.get(spk));
let setting = SETTINGS_STORE.with_borrow(|m| m.get(&spk.v0()));
Ok(setting.map_or(false, |s| s.readers.contains(caller)))
})
.unwrap_or(false)
Expand Down Expand Up @@ -1053,7 +1057,7 @@ pub mod ns {
return Ok(None);
}

let setting = SETTINGS_STORE.with_borrow(|m| m.get(spk));
let setting = SETTINGS_STORE.with_borrow(|m| m.get(&spk.v0()));
Ok(setting.filter(|s| {
spk.4 <= s.version && (can == Some(true) || s.readers.contains(caller))
}))
Expand Down Expand Up @@ -1193,7 +1197,8 @@ pub mod ns {
Err("no permission".to_string())?;
}

SETTINGS_STORE.with_borrow_mut(|r| match r.get(spk) {
let spkv0 = spk.v0();
SETTINGS_STORE.with_borrow_mut(|r| match r.get(&spkv0) {
Some(mut setting) => {
if setting.version != spk.4 {
Err("version mismatch".to_string())?;
Expand All @@ -1204,7 +1209,7 @@ pub mod ns {

match f(&mut setting) {
Ok(rt) => {
r.insert(spk.clone(), setting);
r.insert(spkv0.clone(), setting);
Ok(rt)
}
Err(err) => Err(err),
Expand All @@ -1221,7 +1226,8 @@ pub mod ns {
Err("no permission".to_string())?;
}

SETTINGS_STORE.with_borrow_mut(|r| match r.get(spk) {
let spkv0 = spk.v0();
SETTINGS_STORE.with_borrow_mut(|r| match r.get(&spkv0) {
Some(setting) => {
if setting.version != spk.4 {
Err("version mismatch".to_string())?;
Expand All @@ -1230,7 +1236,7 @@ pub mod ns {
Err("readonly setting can not be deleted".to_string())?;
}

r.remove(spk);
r.remove(&spkv0);
if spk.4 > 1 {
PAYLOADS_STORE.with_borrow_mut(|rr| {
let mut pk = spk.clone();
Expand Down Expand Up @@ -1271,7 +1277,8 @@ pub mod ns {
size += dek.len();
}

let output = SETTINGS_STORE.with_borrow_mut(|r| match r.get(&spk) {
let spkv0 = spk.v0();
let output = SETTINGS_STORE.with_borrow_mut(|r| match r.get(&spkv0) {
Some(mut setting) => {
if setting.version != spk.4 {
Err("version mismatch".to_string())?;
Expand Down Expand Up @@ -1316,6 +1323,7 @@ pub mod ns {
setting.dek = Some(dek);
}

r.insert(spkv0, setting.clone());
Ok(UpdateSettingOutput {
created_at: setting.created_at,
updated_at: setting.updated_at,
Expand Down

0 comments on commit d8804db

Please sign in to comment.