Skip to content
Snippets Groups Projects
Unverified Commit bf29fc45 authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Merge pull request #358 from iotaledger/fix/dashboard-drng

Fix SIGSEGV when drng plugin is disabled
parents 10e14141 7020446b
No related branches found
No related tags found
No related merge requests found
...@@ -21,8 +21,8 @@ func configureDrngLiveFeed() { ...@@ -21,8 +21,8 @@ func configureDrngLiveFeed() {
newRandomness := task.Param(0).(state.Randomness) newRandomness := task.Param(0).(state.Randomness)
sendToAllWSClient(&wsmsg{MsgTypeDrng, &drngMsg{ sendToAllWSClient(&wsmsg{MsgTypeDrng, &drngMsg{
Instance: drng.Instance.State.Committee().InstanceID, Instance: drng.Instance().State.Committee().InstanceID,
DistributedPK: hex.EncodeToString(drng.Instance.State.Committee().DistributedPK), DistributedPK: hex.EncodeToString(drng.Instance().State.Committee().DistributedPK),
Round: newRandomness.Round, Round: newRandomness.Round,
Randomness: hex.EncodeToString(newRandomness.Randomness[:32]), Randomness: hex.EncodeToString(newRandomness.Randomness[:32]),
Timestamp: newRandomness.Timestamp.Format("2 Jan 2006 15:04:05")}}) Timestamp: newRandomness.Timestamp.Format("2 Jan 2006 15:04:05")}})
...@@ -32,23 +32,25 @@ func configureDrngLiveFeed() { ...@@ -32,23 +32,25 @@ func configureDrngLiveFeed() {
} }
func runDrngLiveFeed() { func runDrngLiveFeed() {
newMsgRateLimiter := time.NewTicker(time.Second / 10)
notifyNewRandomness := events.NewClosure(func(message state.Randomness) {
select {
case <-newMsgRateLimiter.C:
drngLiveFeedWorkerPool.TrySubmit(message)
default:
}
})
daemon.BackgroundWorker("Dashboard[DRNGUpdater]", func(shutdownSignal <-chan struct{}) { daemon.BackgroundWorker("Dashboard[DRNGUpdater]", func(shutdownSignal <-chan struct{}) {
drng.Instance.Events.Randomness.Attach(notifyNewRandomness) newMsgRateLimiter := time.NewTicker(time.Second / 10)
defer newMsgRateLimiter.Stop()
notifyNewRandomness := events.NewClosure(func(message state.Randomness) {
select {
case <-newMsgRateLimiter.C:
drngLiveFeedWorkerPool.TrySubmit(message)
default:
}
})
drng.Instance().Events.Randomness.Attach(notifyNewRandomness)
drngLiveFeedWorkerPool.Start() drngLiveFeedWorkerPool.Start()
defer drngLiveFeedWorkerPool.Stop()
<-shutdownSignal <-shutdownSignal
log.Info("Stopping Dashboard[DRNGUpdater] ...") 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") log.Info("Stopping Dashboard[DRNGUpdater] ... done")
}, shutdown.PriorityDashboard) }, shutdown.PriorityDashboard)
} }
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/banner" "github.com/iotaledger/goshimmer/plugins/banner"
"github.com/iotaledger/goshimmer/plugins/config" "github.com/iotaledger/goshimmer/plugins/config"
"github.com/iotaledger/goshimmer/plugins/drng"
"github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/metrics" "github.com/iotaledger/goshimmer/plugins/metrics"
...@@ -77,7 +78,11 @@ func run(plugin *node.Plugin) { ...@@ -77,7 +78,11 @@ func run(plugin *node.Plugin) {
}, shutdown.PriorityDashboard) }, shutdown.PriorityDashboard)
runLiveFeed() runLiveFeed()
runDrngLiveFeed()
// run dRNG live feed if dRNG plugin is enabled
if !node.IsSkipped(drng.Plugin) {
runDrngLiveFeed()
}
// allow any origin for websocket connections // allow any origin for websocket connections
upgrader.CheckOrigin = func(r *http.Request) bool { upgrader.CheckOrigin = func(r *http.Request) bool {
......
package drng package drng
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "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/goshimmer/plugins/config"
"github.com/iotaledger/hive.go/crypto/ed25519" "github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/logger"
"github.com/mr-tron/base58/base58" "github.com/mr-tron/base58/base58"
) )
...@@ -14,6 +19,44 @@ var ( ...@@ -14,6 +19,44 @@ var (
ErrParsingCommitteeMember = errors.New("cannot parse committee member") ErrParsingCommitteeMember = errors.New("cannot parse committee member")
) )
func configureDRNG() *drng.DRNG {
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,
}
return drng.New(state.SetCommittee(committeeConf))
}
// Instance returns the DRNG instance.
func Instance() *drng.DRNG {
once.Do(func() { instance = configureDRNG() })
return instance
}
func parseCommitteeMembers() (result []ed25519.PublicKey, err error) { func parseCommitteeMembers() (result []ed25519.PublicKey, err error) {
for _, committeeMember := range config.Node.GetStringSlice(CfgDRNGCommitteeMembers) { for _, committeeMember := range config.Node.GetStringSlice(CfgDRNGCommitteeMembers) {
if committeeMember == "" { if committeeMember == "" {
......
...@@ -5,10 +5,14 @@ import ( ...@@ -5,10 +5,14 @@ import (
) )
const ( const (
CfgDRNGInstanceID = "drng.instanceId" // CfgDRNGInstanceID defines the config flag of the DRNG instanceID.
CfgDRNGThreshold = "drng.threshold" CfgDRNGInstanceID = "drng.instanceId"
// CfgDRNGThreshold defines the config flag of the DRNG threshold.
CfgDRNGThreshold = "drng.threshold"
// CfgDRNGDistributedPubKey defines the config flag of the DRNG distributed Public Key.
CfgDRNGDistributedPubKey = "drng.distributedPubKey" CfgDRNGDistributedPubKey = "drng.distributedPubKey"
CfgDRNGCommitteeMembers = "drng.committeeMembers" // CfgDRNGCommitteeMembers defines the config flag of the DRNG committee members identities.
CfgDRNGCommitteeMembers = "drng.committeeMembers"
) )
func init() { func init() {
......
package drng package drng
import ( import (
"encoding/hex" "sync"
"github.com/iotaledger/goshimmer/packages/binary/drng" "github.com/iotaledger/goshimmer/packages/binary/drng"
"github.com/iotaledger/goshimmer/packages/binary/drng/payload" "github.com/iotaledger/goshimmer/packages/binary/drng/payload"
"github.com/iotaledger/goshimmer/packages/binary/drng/payload/header" "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/message"
"github.com/iotaledger/goshimmer/packages/binary/messagelayer/tangle" "github.com/iotaledger/goshimmer/packages/binary/messagelayer/tangle"
"github.com/iotaledger/goshimmer/plugins/config"
"github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
...@@ -24,48 +21,19 @@ const PluginName = "DRNG" ...@@ -24,48 +21,19 @@ const PluginName = "DRNG"
var ( var (
// Plugin is the plugin instance of the DRNG plugin. // Plugin is the plugin instance of the DRNG plugin.
Plugin = node.NewPlugin(PluginName, node.Enabled, configure, run) Plugin = node.NewPlugin(PluginName, node.Enabled, configure, run)
Instance *drng.DRNG instance *drng.DRNG
once sync.Once
log *logger.Logger log *logger.Logger
) )
func configure(*node.Plugin) { 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))
configureEvents() configureEvents()
} }
func run(*node.Plugin) {} func run(*node.Plugin) {}
func configureEvents() { func configureEvents() {
instance := Instance()
messagelayer.Tangle.Events.MessageSolid.Attach(events.NewClosure(func(cachedMessage *message.CachedMessage, cachedMessageMetadata *tangle.CachedMessageMetadata) { messagelayer.Tangle.Events.MessageSolid.Attach(events.NewClosure(func(cachedMessage *message.CachedMessage, cachedMessageMetadata *tangle.CachedMessageMetadata) {
cachedMessageMetadata.Release() cachedMessageMetadata.Release()
...@@ -83,13 +51,12 @@ func configureEvents() { ...@@ -83,13 +51,12 @@ func configureEvents() {
log.Info(err) log.Info(err)
return 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 //TODO: handle error
log.Info(err) log.Info(err)
return return
} }
log.Info(Instance.State.Randomness()) log.Info(instance.State.Randomness())
}) })
})) }))
} }
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
// Handler returns the current DRNG committee used. // Handler returns the current DRNG committee used.
func Handler(c echo.Context) error { func Handler(c echo.Context) error {
committee := drng.Instance.State.Committee() committee := drng.Instance().State.Committee()
return c.JSON(http.StatusOK, Response{ return c.JSON(http.StatusOK, Response{
InstanceID: committee.InstanceID, InstanceID: committee.InstanceID,
Threshold: committee.Threshold, Threshold: committee.Threshold,
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
// Handler returns the current DRNG randomness used. // Handler returns the current DRNG randomness used.
func Handler(c echo.Context) error { func Handler(c echo.Context) error {
randomness := drng.Instance.State.Randomness() randomness := drng.Instance().State.Randomness()
return c.JSON(http.StatusOK, Response{ return c.JSON(http.StatusOK, Response{
Round: randomness.Round, Round: randomness.Round,
Randomness: randomness.Randomness, 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