Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcast node's height info into p2p network #3744

Merged
merged 36 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2412217
init monitor message framework
envestcc Dec 29, 2022
0f70388
init broadcast framework
envestcc Dec 30, 2022
5507558
update broadcast framework
envestcc Dec 30, 2022
47e97d5
update broadcast framework
envestcc Dec 30, 2022
935f9cc
add node package to handle node info manager
envestcc Jan 3, 2023
3ecc432
update
envestcc Jan 3, 2023
1090b2a
add node info request and response message
envestcc Jan 6, 2023
6eb404d
remove delegate manager dependency
envestcc Jan 6, 2023
98884a2
add message sign
envestcc Jan 9, 2023
eb56750
add sign for node info message
envestcc Jan 10, 2023
37af551
fix unittest error
envestcc Jan 10, 2023
96d185a
add unittest for node
envestcc Jan 11, 2023
5c0b500
add pubkey in nodeinfo message
envestcc Jan 11, 2023
b15fea7
Merge branch 'master' into monitor_msg
envestcc Jan 11, 2023
6e191c6
fix compile error
envestcc Jan 11, 2023
4660879
update go.mod
envestcc Jan 11, 2023
4284fa8
refactor node unittest
envestcc Jan 12, 2023
fbb6983
refactor unittest
envestcc Jan 12, 2023
19cf088
convert NodeManager.UpdateNode to private
envestcc Jan 13, 2023
b9cd38a
use recovered pubkey from sign to verify
envestcc Jan 13, 2023
fb8f2a9
fix comment
envestcc Jan 13, 2023
5f9bbfd
simplify finding in array
envestcc Jan 13, 2023
1a10a7b
fix lint error
envestcc Jan 13, 2023
8d53cd4
add unicast single request message
envestcc Jan 16, 2023
ebbec69
node push(broadcast) node info message proactively and periodically
envestcc Jan 18, 2023
dc07084
Merge branch 'master' into monitor_msg
envestcc Jan 19, 2023
cbc2a66
fix comment
envestcc Jan 25, 2023
e09792b
chang node to nodeinfo
envestcc Jan 28, 2023
0016e7f
add e2etest for nodeinfo
envestcc Jan 29, 2023
0c6e7ed
fix comment
envestcc Feb 1, 2023
5b4038e
fix comments
envestcc Feb 2, 2023
e9af1cf
Merge branch 'master' into monitor_msg
envestcc Feb 2, 2023
fd70cd1
update mock
envestcc Feb 2, 2023
9947145
move const var _nodeMapSize to config struct
envestcc Feb 6, 2023
63e56ef
Merge branch 'master' into monitor_msg
envestcc Feb 6, 2023
37fdaaa
Update manager.go
dustinxie Feb 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ type (
ReceiveBlock(blk *block.Block) error
// BlockHashByBlockHeight returns block hash by block height
BlockHashByBlockHeight(blkHeight uint64) (hash.Hash256, error)
// HandleMonitorMsg handle monitor msg
HandleMonitorMsg(ctx context.Context, peer string, msg *iotextypes.Monitor) error
}

// coreService implements the CoreService interface
Expand All @@ -158,6 +160,7 @@ type (
chainListener apitypes.Listener
electionCommittee committee.Committee
readCache *ReadCache
delegatesMonitor map[string]*iotextypes.Monitor
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}

// jobDesc provides a struct to get and store logs in core.LogsInRange
Expand Down Expand Up @@ -1564,3 +1567,9 @@ func (core *coreService) SyncingProgress() (uint64, uint64, uint64) {
startingHeight, currentHeight, targetHeight, _ := core.bs.SyncStatus()
return startingHeight, currentHeight, targetHeight
}

// HandleMonitorMsg handle monitor msg
func (core *coreService) HandleMonitorMsg(ctx context.Context, peer string, msg *iotextypes.Monitor) error {
// update delegateMonitor
return nil
}
6 changes: 6 additions & 0 deletions api/serverV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

// ServerV2 provides api for user to interact with blockchain data
Expand Down Expand Up @@ -134,3 +135,8 @@ func (svr *ServerV2) ReceiveBlock(blk *block.Block) error {
func (svr *ServerV2) CoreService() CoreService {
return svr.core
}

// HandleMonitorMsg handle monitor msg
func (svr *ServerV2) HandleMonitorMsg(ctx context.Context, peer string, msg *iotextypes.Monitor) error {
return svr.core.HandleMonitorMsg(ctx, peer, msg)
}
20 changes: 20 additions & 0 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ var (
)
)

