From 272b47eae911b807fd11261e6ae01c9b1951ea8f Mon Sep 17 00:00:00 2001 From: Al Cutter Date: Tue, 23 Jul 2024 16:44:28 +0100 Subject: [PATCH] Simplify indexFunc away --- ct_only.go | 4 +--- entry.go | 17 +++-------------- entry_test.go | 4 ++-- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/ct_only.go b/ct_only.go index c2d26e80c..82ee426c2 100644 --- a/ct_only.go +++ b/ct_only.go @@ -48,11 +48,9 @@ func NewCertificateTransparencySequencedWriter(s Storage) func(context.Context, func convertCTEntry(e *ctonly.Entry) *Entry { r := &Entry{} r.internal.Identity = e.Identity() - r.indexFunc = func(idx uint64) { + r.marshalBundle = func(idx uint64) []byte { r.internal.LeafHash = e.MerkleLeafHash(idx) r.internal.Data = e.LeafData(idx) - } - r.marshalBundle = func() []byte { return r.internal.Data } diff --git a/entry.go b/entry.go index be8d6b79e..17be29496 100644 --- a/entry.go +++ b/entry.go @@ -35,16 +35,8 @@ type Entry struct { Index *uint64 } - // indexFunc will be called when a (potential) index is assigned to the entry. - // This is only intended to be used for schemes where the leaf image being logged is - // somehow dependendent on its position in the log. - // - // Implementation of this function should not modify internal.Index, this will be - // done elsewhere. - indexFunc func(index uint64) - // marshalBundle knows how to convert this entry's Data into a marshalled bundle entry. - marshalBundle func() []byte + marshalBundle func(index uint64) []byte } // Data returns the raw entry bytes which will form the entry in the log. @@ -67,10 +59,7 @@ func (e Entry) Index() *uint64 { return e.internal.Index } // be considered final until the storage Add method has returned successfully with the durably assigned index. func (e *Entry) MarshalBundleData(index uint64) []byte { e.internal.Index = &index - if e.indexFunc != nil { - e.indexFunc(index) - } - return e.marshalBundle() + return e.marshalBundle(index) } // NewEntry creates a new Entry object with leaf data. @@ -90,7 +79,7 @@ func NewEntry(data []byte, opts ...EntryOpt) *Entry { if e.marshalBundle == nil { // By default we will marshal ourselves into a bundle using the mechanism described // by https://c2sp.org/tlog-tiles: - e.marshalBundle = func() []byte { + e.marshalBundle = func(_ uint64) []byte { r := make([]byte, 0, 2+len(e.internal.Data)) r = binary.BigEndian.AppendUint16(r, uint16(len(e.internal.Data))) r = append(r, e.internal.Data...) diff --git a/entry_test.go b/entry_test.go index 4652cd845..2326befe1 100644 --- a/entry_test.go +++ b/entry_test.go @@ -25,8 +25,8 @@ func TestEntryMarshalBundleDelegates(t *testing.T) { wantBundle := []byte(fmt.Sprintf("Yes %d", wantIdx)) e := NewEntry([]byte("this is data")) - e.marshalBundle = func() []byte { - if gotIdx := *e.internal.Index; gotIdx != wantIdx { + e.marshalBundle = func(gotIdx uint64) []byte { + if gotIdx != wantIdx { t.Fatalf("Got idx %d, want %d", gotIdx, wantIdx) } return wantBundle