Skip to content

Commit b7c8d06

Browse files
feat(engine): load catalog into datastore (#535)
If catalog is selected as the datastore type, we fetch all the required data from catalog service and load it into the local datastore. JIRA: https://smartcontract-it.atlassian.net/browse/CLD-781
1 parent b05e2e0 commit b7c8d06

File tree

10 files changed

+2066
-5
lines changed

10 files changed

+2066
-5
lines changed

.changeset/eighty-phones-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": minor
3+
---
4+
5+
feat(engine): load catalog into datastore

.mockery.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,13 @@ packages:
8181
filename: "mock_{{.InterfaceName | snakecase}}.go"
8282
interfaces:
8383
NodeServiceClient:
84+
github.com/smartcontractkit/chainlink-deployments-framework/datastore:
85+
config:
86+
all: false
87+
filename: "mock_{{.InterfaceName | snakecase}}_test.go"
88+
structname: "{{.Mock}}{{.InterfaceName | firstUpper }}"
89+
interfaces:
90+
CatalogStore:
91+
MutableRefStoreV2:
92+
MutableStoreV2:
93+
MutableUnaryStoreV2:

datastore/catalog_loader.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package datastore
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
)
8+
9+
// LoadDataStoreFromCatalog fetches all data from a remote CatalogStore and creates a local
10+
// in-memory DataStore populated with that data. After loading, all operations are performed
11+
// on the local DataStore without any remote calls.
12+
func LoadDataStoreFromCatalog(ctx context.Context, catalog CatalogStore) (DataStore, error) {
13+
// Create a new mutable in-memory datastore
14+
memoryDS := NewMemoryDataStore()
15+
16+
// Fetch all address references from the catalog
17+
addressRefs, err := catalog.Addresses().Fetch(ctx)
18+
if err != nil {
19+
// If no address refs found, treat as empty catalog (valid state)
20+
if !errors.Is(err, ErrAddressRefNotFound) {
21+
return nil, fmt.Errorf("failed to fetch address references from catalog: %w", err)
22+
}
23+
addressRefs = []AddressRef{} // Empty catalog is valid
24+
}
25+
26+
// Populate the address ref store
27+
for _, ref := range addressRefs {
28+
if addErr := memoryDS.Addresses().Add(ref); addErr != nil {
29+
return nil, fmt.Errorf("failed to add address reference to local store: %w", addErr)
30+
}
31+
}
32+
33+
// Fetch all chain metadata from the catalog
34+
chainMetadata, err := catalog.ChainMetadata().Fetch(ctx)
35+
if err != nil {
36+
// If no chain metadata found, treat as empty catalog (valid state)
37+
if !errors.Is(err, ErrChainMetadataNotFound) {
38+
return nil, fmt.Errorf("failed to fetch chain metadata from catalog: %w", err)
39+
}
40+
chainMetadata = []ChainMetadata{} // Empty catalog is valid
41+
}
42+
43+
// Populate the chain metadata store
44+
for _, metadata := range chainMetadata {
45+
if addErr := memoryDS.ChainMetadata().Add(metadata); addErr != nil {
46+
return nil, fmt.Errorf("failed to add chain metadata to local store: %w", addErr)
47+
}
48+
}
49+
50+
// Fetch all contract metadata from the catalog
51+
contractMetadata, err := catalog.ContractMetadata().Fetch(ctx)
52+
if err != nil {
53+
// If no contract metadata found, treat as empty catalog (valid state)
54+
if !errors.Is(err, ErrContractMetadataNotFound) {
55+
return nil, fmt.Errorf("failed to fetch contract metadata from catalog: %w", err)
56+
}
57+
contractMetadata = []ContractMetadata{} // Empty catalog is valid
58+
}
59+
60+
// Populate the contract metadata store
61+
for _, metadata := range contractMetadata {
62+
if addErr := memoryDS.ContractMetadata().Add(metadata); addErr != nil {
63+
return nil, fmt.Errorf("failed to add contract metadata to local store: %w", addErr)
64+
}
65+
}
66+
67+
// Fetch environment metadata from the catalog
68+
envMetadata, err := catalog.EnvMetadata().Get(ctx)
69+
if err != nil {
70+
// EnvMetadata might not exist, which is okay - ignore ErrEnvMetadataNotSet
71+
if !errors.Is(err, ErrEnvMetadataNotSet) {
72+
return nil, fmt.Errorf("failed to fetch environment metadata from catalog: %w", err)
73+
}
74+
// If it's ErrEnvMetadataNotSet, continue without error
75+
} else {
76+
// Populate the environment metadata store
77+
if setErr := memoryDS.EnvMetadata().Set(envMetadata); setErr != nil {
78+
return nil, fmt.Errorf("failed to set environment metadata in local store: %w", setErr)
79+
}
80+
}
81+
82+
// Seal the datastore to make it read-only and return
83+
return memoryDS.Seal(), nil
84+
}

0 commit comments

Comments
 (0)