From fa07a0cfd6e92a76428e16291fc5a02024c05905 Mon Sep 17 00:00:00 2001 From: capossele <angelocapossele@gmail.com> Date: Thu, 23 Apr 2020 17:18:19 +0100 Subject: [PATCH] :recycle: refactors drng instance plugin --- plugins/dashboard/drng_livefeed.go | 10 ++-- plugins/drng/drng.go | 40 +++++++++++++++ plugins/drng/plugin.go | 50 +++++-------------- plugins/webapi/drng/info/committee/handler.go | 2 +- .../webapi/drng/info/randomness/handler.go | 2 +- 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/plugins/dashboard/drng_livefeed.go b/plugins/dashboard/drng_livefeed.go index d1ac2bce..3877ed0e 100644 --- a/plugins/dashboard/drng_livefeed.go +++ b/plugins/dashboard/drng_livefeed.go @@ -21,8 +21,8 @@ func configureDrngLiveFeed() { newRandomness := task.Param(0).(state.Randomness) sendToAllWSClient(&wsmsg{MsgTypeDrng, &drngMsg{ - Instance: drng.Instance.State.Committee().InstanceID, - DistributedPK: hex.EncodeToString(drng.Instance.State.Committee().DistributedPK), + Instance: drng.Instance().State.Committee().InstanceID, + DistributedPK: hex.EncodeToString(drng.Instance().State.Committee().DistributedPK), Round: newRandomness.Round, Randomness: hex.EncodeToString(newRandomness.Randomness[:32]), Timestamp: newRandomness.Timestamp.Format("2 Jan 2006 15:04:05")}}) @@ -42,14 +42,14 @@ func runDrngLiveFeed() { }) daemon.BackgroundWorker("Dashboard[DRNGUpdater]", func(shutdownSignal <-chan struct{}) { - if drng.Instance == nil { + if !drng.Enabled() { return } - drng.Instance.Events.Randomness.Attach(notifyNewRandomness) + drng.Instance().Events.Randomness.Attach(notifyNewRandomness) drngLiveFeedWorkerPool.Start() <-shutdownSignal log.Info("Stopping Dashboard[DRNGUpdater] ...") - drng.Instance.Events.Randomness.Detach(notifyNewRandomness) + drng.Instance().Events.Randomness.Detach(notifyNewRandomness) newMsgRateLimiter.Stop() drngLiveFeedWorkerPool.Stop() log.Info("Stopping Dashboard[DRNGUpdater] ... done") diff --git a/plugins/drng/drng.go b/plugins/drng/drng.go index 7f44946f..a3a014b2 100644 --- a/plugins/drng/drng.go +++ b/plugins/drng/drng.go @@ -1,9 +1,13 @@ package drng import ( + "encoding/hex" "errors" "fmt" + "github.com/iotaledger/goshimmer/packages/binary/drng" + "github.com/iotaledger/goshimmer/packages/binary/drng/state" + cbPayload "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload" "github.com/iotaledger/goshimmer/plugins/config" "github.com/iotaledger/hive.go/crypto/ed25519" "github.com/mr-tron/base58/base58" @@ -14,6 +18,42 @@ var ( ErrParsingCommitteeMember = errors.New("cannot parse committee member") ) +func configureDRNG() *drng.DRNG { + // parse identities of the committee members + committeeMembers, err := parseCommitteeMembers() + if err != nil { + log.Warnf("Invalid %s: %s", CfgDRNGCommitteeMembers, err) + } + + // parse distributed public key of the committee + var dpk []byte + if str := config.Node.GetString(CfgDRNGDistributedPubKey); str != "" { + bytes, err := hex.DecodeString(str) + if err != nil { + log.Warnf("Invalid %s: %s", CfgDRNGDistributedPubKey, err) + } + if l := len(bytes); l != cbPayload.PublicKeySize { + log.Warnf("Invalid %s length: %d, need %d", CfgDRNGDistributedPubKey, l, cbPayload.PublicKeySize) + } + dpk = append(dpk, bytes...) + } + + // configure committee + committeeConf := &state.Committee{ + InstanceID: config.Node.GetUint32(CfgDRNGInstanceID), + Threshold: uint8(config.Node.GetUint32(CfgDRNGThreshold)), + DistributedPK: dpk, + Identities: committeeMembers, + } + + return drng.New(state.SetCommittee(committeeConf)) +} + +func Instance() *drng.DRNG { + once.Do(func() { instance = configureDRNG() }) + return instance +} + func parseCommitteeMembers() (result []ed25519.PublicKey, err error) { for _, committeeMember := range config.Node.GetStringSlice(CfgDRNGCommitteeMembers) { if committeeMember == "" { diff --git a/plugins/drng/plugin.go b/plugins/drng/plugin.go index 42949d54..253d17e4 100644 --- a/plugins/drng/plugin.go +++ b/plugins/drng/plugin.go @@ -1,16 +1,13 @@ package drng import ( - "encoding/hex" + "sync" "github.com/iotaledger/goshimmer/packages/binary/drng" "github.com/iotaledger/goshimmer/packages/binary/drng/payload" "github.com/iotaledger/goshimmer/packages/binary/drng/payload/header" - "github.com/iotaledger/goshimmer/packages/binary/drng/state" - cbPayload "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload" "github.com/iotaledger/goshimmer/packages/binary/messagelayer/message" "github.com/iotaledger/goshimmer/packages/binary/messagelayer/tangle" - "github.com/iotaledger/goshimmer/plugins/config" "github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/logger" @@ -24,42 +21,14 @@ const PluginName = "DRNG" var ( // Plugin is the plugin instance of the DRNG plugin. Plugin = node.NewPlugin(PluginName, node.Enabled, configure, run) - Instance *drng.DRNG + instance *drng.DRNG + once sync.Once log *logger.Logger ) func configure(*node.Plugin) { log = logger.NewLogger(PluginName) - - // parse identities of the committee members - committeeMembers, err := parseCommitteeMembers() - if err != nil { - log.Warnf("Invalid %s: %s", CfgDRNGCommitteeMembers, err) - } - - // parse distributed public key of the committee - var dpk []byte - if str := config.Node.GetString(CfgDRNGDistributedPubKey); str != "" { - bytes, err := hex.DecodeString(str) - if err != nil { - log.Warnf("Invalid %s: %s", CfgDRNGDistributedPubKey, err) - } - if l := len(bytes); l != cbPayload.PublicKeySize { - log.Warnf("Invalid %s length: %d, need %d", CfgDRNGDistributedPubKey, l, cbPayload.PublicKeySize) - } - dpk = append(dpk, bytes...) - } - - // configure committee - committeeConf := &state.Committee{ - InstanceID: config.Node.GetUint32(CfgDRNGInstanceID), - Threshold: uint8(config.Node.GetUint32(CfgDRNGThreshold)), - DistributedPK: dpk, - Identities: committeeMembers, - } - - Instance = drng.New(state.SetCommittee(committeeConf)) - + instance = Instance() configureEvents() } @@ -83,13 +52,20 @@ func configureEvents() { log.Info(err) return } - if err := Instance.Dispatch(msg.IssuerPublicKey(), msg.IssuingTime(), parsedPayload); err != nil { + if err := instance.Dispatch(msg.IssuerPublicKey(), msg.IssuingTime(), parsedPayload); err != nil { //TODO: handle error log.Info(err) return } - log.Info(Instance.State.Randomness()) + log.Info(instance.State.Randomness()) }) })) +} +// Enabled returns the enabled status of the plugin +func Enabled() bool { + if Plugin.Status == node.Disabled { + return false + } + return true } diff --git a/plugins/webapi/drng/info/committee/handler.go b/plugins/webapi/drng/info/committee/handler.go index 9b3a7bf8..6462e8a6 100644 --- a/plugins/webapi/drng/info/committee/handler.go +++ b/plugins/webapi/drng/info/committee/handler.go @@ -10,7 +10,7 @@ import ( // Handler returns the current DRNG committee used. func Handler(c echo.Context) error { - committee := drng.Instance.State.Committee() + committee := drng.Instance().State.Committee() return c.JSON(http.StatusOK, Response{ InstanceID: committee.InstanceID, Threshold: committee.Threshold, diff --git a/plugins/webapi/drng/info/randomness/handler.go b/plugins/webapi/drng/info/randomness/handler.go index 1c124b80..7f8c12a3 100644 --- a/plugins/webapi/drng/info/randomness/handler.go +++ b/plugins/webapi/drng/info/randomness/handler.go @@ -10,7 +10,7 @@ import ( // Handler returns the current DRNG randomness used. func Handler(c echo.Context) error { - randomness := drng.Instance.State.Randomness() + randomness := drng.Instance().State.Randomness() return c.JSON(http.StatusOK, Response{ Round: randomness.Round, Randomness: randomness.Randomness, -- GitLab