Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
phbnf committed Aug 23, 2024
1 parent 82792bb commit 98ab77d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions personalities/sctfe/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ type KV struct {
V []byte
}

// TODO(phboneff): replace with AddIssuersIfNotExist
// IsuerStorage issuer certificates under their hex encoded sha256.
type IssuerStorage interface {
AddIssuersIfNotExist(ctx context.Context, kv []KV) error
}

// CTStorage implements Storage.
type CTStorage struct {
storeData func(context.Context, *ctonly.Entry) (uint64, error)
issuers IssuerStorage
storeData func(context.Context, *ctonly.Entry) (uint64, error)
storeIssuers func(context.Context, []KV) error
}

// NewCTStorage instantiates a CTStorage object.
func NewCTSTorage(logStorage tessera.Storage, issuerStorage IssuerStorage) (*CTStorage, error) {
ctStorage := &CTStorage{
storeData: tessera.NewCertificateTransparencySequencedWriter(logStorage),
issuers: NewCachedIssuerStorage(issuerStorage),
storeData: tessera.NewCertificateTransparencySequencedWriter(logStorage),
storeIssuers: NewCachedIssuerStorage(issuerStorage).AddIssuersIfNotExist,
}
return ctStorage, nil
}
Expand All @@ -83,7 +83,7 @@ func (cts *CTStorage) AddIssuerChain(ctx context.Context, chain []*x509.Certific
key := []byte(hex.EncodeToString(id[:]))
kvs = append(kvs, KV{K: key, V: c.Raw})
}
if err := cts.issuers.AddIssuersIfNotExist(ctx, kvs); err != nil {
if err := cts.storeIssuers(ctx, kvs); err != nil {
return fmt.Errorf("error storing intermediates: %v", err)
}
return nil
Expand All @@ -101,6 +101,11 @@ type cachedIssuerStorage struct {
s IssuerStorage
}

// NewCachedIssuerStorage returns a new local cache to wrapping an IssuerStorage
func NewCachedIssuerStorage(s IssuerStorage) *cachedIssuerStorage {
return &cachedIssuerStorage{s: s, N: maxCachedIssuerKeys, m: make(map[string]struct{})}
}

// AddIssuersIfNotExist adds the issuers to the underlying storage if they're not already cached.
//
// Caches the 256 of issuer it successfuly wrote to the underlying sotrage.
Expand Down Expand Up @@ -129,7 +134,3 @@ func (c *cachedIssuerStorage) AddIssuersIfNotExist(ctx context.Context, kv []KV)
}
return nil
}

func NewCachedIssuerStorage(s IssuerStorage) *cachedIssuerStorage {
return &cachedIssuerStorage{s: s, N: maxCachedIssuerKeys, m: make(map[string]struct{})}
}

0 comments on commit 98ab77d

Please sign in to comment.