Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #281 from oasislabs/armani/ws
Browse files Browse the repository at this point in the history
client: Fix workspace deploys
  • Loading branch information
armaniferrante authored Sep 11, 2019
2 parents 93aa51f + bdc3318 commit 0a6fae1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"build": "yarn build:node && yarn build:browser",
"build:node": "tsc -b && rollup -c rollup/rollup.config.ts",
"build:browser": "tsc -b && rollup -c rollup/rollup.config.browser.ts",
"test": "yarn test:integration && yarn test:browser",
"test": "yarn test:unit && yarn test:integration && yarn test:browser",
"test:unit": "jest test/unit",
"test:integration": "jest test/integration",
"test:browser": "scripts/test-browser.sh",
"coverage": "jest --coverage test/integration"
Expand Down
45 changes: 44 additions & 1 deletion packages/client/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default new Proxy(
}
);

class ServiceDefinition {
export class ServiceDefinition {
constructor(readonly bytecode: Uint8Array, readonly idl: Idl) {}

public async deploy(...args: any[]): Promise<any> {
Expand All @@ -83,8 +83,51 @@ class ServiceDefinition {
} catch (e) {
await configGateway();
}

// Place the bytecode into the DeployOptions.
this.injectBytecode(args);

// Finally, perform the deploy.
return deploy(...args);
}

/**
* Inject the bytecode into the deploy options.
* Deploy options were either provided or not.
*/
private injectBytecode(args: any[]): void {
// Deploy options provided, so inject the bytecode into them.
if (this.idl.constructor.inputs.length + 1 === args.length) {
// Replace the args' options with a cloned version.
const options = (() => {
let o = args.pop();

if (typeof o !== 'object') {
throw new WorkspaceError('Options argument must be an object');
}
if (o.bytecode) {
throw new WorkspaceError(
'Bytecode should not be provided when deploying a workspace service'
);
}

o = JSON.parse(JSON.stringify(o));
args.push(o);
return o;
})();

// Inject the bytecode into the options.
options.bytecode = this.bytecode;
}
// Deploy options not provided, so create them and then add them
// to the arguments.
else if (this.idl.constructor.inputs.length === args.length) {
const options = {
bytecode: this.bytecode,
};
args.push(options);
}
}
}

async function configGateway(): Promise<OasisGateway> {
Expand Down
69 changes: 69 additions & 0 deletions packages/client/test/unit/workspace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ServiceDefinition } from '../../src/workspace';

describe('ServiceDefinition', () => {
describe('injectBytecode', () => {
const bytecode = new Uint8Array([1, 2, 3]);
const idl = {
constructor: {
inputs: [{ type: 'string' }],
},
};
const serviceDef = new ServiceDefinition(bytecode, idl);

it('injects bytecode into the deploy options', async () => {
const deployOptions = { gasLimit: '0x00' };
const args = ['hello', deployOptions];

// @ts-ignore
serviceDef.injectBytecode(args);

const expectedArgs = [
'hello',
{
...deployOptions,
bytecode,
},
];

// Bytecode should be properly injected.
expect(args).toEqual(expectedArgs);
// Original options should not have been mutated.
expect(deployOptions).toEqual({ gasLimit: '0x00' });
});

it('creates deploy options when none exist', async () => {
const args = ['hello'];

// @ts-ignore
serviceDef.injectBytecode(args);

const expectedArgs = [
'hello',
{
bytecode,
},
];

expect(args).toEqual(expectedArgs);
});

it('errors if bytecode is provided', async () => {
const args = [
'hello',
{
bytecode: new Uint8Array([1, 2, 3]),
},
];

try {
// @ts-ignore
serviceDef.injectBytecode(args);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual(
'Bytecode should not be provided when deploying a workspace service'
);
}
});
});
});
3 changes: 1 addition & 2 deletions packages/service/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { DeployError } from '../error';
* value?: string | number;
* };
*
*
* @returns a Service object to make rpc requests with.
*/
export default async function deploy(...args: any[]): Promise<Service> {
Expand Down Expand Up @@ -63,7 +62,7 @@ export default async function deploy(...args: any[]): Promise<Service> {
* DeployOptions type, throwing an error if the arguments are
* malformed in any way.
*/
export async function extractOptions(args: any[]): Promise<DeployOptions> {
async function extractOptions(args: any[]): Promise<DeployOptions> {
if (args.length === 0) {
throw new DeployError(args, 'No deploy arguments provided');
}
Expand Down

0 comments on commit 0a6fae1

Please sign in to comment.