Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to emit 'newContract' events #306

Merged
merged 4 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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