Merge pull request #333 from filecoin-project/feat/th-names
townhall: Node names
This commit is contained in:
commit
4334e4d93d
@ -3,10 +3,13 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/ipfs/go-cid"
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
"github.com/filecoin-project/go-lotus/node/impl/full"
|
"github.com/filecoin-project/go-lotus/node/impl/full"
|
||||||
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
||||||
)
|
)
|
||||||
@ -19,46 +22,60 @@ type Update struct {
|
|||||||
Type string
|
Type string
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendHeadNotifs(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
|
func SendHeadNotifs(nickname string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
|
||||||
ctx := helpers.LifecycleCtx(mctx, lc)
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, chain full.ChainAPI) error {
|
||||||
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
||||||
|
|
||||||
lc.Append(fx.Hook{
|
lc.Append(fx.Hook{
|
||||||
OnStart: func(_ context.Context) error {
|
OnStart: func(_ context.Context) error {
|
||||||
gen, err := chain.Chain.GetGenesis()
|
gen, err := chain.Chain.GetGenesis()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
topic := baseTopic + gen.Cid().String()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
if err := sendHeadNotifs(ctx, ps, topic, chain); err != nil {
|
|
||||||
log.Error("consensus metrics error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
sub, err := ps.Subscribe(topic)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
defer sub.Cancel()
|
|
||||||
|
|
||||||
for {
|
topic := baseTopic + gen.Cid().String()
|
||||||
if _, err := sub.Next(ctx); err != nil {
|
|
||||||
|
go func() {
|
||||||
|
if err := sendHeadNotifs(ctx, ps, topic, chain, nickname); err != nil {
|
||||||
|
log.Error("consensus metrics error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
|
go func() {
|
||||||
|
sub, err := ps.Subscribe(topic)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sub.Cancel()
|
||||||
|
|
||||||
}()
|
for {
|
||||||
return nil
|
if _, err := sub.Next(ctx); err != nil {
|
||||||
},
|
return
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
}()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain full.ChainAPI) error {
|
type message struct {
|
||||||
|
// TipSet
|
||||||
|
Cids []cid.Cid
|
||||||
|
Blocks []*types.BlockHeader
|
||||||
|
Height uint64
|
||||||
|
Weight types.BigInt
|
||||||
|
|
||||||
|
// Meta
|
||||||
|
|
||||||
|
NodeName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain full.ChainAPI, nickname string) error {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@ -72,7 +89,15 @@ func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain
|
|||||||
case notif := <-notifs:
|
case notif := <-notifs:
|
||||||
n := notif[len(notif)-1]
|
n := notif[len(notif)-1]
|
||||||
|
|
||||||
b, err := json.Marshal(n.Val)
|
m := message{
|
||||||
|
Cids: n.Val.Cids(),
|
||||||
|
Blocks: n.Val.Blocks(),
|
||||||
|
Height: n.Val.Height(),
|
||||||
|
Weight: n.Val.Weight(),
|
||||||
|
NodeName: nickname,
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -24,14 +24,17 @@ class App extends React.Component {
|
|||||||
let best = Object.keys(this.state).map(k => this.state[k]).reduce((p, n) => p > n.Height ? p : n.Height, -1)
|
let best = Object.keys(this.state).map(k => this.state[k]).reduce((p, n) => p > n.Height ? p : n.Height, -1)
|
||||||
console.log(best)
|
console.log(best)
|
||||||
|
|
||||||
return Object.keys(this.state).map(k => [k, this.state[k]]).map(([k, v]) => {
|
return <table>{Object.keys(this.state).map(k => [k, this.state[k]]).map(([k, v]) => {
|
||||||
let l = <span>{k} {v.Height}</span>
|
let l = [<td>{k}</td>, <td>{v.NodeName}</td>, <td>{v.Height}</td>]
|
||||||
if(best !== v.Height) {
|
if (best !== v.Height) {
|
||||||
l = <span style={{color: '#f00'}}>{l}</span>
|
l = <tr style={{color: '#f00'}}>{l}</tr>
|
||||||
|
} else {
|
||||||
|
l = <tr>{l}</tr>
|
||||||
}
|
}
|
||||||
|
|
||||||
return <div>{l}</div>
|
return l
|
||||||
})
|
})
|
||||||
|
}</table>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default App;
|
export default App;
|
||||||
|
@ -228,7 +228,7 @@ func Online() Option {
|
|||||||
Override(RunHelloKey, modules.RunHello),
|
Override(RunHelloKey, modules.RunHello),
|
||||||
Override(RunBlockSyncKey, modules.RunBlockSync),
|
Override(RunBlockSyncKey, modules.RunBlockSync),
|
||||||
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
|
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
|
||||||
Override(HeadMetricsKey, metrics.SendHeadNotifs),
|
Override(HeadMetricsKey, metrics.SendHeadNotifs("")),
|
||||||
|
|
||||||
Override(new(*discovery.Local), discovery.NewLocal),
|
Override(new(*discovery.Local), discovery.NewLocal),
|
||||||
Override(new(discovery.PeerResolver), modules.RetrievalResolver),
|
Override(new(discovery.PeerResolver), modules.RetrievalResolver),
|
||||||
@ -293,6 +293,10 @@ func Config(cfg *config.Root) Option {
|
|||||||
|
|
||||||
ApplyIf(func(s *Settings) bool { return s.Online },
|
ApplyIf(func(s *Settings) bool { return s.Online },
|
||||||
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
|
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
|
||||||
|
|
||||||
|
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull },
|
||||||
|
Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import "time"
|
|||||||
type Root struct {
|
type Root struct {
|
||||||
API API
|
API API
|
||||||
Libp2p Libp2p
|
Libp2p Libp2p
|
||||||
|
|
||||||
|
Metrics Metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
// API contains configs for API endpoint
|
// API contains configs for API endpoint
|
||||||
@ -19,6 +21,10 @@ type Libp2p struct {
|
|||||||
ListenAddresses []string
|
ListenAddresses []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Metrics struct {
|
||||||
|
Nickname string
|
||||||
|
}
|
||||||
|
|
||||||
// Default returns the default config
|
// Default returns the default config
|
||||||
func Default() *Root {
|
func Default() *Root {
|
||||||
def := Root{
|
def := Root{
|
||||||
|
Loading…
Reference in New Issue
Block a user