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的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目…...

地震勘探——干扰波识别、井中地震时距曲线特点
目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波:可以用来解决所提出的地质任务的波;干扰波:所有妨碍辨认、追踪有效波的其他波。 地震勘探中,有效波和干扰波是相对的。例如,在反射波…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析
今天聊的内容,我认为是AI开发里面非常重要的内容。它在AI开发里无处不在,当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗",或者让翻译模型 "将这段合同翻译成商务日语" 时,输入的这句话就是 Prompt。…...

Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...
在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module
1、为什么要修改 CONNECT 报文? 多租户隔离:自动为接入设备追加租户前缀,后端按 ClientID 拆分队列。零代码鉴权:将入站用户名替换为 OAuth Access-Token,后端 Broker 统一校验。灰度发布:根据 IP/地理位写…...

HBuilderX安装(uni-app和小程序开发)
下载HBuilderX 访问官方网站:https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本: Windows版(推荐下载标准版) Windows系统安装步骤 运行安装程序: 双击下载的.exe安装文件 如果出现安全提示&…...

微信小程序云开发平台MySQL的连接方式
注:微信小程序云开发平台指的是腾讯云开发 先给结论:微信小程序云开发平台的MySQL,无法通过获取数据库连接信息的方式进行连接,连接只能通过云开发的SDK连接,具体要参考官方文档: 为什么? 因为…...

多种风格导航菜单 HTML 实现(附源码)
下面我将为您展示 6 种不同风格的导航菜单实现,每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...
全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比
目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec? IPsec VPN 5.1 IPsec传输模式(Transport Mode) 5.2 IPsec隧道模式(Tunne…...
Hive 存储格式深度解析:从 TextFile 到 ORC,如何选对数据存储方案?
在大数据处理领域,Hive 作为 Hadoop 生态中重要的数据仓库工具,其存储格式的选择直接影响数据存储成本、查询效率和计算资源消耗。面对 TextFile、SequenceFile、Parquet、RCFile、ORC 等多种存储格式,很多开发者常常陷入选择困境。本文将从底…...

嵌入式学习笔记DAY33(网络编程——TCP)
一、网络架构 C/S (client/server 客户端/服务器):由客户端和服务器端两个部分组成。客户端通常是用户使用的应用程序,负责提供用户界面和交互逻辑 ,接收用户输入,向服务器发送请求,并展示服务…...