Skip to content

Commit 33dbbab

Browse files
committed
feat: burn gas in 'ic_message_channel'
1 parent e77440c commit 33dbbab

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

src/ic_message/ic_message.did

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ChannelECDHInput = record {
2323
type ChannelInfo = record {
2424
id : nat32;
2525
dek : blob;
26+
gas : nat64;
2627
updated_at : nat64;
2728
members : vec principal;
2829
managers : vec principal;

src/ic_message_channel/ic_message_channel.did

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type CanisterStatusType = variant { stopped; stopping; running };
2323
type ChainArgs = variant { Upgrade : UpgradeArgs; Init : InitArgs };
2424
type ChannelBasicInfo = record {
2525
id : nat32;
26+
gas : nat64;
2627
updated_at : nat64;
2728
name : text;
2829
paid : nat64;
@@ -38,6 +39,7 @@ type ChannelECDHInput = record {
3839
type ChannelInfo = record {
3940
id : nat32;
4041
dek : blob;
42+
gas : nat64;
4143
updated_at : nat64;
4244
members : vec principal;
4345
managers : vec principal;
@@ -108,8 +110,10 @@ type Result_8 = variant { Ok : vec Message; Err : text };
108110
type Result_9 = variant { Ok : vec record { nat32; nat32 }; Err : text };
109111
type StateInfo = record {
110112
channel_id : nat32;
113+
incoming_gas : nat;
111114
managers : vec principal;
112115
name : text;
116+
burned_gas : nat;
113117
channels_total : nat64;
114118
messages_total : nat64;
115119
};

src/ic_message_channel/src/api_query.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fn get_state() -> Result<types::StateInfo, String> {
1212
name: s.name.clone(),
1313
managers: s.managers.clone(),
1414
channel_id: s.channel_id,
15+
incoming_gas: s.incoming_gas,
16+
burned_gas: s.burned_gas,
1517
channels_total: store::channel::channels_total(),
1618
messages_total: store::channel::messages_total(),
1719
}))

src/ic_message_channel/src/store.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use std::{
1616

1717
use crate::types;
1818

19+
const MESSAGE_PER_USER_GAS: u64 = 10000;
20+
const MESSAGE_PER_BYTE_GAS: u64 = 1000;
21+
1922
type Memory = VirtualMemory<DefaultMemoryImpl>;
2023

2124
#[derive(Clone, Default, Deserialize, Serialize)]
@@ -24,6 +27,10 @@ pub struct State {
2427
pub managers: BTreeSet<Principal>,
2528
pub channel_id: u32,
2629
pub user_channels: HashMap<Principal, BTreeMap<u32, u32>>,
30+
#[serde(default)]
31+
pub incoming_gas: u128,
32+
#[serde(default)]
33+
pub burned_gas: u128,
2734
}
2835

2936
impl Storable for State {
@@ -68,6 +75,8 @@ pub struct Channel {
6875
pub latest_message_by: Principal,
6976
#[serde(rename = "pa")]
7077
pub paid: u64,
78+
#[serde(rename = "g")]
79+
pub gas: u64,
7180
}
7281

7382
impl Channel {
@@ -102,6 +111,7 @@ impl Channel {
102111
latest_message_by: self.latest_message_by,
103112
updated_at: self.updated_at,
104113
paid: self.paid,
114+
gas: self.gas,
105115
my_setting,
106116
}
107117
}
@@ -349,18 +359,16 @@ pub mod channel {
349359
if mid.1 == u32::MAX {
350360
ic_cdk::trap("message id overflow");
351361
}
362+
let message = Message {
363+
kind: 1,
364+
reply_to: 0,
365+
created_at: now_ms,
366+
created_by: caller,
367+
payload: to_cbor_bytes(&message).into(),
368+
};
352369

353370
MESSAGE_STORE.with(|r| {
354-
r.borrow_mut().insert(
355-
mid,
356-
Message {
357-
kind: 1,
358-
reply_to: 0,
359-
created_at: now_ms,
360-
created_by: caller,
361-
payload: to_cbor_bytes(&message).into(),
362-
},
363-
);
371+
r.borrow_mut().insert(mid, message);
364372
});
365373
}
366374

@@ -370,6 +378,7 @@ pub mod channel {
370378
now_ms: u64,
371379
) -> Result<types::ChannelInfo, String> {
372380
let id = state::with_mut(|s| {
381+
s.incoming_gas = s.incoming_gas.saturating_add(input.paid as u128);
373382
s.channel_id = s.channel_id.saturating_add(1);
374383
s.channel_id
375384
});
@@ -408,6 +417,7 @@ pub mod channel {
408417
latest_message_at: 1,
409418
latest_message_by: caller,
410419
paid: input.paid,
420+
gas: input.paid,
411421
updated_at: now_ms,
412422
};
413423

@@ -541,10 +551,17 @@ pub mod channel {
541551
if v.latest_message_at == u32::MAX {
542552
Err("message id overflow".to_string())?;
543553
}
544-
554+
let gas = MESSAGE_PER_USER_GAS * (v.managers.len() + v.members.len()) as u64
555+
+ MESSAGE_PER_BYTE_GAS * msg.payload.len() as u64;
556+
if v.gas < gas {
557+
Err("not enough gas".to_string())?;
558+
}
559+
v.gas = v.gas.saturating_sub(gas);
545560
v.latest_message_by = msg.created_by;
546561
let mid = v.latest_message_at;
547562
state::with_mut(|s| {
563+
s.burned_gas = s.burned_gas.saturating_add(gas as u128);
564+
548565
for (p, c) in v.managers.iter_mut() {
549566
if p != &msg.created_by {
550567
c.unread += 1;
@@ -612,6 +629,7 @@ pub mod channel {
612629
latest_message_at: v.latest_message_at,
613630
latest_message_by: v.latest_message_by,
614631
paid: v.paid,
632+
gas: v.gas,
615633
my_setting: my_setting.to_owned().into(),
616634
});
617635
}

src/ic_message_channel/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ pub struct StateInfo {
1111
pub channel_id: u32,
1212
pub channels_total: u64,
1313
pub messages_total: u64,
14+
pub incoming_gas: u128,
15+
pub burned_gas: u128,
1416
}

src/ic_message_types/src/channel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct ChannelInfo {
2929
pub created_by: Principal,
3030
pub updated_at: u64,
3131
pub paid: u64,
32+
pub gas: u64,
3233
pub message_start: u32,
3334
pub latest_message_at: u32,
3435
pub latest_message_by: Principal,
@@ -44,6 +45,7 @@ pub struct ChannelBasicInfo {
4445
pub latest_message_at: u32,
4546
pub latest_message_by: Principal,
4647
pub paid: u64,
48+
pub gas: u64,
4749
pub my_setting: ChannelSetting,
4850
}
4951

0 commit comments

Comments
 (0)