Skip to content

Commit

Permalink
Better is syncing API (#44)
Browse files Browse the repository at this point in the history
* better is syncing

* wait 10s

* remove wait
  • Loading branch information
LowEntropyBody authored Sep 1, 2020
1 parent e4746e3 commit f3e974d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/service/BlockService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Block } from 'crust-sdk'
import Endpoint from 'crust-sdk/api/common/Endpoint';

class NewSystemHealth {
peers: number
isSyncing: boolean
shouldHavePeers: boolean
}

export default class BlockService {

block: Block
Expand All @@ -13,12 +19,31 @@ export default class BlockService {
return await this.block.head()
}

sleep = (time : number) => {
return new Promise((resolve) => setTimeout(resolve, time));
}

blockHash = async (blockNumber: number) => {
return await this.block.blockHash(blockNumber)
}

systemHealth = async () => {
return await this.block.health()
let sh = await this.block.health()
let nsh = new NewSystemHealth
nsh.isSyncing = sh.isSyncing.isTrue
nsh.peers = sh.peers.toNumber()
nsh.shouldHavePeers = sh.shouldHavePeers.isTrue

if (!nsh.isSyncing) {
let h1 = await this.head()
await this.sleep(3000)
let h2 = await this.head()
if (h1.number.toNumber() + 1 < h2.number.toNumber()) {
nsh.isSyncing = true
}
}

return nsh
}

}

0 comments on commit f3e974d

Please sign in to comment.