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 6 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
28 changes: 16 additions & 12 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/gasstation"
"github.com/iotexproject/iotex-core/node"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/pkg/version"
Expand Down Expand Up @@ -158,6 +159,7 @@ type (
chainListener apitypes.Listener
electionCommittee committee.Committee
readCache *ReadCache
delegateManager node.NodeManager
}

// jobDesc provides a struct to get and store logs in core.LogsInRange
Expand Down Expand Up @@ -207,6 +209,7 @@ func newCoreService(
bfIndexer blockindex.BloomFilterIndexer,
actPool actpool.ActPool,
registry *protocol.Registry,
dm node.NodeManager,
opts ...Option,
) (CoreService, error) {
if cfg == (Config{}) {
Expand All @@ -219,18 +222,19 @@ func newCoreService(
}

core := coreService{
bc: chain,
bs: bs,
sf: sf,
dao: dao,
indexer: indexer,
bfIndexer: bfIndexer,
ap: actPool,
cfg: cfg,
registry: registry,
chainListener: NewChainListener(500),
gs: gasstation.NewGasStation(chain, dao, cfg.GasStation),
readCache: NewReadCache(),
bc: chain,
bs: bs,
sf: sf,
dao: dao,
indexer: indexer,
bfIndexer: bfIndexer,
ap: actPool,
cfg: cfg,
registry: registry,
chainListener: NewChainListener(500),
gs: gasstation.NewGasStation(chain, dao, cfg.GasStation),
readCache: NewReadCache(),
delegateManager: dm,
}

for _, opt := range opts {
Expand Down
4 changes: 3 additions & 1 deletion api/serverV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/node"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/state/factory"
)
Expand All @@ -43,9 +44,10 @@ func NewServerV2(
bfIndexer blockindex.BloomFilterIndexer,
actPool actpool.ActPool,
registry *protocol.Registry,
dm node.NodeManager,
opts ...Option,
) (*ServerV2, error) {
coreAPI, err := newCoreService(cfg, chain, bs, sf, dao, indexer, bfIndexer, actPool, registry, opts...)
coreAPI, err := newCoreService(cfg, chain, bs, sf, dao, indexer, bfIndexer, actPool, registry, dm, opts...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/iotexproject/iotex-core/consensus"
rp "github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/node"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state/factory"
Expand Down Expand Up @@ -377,6 +378,12 @@ func (builder *Builder) createBlockchain(forSubChain, forTest bool) blockchain.B
return blockchain.NewBlockchain(builder.cfg.Chain, builder.cfg.Genesis, builder.cs.blockdao, factory.NewMinter(builder.cs.factory, builder.cs.actpool), chainOpts...)
}

func (builder *Builder) buildNodeManager() {
dm := node.NewDelegateManager(&builder.cfg.Node, builder.cs.consensus, builder.cs.p2pAgent, builder.cs.chain)
builder.cs.delegateManager = dm
builder.cs.lifecycle.Add(dm)
}

func (builder *Builder) buildBlockSyncer() error {
if builder.cs.blocksync != nil {
return nil
Expand Down Expand Up @@ -622,6 +629,7 @@ func (builder *Builder) build(forSubChain, forTest bool) (*ChainService, error)
if err := builder.buildBlockSyncer(); err != nil {
return nil, err
}
builder.buildNodeManager()
cs := builder.cs
builder.cs = nil

Expand Down
13 changes: 13 additions & 0 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/consensus"
"github.com/iotexproject/iotex-core/node"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
Expand Down Expand Up @@ -84,6 +85,7 @@ type ChainService struct {
candidateIndexer *poll.CandidateIndexer
candBucketsIndexer *staking.CandidatesBucketsIndexer
registry *protocol.Registry
delegateManager node.NodeManager
}

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

// HandleNodeInfoMsg handles nodeinfo message.
func (cs *ChainService) HandleNodeInfoMsg(ctx context.Context, peer string, msg *iotextypes.NodeInfo) error {
cs.delegateManager.UpdateNode(&node.Node{
Addr: peer,
Height: msg.Height,
Version: msg.Version,
})
return nil
}

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

Expand Down Expand Up @@ -222,6 +234,7 @@ func (cs *ChainService) NewAPIServer(cfg api.Config, plugins map[int]interface{}
cs.bfIndexer,
cs.actpool,
cs.registry,
cs.delegateManager,
apiServerOptions...,
)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/iotexproject/iotex-core/consensus/consensusfsm"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/dispatcher"
"github.com/iotexproject/iotex-core/node"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/log"
)
Expand Down Expand Up @@ -129,6 +130,7 @@ type (
Log log.GlobalConfig `yaml:"log"`
SubLogs map[string]log.GlobalConfig `yaml:"subLogs"`
Genesis genesis.Genesis `yaml:"genesis"`
Node node.Config `yaml:"node"`
}

// Validate is the interface of validating the config
Expand Down
19 changes: 13 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
HandleNodeInfoMsg(context.Context, string, *iotextypes.NodeInfo) error
}

// Dispatcher is used by peers, handles incoming block and header notifications and relays announcements of new blocks.
Expand All @@ -69,12 +70,14 @@ 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"},
)
)

func init() {
Expand Down Expand Up @@ -425,6 +428,10 @@ 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.NodeInfo:
if err := subscriber.HandleNodeInfoMsg(ctx, peer, msg); err != nil {
log.L().Debug("Failed to handle monitor message.", zap.Error(err))
}
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
12 changes: 12 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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 node

import "time"

type Config struct {
NodeInfoBroadcastInterval time.Duration `yaml:"nodeInfoBroadcastInterval"`
}
20 changes: 20 additions & 0 deletions node/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 node

import "github.com/prometheus/client_golang/prometheus"

var nodeDelegateHeightGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "iotex_node_delegate_height_gauge",
Help: "delegate height",
},
[]string{"address", "version"},
)

