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

Commit

Permalink
Merge pull request #9 from maticnetwork/develop
Browse files Browse the repository at this point in the history
Sync Progress Tracking
  • Loading branch information
prabal-banerjee authored Apr 12, 2021
2 parents 978a7a0 + 1c7c7f8 commit 7fb6061
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ make run

## Usage

Given block number ( as decimal number/ string ) returns confidence obtained by light client for this block
1. Given block number ( as decimal number/ string ) returns confidence obtained by light client for this block

```bash
curl -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"get_blockConfidence","params": {"number": 223}, "id": 1}' http://localhost:7000/v1/json-rpc | jq
Expand All @@ -65,6 +65,7 @@ curl -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"get_b
}
}
```
---

For malformed block numbers, following will be responded with

Expand All @@ -86,4 +87,26 @@ curl -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"get_b

> Note : You'll receive `0 %` in response, when no verification is yet done for requested block.
---

2. Get progress using

```bash
curl -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"get_progress", "id": 1}' http://localhost:7000/v1/json-rpc | jq
```

```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"done": "4",
"target": "13542",
"rate": "0.57 block(s) /second",
"eta": "6 hours, 36 minutes, 26.266 seconds",
"uptime": "7.028 seconds"
}
}
```

**More info coming ...**
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"homepage": "https://github.com/maticnetwork/da-light-client#readme",
"dependencies": {
"@polkadot/api": "^4.1.1",
"bignumber.js": "^9.0.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
Expand Down
2 changes: 2 additions & 0 deletions src/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ const startLightClient = async _ => {
api.rpc.chain.subscribeNewHeads(async header => {

console.log(`🚀 Chain tip @ ${header.number}`)
// keeping track of latest block of chain
state.updateLatest(BigInt(header.number))

if (first) {

Expand Down
12 changes: 12 additions & 0 deletions src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ server.addMethod('get_blockConfidence', ({ number }) => {
}
})

server.addMethod('get_progress', _ => {

return {
done: state.done().toString(),
target: state.latest.toString(),
rate: `${state.rate().toFixed(2)} block(s)/ second`,
eta: state.eta(),
uptime: state.uptime()
}

})

const app = express()
app.use(express.json())
app.use(cors())
Expand Down
29 changes: 29 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { BigNumber } = require('bignumber.js')
const humanizeDuration = require('humanize-duration')

class BlockConfidence {

constructor() {
this.blocks = {}
this.latest = 0n
// this is milliseconds
this.startedAt = new Date().getTime()
}

incrementConfidence(number) {
Expand All @@ -16,6 +22,29 @@ class BlockConfidence {
return `${(1 - (1 / Math.pow(2, this.blocks[number] || 0))) * 100} %`
}

done() {
return Object.keys(this.blocks).length
}

updateLatest(num) {
this.latest = num
}

rate() {
// per second blocks getting verified
return new BigNumber(this.done() / ((new Date().getTime() - this.startedAt) / 1000))
}

eta() {
// expected when syncing will be fully done
return humanizeDuration(new BigNumber((this.latest - BigInt(this.done())).toString()).div(this.rate()).toNumber() * 1000)
}

uptime() {
// light client is up & running for duration
return humanizeDuration(new Date().getTime() - this.startedAt)
}

}

module.exports = { BlockConfidence }

0 comments on commit 7fb6061

Please sign in to comment.