Skip to content

Commit

Permalink
Merge pull request #306 from SeesePlusPlus/emit-new-contract-event
Browse files Browse the repository at this point in the history
Add option to emit 'newContract' events
  • Loading branch information
holgerd77 authored Jul 11, 2018
2 parents 1394a2e + fafd74c commit 4ecae8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand Down
25 changes: 20 additions & 5 deletions lib/runCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ecae8a

Please sign in to comment.