diff --git a/plugins/dashboard/drng_livefeed.go b/plugins/dashboard/drng_livefeed.go index d1ac2bce5fda7c2f99ad4191a19265a0f2997c13..3877ed0e2d1a0e2d3cb286812fa8d3ecfc171440 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 7f44946f9de4f6ed79321e6f7840e097627aba7c..a3a014b288d7038fa07c13b81409164658beb758 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 42949d5468d5fa7854cb4309b970aca07f1765ac..253d17e42fc6e5da7ac594ec4b102d53c6540223 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 9b3a7bf8ff8db02929e4d06f8e94ac3526f7e71a..6462e8a6cd743be2ed3618e8a434e9d08da7024a 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 1c124b80afb646d07fd61df648bbe5c35d24318d..7f8c12a34981867cf6b7f4d3cd966a135952c7df 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,