func init() {
prometheus.MustRegister(nodeDelegateHeightGauge)
}
envestcc marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 75 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 node

import (
"context"

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

type Node struct {
Addr string
Height uint64
Version string
}

// NodeManager manage nodes on the blockchain
type NodeManager interface {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
lifecycle.StartStopper
UpdateNode(*Node)
GetNode(addr string) *Node
}

type delegateManager struct {
cfg Config
nodeMap map[string]*Node
consensus consensus.Consensus
envestcc marked this conversation as resolved.
Show resolved Hide resolved
broadcaster lifecycle.StartStopper
p2pAgent p2p.Agent
bc blockchain.Blockchain
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}

func NewDelegateManager(cfg *Config, consensus consensus.Consensus, p2pAgent p2p.Agent, bc blockchain.Blockchain) NodeManager {
dm := &delegateManager{
cfg: *cfg,
nodeMap: make(map[string]*Node),
consensus: consensus,
p2pAgent: p2pAgent,
bc: bc,
}
dm.broadcaster = routine.NewRecurringTask(dm.broadcast, cfg.NodeInfoBroadcastInterval)
return dm
}

func (dm *delegateManager) Start(ctx context.Context) error {
return dm.broadcaster.Start(ctx)
}
func (dm *delegateManager) Stop(ctx context.Context) error {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
return dm.broadcaster.Stop(ctx)
}
func (dm *delegateManager) UpdateNode(node *Node) {
// update dm.nodeMap
n := *node
dm.nodeMap[node.Addr] = &n
// update metric
nodeDelegateHeightGauge.WithLabelValues("address", node.Addr, "version", node.Version).Set(float64(node.Height))
}
func (dm *delegateManager) GetNode(addr string) *Node {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
return dm.nodeMap[addr]
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}
func (dm *delegateManager) broadcast() {
dm.p2pAgent.BroadcastOutbound(context.Background(), &iotextypes.NodeInfo{
Height: dm.bc.TipHeight(),
Version: version.PackageVersion,
})
}
1 change: 0 additions & 1 deletion server/itx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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