From d374a4ba23315c8b1ef949396879cc210071eb46 Mon Sep 17 00:00:00 2001 From: Tim Coulter Date: Thu, 24 Mar 2016 10:15:30 -0700 Subject: [PATCH] Merge #24. Add -s/--seed option to see the addresses that are created. Add -a/--accounts options to specify how many accounts you want to create. --- bin/testrpc | 2 ++ lib/blockchain.js | 6 ++++-- lib/manager.js | 3 ++- lib/random.js | 16 ++++++++++++++++ package.json | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 lib/random.js diff --git a/bin/testrpc b/bin/testrpc index 871cfa22c..a533978ae 100755 --- a/bin/testrpc +++ b/bin/testrpc @@ -23,5 +23,7 @@ function parseAccounts(accounts) { TestRPC.startServer(console, { port: argv.p || argv.port, debug: argv.d || argv.debug, + seed: argv.s || argv.seed, + total_accounts: argv.a || argv.accounts, accounts: parseAccounts(argv.account) }); diff --git a/lib/blockchain.js b/lib/blockchain.js index 55491da45..86a3e1718 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -4,7 +4,8 @@ var VM = require('ethereumjs-vm'); var Trie = require('merkle-patricia-tree'); var FakeTransaction = require('ethereumjs-tx/fake.js'); var utils = require('ethereumjs-util'); -var crypto = require('crypto'); +var seedrandom = require('seedrandom'); +var random = require('./random'); Blockchain = function(logger, options) { this.stateTrie = new Trie(); @@ -23,6 +24,7 @@ Blockchain = function(logger, options) { this.lastBlockHash = "0x0000000000000000000000000000000000000000000000000000000000000000"; this.snapshots = []; this.logger = logger || console; + this.rng = seedrandom(options.seed); if (options.debug == true) { this.vm.on('step', function(info){ @@ -54,7 +56,7 @@ Blockchain.prototype.toHex = function(val) { Blockchain.prototype.addAccount = function(opts, callback) { var self = this; - var secretKey = opts.secretKey || crypto.randomBytes(32); + var secretKey = opts.secretKey || random.randomBytes(32, this.rng); var publicKey = utils.privateToPublic(new Buffer(secretKey)); var address = utils.pubToAddress(new Buffer(publicKey)); diff --git a/lib/manager.js b/lib/manager.js index 88351baea..9d4c0a8e8 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -14,6 +14,7 @@ function Manager(logger, options) { this.blockchain = new Blockchain(logger, options); this.initialized = false; this.accounts = options.accounts; + this.total_accounts = options.total_accounts || 10; } Manager.prototype.initialize = function(callback) { @@ -27,7 +28,7 @@ Manager.prototype.initialize = function(callback) { }); } else { // Add 10 accounts, for testing purposes. - async.times(10, function(n, next) { + async.times(this.total_accounts, function(n, next) { self.blockchain.addAccount({}, next); }, function() { self.initialized = true; diff --git a/lib/random.js b/lib/random.js new file mode 100644 index 000000000..0098bb5ff --- /dev/null +++ b/lib/random.js @@ -0,0 +1,16 @@ +module.exports = { + // Mimics crypto.random bytes, but takes in a random number generator + // as its second parameter. rng is expected to be a function that takes + // no parameters and returns a result like Math.random(). + // This is important because it allows for a seeded random number generator. + // Since this is a mock RPC library, the rng doesn't need to be cryptographically secure. + randomBytes: function(length, rng) { + var buf = []; + + for (var i = 0; i < length; i++) { + buf.push(rng()*255); + } + + return new Buffer(buf); + } +} diff --git a/package.json b/package.json index 391be6f0e..727e10d23 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "ethereumjs-util": "^4.0.1", "ethereumjs-vm": "^1.2.1", "merkle-patricia-tree": "2.1.2", + "seedrandom": "^2.4.2", "shelljs": "^0.6.0", "web3": "^0.15.1", "web3-provider-engine": "^6.0.1",