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

Rewrite runBlock internals with async/await #494

Merged
merged 1 commit into from
Apr 11, 2019
Merged

Rewrite runBlock internals with async/await #494

merged 1 commit into from
Apr 11, 2019

Conversation

s1na
Copy link
Contributor

@s1na s1na commented Apr 10, 2019

This PR:

  • Rewrites runBlock internals with promises and async/await syntax
  • It re-structures the code somewhat and breaks it into different methods
  • One of my goals has been to reduce shared global state, and rather have methods that explicitly get some parameters and return results, and to reduce the region where side-effects happen. As such, most of the methods don't have side-effects (don't modify the block or state).
  • I've also re-ordered some parts of the code where it doesn't change the logic to get closer to the above goals.
  • This won't be the final state for runBlock and (similar to runTx) is the first iteration.

Some side note: Currently the types we have are very heavy and serve multiple purposes. E.g. ethereumjs-block contains RLP serialization/deserialization of blocks, db interactions and also validation and other logic. I think we should break them and have separate types for each of these purposes. One type for RLP serialization, one for storing/fetching from db (which uses RLPBlock), one for consensus logic. The same applies to Blockchain and Tx. Not sure yet about details, but thought I'd ask for feedback on this idea. One other benefit for this separation is that we could then have a base class for the consensus logic part of the type, and have a child class for each fork for that type, e.g. PetersburgBlock which contains the logic for blocks after Petersburg.

(To be merged into #479)

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 95.266% when pulling 9030583 on refactor/runBlock into 519a076 on v4.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just had a look through all code parts changed.

* @type {Object}
* @property {Block} block emits the block that is about to be processed
*/
await this._emit('beforeBlock', opts.block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok (checking per async task).

*/
self.emit('afterBlock', result, cb)
// Checkpoint state
await state.checkpoint()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setStateRoot, checkpointState logic ok, just for reference: order of validateBlock has been changed respectively moved further down the line.

// Pay ommers and miners
await assignBlockRewards.bind(this)(block)
return txResults
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved validateBlock, processTransactions and payOmmersAndMiners here, introduced hierarchical call structure for applyBlock and applyTransactions, good.

let gasUsed = new BN(0)
const receiptTrie = new Trie()
const receipts = []
const txResults = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this more local and targeted initialization of variables.

cb(err)
}
return
const txReceipt = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cleanup here on receipt creation, good.

self.stateManager.getStateRoot(function (err, stateRoot) {
if (err) return cb(err)
return { bloom, gasUsed, receiptRoot: receiptTrie.root, receipts, results: txResults }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: inlined the result processing from parseTxResult.

const totalNiblingReward = niblingReward.muln(ommers.length)
const reward = minerReward.add(totalNiblingReward)
return reward
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cleanup on miner/ommer (side note: we should rename to "uncle", this confused me for quite some time) code, looks good.

@holgerd77
Copy link
Member

Additional note: we should add some more API to this sometime.

@s1na s1na merged commit 484a339 into v4 Apr 11, 2019
@s1na s1na deleted the refactor/runBlock branch April 11, 2019 07:31
This was referenced Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants