版本比较

密钥

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

...

参数:

参数名称

类型

描述

state

StateT

初始状态

config

Config

状态通道配置

返回值:

返回值

类型

能力

描述

state_channel_id

u256u64

状态通道ID

业务逻辑:

使用一个自定义的初始状态来创建一个状态通道。状态通道的合约代码通过状态的模块自动提取。

...

代码块
module rooch_demo::editor {
     use aptos_framework::aptos_coin::{Self, AptosCoin};
     use rooch::state_channel::{Self, StateChannel};
     
     const ERR_FILE_NOT_EXISTS: u64 = 5;
     const ERR_FILE_ALREADY_OPEN: u64 = 6;
     
     public entry fun document_editor_open(account: &signer, drive_address: address, file_id: u64) {
         assert!(drive_file_exists(drive_address, file_id), Errors::requires_address(ERR_FILE_NOT_EXISTS));
         
         let editor = borrow_global_mut<DocumentEditor>(EDITOR_ADDRESS);
         assert!(!table::contains(editor.editingDocuments, (drive_address, file_id)), Errors::requires_address(ERR_FILE_ALREADY_OPEN));
         
         let doc = document_create(account, drive_address, file_id)
         let cfg = state_channel::create_config(1); //加入状态通道需要质押1个AptosCoin
         let ch_id = state_channel::create<Document>create<Document,(),AptosCoin>(doc, cfg);
         
         let editing_doc = EditingDocument{
             drive_address: drive_address,
             file_id: file_id,
             state_channel_id: ch_id
         }
         
         table::upser(editor.editingDocuments, (drive_address, file_id), editing_doc)
     }
}

...

代码块
module rooch_demo::chat {
     use std::vector;
     use std::string::{String, utf8};
     
     use aptos_framework::aptos_coin::{Self, AptosCoin};
     use rooch::state_channel::{Self, StateChannel};
     
     public fun chat_session_create(account: &signer, server_address: address, chat_group_id: u64) {
         let addr = signer::address_of(account);
     
         let server = borrow_global_mut<Server>(server_address);
         let chat_account = borrow_global_mut<ChatAccount>(addr);
         let chat_group = server_withdraw_chat_group(server, chat_group_id);
         
         let ch_idcfg = state_channel::create<ChatGroup>create_config(0); //加入状态通道需要质押0个AptosCoin
         let ch_id = state_channel::create<ChatGroup,(),AptosCoin>(chat_group, cfg);
         
         let session = ChatSession{
             server_address: server_address,
             chat_group_id: chat_group_id,
             state_channel_id: ch_id
         }
         
         server_add_chat_session(server, copy session)
         chat_account_add_chat_session(chat_account, copy session)
     }
}

...

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use aptos_framework::aptos_coin::{Self, AptosCoin};
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_create(account: &signer, world_address: address, land_id: u64) {
         let addr = signer::address_of(account);
     
         let world = borrow_global_mut<World>(world_address);
         let game_account = borrow_global_mut<GameAccount>(addr);
         let land = world_withdraw_land(world, land_id);
         
         let ch_idcfg = state_channel::create<Land>create_config(land1); //加入状态通道需要质押1个AptosCoin
         let         let session = LandSession(settle_cap, ch_id) = state_channel::create<Land,(Hero,Inventory),AptosCoin>(land, cfg);
         
         let session = LandSession{
             world_address: world_address,
             land_id: land_id,
             state_channel_id: ch_id,
             settle_cap: settle_cap,
         }
         
         world_add_land_session(world, copy session)
         game_account_add_land_session(game_account, copy session)
     }
}

...

代码块
rooch::state_channel::join

类型参数:

类型参数

约束

描述

StateTAssets

store

状态类型资产类型

参数:

参数名称

类型

描述

sender

&signer

发起方

state_channel_id

u256u64

状态通道ID

assets

Assets

带入状态通道的资产

...

代码块
module rooch_demo::chat {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun chat_session_join(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::join<()>(account, session.state_channel_id, ());
     }
}

MoveCraft合约,加入地块

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_join(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 hero = game_account_withdraw_hero(game_account);
         let inventory = game_account_withdraw_inventory(game_account);
         state_channel::join<(Hero, Inventory)>(account, session.state_channel_id, (hero, inventory));
     }
}

...

代码块
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::leaveleave<()>(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::leaveleave<()>(account, session.state_channel_id);
     }
}

...

代码块
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 (hero, inventory) = state_channel::leave<Inventory>leave<(Hero, Inventory)>(account, session.state_channel_id);
         game_account_deposit_inventoryhero(game_account, hero)
         game_account_deposit_inventory(game_account, inventory)
     }
}

1.2.4 关闭状态通道

代码块
rooch::state_channel::close

...

代码块
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::closeclose<Document>(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::closeclose<ChatGroup>(account, session.state_channel_id);
         chat_account_destroy(session)
         server_deposit_chat_group(server, chat_group)
     }
}

...

代码块
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>close<Land>(account, session.state_channel_id);
         world_destroy_session(world, session)
         world_deposit_land(game_account, land)
     }
}

...

类型参数

约束

描述

StateT

store

状态类型

参数:

参数名称

类型

描述

sender

&signer

发起方

state_channel_id

u256u64

状态通道ID

peer_delays

[]PeerDelay

和其他节点的延迟

返回值:

业务逻辑:

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

...

更新状态通道成员的, lastAliveTime

调用示例:

协同编辑器

代码块
aptos move run \
  --function-id rooch::state_channel::createkeep_proposal_entry

类型参数:

...

类型参数

...

约束

...

描述

...

StateT

...

store

...

状态类型

...

ActionT

...

copy, store

...

提案的Action

参数:

...

参数名称

...

类型

...

描述

...

sender

...

signer

...

发起方

...

state_channel_id

...

u256

...

状态通道ID

...

action

...

ActionT

...

提案的动作

返回值:

业务逻辑:

发起一个提案。

支持的提案:

...

提案Action

...

提案描述

...

SettlementAction

...

结算

...

PunishAction

...

alive_entry \
  --type-arg=rooch_demo::editor::Document \
  --arg=1

聊天合约

代码块
aptos move run \
  --function-id rooch::state_channel::keep_alive_entry \
  --type-arg=rooch_demo::chat::ChatGroup \
  --arg=1

MoveCraft合约

代码块
aptos move run \
  --function-id rooch::state_channel::keep_alive_entry \
  --type-arg=rooch_demo::movecraft::Land \
  --arg=1

1.3.2 创建提案

代码块
rooch::state_channel::create_proposal_entry

类型参数:

类型参数

约束

描述

StateT

store

状态类型

ActionT

copy, store

提案的Action

参数:

参数名称

类型

描述

sender

signer

发起方

state_channel_id

u256

状态通道ID

action

ActionT

提案的动作

返回值:

业务逻辑:

发起一个提案。

支持的提案:

提案Action

提案描述

SettlementAction

结算

PunishAction

惩罚

调用示例:

协同编辑器

代码块
aptos move run \
  --function-id rooch::state_channel::create_proposal_entry \
  --type-arg=rooch_demo::editor::Document \
  --type-arg=rooch_demo::editor::SettlementDocument \
  --arg=1001
  --arg=1

聊天合约

代码块
aptos move run \
  --function-id rooch::state_channel::keep_alive_entry \
  --type-arg=rooch_demo::chat::ChatGroup \
  --arg=1

MoveCraft合约

代码块
aptos move run \
  --function-id rooch::state_channel::keep_alive_entry \
  --type-arg=rooch_demo::movecraft::Land \
  --arg=1

1.3.3 对提案进行投票

代码块
rooch::state_channel:vote_proposal_entry

...