Auto set swarm account to account controlled by the node

When there is no account/password specified for swarm, there was an error shown in the logs (asking for address/password), and the swarm process would quit.

This PR changes the behaviour so that if a swarm address/password are not specified in the config, it attempts to use the blockchain address/password specified in `config/blockchain > account`. If `config/blockchain > account > address` doesn’t exist, the first account controlled by the node is used (provided by `web3.eth.getAccounts`, along with the password from `config/blockchain > account > password`.
This commit is contained in:
emizzle 2018-10-17 16:14:55 +11:00 committed by Pascal Precht
parent 3437f80a89
commit 294e14446f
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 42 additions and 11 deletions

View File

@ -350,8 +350,9 @@ class BlockchainConnector {
} }
let accountConfig = self.config.blockchainConfig.account; let accountConfig = self.config.blockchainConfig.account;
let selectedAccount = accountConfig && accountConfig.address; let selectedAccount = accountConfig && accountConfig.address;
self.setDefaultAccount(selectedAccount || accounts[0]); const defaultAccount = selectedAccount || accounts[0];
cb(); self.setDefaultAccount(defaultAccount);
cb(null, defaultAccount);
}); });
} }

View File

@ -111,13 +111,21 @@ class StorageProcessesLauncher {
silent: self.logger.logLevel !== 'trace', silent: self.logger.logLevel !== 'trace',
exitCallback: self.processExited.bind(this, storageName) exitCallback: self.processExited.bind(this, storageName)
}); });
this.events.once("web3Ready", () => {
this.events.request("blockchain:object", (blockchain) => {
blockchain.determineDefaultAccount((err, defaultAccount) => {
self.processes[storageName].send({ self.processes[storageName].send({
action: constants.blockchain.init, options: { action: constants.storage.init, options: {
storageConfig: self.storageConfig, storageConfig: self.storageConfig,
blockchainConfig: self.blockchainConfig, blockchainConfig: self.blockchainConfig,
cors: self.buildCors() cors: self.buildCors(),
defaultAccount: defaultAccount
} }
}); });
});
});
});
self.processes[storageName].on('result', constants.storage.initiated, (msg) => { self.processes[storageName].on('result', constants.storage.initiated, (msg) => {
if (msg.error) { if (msg.error) {

View File

@ -12,21 +12,43 @@ class SwarmProcess extends ProcessWrapper {
this.blockchainConfig = options.blockchainConfig; this.blockchainConfig = options.blockchainConfig;
this.cors = options.cors; this.cors = options.cors;
this.command = this.storageConfig.swarmPath || 'swarm'; this.command = this.storageConfig.swarmPath || 'swarm';
this.defaultAccount = options.defaultAccount;
} }
startSwarmDaemon() { startSwarmDaemon() {
const self = this; const self = this;
if (!this.storageConfig.account || !this.storageConfig.account.address || !this.storageConfig.account.password) { let bzzaccount;
let password;
// use our storage config address/password if we have it
if (this.storageConfig.account && this.storageConfig.account.address && this.storageConfig.account.password) {
bzzaccount = this.storageConfig.account.address;
password = fs.dappPath(this.storageConfig.account.password);
}
// default to our blockchain config account, or our default account
else if (!this.blockchainConfig.isDev &&
this.blockchainConfig.mineWhenNeeded &&
this.blockchainConfig.account &&
(this.blockchainConfig.account.address || this.defaultAccount) &&
this.blockchainConfig.account.password
) {
// defaultAccount is populated from blockchain_connector.determineDefaultAccount which is either
// config/blockchain.js > account > address or the first address returned from web3.eth.getAccounts
// (usually the default account)
bzzaccount = this.defaultAccount;
password = fs.dappPath(this.blockchainConfig.account.password);
console.trace(`Swarm account/password falling back to the blockchain account ${this.defaultAccount}. The account is either specified in config/blockchain.js > account > address or is the first address returned from web3.eth.getAccounts. The password is specified in config/blockchain.js > account > address.`);
}
else {
return 'Account address and password are needed in the storage config to start the Swarm process'; return 'Account address and password are needed in the storage config to start the Swarm process';
} }
const datadir = this.blockchainConfig.datadir || fs.dappPath(`.embark/development/datadir`); const datadir = this.blockchainConfig.datadir || fs.dappPath(`.embark/development/datadir`);
const args = [ const args = [
'--datadir', datadir, '--datadir', datadir,
'--bzzaccount', this.storageConfig.account.address, '--bzzaccount', bzzaccount,
'--password', fs.dappPath(this.storageConfig.account.password),
'--corsdomain', self.cors.join(',') '--corsdomain', self.cors.join(',')
]; ];
if (password) args.push('--password', password);
console.trace('Starting swarm process with arguments: ' + args.join(' ')); console.trace('Starting swarm process with arguments: ' + args.join(' '));
this.child = child_process.spawn(this.command, args); this.child = child_process.spawn(this.command, args);