Skip to content

Instantly share code, notes, and snippets.

@rrrkren
Created April 3, 2023 18:38
Show Gist options
  • Save rrrkren/16fc0a39b3adb5b7337c7954a2060f2e to your computer and use it in GitHub Desktop.
Save rrrkren/16fc0a39b3adb5b7337c7954a2060f2e to your computer and use it in GitHub Desktop.
get root height of a flow sporked network through binary search
package main
import (
"fmt"
flowclient "github.com/onflow/flow-go-sdk/client"
"google.golang.org/grpc"
)
import (
"context"
)
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
client, err := flowclient.New("access-001.devnet16.nodes.onflow.org:9000", grpc.WithInsecure())
panicIfErr(err)
err = client.Ping(context.Background())
panicIfErr(err)
latestBlock, err := client.GetLatestBlockHeader(context.Background(), true)
panicIfErr(err)
min := uint64(0)
max := latestBlock.Height
for min < max-1 {
mid := (min + max) / 2
_, err := client.GetBlockByHeight(context.Background(), mid)
if err != nil {
// not exist, go for mid - max
min = mid
continue
}
// exists, go for min - mid
max = mid
}
fmt.Printf("root height: %d\n", max)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment