embark/packages/web3Connector/index.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

const path = require('path');
const fs = require('fs');
function whenRuncodeReady(embark) {
return new Promise((resolve) => {
embark.events.on('runcode:ready', () => {
resolve();
});
});
}
function getWeb3Location(embark) {
return new Promise((resolve, reject) => {
embark.events.request("version:get:web3", (web3Version) => {
if (web3Version === "1.0.0-beta") {
const nodePath = embark.embarkPath('node_modules');
const web3Path = require.resolve("web3", {paths: [nodePath]});
return resolve(web3Path);
}
embark.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
if (err) {
return reject(err);
}
const locationPath = embark.embarkPath(location);
resolve(locationPath);
});
});
});
}
module.exports = async (embark) => {
@fix(embark/core): Fix VM2 + Remap Imports + Monorepo + Tests This PR introduces a large number of changes (for that, I am very sorry). Breaking this up in to smaller PR's was causing tests to fail, and the likelihood of them getting merged diminished. There a couple of PRs this PR closes, and as such, I have kep the original commits to preserve the history. The first two (of three) commits are the close PRs, and the last is the glue bringin it all together. The main goal of this PR is to fix the fragility of the tests with EmbarkJS, however in doing so, a number of recent features have been updated: Remapping of imports still had a few edge cases that needed to be ironed out, as well as have unit tests created for them. More details of the changes an be seen in the closed PR (below). The main issue with VM2 was running the code generated EmbarkJS code inside the VM, and getting EmbarkJS contracts out of the VM and available to the tests. This fixed issues where ENS may not have been available to the tests. Notable additions include adding `EmbarkJS.Blockchain.connectTests` to the tests lifecycle, and ensuring `EmbarkJS` is only every required in the console module and not used or passed around elsewhere. As mentioned above, the main issue with the tests in the context of a monorepo and with embark running as module inside the dapp’s `node_modules`, there were issues getting the correct contract state available inside of the tests. For some reason, this particular case was causing the tests to fail, with ENS never being available (assuming this to be an issue with `EmbarkJS.Blockchain.connect()` never being called). The main fix for this came with passing `web3` as an option in to `EmbarkJS.Blockchain.setProvider()`. --- 1. https://github.com/embark-framework/embark/pull/1286 2. https://github.com/embark-framework/embark/pull/1275 Go to bottom for details --- There are few known issues with this PR. Instead of trying to fix all of them with this PR, I was hoping to get these issues tackled in separate PRs. 1. `deployIf` directive on contracts defined in the config are not working, however the tests are passing. The issue is that if `ContractA` has a `deployIf` set to `!!ContractB.options.address`, it means that it will not deploy if `ContractB` is not deployed. However, it appears that `ContractA` is attempted to be deployed prior to `ContractB` being deployed, and therefore `ContractA` fails to deploy. Instead, because `ContractA` depends on `ContractB`, `ContractB` should be deployed before `ContractA`. 2. `embark test --node embark` does not seem to be functioning for reasons unknown. 3. Remix tests: Currently there is support for adding contract tests that get process by `remix-tests`, however, there is an error that I believe is not due to embark, but due to the Assert library. For example, if we add a `test/remix_test.sol` to the `test_app` with the following content: ``` pragma solidity ^0.4.24; import "remix_tests.sol"; import "../app/contracts/simple_storage.sol"; contract SimpleStorageTest { SimpleStorage simpleStorage; function beforeAll() public { simpleStorage = new SimpleStorage(100); } function initialValueShouldBeCorrect() public { return Assert.equal( 100, simpleStorage.storedData, "stored data is not what I expected" ); } } ``` After compilation, we would get the error: ``` remix_test.sol:14:12: TypeError: Member "equal" not found or not visible after argument-dependent lookup in type(library Assert) return Assert.equal( ^—————^ ``` --- This branch is based off of ()`refactor/embarkjs-in-monorepo`)[https://github.com/embark-framework/embark/tree/refactor/embarkjs-in-monorepo], which does not have passing tests due to the `EmbarkJS` ENS issue mentioned above. However, you should (hopefully) see the tests passing in this branch, meaning if both branches are merged, the tests should be passing. Related PRs: https://github.com/embark-framework/embark-solc/pull/24 --- Changes include: 1. Add unit tests for recursively remapping imports 2. Handle plugin contracts correctly 3. Allow `prepareForCompilation` to be called from `File`, allowing for external compilers, like `embark-solc` to call this function before compilation. 4. Add flattened remappings to `File` that gets prepared for compilation (ie has it's imports remapped) 5. Return remapped contract content when file type is http. Previously this was broken, as always freshly downloaded (original) content was returned. 6. Handle additional cases for `custom` and http file types. This PR was tested with: - `embark` unit tests - `embark` test_app - `embark` test_app with `embark-solc` plugin - `embark` test_app with `embark-flattener` plugin - `Giveth/lpp-campaign` Related change to get `embark-solc` up-to-date` with these changes: https://github.com/embark-framework/embark-solc/pull/24 When embark was running as module inside the dapp’s `node_modules`, the tests were failing due to several issues: 1. `web3` was not being set in the global namespace of vm2. `EmbarkJS.Blockchain.setProvider` was therefore failing because it relies on `global.web3` to be set. I guess somehow this works when the test app was running in a child tree of the executing program. maybe this is a security feature of vm2, but i’m not sure. 2. `embarkjs` provider code being injected to the vm twice. This really was the initial point of failure, as this piece of code is requiring embarkjs, so i’m assuming, but again not sure, that maybe it was getting a second instance of `EmbarkJS` which did not have it’s providers set up correctly (hence the error with `ENS provider not set`). Fixes for those issues in this PR: 1. To circumvent the web3 issue, we are now setting `global.web3` for tests only (the global web3 accessible in the tests), and `web3` is now explicitly passed in to `EmbarkJS.Blockchain.setProvider` 2. To fix the `embarkjs` code being called twice, we are not re-injecting this code to the VM during test initialisations
2019-02-05 04:38:33 +00:00
let blockchainConnectorReady = false;
await whenRuncodeReady(embark);
const web3LocationPromise = getWeb3Location(embark);
embark.events.setCommandHandler('blockchain:connector:ready', (cb) => {
@fix(embark/core): Fix VM2 + Remap Imports + Monorepo + Tests This PR introduces a large number of changes (for that, I am very sorry). Breaking this up in to smaller PR's was causing tests to fail, and the likelihood of them getting merged diminished. There a couple of PRs this PR closes, and as such, I have kep the original commits to preserve the history. The first two (of three) commits are the close PRs, and the last is the glue bringin it all together. The main goal of this PR is to fix the fragility of the tests with EmbarkJS, however in doing so, a number of recent features have been updated: Remapping of imports still had a few edge cases that needed to be ironed out, as well as have unit tests created for them. More details of the changes an be seen in the closed PR (below). The main issue with VM2 was running the code generated EmbarkJS code inside the VM, and getting EmbarkJS contracts out of the VM and available to the tests. This fixed issues where ENS may not have been available to the tests. Notable additions include adding `EmbarkJS.Blockchain.connectTests` to the tests lifecycle, and ensuring `EmbarkJS` is only every required in the console module and not used or passed around elsewhere. As mentioned above, the main issue with the tests in the context of a monorepo and with embark running as module inside the dapp’s `node_modules`, there were issues getting the correct contract state available inside of the tests. For some reason, this particular case was causing the tests to fail, with ENS never being available (assuming this to be an issue with `EmbarkJS.Blockchain.connect()` never being called). The main fix for this came with passing `web3` as an option in to `EmbarkJS.Blockchain.setProvider()`. --- 1. https://github.com/embark-framework/embark/pull/1286 2. https://github.com/embark-framework/embark/pull/1275 Go to bottom for details --- There are few known issues with this PR. Instead of trying to fix all of them with this PR, I was hoping to get these issues tackled in separate PRs. 1. `deployIf` directive on contracts defined in the config are not working, however the tests are passing. The issue is that if `ContractA` has a `deployIf` set to `!!ContractB.options.address`, it means that it will not deploy if `ContractB` is not deployed. However, it appears that `ContractA` is attempted to be deployed prior to `ContractB` being deployed, and therefore `ContractA` fails to deploy. Instead, because `ContractA` depends on `ContractB`, `ContractB` should be deployed before `ContractA`. 2. `embark test --node embark` does not seem to be functioning for reasons unknown. 3. Remix tests: Currently there is support for adding contract tests that get process by `remix-tests`, however, there is an error that I believe is not due to embark, but due to the Assert library. For example, if we add a `test/remix_test.sol` to the `test_app` with the following content: ``` pragma solidity ^0.4.24; import "remix_tests.sol"; import "../app/contracts/simple_storage.sol"; contract SimpleStorageTest { SimpleStorage simpleStorage; function beforeAll() public { simpleStorage = new SimpleStorage(100); } function initialValueShouldBeCorrect() public { return Assert.equal( 100, simpleStorage.storedData, "stored data is not what I expected" ); } } ``` After compilation, we would get the error: ``` remix_test.sol:14:12: TypeError: Member "equal" not found or not visible after argument-dependent lookup in type(library Assert) return Assert.equal( ^—————^ ``` --- This branch is based off of ()`refactor/embarkjs-in-monorepo`)[https://github.com/embark-framework/embark/tree/refactor/embarkjs-in-monorepo], which does not have passing tests due to the `EmbarkJS` ENS issue mentioned above. However, you should (hopefully) see the tests passing in this branch, meaning if both branches are merged, the tests should be passing. Related PRs: https://github.com/embark-framework/embark-solc/pull/24 --- Changes include: 1. Add unit tests for recursively remapping imports 2. Handle plugin contracts correctly 3. Allow `prepareForCompilation` to be called from `File`, allowing for external compilers, like `embark-solc` to call this function before compilation. 4. Add flattened remappings to `File` that gets prepared for compilation (ie has it's imports remapped) 5. Return remapped contract content when file type is http. Previously this was broken, as always freshly downloaded (original) content was returned. 6. Handle additional cases for `custom` and http file types. This PR was tested with: - `embark` unit tests - `embark` test_app - `embark` test_app with `embark-solc` plugin - `embark` test_app with `embark-flattener` plugin - `Giveth/lpp-campaign` Related change to get `embark-solc` up-to-date` with these changes: https://github.com/embark-framework/embark-solc/pull/24 When embark was running as module inside the dapp’s `node_modules`, the tests were failing due to several issues: 1. `web3` was not being set in the global namespace of vm2. `EmbarkJS.Blockchain.setProvider` was therefore failing because it relies on `global.web3` to be set. I guess somehow this works when the test app was running in a child tree of the executing program. maybe this is a security feature of vm2, but i’m not sure. 2. `embarkjs` provider code being injected to the vm twice. This really was the initial point of failure, as this piece of code is requiring embarkjs, so i’m assuming, but again not sure, that maybe it was getting a second instance of `EmbarkJS` which did not have it’s providers set up correctly (hence the error with `ENS provider not set`). Fixes for those issues in this PR: 1. To circumvent the web3 issue, we are now setting `global.web3` for tests only (the global web3 accessible in the tests), and `web3` is now explicitly passed in to `EmbarkJS.Blockchain.setProvider` 2. To fix the `embarkjs` code being called twice, we are not re-injecting this code to the VM during test initialisations
2019-02-05 04:38:33 +00:00
if (blockchainConnectorReady) {
return cb();
}
fix(@embark/console): Fix console not working with VM2/monorepo The console was not working correctly with the latest VM2/monorepo updates. This PR addresses namely fixes this problem, but also adds a few more notable changes: * SIGNIFICANT improvement in loading time for `embark console` with an already running `embark run`. This is due to removing unneeded services starting, and instead forwarding user input to the main `embark run` process. * All user input commands are now forwarded to the `embark run` process via IPC insteaad of evaluating the command in the `embark console` process. * Removed IPC console history as it's no longer needed due to the above. Side effects: ** The signature of the `runcode:eval` and `runcode:register` events was changed to remove the `toRecord` parameter. ** Old `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, isNotUserInput, tolerateError)` ** New `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, tolerateError)` ** Old `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, toRecord, (err, result) => {})` ** New `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, (err, result) => {})` * Removed unneeded `forceRegister` flag. * Removed the `VM.getWeb3Config` method as it's no longer being used (EmbarkJS contracts are pulled out from the VM instead). * Updated `web3Connector` `blockchain:connector:ready` to allow for event requests or event emissions. * In the tests, removed the initial `initWeb3Provider` in the `init` as it was being called twice. * In the tests, removed the `web3Connector` check message as the tests are now using the Console, and the console does this check. This was causing duplicate messages to be displayed in the output. * Fix `web3 is not defined` browser error
2019-02-11 04:29:37 +00:00
embark.events.once("blockchain:connector:ready", () => {
cb();
});
});
fix(@embark/console): Fix console not working with VM2/monorepo The console was not working correctly with the latest VM2/monorepo updates. This PR addresses namely fixes this problem, but also adds a few more notable changes: * SIGNIFICANT improvement in loading time for `embark console` with an already running `embark run`. This is due to removing unneeded services starting, and instead forwarding user input to the main `embark run` process. * All user input commands are now forwarded to the `embark run` process via IPC insteaad of evaluating the command in the `embark console` process. * Removed IPC console history as it's no longer needed due to the above. Side effects: ** The signature of the `runcode:eval` and `runcode:register` events was changed to remove the `toRecord` parameter. ** Old `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, isNotUserInput, tolerateError)` ** New `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, tolerateError)` ** Old `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, toRecord, (err, result) => {})` ** New `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, (err, result) => {})` * Removed unneeded `forceRegister` flag. * Removed the `VM.getWeb3Config` method as it's no longer being used (EmbarkJS contracts are pulled out from the VM instead). * Updated `web3Connector` `blockchain:connector:ready` to allow for event requests or event emissions. * In the tests, removed the initial `initWeb3Provider` in the `init` as it was being called twice. * In the tests, removed the `web3Connector` check message as the tests are now using the Console, and the console does this check. This was causing duplicate messages to be displayed in the output. * Fix `web3 is not defined` browser error
2019-02-11 04:29:37 +00:00
web3LocationPromise.then((_web3Location) => {
blockchainConnectorReady = true;
embark.events.emit('blockchain:connector:ready');
});
let web3Location = await web3LocationPromise;
web3Location = web3Location.replace(/\\/g, '/');
fix(@embark/console): Fix console not working with VM2/monorepo The console was not working correctly with the latest VM2/monorepo updates. This PR addresses namely fixes this problem, but also adds a few more notable changes: * SIGNIFICANT improvement in loading time for `embark console` with an already running `embark run`. This is due to removing unneeded services starting, and instead forwarding user input to the main `embark run` process. * All user input commands are now forwarded to the `embark run` process via IPC insteaad of evaluating the command in the `embark console` process. * Removed IPC console history as it's no longer needed due to the above. Side effects: ** The signature of the `runcode:eval` and `runcode:register` events was changed to remove the `toRecord` parameter. ** Old `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, isNotUserInput, tolerateError)` ** New `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, tolerateError)` ** Old `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, toRecord, (err, result) => {})` ** New `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, (err, result) => {})` * Removed unneeded `forceRegister` flag. * Removed the `VM.getWeb3Config` method as it's no longer being used (EmbarkJS contracts are pulled out from the VM instead). * Updated `web3Connector` `blockchain:connector:ready` to allow for event requests or event emissions. * In the tests, removed the initial `initWeb3Provider` in the `init` as it was being called twice. * In the tests, removed the `web3Connector` check message as the tests are now using the Console, and the console does this check. This was causing duplicate messages to be displayed in the output. * Fix `web3 is not defined` browser error
2019-02-11 04:29:37 +00:00
embark.events.emit('runcode:register', '__Web3', require(web3Location));
let code = `\nconst Web3 = global.__Web3 || require('${web3Location}');`;
code += `\nglobal.Web3 = Web3;`;
const connectorCode = fs.readFileSync(path.join(__dirname, 'web3Connector.js'), 'utf8');
code += connectorCode;
code += "\nEmbarkJS.Blockchain.registerProvider('web3', web3Connector);";
fix(@embark/console): Fix console not working with VM2/monorepo The console was not working correctly with the latest VM2/monorepo updates. This PR addresses namely fixes this problem, but also adds a few more notable changes: * SIGNIFICANT improvement in loading time for `embark console` with an already running `embark run`. This is due to removing unneeded services starting, and instead forwarding user input to the main `embark run` process. * All user input commands are now forwarded to the `embark run` process via IPC insteaad of evaluating the command in the `embark console` process. * Removed IPC console history as it's no longer needed due to the above. Side effects: ** The signature of the `runcode:eval` and `runcode:register` events was changed to remove the `toRecord` parameter. ** Old `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, isNotUserInput, tolerateError)` ** New `runcode:eval` signature: `events.request("runcode:eval", "code to be evaluated", (err, result) => {}, tolerateError)` ** Old `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, toRecord, (err, result) => {})` ** New `runcode:register` signature: `events.request("runcode:register", "varName", variableValue, (err, result) => {})` * Removed unneeded `forceRegister` flag. * Removed the `VM.getWeb3Config` method as it's no longer being used (EmbarkJS contracts are pulled out from the VM instead). * Updated `web3Connector` `blockchain:connector:ready` to allow for event requests or event emissions. * In the tests, removed the initial `initWeb3Provider` in the `init` as it was being called twice. * In the tests, removed the `web3Connector` check message as the tests are now using the Console, and the console does this check. This was causing duplicate messages to be displayed in the output. * Fix `web3 is not defined` browser error
2019-02-11 04:29:37 +00:00
code += "\nEmbarkJS.Blockchain.setProvider('web3', {});";
embark.addCodeToEmbarkJS(code);
@fix(embark/core): Fix VM2 + Remap Imports + Monorepo + Tests This PR introduces a large number of changes (for that, I am very sorry). Breaking this up in to smaller PR's was causing tests to fail, and the likelihood of them getting merged diminished. There a couple of PRs this PR closes, and as such, I have kep the original commits to preserve the history. The first two (of three) commits are the close PRs, and the last is the glue bringin it all together. The main goal of this PR is to fix the fragility of the tests with EmbarkJS, however in doing so, a number of recent features have been updated: Remapping of imports still had a few edge cases that needed to be ironed out, as well as have unit tests created for them. More details of the changes an be seen in the closed PR (below). The main issue with VM2 was running the code generated EmbarkJS code inside the VM, and getting EmbarkJS contracts out of the VM and available to the tests. This fixed issues where ENS may not have been available to the tests. Notable additions include adding `EmbarkJS.Blockchain.connectTests` to the tests lifecycle, and ensuring `EmbarkJS` is only every required in the console module and not used or passed around elsewhere. As mentioned above, the main issue with the tests in the context of a monorepo and with embark running as module inside the dapp’s `node_modules`, there were issues getting the correct contract state available inside of the tests. For some reason, this particular case was causing the tests to fail, with ENS never being available (assuming this to be an issue with `EmbarkJS.Blockchain.connect()` never being called). The main fix for this came with passing `web3` as an option in to `EmbarkJS.Blockchain.setProvider()`. --- 1. https://github.com/embark-framework/embark/pull/1286 2. https://github.com/embark-framework/embark/pull/1275 Go to bottom for details --- There are few known issues with this PR. Instead of trying to fix all of them with this PR, I was hoping to get these issues tackled in separate PRs. 1. `deployIf` directive on contracts defined in the config are not working, however the tests are passing. The issue is that if `ContractA` has a `deployIf` set to `!!ContractB.options.address`, it means that it will not deploy if `ContractB` is not deployed. However, it appears that `ContractA` is attempted to be deployed prior to `ContractB` being deployed, and therefore `ContractA` fails to deploy. Instead, because `ContractA` depends on `ContractB`, `ContractB` should be deployed before `ContractA`. 2. `embark test --node embark` does not seem to be functioning for reasons unknown. 3. Remix tests: Currently there is support for adding contract tests that get process by `remix-tests`, however, there is an error that I believe is not due to embark, but due to the Assert library. For example, if we add a `test/remix_test.sol` to the `test_app` with the following content: ``` pragma solidity ^0.4.24; import "remix_tests.sol"; import "../app/contracts/simple_storage.sol"; contract SimpleStorageTest { SimpleStorage simpleStorage; function beforeAll() public { simpleStorage = new SimpleStorage(100); } function initialValueShouldBeCorrect() public { return Assert.equal( 100, simpleStorage.storedData, "stored data is not what I expected" ); } } ``` After compilation, we would get the error: ``` remix_test.sol:14:12: TypeError: Member "equal" not found or not visible after argument-dependent lookup in type(library Assert) return Assert.equal( ^—————^ ``` --- This branch is based off of ()`refactor/embarkjs-in-monorepo`)[https://github.com/embark-framework/embark/tree/refactor/embarkjs-in-monorepo], which does not have passing tests due to the `EmbarkJS` ENS issue mentioned above. However, you should (hopefully) see the tests passing in this branch, meaning if both branches are merged, the tests should be passing. Related PRs: https://github.com/embark-framework/embark-solc/pull/24 --- Changes include: 1. Add unit tests for recursively remapping imports 2. Handle plugin contracts correctly 3. Allow `prepareForCompilation` to be called from `File`, allowing for external compilers, like `embark-solc` to call this function before compilation. 4. Add flattened remappings to `File` that gets prepared for compilation (ie has it's imports remapped) 5. Return remapped contract content when file type is http. Previously this was broken, as always freshly downloaded (original) content was returned. 6. Handle additional cases for `custom` and http file types. This PR was tested with: - `embark` unit tests - `embark` test_app - `embark` test_app with `embark-solc` plugin - `embark` test_app with `embark-flattener` plugin - `Giveth/lpp-campaign` Related change to get `embark-solc` up-to-date` with these changes: https://github.com/embark-framework/embark-solc/pull/24 When embark was running as module inside the dapp’s `node_modules`, the tests were failing due to several issues: 1. `web3` was not being set in the global namespace of vm2. `EmbarkJS.Blockchain.setProvider` was therefore failing because it relies on `global.web3` to be set. I guess somehow this works when the test app was running in a child tree of the executing program. maybe this is a security feature of vm2, but i’m not sure. 2. `embarkjs` provider code being injected to the vm twice. This really was the initial point of failure, as this piece of code is requiring embarkjs, so i’m assuming, but again not sure, that maybe it was getting a second instance of `EmbarkJS` which did not have it’s providers set up correctly (hence the error with `ENS provider not set`). Fixes for those issues in this PR: 1. To circumvent the web3 issue, we are now setting `global.web3` for tests only (the global web3 accessible in the tests), and `web3` is now explicitly passed in to `EmbarkJS.Blockchain.setProvider` 2. To fix the `embarkjs` code being called twice, we are not re-injecting this code to the VM during test initialisations
2019-02-05 04:38:33 +00:00
code = "EmbarkJS.Blockchain.setProvider('web3', {web3});";
const shouldInit = (_config) => {
return true;
};
embark.addConsoleProviderInit('blockchain', code, shouldInit);
};