Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit d374a4b

Browse files
Tim CoulterTim Coulter
authored andcommitted
Merge trufflesuite#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.
1 parent ac4e085 commit d374a4b

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

bin/testrpc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ function parseAccounts(accounts) {
2323
TestRPC.startServer(console, {
2424
port: argv.p || argv.port,
2525
debug: argv.d || argv.debug,
26+
seed: argv.s || argv.seed,
27+
total_accounts: argv.a || argv.accounts,
2628
accounts: parseAccounts(argv.account)
2729
});

lib/blockchain.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var VM = require('ethereumjs-vm');
44
var Trie = require('merkle-patricia-tree');
55
var FakeTransaction = require('ethereumjs-tx/fake.js');
66
var utils = require('ethereumjs-util');
7-
var crypto = require('crypto');
7+
var seedrandom = require('seedrandom');
8+
var random = require('./random');
89

910
Blockchain = function(logger, options) {
1011
this.stateTrie = new Trie();
@@ -23,6 +24,7 @@ Blockchain = function(logger, options) {
2324
this.lastBlockHash = "0x0000000000000000000000000000000000000000000000000000000000000000";
2425
this.snapshots = [];
2526
this.logger = logger || console;
27+
this.rng = seedrandom(options.seed);
2628

2729
if (options.debug == true) {
2830
this.vm.on('step', function(info){
@@ -54,7 +56,7 @@ Blockchain.prototype.toHex = function(val) {
5456
Blockchain.prototype.addAccount = function(opts, callback) {
5557
var self = this;
5658

57-
var secretKey = opts.secretKey || crypto.randomBytes(32);
59+
var secretKey = opts.secretKey || random.randomBytes(32, this.rng);
5860
var publicKey = utils.privateToPublic(new Buffer(secretKey));
5961
var address = utils.pubToAddress(new Buffer(publicKey));
6062

lib/manager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Manager(logger, options) {
1414
this.blockchain = new Blockchain(logger, options);
1515
this.initialized = false;
1616
this.accounts = options.accounts;
17+
this.total_accounts = options.total_accounts || 10;
1718
}
1819

1920
Manager.prototype.initialize = function(callback) {
@@ -27,7 +28,7 @@ Manager.prototype.initialize = function(callback) {
2728
});
2829
} else {
2930
// Add 10 accounts, for testing purposes.
30-
async.times(10, function(n, next) {
31+
async.times(this.total_accounts, function(n, next) {
3132
self.blockchain.addAccount({}, next);
3233
}, function() {
3334
self.initialized = true;

lib/random.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
// Mimics crypto.random bytes, but takes in a random number generator
3+
// as its second parameter. rng is expected to be a function that takes
4+
// no parameters and returns a result like Math.random().
5+
// This is important because it allows for a seeded random number generator.
6+
// Since this is a mock RPC library, the rng doesn't need to be cryptographically secure.
7+
randomBytes: function(length, rng) {
8+
var buf = [];
9+
10+
for (var i = 0; i < length; i++) {
11+
buf.push(rng()*255);
12+
}
13+
14+
return new Buffer(buf);
15+
}
16+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"ethereumjs-util": "^4.0.1",
2222
"ethereumjs-vm": "^1.2.1",
2323
"merkle-patricia-tree": "2.1.2",
24+
"seedrandom": "^2.4.2",
2425
"shelljs": "^0.6.0",
2526
"web3": "^0.15.1",
2627
"web3-provider-engine": "^6.0.1",

0 commit comments

Comments
 (0)