Skip to content

Commit

Permalink
Remove deprecated events storage
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Mar 25, 2024
1 parent 2fd9ee6 commit 06d423b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 80 deletions.
35 changes: 5 additions & 30 deletions rs/canister/impl/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ use ic_stable_structures::{
};

const UPGRADES: MemoryId = MemoryId::new(0);
const EVENTS_V2_INDEX: MemoryId = MemoryId::new(1);
const EVENTS_V2_DATA: MemoryId = MemoryId::new(2);
const EVENTS_INDEX: MemoryId = MemoryId::new(3);
const EVENTS_DATA: MemoryId = MemoryId::new(4);
const STRING_TO_NUM_MAP: MemoryId = MemoryId::new(5);
const NUM_TO_STRING_INDEX: MemoryId = MemoryId::new(6);
const NUM_TO_STRING_DATA: MemoryId = MemoryId::new(7);
const STRING_TO_NUM_V2_MAP: MemoryId = MemoryId::new(8);
const NUM_TO_STRING_V2_INDEX: MemoryId = MemoryId::new(9);
const NUM_TO_STRING_V2_DATA: MemoryId = MemoryId::new(10);
const EVENTS_INDEX: MemoryId = MemoryId::new(1);
const EVENTS_DATA: MemoryId = MemoryId::new(2);
const STRING_TO_NUM_MAP: MemoryId = MemoryId::new(8);
const NUM_TO_STRING_INDEX: MemoryId = MemoryId::new(9);
const NUM_TO_STRING_DATA: MemoryId = MemoryId::new(10);

pub type Memory = VirtualMemory<DefaultMemoryImpl>;

Expand Down Expand Up @@ -46,26 +41,6 @@ pub fn get_num_to_string_data_memory() -> Memory {
get_memory(NUM_TO_STRING_DATA)
}

pub fn get_events_v2_index_memory() -> Memory {
get_memory(EVENTS_V2_INDEX)
}

pub fn get_events_v2_data_memory() -> Memory {
get_memory(EVENTS_V2_DATA)
}

pub fn get_string_to_num_v2_map_memory() -> Memory {
get_memory(STRING_TO_NUM_V2_MAP)
}

pub fn get_num_to_string_v2_index_memory() -> Memory {
get_memory(NUM_TO_STRING_V2_INDEX)
}

pub fn get_num_to_string_v2_data_memory() -> Memory {
get_memory(NUM_TO_STRING_V2_DATA)
}

fn get_memory(id: MemoryId) -> Memory {
MEMORY_MANAGER.with(|m| m.get(id))
}
13 changes: 1 addition & 12 deletions rs/canister/impl/src/model/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::memory::{
get_events_data_memory, get_events_index_memory, get_events_v2_data_memory,
get_events_v2_index_memory, Memory,
};
use crate::memory::{get_events_data_memory, get_events_index_memory, Memory};
use crate::model::string_to_num_map::StringToNumMap;
use candid::Deserialize;
use event_store_canister::{Anonymizable, IdempotentEvent, IndexedEvent, TimestampMillis};
Expand All @@ -18,14 +15,6 @@ pub struct Events {
}

