Skip to content

Commit c082905

Browse files
author
Manoranjith
committed
💥 Change proposal ID generation mechanism
- Generate proposal ID by drawing a series of bits from a good random source. - Because, it is sufficient for the proposal ID to be unique for the proposer. - This removes the need for implementing an encoding scheme for proposal ID generation. Signed-off-by: Manoranjith <ponraj.manoranjitha@in.bosch.com>
1 parent f3f8a24 commit c082905

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

client/proposalmsgs.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package client
1616

1717
import (
18+
"crypto/rand"
1819
"hash"
1920
"io"
2021

@@ -104,6 +105,7 @@ type (
104105
// BaseChannelProposal implements the channel proposal messages from the
105106
// Multi-Party Channel Proposal Protocol (MPCPP).
106107
BaseChannelProposal struct {
108+
proposalID ProposalID // Unique ID for the proposal.
107109
ChallengeDuration uint64 // Dispute challenge duration.
108110
NonceShare NonceShare // Proposer's channel nonce share.
109111
App channel.App // App definition, or nil.
@@ -165,8 +167,14 @@ func makeBaseChannelProposal(
165167
}
166168
}
167169

170+
var proposalID ProposalID
171+
if _, err := io.ReadFull(rand.Reader, proposalID[:]); err != nil {
172+
return BaseChannelProposal{}, errors.Wrap(err, "generating proposal ID")
173+
}
174+
168175
return BaseChannelProposal{
169176
ChallengeDuration: challengeDuration,
177+
ProposalID: proposalID,
170178
NonceShare: opt.nonce(),
171179
App: opt.App(),
172180
InitData: opt.AppData(),
@@ -175,6 +183,11 @@ func makeBaseChannelProposal(
175183
}, nil
176184
}
177185

186+
// ProposalID returns the proposal id for this channel proposal.
187+
func (p *BaseChannelProposal) ProposalID() ProposalID {
188+
return p.proposalID
189+
}
190+
178191
// Base returns the channel proposal's common values.
179192
func (p *BaseChannelProposal) Base() *BaseChannelProposal {
180193
return p
@@ -188,7 +201,7 @@ func (p BaseChannelProposal) NumPeers() int {
188201
// Encode encodes the BaseChannelProposal into an io.Writer.
189202
func (p BaseChannelProposal) Encode(w io.Writer) error {
190203
optAppAndDataEnc := channel.OptAppAndDataEnc{App: p.App, Data: p.InitData}
191-
return perunio.Encode(w, p.ChallengeDuration, p.NonceShare,
204+
return perunio.Encode(w, p.ProposalID(), p.ChallengeDuration, p.NonceShare,
192205
optAppAndDataEnc, p.InitBals, p.FundingAgreement)
193206
}
194207

@@ -198,7 +211,7 @@ func (p *BaseChannelProposal) Decode(r io.Reader) (err error) {
198211
p.InitBals = new(channel.Allocation)
199212
}
200213
optAppAndDataDec := channel.OptAppAndDataDec{App: &p.App, Data: &p.InitData}
201-
return perunio.Decode(r, &p.ChallengeDuration, &p.NonceShare,
214+
return perunio.Decode(r, &p.proposalID, &p.ChallengeDuration, &p.NonceShare,
202215
optAppAndDataDec, p.InitBals, &p.FundingAgreement)
203216
}
204217

@@ -275,16 +288,6 @@ func (LedgerChannelProposalMsg) Type() wire.Type {
275288
return wire.LedgerChannelProposal
276289
}
277290

278-
// ProposalID returns the identifier of this channel proposal request.
279-
func (p LedgerChannelProposalMsg) ProposalID() (propID ProposalID) {
280-
hasher := newHasher()
281-
if err := perunio.Encode(hasher, p); err != nil {
282-
log.Panicf("proposal ID nonce encoding: %v", err)
283-
}
284-
copy(propID[:], hasher.Sum(nil))
285-
return
286-
}
287-
288291
// Encode encodes a ledger channel proposal.
289292
func (p LedgerChannelProposalMsg) Encode(w io.Writer) error {
290293
if err := p.assertValidNumParts(); err != nil {
@@ -347,16 +350,6 @@ func NewSubChannelProposal(
347350
return
348351
}
349352

350-
// ProposalID returns the identifier of this channel proposal request.
351-
func (p SubChannelProposalMsg) ProposalID() (propID ProposalID) {
352-
hasher := newHasher()
353-
if err := perunio.Encode(hasher, p); err != nil {
354-
log.Panicf("proposal ID nonce encoding: %v", err)
355-
}
356-
copy(propID[:], hasher.Sum(nil))
357-
return
358-
}
359-
360353
// Encode encodes the SubChannelProposal into an io.Writer.
361354
func (p SubChannelProposalMsg) Encode(w io.Writer) error {
362355
return perunio.Encode(w, p.BaseChannelProposal, p.Parent)
@@ -602,16 +595,6 @@ func (VirtualChannelProposalMsg) Type() wire.Type {
602595
return wire.VirtualChannelProposal
603596
}
604597

605-
// ProposalID returns the identifier of this channel proposal.
606-
func (p VirtualChannelProposalMsg) ProposalID() (propID ProposalID) {
607-
hasher := newHasher()
608-
if err := perunio.Encode(hasher, p); err != nil {
609-
log.Panicf("proposal ID base encoding: %v", err)
610-
}
611-
copy(propID[:], hasher.Sum(nil))
612-
return
613-
}
614-
615598
// Accept constructs an accept message that belongs to a proposal message.
616599
func (p VirtualChannelProposalMsg) Accept(
617600
responder wallet.Address,

0 commit comments

Comments
 (0)