Skip to content

Commit

Permalink
Correctly store and retrieve DKGs
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Mar 1, 2024
1 parent d0c8171 commit 492dff8
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 109 deletions.
2 changes: 1 addition & 1 deletion integration/dvotingdela.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func newDVotingNode(t require.TestingT, path string, randSource rand.Source) dVo

formFac := etypes.NewFormFactory(etypes.CiphervoteFactory{}, rosterFac)

dkg := pedersen.NewPedersen(onet, srvc, pool, formFac, signer)
dkg := pedersen.NewPedersen(onet, srvc, db, pool, formFac, signer)

evoting.RegisterContract(exec, evoting.NewContract(accessService, dkg, rosterFac))

Expand Down
55 changes: 2 additions & 53 deletions services/dkg/pedersen/controller/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,11 @@ func (a *initAction) Execute(ctx node.Context) error {
return xerrors.Errorf("failed to make client: %v", err)
}

actor, err := dkg.Listen(formIDBuf, signed.NewManager(signer, &client))
_, err = dkg.Listen(formIDBuf, signed.NewManager(signer, &client))
if err != nil {
return xerrors.Errorf("failed to start the RPC: %v", err)
}

dela.Logger.Info().Msg("DKG has been initialized successfully")

err = updateDKGStore(ctx.Injector, func(tx kv.WritableTx) error {
bucket, err := tx.GetBucketOrCreate([]byte(BucketName))
if err != nil {
return err
}

actorBuf, err := actor.MarshalJSON()
if err != nil {
return err
}

return bucket.Set(formIDBuf, actorBuf)
})
if err != nil {
return xerrors.Errorf("failed to update DKG store: %v", err)
}

dela.Logger.Info().Msgf("DKG was successfully linked to form %v", formIDBuf)

return nil
Expand Down Expand Up @@ -137,23 +118,6 @@ func (a *setupAction) Execute(ctx node.Context) error {
Hex("DKG public key", pubkeyBuf).
Msg("DKG public key")

err = updateDKGStore(ctx.Injector, func(tx kv.WritableTx) error {
bucket, err := tx.GetBucketOrCreate([]byte(BucketName))
if err != nil {
return err
}

actorBuf, err := actor.MarshalJSON()
if err != nil {
return err
}

return bucket.Set(formIDBuf, actorBuf)
})
if err != nil {
return xerrors.Errorf("failed to update DKG store: %v", err)
}

return nil
}

Expand Down Expand Up @@ -190,7 +154,7 @@ func (a *exportInfoAction) Execute(ctx node.Context) error {
}