impl Events {
pub fn new_v2() -> Self {
Events {
events: StableLog::init(get_events_v2_index_memory(), get_events_v2_data_memory())
.unwrap(),
string_to_num_map: StringToNumMap::new_v2(),
}
}

pub fn get(&self, start: u64, length: u64) -> Vec<IndexedEvent> {
self.events
.iter()
Expand Down
16 changes: 2 additions & 14 deletions rs/canister/impl/src/model/string_to_num_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::memory::{
get_num_to_string_data_memory, get_num_to_string_index_memory,
get_num_to_string_v2_data_memory, get_num_to_string_v2_index_memory,
get_string_to_num_map_memory, get_string_to_num_v2_map_memory, Memory,
get_num_to_string_data_memory, get_num_to_string_index_memory, get_string_to_num_map_memory,
Memory,
};
use ic_stable_structures::{StableBTreeMap, StableLog};

Expand All @@ -11,17 +10,6 @@ pub struct StringToNumMap {
}

impl StringToNumMap {
pub fn new_v2() -> Self {
StringToNumMap {
string_to_num: StableBTreeMap::init(get_string_to_num_v2_map_memory()),
num_to_string: StableLog::init(
get_num_to_string_v2_index_memory(),
get_num_to_string_v2_data_memory(),
)
.unwrap(),
}
}

pub fn convert_to_num(&mut self, string: String) -> u32 {
if let Some(i) = self.string_to_num.get(&string) {
i
Expand Down
18 changes: 7 additions & 11 deletions rs/canister/impl/src/queries/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ use ic_cdk::query;

#[query(guard = "caller_can_read_events")]
fn events(args: EventsArgs) -> EventsResponse {
state::read(|s| {
let stats = s.events().stats();
let events = s.events().get(args.start, args.length);

EventsResponse {
events,
latest_event_index: stats.latest_event_index,
}
})
events_inner(args)
}

#[query(guard = "caller_can_read_events")]
fn events_v2(args: EventsArgs) -> EventsResponse {
events_inner(args)
}

fn events_inner(args: EventsArgs) -> EventsResponse {
state::read(|s| {
let stats = s.events_v2().stats();
let events = s.events_v2().get(args.start, args.length);
let stats = s.events().stats();
let events = s.events().get(args.start, args.length);

EventsResponse {
events,
Expand Down
11 changes: 1 addition & 10 deletions rs/canister/impl/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ thread_local! {
pub struct State {
push_events_whitelist: HashSet<Principal>,
read_events_whitelist: HashSet<Principal>,
#[serde(default)]
time_granularity: Option<Milliseconds>,
#[serde(skip)]
events: Events,
#[serde(skip, default = "Events::new_v2")]
events_v2: Events,
event_deduper: EventDeduper,
salt: Salt,
}
Expand Down Expand Up @@ -62,7 +59,6 @@ impl State {
read_events_whitelist,
time_granularity,
events: Events::default(),
events_v2: Events::new_v2(),
event_deduper: EventDeduper::default(),
salt: Salt::default(),
}
Expand All @@ -89,10 +85,6 @@ impl State {
&self.events
}

pub fn events_v2(&self) -> &Events {
&self.events_v2
}

pub fn set_salt(&mut self, salt: [u8; 32]) {
self.salt.set(salt);
}
Expand All @@ -105,8 +97,7 @@ impl State {
.saturating_sub(event.timestamp % granularity);
}

self.events.push(event.clone(), self.salt.get());
self.events_v2.push(event, self.salt.get());
self.events.push(event, self.salt.get());
}
}
}
2 changes: 1 addition & 1 deletion rs/consumer/agent_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl AgentRuntime {
) -> Result<EventsResponse, (i32, String)> {
match self
.agent
.query(&canister_id, "events_v2")
.query(&canister_id, "events")
.with_arg(candid::encode_one(args).unwrap())
.call()
.await
Expand Down
2 changes: 1 addition & 1 deletion rs/consumer/cdk_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl CdkRuntime {
canister_id: Principal,
args: EventsArgs,
) -> Result<EventsResponse, (i32, String)> {
match ic_cdk::call(canister_id, "events_v2", (args,)).await {
match ic_cdk::call(canister_id, "events", (args,)).await {
Ok((response,)) => Ok(response),
Err((code, msg)) => Err((code as i32, msg)),
}
Expand Down
4 changes: 4 additions & 0 deletions ts/consumer/compile-candid.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
SCRIPT=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT")
cd $SCRIPT_DIR

didc bind ./../../rs/canister/api/can.did -t ts > ./src/candid/types.d.ts
didc bind ./../../rs/canister/api/can.did -t js > ./src/candid/idl.js
2 changes: 1 addition & 1 deletion ts/consumer/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Client {
}

public async events(start: bigint, length: bigint): Promise<EventsResponse> {
const candid = await this.canister.events_v2({
const candid = await this.canister.events({
start,
length,
});
Expand Down

0 comments on commit 06d423b

Please sign in to comment.