Skip to content

Commit

Permalink
[FAB-16496] Clean up exposed protobuf types
Browse files Browse the repository at this point in the history
Several of the stub and shim APIs return protobuf
types directly from the protobufjs parser:

Stub:

- getCreator() returns a protobufjs SerializedIdentity object
- getTransient() returns a Map<string, Buffer> that we build now, rather than protobufjs
- getSignedProposal() is crazy, and returns a hand crafted structure similar to a SignedProposal object, but with all of the payloads parsed
- getTxTimestamp() returns a protobufjs Timestamp object
- getBinding() should be OK but probably needs checking
- All calls that return iterators return either a protobufjs KV object, or a protobufjs KeyModification object

Shim:

- success() returns a Response object
- error() returns a Response object

By switching to protobufjs v6, all of these types
have changed. Names are camelCase rather than
underscore_sep_erated, and there are no getters or
setters.

This CR changes the TypeScript definitions to match
the new protobuf formats, and also changes the
structure of the "signed proposal" to match the
style of an actual SignedProposal.

Signed-off-by: Simon Stone <[email protected]>
Change-Id: I1d84512142ce57e8d089a901ebdc35434720b55d
  • Loading branch information
Simon Stone committed Sep 6, 2019
1 parent f499e22 commit 2c39852
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 89 deletions.
6 changes: 3 additions & 3 deletions fabric-shim/lib/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ class ChaincodeStub {
let signatureHeader;
try {
signatureHeader = fabprotos.common.SignatureHeader.decode(header.signatureHeader);
decodedSP.proposal.header.signature_header = {nonce: signatureHeader.nonce};
decodedSP.proposal.header.signatureHeader = {nonce: signatureHeader.nonce};
} catch (err) {
throw new Error(util.format('Decoding SignatureHeader failed: %s', err));
}

let creator;
try {
creator = fabprotos.msp.SerializedIdentity.decode(signatureHeader.creator);
decodedSP.proposal.header.signature_header.creator = creator;
decodedSP.proposal.header.signatureHeader.creator = creator;
this.creator = creator;
} catch (err) {
throw new Error(util.format('Decoding SerializedIdentity failed: %s', err));
Expand All @@ -188,7 +188,7 @@ class ChaincodeStub {
let channelHeader;
try {
channelHeader = fabprotos.common.ChannelHeader.decode(header.channelHeader);
decodedSP.proposal.header.channel_header = channelHeader;
decodedSP.proposal.header.channelHeader = channelHeader;
this.txTimestamp = channelHeader.timestamp;
} catch (err) {
throw new Error(util.format('Decoding ChannelHeader failed: %s', err));
Expand Down
1 change: 0 additions & 1 deletion fabric-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"dependencies": {
"@fidm/x509": "^1.2.1",
"@grpc/proto-loader": "^0.5.1",
"@types/google-protobuf": "^3.2.7",
"@types/node": "^8.9.4",
"ajv": "^6.5.5",
"fabric-contract-api": "unstable",
Expand Down
55 changes: 15 additions & 40 deletions fabric-shim/test/typescript/chaincode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { Shim,
StateQueryResponse,
KeyEndorsementPolicy,
ENDORSER_ROLES,
Timestamp
} from 'fabric-shim';

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';
import { LoggerInstance } from 'winston';

class TestTS implements ChaincodeInterface {
Expand Down Expand Up @@ -160,14 +160,10 @@ class TestTS implements ChaincodeInterface {
await historyQuery.close();
const done: boolean = historyNext.done;
const keyMod: Iterators.KeyModification = historyNext.value;
let isDelete: boolean = keyMod.is_delete;
isDelete = keyMod.getIsDelete();
let isDelete: boolean = keyMod.isDelete;
let timestamp: Timestamp = keyMod.timestamp;
timestamp = keyMod.getTimestamp();
let txid: string = keyMod.tx_id;
txid = keyMod.getTxId();
let value: Buffer = keyMod.value.toBuffer();
value = keyMod.getValue().toBuffer();
let txid: string = keyMod.txId;
let value: Uint8Array = keyMod.value;
}

async testStateQueryIterator(stateQuery: Iterators.StateQueryIterator) {
Expand All @@ -176,9 +172,7 @@ class TestTS implements ChaincodeInterface {
const done: boolean = stateNext.done;
const keyVal: Iterators.KV = stateNext.value;
let key: string = keyVal.key;
key = keyVal.getKey();
let val: Buffer = keyVal.value.toBuffer();
val = keyVal.getValue().toBuffer();
let val: Uint8Array = keyVal.value;
}

async testPrivateData(stub: ChaincodeStub): Promise<void> {
Expand Down Expand Up @@ -209,7 +203,7 @@ class TestTS implements ChaincodeInterface {
}
const iteratorHistory = stub.getHistoryForKey('key1');
for await (const res of iteratorHistory) {
const tx_id = res.tx_id;
const tx_id = res.txId;
}
}

Expand Down Expand Up @@ -272,44 +266,25 @@ class TestTS implements ChaincodeInterface {
}

testSignedProposal(proposal: ChaincodeProposal.SignedProposal) {
let prop: ChaincodeProposal.Proposal = proposal.proposal_bytes;
let sig: Buffer = proposal.signature;
prop = proposal.getProposalBytes();
sig = proposal.getSignature();
let prop: ChaincodeProposal.Proposal = proposal.proposal;
let sig: Uint8Array = proposal.signature;

let ext: Buffer = prop.extension;
ext = prop.getExtension();
let hdr: ChaincodeProposal.Header = prop.header;
hdr = prop.getHeader();
let payload: ChaincodeProposal.ChaincodeProposalPayload = prop.payload;
payload = prop.getPayload();

let cHdr: ChaincodeProposal.ChannelHeader = hdr.channel_header;
cHdr = hdr.getChannelHeader();
let sHdr: ChaincodeProposal.SignatureHeader = hdr.signature_header;
sHdr = hdr.getSignatureHeader();
let cHdr: ChaincodeProposal.ChannelHeader = hdr.channelHeader;
let sHdr: ChaincodeProposal.SignatureHeader = hdr.signatureHeader;

let chId: string = cHdr.channel_id;
chId = cHdr.getChannelId();
let chId: string = cHdr.channelId;
let epoch: number = cHdr.epoch;
epoch = cHdr.getEpoch();
ext = cHdr.extension;
ext = cHdr.getExtension();
let timestamp: Timestamp = cHdr.timestamp;
timestamp = cHdr.getTimestamp();
let hash: Buffer = cHdr.tls_cert_hash;
hash = cHdr.getTlsCertHash();
let txId: string = cHdr.tx_id;
txId = cHdr.getTxId();
let hash: Uint8Array = cHdr.tlsCertHash;
let txId: string = cHdr.txId;
let type: ChaincodeProposal.HeaderType = cHdr.type;
type = cHdr.getType();
let version: number = cHdr.version;
version = cHdr.getVersion();

let creator: SerializedIdentity = sHdr.creator;
creator = sHdr.getCreator();
let nonce: Buffer = sHdr.nonce;
nonce = sHdr.getNonce();
let nonce: Uint8Array = sHdr.nonce;

let input: Buffer = payload.input;
input = payload.getInput();
Expand All @@ -318,7 +293,7 @@ class TestTS implements ChaincodeInterface {
}

testQueryResponseMetadata(metadata: QueryResponseMetadata) {
const cnt: number = metadata.fetched_records_count;
const cnt: number = metadata.fetchedRecordsCount;
const bookmark: string = metadata.bookmark;
}

Expand Down
4 changes: 2 additions & 2 deletions fabric-shim/test/unit/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ describe('Stub', () => {
signature: 'some signature',
proposal: {
header: {
signature_header: {
signatureHeader: {
creator: 'some creator',
nonce: Buffer.from('some nonce')
},
channel_header: decodedChannelHeader
channelHeader: decodedChannelHeader
},
payload: decodedCCPP
}
Expand Down
60 changes: 17 additions & 43 deletions fabric-shim/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/
declare module 'fabric-shim' {

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';
import { EventEmitter } from 'events';
import { LoggerInstance } from 'winston';

interface ProtobufBytes {
toBuffer(force_copy?: boolean): Buffer;
toString(encoding?: string): string;
interface Timestamp {
seconds: number;
nanos: number;
}

interface ChaincodeResponse {
Expand Down Expand Up @@ -48,7 +47,7 @@ declare module 'fabric-shim' {
}

interface QueryResponseMetadata {
fetched_records_count: number;
fetchedRecordsCount: number;
bookmark: string;
}

Expand Down Expand Up @@ -153,21 +152,14 @@ declare module 'fabric-shim' {
interface KV {
namespace: string;
key: string;
value: ProtobufBytes;
getNamespace(): string;
getKey(): string;
getValue(): ProtobufBytes;
value: Uint8Array;
}

interface KeyModification {
is_delete: boolean;
value: ProtobufBytes;
isDelete: boolean;
value: Uint8Array;
timestamp: Timestamp;
tx_id: string;
getIsDelete(): boolean;
getValue(): ProtobufBytes;
getTimestamp(): Timestamp;
getTxId(): string;
txId: string;
}
}

Expand Down Expand Up @@ -207,52 +199,34 @@ declare module 'fabric-shim' {

export namespace ChaincodeProposal {
interface SignedProposal {
proposal_bytes: Proposal;
getProposalBytes(): Proposal;
signature: Buffer;
getSignature(): Buffer;
proposal: Proposal;
signature: Uint8Array;
}

interface Proposal {
header: Header;
getHeader(): Header;
payload: ChaincodeProposalPayload;
getPayload(): ChaincodeProposalPayload;
extension: Buffer;
getExtension(): Buffer;
}

interface Header {
channel_header: ChannelHeader;
getChannelHeader(): ChannelHeader;
signature_header: SignatureHeader;
getSignatureHeader(): SignatureHeader;
channelHeader: ChannelHeader;
signatureHeader: SignatureHeader;
}

interface ChannelHeader {
type: HeaderType;
getType(): HeaderType;
version: number;
getVersion(): number;
timestamp: Timestamp;
getTimestamp(): Timestamp;
channel_id: string;
getChannelId(): string;
tx_id: string;
getTxId(): string;
channelId: string;
txId: string;
epoch: number;
getEpoch(): number;
extension: Buffer;
getExtension(): Buffer;
tls_cert_hash: Buffer;
getTlsCertHash(): Buffer;
extension: Uint8Array;
tlsCertHash: Uint8Array;
}

interface SignatureHeader {
creator: SerializedIdentity;
getCreator(): SerializedIdentity;
nonce: Buffer;
getNonce(): Buffer;
nonce: Uint8Array;
}

interface ChaincodeProposalPayload {
Expand Down

0 comments on commit 2c39852

Please sign in to comment.