Skip to content

Commit

Permalink
FABN-1519: Refactor of full and private event factory (#190)
Browse files Browse the repository at this point in the history
Simplification of factory code for creating private events.

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored Mar 19, 2020
1 parent 1f446c6 commit 9560c41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
25 changes: 25 additions & 0 deletions fabric-network/src/impl/event/fullcontracteventfactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2020 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

import { ContractEvent, TransactionEvent } from '../../events';

export function newFullContractEvents(transactionEvent: TransactionEvent): ContractEvent[] {
const transactionActions: any[] = transactionEvent.transactionData.actions || [];
return transactionActions.map((transactionAction) => {
const chaincodeEvent = transactionAction.payload.action.proposal_response_payload.extension.events;
return newFullContractEvent(transactionEvent, chaincodeEvent);
});
}

function newFullContractEvent(transactionEvent: TransactionEvent, chaincodeEvent: any): ContractEvent {
const contractEvent: ContractEvent = {
chaincodeId: chaincodeEvent.chaincode_id,
eventName: chaincodeEvent.event_name,
payload: chaincodeEvent.payload,
getTransactionEvent: () => transactionEvent
};
return Object.freeze(contractEvent);
}
22 changes: 2 additions & 20 deletions fabric-network/src/impl/event/fulltransactioneventfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

import { Block } from 'fabric-common';
import { BlockEvent, ContractEvent, TransactionEvent } from '../../events';
import { BlockEvent, TransactionEvent } from '../../events';
import { cachedResult } from '../gatewayutils';
import { newFullContractEvents } from './fullcontracteventfactory';
import * as TransactionStatus from './transactionstatus';
import util = require('util');
// @ts-ignore no implicit any
import protos = require('fabric-protos');

Expand Down Expand Up @@ -48,21 +48,3 @@ export function newFullTransactionEvent(blockEvent: BlockEvent, txEnvelopeIndex:

return Object.freeze(transactionEvent);
}

function newFullContractEvents(transactionEvent: TransactionEvent): ContractEvent[] {
const transactionActions: any[] = transactionEvent.transactionData.actions || [];
return transactionActions.map((transactionAction) => {
const chaincodeEvent = transactionAction.payload.action.proposal_response_payload.extension.events;
return newFullContractEvent(transactionEvent, chaincodeEvent);
});
}

function newFullContractEvent(transactionEvent: TransactionEvent, chaincodeEvent: any): ContractEvent {
const contractEvent: ContractEvent = {
chaincodeId: chaincodeEvent.chaincode_id,
eventName: chaincodeEvent.event_name,
payload: chaincodeEvent.payload,
getTransactionEvent: () => transactionEvent
};
return Object.freeze(contractEvent);
}
21 changes: 5 additions & 16 deletions fabric-network/src/impl/event/privateblockeventfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
*/

import { Block, EventInfo, PrivateData } from 'fabric-common';
import { BlockEvent, TransactionEvent, ContractEvent } from '../../events';
import { BlockEvent, TransactionEvent } from '../../events';
import { cachedResult } from '../gatewayutils';
import { newFullBlockEvent } from './fullblockeventfactory';
import { newFullContractEvents } from './fullcontracteventfactory';
import { getTransactionEnvelopeIndexes, newFullTransactionEvent } from './fulltransactioneventfactory';

import util = require('util');
// @ts-ignore no implicit any
import protos = require('fabric-protos');

import util = require('util');

export function newPrivateBlockEvent(eventInfo: EventInfo): BlockEvent {
const privateData = eventInfo.privateData;
if (!privateData) {
Expand Down Expand Up @@ -46,20 +47,8 @@ function newPrivateTransactionEvent(blockEvent: BlockEvent, index: number, priva
isValid: fullTransactionEvent.isValid,
privateData,
getBlockEvent: () => blockEvent,
getContractEvents: cachedResult(() => fullTransactionEvent.getContractEvents()
.map((contractEvent) => newPrivateContractEvent(contractEvent, privateTransactionEvent)))
getContractEvents: cachedResult(() => newFullContractEvents(privateTransactionEvent))
};

return Object.freeze(privateTransactionEvent);
}

function newPrivateContractEvent(contractEvent: ContractEvent, transactionEvent: TransactionEvent): ContractEvent {
const privateContractEvent: ContractEvent = {
chaincodeId: contractEvent.chaincodeId,
eventName: contractEvent.eventName,
payload: contractEvent.payload,
getTransactionEvent: () => transactionEvent
};

return Object.freeze(privateContractEvent);
}
5 changes: 4 additions & 1 deletion fabric-network/test/impl/event/contractlistener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ describe('contract event listener', () => {

function newPrivateEvent(blockNumber: number): EventInfo {
return Object.assign(newEvent(blockNumber), {
privateData: 'PRIVATE_DATA'
privateData: [
'PRIVATE_DATA'
]
});
}

Expand Down Expand Up @@ -469,5 +471,6 @@ describe('contract event listener', () => {
const [contractEvent] = await listener.completePromise;

assertCanNavigateEvents(contractEvent);
expect(contractEvent.getTransactionEvent().privateData).to.equal(event.privateData[0]);
});
});

0 comments on commit 9560c41

Please sign in to comment.