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

Commit

Permalink
Merge trufflesuite#24. Add -s/--seed option to see the addresses that…
Browse files Browse the repository at this point in the history
… are created. Add -a/--accounts options to specify how many accounts you want to create.
  • Loading branch information
Tim Coulter authored and Tim Coulter committed Mar 24, 2016
1 parent ac4e085 commit d374a4b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bin/testrpc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
6 changes: 4 additions & 2 deletions lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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){
Expand Down Expand Up @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d374a4b

Please sign in to comment.