13、fabric node sdk1.4.8简单实现区块浏览器的其他功能

声明使用的版本

  • fabric版本如下:1.4.8
  • node-sdk版本:1.4.8
  • node版本:v8.11.1
  • ubuntu版本:16.04

本文要实现的区块浏览器功能

以下要查询的功能都可以在系统链码qscc中查询得到

  • GetChainInfo:获取链信息
  • GetBlockByNumer:按区块号获取区块数据
  • GetBlockByHash:按区块哈希获取区块数据
  • GetTransactionById:按交易ID获取交易数据
  • GetBlockByTxId:按交易ID获取区块数据

开始实现功能

qscc的概念

QSCC:Query System Chaincode,提供账本查询 API。如获取区块和交易等信息

使用高层api查询qscc

qscc其实是一种智能合约,当我们启动整个网络,系统就已经安装好了。qscc中智能合约函数一共有上面列出的几种,我们挑两个常用的来编写查询

得到调用invoke函数提交的交易id

通过id可以查询某一个交易的信息,首先我们需要修改一下之前的invoke函数,因为之前只是提交,而并没有去获取txid,但是txid又是查询交易比较重要的一个条件

// 调用链码
async function invoke(userName, channelName, chaincodeId, invokeName, ...args) {
    try {
        // 替换成普通用户
        const ccpPath = path.resolve(__dirname, "connect-sdk.json");
        const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
        const walletPath = path.join(process.cwd(), 'wallet');
        const wallet = new FileSystemWallet(walletPath);
        console.log(`Wallet path: ${walletPath}`);
        let checkUserResult = await userOpt.checkUser(wallet, userName)
        if(!checkUserResult) {
            // 从crypto-config文件夹中导出org admin的身份,保存到钱包中
            logger.info("user not register.")
            return
        }

        // Create a new gateway for connecting to our peer node.
		const gateway = new Gateway();
        await gateway.connect(ccp, { wallet, identity: userName, discovery: { enabled: true, asLocalhost: false } });
        //await gateway.connect(config.ccpPath, { wallet, identity: config.user, discovery: { enabled: false } });

        // Get the network (channel) our contract is deployed to.
        const network = await gateway.getNetwork(channelName);
        // Get the contract from the network.
        const contract = network.getContract(chaincodeId);
        // Submit the specified transaction.
		const transaction = await contract.createTransaction(invokeName)
		await transaction.submit(...args[0])
		console.log(transaction.getTransactionID()); // 这样就能拿到交易的id
		await contract.submitTransaction(invokeName, ...args[0]);
        console.log('Transaction has been submitted');
        // Disconnect from the gateway.
		await gateway.disconnect();
		return transaction.getTransactionID()
    } catch (error) {
        console.error(`Failed to submit transaction: ${error}`);
        process.exit(1);
    }
}

我们使用这个函数,得到的返回值就是交易的id

async function test2() {
	const channelName = "mychannel"
	const chaincodeId = "mycc"
	const orgAdminName = "admin1"
	const invokeName = "invoke"
	const invokeArgs = ["a", "b", "1000"]
	const txId = invoke(orgAdminName, channelName, chaincodeId, invokeName, invokeArgs)
	console.log(txId)
}

假设我们的txid为633dff8c5e123be03273b842598e5c890158a73a45cfeb3c353a70ed2d330b50,那么我们来完成下面的两个功能

调用智能合约中GetTransactionByID函数

通过txId可以查询到交易信息,交易信息被封装起来,存放在block中,node sdk给我们提供了一个解开block的类,block类的结构如下

header
	number -- {int}
	previous_hash -- {byte[]}
	data_hash -- {byte[]}
data
	data -- {array}
		signature -- {byte[]}
		payload
			header -- {Header}
			data -- {ConfigEnvelope | Transaction}
metadata
	metadata -- {array} #each arra
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值