Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Balance change after new block is delayed by several seconds - Closes #2403 #2519

Merged
merged 10 commits into from
Nov 12, 2018

Conversation

lsilvs
Copy link
Contributor

@lsilvs lsilvs commented Nov 1, 2018

What was the problem?

New blocks were been broadcasted before cache for blocks and transactions been cleared.

How did I fix it?

Changed the broadcastBlock operation to be executed after applyBlock

How to test it?

Run the tests.
Follow the steps described in the issue #2403

Review checklist

@lsilvs lsilvs self-assigned this Nov 1, 2018
@lsilvs lsilvs changed the title Wait applyBlock before broadcastBlock - Closes #2403 Balance change after new block is delayed by several seconds - Closes #2403 Nov 2, 2018
MaciejBaj
MaciejBaj previously approved these changes Nov 2, 2018
nazarhussain
nazarhussain previously approved these changes Nov 5, 2018
@SargeKhan
Copy link
Contributor

SargeKhan commented Nov 5, 2018

This PR is changing the behavior of broadcasting of blocks. By which I mean that earlier the node was broadcasting blocks even they were invalid whereas after this change node will only broadcast blocks which it considers to be valid. Therefore, this change will affect how the network behaves during fork recovery.

Copy link
Contributor

@4miners 4miners left a comment

Choose a reason for hiding this comment

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

I agree with @SargeKhan, we should not change the behavior when we don't need to.

@lsilvs
Copy link
Contributor Author

lsilvs commented Nov 6, 2018

Good point @SargeKhan
I'll come up with something else then.

@4miners
Copy link
Contributor

4miners commented Nov 6, 2018

@lsilvs So what is currently wrong with the logic here is that blocks/change event is emitted from the wrong place. It should be emited after validation/verification, when block is actually accepted by the node. The simplest valid solution would be to add a new function, like:

Blocks.prototype.onNewBlock = block => {
    // Emit notification
    library.network.io.sockets.emit('blocks/change', block);
};

And remove emitting this event from Transport->onBroadcastBlock.

@lsilvs lsilvs dismissed stale reviews from nazarhussain and MaciejBaj via a60129b November 8, 2018 09:26
@lsilvs
Copy link
Contributor Author

lsilvs commented Nov 8, 2018

As discussed with @4miners and @nazarhussain , just adding onNewBlock inside Block module is not enough as we can't guarantee that the cache will be cleared before emitting blocks/change message.

The solution was to refactor onNewBlock inside Cache module to a regular method and call it from onNewBlock inside Block module making sure we emit blocks/change message after the cache function is finished.

* @param {function} cb
* @todo Add description for the params
* @todo Add @returns tag
*/
Cache.prototype.onNewBlock = function(block, cb) {
Cache.prototype.clearBlockRelatedCache = function(cb) {
cb = cb || function() {};

logger.debug(
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change this log message. As its not Cache - onNewBlock now.

* @todo Add @returns tag
*/
Blocks.prototype.onNewBlock = function(block) {
modules.cache.clearBlockRelatedCache(err => {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is already two methods removeByPattern and deleteJsonForKey in general. I would rather suggest to develop a generic method clearCacheFor(scope), scope can be blocks, transactions or in future any thing. This way we can make that single method useable on different cases.

nazarhussain
nazarhussain previously approved these changes Nov 8, 2018
4miners
4miners previously approved these changes Nov 12, 2018
@@ -404,6 +402,9 @@ Cache.prototype.onSyncFinished = function() {

Cache.prototype.KEYS = {
transactionCount: 'transactionCount',
blocksApi: '/api/blocks*',
transactionsApi: '/api/transactions*',
delegatesApi: '/api/delegates*',
Copy link
Contributor

Choose a reason for hiding this comment

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

Where this is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean the delegatesApi?
If so, I meant to use it in onFinishRound and onTransactionsSaved to keep consistency but it seems I've forgotten.
Good catch! I will added it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, thanks.

@lsilvs lsilvs dismissed stale reviews from 4miners and nazarhussain via dd38f3e November 12, 2018 11:55
@MaciejBaj
Copy link
Contributor

It might solve the #955 as well.

@MaciejBaj MaciejBaj merged commit fe52cfb into development Nov 12, 2018
@MaciejBaj MaciejBaj deleted the 2403-balance-change-after-new-block-delayed branch November 12, 2018 16:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Balance change after new block is delayed by several seconds
5 participants