基于hyperledger联盟链的汽车轨迹追溯系统 (三)

Posted 计划是动力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于hyperledger联盟链的汽车轨迹追溯系统 (三)相关的知识,希望对你有一定的参考价值。

一、后端接口搭建

引入模块

引入之前封装的模块

const tool = require('./tool')
const influxdb = require('./influxdb')
module.exports = {
}

插入轨迹完整流程

插入轨迹,轨迹先流入大数据处理平台influxdb,根据算法hashFnc32a可以将上传的数据打包成唯一编码hash,然后hash上链,最后再刻入第一次的交易hsh,以便日后进行交易回溯

async insertTrail (ctx) {
		const queryBody = ctx.request.query
		const Trid = queryBody.Trid
        console.log(`Trid.....${Trid}`)
        const jsonStr = queryBody.jsonStr;
		console.log(`jsonStr.....${jsonStr}`)
		var DataHash = tool.hashFnv32a(jsonStr,true)
		console.log(`DataHash.....${DataHash}`)
		var trail = JSON.parse(jsonStr)
		var trailToBlockChain = {
			 username: trail.username,
			 userid : trail.userid,
			 devid : trail.devid,
			 devstr : trail.devstr,
			 trid : trail.trid,
			 datahash : DataHash,
			 tranhash : ""
		}
		var trailToBlockChainStr =  JSON.stringify(trailToBlockChain)
		// 执行数据流入influxdb
		await influxdb.writePoints(Trid, trail.userid, jsonStr)
		var tx_id = null
		const request = {
		        //targets : --- letting this default to the peers assigned to the channel
		        chaincodeId: 'trail',
		        fcn: 'insertTrail',
		        args : [Trid,trailToBlockChainStr],
		        chainId: 'mychannel',
		        txId: tx_id
		}
		
		TranHash = await tool.executeTranSaction(request)
		// 获得交易完成的hash
		console.log(`TranHash.....${TranHash}`)
		trailToBlockChain.tranhash = TranHash
		trailToBlockChainStr =  JSON.stringify(trailToBlockChain)
		console.log(`addHash....${trailToBlockChainStr}`)
		const nrequest = {
			//targets : --- letting this default to the peers assigned to the channel
			chaincodeId: 'trail',
			fcn: 'updateTrail',
			args : [Trid,trailToBlockChainStr],
			chainId: 'mychannel',
			txId: tx_id
	}
		await tool.executeTranSaction(nrequest)
		// 重新写入记录第一次插入的交易hash
		ctx.body = TranHash
	},

区块链验证

将取出的json字符串进行hash计算同区块链上链的DataHash进行比对,证明未被篡改

async blockChainExamine (ctx) {
		const queryBody = ctx.request.query
		const trid = queryBody.trid
        console.log(`trid.....${trid}`)
		console.log(trid)
		var tx_id = null
		    const request = {
		        chaincodeId: 'trail',
		        txId: tx_id,
		        fcn: 'searchOneTrail',
				args: [trid],
		}
		trailToBlockChainStr = await tool.excuteQuery(request)
		console.log(`trailToBlockChainStr.....${trailToBlockChainStr}`)
		var trailToBlockChain = JSON.parse(trailToBlockChainStr)
		var DataHash = trailToBlockChain.datahash
		console.log(`DataHash....${DataHash}`)
		var influxTrail = await influxdb.queryPointsByTrid(trid)
		// console.log(influxTrail[0])
		console.log(`influxTrail.jsonStr....${influxTrail[0].jsonStr}`)
		var influxDataHash = tool.hashFnv32a(influxTrail[0].jsonStr, true)
		console.log(`influxDataHash....${influxDataHash}`)
		if(influxDataHash === DataHash){
			ctx.body = "区块链hash验证成功,轨迹唯一hash编码为:"+DataHash
		}else {
			ctx.body = "区块链系统比对异常,该数据非原始数据,原数据唯一hash编码为:"+DataHash
		}
	}

封装区块信息查询

async searchBlockByTrid ( ctx ) {
		// 这个接口提供给用户,方便用户根据trid查询区块链的存证信息
		const queryBody = ctx.request.query;
		const trid = queryBody.trid;
		console.log(`trid.....${trid}`)
		var tx_id = null
		const request = {
			chaincodeId: 'trail',
			txId: tx_id,
			fcn: 'searchOneTrail',
			args: [trid],
				};
		var trailJson = await tool.excuteQuery(request);
		var trail = JSON.parse(trailJson);
		var blockDetail = await tool.excuteRecall(trail.tranhash)
		console.log("所在区块号为:"+blockDetail.header.number);
		console.log("所在区块hash为:"+blockDetail.header.data_hash);
		console.log("交易hash为为:"+blockDetail.data.data[0].payload.header.channel_header.tx_id);
		console.log("交易时间戳为为:"+blockDetail.data.data[0].payload.header.channel_header.timestamp);
		var json_date = new Date(blockDetail.data.data[0].payload.header.channel_header.timestamp).toJSON();
		var theTime = new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\\.[\\d]{3}Z/, '');
		var block = {
			transactionIndex: "汽车轨迹链",
			hash: blockDetail.data.data[0].payload.header.channel_header.tx_id,
			blockNumber: blockDetail.header.number,
			blockHash: blockDetail.header.data_hash,
			timestamp: theTime
			};
		ctx.body = block;
	}

查询

这里支持直接从influxdb时序数据库拿取时序数据(开放用户)、从区块链拿取DataHash数据(开放管理员)、查询区块链世界状态痕迹(开放管理员)、回溯交易信息(开放管理员)

