Skip to content

Commit

Permalink
feat(tools): block ETA (#3829)
Browse files Browse the repository at this point in the history
I used this tool to predict the arrival time of the v2 activation height
on Mocha.
  • Loading branch information
rootulp authored Aug 30, 2024
1 parent 62b6c57 commit 43344db
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tools/blocketa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Block ETA (Estimated Time of Arrival)

blocketa is a tool that estimates the time of arrival of a block height.

## Usage

```shell
$ go run main.go https://celestia-mocha-rpc.publicnode.com:443 2585031
chainID: mocha-4
currentHeight: 2580660
currentTime: 2024-08-28 02:46:32.933542677 +0000 UTC
diffInBlockHeight: 4371
diffInTime: 14h37m50.55s
arrivalTime: 2024-08-28 17:24:23.483542677 +0000 UTC
```

> [!NOTE]
> The block time is currently hard-coded. If you're running this for a network with a different block time, you'll need to update the `blockTime` constant in the main.go file. You can use https://www.mintscan.io/celestia/block/ or the blocktime tool.
81 changes: 81 additions & 0 deletions tools/blocketa/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"context"
"fmt"
"os"
"strconv"
"time"

"github.com/tendermint/tendermint/rpc/client/http"
)

const (
// blockTime is the observed average time between blocks. You can update this
// value based on the block time on https://www.mintscan.io/celestia/block/ or
// the output from the blocktime tool.
blockTime = 12.05 // seconds between blocks for Mocha

// exampleArabicaRPC is an example node RPC endpoint for the Arabica testnet.
exampleArabicaRPC = "https://rpc.celestia-arabica-11.com:443"

// exampleMochaRPC is an example node RPC endpoint for the Mocha testnet.
exampleMochaRPC = "https://celestia-mocha-rpc.publicnode.com:443"

// exampleArabicaHeight is an example block height for the Arabica testnet.
exampleArabicaHeight = 1751707

// exampleMochaHeight is an example block height for the Mocha testnet.
exampleMochaHeight = 2585031
)

func main() {
if err := Run(); err != nil {
fmt.Printf("ERROR: %s", err.Error())
}
}

func Run() error {
if len(os.Args) < 3 {
fmt.Printf("Usage: %s <node_rpc> <target_block_height>\n", os.Args[0])
fmt.Printf("Example: %s %s %v\n", os.Args[0], exampleArabicaRPC, exampleArabicaHeight)
fmt.Printf("Example: %s %s %v\n", os.Args[0], exampleMochaRPC, exampleMochaHeight)
return nil
}

_, nodeRPC, targetBlockHeightArg := os.Args[0], os.Args[1], os.Args[2]
targetBlockHeight, err := strconv.ParseInt(targetBlockHeightArg, 10, 64)
if err != nil {
return err
}
c, err := http.New(nodeRPC, "/websocket")
if err != nil {
return err
}
resp, err := c.Status(context.Background())
if err != nil {
return err
}
chainID := resp.NodeInfo.Network
currentHeight := resp.SyncInfo.LatestBlockHeight
currentTime := resp.SyncInfo.LatestBlockTime

if currentHeight >= targetBlockHeight {
return fmt.Errorf("current height %v is already after target height %v", currentHeight, targetBlockHeight)
}
diffInBlockHeight := targetBlockHeight - currentHeight
diffInSeconds := blockTime * float64(diffInBlockHeight)
diffInTime, err := time.ParseDuration(fmt.Sprintf("%vs", diffInSeconds))
if err != nil {
return err
}
arrivalTime := currentTime.Add(diffInTime)

fmt.Printf("chainID: %v\n", chainID)
fmt.Printf("currentHeight: %v\n", currentHeight)
fmt.Printf("currentTime: %v\n", currentTime.String())
fmt.Printf("diffInBlockHeight: %v\n", diffInBlockHeight)
fmt.Printf("diffInTime: %v\n", diffInTime)
fmt.Printf("arrivalTime: %v\n", arrivalTime)
return nil
}

0 comments on commit 43344db

Please sign in to comment.