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() {
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")}})
......@@ -32,23 +32,25 @@ func configureDrngLiveFeed() {
}
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{}) {
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()
defer drngLiveFeedWorkerPool.Stop()
<-shutdownSignal
log.Info("Stopping Dashboard[DRNGUpdater] ...")
drng.Instance.Events.Randomness.Detach(notifyNewRandomness)
newMsgRateLimiter.Stop()
drngLiveFeedWorkerPool.Stop()
drng.Instance().Events.Randomness.Detach(notifyNewRandomness)
log.Info("Stopping Dashboard[DRNGUpdater] ... done")
}, shutdown.PriorityDashboard)
}
......@@ -14,6 +14,7 @@ import (
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/banner"
"github.com/iotaledger/goshimmer/plugins/config"
"github.com/iotaledger/goshimmer/plugins/drng"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/metrics"
......@@ -77,7 +78,11 @@ func run(plugin *node.Plugin) {
}, shutdown.PriorityDashboard)
runLiveFeed()
runDrngLiveFeed()
// run dRNG live feed if dRNG plugin is enabled
if !node.IsSkipped(drng.Plugin) {
runDrngLiveFeed()
}
// allow any origin for websocket connections
upgrader.CheckOrigin = func(r *http.Request) bool {
......
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/iotaledger/hive.go/logger"
"github.com/mr-tron/base58/base58"
)
......@@ -14,6 +19,44 @@ var (
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) {
for _, committeeMember := range config.Node.GetStringSlice(CfgDRNGCommitteeMembers) {
if committeeMember == "" {
......
......@@ -5,10 +5,14 @@ import (
)
const (
CfgDRNGInstanceID = "drng.instanceId"
CfgDRNGThreshold = "drng.threshold"
// CfgDRNGInstanceID defines the config flag of the DRNG instanceID.
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"
CfgDRNGCommitteeMembers = "drng.committeeMembers"
// CfgDRNGCommitteeMembers defines the config flag of the DRNG committee members identities.
CfgDRNGCommitteeMembers = "drng.committeeMembers"
)
func init() {
......
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,48 +21,19 @@ 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))
func configure(_ *node.Plugin) {
configureEvents()
}
func run(*node.Plugin) {}
func configureEvents() {
instance := Instance()
messagelayer.Tangle.Events.MessageSolid.Attach(events.NewClosure(func(cachedMessage *message.CachedMessage, cachedMessageMetadata *tangle.CachedMessageMetadata) {
cachedMessageMetadata.Release()
......@@ -83,13 +51,12 @@ 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())
})
}))
}
......@@ -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