Skip to content

Commit

Permalink
Simplify indexFunc away
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Jul 23, 2024
1 parent 0e915aa commit 272b47e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
4 changes: 1 addition & 3 deletions ct_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 3 additions & 14 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 272b47e

Please sign in to comment.