FISCO BCOS 区块链应用(五)结合WeBase开发区块链目录管理系统

2023-11-13

目录

前提条件及说明

1.1 搭建Fisco Bcos区块链底层平台

1.2 搭建java项目并引入 web3sdk

1.3 搭建WeBase区块链管理平台

应用开发

 1合约设计

2代码实现

3合约编译

4 java SDK集成

应用端对接

业务代码接入

应用界面效果


前提条件及说明

1.1 搭建Fisco Bcos区块链底层平台

官方地址:

FISCO BCOS 区块链 — FISCO BCOS v2.8.0 文档

打不开可以看看其他的2.2版本地址:

 版本及兼容 - 《FISCO BCOS 2.2 技术文档》 - 书栈网 · BookStack

本案例环境以fisco bcos2.4 , 双机, 单群组, 4机构 8节点为例.

1.2 搭建java项目并引入 web3sdk

        <dependency>
            <groupId>org.fisco-bcos</groupId>
            <artifactId>web3sdk</artifactId>
            <version>2.4.0</version>
            <scope>${dependency.scope}</scope>
        </dependency>

1.3 搭建WeBase区块链管理平台

官方地址:

WeBASE 技术文档 — WeBASE v1.5.4 文档

搭建到节点管理平台即可.即部署 节点前置服务+节点管理服务.

Fisco Bcos内置的案例居为0.4.x版本. 建议也使用该版本进行合约开发.

WeBase提供solidity开发IDE, 可以直接编写,编译合约代码,我们使用172.16.0.119是内网IP, 同学们使用需替换成自己部署的地址

http://172.16.0.119:5002/WeBASE-Front/#/contract

也推荐使用常用的在线IDE, remix

Remix - Ethereum IDE

基于Fisco Bcos能较为便捷的把指定的业务数据上链.

内置封装了Table.sol接口合约. 我们开发业务应用时可基于该合约接口, 以类似操作数据库DAO层的形式, 实现CRUD操作. 

常规使用了Table.sol中的table合约

 

contract TableFactory {
    function openTable(string memory) public view returns (Table); //open table
    function createTable(string, string, string) public returns (int256); //create table
}

其中createtable中的3个参数分别为 表名, 主键字段, 值字段,多个用逗号分割.

//Table main contract
contract Table {
    function select(string, Condition) public view returns (Entries);
    function insert(string, Entry) public returns (int256);
    function update(string, Entry, Condition) public returns (int256);
    function remove(string, Condition) public returns (int256);

    function newEntry() public view returns (Entry);
    function newCondition() public view returns (Condition);
}

  • Table合约的insert、remove、update和select函数中key的类型为string,其长度最大支持255字符。
  • Entry的get/set接口的key的类型为string,其长度最大支持255字符,value支持的类型有int256(int)、address和string,其中string的不能超过16MB

应用开发

 1合约设计

        系统功能开发都需要基于业务和一定的应用场景. 我们以已有的目录管理系统作为应用案例.  目录是数据采集、共享、交换的向导,通过收集维护各部门的数据目录, 最终形成某个全量的信用主体应用目录, 并可对外公开发布。  通过数据项、信息类的标准化、制订、编目,最终把目录信息上链。

 已基于业务表设计的table合约有数据项合约、信息类合约以及目录节点合约。

合约一旦部署发布,则不可删除, 则合约修改已新增的形式.

如信息类合约 UBcosInfoclassContract_V3.  需自行把 V1,V2版本的数据重新通过v3版本合约上链.  信息类合约是把传统数据库表中的数据用table合约用相同的结构把数据上链.

2代码实现

确定业务表上链的字段包括:

string rid, string clsname, string clscode,string clsenname,string reftype,string sourcedept,string remark

其中rid作为table key.  table表名暂定为: u_bcos_infoclass_v3

在构造函数里进行表的创建, 合约包含了 select,insert,update,remove 4个接口。 

UBcosInfoclassContract_V3 合约代码:

pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
import "./Table.sol";

contract UBcosInfoclassContract_V3 {
    event CreateResult(int256 count);
    event InsertResult(int256 count);
    event UpdateResult(int256 count);
    event RemoveResult(int256 count);

    TableFactory tableFactory;
    string constant TABLE_NAME = "u_bcos_infoclass_v3";
    constructor() public {
        tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
           //创建信息类合约表, 参数分别为: 
		 //TABLE_NAME:表名, 
		 //表关键字段:rid, 
		//表值字段:"clsname,clscode,clsenname,reftype,sourcedept,remark"
        tableFactory.createTable(TABLE_NAME, "rid", "clsname,clscode,clsenname,reftype,sourcedept,remark");
    }

    //select records
    function select(string rid)
        public
        view
        returns (string, string, string,string)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

        Condition condition = table.newCondition();

        Entries entries = table.select(rid, condition);
        string memory rid_bytes;
        string memory clsname_bytes;
        string memory clscode_bytes;
        string memory sourcedept_bytes;

        if(entries.size()>0){
            Entry entry = entries.get(0);
            rid_bytes = entry.getString("rid");
            clsname_bytes = entry.getString("clsname");
            clscode_bytes = entry.getString("clscode");
            sourcedept_bytes = entry.getString("sourcedept");
        }
        return (rid_bytes, clsname_bytes, clscode_bytes,sourcedept_bytes);
    }
    //insert records
    function insert(string rid, string clsname, string clscode,string clsenname,string reftype,string sourcedept,string remark)
        public
        returns (int256)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

        Entry entry = table.newEntry();
        entry.set("rid", rid);
        entry.set("clsname", clsname);
        entry.set("clscode", clscode);
        entry.set("clsenname", clsenname);
        entry.set("reftype", reftype);
        entry.set("sourcedept", sourcedept);
        entry.set("remark", remark);

        int256 count = table.insert(rid, entry);
        emit InsertResult(count);

        return count;
    }
    //update records
    function update(string rid, string clsname, string clscode,string clsenname,string reftype,string sourcedept,string remark)
        public
        returns (int256)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

        Entry entry = table.newEntry();
        entry.set("clsname", clsname);
        entry.set("clscode", clscode);
        entry.set("clsenname", clsenname);
        entry.set("reftype", reftype);
        entry.set("sourcedept", sourcedept);
        entry.set("remark", remark);

        Condition condition = table.newCondition();
        condition.EQ("rid", rid);
        

        int256 count = table.update(rid, entry, condition);
        emit UpdateResult(count);

        return count;
    }
    //remove records
    function remove(string rid ) public returns (int256) {
        Table table = tableFactory.openTable(TABLE_NAME);

        Condition condition = table.newCondition();
        condition.EQ("rid", rid);
        //condition.EQ("item_id", item_id);

        int256 count = table.remove(rid, condition);
        emit RemoveResult(count);

        return count;
    }
}

3合约编译

在WeBase结点管理服务中 新建合约, 并粘贴代码. 点击编译即可.

可以在IDE面板直接进行合约方法的调用

编译后可导出得到合约的java文件.  地层还是console控制台代码转换, Webase命令行操作的方式进行了可视化界面集成.

4 java SDK集成

 通过得到的UBcosInfoclassContract_V3.java文件

   对合约类进行接口封装

package com.dxx.blockchain.contract.transaction;

/**
 * 信息类合约交易接口
 * @author wuhaixin
 * 2020年6月14日
 */

public interface UBcosInfoclassContractV3Transaction {
    
    /**
     * 插入信息类,返回[blockNumber,trans_hash]
     * 2020年6月14日下午10:25:56
     * @param rid
     * @param clsname
     * @param clscode
     * @param clsenname
     * @param reftype
     * @param sourcedept
     * @param remark
     * @return
     * @throws Exception
     * @author wuhaixin
     */
    public String[] insertInfoClass(String rid,String clsname,String clscode,String clsenname,String reftype,String sourcedept,String remark) throws Exception;
    
