Skip to content

Commit

Permalink
fix: data race issue on updating certificate (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Oct 13, 2023
1 parent a838ea1 commit 65d0f18
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions types/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,21 @@ func (cert *Certificate) Hash() hash.Hash {
}

func (cert *Certificate) Clone() *Certificate {
return &Certificate{
cloned := &Certificate{
data: certificateData{
Height: cert.Height(),
Round: cert.Round(),
Committers: cert.Committers(),
Absentees: cert.Absentees(),
Signature: cert.Signature(),
Committers: make([]int32, len(cert.data.Committers)),
Absentees: make([]int32, len(cert.data.Absentees)),
Signature: new(bls.Signature),
},
}

copy(cloned.data.Committers, cert.data.Committers)
copy(cloned.data.Absentees, cert.data.Absentees)
*cloned.data.Signature = *cert.data.Signature

return cloned
}

// SerializeSize returns the number of bytes it would take to serialize the block.
Expand Down
2 changes: 1 addition & 1 deletion types/certificate/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,6 @@ func TestClone(t *testing.T) {
cert1 := ts.GenerateTestCertificate(ts.RandHeight())
cert2 := cert1.Clone()

cert1.AddSignature(cert1.Absentees()[0], ts.RandBLSSignature())
cert2.AddSignature(cert2.Absentees()[0], ts.RandBLSSignature())
assert.NotEqual(t, cert1, cert2)
}

0 comments on commit 65d0f18

Please sign in to comment.