Skip to content

Commit

Permalink
feat: burn gas in 'ic_message_channel'
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 1, 2024
1 parent e77440c commit 33dbbab
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/ic_message/ic_message.did
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ChannelECDHInput = record {
type ChannelInfo = record {
id : nat32;
dek : blob;
gas : nat64;
updated_at : nat64;
members : vec principal;
managers : vec principal;
Expand Down
4 changes: 4 additions & 0 deletions src/ic_message_channel/ic_message_channel.did
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type CanisterStatusType = variant { stopped; stopping; running };
type ChainArgs = variant { Upgrade : UpgradeArgs; Init : InitArgs };
type ChannelBasicInfo = record {
id : nat32;
gas : nat64;
updated_at : nat64;
name : text;
paid : nat64;
Expand All @@ -38,6 +39,7 @@ type ChannelECDHInput = record {
type ChannelInfo = record {
id : nat32;
dek : blob;
gas : nat64;
updated_at : nat64;
members : vec principal;
managers : vec principal;
Expand Down Expand Up @@ -108,8 +110,10 @@ type Result_8 = variant { Ok : vec Message; Err : text };
type Result_9 = variant { Ok : vec record { nat32; nat32 }; Err : text };
type StateInfo = record {
channel_id : nat32;
incoming_gas : nat;
managers : vec principal;
name : text;
burned_gas : nat;
channels_total : nat64;
messages_total : nat64;
};
Expand Down
2 changes: 2 additions & 0 deletions src/ic_message_channel/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ fn get_state() -> Result<types::StateInfo, String> {
name: s.name.clone(),
managers: s.managers.clone(),
channel_id: s.channel_id,
incoming_gas: s.incoming_gas,
burned_gas: s.burned_gas,
channels_total: store::channel::channels_total(),
messages_total: store::channel::messages_total(),
}))
Expand Down
40 changes: 29 additions & 11 deletions src/ic_message_channel/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::{

use crate::types;

const MESSAGE_PER_USER_GAS: u64 = 10000;
const MESSAGE_PER_BYTE_GAS: u64 = 1000;

type Memory = VirtualMemory<DefaultMemoryImpl>;

#[derive(Clone, Default, Deserialize, Serialize)]
Expand All @@ -24,6 +27,10 @@ pub struct State {
pub managers: BTreeSet<Principal>,
pub channel_id: u32,
pub user_channels: HashMap<Principal, BTreeMap<u32, u32>>,
#[serde(default)]
pub incoming_gas: u128,
#[serde(default)]
pub burned_gas: u128,
}

impl Storable for State {
Expand Down Expand Up @@ -68,6 +75,8 @@ pub struct Channel {
pub latest_message_by: Principal,
#[serde(rename = "pa")]
pub paid: u64,
#[serde(rename = "g")]
pub gas: u64,
}

impl Channel {
Expand Down Expand Up @@ -102,6 +111,7 @@ impl Channel {
latest_message_by: self.latest_message_by,
updated_at: self.updated_at,
paid: self.paid,
gas: self.gas,
my_setting,
}
}
Expand Down Expand Up @@ -349,18 +359,16 @@ pub mod channel {
if mid.1 == u32::MAX {
ic_cdk::trap("message id overflow");
}
let message = Message {
kind: 1,
reply_to: 0,
created_at: now_ms,
created_by: caller,
payload: to_cbor_bytes(&message).into(),
};

MESSAGE_STORE.with(|r| {
r.borrow_mut().insert(
mid,
Message {
kind: 1,
reply_to: 0,
created_at: now_ms,
created_by: caller,
payload: to_cbor_bytes(&message).into(),
},
);
r.borrow_mut().insert(mid, message);
});
}

Expand All @@ -370,6 +378,7 @@ pub mod channel {
now_ms: u64,
) -> Result<types::ChannelInfo, String> {
let id = state::with_mut(|s| {
s.incoming_gas = s.incoming_gas.saturating_add(input.paid as u128);
s.channel_id = s.channel_id.saturating_add(1);
s.channel_id
});
Expand Down Expand Up @@ -408,6 +417,7 @@ pub mod channel {
latest_message_at: 1,
latest_message_by: caller,
paid: input.paid,
gas: input.paid,
updated_at: now_ms,
};

Expand Down Expand Up @@ -541,10 +551,17 @@ pub mod channel {
if v.latest_message_at == u32::MAX {
Err("message id overflow".to_string())?;
}

let gas = MESSAGE_PER_USER_GAS * (v.managers.len() + v.members.len()) as u64
+ MESSAGE_PER_BYTE_GAS * msg.payload.len() as u64;
if v.gas < gas {
Err("not enough gas".to_string())?;
}
v.gas = v.gas.saturating_sub(gas);
v.latest_message_by = msg.created_by;
let mid = v.latest_message_at;
state::with_mut(|s| {
s.burned_gas = s.burned_gas.saturating_add(gas as u128);

for (p, c) in v.managers.iter_mut() {
if p != &msg.created_by {
c.unread += 1;
Expand Down Expand Up @@ -612,6 +629,7 @@ pub mod channel {
latest_message_at: v.latest_message_at,
latest_message_by: v.latest_message_by,
paid: v.paid,
gas: v.gas,
my_setting: my_setting.to_owned().into(),
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/ic_message_channel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ pub struct StateInfo {
pub channel_id: u32,
pub channels_total: u64,
pub messages_total: u64,
pub incoming_gas: u128,
pub burned_gas: u128,
}
2 changes: 2 additions & 0 deletions src/ic_message_types/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct ChannelInfo {
pub created_by: Principal,
pub updated_at: u64,
pub paid: u64,
pub gas: u64,
pub message_start: u32,
pub latest_message_at: u32,
pub latest_message_by: Principal,
Expand All @@ -44,6 +45,7 @@ pub struct ChannelBasicInfo {
pub latest_message_at: u32,
pub latest_message_by: Principal,
pub paid: u64,
pub gas: u64,
pub my_setting: ChannelSetting,
}

Expand Down

0 comments on commit 33dbbab

Please sign in to comment.