Created
January 30, 2020 09:31
-
-
Save dtynn/2d5bed675489fa12c09800ba29d14331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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