Skip to content

Commit

Permalink
convert: try to cast hash byte slice
Browse files Browse the repository at this point in the history
  • Loading branch information
johningve committed Aug 22, 2022
1 parent 6559ef4 commit a0f05ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
28 changes: 19 additions & 9 deletions internal/proto/hotstuffpb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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{}
Expand Down
34 changes: 34 additions & 0 deletions internal/proto/hotstuffpb/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package hotstuffpb

import (
"bytes"
"crypto/rand"
"io"
"testing"

"github.com/relab/hotstuff"
Expand Down Expand Up @@ -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
}

0 comments on commit a0f05ea

Please sign in to comment.