Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
algoidan committed May 8, 2022
1 parent 32a8164 commit 9c6a06a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 1 addition & 2 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ again:
// Build returns a compact certificate, if the builder has accumulated
// enough signatures to construct it.
func (b *Builder) Build() (*Cert, error) {

if b.signedWeight <= b.provenWeight {
if !b.Ready() {
return nil, fmt.Errorf("%w: %d <= %d", ErrSignedWeightLessThanProvenWeight, b.signedWeight, b.provenWeight)
}

Expand Down
21 changes: 16 additions & 5 deletions crypto/compactcert/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ func generateCertForTesting(a *require.Assertions) paramsForTest {
b, err := MkBuilder(data, compactCertRoundsForTests, uint64(totalWeight/2), parts, partcom, compactCertStrengthTargetForTests)
a.NoError(err)

for i := 0; i < npart; i++ {
a.False(b.Present(uint64(i)))
a.NoError(b.IsValid(uint64(i), sigs[i], !doLargeTest))
b.Add(uint64(i), sigs[i])
for i := uint64(0); i < uint64(npart); i++ {
a.False(b.Present(i))
a.NoError(b.IsValid(i, sigs[i], !doLargeTest))
b.Add(i, sigs[i])

// sanity check that the builder add the signature
isPresent, err := b.Present(i)
a.NoError(err)
a.True(isPresent)
}

cert, err := b.Build()
Expand Down Expand Up @@ -465,7 +470,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {
a.ErrorIs(builder.IsValid(0, sig, true), merklesignature.ErrSignatureSaltVersionMismatch)
}

func TestReady(t *testing.T) {
func TestBuildAndReady(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)

Expand All @@ -480,12 +485,18 @@ func TestReady(t *testing.T) {
a.NoError(err)

a.False(builder.Ready())
_, err = builder.Build()
a.ErrorIs(err, ErrSignedWeightLessThanProvenWeight)

builder.signedWeight = builder.provenWeight
a.False(builder.Ready())
_, err = builder.Build()
a.ErrorIs(err, ErrSignedWeightLessThanProvenWeight)

builder.signedWeight = builder.provenWeight + 1
a.True(builder.Ready())
_, err = builder.Build()
a.NotErrorIs(err, ErrSignedWeightLessThanProvenWeight)

}

Expand Down

0 comments on commit 9c6a06a

Please sign in to comment.