async searchAllTrail ( ctx ) {
		// 这个接口开放给管理员,查看区块链上上链的轨迹信息
		var tx_id = null;
		    const request = {
		        chaincodeId: 'trail',
		        txId: tx_id,
		        fcn: 'searchAllTrail',
				args: [""]
		};
		ctx.body = await tool.excuteQuery(request)
	},
	async searchOneTrail ( ctx ) {
		// 这个接口开放给管理员,查看区块链上上链的轨迹信息
		const queryBody = ctx.request.query;
        const trid = queryBody.trid;
		console.log(`trid.....${trid}`)
		var tx_id = null
		    const request = {
		        chaincodeId: 'trail',
		        txId: tx_id,
		        fcn: 'searchOneTrail',
				args: [trid],
		};
		ctx.body = await tool.excuteQuery(request);
	},
	async updateTrail (ctx) {
		const queryBody = ctx.request.query;
		const Trid = queryBody.Trid;
        console.log(`Trid.....${Trid}`)
        const jsonStr = queryBody.jsonStr;
		console.log(`jsonStr.....${jsonStr}`)
		var DataHash = tool.hashFnv32a(jsonStr,true)
		console.log(`DataHash.....${DataHash}`)
		var trail = JSON.parse(jsonStr)
		var trailToBlockChain = {
			 username: trail.username,
			 userid : trail.userid,
			 devid : trail.devid,
			 devstr : trail.devstr,
			 trid : trail.trid,
			 datahash : DataHash,
			 tranhash : trail.tranhash
		}
		var trailToBlockChainStr =  JSON.stringify(trailToBlockChain)
		var tx_id = null;
		    const request = {
		        //targets : --- letting this default to the peers assigned to the channel
		        chaincodeId: 'trail',
		        fcn: 'updateTrail',
		        args : [Trid,trailToBlockChainStr],
		        chainId: 'mychannel',
		        txId: tx_id
		    };
        ctx.body = await tool.executeTranSaction(request)
	},
	async searchBlockByHash ( ctx ) {
		const queryBody = ctx.request.query;
        const hash = queryBody.hash;
        console.log(`hash.....${hash}`)
		ctx.body = await tool.excuteRecall(hash)
	},
	async searchTrailByTrid ( ctx ) {
		//开放给用户,支持用户的轨迹查询
		const queryBody = ctx.request.query;
		const trid = queryBody.trid;
		ctx.body = await influxdb.queryPointsByTrid(trid)[0]
	},
	async searchTrailByid ( ctx ) {
		//开放给用户,支持用户的轨迹查询
		const queryBody = ctx.request.query;
		const userid = queryBody.userid;
		ctx.body = await influxdb.queryPointsByUserid(userid)
	},
	async searchTrailFromBlockByid ( ctx ) {
		//开放给用户,支持用户的轨迹查询
		const queryBody = ctx.request.query;
		const userid = queryBody.userid;
		根据用户查询链上的轨迹信息
		var tx_id = null;
        console.log(`userid.....${userid}`)
			console.log(userid);
		    const request = {
		        chaincodeId: 'trail',
		        txId: tx_id,
		        fcn: 'searchTrailByid',
				args: [userid],
		    };
		ctx.body = await tool.excuteQuery(request);
	},
	async getHistoryForTrail ( ctx ) {
		const queryBody = ctx.request.query;
		const trid = queryBody.trid;
        console.log(`trid.....${trid}`)
		console.log(trid);
		var tx_id = null;
		    const request = {
		        chaincodeId: 'trail',
		        txId: tx_id,
		        fcn: 'getHistoryForTrail',
				args: [trid],
		    };
		ctx.body = await tool.excuteQuery(request)
	},

二、配置路由

const router = require('koa-router')()
const IndexController = require('./../controllers/index')

router
    // .get('/',IndexController.indexPage)
    .get('/insertTrail',IndexController.insertTrail)
    .get('/searchAllTrail',IndexController.searchAllTrail)
    .get('/searchOneTrail',IndexController.searchOneTrail)
    // .get('/deleteTrail',IndexController.deleteTrail)
    .get('/updateTrail',IndexController.updateTrail)
    .get('/searchBlockByHash',IndexController.searchBlockByHash)
    .get('/searchTrailByid',IndexController.searchTrailByid)
    .get('/getHistoryForTrail',IndexController.getHistoryForTrail)
    // .get('/getDeletedTrails',IndexController.getDeletedTrails)
    .get('/blockChainExamine',IndexController.blockChainExamine)
    .get('/searchTrailFromBlockByid',IndexController.searchTrailFromBlockByid)
    .get('/searchTrailByTrid',IndexController.searchTrailByTrid)
    .get('/searchBlockByTrid',IndexController.searchBlockByTrid)
module.exports = router

三、简单的测试调用

hash上链

hash上链
区块链校验

区块链校验
交易追溯

交易追溯
大数据流入时序数据库
大数据

取时序数据

取时序数据
历史世界状态回溯

history

以上是关于基于hyperledger联盟链的汽车轨迹追溯系统 (三)的主要内容,如果未能解决你的问题,请参考以下文章

基于hyperledger联盟链的汽车轨迹追溯系统

基于hyperledger联盟链的汽车轨迹追溯系统

基于hyperledger联盟链的汽车轨迹追溯系统

基于hyperledger联盟链的汽车轨迹追溯系统

基于hyperledger联盟链的汽车轨迹追溯系统

搭建基于hyperledger fabric的联盟社区 --升级chaincode