Skip to content

Commit 34c8bac

Browse files
author
Andrei Smirnov
committed
dkg: frostp2p code quality
1 parent bfc2739 commit 34c8bac

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

‎dkg/frostp2p.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,13 @@ func newBcastCallback(peers map[peer.ID]cluster.NodeIdx, round1CastsRecv chan *p
140140
return errors.New("invalid round 1 casts message")
141141
}
142142

143+
peerNode, ok := peers[pID]
144+
if !ok {
145+
return errors.New("unknown peer in round 1 cast", z.Any("peer", p2p.PeerName(pID)))
146+
}
147+
143148
for _, cast := range msg.GetCasts() {
144-
if int(cast.GetKey().GetSourceId()) != peers[pID].ShareIdx {
149+
if int(cast.GetKey().GetSourceId()) != peerNode.ShareIdx {
145150
return errors.New("invalid round 1 cast source ID")
146151
} else if cast.GetKey().GetTargetId() != 0 {
147152
return errors.New("invalid round 1 cast target ID")
@@ -174,8 +179,13 @@ func newBcastCallback(peers map[peer.ID]cluster.NodeIdx, round1CastsRecv chan *p
174179
return errors.New("invalid round 2 casts message")
175180
}
176181

182+
peerNode, ok := peers[pID]
183+
if !ok {
184+
return errors.New("unknown peer in round 2 cast", z.Any("peer", p2p.PeerName(pID)))
185+
}
186+
177187
for _, cast := range msg.GetCasts() {
178-
if int(cast.GetKey().GetSourceId()) != peers[pID].ShareIdx {
188+
if int(cast.GetKey().GetSourceId()) != peerNode.ShareIdx {
179189
return errors.New("invalid round 2 cast source ID")
180190
} else if cast.GetKey().GetTargetId() != 0 {
181191
return errors.New("invalid round 2 cast target ID")
@@ -209,18 +219,28 @@ func newP2PCallback(p2pNode host.Host, peers map[peer.ID]cluster.NodeIdx, round1
209219
return nil, false, errors.New("invalid round 1 p2p message")
210220
}
211221

222+
sourcePeer, ok := peers[pID]
223+
if !ok {
224+
return nil, false, errors.New("unknown source peer in round 1 p2p", z.Any("peer", p2p.PeerName(pID)))
225+
}
226+
227+
targetPeer, ok := peers[p2pNode.ID()]
228+
if !ok {
229+
return nil, false, errors.New("unknown target peer in round 1 p2p", z.Any("peer", p2p.PeerName(p2pNode.ID())))
230+
}
231+
212232
for _, share := range msg.GetShares() {
213-
if int(share.GetKey().GetSourceId()) != peers[pID].ShareIdx {
233+
if int(share.GetKey().GetSourceId()) != sourcePeer.ShareIdx {
214234
return nil, false, errors.New("invalid round 1 p2p source ID")
215-
} else if int(share.GetKey().GetTargetId()) != peers[p2pNode.ID()].ShareIdx {
235+
} else if int(share.GetKey().GetTargetId()) != targetPeer.ShareIdx {
216236
return nil, false, errors.New("invalid round 1 p2p target ID")
217237
} else if int(share.GetKey().GetValIdx()) < 0 || int(share.GetKey().GetValIdx()) >= numVals {
218238
return nil, false, errors.New("invalid round 1 p2p validator index")
219239
}
220240
}
221241

222242
if dedupRound1P2P[pID] {
223-
log.Debug(ctx, "Ignoring duplicate round 2 message", z.Any("peer", p2p.PeerName(pID)))
243+
log.Debug(ctx, "Ignoring duplicate round 1 message", z.Any("peer", p2p.PeerName(pID)))
224244
return nil, false, nil
225245
}
226246

@@ -452,7 +472,7 @@ func round1CastFromProto(cast *pb.FrostRound1Cast) (msgKey, frost.Round1Bcast, e
452472

453473
ci, err := curve.Scalar.SetBytes(cast.GetCi())
454474
if err != nil {
455-
return msgKey{}, frost.Round1Bcast{}, errors.Wrap(err, "decode c1 scalar")
475+
return msgKey{}, frost.Round1Bcast{}, errors.Wrap(err, "decode ci scalar")
456476
}
457477

458478
var comms []curves.Point
@@ -498,7 +518,7 @@ func round2CastFromProto(cast *pb.FrostRound2Cast) (msgKey, frost.Round2Bcast, e
498518

499519
vkShare, err := curve.Point.FromAffineCompressed(cast.GetVkShare())
500520
if err != nil {
501-
return msgKey{}, frost.Round2Bcast{}, errors.Wrap(err, "decode c1 scalar")
521+
return msgKey{}, frost.Round2Bcast{}, errors.Wrap(err, "decode verification key share")
502522
}
503523

504524
key, err := keyFromProto(cast.GetKey())

0 commit comments

Comments
 (0)