Skip to content

Commit

Permalink
Update types and change to new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
johningve committed May 25, 2022
1 parent cda20be commit 52d8a04
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 214 deletions.
10 changes: 5 additions & 5 deletions consensus/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type VoteMsg struct {

// TimeoutMsg is broadcast whenever a replica has a local timeout.
type TimeoutMsg struct {
ID hotstuff.ID // The ID of the replica who sent the message.
View View // The view that the replica wants to enter.
ViewSignature Signature // A signature of the view
MsgSignature Signature // A signature of the view, QC.BlockHash, and the replica ID
SyncInfo SyncInfo // The highest QC/TC known to the sender.
ID hotstuff.ID // The ID of the replica who sent the message.
View View // The view that the replica wants to enter.
ViewSignature QuorumSignature // A signature of the view
MsgSignature QuorumSignature // A signature of the view, QC.BlockHash, and the replica ID
SyncInfo SyncInfo // The highest QC/TC known to the sender.
}

// Hash returns a hash of the timeout message.
Expand Down
20 changes: 16 additions & 4 deletions consensus/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,29 @@ type ThresholdSignature = QuorumSignature

// PartialCert is a signed block hash.
type PartialCert struct {
signature Signature
// shortcut to the signer of the signature
signer hotstuff.ID
signature QuorumSignature
blockHash Hash
}

// NewPartialCert returns a new partial certificate.
func NewPartialCert(signature Signature, blockHash Hash) PartialCert {
return PartialCert{signature, blockHash}
func NewPartialCert(signature QuorumSignature, blockHash Hash) PartialCert {
var signer hotstuff.ID
signature.Participants().RangeWhile(func(i hotstuff.ID) bool {
signer = i
return false
})
return PartialCert{signer, signature, blockHash}
}

// Signer returns the ID of the replica that created the certificate.
func (pc PartialCert) Signer() hotstuff.ID {
return pc.signer
}

// Signature returns the signature.
func (pc PartialCert) Signature() Signature {
func (pc PartialCert) Signature() QuorumSignature {
return pc.signature
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCreatePartialCert(t *testing.T) {
t.Error("Partial certificate hash does not match block hash!")
}

if signerID := partialCert.Signature().Signer(); signerID != hotstuff.ID(1) {
if signerID := partialCert.Signer(); signerID != hotstuff.ID(1) {
t.Errorf("Wrong ID for signer in partial certificate: got: %d, want: %d", signerID, hotstuff.ID(1))
}
}
Expand Down Expand Up @@ -191,13 +191,13 @@ func setup(newFunc func() consensus.Crypto, keyFunc keyFunc) setupFunc {
}
}

func NewCache(impl func() consensus.CryptoImpl) func() consensus.Crypto {
func NewCache(impl func() consensus.CryptoBase) func() consensus.Crypto {
return func() consensus.Crypto {
return crypto.NewCache(impl(), 10)
}
}

func NewBase(impl func() consensus.CryptoImpl) func() consensus.Crypto {
func NewBase(impl func() consensus.CryptoBase) func() consensus.Crypto {
return func() consensus.Crypto {
return crypto.New(impl())
}
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,6 @@ golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxb
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w=
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/orchestration/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (w *Worker) createReplica(opts *orchestrationpb.ReplicaOpts) (*replica.Repl
return nil, fmt.Errorf("invalid byzantine strategy: '%s'", opts.GetByzantineStrategy())
}

var cryptoImpl consensus.CryptoImpl
var cryptoImpl consensus.CryptoBase
if !modules.GetModule(opts.GetCrypto(), &cryptoImpl) {
return nil, fmt.Errorf("invalid crypto name: '%s'", opts.GetCrypto())
}
Expand Down
38 changes: 19 additions & 19 deletions internal/proto/hotstuffpb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func SignatureFromProto(sig *Signature) consensus.Signature {
return nil
}

// ThresholdSignatureToProto converts a threshold signature to a protocol buffers message.
func ThresholdSignatureToProto(sig consensus.QuorumSignature) *ThresholdSignature {
signature := &ThresholdSignature{}
// QuorumSignatureToProto converts a threshold signature to a protocol buffers message.
func QuorumSignatureToProto(sig consensus.QuorumSignature) *QuorumSignature {
signature := &QuorumSignature{}
switch s := sig.(type) {
case ecdsa.MultiSignature:
sigs := make([]*ECDSASignature, 0, len(s))
Expand All @@ -61,20 +61,20 @@ func ThresholdSignatureToProto(sig consensus.QuorumSignature) *ThresholdSignatur
S: p.S().Bytes(),
})
}
signature.AggSig = &ThresholdSignature_ECDSASigs{ECDSASigs: &ECDSAThresholdSignature{
signature.Sig = &QuorumSignature_ECDSASigs{ECDSASigs: &ECDSAMultiSignature{
Sigs: sigs,
}}
case *bls12.AggregateSignature:
signature.AggSig = &ThresholdSignature_BLS12Sig{BLS12Sig: &BLS12AggregateSignature{
signature.Sig = &QuorumSignature_BLS12Sig{BLS12Sig: &BLS12AggregateSignature{
Sig: s.ToBytes(),
Participants: s.Bitfield().Bytes(),
}}
}
return signature
}

// ThresholdSignatureFromProto converts a protocol buffers message to a threshold signature.
func ThresholdSignatureFromProto(sig *ThresholdSignature) consensus.QuorumSignature {
// QuorumSignatureFromProto converts a protocol buffers message to a threshold signature.
func QuorumSignatureFromProto(sig *QuorumSignature) consensus.QuorumSignature {
if signature := sig.GetECDSASigs(); signature != nil {
sigs := make([]*ecdsa.Signature, len(signature.GetSigs()))
for i, sig := range signature.GetSigs() {
Expand All @@ -100,7 +100,7 @@ func ThresholdSignatureFromProto(sig *ThresholdSignature) consensus.QuorumSignat
func PartialCertToProto(cert consensus.PartialCert) *PartialCert {
hash := cert.BlockHash()
return &PartialCert{
Sig: SignatureToProto(cert.Signature()),
Sig: QuorumSignatureToProto(cert.Signature()),
Hash: hash[:],
}
}
Expand All @@ -109,14 +109,14 @@ func PartialCertToProto(cert consensus.PartialCert) *PartialCert {
func PartialCertFromProto(cert *PartialCert) consensus.PartialCert {
var h consensus.Hash
copy(h[:], cert.GetHash())
return consensus.NewPartialCert(SignatureFromProto(cert.GetSig()), h)
return consensus.NewPartialCert(QuorumSignatureFromProto(cert.GetSig()), h)
}

// QuorumCertToProto converts a consensus.QuorumCert to a hotstuffpb.QuorumCert.
func QuorumCertToProto(qc consensus.QuorumCert) *QuorumCert {
hash := qc.BlockHash()
return &QuorumCert{
Sig: ThresholdSignatureToProto(qc.Signature()),
Sig: QuorumSignatureToProto(qc.Signature()),
Hash: hash[:],
View: uint64(qc.View()),
}
Expand All @@ -126,7 +126,7 @@ func QuorumCertToProto(qc consensus.QuorumCert) *QuorumCert {
func QuorumCertFromProto(qc *QuorumCert) consensus.QuorumCert {
var h consensus.Hash
copy(h[:], qc.GetHash())
return consensus.NewQuorumCert(ThresholdSignatureFromProto(qc.GetSig()), consensus.View(qc.GetView()), h)
return consensus.NewQuorumCert(QuorumSignatureFromProto(qc.GetSig()), consensus.View(qc.GetView()), h)
}

// ProposalToProto converts a ProposeMsg to a protobuf message.
Expand Down Expand Up @@ -180,10 +180,10 @@ func TimeoutMsgFromProto(m *TimeoutMsg) consensus.TimeoutMsg {
timeoutMsg := consensus.TimeoutMsg{
View: consensus.View(m.GetView()),
SyncInfo: SyncInfoFromProto(m.GetSyncInfo()),
ViewSignature: SignatureFromProto(m.GetViewSig()),
ViewSignature: QuorumSignatureFromProto(m.GetViewSig()),
}
if m.GetViewSig() != nil {
timeoutMsg.MsgSignature = SignatureFromProto(m.GetMsgSig())
timeoutMsg.MsgSignature = QuorumSignatureFromProto(m.GetMsgSig())
}
return timeoutMsg
}
Expand All @@ -193,24 +193,24 @@ func TimeoutMsgToProto(timeoutMsg consensus.TimeoutMsg) *TimeoutMsg {
tm := &TimeoutMsg{
View: uint64(timeoutMsg.View),
SyncInfo: SyncInfoToProto(timeoutMsg.SyncInfo),
ViewSig: SignatureToProto(timeoutMsg.ViewSignature),
ViewSig: QuorumSignatureToProto(timeoutMsg.ViewSignature),
}
if timeoutMsg.MsgSignature != nil {
tm.MsgSig = SignatureToProto(timeoutMsg.MsgSignature)
tm.MsgSig = QuorumSignatureToProto(timeoutMsg.MsgSignature)
}
return tm
}

// TimeoutCertFromProto converts a timeout certificate from the protobuf type to the hotstuff type.
func TimeoutCertFromProto(m *TimeoutCert) consensus.TimeoutCert {
return consensus.NewTimeoutCert(ThresholdSignatureFromProto(m.GetSig()), consensus.View(m.GetView()))
return consensus.NewTimeoutCert(QuorumSignatureFromProto(m.GetSig()), consensus.View(m.GetView()))
}

// TimeoutCertToProto converts a timeout certificate from the hotstuff type to the protobuf type.
func TimeoutCertToProto(timeoutCert consensus.TimeoutCert) *TimeoutCert {
return &TimeoutCert{
View: uint64(timeoutCert.View()),
Sig: ThresholdSignatureToProto(timeoutCert.Signature()),
Sig: QuorumSignatureToProto(timeoutCert.Signature()),
}
}

Expand All @@ -220,7 +220,7 @@ func AggregateQCFromProto(m *AggQC) consensus.AggregateQC {
for id, pQC := range m.GetQCs() {
qcs[hotstuff.ID(id)] = QuorumCertFromProto(pQC)
}
return consensus.NewAggregateQC(qcs, ThresholdSignatureFromProto(m.GetSig()), consensus.View(m.GetView()))
return consensus.NewAggregateQC(qcs, QuorumSignatureFromProto(m.GetSig()), consensus.View(m.GetView()))
}

// AggregateQCToProto converts an AggregateQC from the hotstuff type to the protobuf type.
Expand All @@ -229,7 +229,7 @@ func AggregateQCToProto(aggQC consensus.AggregateQC) *AggQC {
for id, qc := range aggQC.QCs() {
pQCs[uint32(id)] = QuorumCertToProto(qc)
}
return &AggQC{QCs: pQCs, Sig: ThresholdSignatureToProto(aggQC.Sig()), View: uint64(aggQC.View())}
return &AggQC{QCs: pQCs, Sig: QuorumSignatureToProto(aggQC.Sig()), View: uint64(aggQC.View())}
}

// SyncInfoFromProto converts a SyncInfo struct from the protobuf type to the hotstuff type.
Expand Down
Loading

0 comments on commit 52d8a04

Please sign in to comment.