Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Support defining accounts instead of the randomly generated ones #24

Merged
merged 3 commits into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion bin/testrpc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@
var argv = require('yargs').argv;
var TestRPC = require('..');

function parseAccounts(accounts) {
function splitAccount(account) {
account = account.split(',')
return { secretKey: new Buffer(account[0], 'hex'), balance: account[1] }
}

if (typeof accounts === 'string')
return [ splitAccount(accounts) ];
else if (!Array.isArray(accounts))
return;

var ret = []
for (var i = 0; i < accounts.length; i++) {
ret.push(splitAccount(accounts[i]));
}
return ret;
}

TestRPC.startServer(console, {
port: argv.p || argv.port,
debug: argv.d || argv.debug
debug: argv.d || argv.debug,
accounts: parseAccounts(argv.account)
});
6 changes: 3 additions & 3 deletions lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ Blockchain.prototype.toHex = function(val) {
return utils.addHexPrefix(val);
}

Blockchain.prototype.addAccount = function(callback) {
Blockchain.prototype.addAccount = function(opts, callback) {
var self = this;

var secretKey = crypto.randomBytes(32);
var secretKey = opts.secretKey || crypto.randomBytes(32);
var publicKey = utils.privateToPublic(new Buffer(secretKey));
var address = utils.pubToAddress(new Buffer(publicKey));

account = new Account();
account.balance = "0xffffffffffffff00000000000000001";
account.balance = opts.balance || "0xffffffffffffff00000000000000001";

this.stateTrie.put(address, account.serialize(), function(err, result) {
if (err != null) {
Expand Down
21 changes: 15 additions & 6 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ inherits(Manager, Subprovider)
function Manager(logger, options) {
this.blockchain = new Blockchain(logger, options);
this.initialized = false;
this.accounts = options.accounts;
}

Manager.prototype.initialize = function(callback) {
var self = this;

// Add 10 accounts, for testing purposes.
async.times(10, function(n, next) {
self.blockchain.addAccount(next);
}, function() {
self.initialized = true;
});
if (this.accounts) {
async.each(this.accounts, function(account, next) {
self.blockchain.addAccount(account, next);
}, function() {
self.initialized = true;
});
} else {
// Add 10 accounts, for testing purposes.
async.times(10, function(n, next) {
self.blockchain.addAccount({}, next);
}, function() {
self.initialized = true;
});
}
};

Manager.prototype.waitForInitialization = function(callback) {
Expand Down