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

implement isDelegate() in nodeinfo #3804

Merged
merged 7 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type (
CorrectGasRefund bool
FixRewardErroCheckPosition bool
EnableWeb3Rewarding bool
EnableNodeInfo bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -246,6 +247,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
CorrectGasRefund: g.IsOkhotsk(height),
FixRewardErroCheckPosition: g.IsOkhotsk(height),
EnableWeb3Rewarding: g.IsToBeEnabled(height),
EnableNodeInfo: g.IsToBeEnabled(height),
},
)
}
Expand Down
18 changes: 15 additions & 3 deletions chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/iotexproject/iotex-core/nodeinfo"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-election/committee"
"github.com/pkg/errors"
Expand Down Expand Up @@ -378,10 +379,19 @@ 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) buildNodeInfoManager() {
dm := nodeinfo.NewInfoManager(&builder.cfg.NodeInfo, builder.cs.p2pAgent, builder.cs.chain, builder.cfg.Chain.ProducerPrivateKey())
func (builder *Builder) buildNodeManager() error {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
cs := builder.cs
protocol := staking.FindProtocol(builder.cs.Registry())
envestcc marked this conversation as resolved.
Show resolved Hide resolved
if protocol == nil {
return errors.New("cannot find staking protocol")
}

dm := nodeinfo.NewInfoManager(&builder.cfg.NodeInfo, builder.cs.p2pAgent, builder.cs.chain, builder.cfg.Chain.ProducerPrivateKey(), func(ctx context.Context) (state.CandidateList, error) {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
return protocol.ActiveCandidates(ctx, cs.factory, 0)
})
builder.cs.nodeInfoManager = dm
builder.cs.lifecycle.Add(dm)
return nil
}

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

Expand Down
1 change: 1 addition & 0 deletions e2etest/nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func newConfigForNodeInfoTest(triePath, dBPath, idxDBPath string) (config.Config
if err != nil {
return cfg, nil, err
}
cfg.Genesis.ToBeEnabledBlockHeight = 0
testTriePath, err := testutil.PathOfTempFile(triePath)
if err != nil {
return cfg, nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ require (
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/shirou/gopsutil/v3 v3.22.2
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/text v0.5.0
)

Expand Down Expand Up @@ -195,7 +196,6 @@ require (
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.2.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,8 @@ golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm0
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY=
golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 h1:xP7rWLUr1e1n2xkK5YB4LI0hPEy3LJC6Wk+D4pGlOJg=
golang.org/x/exp v0.0.0-20230206171751-46f607a40771/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down Expand Up @@ -1607,7 +1609,6 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/xerrors v0.0.0-20190212162355-a5947ffaace3/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
Expand Down
2 changes: 2 additions & 0 deletions nodeinfo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ type Config struct {
EnableBroadcastNodeInfo bool `yaml:"enableBroadcastNodeInfo"`
BroadcastNodeInfoInterval time.Duration `yaml:"broadcastNodeInfoInterval"`
NodeMapSize int `yaml:"nodeMapSize"`
DelegateCacheTTL time.Duration `yaml:"delegateCacheTTL"`
}

// DefaultConfig is the default config
var DefaultConfig = Config{
EnableBroadcastNodeInfo: false,
BroadcastNodeInfoInterval: 5 * time.Minute,
NodeMapSize: 1000,
DelegateCacheTTL: 30 * time.Minute,
}
87 changes: 58 additions & 29 deletions nodeinfo/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package nodeinfo

import (
"context"
"sync/atomic"
"time"

"github.com/iotexproject/go-pkgs/cache/lru"
Expand All @@ -17,13 +18,18 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/routine"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
"github.com/iotexproject/iotex-core/state"
)

type (
Expand All @@ -35,8 +41,11 @@ type (

chain interface {
TipHeight() uint64
Genesis() genesis.Genesis
}

delegatesGetFunc func(context.Context) (state.CandidateList, error)

// Info node infomation
Info struct {
Version string
Expand All @@ -48,13 +57,15 @@ type (

// InfoManager manage delegate node info
InfoManager struct {
version string
address string
nodeMap *lru.Cache
broadcastTask *routine.RecurringTask
transmitter transmitter
chain chain
privKey crypto.PrivateKey
lifecycle.Lifecycle
envestcc marked this conversation as resolved.
Show resolved Hide resolved
version string
address string
isDelegate atomic.Value // bool
envestcc marked this conversation as resolved.
Show resolved Hide resolved
nodeMap *lru.Cache
transmitter transmitter
chain chain
privKey crypto.PrivateKey
getDelegates delegatesGetFunc
}
)

Expand All @@ -71,43 +82,54 @@ func init() {
}

// NewInfoManager new info manager
func NewInfoManager(cfg *Config, t transmitter, h chain, privKey crypto.PrivateKey) *InfoManager {
func NewInfoManager(cfg *Config, t transmitter, h chain, privKey crypto.PrivateKey, fun delegatesGetFunc) *InfoManager {
dm := &InfoManager{
nodeMap: lru.New(cfg.NodeMapSize),
transmitter: t,
chain: h,
privKey: privKey,
version: version.PackageVersion,
address: privKey.PublicKey().Address().String(),
}

dm.broadcastTask = routine.NewRecurringTask(func() {
nodeMap: lru.New(cfg.NodeMapSize),
transmitter: t,
chain: h,
envestcc marked this conversation as resolved.
Show resolved Hide resolved
privKey: privKey,
version: version.PackageVersion,
address: privKey.PublicKey().Address().String(),
getDelegates: fun,
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}
dm.isDelegate.Store(false)
// init recurring tasks
broadcastTask := routine.NewRecurringTask(func() {
ctx := protocol.WithFeatureCtx(
protocol.WithBlockCtx(
genesis.WithGenesisContext(context.Background(), dm.chain.Genesis()),
protocol.BlockCtx{BlockHeight: dm.chain.TipHeight()},
),
)
if !protocol.MustGetFeatureCtx(ctx).EnableNodeInfo {
return
}
// delegates or nodes who are turned on will broadcast
if cfg.EnableBroadcastNodeInfo || dm.isDelegate() {
if cfg.EnableBroadcastNodeInfo || dm.isDelegate.Load().(bool) {
if err := dm.BroadcastNodeInfo(context.Background()); err != nil {
log.L().Error("nodeinfo manager broadcast node info failed", zap.Error(err))
}
} else {
log.L().Debug("nodeinfo manager general node disabled node info broadcast")
}
}, cfg.BroadcastNodeInfoInterval)
updateDelegateCacheTask := routine.NewRecurringTask(func() {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
if err := dm.updateDelegateCache(); err != nil {
log.L().Error("nodeinfo manager update delegate cache failed", zap.Error(err))
}
}, cfg.DelegateCacheTTL)
dm.AddModels(updateDelegateCacheTask, broadcastTask)
return dm
}

// Start start delegate broadcast task
func (dm *InfoManager) Start(ctx context.Context) error {
if dm.broadcastTask != nil {
return dm.broadcastTask.Start(ctx)
}
return nil
return dm.OnStart(ctx)
}

// Stop stop delegate broadcast task
func (dm *InfoManager) Stop(ctx context.Context) error {
if dm.broadcastTask != nil {
return dm.broadcastTask.Stop(ctx)
}
return nil
return dm.OnStop(ctx)
}

// HandleNodeInfo handle node info message
Expand Down Expand Up @@ -214,9 +236,16 @@ func (dm *InfoManager) genNodeInfoMsg() (*iotextypes.NodeInfo, error) {
return req, nil
}

func (dm *InfoManager) isDelegate() bool {
// TODO whether i am delegate
return false
func (dm *InfoManager) updateDelegateCache() error {
candList, err := dm.getDelegates(context.Background())
if err != nil {
return err
}
log.L().Debug("nodeinfo manager active candidates", zap.Any("candidates", candList))
dm.isDelegate.Store(slices.ContainsFunc(candList, func(e *state.Candidate) bool {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
return dm.address == e.Address
}))
return nil
}

func hashNodeInfo(msg *iotextypes.NodeInfoCore) hash.Hash256 {
Expand Down
Loading