Skip to content

Commit

Permalink
feat(@embark/blockchain): make GanacheCLI the default dev blockchain
Browse files Browse the repository at this point in the history
Set Ganache as a blockchain client that doesn't need to be started.
Set it as the default client, at least for development.
Move all blockchain related stuff in the blockchain component
Includes a fix by @emmizle to fix the WS connection in the proxy
  • Loading branch information
jrainville committed Feb 14, 2020
1 parent b2f670b commit cd934f8
Show file tree
Hide file tree
Showing 28 changed files with 218 additions and 426 deletions.
3 changes: 2 additions & 1 deletion dapps/templates/boilerplate/config/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module.exports = {
// default applies to all environments
default: {
enabled: true,
client: "geth" // Can be geth or parity (default:geth)
client: "geth" // Can be ganache-cli, geth or parity (default: geth)
},

development: {
client: 'ganache-cli',
clientConfig: {
miningMode: 'dev' // Mode in which the node mines. Options: dev, auto, always, off
}
Expand Down
3 changes: 2 additions & 1 deletion dapps/templates/demo/config/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module.exports = {
// default applies to all environments
default: {
enabled: true,
client: "geth" // Can be geth or parity (default:geth)
client: "geth" // Can be ganache-cli, geth or parity (default: geth)
},

development: {
client: 'ganache-cli',
clientConfig: {
miningMode: 'dev' // Mode in which the node mines. Options: dev, auto, always, off
}
Expand Down
3 changes: 2 additions & 1 deletion dapps/tests/app/config/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module.exports = {
// default applies to all environments
default: {
enabled: true,
client: "geth" // Can be geth or parity (default:geth)
client: "geth" // Can be ganache-cli, geth or parity (default: geth)
},

development: {
client: 'ganache-cli',
clientConfig: {
miningMode: 'dev' // Mode in which the node mines. Options: dev, auto, always, off
},
Expand Down
2 changes: 1 addition & 1 deletion dapps/tests/app/test/another_storage_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ config({
{
"mnemonic": "example exile argue silk regular smile grass bomb merge arm assist farm",
balance: "5ether",
hdpath: "m/44'/1'/0'/0/",
numAddresses: 10
}
]
Expand Down Expand Up @@ -49,7 +50,6 @@ contract("AnotherStorage", function() {

for (let i = 1; i < numAddresses - 3; i++) {
balance = await web3.eth.getBalance(accounts[i]);
console.log('Account', i , balance);
assert.strictEqual(parseInt(balance, 10), 5000000000000000000, `Account ${i} doesn't have the balance set`);
}
});
Expand Down
4 changes: 3 additions & 1 deletion packages/core/core/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
"call": "eth_call",
"clients": {
"geth": "geth",
"parity": "parity"
"parity": "parity",
"ganache": "ganache-cli"
},
"defaultMnemonic": "example exile argue silk regular smile grass bomb merge arm assist farm",
"blockchainReady": "blockchainReady",
"blockchainExit": "blockchainExit",
"defaults": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/configDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getBlockchainDefaults(env) {
miningMode: 'dev' // Mode in which the node mines. Options: dev, auto, always, off
},
enabled: true,
client: constants.blockchain.clients.geth,
client: constants.blockchain.clients.ganache,
proxy: true,
datadir: `.embark/${env}/datadir`,
rpcHost: "localhost",
Expand Down
1 change: 0 additions & 1 deletion packages/core/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"embark-authenticator": "^5.2.0-nightly.3",
"embark-basic-pipeline": "^5.2.0-nightly.3",
"embark-blockchain": "^5.2.0-nightly.3",
"embark-blockchain-client": "^5.1.1",
"embark-code-runner": "^5.2.0-nightly.3",
"embark-communication": "^5.2.0-nightly.3",
"embark-compiler": "^5.2.0-nightly.3",
Expand Down
4 changes: 1 addition & 3 deletions packages/core/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ export class Engine {

blockchainStackComponents() {
this.registerModulePackage('embark-blockchain', { plugins: this.plugins, ipc: this.ipc });
this.registerModulePackage('embark-blockchain-client');
this.registerModulePackage('embark-process-logs-api-manager');
}

Expand All @@ -259,7 +258,6 @@ export class Engine {
this.registerModulePackage('embark-compiler', { plugins: this.plugins, isCoverage: options.isCoverage });
this.registerModulePackage('embark-contracts-manager', { plugins: this.plugins, compileOnceOnly: options.compileOnceOnly });
this.registerModulePackage('embark-deployment', { plugins: this.plugins, onlyCompile: options.onlyCompile });
this.registerModulePackage('embark-blockchain-client');
this.registerModulePackage('embark-storage');
this.registerModulePackage('embark-communication');
this.registerModulePackage('embark-namesystem');
Expand All @@ -269,6 +267,7 @@ export class Engine {

blockchainComponents() {
// plugins
this.registerModulePackage('embark-ganache');
this.registerModulePackage('embark-geth');
this.registerModulePackage('embark-parity');
}
Expand All @@ -287,7 +286,6 @@ export class Engine {
}

contractsComponents(_options) {
this.registerModulePackage('embark-ganache');
this.registerModulePackage('embark-ethereum-blockchain-client');
this.registerModulePackage('embark-web3');
this.registerModulePackage('embark-accounts-manager');
Expand Down
3 changes: 0 additions & 3 deletions packages/core/engine/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@
{
"path": "../../stack/blockchain"
},
{
"path": "../../stack/blockchain-client"
},
{
"path": "../../stack/communication"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/embark/src/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('embark.Config', function () {
config.loadBlockchainConfigFile();
let expectedConfig = {
"enabled": true,
"client": "geth",
"client": "ganache-cli",
"proxy": true,
"clientConfig": {
"miningMode": "dev"
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('embark.Config', function () {
it('should convert Ether units', function () {
let expectedConfig = {
"enabled": true,
"client": "geth",
"client": "ganache-cli",
"proxy": true,
"clientConfig": {
"miningMode": "dev"
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('embark.Config', function () {
it('should accept unitless gas values', function () {
let expectedConfig = {
"enabled": true,
"client": "geth",
"client": "ganache-cli",
"proxy": true,
"clientConfig": {
"miningMode": "dev"
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('embark.Config', function () {
it('should use the specified endpoint', () => {
let expectedConfig = {
"enabled": true,
"client": "geth",
"client": "ganache-cli",
"proxy": true,
"clientConfig": {
"miningMode": "dev"
Expand Down
14 changes: 10 additions & 4 deletions packages/plugins/ethereum-blockchain-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const embarkJsUtils = require('embarkjs').Utils;
import checkContractSize from "./checkContractSize";
const {ZERO_ADDRESS} = AddressUtils;
import EthereumAPI from "./api";

import constants from "embark-core/constants";

class EthereumBlockchainClient {

Expand All @@ -24,7 +24,7 @@ class EthereumBlockchainClient {
this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.doLinking.bind(this));
this.embark.registerActionForEvent('deployment:contract:beforeDeploy', checkContractSize.bind(this));
this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.determineAccounts.bind(this));
this.events.request("blockchain:client:register", "ethereum", this.getClient.bind(this));
this.events.request("blockchain:client:register", "ethereum", this.getEthereumClient.bind(this));
this.events.request("deployment:deployer:register", "ethereum", this.deployer.bind(this));

this.events.on("blockchain:started", () => {
Expand All @@ -50,8 +50,14 @@ class EthereumBlockchainClient {
ethereumApi.registerAPIs();
}

getClient() {
return {};
getEthereumClient(endpoint) {
if (endpoint.startsWith('ws')) {
return new Web3.providers.WebsocketProvider(endpoint, {
headers: { Origin: constants.embarkResourceOrigin }
});
}
const web3 = new Web3(endpoint);
return web3.currentProvider;
}

async deployer(contract, done) {
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/ganache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"dependencies": {
"@babel/runtime-corejs3": "7.7.4",
"core-js": "3.4.3",
"embark-core": "^5.2.0-nightly.0",
"embark-i18n": "^5.1.1",
"ganache-cli": "6.8.2"
},
"devDependencies": {
Expand Down
76 changes: 73 additions & 3 deletions packages/plugins/ganache/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,80 @@
import {__} from 'embark-i18n';
const constants = require('embark-core/constants');

class Ganache {
constructor(embark) {
embark.events.request('blockchain:vm:register', () => {
const ganache = require('ganache-cli');
this.embark = embark;
this.currentProvider = null;

this.embark.events.request("blockchain:node:register", constants.blockchain.clients.ganache, {
isStartedFn: (cb) => {
cb(null, !!this.currentProvider); // Always assume it's started, because it's just a provider (nothing to start)
},
launchFn: (cb) => {
this._getProvider(); // No need to return anything, we just want to populate currentProvider
this.embark.logger.info(__('Blockchain node is ready').cyan);
cb();
},
stopFn: (cb) => {
this.currentProvider = null;
cb();
},
provider: async (_endpoint) => {
return this._getProvider();
}
});

this._registerStatusCheck();
}

_getProvider() {
if (this.currentProvider) {
return this.currentProvider;
}
const ganache = require('ganache-cli');
const blockchainConfig = this.embark.config.blockchainConfig;

// Ensure the dir exists before initiating Ganache, because Ganache has a bug
// => https://github.com/trufflesuite/ganache-cli/issues/558
this.embark.fs.ensureDirSync(blockchainConfig.datadir);

const hasAccounts = blockchainConfig.accounts && blockchainConfig.accounts.length;

const isTest = this.embark.currentContext.includes('test');

this.currentProvider = ganache.provider({
// Default to 8000000, which is the server default
// Somehow, the provider default is 6721975
return ganache.provider({gasLimit: '0x7A1200'});
gasLimit: blockchainConfig.targetGasLimit || '0x7A1200',
blockTime: blockchainConfig.simulatorBlocktime,
network_id: blockchainConfig.networkId || 1337,
db_path: isTest ? '' : blockchainConfig.datadir,
default_balance_ether: '99999',
mnemonic: hasAccounts || isTest ? '' : constants.blockchain.defaultMnemonic
});
return this.currentProvider;
}

_registerStatusCheck() {
this.embark.events.request("services:register", 'Ethereum (VM)', (cb) => {
if (!this.currentProvider) {
return cb({name: "Ethereum provider stopped", status: 'off'});
}

const sendMethod = (this.currentProvider.sendAsync) ? this.currentProvider.sendAsync.bind(this.currentProvider) : this.currentProvider.send.bind(this.currentProvider);
sendMethod({
jsonrpc: '2.0',
method: 'web3_clientVersion',
params: [],
id: Date.now().toString().substring(9)
},
(error, res) => {
if (error || !res.result) {
return cb({name: "Ethereum provider failure", status: 'off'});
}
const versionParts = res.result.split('/');
cb({name: `Ganache - ${versionParts[1] || ''}`, status: 'on'});
});
});
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/plugins/ganache/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
"extends": "../../../tsconfig.base.json",
"include": [
"src/**/*"
],
"references": [
{
"path": "../../core/core"
},
{
"path": "../../core/i18n"
}
]
}
2 changes: 1 addition & 1 deletion packages/plugins/geth/src/devtxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class DevTxs {
if (!this.shouldStartDevTxs()) {
return;
}
const provider = await this.events.request2("blockchain:client:provider", "ethereum", this.blockchainConfig.endpoint);
const provider = await this.events.request2("blockchain:node:provider", "ethereum");
this.web3 = new Web3(provider);

const accounts = await this.web3.eth.getAccounts();
Expand Down
5 changes: 2 additions & 3 deletions packages/plugins/geth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import { BlockchainClient } from './blockchain';
import { ws, rpcWithEndpoint } from './check.js';
import DevTxs from "./devtxs";
const constants = require('embark-core/constants');
import constants from 'embark-core/constants';

class Geth {
constructor(embark) {
Expand Down Expand Up @@ -66,8 +66,8 @@ class Geth {
if (err) {
this.logger.error(`Error launching blockchain process: ${err.message || err}`);
}
this.setupDevTxs();
readyCb();
this.setupDevTxs();
});
this.registerServiceCheck();
},
Expand Down Expand Up @@ -112,7 +112,6 @@ class Geth {
rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}

// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => {
this._doCheck(cb);
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/parity/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {BlockchainClient} from "./blockchain";
const {normalizeInput} = require('embark-utils');
import {BlockchainProcessLauncher} from './blockchainProcessLauncher';
import {ws, rpcWithEndpoint} from './check.js';
const constants = require('embark-core/constants');
import constants from 'embark-core/constants';

class Parity {

Expand Down
4 changes: 0 additions & 4 deletions packages/stack/blockchain-client/.npmrc

This file was deleted.

Loading

0 comments on commit cd934f8

Please sign in to comment.