Skip to content

Instantly share code, notes, and snippets.

@dtynn
Created January 30, 2020 09:31
Show Gist options
  • Save dtynn/2d5bed675489fa12c09800ba29d14331 to your computer and use it in GitHub Desktop.
Save dtynn/2d5bed675489fa12c09800ba29d14331 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"time"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/lib/rpcli"
"github.com/libp2p/go-libp2p-core/peer"
)
type API struct {
// ID func(context.Context) (peer.ID, error)
ID func(context.Context) (peer.ID, error) `failfast:""`
ChainNotify func(context.Context) (<-chan []*store.HeadChange, error) `perm:"read"`
// ChainNotify func(context.Context) (<-chan []*store.HeadChange, error) `perm:"read" failfast:""`
}
func main() {
addr := "ws://127.0.0.1:1234/rpc/v0"
var api API
closer, err := rpcli.NewMergeClient(addr, "Filecoin",
[]interface{}{
&api,
}, nil)
if err != nil {
log.Fatalln("make merge client:", err)
}
defer closer()
go func() {
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
id, err := api.ID(context.Background())
if err != nil {
log.Printf("unable to get id: %v", err)
} else {
log.Printf("get peer id: %s", id)
}
}
}
}()
chanNotify, err := api.ChainNotify(context.Background())
if err != nil {
log.Fatalln("request ChainNotify:", err)
}
log.Printf("chan notify start")
for changes := range chanNotify {
cur := 0
revert := 0
apply := 0
unknown := 0
for _, hc := range changes {
switch hc.Type {
case store.HCCurrent:
cur++
case store.HCApply:
apply++
case store.HCRevert:
revert++
default:
unknown++
}
}
log.Printf("get head changes, cur=%d, revert=%d, apply=%d, unknown=%d", cur, revert, apply, unknown)
}
log.Println("notify chan closed")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment