Skip to content

Commit

Permalink
implement isDelegate() in nodeinfo (#3804)
Browse files Browse the repository at this point in the history
* implement nodinfo.isDelegate
  • Loading branch information
envestcc authored and dustinxie committed Feb 15, 2023
1 parent 06e7c74 commit c89788a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 44 deletions.
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
17 changes: 14 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,18 @@ 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) buildNodeInfoManager() error {
cs := builder.cs
stk := staking.FindProtocol(cs.Registry())
if stk == nil {
return errors.New("cannot find staking protocol")
}
dm := nodeinfo.NewInfoManager(&builder.cfg.NodeInfo, cs.p2pAgent, cs.chain, builder.cfg.Chain.ProducerPrivateKey(), func(ctx context.Context) (state.CandidateList, error) {
return stk.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 +638,9 @@ func (builder *Builder) build(forSubChain, forTest bool) (*ChainService, error)
if err := builder.buildBlockSyncer(); err != nil {
return nil, err
}
builder.buildNodeInfoManager()
if err := builder.buildNodeInfoManager(); 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
71 changes: 52 additions & 19 deletions nodeinfo/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,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 +40,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 +56,15 @@ type (

// InfoManager manage delegate node info
InfoManager struct {
lifecycle.Lifecycle
version string
address string
delegateCache bool
nodeMap *lru.Cache
broadcastTask *routine.RecurringTask
transmitter transmitter
chain chain
privKey crypto.PrivateKey
getDelegates delegatesGetFunc
}
)

Expand All @@ -71,17 +81,29 @@ func init() {
}

// NewInfoManager new info manager
func NewInfoManager(cfg *Config, t transmitter, h chain, privKey crypto.PrivateKey) *InfoManager {
func NewInfoManager(cfg *Config, t transmitter, ch chain, privKey crypto.PrivateKey, getDelegatesHandler delegatesGetFunc) *InfoManager {
dm := &InfoManager{
nodeMap: lru.New(cfg.NodeMapSize),
transmitter: t,
chain: h,
privKey: privKey,
version: version.PackageVersion,
address: privKey.PublicKey().Address().String(),
nodeMap: lru.New(cfg.NodeMapSize),
transmitter: t,
chain: ch,
privKey: privKey,
version: version.PackageVersion,
address: privKey.PublicKey().Address().String(),
getDelegates: getDelegatesHandler,
}
// 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 {
log.L().Debug("nodeinfo manager feature is disabled")
return
}

dm.broadcastTask = routine.NewRecurringTask(func() {
// delegates or nodes who are turned on will broadcast
if cfg.EnableBroadcastNodeInfo || dm.isDelegate() {
if err := dm.BroadcastNodeInfo(context.Background()); err != nil {
Expand All @@ -91,23 +113,18 @@ func NewInfoManager(cfg *Config, t transmitter, h chain, privKey crypto.PrivateK
log.L().Debug("nodeinfo manager general node disabled node info broadcast")
}
}, cfg.BroadcastNodeInfoInterval)
dm.Add(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 @@ -215,8 +232,24 @@ func (dm *InfoManager) genNodeInfoMsg() (*iotextypes.NodeInfo, error) {
}

func (dm *InfoManager) isDelegate() bool {
// TODO whether i am delegate
return false
if !dm.delegateCache {
if err := dm.updateDelegateCache(); err != nil {
log.L().Error("nodeinfo manager update delegate cache failed", zap.Error(err))
}
}
return dm.delegateCache
}

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.delegateCache = slices.ContainsFunc(candList, func(e *state.Candidate) bool {
return dm.address == e.Address
})
return nil
}

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

0 comments on commit c89788a

Please sign in to comment.