|
|
@ -96,7 +96,7 @@ func (consensus *Consensus) startConsensus(newBlock *blockchain.Block) { |
|
|
|
func (consensus *Consensus) commitByLeader(firstRound bool) { |
|
|
|
func (consensus *Consensus) commitByLeader(firstRound bool) { |
|
|
|
// Generate leader's own commitment
|
|
|
|
// Generate leader's own commitment
|
|
|
|
secret, commitment := crypto.Commit(crypto.Ed25519Curve) |
|
|
|
secret, commitment := crypto.Commit(crypto.Ed25519Curve) |
|
|
|
consensus.secret = secret |
|
|
|
consensus.secret[consensus.consensusId] = secret |
|
|
|
if firstRound { |
|
|
|
if firstRound { |
|
|
|
(*consensus.commitments)[consensus.nodeId] = commitment |
|
|
|
(*consensus.commitments)[consensus.nodeId] = commitment |
|
|
|
consensus.bitmap.SetKey(consensus.pubKey, true) |
|
|
|
consensus.bitmap.SetKey(consensus.pubKey, true) |
|
|
@ -164,6 +164,9 @@ func (consensus *Consensus) processCommitMessage(payload []byte, targetState Con |
|
|
|
// proceed only when the message is not received before
|
|
|
|
// proceed only when the message is not received before
|
|
|
|
_, ok = (*commitments)[validatorId] |
|
|
|
_, ok = (*commitments)[validatorId] |
|
|
|
shouldProcess := !ok |
|
|
|
shouldProcess := !ok |
|
|
|
|
|
|
|
if len((*commitments)) >= ((len(consensus.publicKeys)*2)/3 + 1) { |
|
|
|
|
|
|
|
shouldProcess = false |
|
|
|
|
|
|
|
} |
|
|
|
if shouldProcess { |
|
|
|
if shouldProcess { |
|
|
|
point := crypto.Ed25519Curve.Point() |
|
|
|
point := crypto.Ed25519Curve.Point() |
|
|
|
point.UnmarshalBinary(commitment) |
|
|
|
point.UnmarshalBinary(commitment) |
|
|
@ -177,7 +180,7 @@ func (consensus *Consensus) processCommitMessage(payload []byte, targetState Con |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len((*commitments)) >= len(consensus.publicKeys) && consensus.state < targetState { |
|
|
|
if len((*commitments)) >= ((len(consensus.publicKeys)*2)/3+1) && consensus.state < targetState { |
|
|
|
consensus.Log.Debug("Enough commitments received with signatures", "num", len(*commitments), "state", consensus.state) |
|
|
|
consensus.Log.Debug("Enough commitments received with signatures", "num", len(*commitments), "state", consensus.state) |
|
|
|
|
|
|
|
|
|
|
|
// Broadcast challenge
|
|
|
|
// Broadcast challenge
|
|
|
@ -213,7 +216,7 @@ func (consensus *Consensus) processCommitMessage(payload []byte, targetState Con |
|
|
|
// Leader commit to the message itself before receiving others commits
|
|
|
|
// Leader commit to the message itself before receiving others commits
|
|
|
|
func (consensus *Consensus) responseByLeader(challenge kyber.Scalar, firstRound bool) { |
|
|
|
func (consensus *Consensus) responseByLeader(challenge kyber.Scalar, firstRound bool) { |
|
|
|
// Generate leader's own commitment
|
|
|
|
// Generate leader's own commitment
|
|
|
|
response, err := crypto.Response(crypto.Ed25519Curve, consensus.priKey, consensus.secret, challenge) |
|
|
|
response, err := crypto.Response(crypto.Ed25519Curve, consensus.priKey, consensus.secret[consensus.consensusId], challenge) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
if firstRound { |
|
|
|
if firstRound { |
|
|
|
(*consensus.responses)[consensus.nodeId] = response |
|
|
|
(*consensus.responses)[consensus.nodeId] = response |
|
|
@ -229,6 +232,7 @@ func (consensus *Consensus) responseByLeader(challenge kyber.Scalar, firstRound |
|
|
|
|
|
|
|
|
|
|
|
// Processes the response message sent from validators
|
|
|
|
// Processes the response message sent from validators
|
|
|
|
func (consensus *Consensus) processResponseMessage(payload []byte, targetState ConsensusState) { |
|
|
|
func (consensus *Consensus) processResponseMessage(payload []byte, targetState ConsensusState) { |
|
|
|
|
|
|
|
consensus.Log.Warn("Received RESPONSE 1") |
|
|
|
//#### Read payload data
|
|
|
|
//#### Read payload data
|
|
|
|
offset := 0 |
|
|
|
offset := 0 |
|
|
|
// 4 byte consensus id
|
|
|
|
// 4 byte consensus id
|
|
|
@ -254,6 +258,8 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
|
|
|
|
|
|
|
|
shouldProcess := true |
|
|
|
shouldProcess := true |
|
|
|
consensus.mutex.Lock() |
|
|
|
consensus.mutex.Lock() |
|
|
|
|
|
|
|
defer consensus.mutex.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
// check consensus Id
|
|
|
|
// check consensus Id
|
|
|
|
if consensusId != consensus.consensusId { |
|
|
|
if consensusId != consensus.consensusId { |
|
|
|
shouldProcess = false |
|
|
|
shouldProcess = false |
|
|
@ -276,6 +282,7 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
consensus.Log.Warn("Received RESPONSE 2") |
|
|
|
commitments := consensus.commitments // targetState == COLLECTIVE_SIG_DONE
|
|
|
|
commitments := consensus.commitments // targetState == COLLECTIVE_SIG_DONE
|
|
|
|
responses := consensus.responses |
|
|
|
responses := consensus.responses |
|
|
|
bitmap := consensus.bitmap |
|
|
|
bitmap := consensus.bitmap |
|
|
@ -288,6 +295,12 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
// proceed only when the message is not received before
|
|
|
|
// proceed only when the message is not received before
|
|
|
|
_, ok = (*responses)[validatorId] |
|
|
|
_, ok = (*responses)[validatorId] |
|
|
|
shouldProcess = shouldProcess && !ok |
|
|
|
shouldProcess = shouldProcess && !ok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len((*responses)) >= ((len(consensus.publicKeys)*2)/3 + 1) { |
|
|
|
|
|
|
|
consensus.Log.Warn("quiting 3") |
|
|
|
|
|
|
|
shouldProcess = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if shouldProcess { |
|
|
|
if shouldProcess { |
|
|
|
// verify the response matches the received commit
|
|
|
|
// verify the response matches the received commit
|
|
|
|
responseScalar := crypto.Ed25519Curve.Scalar() |
|
|
|
responseScalar := crypto.Ed25519Curve.Scalar() |
|
|
@ -300,19 +313,18 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
(*responses)[validatorId] = responseScalar |
|
|
|
(*responses)[validatorId] = responseScalar |
|
|
|
consensus.Log.Debug("Received new response message", "num", len(*responses)) |
|
|
|
consensus.Log.Debug("Received new response message", "num", len(*responses)) |
|
|
|
// Set the bitmap indicate this validate signed. TODO: figure out how to resolve the inconsistency of validators from commit and response messages
|
|
|
|
// Set the bitmap indicate this validate signed. TODO: figure out how to resolve the inconsistency of validators from commit and response messages
|
|
|
|
consensus.bitmap.SetKey(value.PubKey, true) |
|
|
|
bitmap.SetKey(value.PubKey, true) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
consensus.mutex.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !shouldProcess { |
|
|
|
if !shouldProcess { |
|
|
|
|
|
|
|
consensus.Log.Warn("returning 3") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(*responses) >= len(consensus.publicKeys) && consensus.state != targetState { |
|
|
|
consensus.Log.Warn("Received RESPONSE 3") |
|
|
|
consensus.mutex.Lock() |
|
|
|
if len(*responses) >= ((len(consensus.publicKeys)*2)/3+1) && consensus.state != targetState { |
|
|
|
if len(*responses) >= len(consensus.publicKeys) && consensus.state != targetState { |
|
|
|
if len(*responses) >= ((len(consensus.publicKeys)*2)/3+1) && consensus.state != targetState { |
|
|
|
consensus.Log.Debug("Enough responses received with signatures", "num", len(*responses), "state", consensus.state) |
|
|
|
consensus.Log.Debug("Enough responses received with signatures", "num", len(*responses), "state", consensus.state) |
|
|
|
// Aggregate responses
|
|
|
|
// Aggregate responses
|
|
|
|
responseScalars := []kyber.Scalar{} |
|
|
|
responseScalars := []kyber.Scalar{} |
|
|
@ -329,6 +341,7 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
if targetState == FINISHED { |
|
|
|
if targetState == FINISHED { |
|
|
|
aggregatedCommitment = consensus.aggregatedFinalCommitment |
|
|
|
aggregatedCommitment = consensus.aggregatedFinalCommitment |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
collectiveSigAndBitmap, err := crypto.Sign(crypto.Ed25519Curve, aggregatedCommitment, aggregatedResponse, bitmap) |
|
|
|
collectiveSigAndBitmap, err := crypto.Sign(crypto.Ed25519Curve, aggregatedCommitment, aggregatedResponse, bitmap) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -377,7 +390,6 @@ func (consensus *Consensus) processResponseMessage(payload []byte, targetState C |
|
|
|
consensus.ReadySignal <- struct{}{} |
|
|
|
consensus.ReadySignal <- struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
consensus.mutex.Unlock() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|