Skip to content

Commit

Permalink
fix(tx): fix the watch block error
Browse files Browse the repository at this point in the history
  • Loading branch information
lnkyan committed Jul 10, 2019
1 parent 4a814a0 commit 531d58a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/watchers/block_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ export default class {
* @param {number} height
*/
fetchBlock(height) {
return this.requester.send(`${CHAIN_NAME}_chain_getBlockByHeight`, [height, !!this.getWithBody()]).then(result => {
return this.requester.send(`${CHAIN_NAME}_getBlockByHeight`, [height, !!this.getWithBody()]).then(result => {
return parseBlock(this.chainID, result, this.getWithBody())
})
}

/**
* requester's watch callback
* @param {Object} block
* @param {Error} error
*/
watchHandler(block, error) {
if (error) {
Expand Down Expand Up @@ -153,24 +154,23 @@ export default class {
if (block.header.height < this.lastBlockHeight) { // 新块变小抛异常
throw new Error('block height must be bigger than the height of current block')
} else if (block.header.height < nextHeight) {
// 补齐中间的块
this.insert(block)
this.checkNotifiedBlock()
} else { // 出现块不连续情况
if (nextHeight === 0) {
this.pendingBlocks.push(block)
this.checkNotifiedBlock()
return
}
} else if (this.pendingBlocks.length) {
// 收到的新块高度比任何pending块的都大,说明出现了新的漏块情况
this.pendingBlocks.push(block)
for (let i = nextHeight; i < block.header.height; i++) {
const newBlockPromise = fetchBlock(i)
newBlockPromise.then(result => {
// 并发拉取
fetchBlock(i).then(result => {
this.insert(result)
this.checkNotifiedBlock()
})
}
} else {
// 正常按顺序收到块(pending列表为空),或创始块
this.pendingBlocks.push(block)
this.checkNotifiedBlock()
}
}
}


0 comments on commit 531d58a

Please sign in to comment.