From f86ec95726cdb9d015b0ca1d4d95d583050fa6b4 Mon Sep 17 00:00:00 2001 From: Varun Agarwal Date: Sun, 25 Aug 2019 14:19:02 +0530 Subject: [PATCH] [FAB-16390] Added filter for invalid transactions Signed-off-by: Varun Agarwal Change-Id: Idb6c57e047bba9a176b312f86d05c2ee21aeb175 --- off_chain_data/blockProcessing.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/off_chain_data/blockProcessing.js b/off_chain_data/blockProcessing.js index cac895a9b1..a5d2c61993 100644 --- a/off_chain_data/blockProcessing.js +++ b/off_chain_data/blockProcessing.js @@ -2,7 +2,7 @@ * Copyright IBM Corp. All Rights Reserved. * * SPDX-License-Identifier: Apache-2.0 - * + * */ 'use strict'; @@ -35,20 +35,35 @@ exports.processBlockEvent = async function (channelname, block, use_couchdb, nan const dataArray = block.data.data; + // transaction filter for each transaction in dataArray + const txSuccess = block.metadata.metadata[2]; + for (var dataItem in dataArray) { // reject if a timestamp is not set if (dataArray[dataItem].payload.header.channel_header.timestamp == undefined) { - reject(new Error('Block timestamp is not defined')); + reject(new Error('Transaction timestamp is not defined')); + } + + // tx may be rejected at commit stage by peers + // only valid transactions (code=0) update the word state and off-chain db + // filter through valid tx, refer below for list of error codes + // https://github.com/hyperledger/fabric-sdk-node/blob/release-1.4/fabric-client/lib/protos/peer/transaction.proto + if (txSuccess[dataItem] !== 0) { + continue(); } const timestamp = dataArray[dataItem].payload.header.channel_header.timestamp; - // reject if no actions are set + // continue to next tx if no actions are set if (dataArray[dataItem].payload.data.actions == undefined) { - break; + continue(); } + // actions are stored as an array. In Fabric 1.4.3 only one + // action exists per tx so we may simply use actions[0] + // in case Fabric adds support for multiple actions + // a for loop is used for demonstration const actions = dataArray[dataItem].payload.data.actions; // iterate through all actions @@ -79,7 +94,7 @@ exports.processBlockEvent = async function (channelname, block, use_couchdb, nan writeObject.timestamp = timestamp; writeObject.values = rwSet[record].rwset.writes; - console.log(`Block Timestamp: ${writeObject.timestamp}`); + console.log(`Transaction Timestamp: ${writeObject.timestamp}`); console.log(`ChaincodeID: ${writeObject.chaincodeid}`); console.log(writeObject.values); @@ -198,4 +213,4 @@ function isJSON(value) { return false; } return true; -} \ No newline at end of file +}