Skip to content
Snippets Groups Projects
Commit fa07a0cf authored by capossele's avatar capossele
Browse files

:recycle: refactors drng instance plugin

parent af89d99a
No related branches found
No related tags found
No related merge requests found
......@@ -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")
......
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 == "" {
......
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
}
......@@ -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,
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment