mirror of https://github.com/certusone/dc4bc.git
fix calculating participantID
This commit is contained in:
parent
6e6e6f0452
commit
25a3450f37
31
dkg/dkg.go
31
dkg/dkg.go
|
@ -3,6 +3,7 @@ package dkg
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -37,7 +38,6 @@ func Init() *DKG {
|
||||||
|
|
||||||
d.deals = make(map[string]*dkg.Deal)
|
d.deals = make(map[string]*dkg.Deal)
|
||||||
d.commits = make(map[string][]kyber.Point)
|
d.commits = make(map[string][]kyber.Point)
|
||||||
d.responses = newMessageStore(9)
|
|
||||||
|
|
||||||
return &d
|
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) {
|
func (d *DKG) InitDKGInstance(t int) (err error) {
|
||||||
sort.Sort(d.pubkeys)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -82,10 +105,6 @@ func (d *DKG) GetDeals() (map[int]*dkg.Deal, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, deal := range deals {
|
|
||||||
d.ParticipantID = int(deal.Index) // Same for each deal.
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return deals, nil
|
return deals, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
main.go
16
main.go
|
@ -23,6 +23,15 @@ type Transport struct {
|
||||||
nodes []*dkglib.DKG
|
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) {
|
func (t *Transport) BroadcastPK(participant string, pk kyber.Point) {
|
||||||
for idx, node := range t.nodes {
|
for idx, node := range t.nodes {
|
||||||
if ok := node.StorePubKey(participant, pk); !ok {
|
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) {
|
func (t *Transport) BroadcastDeals(participant string, deals map[int]*dkg.Deal) {
|
||||||
for index, deal := range deals {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue