2019-10-10 11:07:00 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-10-11 02:45:45 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-10-10 11:07:00 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-10-10 23:50:49 +00:00
|
|
|
"go.uber.org/fx"
|
2019-10-10 11:07:00 +00:00
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-10-10 11:07:00 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/impl/full"
|
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("metrics")
|
|
|
|
|
|
|
|
const baseTopic = "/fil/headnotifs/"
|
|
|
|
|
|
|
|
type Update struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
func SendHeadNotifs(nickname string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
|
|
|
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
|
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
2019-10-10 23:50:49 +00:00
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(_ context.Context) error {
|
|
|
|
gen, err := chain.Chain.GetGenesis()
|
2019-10-10 23:50:49 +00:00
|
|
|
if err != nil {
|
2019-10-11 02:45:45 +00:00
|
|
|
return err
|
2019-10-10 23:50:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
topic := baseTopic + gen.Cid().String()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := sendHeadNotifs(ctx, ps, topic, chain, nickname); err != nil {
|
|
|
|
log.Error("consensus metrics error", err)
|
2019-10-10 23:50:49 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-11 02:45:45 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
sub, err := ps.Subscribe(topic)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer sub.Cancel()
|
2019-10-10 23:50:49 +00:00
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
for {
|
|
|
|
if _, err := sub.Next(ctx); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type message struct {
|
|
|
|
// TipSet
|
|
|
|
Cids []cid.Cid
|
|
|
|
Blocks []*types.BlockHeader
|
|
|
|
Height uint64
|
|
|
|
Weight types.BigInt
|
|
|
|
|
|
|
|
// Meta
|
2019-10-10 11:07:00 +00:00
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
NodeName string
|
2019-10-10 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain full.ChainAPI, nickname string) error {
|
2019-10-10 11:07:00 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
notifs, err := chain.ChainNotify(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case notif := <-notifs:
|
|
|
|
n := notif[len(notif)-1]
|
|
|
|
|
2019-10-15 05:00:30 +00:00
|
|
|
w, err := chain.ChainTipSetWeight(ctx, n.Val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:45:45 +00:00
|
|
|
m := message{
|
|
|
|
Cids: n.Val.Cids(),
|
|
|
|
Blocks: n.Val.Blocks(),
|
|
|
|
Height: n.Val.Height(),
|
2019-10-15 05:00:30 +00:00
|
|
|
Weight: w,
|
2019-10-11 02:45:45 +00:00
|
|
|
NodeName: nickname,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(m)
|
2019-10-10 11:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ps.Publish(topic, b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|