From a0f05eac214752597707002044416bc1ef31b9a3 Mon Sep 17 00:00:00 2001 From: John Ingve Olsen Date: Fri, 19 Aug 2022 15:47:43 +0200 Subject: [PATCH] convert: try to cast hash byte slice --- internal/proto/hotstuffpb/convert.go | 28 +++++++++++++------ internal/proto/hotstuffpb/convert_test.go | 34 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/internal/proto/hotstuffpb/convert.go b/internal/proto/hotstuffpb/convert.go index 5be7b43b..b2f1ed19 100644 --- a/internal/proto/hotstuffpb/convert.go +++ b/internal/proto/hotstuffpb/convert.go @@ -70,9 +70,10 @@ func PartialCertToProto(cert hotstuff.PartialCert) *PartialCert { // PartialCertFromProto converts a hotstuffpb.PartialCert to an ecdsa.PartialCert. func PartialCertFromProto(cert *PartialCert) hotstuff.PartialCert { - var h hotstuff.Hash - copy(h[:], cert.GetHash()) - return hotstuff.NewPartialCert(QuorumSignatureFromProto(cert.GetSig()), h) + return hotstuff.NewPartialCert( + QuorumSignatureFromProto(cert.GetSig()), + convertHash(cert.GetHash()), + ) } // QuorumCertToProto converts a consensus.QuorumCert to a hotstuffpb.QuorumCert. @@ -87,9 +88,11 @@ func QuorumCertToProto(qc hotstuff.QuorumCert) *QuorumCert { // QuorumCertFromProto converts a hotstuffpb.QuorumCert to an ecdsa.QuorumCert. func QuorumCertFromProto(qc *QuorumCert) hotstuff.QuorumCert { - var h hotstuff.Hash - copy(h[:], qc.GetHash()) - return hotstuff.NewQuorumCert(QuorumSignatureFromProto(qc.GetSig()), hotstuff.View(qc.GetView()), h) + return hotstuff.NewQuorumCert( + QuorumSignatureFromProto(qc.GetSig()), + hotstuff.View(qc.GetView()), + convertHash(qc.GetHash()), + ) } // ProposalToProto converts a ProposeMsg to a protobuf message. @@ -127,10 +130,8 @@ func BlockToProto(block *hotstuff.Block) *Block { // BlockFromProto converts a hotstuffpb.Block to a consensus.Block. func BlockFromProto(block *Block) *hotstuff.Block { - var p hotstuff.Hash - copy(p[:], block.GetParent()) return hotstuff.NewBlock( - p, + convertHash(block.GetParent()), QuorumCertFromProto(block.GetQC()), unsafeBytesToString(block.GetCommand()), hotstuff.View(block.GetView()), @@ -225,6 +226,15 @@ func SyncInfoToProto(syncInfo hotstuff.SyncInfo) *SyncInfo { return m } +func convertHash(b []byte) (h hotstuff.Hash) { + if len(b) < len(h) { + copy(h[:], b) + } else { + h = *(*hotstuff.Hash)(b) + } + return h +} + func unsafeStringToBytes(s string) []byte { if s == "" { return []byte{} diff --git a/internal/proto/hotstuffpb/convert_test.go b/internal/proto/hotstuffpb/convert_test.go index 8540d6a3..68121cf4 100644 --- a/internal/proto/hotstuffpb/convert_test.go +++ b/internal/proto/hotstuffpb/convert_test.go @@ -2,6 +2,8 @@ package hotstuffpb import ( "bytes" + "crypto/rand" + "io" "testing" "github.com/relab/hotstuff" @@ -95,3 +97,35 @@ func TestConvertTimeoutCertBLS12(t *testing.T) { t.Fatal("Failed to verify timeout cert") } } + +func BenchmarkConvertHash(b *testing.B) { + s := make([]byte, 32) + _, err := io.ReadFull(rand.Reader, s) + if err != nil { + b.Fatal(err) + } + + var h hotstuff.Hash + + for i := 0; i < b.N; i++ { + h = convertHash(s) + } + + _ = h +} + +func BenchmarkCopyHash(b *testing.B) { + s := make([]byte, 32) + _, err := io.ReadFull(rand.Reader, s) + if err != nil { + b.Fatal(err) + } + + var h hotstuff.Hash + + for i := 0; i < b.N; i++ { + copy(h[:], s) + } + + _ = h +}