fix calculating participantID

This commit is contained in:
programmer10110 2020-07-26 20:46:39 +03:00
parent 6e6e6f0452
commit 25a3450f37
2 changed files with 40 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package dkg
import (
"errors"
"fmt"
"math"
"sort"
"sync"
@ -37,7 +38,6 @@ func Init() *DKG {
d.deals = make(map[string]*dkg.Deal)
d.commits = make(map[string][]kyber.Point)
d.responses = newMessageStore(9)
return &d
}
@ -56,10 +56,33 @@ func (d *DKG) StorePubKey(participant string, pk kyber.Point) bool {
})
}
func (d *DKG) calcParticipantID() int {
for idx, p := range d.pubkeys {
if p.PK.Equal(d.pubKey) {
return idx
}
}
return -1
}
func (d *DKG) InitDKGInstance(t int) (err error) {
sort.Sort(d.pubkeys)
d.instance, err = dkg.NewDistKeyGenerator(d.suite, d.secKey, d.pubkeys.GetPKs(), t)
publicKeys := d.pubkeys.GetPKs()
participantsCount := len(publicKeys)
participantID := d.calcParticipantID()
if participantID < 0 {
return fmt.Errorf("failed to determine participant index")
}
d.ParticipantID = participantID
d.responses = newMessageStore(int(math.Pow(float64(participantsCount)-1, 2)))
d.instance, err = dkg.NewDistKeyGenerator(d.suite, d.secKey, publicKeys, t)
if err != nil {
return err
}
@ -82,10 +105,6 @@ func (d *DKG) GetDeals() (map[int]*dkg.Deal, error) {
if err != nil {
return nil, err
}
for _, deal := range deals {
d.ParticipantID = int(deal.Index) // Same for each deal.
break
}
return deals, nil
}

16
main.go
View File

@ -23,6 +23,15 @@ type Transport struct {
nodes []*dkglib.DKG
}
func (t *Transport) getNodeByParticipantID(id int) *dkglib.DKG {
for _, node := range t.nodes {
if node.ParticipantID == id {
return node
}
}
return nil
}
func (t *Transport) BroadcastPK(participant string, pk kyber.Point) {
for idx, node := range t.nodes {
if ok := node.StorePubKey(participant, pk); !ok {
@ -39,7 +48,12 @@ func (t *Transport) BroadcastCommits(participant string, commits []kyber.Point)
func (t *Transport) BroadcastDeals(participant string, deals map[int]*dkg.Deal) {
for index, deal := range deals {
t.nodes[index].StoreDeal(participant, deal)
dstNode := t.getNodeByParticipantID(index)
if dstNode == nil {
fmt.Printf("Node with index #%d not found\n", index)
continue
}
dstNode.StoreDeal(participant, deal)
}
}