From c09ee35445f7383af8bd318f2eeb9f75d3245603 Mon Sep 17 00:00:00 2001 From: robinbryce Date: Tue, 4 Jun 2024 11:00:33 +0100 Subject: [PATCH] fix: make tags accessible to callers of signerootreader (#18) 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 --- massifs/signedrootreader.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/massifs/signedrootreader.go b/massifs/signedrootreader.go index e3aa2fb..ad6f483 100644 --- a/massifs/signedrootreader.go +++ b/massifs/signedrootreader.go @@ -2,6 +2,7 @@ package massifs import ( "context" + "errors" "github.com/datatrails/go-datatrails-common/azblob" "github.com/datatrails/go-datatrails-common/cbor" @@ -9,12 +10,19 @@ import ( "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 { @@ -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, @@ -48,6 +66,7 @@ func (s *SignedRootReader) GetLazyContext( if err != nil { return LogBlobContext{}, 0, err } + s.lastContext = logBlobContext return logBlobContext, count, nil } @@ -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 { @@ -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 }