fisco bcosV3 Table智能合约开发
环境 : fisco bcos 3.11.0
webase-front : 3.1.1
console 3.8.0
table合约【3.2.0版本后的】
前言
最近在做毕设,数据的存储方式考虑使用fisco-bcos的table表存储,经过这几天的研究,发现对于fisco2
和 fisco3
版本的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 500uint32 count;
}// 表管理合约,是静态Precompiled,有固定的合约地址
abstract contract TableManager {// 创建表,传入TableInfofunction 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查询entryfunction 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更新entryfunction 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删除entryfunction 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) public view virtual returns (bool, string memory);function set(string memory key, string memory value) public virtual returns (int32);
}
EntryWrapper.sol
这个类似于mybatisplus里面的wrapper条件构造器,用来进行附加查询条件使用,还有Entry用来做返回数据的数据结构
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
pragma experimental ABIEncoderV2;
import "./Cast.sol";// 记录,用于select和insert
struct Entry {string key;string[] fields; // 考虑2.0的Entry接口,临时Precompiled的问题,考虑加工具类接口
}contract EntryWrapper { Cast constant cast = Cast(address(0x100f)); Entry entry;constructor(Entry memory _entry) public {entry = _entry;}function setEntry(Entry memory _entry) public {entry = _entry;}function getEntry() public view returns(Entry memory) {return entry;}function fieldSize() public view returns (uint256) {return entry.fields.length;}function getInt(uint256 idx) public view returns (int256) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return cast.stringToS256(entry.fields[idx]);}function getUInt(uint256 idx) public view returns (uint256) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return cast.stringToU256(entry.fields[idx]);}function getAddress(uint256 idx) public view returns (address) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return cast.stringToAddr(entry.fields[idx]);}function getBytes64(uint256 idx) public view returns (bytes1[64] memory) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return bytesToBytes64(bytes(entry.fields[idx]));}function getBytes32(uint256 idx) public view returns (bytes32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return cast.stringToBytes32(entry.fields[idx]);}function getString(uint256 idx) public view returns (string memory) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");return entry.fields[idx];}function set(uint256 idx, int256 value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = cast.s256ToString(value);return 0;}function set(uint256 idx, uint256 value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = cast.u256ToString(value);return 0;}function set(uint256 idx, string memory value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = value;return 0;}function set(uint256 idx, address value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = cast.addrToString(value);return 0;}function set(uint256 idx, bytes32 value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = cast.bytes32ToString(value);return 0;}function set(uint256 idx, bytes1[64] memory value) public returns(int32) {require(idx >= 0 && idx < fieldSize(), "Index out of range!");entry.fields[idx] = string(bytes64ToBytes(value));return 0;}function setKey(string memory value) public {entry.key = value;}function getKey() public view returns (string memory) {return entry.key;}function bytes64ToBytes(bytes1[64] memory src) private pure returns(bytes memory) {bytes memory dst = new bytes(64);for(uint32 i = 0; i < 64; i++) {dst[i] = src[i][0];}return dst;}function bytesToBytes64(bytes memory src) private pure returns(bytes1[64] memory) {bytes1[64] memory dst;for(uint32 i = 0; i < 64; i++) {dst[i] = src[i];}return dst;}
}
Cast.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
pragma experimental ABIEncoderV2;abstract contract Cast {function stringToS256(string memory) public virtual view returns (int256);function stringToS64(string memory) public virtual view returns (int64);function stringToU256(string memory) public virtual view returns (uint256);function stringToAddr(string memory) public virtual view returns (address);function stringToBytes32(string memory) public virtual view returns (bytes32);function s256ToString(int256) public virtual view returns (string memory);function s64ToString(int64) public virtual view returns (string memory);function u256ToString(uint256) public virtual view returns (string memory);function addrToString(address) public virtual view returns (string memory);function bytes32ToString(bytes32) public virtual view returns (string memory);
}
编写TableTest
这里我直接用官网的例子代码进行测试,不过官网例子与实际的table合约有出入,需要进行修改
https://fisco-bcos-doc.readthedocs.io/zh-cn/latest/docs/contract_develop/c++_contract/use_crud_precompiled.html
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
pragma experimental ABIEncoderV2;
import "./Table.sol";contract TestTable{// 创建TableManager对象,其在区块链上的固定地址是0x1002
TableManager constant tm = TableManager(address(0x1002));
Table table;
string constant TABLE_NAME = "t_test";
constructor () public{// 创建t_test表,表的主键名为id,其他字段名为name和agestring[] memory columnNames = new string[](2);columnNames[0] = "name";columnNames[1] = "age";KeyOrder keyOrder;TableInfo memory tf = TableInfo(KeyOrder.Numerical,"id", columnNames);tm.createTable(TABLE_NAME, tf);// 获取真实的地址,存在合约中address t_address = tm.openTable(TABLE_NAME);require(t_address!=address(0x0),"");table = Table(t_address);
} function insert(string memory id,string memory name,string memory age) public returns (int32){string[] memory columns = new string[](2);columns[0] = name;columns[1] = age;Entry memory entry = Entry(id, columns);int32 result = table.insert(entry);// emit InsertResult(result);return result;
}function update(string memory id, string memory name, string memory age) public returns (int32){UpdateField[] memory updateFields = new UpdateField[](2);updateFields[0] = UpdateField("name", name);updateFields[1] = UpdateField("age", age);int32 result = table.update(id, updateFields);return result;
}function remove(string memory id) public returns(int32){int32 result = table.remove(id);return result;
}function select(string memory id) public view returns (string memory,string memory)
{Entry memory entry = table.select(id);string memory name;string memory age;if(entry.fields.length==2){name = entry.fields[0];age = entry.fields[1];}return (name,age);
}// enum ConditionOP {GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS}
// struct Condition {
// ConditionOP op;
// string field;
// string value;
// }
function selectMore(string memory age)publicviewreturns (Entry[] memory entries)
{Condition[] memory conds = new Condition[](1);Condition memory eq= Condition({op: ConditionOP.EQ, field: "age",value: age});conds[0] = eq;Limit memory limit = Limit({offset: 0, count: 100});entries = table.select(conds, limit);return entries;
}}
TableInfo的问题
实际的TableInfo 结构如下
// KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字
enum KeyOrder {Lexicographic, Numerical}
struct TableInfo {KeyOrder keyOrder;string keyColumn;string[] valueColumns;
}
是需要三个参数的,而官网的例子只用两个参数,缺少的第一个参数是枚举类,意思是你的主键是string类型还是数字类型,需要标明。
Condition的问题
实际的Condition结构如下
// 筛选条件,大于、大于等于、小于、小于等于
enum ConditionOP {GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS}
struct Condition {ConditionOP op;string field;string value;
}
官网的只有两个参数,缺少的那个参数是field,也就是此条件是要用在表的哪个字段时安个。
关于主键的问题
在fisco2的table表合约开发时候,因其的设计,导致主键是可以重复的。
但在测试fisco3的table表合约开发的时候,我用重复的主键添加多个数据,发现它遵守了主键唯一
的规则,有兴趣的读者可以去测试一下,特别是用TestTable的selectMore
,把field改成主键字段,可以测试到返回不了多个数据
结语
这段时间会继续开发v3的table合约,在开发的时候遇到的坑和新发现会持续更新到博客上
相关文章:
fisco bcosV3 Table智能合约开发
环境 : fisco bcos 3.11.0 webase-front : 3.1.1 console 3.8.0 table合约【3.2.0版本后的】 前言 最近在做毕设,数据的存储方式考虑使用fisco-bcos的table表存储,经过这几天的研究,发现对于fisco2和 fisco3版本的table表合约功能…...

