fisco bcosV3 Table智能合约开发

环境 : fisco bcos 3.11.0
webase-front : 3.1.1
console 3.8.0
table合约【3.2.0版本后的】

前言

最近在做毕设,数据的存储方式考虑使用fisco-bcos的table表存储,经过这几天的研究,发现对于fisco2fisco3版本的table表合约功能差异还是比较大的,比较起来V3的table合约功能性更丰富,更加的方便开发。

读者们要是没用过v3的链子,可以在fisco3 这里简单启动一条链子,Air版本的搭建和fisco2搭建的链子命令无多大差别【文章后面也有对应的控制台搭建命令,注意控制台2和3版本的链子不互通
然后就是webase-front要使用3.0以上的版本,链接在这里https://webasedoc.readthedocs.io/zh-cn/lab/docs/WeBASE-Install/developer.html

关于fisco3 的Table合约

不知道是不是webase-front 版本的问题,我并未在其代码仓库里找到Table.sol合约的文件,只有KVTable.sol合约。然后我去github的fisco仓库找到了fisco3的版本合约文件,还附带两个合约,都需要import进去才行
[更新一下,这些合约也可以在控制台3.8.0上的contracts/solidity文件夹上找到,注意:v3版本有两个Table合约,一个是3.2.0版本以上,一个是3.2.0以前的版本,我所有介绍的是3.2.0以上的,官方文档给的是3.2.0以前的例子]

Table.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
pragma experimental ABIEncoderV2;
import "./EntryWrapper.sol";

// KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字
enum KeyOrder {
   Lexicographic, Numerical}
struct TableInfo {
   
    KeyOrder keyOrder;
    string keyColumn;
    string[] valueColumns;
}

// 更新字段,用于update
struct UpdateField {
   
    string columnName;
    // 考虑工具类
    string value;
}

// 筛选条件,大于、大于等于、小于、小于等于
enum ConditionOP {
   GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS}
struct Condition {
   
    ConditionOP op;
    string field;
    string value;
}

// 数量限制
struct Limit {
   
    uint32 offset;
    // count limit max is 500
    uint32 count;
}

// 表管理合约,是静态Precompiled,有固定的合约地址
abstract contract TableManager {
   
    // 创建表,传入TableInfo
    function createTable(string memory path, TableInfo memory tableInfo) public virtual returns (int32);

    // 创建KV表,传入key和value字段名
    function createKVTable(string memory tableName, string memory keyField, string memory valueField) public virtual returns (int32);

    // 只提供给Solidity合约调用时使用
    function openTable(string memory path) public view virtual returns (address);

    // 变更表字段
    // 只能新增字段,不能删除字段,新增的字段默认值为空,不能与原有字段重复
    function appendColumns(string memory path, string[] memory newColumns) public virtual returns (int32);

    // 获取表信息
    function descWithKeyOrder(string memory tableName) public view virtual returns (TableInfo memory);
}

// 表合约,是动态Precompiled,TableManager创建时指定地址
abstract contract Table {
   
    // 按key查询entry
    function select(string memory key) public virtual view returns (Entry memory);

    // 按条件批量查询entry,condition为空则查询所有记录
    function select(Condition[] memory conditions, Limit memory limit) public virtual view returns (Entry[] memory);

    // 按照条件查询count数据
    function count(Condition[] memory conditions) public virtual view returns (uint32);

    // 插入数据
    function insert(Entry memory entry) public virtual returns (int32);

    // 按key更新entry
    function update(string memory key, UpdateField[] memory updateFields) public virtual returns (int32);

    // 按条件批量更新entry,condition为空则更新所有记录
    function update(Condition[] memory conditions, Limit memory limit, UpdateField[] memory updateFields) public virtual returns (int32);

    // 按key删除entry
    function remove(string memory key) public virtual returns (int32);
    // 按条件批量删除entry,condition为空则删除所有记录
    function remove(Condition[] memory conditions, Limit memory limit) public virtual returns (int32);
}

abstract contract KVTable {
   
    function get(string memory key) 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

已久依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值