type monitorMsgHandler interface {
HandleMonitorMsg(context.Context, string, *iotextypes.Monitor) error
}

func init() {
prometheus.MustRegister(_apiCallWithChainIDMtc)
prometheus.MustRegister(_apiCallWithOutChainIDMtc)
Expand All @@ -84,6 +88,8 @@ type ChainService struct {
candidateIndexer *poll.CandidateIndexer
candBucketsIndexer *staking.CandidatesBucketsIndexer
registry *protocol.Registry
monitorHandler monitorMsgHandler
monitorBroadcaster lifecycle.StartStopper
}

// Start starts the server
Expand Down Expand Up @@ -163,6 +169,11 @@ func (cs *ChainService) HandleConsensusMsg(msg *iotextypes.ConsensusMessage) err
return cs.consensus.HandleConsensusMsg(msg)
}

// HandleMonitorMsg handles monitor message.
func (cs *ChainService) HandleMonitorMsg(ctx context.Context, peer string, msg *iotextypes.Monitor) error {
return cs.monitorHandler.HandleMonitorMsg(ctx, peer, msg)
}

// ChainID returns ChainID.
func (cs *ChainService) ChainID() uint32 { return cs.chain.ChainID() }

Expand Down Expand Up @@ -199,6 +210,15 @@ func (cs *ChainService) BlockSync() blocksync.BlockSync {
// Registry returns a pointer to the registry
func (cs *ChainService) Registry() *protocol.Registry { return cs.registry }

// SetMonitorHandler set the monitor handler
func (cs *ChainService) SetMonitorHandler(handler monitorMsgHandler) { cs.monitorHandler = handler }

// SetMonitorBroadcaster sets the block sync instance
func (cs *ChainService) SetMonitorBroadcaster(mb lifecycle.StartStopper) {
cs.monitorBroadcaster = mb
cs.lifecycle.Add(mb)
}

// NewAPIServer creates a new api server
func (cs *ChainService) NewAPIServer(cfg api.Config, plugins map[int]interface{}) (*api.ServerV2, error) {
if cfg.GRPCPort == 0 && cfg.HTTPPort == 0 {
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ type (
// System is the system config
System struct {
// Active is the status of the node. True means active and false means stand-by
Active bool `yaml:"active"`
HeartbeatInterval time.Duration `yaml:"heartbeatInterval"`
Active bool `yaml:"active"`
HeartbeatInterval time.Duration `yaml:"heartbeatInterval"`
MonitorBroadcastInterval time.Duration `yaml:"monitorBroadcastInterval"`
envestcc marked this conversation as resolved.
Show resolved Hide resolved
// HTTPProfilingPort is the port number to access golang performance profiling data of a blockchain node. It is
// 0 by default, meaning performance profiling has been disabled
HTTPAdminPort int `yaml:"httpAdminPort"`
Expand Down
28 changes: 22 additions & 6 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Subscriber interface {
HandleBlock(context.Context, string, *iotextypes.Block) error
HandleSyncRequest(context.Context, peer.AddrInfo, *iotexrpc.BlockSync) error
HandleConsensusMsg(*iotextypes.ConsensusMessage) error
HandleMonitorMsg(context.Context, string, *iotextypes.Monitor) error
}

// Dispatcher is used by peers, handles incoming block and header notifications and relays announcements of new blocks.
Expand All @@ -69,16 +70,26 @@ type Dispatcher interface {
HandleTell(context.Context, uint32, peer.AddrInfo, proto.Message)
}

var requestMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_dispatch_request",
Help: "Dispatcher request counter.",
},
[]string{"method", "succeed"},
var (
requestMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_dispatch_request",
Help: "Dispatcher request counter.",
},
[]string{"method", "succeed"},
)
delegateHeightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "iotex_p2p_delegate_height_gauge",
Help: "delegate height",
},
[]string{"address", "version"},
)
)

func init() {
prometheus.MustRegister(requestMtc)
prometheus.MustRegister(delegateHeightGauge)
}

// blockMsg packages a proto block message.
Expand Down Expand Up @@ -425,6 +436,11 @@ func (d *IotxDispatcher) HandleBroadcast(ctx context.Context, chainID uint32, pe
d.dispatchAction(ctx, chainID, message)
case *iotextypes.Block:
d.dispatchBlock(ctx, chainID, peer, message)
case *iotextypes.Monitor:
if err := subscriber.HandleMonitorMsg(ctx, peer, msg); err != nil {
log.L().Debug("Failed to handle monitor message.", zap.Error(err))
}
// update delegateHeightGauge metric
envestcc marked this conversation as resolved.
Show resolved Hide resolved
default:
msgType, _ := goproto.GetTypeFromRPCMsg(message)
log.L().Warn("Unexpected msgType handled by HandleBroadcast.", zap.Any("msgType", msgType))
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ require (
replace github.com/ethereum/go-ethereum => github.com/iotexproject/go-ethereum v0.4.2

replace golang.org/x/xerrors => golang.org/x/xerrors v0.0.0-20190212162355-a5947ffaace3

// replace github.com/iotexproject/iotex-proto v0.5.10 => github.com/envestcc/iotex-proto v0.0.0-20221230052651-5f53099aee3e

replace github.com/iotexproject/iotex-proto => /Users/chenchen/dev/iotex-proto
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ github.com/iotexproject/iotex-antenna-go/v2 v2.5.1 h1:Z7X0qXxc/4hSSE3koBaFRpLAdi
github.com/iotexproject/iotex-antenna-go/v2 v2.5.1/go.mod h1:8pDZcM45M0gY6jm3PoM20rzoD2Z0vg3Hg64RS4c3qx0=
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d h1:/j1xCAC9YiG/8UKqYvycS/v3ddVsb1G7AMyLXOjeYI0=
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d/go.mod h1:GRWevxtqQ4gPMrd7Qxhr29/7aTgvjiTp+rFI9KMMZEo=
github.com/iotexproject/iotex-proto v0.5.0/go.mod h1:Xg6REkv+nTZN+OC22xXIQuqKdTWWHwOAJEXCoMpDwtI=
github.com/iotexproject/iotex-proto v0.5.10 h1:+7Hw8KYposo0tJxgIEnPRpKU/TlQGMNn1S0tpSUz6RI=
github.com/iotexproject/iotex-proto v0.5.10/go.mod h1:OfmLvjBmy5EYeLxxDv6kesJq+Mm3Adn5GKgDJgF9G9U=
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
Expand Down Expand Up @@ -1446,6 +1443,7 @@ golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1544,6 +1542,7 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
36 changes: 36 additions & 0 deletions server/itx/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package itx

import (
"context"

"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/version"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

type MonitorBroadcaster struct {
agent p2p.Agent
bc blockchain.Blockchain
}

// NewMonitorBroadcaster new monitor broadcaster
func NewMonitorBroadcaster(agent p2p.Agent, bc blockchain.Blockchain) *MonitorBroadcaster {
return &MonitorBroadcaster{
agent: agent,
bc: bc,
}
}

// Broadcast broadcast monitor msg
func (j *MonitorBroadcaster) Broadcast() {
j.agent.BroadcastOutbound(context.Background(), &iotextypes.Monitor{
Version: version.PackageVersion,
Height: j.bc.TipHeight(),
})
}
4 changes: 3 additions & 1 deletion server/itx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func newServer(cfg config.Config, testing bool) (*Server, error) {
if err := cs.Blockchain().AddSubscriber(apiServer); err != nil {
return nil, errors.Wrap(err, "failed to add api server as subscriber")
}
cs.SetMonitorHandler(apiServer)
cs.SetMonitorBroadcaster(routine.NewRecurringTask(NewMonitorBroadcaster(p2pAgent, cs.Blockchain()).Broadcast, cfg.System.MonitorBroadcastInterval))
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: explorer dependency deleted here at #1085, need to revive by migrating to api
chains[cs.ChainID()] = cs
dispatcher.AddSubscriber(cs.ChainID(), cs)
Expand Down Expand Up @@ -125,7 +128,6 @@ func (s *Server) Start(ctx context.Context) error {
if err := s.dispatcher.Start(cctx); err != nil {
return errors.Wrap(err, "error when starting dispatcher")
}

return nil
}

Expand Down