leetcode刷题记录(四十八)——128. 最长连续序列
(一)问题描述 128. 最长连续序列 - 力扣(LeetCode)128. 最长连续序列 - 给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复…...

HTML中如何保留字符串的空白符和换行符号的效果
有个字符串 储值门店{{thing3.DATA}}\n储值卡号{{character_string1.DATA}}\n储值金额{{amount4.DATA}}\n当前余额{{amount5.DATA}}\n储值时间{{time2.DATA}} , HTML中想要保留 \n的换行效果的有下面3种方法: 1、style 中 设置 white-space: pre-lin…...

Linux入门——环境基础开发(上)
Linux 软件包管理器 yum 什么是软件包 在Linux操作系统中,安装软件的方式通常较为复杂,其基本流程涉及下载程序源代码并通过编译得到可执行程序。然而,这种方法需要开发者具备一定的编程知识和环境配置能力,对于许多用户而言&am…...
c++类和对象---下
文章目录 一、类的静态成员 1.1.静态成员变量:所有对象共享的成员变量。 1.2.静态成员函数:可以访问静态成员变量,但不能访问非静态成员变量。 二、类的继承 2.1.继承:子类继承父类的成员变量和成员函数。 2.2.多态:基…...
组件中的Props
在项目开发中,在开发某些界面时,我们可以将一些代码封装成组件来简化代码。但是,如果某些情况下组件中的某些属性不是一成不变的(比如一个头像+姓名的组件,每次使用时都需要改变其图像src和姓名字符串),我们就可以使用Props。 我们要使用Props,我们需要先在组件中声明…...
并行服务、远程SSH无法下载conda,报错404
原下载代码无效,报错404 wget -c https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh 使用下面代码下载 wget --user-agent"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12…...

