diff --git a/README.md b/README.md index bab62354dd..8679781623 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,13 @@ To build for standalone use in the browser, install `browserify` and check [run- - [`vm.runCode(opts, cb)`](#vmruncodeopts-cb) - [`vm.generateCanonicalGenesis(cb)`](#vmgeneratecanonicalgenesiscb) - [`vm.generateGenesis(cb)`](#vmgenerategenesiscb) - - [`VM` debugging hooks](#vm-debugging-hooks) - - [`vm.onStep`](#vmonstep) + - [`VM` Events](#events) + - [`step`](#step) + - [`newContract`](#newcontract) + - [`beforeBlock`](#beforeblock) + - [`afterBlock`](#afterblock) + - [`beforeTx`](#beforetx) + - [`afterTx`](#aftertx) ### `new VM([opts])` Creates a new VM object @@ -140,7 +145,7 @@ vm.generateGenesis(genesisData, function(){ ``` ### `events` -All events are instances of [async-eventemmiter](https://www.npmjs.com/package/async-eventemitter). If an event handler has an arity of 2 the VM will pause until the callback is called +All events are instances of [async-eventemmiter](https://www.npmjs.com/package/async-eventemitter). If an event handler has an arity of 2 the VM will pause until the callback is called, otherwise the VM will treat the event handler as a synchronous function. #### `step` The `step` event is given an `Object` and callback. The `Object` has the following properties. @@ -155,6 +160,11 @@ The `step` event is given an `Object` and callback. The `Object` has the followi - `memory` - the memory of the VM as a `buffer` - `cache` - The account cache. Contains all the accounts loaded from the trie. It is an instance of [functional red black tree](https://www.npmjs.com/package/functional-red-black-tree) +#### `newContract` +The `newContract` event is given an `Object` and callback. The `Object` has the following properties. +- `address`: The created address for the new contract (type `Buffer | Uint8Array`) +- `code`: The deployment bytecode for reference (type `Buffer | Uint8Array`) + #### `beforeBlock` Emits the block that is about to be processed. diff --git a/lib/runCall.js b/lib/runCall.js index b1ca92bcfc..1b77f78145 100644 --- a/lib/runCall.js +++ b/lib/runCall.js @@ -75,11 +75,26 @@ module.exports = function (opts, cb) { done(err) } - stateManager.getAccount(createdAddress, function (err, account) { - toAccount = account - toAccount.nonce = new BN(1).toArrayLike(Buffer) - done(err) - }) + async.series([ + newContractEvent, + getAccount + ], done) + + function newContractEvent (callback) { + self.emit('newContract', { + address: createdAddress, + code: code + }, callback) + } + + function getAccount (callback) { + stateManager.getAccount(createdAddress, function (err, account) { + toAccount = account + const NONCE_OFFSET = 1 + toAccount.nonce = new BN(toAccount.nonce).addn(NONCE_OFFSET).toArrayLike(Buffer) + callback(err) + }) + } }) } else { // else load the `to` account