    /**
     * 更新信息类.返回[blockNumber,trans_hash]
     * 2020年6月14日下午10:25:45
     * @param rid
     * @param clsname
     * @param clscode
     * @param clsenname
     * @param reftype
     * @param sourcedept
     * @param remark
     * @return
     * @throws Exception
     * @author wuhaixin
     */
    public String[] updateInfoClass(String rid,String clsname,String clscode,String clsenname,String reftype,String sourcedept,String remark) throws Exception;
    
    /**
     * 根据rid删除链上的记录返回[blockNumber,trans_hash]
     * 2020年6月14日下午10:25:27
     * @param rid
     * @return
     * @throws Exception
     * @author wuhaixin
     */
    public String[] removeInfoClass(String rid) throws Exception;
    
    /**
     * 返回 rid,clsname,clscode,sourcedept
     * 2020年6月14日下午10:25:13
     * @param rid
     * @return
     * @throws Exception
     * @author wuhaixin
     */
    public String[] selectInfoClass(String rid) throws Exception;

}

部分实现类代码

/**
 * 信息类合约交易实现
 * @author wuhaixin
 * 2020年6月14日
 */
@Component
public class UBcosInfoclassContractV3TransactionImpl implements UBcosInfoclassContractV3Transaction{


/**
     * 插入信息类,返回[blockNumber,trans_hash]
     * 2020年6月14日下午10:25:56
     * @param rid
     * @param clsname
     * @param clscode
     * @param clsenname
     * @param reftype
     * @param sourcedept
     * @param remark
     * @return
     * @throws Exception
     * @author wuhaixin
     */
    public String[] insertInfoClass(String rid,String clsname,String clscode,String clsenname,String reftype,String sourcedept,String remark) throws Exception{
        
        clsname = clsname==null?"":clsname;
        clscode = clscode==null?"":clscode;
        clsenname = clsenname==null?"":clsenname;
        reftype = reftype==null?"":reftype;
        sourcedept = sourcedept==null?"":sourcedept;
        remark = remark==null?"":remark;
        
        Web3j web3j = Web3jFactory.getWeb3j();
        Credentials credentials = CredentialsFactory.getCredentials();
        
        BigInteger gasPrice = new BigInteger("0");
        BigInteger gasLimit = new BigInteger("300000000");

        //可以使用deploy或者load函数初始化合约对象,两者使用场景不同,前者适用于初次部署合约,后者在合约已经部署并且已知合约地址时使用。
        //deploy每次会产生一个新的合约地址.
        //UBcosInfoclassContract_V3 contract = UBcosInfoclassContract_V3.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
        //load使用固定的合约地址.
        UBcosInfoclassContract_V3 contract = UBcosInfoclassContract_V3.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
        TransactionReceipt transactionReceipt = contract.insert(rid, clsname, clscode, clsenname, reftype, sourcedept, remark).send();
        String blockNumber = transactionReceipt.getBlockNumber().intValue()+"";
        String trans_hash = transactionReceipt.getTransactionHash();
        return new String[]{blockNumber,trans_hash};
    }

至此, 信息类合约接口已开发完毕, 可提供给应用系统使用.

应用端对接

业务代码接入

信息类业务应用代码如下:

主要是上链与保存区块信息

 String[] transResult = uBcosInfoclassContractV3Transaction.insertInfoClass(result.getId(), result.getClsname(), result.getClscode(), 
                    result.getClsenname(), result.getReftype(), result.getSourcedept(), result.getRemark());
/**
* 控制层顶部添加@RequestMapping("/infoclass")
* 编码人员根据实际选择资源授权的类型.admin
* 方法名采用动宾结构,uri请求则去除宾语,比如方法:addUser,则requestmapping("/add")
* 若是页面跳转,则与方法名一致,如addUser对应得jsp页面为addUser.jsp
* controller的入口界面文件除外.如userIndex.jsp
* 最终访问路径为: {应用名称}/infoclass/admin/*,{应用名称}/infoclass/usr/*,{应用名称}/infoclass/web/*,
* InfoClassController 控制器
* 2018年07月05日
* @author whx
*/
@Controller
@RequestMapping("/infoclass")
public class InfoClassController {

