@@ -16,6 +16,9 @@ use std::{
1616
1717use crate :: types;
1818
19+ const MESSAGE_PER_USER_GAS : u64 = 10000 ;
20+ const MESSAGE_PER_BYTE_GAS : u64 = 1000 ;
21+
1922type 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
2936impl 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
7382impl 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 }
0 commit comments