From 80e52e03d5bd7ead65de9b4b5ec2e0d21b7dc3d7 Mon Sep 17 00:00:00 2001 From: Al Cutter Date: Fri, 10 Jan 2025 18:38:12 +0000 Subject: [PATCH] Pass in bundle hasher --- cmd/experimental/migrate/internal/migrate.go | 16 +++++++++++++++ cmd/experimental/migrate/posix/main.go | 2 +- storage/posix/files.go | 21 +++++++++++--------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cmd/experimental/migrate/internal/migrate.go b/cmd/experimental/migrate/internal/migrate.go index 576d8226..fca11492 100644 --- a/cmd/experimental/migrate/internal/migrate.go +++ b/cmd/experimental/migrate/internal/migrate.go @@ -21,6 +21,8 @@ import ( "time" "github.com/avast/retry-go/v4" + "github.com/transparency-dev/merkle/rfc6962" + "github.com/transparency-dev/trillian-tessera/api" "github.com/transparency-dev/trillian-tessera/api/layout" "github.com/transparency-dev/trillian-tessera/client" "golang.org/x/sync/errgroup" @@ -179,3 +181,17 @@ func (m *copier) migrateWorker(ctx context.Context) error { } return nil } + +// BundleHasher parses a C2SP tlog-tile bundle and returns the leaf hashes of each entry it contains. +func BundleHasher(bundle []byte) ([][]byte, error) { + eb := &api.EntryBundle{} + if err := eb.UnmarshalText(bundle); err != nil { + return nil, fmt.Errorf("unmarshal: %v", err) + } + r := make([][]byte, 0, len(eb.Entries)) + for _, e := range eb.Entries { + h := rfc6962.DefaultHasher.HashLeaf(e) + r = append(r, h[:]) + } + return r, nil +} diff --git a/cmd/experimental/migrate/posix/main.go b/cmd/experimental/migrate/posix/main.go index a3c8243e..90044e25 100644 --- a/cmd/experimental/migrate/posix/main.go +++ b/cmd/experimental/migrate/posix/main.go @@ -66,7 +66,7 @@ func main() { } // Create our Tessera storage backend: - st, err := posix.NewMigrationTarget(ctx, *storageDir, *initialise, tessera.WithCTLayout()) + st, err := posix.NewMigrationTarget(ctx, *storageDir, *initialise, internal.BundleHasher, tessera.WithCTLayout()) if err != nil { klog.Exitf("Failed to create new GCP storage: %v", err) } diff --git a/storage/posix/files.go b/storage/posix/files.go index 5e5ce35a..08b74848 100644 --- a/storage/posix/files.go +++ b/storage/posix/files.go @@ -509,10 +509,14 @@ func createExclusive(f string, d []byte) error { return nil } +// BundleHasherFunc is the signature of a function which knows how to parse an entry bundle and calculate leaf hashes for its entries. +type BundleHasherFunc func(entryBundle []byte) (LeafHashes [][]byte, err error) + // NewMigrationTarget creates a new POSIX storage for the MigrationTarget lifecycle mode. // - path is a directory in which the log should be stored // - create must only be set when first creating the log, and will create the directory structure and an empty checkpoint -func NewMigrationTarget(ctx context.Context, path string, create bool, opts ...func(*options.StorageOptions)) (*MigrationStorage, error) { +// - bundleHasher knows how to parse the provided entry bundle, and returns a slice of leaf hashes for the entries it contains. +func NewMigrationTarget(ctx context.Context, path string, create bool, bundleHasher BundleHasherFunc, opts ...func(*options.StorageOptions)) (*MigrationStorage, error) { opt := storage.ResolveStorageOptions(opts...) r := &MigrationStorage{ @@ -520,6 +524,7 @@ func NewMigrationTarget(ctx context.Context, path string, create bool, opts ...f path: path, entriesPath: opt.EntriesPath, }, + bundleHasher: bundleHasher, } if err := r.s.initialise(create); err != nil { return nil, err @@ -528,7 +533,8 @@ func NewMigrationTarget(ctx context.Context, path string, create bool, opts ...f } type MigrationStorage struct { - s Storage + s Storage + bundleHasher BundleHasherFunc } func (m *MigrationStorage) AwaitIntegration(ctx context.Context, sourceSize uint64, sourceRoot []byte) error { @@ -626,14 +632,11 @@ func (m *MigrationStorage) fetchLeafHashes(ctx context.Context, from, to, source return nil, fmt.Errorf("ReadEntryBundle(%d.%d): %v", idx, p, err) } - eb := &api.EntryBundle{} - if err := eb.UnmarshalText(b); err != nil { - return nil, fmt.Errorf("unmarshal bundle index %d: %v", idx, err) - } - for _, e := range eb.Entries { - h := rfc6962.DefaultHasher.HashLeaf(e) - lh = append(lh, h[:]) + bh, err := m.bundleHasher(b) + if err != nil { + return nil, fmt.Errorf("bundleHasherFunc for bundle index %d: %v", idx, err) } + lh = append(lh, bh...) n++ if n >= maxBundles { break