版本比较

密钥

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

代码块
module rooch_demo::editor {
     use rooch::state_channel::{Self, StateChannel};
     
     const ERR_FILE_NOT_EXISTS: u64 = 5;
     
     public fun editing_document_leave(account: &signer, doc_id: u64) {
          let editor = borrow_global_mut<DocumentEditor>(EDITOR_ADDRESS); 
          let editing_doc = table::borrow<u64, EditingDocument>(&editor.editingDocuments, doc_id)
          state_channel::leave(account, editing_doc.channel_id);
     }
}

聊天合约,加入群聊聊天合约,离开群聊

代码块
module rooch_demo::chat {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun chat_session_leave(account: &signer, chat_group_id: u64) {
         let addr = signer::address_of(account);
  
         let chat_account = borrow_global_mut<ChatAccount>(addr);
         let session = chat_account_borrow_chat_session(chat_account, chat_group_id)
         state_channel::leave(account, session.state_channel_id);
     }
}

MoveCraft合约,加入地块MoveCraft合约,离开地块

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_leave(account: &signer, land_id: u64) {
         let addr = signer::address_of(account);
     
         let game_account = borrow_global_mut<GameAccount>(addr);
         let session = game_account_borrow_session(game_account, land_id);
         
         let inventory = state_channel::leave<Inventory>(account, session.state_channel_id);
         game_account_deposit_inventory(game_account, inventory)
     }
}

1.2.4 关闭状态通道

代码块
close_rooch::state_channel::close

类型参数:

类型参数

约束

描述

StateT

store

状态类型

...

业务逻辑:

关闭某个状态通道,同时触发状态通道结算。

示例:

协同编辑器,关闭状态通道

代码块
module rooch_demo::editor {
     use rooch::state_channel::{Self, StateChannel};
     
     const ERR_FILE_NOT_EXISTS: u64 = 5;
     
     public fun editing_document_close(account: &signer, doc_id: u64) {
          let editor = borrow_global_mut<DocumentEditor>(EDITOR_ADDRESS); 
          let editing_doc = table::borrow<u64, EditingDocument>(&editor.editingDocuments, doc_id)
          let doc = state_channel::close(account, editing_doc.channel_id);
          editing_document_destroy(editing_doc)
          document_destroy(doc) 
     }
}

聊天合约,关闭群聊

代码块
module rooch_demo::chat {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun chat_session_close(account: &signer, chat_group_id: u64) {
         let addr = signer::address_of(account);
  
         let chat_account = borrow_global_mut<ChatAccount>(addr);
         let session = chat_account_borrow_chat_session(chat_account, chat_group_id)
         let chat_group = state_channel::close(account, session.state_channel_id);
         chat_account_destroy(session)
         server_deposit_chat_group(server, chat_group)
     }
}

MoveCraft合约,关闭地块

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_close(account: &signer, land_id: u64) {
         let addr = signer::address_of(account);
     
         let game_account = borrow_global_mut<GameAccount>(addr);
         let session = world_borrow_session(game_account, land_id);
         
         let land = state_channel::close<Inventory>(account, session.state_channel_id);
         world_destroy_session(world, session)
         world_deposit_land(game_account, land)
     }
}

1.3 给P2P Node的接口

1.3.1 保持心跳

...