Skip to content

Commit

Permalink
fix: make tags accessible to callers of signerootreader (#18)
Browse files Browse the repository at this point in the history
A quirk of the interface for lazily reading the root seal blobs
prevented the blob tags from being available to the caller of
ReadLogicalContext.

This also now works consistently for all methods on the signed root
reader which use this method internaly.

Rather than egregiously break an existing public interface, and also
because it seems cleaner, we just save the last context read and provide
a method to retrieve it if wanted.

The method to retrieve will error if the context hasn't been read at
all.

AB#9570

Co-authored-by: Robin Bryce <robin.bryce@datatrails.ai>
  • Loading branch information
robinbryce and Robin Bryce authored Jun 4, 2024
1 parent 563997e commit c09ee35
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions massifs/signedrootreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package massifs

import (
"context"
"errors"

"github.com/datatrails/go-datatrails-common/azblob"
"github.com/datatrails/go-datatrails-common/cbor"
dtcose "github.com/datatrails/go-datatrails-common/cose"
"github.com/datatrails/go-datatrails-common/logger"
)

var (
ErrLogContextNotRead = errors.New("attempted to use lastContext before it was read")
)

// SignedRootReader provides a context for reading the signed tree head associated with a massif
// Note: the acronym is due to RFC 9162
type SignedRootReader struct {
log logger.Logger
store logBlobReader
codec cbor.CBORCodec
// lastContext saves the last context read from blob store, this includes
// Tags if they were requested
lastContext LogBlobContext
}

func NewSignedRootReader(log logger.Logger, store logBlobReader, codec cbor.CBORCodec) SignedRootReader {
Expand All @@ -26,6 +34,16 @@ func NewSignedRootReader(log logger.Logger, store logBlobReader, codec cbor.CBOR
return r
}

// GetLastReadContext returns a copy of the most recently read context. Use this
// to get access to the tags when using WithGetTags. If the context hasn't been
// read (directly or indirectly) an error is returned.
func (s *SignedRootReader) GetLastReadContext() (LogBlobContext, error) {
if s.lastContext.BlobPath == "" {
return LogBlobContext{}, ErrLogContextNotRead
}
return s.lastContext, nil
}

func (s *SignedRootReader) GetLazyContext(
ctx context.Context, tenantIdentity string, which LogicalBlob,
opts ...azblob.Option,
Expand All @@ -48,6 +66,7 @@ func (s *SignedRootReader) GetLazyContext(
if err != nil {
return LogBlobContext{}, 0, err
}
s.lastContext = logBlobContext
return logBlobContext, count, nil
}

Expand All @@ -60,6 +79,7 @@ func (s *SignedRootReader) ReadLogicalContext(
if err != nil {
return nil, MMRState{}, err
}
s.lastContext = logContext

signed, unverifiedState, err := DecodeSignedRoot(s.codec, logContext.Data)
if err != nil {
Expand Down Expand Up @@ -130,14 +150,15 @@ func (s *SignedRootReader) GetLatestMassifSignedRoot(
opts ...azblob.Option,
) (*dtcose.CoseSign1Message, MMRState, error) {

lc := LogBlobContext{
logContext := LogBlobContext{
BlobPath: TenantMassifSignedRootPath(tenantIdentity, massifIndex),
}
err := lc.ReadData(ctx, s.store, opts...)
err := logContext.ReadData(ctx, s.store, opts...)
if err != nil {
return nil, MMRState{}, err
}
signed, unverifiedState, err := DecodeSignedRoot(s.codec, lc.Data)
s.lastContext = logContext
signed, unverifiedState, err := DecodeSignedRoot(s.codec, logContext.Data)
if err != nil {
return nil, MMRState{}, err
}
Expand Down

0 comments on commit c09ee35

Please sign in to comment.