embark/packages/stack/deployment/test/deployment.spec.js

134 lines
5.3 KiB
JavaScript
Raw Normal View History

import sinon from 'sinon';
import assert from 'assert';
fix: ensure that packages properly specify their dependencies Many packages in the monorepo did not specify all of their dependencies; they were effectively relying on resolution in the monorepo's root `node_modules`. In a production release of `embark` and `embark[js]-*` packages this can lead to broken packages. To fix the problem currently and to help prevent it from happening again, make use of the `eslint-plugin-import` package's `import/no-extraneous-dependencies` and `import/no-unresolved` rules. In the root `tslint.json` set `"no-implicit-dependencies": true`, wich is the tslint equivalent of `import/no-extraneous-dependencies`; there is no tslint equivalent for `import/no-unresolved`, but we will eventually replace tslint with an eslint configuration that checks both `.js` and `.ts` files. For `import/no-unresolved` to work in our monorepo setup, in most packages add an `index.js` that has: ```js module.exports = require('./dist'); // or './dist/lib' in some cases ``` And point `"main"` in `package.json` to `"./index.js"`. Despite what's indicated in npm's documentation for `package.json`, it's also necessary to add `"index.js"` to the `"files"` array. Make sure that all `.js` files that can and should be linted are in fact linted. For example, files in `packages/embark/src/cmd/` weren't being linted and many test suites weren't being linted. Bump all relevant packages to `eslint@6.8.0`. Fix all linter errors that arose after these changes. Implement a `check-yarn-lock` script that's run as part of `"ci:full"` and `"qa:full"`, and can manually be invoked via `yarn cylock` in the root of the monorepo. The script exits with error if any specifiers are found in `yarn.lock` for `embark[js][-*]` and/or `@embarklabs/*` (with a few exceptions, cf. `scripts/check-yarn-lock.js`).
2020-02-20 23:47:01 +00:00
import { fakeEmbark } from 'embark-testing';
import Deployment from '../src/';
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["assert", "expect"] }] */
describe('stack/deployment', () => {
const { embark, plugins } = fakeEmbark();
fix: ensure that packages properly specify their dependencies Many packages in the monorepo did not specify all of their dependencies; they were effectively relying on resolution in the monorepo's root `node_modules`. In a production release of `embark` and `embark[js]-*` packages this can lead to broken packages. To fix the problem currently and to help prevent it from happening again, make use of the `eslint-plugin-import` package's `import/no-extraneous-dependencies` and `import/no-unresolved` rules. In the root `tslint.json` set `"no-implicit-dependencies": true`, wich is the tslint equivalent of `import/no-extraneous-dependencies`; there is no tslint equivalent for `import/no-unresolved`, but we will eventually replace tslint with an eslint configuration that checks both `.js` and `.ts` files. For `import/no-unresolved` to work in our monorepo setup, in most packages add an `index.js` that has: ```js module.exports = require('./dist'); // or './dist/lib' in some cases ``` And point `"main"` in `package.json` to `"./index.js"`. Despite what's indicated in npm's documentation for `package.json`, it's also necessary to add `"index.js"` to the `"files"` array. Make sure that all `.js` files that can and should be linted are in fact linted. For example, files in `packages/embark/src/cmd/` weren't being linted and many test suites weren't being linted. Bump all relevant packages to `eslint@6.8.0`. Fix all linter errors that arose after these changes. Implement a `check-yarn-lock` script that's run as part of `"ci:full"` and `"qa:full"`, and can manually be invoked via `yarn cylock` in the root of the monorepo. The script exits with error if any specifiers are found in `yarn.lock` for `embark[js][-*]` and/or `@embarklabs/*` (with a few exceptions, cf. `scripts/check-yarn-lock.js`).
2020-02-20 23:47:01 +00:00
// eslint-disable-next-line no-unused-vars
let deployment;
let deployedContracts = [];
let beforeAllAction, beforeDeployAction, shouldDeployAction, deployedAction, afterAllAction;
let deployFn;
let doneCb;
beforeEach(() => {
embark.config.blockchainConfig = { enabled: true };
deployment = new Deployment(embark, { plugins });
beforeAllAction = sinon.spy((params, cb) => { cb(null, params); });
beforeDeployAction = sinon.spy((params, cb) => { cb(null, params); });
shouldDeployAction = sinon.spy((params, cb) => { cb(null, params); });
deployedAction = sinon.spy((params, cb) => { cb(null, params); });
afterAllAction = sinon.spy((params, cb) => { cb(null, params); });
feat(@embark/quorum): Add support for Quorum blockchains Add support for *connecting to* Quorum blockchains. This plugin will not start a Quorum node or nodes automatically as Embark does with other chains. This plugins supports deploying contracts publically and privately using the Tessera private transaction manager. This plugin supports sending of public and private transactions using the Tessera private transaction manager. Add ability to skip bytecode checking as part of the contract deployment process. Instruct the deployer to skip checking if the contract bytecode exists on-chain before deploying the contract. This is important in the case of having many private nodes in a network because if a contract is deployed privately to node 1 and 7, running Embark on node 2 should skip the bytecode check as the contract *is not* deployed on node 2, nor do we want it deployed on node 2. If the bytecode check was in place, Embark would have deployed it to node 2 and therefore not adhered to the privacy needs. Add Ethereum contract deployer for Quorum, allowing for deploying of public and private contracts using `privateFor` and `privateFrom` (see Contract config updates below). Add web3 extensions enabling specific functionality for Quorum. Extensions includes those provided by [`quorum-js`](https://github.com/jpmorganchase/quorum.js), as well as some custom monkeypatches that override web3 method output formatting, including: - web3.eth.getBlock - web3.eth.getTransaction - web3.eth.getTransactionReceipt - web3.eth.decodeParameters DApps wishing to take advantage of these overrides will need to patch web3 as follows: ``` import {patchWeb3} from "embark-quorum"; import Web3 from "web3"; let web3 = new Web3(...); web3 = patchWeb3(web3); ``` Add support for sending a raw private transaction in the Quorum network. This includes running actions from the proxy after an `eth_sendTransaction` RPC request has been transformed in to `eth_sendRawTransaction` after being signed. fix(@embark/transaction-logger): Fix bug when sending a 0-value transaction. Add `originalRequest` to the proxy when modifying `eth_sendTransaction` to `eth_sendRawTransaction`, so that the original transaction parameters (including `privateFor` and `privateFrom`) can be used to sign a raw private transaction in the `eth_sendRawTransaction` action. Added the following properties on to blockchain config: - *`client`* `{boolean}` - Allows `quorum` to be specified as the blockchain client - *`clientConfig/tesseraPrivateUrl`* `{string}` - URL of the Tessera private transaction manager ``` client: "quorum", clientConfig: { tesseraPrivateUrl: "http://localhost:9081" // URL of the Tessera private transaction manager } ``` Added the following properties to the contracts config: - *`skipBytecodeCheck`* `{boolean}` - Instructs the deployer to skip checking if the bytecode of the contract exists on the chain before deploying the contract. This is important in the case of having many private nodes in a network because if a contract is deployed privately to node 1 and 7, running Embark on node 2 should skip the bytecode check as the contract *is not* deployed on node 2, nor do we want it deployed on node 2. If the bytecode check was in place, Embark would have deployed it to node 2 and therefore not adhered to the privacy needs. - *`privateFor`* `{string[]}` - When sending a private transaction, an array of the recipient nodes' base64-encoded public keys. - *`privateFrom`* `{string}` - When sending a private transaction, the sending party's base64-encoded public key to use ``` environment: { deploy: { SimpleStorage: { skipBytecodeCheck: true, privateFor: ["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc"], privateFrom: "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=" } } }, ``` - *`proxy:endpoint:http:get`* - get the HTTP endpoint of the proxy regardless of blockchain settings - *`proxy:endpoint:ws:get`* - get the WS endpoint of the proxy regardless of blockchain settings - *`runcode:register:<variable>`* - when variables are registered in the console using `runcode:register`, actions with the name of the variable (ie `runcode:register:web3`) will be run *before* the variable is actually registered in the console. This allows a variable to be modified by plugins before being registered in the console.
2020-03-02 11:34:50 +00:00
deployFn = sinon.spy((contract, addlDeployParams, done) => {
deployedContracts.push(contract);
done(null, {}); // deployer needs to finish with a receipt object
});
doneCb = sinon.fake();
});
afterEach(() => {
deployedContracts = [];
embark.teardown();
sinon.restore();
});
test('it should register deployFn and deploy contract by using it', () => {
let testContract = { className: 'TestContract', shouldDeploy: true };
embark.registerActionForEvent('deployment:contract:beforeDeploy', beforeDeployAction);
embark.registerActionForEvent('deployment:contract:shouldDeploy', shouldDeployAction);
embark.registerActionForEvent('deployment:contract:deployed', deployedAction);
embark.events.request('deployment:deployer:register', 'ethereum', deployFn);
embark.events.request('deployment:contract:deploy', testContract, doneCb);
fix: ensure that packages properly specify their dependencies Many packages in the monorepo did not specify all of their dependencies; they were effectively relying on resolution in the monorepo's root `node_modules`. In a production release of `embark` and `embark[js]-*` packages this can lead to broken packages. To fix the problem currently and to help prevent it from happening again, make use of the `eslint-plugin-import` package's `import/no-extraneous-dependencies` and `import/no-unresolved` rules. In the root `tslint.json` set `"no-implicit-dependencies": true`, wich is the tslint equivalent of `import/no-extraneous-dependencies`; there is no tslint equivalent for `import/no-unresolved`, but we will eventually replace tslint with an eslint configuration that checks both `.js` and `.ts` files. For `import/no-unresolved` to work in our monorepo setup, in most packages add an `index.js` that has: ```js module.exports = require('./dist'); // or './dist/lib' in some cases ``` And point `"main"` in `package.json` to `"./index.js"`. Despite what's indicated in npm's documentation for `package.json`, it's also necessary to add `"index.js"` to the `"files"` array. Make sure that all `.js` files that can and should be linted are in fact linted. For example, files in `packages/embark/src/cmd/` weren't being linted and many test suites weren't being linted. Bump all relevant packages to `eslint@6.8.0`. Fix all linter errors that arose after these changes. Implement a `check-yarn-lock` script that's run as part of `"ci:full"` and `"qa:full"`, and can manually be invoked via `yarn cylock` in the root of the monorepo. The script exits with error if any specifiers are found in `yarn.lock` for `embark[js][-*]` and/or `@embarklabs/*` (with a few exceptions, cf. `scripts/check-yarn-lock.js`).
2020-02-20 23:47:01 +00:00
assert(beforeDeployAction.calledOnce);
assert(shouldDeployAction.calledOnce);
assert(deployFn.calledWith(testContract));
assert.equal(deployedContracts[0], testContract);
assert(doneCb.calledOnce);
});
test('it should deploy list of contracts', () => {
let testContracts = [
{ className: 'Contract1', shouldDeploy: true },
{ className: 'Contract2', shouldDeploy: true },
{ className: 'Contract3', shouldDeploy: true }
];
embark.registerActionForEvent('deployment:deployContracts:beforeAll', beforeAllAction);
embark.registerActionForEvent('deployment:contract:beforeDeploy', beforeDeployAction);
embark.registerActionForEvent('deployment:contract:shouldDeploy', shouldDeployAction);
embark.registerActionForEvent('deployment:contract:deployed', deployedAction);
embark.registerActionForEvent('deployment:deployContracts:afterAll', afterAllAction);
embark.events.request('deployment:deployer:register', 'ethereum', deployFn);
embark.events.request('deployment:contracts:deploy', testContracts, {}, doneCb);
assert(beforeAllAction.calledOnce);
embark.events.assert.commandHandlerCalledWith('deployment:contract:deploy', testContracts[0]);
embark.events.assert.commandHandlerCalledWith('deployment:contract:deploy', testContracts[1]);
embark.events.assert.commandHandlerCalledWith('deployment:contract:deploy', testContracts[2]);
assert(deployFn.calledWith(testContracts[0]));
assert(deployFn.calledWith(testContracts[1]));
assert(deployFn.calledWith(testContracts[2]));
assert.equal(deployedAction.callCount, 3);
assert.equal(deployedContracts.length, 3);
assert(afterAllAction.calledOnce);
assert(doneCb.calledOnce);
});
test('it should deploy contracts in correct order', () => {
let testContracts = [
{ className: 'A', shouldDeploy: true },
{ className: 'B', shouldDeploy: true },
{ className: 'C', shouldDeploy: true },
{ className: 'D', shouldDeploy: true },
{ className: 'E', shouldDeploy: true },
{ className: 'F', shouldDeploy: true }
];
let testContractDependencies = {
A: ['B'],
B: [],
C: ['A'],
D: ['F'],
E: ['A'],
F: []
};
embark.registerActionForEvent('deployment:deployContracts:beforeAll', beforeAllAction);
embark.registerActionForEvent('deployment:contract:beforeDeploy', beforeDeployAction);
embark.registerActionForEvent('deployment:contract:shouldDeploy', shouldDeployAction);
embark.registerActionForEvent('deployment:contract:deployed', deployedAction);
embark.registerActionForEvent('deployment:deployContracts:afterAll', afterAllAction);
embark.events.request('deployment:deployer:register', 'ethereum', deployFn);
embark.events.request('deployment:contracts:deploy', testContracts, testContractDependencies, doneCb);
assert.equal(deployedContracts.length, 6);
// expected result: [B, F, A, D, C, E]
assert.equal(deployedContracts[0].className, testContracts[1].className);
assert.equal(deployedContracts[1].className, testContracts[5].className);
assert.equal(deployedContracts[2].className, testContracts[0].className);
assert.equal(deployedContracts[3].className, testContracts[3].className);
assert.equal(deployedContracts[4].className, testContracts[2].className);
assert.equal(deployedContracts[5].className, testContracts[4].className);
});
});