迅为RK3568开发板篇OpenHarmony配置HDF驱动控制LED-新增 topeet子系统-编写 bundle.json文件
bundle.json 文件内容如下所示: 下面是对各个字段的解释: 1. name: "ohos/demos" - 这是组件或项目的名称,这里表示它属于 OHOS(OpenHarmony OS)生态系统下的一个名为"demos"的组件。 2. descri…...

深度剖析RabbitMQ:从基础组件到管理页面详解
文章目录 一、简介二、Overview2.1 Overview->Totals2.2 Overview->Nodesbroker的属性2.3 Overview->Churn statistics2.4 Overview->Ports and contexts2.5 Overview->Export definitions2.6 Overview->Import definitions 三、Connections连接的属性 四、C…...

usb通过hdc连接鸿蒙next的常用指令
参考官方 注册报名https://www.hiascend.com/developer/activities/details/44de441ef599450596131c8cb52f7f8c/signup?channelCodeS1&recommended496144 hdc-调试命令-调测调优-系统 - 华为HarmonyOS开发者https://developer.huawei.com/consumer/cn/doc/harmonyos-guid…...

【落羽的落羽 C语言篇】文件操作
文章目录 一、文件的概念和分类1. 概念和分类2. 文件名3. 数据文件 三、文件操作1. 文件的打开和关闭1.1 流1.2 文件指针1.3 文件的打开和关闭 2. 文件的顺序读写3. 文件的随机读写4. 文件读取的判定5. 文件缓冲区 一、文件的概念和分类 1. 概念和分类 文件是用来保存数据的。…...

RNN之:LSTM 长短期记忆模型-结构-理论详解-及实战(Matlab向)
0.前言 递归!循环神经网络Recurrent Neural Network 循环神经网络(又称递归神经网络,Recurrent Neural Network,RNN)。是一种用于处理序列数据的神经网络结构,具有记忆功能,能够捕捉序列中的时…...
战略与规划方法——深入解析波士顿矩阵(BCG Matrix):分析产品组合的关键工具
深入解析波士顿矩阵(BCG Matrix):分析产品组合的关键工具 在现代商业管理中,合理地分析和管理产品组合对于企业的成功至关重要。波士顿矩阵(BCG Matrix),又称为成长份额矩阵,是一种由波士顿咨询集团(Boston Consulting Group)在20世纪70年代提出的战略工具,用于帮助…...
【记录52】el-table-column 添加fixed属性 滚动条无法滑动
问题: el-table-column 添加fixed属性 滚动条无法滑动 使用element UI组件,用到el-table的el-table-column的fixed属性时,当滚动条长度小于固定列时,滚动条无法通过鼠标去点击滑动操作 原因 fixed是用来固定列的属性,其…...