err = db.View(func(tx kv.ReadableTx) error {
bucket := tx.GetBucket([]byte(BucketName))
bucket := tx.GetBucket([]byte(pedersen.BucketName))
if bucket == nil {
return nil
}
Expand Down Expand Up @@ -333,21 +297,6 @@ func (a *RegisterHandlersAction) Execute(ctx node.Context) error {
return nil
}

func updateDKGStore(inj node.Injector, fn func(kv.WritableTx) error) error {
var db kv.DB
err := inj.Resolve(&db)
if err != nil {
return xerrors.Errorf("failed to resolve db: %v", err)
}

err = db.Update(fn)
if err != nil {
return xerrors.Errorf("failed to update: %v", err)
}

return nil
}

func makeClient(inj node.Injector) (client, error) {
var service ordering.Service
err := inj.Resolve(&service)
Expand Down
3 changes: 2 additions & 1 deletion services/dkg/pedersen/controller/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"testing"

"github.com/c4dt/d-voting/services/dkg/pedersen"
"golang.org/x/xerrors"

"github.com/c4dt/d-voting/internal/testing/fake"
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestSetupAction_Execute(t *testing.T) {

// Check that the map contains the actor
err = db.View(func(tx kv.ReadableTx) error {
bucket := tx.GetBucket([]byte(BucketName))
bucket := tx.GetBucket([]byte(pedersen.BucketName))
require.NotNil(t, bucket)

pubKeyBuf := bucket.Get(formIDBuf)
Expand Down
30 changes: 3 additions & 27 deletions services/dkg/pedersen/controller/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package controller

import (
"encoding"
"encoding/json"
"path/filepath"

"github.com/c4dt/d-voting/contracts/evoting"
"go.dedis.ch/dela/core/txn/pool"
"go.dedis.ch/dela/core/txn/signed"
"go.dedis.ch/dela/crypto"
"go.dedis.ch/dela/crypto/bls"
"go.dedis.ch/dela/crypto/loader"

"github.com/c4dt/d-voting/contracts/evoting"
"github.com/c4dt/d-voting/services/dkg/pedersen"
"go.dedis.ch/dela/cli"
"go.dedis.ch/dela/cli/node"
Expand All @@ -27,8 +26,6 @@ import (
etypes "github.com/c4dt/d-voting/contracts/evoting/types"
)

// BucketName is the name of the bucket in the database.
const BucketName = "dkgmap"
const privateKeyFile = "private.key"

// NewController returns a new controller initializer
Expand Down Expand Up @@ -150,31 +147,10 @@ func (m controller) OnStart(ctx cli.Flags, inj node.Injector) error {

formFac := etypes.NewFormFactory(etypes.CiphervoteFactory{}, rosterFac)

dkg := pedersen.NewPedersen(no, srvc, p, formFac, signer)
dkg := pedersen.NewPedersen(no, srvc, db, p, formFac, signer)

// Use dkgMap to fill the actors map
err = db.View(func(tx kv.ReadableTx) error {
bucket := tx.GetBucket([]byte(BucketName))
if bucket == nil {
return nil
}

return bucket.ForEach(func(formIDBuf, handlerDataBuf []byte) error {

handlerData := pedersen.HandlerData{}
err = json.Unmarshal(handlerDataBuf, &handlerData)
if err != nil {
return err
}

_, err = dkg.NewActor(formIDBuf, p, signed.NewManager(signer, &client), handlerData)
if err != nil {
return err
}

return nil
})
})
err = dkg.ReadActors(signed.NewManager(signer, &client))
if err != nil {
return xerrors.Errorf("database read failed: %v", err)
}
Expand Down
48 changes: 34 additions & 14 deletions services/dkg/pedersen/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"container/list"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io"
Expand All @@ -19,10 +18,10 @@ import (
"go.dedis.ch/dela/core/txn"
"go.dedis.ch/dela/core/txn/pool"
"go.dedis.ch/dela/crypto"
"go.dedis.ch/dela/mino/minogrpc/session"
jsondela "go.dedis.ch/dela/serde/json"

etypes "github.com/c4dt/d-voting/contracts/evoting/types"
"github.com/c4dt/d-voting/internal/testing/fake"
"github.com/c4dt/d-voting/services/dkg"
"github.com/c4dt/d-voting/services/dkg/pedersen/types"
"go.dedis.ch/dela"
Expand Down Expand Up @@ -72,13 +71,16 @@ type Handler struct {
log zerolog.Logger
running bool

saveState func(*Handler)

status *dkg.Status
}

// NewHandler creates a new handler
func NewHandler(me mino.Address, service ordering.Service, pool pool.Pool,
txnmngr txn.Manager, pubSharesSigner crypto.Signer, handlerData HandlerData,
context serde.Context, formFac serde.Factory, status *dkg.Status) *Handler {
context serde.Context, formFac serde.Factory, status *dkg.Status,
saveState func(*Handler)) *Handler {

privKey := handlerData.PrivKey
pubKey := handlerData.PubKey
Expand All @@ -105,7 +107,8 @@ func NewHandler(me mino.Address, service ordering.Service, pool pool.Pool,
log: log,
running: false,

status: status,
saveState: saveState,
status: status,
}
}

Expand Down Expand Up @@ -299,6 +302,7 @@ func (h *Handler) doDKG(deals, resps *list.List, out mino.Sender, from mino.Addr
dela.Logger.Error().Msgf("got an error while sending pub key: %v", err)
return
}
h.saveState(h)
}

func (h *Handler) deal(out mino.Sender) error {
Expand Down Expand Up @@ -612,7 +616,7 @@ func (hd *HandlerData) MarshalJSON() ([]byte, error) {
return nil, err
}

return json.Marshal(&struct {
ret, err := json.Marshal(&struct {
StartRes []byte `json:",omitempty"`
PrivShare []byte `json:",omitempty"`
PubKey []byte
Expand All @@ -623,6 +627,8 @@ func (hd *HandlerData) MarshalJSON() ([]byte, error) {
PubKey: pubKeyBuf,
PrivKey: privKeyBuf,
})

return ret, err
}

// UnmarshalJSON fills a HandlerData with previously marshalled data.
Expand All @@ -640,7 +646,10 @@ func (hd *HandlerData) UnmarshalJSON(data []byte) error {

// Unmarshal StartRes
hd.StartRes = &state{}
hd.StartRes.UnmarshalJSON(aux.StartRes)
err = hd.StartRes.UnmarshalJSON(aux.StartRes)
if err != nil {
return err
}

// Unmarshal PrivShare
if aux.PrivShare == nil {
Expand All @@ -655,7 +664,10 @@ func (hd *HandlerData) UnmarshalJSON(data []byte) error {
return err
}
privShareV := suite.Scalar()
privShareV.UnmarshalBinary(privShareBuf.V)
err = privShareV.UnmarshalBinary(privShareBuf.V)
if err != nil {
return err
}
privShare := &share.PriShare{
I: privShareBuf.I,
V: privShareV,
Expand All @@ -665,12 +677,18 @@ func (hd *HandlerData) UnmarshalJSON(data []byte) error {

// Unmarshal PubKey
pubKey := suite.Point()
pubKey.UnmarshalBinary(aux.PubKey)
err = pubKey.UnmarshalBinary(aux.PubKey)
if err != nil {
return err
}
hd.PubKey = pubKey

// Unmarshal PrivKey
privKey := suite.Scalar()
privKey.UnmarshalBinary(aux.PrivKey)
err = privKey.UnmarshalBinary(aux.PrivKey)
if err != nil {
return err
}
hd.PrivKey = privKey

return nil
Expand Down Expand Up @@ -739,13 +757,15 @@ func (s *state) MarshalJSON() ([]byte, error) {
}
}

return json.Marshal(&struct {
ret, err := json.Marshal(&struct {
DistKey []byte `json:",omitempty"`
Participants [][]byte `json:",omitempty"`
}{
DistKey: distKeyBuf,
Participants: participantsBuf,
})

return ret, err
}

func (s *state) UnmarshalJSON(data []byte) error {
Expand All @@ -770,11 +790,11 @@ func (s *state) UnmarshalJSON(data []byte) error {
}

if aux.Participants != nil {
// TODO: Is using a fake implementation a problem?
f := fake.NewBadMino().GetAddressFactory()
// TODO: use addressFactory here
f := session.AddressFactory{}
var participants = make([]mino.Address, len(aux.Participants))
for i := 0; i < len(aux.Participants); i++ {
participants[i] = f.FromText(aux.Participants[i])
for i, partStr := range aux.Participants {
participants[i] = f.FromText(partStr)
}
s.SetParticipants(participants)
} else {
Expand Down
4 changes: 3 additions & 1 deletion services/dkg/pedersen/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
formTypes "github.com/c4dt/d-voting/contracts/evoting/types"
"go.dedis.ch/dela/core/access"
"go.dedis.ch/dela/core/txn/signed"
"go.dedis.ch/dela/mino/minogrpc/session"
"go.dedis.ch/dela/serde/json"

"github.com/c4dt/d-voting/internal/testing/fake"
Expand Down Expand Up @@ -264,7 +265,8 @@ func TestState_MarshalJSON(t *testing.T) {

// Try with some data
distKey := suite.Point().Pick(suite.RandomStream())
participants := []mino.Address{fake.NewAddress(0), fake.NewAddress(1)}
// TODO: use AddressFactory here
participants := []mino.Address{session.NewAddress("grpcs://localhost:12345"), session.NewAddress("grpcs://localhost:1234")}

s1.SetDistKey(distKey)
s1.SetParticipants(participants)
Expand Down
Loading

0 comments on commit 492dff8

Please sign in to comment.