Skip to content

Commit

Permalink
tests: fixing a broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed May 5, 2023
1 parent a1068b0 commit 8829a5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
10 changes: 6 additions & 4 deletions types/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ func TestBlockHash(t *testing.T) {
assert.Equal(t, d, d2)

headerSize := b.Header().SerializeSize()
certSize := b.PrevCertificate().SerializeSize()
headerData := d[:headerSize]
certData := d[headerSize : headerSize+certSize]
// uncomment this comment later
// certSize := b.PrevCertificate().SerializeSize()
// certData := d[headerSize : headerSize+certSize]
certData := b.PrevCertificate().hashBytes()
certHash := hash.CalcHash(certData)

txHashes := make([]hash.Hash, 0)
Expand All @@ -130,10 +132,10 @@ func TestBlockHash(t *testing.T) {
hashData = append(hashData, util.Int32ToSlice(int32(b.Transactions().Len()))...)

expected1 := hash.CalcHash(hashData)
expected2, _ := hash.FromString("c856bd7d0ce99842ec450ffad6416c45320d667b8b7c4927727860a43290595c")
expected2, _ := hash.FromString("665e48e4dcbe3d4ad4871ed01606ced559501ad969093f9c63f8f0c41b25819b")
assert.Equal(t, b.Hash(), expected1)
assert.Equal(t, b.Hash(), expected2)
assert.Equal(t, b.Stamp(), hash.Stamp{0xc8, 0x56, 0xbd, 0x7d})
assert.Equal(t, b.Stamp(), hash.Stamp{0x66, 0x5e, 0x48, 0xe4})
}

func TestMakeBlock(t *testing.T) {
Expand Down
27 changes: 17 additions & 10 deletions types/block/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,35 @@ func (cert *Certificate) SanityCheck() error {
return nil
}

func (cert *Certificate) Hash() hash.Hash {
// Remove this function later
// read below comment
func (cert *Certificate) hashBytes() []byte {
w := bytes.NewBuffer(make([]byte, 0, cert.SerializeSize()))
if err := encoding.WriteVarInt(w, uint64(cert.Round())); err != nil {
return hash.UndefHash
return nil

Check warning on line 69 in types/block/certificate.go

View check run for this annotation

Codecov / codecov/patch

types/block/certificate.go#L69

Added line #L69 was not covered by tests
}
if err := encoding.WriteVarInt(w, uint64(len(cert.data.Absentees))); err != nil {
return hash.UndefHash
return nil

Check warning on line 72 in types/block/certificate.go

View check run for this annotation

Codecov / codecov/patch

types/block/certificate.go#L72

Added line #L72 was not covered by tests
}
for _, n := range cert.data.Absentees {
if err := encoding.WriteVarInt(w, uint64(n)); err != nil {
return hash.UndefHash
return nil

Check warning on line 76 in types/block/certificate.go

View check run for this annotation

Codecov / codecov/patch

types/block/certificate.go#L76

Added line #L76 was not covered by tests
}
}
if err := cert.data.Signature.Encode(w); err != nil {
return hash.UndefHash
return nil

Check warning on line 80 in types/block/certificate.go

View check run for this annotation

Codecov / codecov/patch

types/block/certificate.go#L80

Added line #L80 was not covered by tests
}
return w.Bytes()
}

func (cert *Certificate) Hash() hash.Hash {
// TODO: Add a comment on certificate hash
// Technically, we don't need to include the committers list inside the certificate.
// At each height, the committers are the same as the committee members.
// As a possible enhancement in the future, we can remove the committers from the certificate.
// In this case, increasing the committee size won't increase the size of the certificate.

// TODO: a comment on certificate hash
// Technically we don't need to include the committers list inside the certificate.
// In each height the committers are same as the committee members.
// We can remove the committers from the certificate as a possible enhancement in future,.
return hash.CalcHash(w.Bytes())
return hash.CalcHash(cert.hashBytes())
}

// SerializeSize returns the number of bytes it would take to serialize the block.
Expand Down

0 comments on commit 8829a5a

Please sign in to comment.