Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
encapsulating tx reception in blockSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
fix committed Feb 8, 2017
1 parent 24b96f7 commit 002b40e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion modules/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Blockchain.prototype.isForked = function(block){

Blockchain.prototype.isPresent = function(block){
//console.log(__private.blockchain);
return __private.blockchain[block.height] && __private.blockchain[block.height].id == block.id;
return (__private.blockchain[block.height] && __private.blockchain[block.height].id == block.id) || __private.orphanedBlocks[block.height];
}

Blockchain.prototype.isReady = function(block){
Expand Down
29 changes: 13 additions & 16 deletions modules/nodeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,21 @@ NodeManager.prototype.onRebuildBlockchain = function(blocksToRemove, state, cb)
NodeManager.prototype.onBlockReceived = function(block, peer, cb) {
if(!block.ready){
if(block.orphaned){
// usually receive a bunch of orphaned blocks, doing one after the other
return library.orphanedBlockSequence.add(function(sequenceCb){
var lastBlock = modules.blockchain.getLastBlock();
if(lastBlock.height == block.height){
//all right we are at the beginning of a fork, let's swap asap if needed
if(block.id < lastBlock.id){ // lowest id win
library.logger.info("Orphaned block has a smaller id, swaping with lastBlock", {id: block.id, height:block.height});
return modules.blocks.swapLastBlockWith(block, sequenceCb);
}
else{
library.logger.info("Orphaned block has a bigger id, processing skipped", {id: block.id, height:block.height});
return sequenceCb && sequenceCb(null, block);
}
var lastBlock = modules.blockchain.getLastBlock();
if(lastBlock.height == block.height){
//all right we are at the beginning of a fork, let's swap asap if needed
if(block.id < lastBlock.id){ // lowest id win
library.logger.info("Orphaned block has a smaller id, swaping with lastBlock", {id: block.id, height:block.height});
return modules.blocks.swapLastBlockWith(block, cb);
}
else {
return sequenceCb && sequenceCb(null, block);
else{
library.logger.info("Orphaned block has a bigger id, processing skipped", {id: block.id, height:block.height});
return cb && cb(null, block);
}
}, cb);
}
else {
return cb && cb(null, block);
}
}
else{
library.logger.debug("Skip processing block", {id: block.id, height:block.height});
Expand Down

0 comments on commit 002b40e

Please sign in to comment.