波场链/币安链/马蹄链2.0佛萨奇开发源码
2.0佛萨奇马蹄链/币安链/波场链dapp智能合约系统开发(详情及规则)
5.1.0. 部署调用合约编译完成后,将得到一个.wasm格式的合约文件,可将之部署到指定到长安链上,完成合约部署。 部署合约的使用教程可详见:部署示例合约。
5.1. 1迭代器使用示例点击此处查看接口说明
使用示例如下:
#[no_mangle] pub extern "C" fn how_to_use_iterator() { let ctx = &mut sim_context::get_sim_context(); // 构造数据 ctx.put_state("key1", "field1", "val".as_bytes()); ctx.put_state("key1", "field2", "val".as_bytes()); ctx.put_state("key1", "field23", "val".as_bytes()); ctx.put_state("key1", "field3", "val".as_bytes()); // 使用迭代器,能查出来 field1,field2,field23 三条数据 let r = ctx.new_iterator_with_field("key1", "field1", "field3"); if r.is_ok() { let rs = r.unwrap(); // 遍历 while rs.has_next() { // 获取下一行值 let row = rs.next_row().unwrap(); let key = row.get_string("key").unwrap(); let field = row.get_bytes("field"); let val = row.get_bytes("value"); // do something } // 关闭游标 rs.close(); } ctx.put_state("key2", "field1", "val".as_bytes()); ctx.put_state("key3", "field2", "val".as_bytes()); ctx.put_state("key33", "field2", "val".as_bytes()); ctx.put_state("key4", "field3", "val".as_bytes()); // 能查出来 key2,key3,key33 三条数据 ctx.new_iterator("key2", "key4"); // 能查出来 key3,key33 两条数据 ctx.new_iterator_prefix_with_key("key3"); // 能查出来 field2,field23 三条数据 ctx.new_iterator_prefix_with_key_field("key1", "field2"); ctx.put_state_from_key("key5","val".as_bytes()); ctx.put_state_from_key("key56","val".as_bytes()); ctx.put_state_from_key("key6","val".as_bytes()); // 能查出来 key5,key56 两条数据 ctx.new_iterator("key5", "key6"); }复制
5.1.2 Rust SDK API描述5.1.3. 下载 contract-sdk-rust 项目代码git clone https://git.chainmaker.org.cn/chainmaker/contract-sdk-rust -b v2.3.0复制
5.1.4. 项目文件说明tree -I target ├── Cargo.lock # 依赖版本信息 ├── Cargo.toml # 项目配置及依赖,参考:https://rustwasm.github.io/wasm-pack/book/cargo-toml-configuration.html ├── Makefile # build一个wasm文件 ├── README.md # 编译环境说明 ├── src │ ├── easycodec.rs # 序列化工具类 │ ├── lib.rs # 程序入口 │ ├── sim_context.rs # 合约SDK主要接口及实现 │ ├── sim_context_bulletproofs.rs # 合约SDK基于bulletproofs的范围证明接口实现 │ ├── sim_context_paillier.rs # 合约SDK基于paillier的半同态运算接口实现 │ ├── sim_context_rs.rs # 合约SDK sql接口实现 │ └── vec_box.rs # 内存管理类复制
5.1.5. 内置链交互接口用于链与SDK数据交互。
// 申请size大小内存,返回该内存的首地址 pub extern "C" fn allocate(size: usize) -> i32 {} // 释放某地址 pub extern "C" fn deallocate(pointer: *mut c_void) {} // 获取SDK运行时环境 pub extern "C" fn runtime_type() -> i32 { 0 }复制
5.1.6 用户与链交互接口/// SimContext is a interface with chainmaker interaction pub trait SimContext { // common method fn call_contract( &self, contract_name: &str, method: &str, param: EasyCodec, ) -> Result<Vec<u8>, result_code>; fn ok(&self, value: &[u8]) -> result_code; fn error(&self, body: &str) -> result_code; fn log(&self, msg: &str); fn arg(&self, key: &str) -> Result<Vec<u8>, String>; fn arg_as_utf8_str(&self, key: &str) -> String; fn args(&self) -> &EasyCodec; fn get_creator_org_id(&self) -> String; fn get_creator_pub_key(&self) -> String; fn get_creator_role(&self) -> String; fn get_sender_org_id(&self) -> String; fn get_sender_pub_key(&self) -> String; fn get_sender_role(&self) -> String; fn get_block_height(&self) -> u64; fn get_tx_id(&self) -> String; fn emit_event(&mut self, topic: &str, data: &Vec<String>) -> result_code; // paillier fn get_paillier_sim_context(&self) -> Box<dyn PaillierSimContext>; // bulletproofs fn get_bulletproofs_sim_context(&self) -> Box<dyn BulletproofsSimContext>; // sql fn get_sql_sim_context(&self) -> Box<dyn S>; // KV method fn get_state(&self, key: &str, field: &str) -> Result<Vec<u8>, result_code>; fn get_state_from_key(&self, key: &str) -> Result<Vec<u8>, result_code>; fn put_state(&self, key: &str, field: &str, value: &[u8]) -> result_code; fn put_state_from_key(&self, key: &str, value: &[u8]) -> result_code; fn delete_state(&self, key: &str, field: &str) -> result_code; fn delete_state_from_key(&self, key: &str) -> result_code; /// new_iterator range of [startKey, limitKey), front closed back open fn new_iterator( &self, start_key: &str, limit_key: &str, ) -> Result<Box<dyn ResultSet>, result_code>;