晨辉面试抽签和评分管理系统之十:如何搭建自己的数据库服务器,使用本软件的网络版
晨辉面试抽签和评分管理系统(下载地址:www.chenhuisoft.cn)是公务员招录面试、教师资格考试面试、企业招录面试等各类面试通用的考生编排、考生入场抽签、候考室倒计时管理、面试考官抽签、面试评分记录和成绩核算的面试全流程信息化管理软件。提供了考生…...
主链和Layer2之间资产转移
主链和Layer2之间资产转移 主链和Layer2之间资产转移是实现Layer2技术的关键环节,以下是资产转移的流程、流行解决方案及原理: 资产从主链转移到Layer2 用户在主链上发起一笔交易,将资产发送到一个特定的智能合约地址,这个合约是主链与Layer2之间的桥梁。智能合约会锁定用…...

麒麟操作系统服务架构保姆级教程(十)rewrite跳转
如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情 我们访问一个网页的时候会遇到一些奇形怪状的url地址,想优化一下,看着顺眼一点,或者打开一个短视频软件想摸鱼刷一会视频,在打开界面的时候无意间按到…...

MySQL表的创建实验
创建并使用数据库mydb6_product 。 mysql> create database mydb6_product; Query OK, 1 row affected (0.01 sec)mysql> use mydb6_product; Database changed 新建employees表。 对于gender,有默认值意味着不为空,在建表时可以选择不写not nul…...

【高可用自动化体系】自动化体系
架构设计的愿景就是高可用、高性能、高扩展、高效率。为了实现架构设计四高愿景,需要实现自动化系统目标: 标准化。 流程自助化。 可视化:可观测系统各项指标、包括全链路跟踪。 自动化:ci/cd 自动化部署。 精细化:…...
总结SpringBoot项目中读取resource目录下的文件多种方法
系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目…...
[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?
🧠 智能合约中的数据是如何在区块链中保持一致的? 为什么所有区块链节点都能得出相同结果?合约调用这么复杂,状态真能保持一致吗?本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里…...

Linux 文件类型,目录与路径,文件与目录管理
文件类型 后面的字符表示文件类型标志 普通文件:-(纯文本文件,二进制文件,数据格式文件) 如文本文件、图片、程序文件等。 目录文件:d(directory) 用来存放其他文件或子目录。 设备…...
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

centos 7 部署awstats 网站访问检测
一、基础环境准备(两种安装方式都要做) bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats࿰…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建
华为云FlexusDeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色,华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型,能助力我们轻松驾驭 DeepSeek-V3/R1,本文中将分享如何…...

优选算法第十二讲:队列 + 宽搜 优先级队列
优选算法第十二讲:队列 宽搜 && 优先级队列 1.N叉树的层序遍历2.二叉树的锯齿型层序遍历3.二叉树最大宽度4.在每个树行中找最大值5.优先级队列 -- 最后一块石头的重量6.数据流中的第K大元素7.前K个高频单词8.数据流的中位数 1.N叉树的层序遍历 2.二叉树的锯…...
Java多线程实现之Thread类深度解析
Java多线程实现之Thread类深度解析 一、多线程基础概念1.1 什么是线程1.2 多线程的优势1.3 Java多线程模型 二、Thread类的基本结构与构造函数2.1 Thread类的继承关系2.2 构造函数 三、创建和启动线程3.1 继承Thread类创建线程3.2 实现Runnable接口创建线程 四、Thread类的核心…...

算法笔记2
1.字符串拼接最好用StringBuilder,不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>()); 3.去掉首尾空格...

短视频矩阵系统文案创作功能开发实践,定制化开发
在短视频行业迅猛发展的当下,企业和个人创作者为了扩大影响力、提升传播效果,纷纷采用短视频矩阵运营策略,同时管理多个平台、多个账号的内容发布。然而,频繁的文案创作需求让运营者疲于应对,如何高效产出高质量文案成…...

Qemu arm操作系统开发环境
使用qemu虚拟arm硬件比较合适。 步骤如下: 安装qemu apt install qemu-system安装aarch64-none-elf-gcc 需要手动下载,下载地址:https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x…...