Skip to content

Commit

Permalink
Updated SDK to correctly marshall/unmarshall new protocol type field.
Browse files Browse the repository at this point in the history
Implements the following:

* Adds ProtocolType enum both in JavaScript code and TypeScript type definitions
* Adds "bimap" dependency, a bidirectional map to keep track between Protobuf and JavaScript enums
  • Loading branch information
Kriys94 committed Jul 22, 2019
1 parent 13d5735 commit 0333d1a
Show file tree
Hide file tree
Showing 36 changed files with 472 additions and 188 deletions.
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

# gitlab options
PROTO_REPO=pkg
PROTO_DIST_DIR=proto
PROTO_DIST_DIR=types
PROTO_SRC_DIR=types
GITLAB_PATH=ConsenSys/client/fr/core-stack
PROTO_TAG=v0.6.1
PROTO_TAG=v0.7.0

protobuf: ## Generate protobuf stubs
@docker-compose -f scripts/docker-compose.yml up
@sh scripts/update-permissions.sh

import-proto:
rm -rf $(PROTO_REPO) $(PROTO_DIST_DIR)
git clone -b $(PROTO_TAG) --single-branch git@gitlab.com:$(GITLAB_PATH)/$(PROTO_REPO).git;
mkdir -p $(PROTO_DIST_DIR);
mv $(PROTO_REPO)/$(PROTO_SRC_DIR)/* $(PROTO_DIST_DIR);
rm -rf $(PROTO_REPO)/;
rm $(PROTO_DIST_DIR)/*/*.go;
@rm -rf $(PROTO_REPO) $(PROTO_DIST_DIR)
@git clone -b $(PROTO_TAG) --single-branch git@gitlab.com:$(GITLAB_PATH)/$(PROTO_REPO).git;
@mkdir -p $(PROTO_DIST_DIR);
@mv $(PROTO_REPO)/$(PROTO_SRC_DIR)/* $(PROTO_DIST_DIR);
@rm -rf $(PROTO_REPO)/;
@rm $(PROTO_DIST_DIR)/*/*.go;

57 changes: 20 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.3.1",
"description": "CoreStack SDK",
"main": "./lib/index.js",
"types": "./types/index.d.ts",
"types": "./typedefinitions/index.d.ts",
"scripts": {
"clean": "rimraf ./coverage ./lib",
"test": "cross-env NODE_ENV=test jest",
Expand Down Expand Up @@ -56,6 +56,7 @@
"webpack-node-externals": "~1.7.2"
},
"dependencies": {
"bimap": "0.0.15",
"google-protobuf": "~3.8.0",
"kafka-node": "~4.1.3",
"uuid": "~3.3.2",
Expand Down
7 changes: 7 additions & 0 deletions scripts/update-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for file in `find . -name *_pb.js -not -path "*/node_modules/*"`; do
echo "# $file"
sudo chown -R $USER $file
sudo chmod 775 $file
sudo chown -R $USER $(dirname "$file")
sudo chmod 775 $(dirname "$file")
done
Empty file modified src/broker/types/abi/abi_pb.js
100644 → 100755
Empty file.
Empty file modified src/broker/types/args/call_pb.js
100644 → 100755
Empty file.
Empty file modified src/broker/types/args/private_pb.js
100644 → 100755
Empty file.
48 changes: 32 additions & 16 deletions src/broker/types/chain/__tests__/protocol.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { marshallProtocol, unmarshallProtocol } from '../protocol'
import envelope_pb from '../../envelope/envelope_pb'
import { marshallProtocol, ProtocolType, unmarshallProtocol } from '../protocol'

let envelope

Expand All @@ -13,37 +13,53 @@ describe("# marshallTransaction ", () => {

expect(() => {
marshallProtocol(envelope, testMsg)
}).toThrow();
}).toThrow('Protocol message has invalid format');
})

test("set invalid object format", () => {
const testMsg = {error: 'testError'}

expect(() => {
marshallProtocol(envelope, testMsg)
}).toThrow();
}).toThrow('Protocol message do not expect "error" field');
})

test("invalid protocol type", () => {
const testMsg = {
type: 'invalid.protocol'
}

expect(() => {
marshallProtocol(envelope, testMsg)
}).toThrow('Cannot convert protocol type "invalid.protocol"');
})

test("should unmarshall unknown protocol type", () => {
const pbMessage = {
type: 123,
extraMap: {}
}

const receipt = unmarshallProtocol(pbMessage)
const expected = {
type: undefined,
extra: {}
}
expect(receipt).toEqual(expected)
})

test("set object", () => {
const testMsg = {
name: 'test',
tag: 'test',
type: ProtocolType.EthereumConstantinople,
extra: {
extra1: 'testExtra1',
extra2: 'testExtra2'
}
}

marshallProtocol(envelope, testMsg)
const receipt = unmarshallProtocol(envelope.getProtocol().toObject())
const expected = {
name: 'test',
tag: 'test',
extraMap: {
extra1: 'testExtra1',
extra2: 'testExtra2'
}
}
expect(receipt).toEqual(expected)

expect(receipt).toEqual(testMsg)
})

})
Empty file modified src/broker/types/chain/chain_pb.js
100644 → 100755
Empty file.
Loading

0 comments on commit 0333d1a

Please sign in to comment.