Skip to content

Commit

Permalink
Pass in bundle hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Jan 10, 2025
1 parent 535600e commit 80e52e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
16 changes: 16 additions & 0 deletions cmd/experimental/migrate/internal/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/experimental/migrate/posix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 12 additions & 9 deletions storage/posix/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,17 +509,22 @@ 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{
s: Storage{
path: path,
entriesPath: opt.EntriesPath,
},
bundleHasher: bundleHasher,
}
if err := r.s.initialise(create); err != nil {
return nil, err
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80e52e0

Please sign in to comment.