Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dkg status metrics #126

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions contracts/evoting/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ var (
},
[]string{"election"},
)

PromElectionDkgStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "dvoting_dkg_status",
Help: "status of distributed key generator",
},
[]string{"election"},
)
)

const (
Expand Down
6 changes: 6 additions & 0 deletions services/dkg/pedersen/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.dedis.ch/dela"
"go.dedis.ch/dela/core/ordering"

"github.com/dedis/d-voting/contracts/evoting"
etypes "github.com/dedis/d-voting/contracts/evoting/types"

"github.com/dedis/d-voting/internal/tracing"
Expand Down Expand Up @@ -134,6 +135,8 @@ func (s *Pedersen) NewActor(electionIDBuf []byte, pool pool.Pool, txmngr txn.Man
status: dkg.Status{Status: dkg.Initialized},
}

evoting.PromElectionDkgStatus.WithLabelValues(electionID).Set(float64(dkg.Initialized))

s.Lock()
defer s.Unlock()
s.actors[electionID] = a
Expand Down Expand Up @@ -169,6 +172,8 @@ func (a *Actor) setErr(err error, args map[string]interface{}) {
Err: err,
Args: args,
}

evoting.PromElectionDkgStatus.WithLabelValues(a.electionID).Set(float64(dkg.Failed))
}

// Setup implements dkg.Actor. It initializes the DKG protocol across all
Expand Down Expand Up @@ -297,6 +302,7 @@ func (a *Actor) Setup() (kyber.Point, error) {
}

a.status = dkg.Status{Status: dkg.Setup}
evoting.PromElectionDkgStatus.WithLabelValues(a.electionID).Set(float64(dkg.Setup))

return dkgPubKeys[0], nil
}
Expand Down
26 changes: 26 additions & 0 deletions services/dkg/pedersen/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
"go.dedis.ch/dela/core/validation"
"golang.org/x/xerrors"

"github.com/dedis/d-voting/contracts/evoting"
etypes "github.com/dedis/d-voting/contracts/evoting/types"
"github.com/dedis/d-voting/internal/testing/fake"
"github.com/dedis/d-voting/services/dkg"
"github.com/dedis/d-voting/services/dkg/pedersen/types"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.dedis.ch/dela/core/ordering/cosipbft/authority"
"go.dedis.ch/dela/core/store/kv"
Expand Down Expand Up @@ -46,12 +48,15 @@ func init() {
// If you get the persistent data from an actor and then recreate an actor
// from that data, the persistent data should be the same in both actors.
func TestActor_MarshalJSON(t *testing.T) {
initMetrics()

p := NewPedersen(fake.Mino{}, &fake.Service{}, &fake.Pool{}, fake.Factory{}, fake.Signer{})

// Create new actor
actor1, err := p.NewActor([]byte("deadbeef"), &fake.Pool{},
fake.Manager{}, NewHandlerData())
require.NoError(t, err)
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))

// Serialize its persistent data
actor1Buf, err := actor1.MarshalJSON()
Expand All @@ -62,8 +67,11 @@ func TestActor_MarshalJSON(t *testing.T) {
err = handlerData.UnmarshalJSON(actor1Buf)
require.NoError(t, err)

initMetrics()

actor2, err := p.NewActor([]byte("beefdead"), &fake.Pool{}, fake.Manager{}, handlerData)
require.NoError(t, err)
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))

// Check that the persistent data is the same for both actors
requireActorsEqual(t, actor1, actor2)
Expand All @@ -72,6 +80,8 @@ func TestActor_MarshalJSON(t *testing.T) {
// After initializing a Pedersen when dkgMap is not empty, the actors map should
// contain the same information as dkgMap
func TestPedersen_InitNonEmptyMap(t *testing.T) {
initMetrics()

// Create a new DKG map and fill it with data
dkgMap := fake.NewInMemoryDB()

Expand Down Expand Up @@ -141,9 +151,14 @@ func TestPedersen_InitNonEmptyMap(t *testing.T) {

_, err = p.NewActor(electionIDBuf, &fake.Pool{}, fake.Manager{}, handlerData)
if err != nil {
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
return err
} else {
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
}

initMetrics()

return nil
})
})
Expand Down Expand Up @@ -301,6 +316,8 @@ func TestPedersen_TwoListens(t *testing.T) {
}

func TestPedersen_Setup(t *testing.T) {
initMetrics()

electionID := "d3adbeef"

service := fake.NewService(electionID, etypes.Election{
Expand All @@ -325,6 +342,9 @@ func TestPedersen_Setup(t *testing.T) {

_, err := actor.Setup()
require.EqualError(t, err, "failed to get election: election does not exist: <nil>")
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))

initMetrics()

actor.electionID = electionID

Expand All @@ -333,6 +353,7 @@ func TestPedersen_Setup(t *testing.T) {

_, err = actor.Setup()
require.EqualError(t, err, fake.Err("failed to stream"))
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))

// RPC is bogus 2
actor.rpc = fake.NewRPC()
Expand Down Expand Up @@ -396,6 +417,7 @@ func TestPedersen_Setup(t *testing.T) {
// We test that particular behaviour later.
_, err = actor.Setup()
require.NoError(t, err)
require.Equal(t, float64(dkg.Setup), testutil.ToFloat64(evoting.PromElectionDkgStatus))
}

func TestPedersen_GetPublicKey(t *testing.T) {
Expand Down Expand Up @@ -623,6 +645,10 @@ func TestPedersen_ComputePubshares_OK(t *testing.T) {
// -----------------------------------------------------------------------------
// Utility functions

func initMetrics() {
evoting.PromElectionDkgStatus.Reset()
}

// actorsEqual checks that two actors hold the same data
func requireActorsEqual(t require.TestingT, actor1, actor2 dkg.Actor) {
actor1Data, err := actor1.MarshalJSON()
Expand Down