   /**注入信息类合约交易接口*/
    @Autowired
    UBcosInfoclassContractV3Transaction uBcosInfoclassContractV3Transaction;

  
/**
* 新增对象
* 2018年07月05日
* @param entity
* @author whx
*/
@Operator(operator="添加信息类")
@ResponseBody
@RequestMapping("/admin/addInfoClass")
public AjaxResponse addInfoClass(InfoClassDO entity){
	AjaxResponse resp = AjaxResponse.instance();
	resp.setSuccess(false);
	InfoClassDO result = infoClassService.saveAndCreateTable(entity);
	
	try {
	    result.setSourcedept(AuthedUserUtil.getUserAccount());
	
              String[] transResult = uBcosInfoclassContractV3Transaction.insertInfoClass(result.getId(), result.getClsname(), result.getClscode(), 
                    result.getClsenname(), result.getReftype(), result.getSourcedept(), result.getRemark());
            
            result.setBlockNumber(Integer.parseInt(transResult[0]));
            result.setTrans_hash(transResult[1]);
            infoClassService.update(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
	
	if(result!=null){
		resp.setSuccess(true);
		resp.setMessage("保存成功!");
	}else{
		resp.setMessage("信息类英文名称对应的数据库表已存在,无法新建信息类,请修改编码或使用生成功能!");
	}
	return resp;
}
}
}

应用界面效果

信息类管理模块增改时会插入区块链的块高和交易Hash

该区块交易信息也可以从区块链管理平台上查看.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FISCO BCOS 区块链应用(五)结合WeBase开发区块链目录管理系统 的相关文章

随机推荐

  • AI工具汇总

    大家好 我是可夫小子 关注AIGC 读书和自媒体 ChatGPT已经火了这么久 我也写不了少玩ChatGPT的方法 昨天OpenAI又推出了苹果手机的APP 我也介绍下载和安装的攻略 但根据读者反馈 仍然还是有许多同学没能用上 今天我就把我
  • VS2015远程编译Linux项目

    用VS2015开发Linux程序详细教程 配置篇 crazytea的博客 CSDN博客 linux 程序开发VS2015推出了跨平台开发 其中包括了对Linux程序开发的支持 最近刚好需要开发Linux程序 对其进行了一些研究 首先介绍下涉
  • redis的安装与配置

    第一章 redis 1 1redis的概述 1 2关系型数据库与非关系数据库 1 3关系型数据库和非关系型数据库区别 1 4redis优点与缺点 第二章redis的安装 2 1 YUM安装 2 2下载编译安装 2 2 1关闭防火墙 2 2
  • element左侧导航栏el-menu,菜单栏文字超出不折行问题

    在CSS样式中加上这些样式就可以了 el submenu title display flex align items center el submenu title span white space normal word break b
  • C# 面向对象05 StringBuilder的用法

    好处 相比普通的 string处理 提高了字符串的处理速度 注意点 使用时需要使用对象的方式 StringBuilder world new StringBuilder using System using System Diagnosti
  • SimpleDateFormat模式字符串格式

    SimpleDateFormat模式字符串 new SimpleDateFormat String parm parm为一个字符串 表示格式 时间模式 字母 时间元素 表示 示例 y 年 Year 1996 96 M 年中的月份 Month
  • PyTorch基础之模型保存与重载模块、可视化模块讲解(附源码)

    训练模型时 在众多训练好的模型中会有几个较好的模型 我们希望储存这些模型对应的参数值 避免后续难以训练出更好的结果 同时也方便我们复现这些模型 用于之后的研究 PyTorch提供了模型的保存与重载模块 包括torch save 和torch
  • jvm内存模型与垃圾回收(下)

    上篇地址 jvm内存模型与垃圾回收 上 1 垃圾回收相关算法 标记清除 标记整理 复制 这三个看上面的文章 1 1 分代收集算法 将不同生命周期的对象采用不同的收集方式 以便提高回收效率 一般是将Java堆分为新生代和老年代 这样可以根据各
  • 接口与实现

    public interface Computable 接口体包含常量和抽象方法 没有普通方法 int MAX 46 常量 int f int x 抽象方法 默认权限friendly public class China implement
  • 【C++】使用Windows操作系统的API在控制台输出绿色的文本

    2023年8月21日 周一下午 include
  • linux桌面远程控制,在LINUX下远程控制WIN桌面

    rdesktop 是个在veket下访问视窗系统 远程桌面的客户端程式 当前 rdesktop 所支持的 视窗系统 系列版本包括 NT 2000 XP 和2003 通过使用 rdesktop 所实现的远程桌面协议 RDP 你能在veket系
  • 企业微信群机器人开发

    准备工作 已经注册了有效的企业微信账号 并且在客户端上已经登录 现有或者新建有效的包含多名企业微信成员的群聊 创建群聊机器人 右键群聊 gt 管理聊天信息 gt 添加群机器人 使用群机器人 在终端某个群组添加机器人之后 创建者可以在机器人详
  • 【单片机笔记】STM32+ESP8266通过AT指令WIFI连接阿里云MQTT服务器

    上一篇使用USB转串口的方式通过ESP8266wifi模块的方式成功连接上了阿里云 现在就要通过单片机来替换电脑上位机了 这样单片机自动的去调用并发送串口数据更加方便 也更加符合一个产品的开发 板载的传感器有NTC温度 光强 这两个主要用来
  • 音频采样率、采样深度、占用字节数浅析

    1 从一个问题来看 16K采样率 16bit采样深度 20ms的数据共占用多少字节 想要解这个问题 首先就要明白采样率是什么 它的单位是什么 采样率 就是指音频在每秒的采样次数 采样多少个点 单位是赫兹 hz 在这里 尤其不要与比特率 bp
  • wxc-icon使用

  • Vue3中的computed函数详解

    计算属性是Vue中常用的一种方式 主要用于在模板中放置逻辑计算 方便开发者进行数据操作和展示 在Vue3中 计算属性依然是非常重要的一种功能 而computed函数则更加的方便计算属性的使用 本文将对Vue3中的computed函数进行详细
  • 修改约束(注意是否存在字段名的情况)

    添加字段名时添加约束 语法 alter table 表名 add 字段名 数据类型 约束名 注意 这个方法会让表中让所有address都变成 中国 表达有点不清晰 自己去试试此方法和其他方法就知道了 例如 alter table emp a
  • 汇编语言笔记-ARM架构基本寄存器

    文章目录 寄存器组 1 R0 R12 2 R13 3 R14 4 R15 特殊寄存器 程序状态寄存器 xPSR 中断 异常屏蔽寄存器 CONTROL寄存器 浮点寄存器 1 S0 S31和D0 D15 浮点状态和控制寄存器 FPSCR 浮点单
  • 玩转ChatGPT:Excel操作初探

    一 写在前面 首先还是让小Chat推销下自己 Excel 表格制作是个技术活 你掌握了吗 没关系 现在有了 ChatGPT 让 Excel 辅助操作变得更简单 再也不用苦恼于数据分析和整理了 让 ChatGPT 成为你的数据处理助手 让 E
  • FISCO BCOS 区块链应用(五)结合WeBase开发区块链目录管理系统

    目录 前提条件及说明 1 1 搭建Fisco Bcos区块链底层平台 1 2 搭建java项目并引入 web3sdk 1 3 搭建WeBase区块链管理平台 应用开发 1合约设计 2代码实现 3合约编译 4 java SDK集成 应用端对接