-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add catalog update address refs changeset #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gustavogama-cll
wants to merge
1
commit into
ggama/feat/add-catalog-create-address-refs-changeset
Choose a base branch
from
ggama/feat/add-catalog-update-address-refs-changeset
base: ggama/feat/add-catalog-create-address-refs-changeset
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package changesets | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/samber/lo" | ||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldfops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
|
|
||
| "github.com/smartcontractkit/cld-changesets/catalog/operations" | ||
| ) | ||
|
|
||
| // UpdateAddressRefChangeset updates existing address ref entries in the Catalog service. | ||
| type UpdateAddressRefChangeset struct{} | ||
|
|
||
| type UpdateAddressRefChangesetInput struct { | ||
| AddressRefs []cldfdatastore.AddressRef `json:"addressRefs"` | ||
| } | ||
|
|
||
| // VerifyPreconditions ensures the input is valid. | ||
| func (UpdateAddressRefChangeset) VerifyPreconditions(e cldf.Environment, input UpdateAddressRefChangesetInput) error { | ||
| if len(input.AddressRefs) == 0 { | ||
| return errors.New("missing address refs input") | ||
| } | ||
| if e.DataStore == nil { | ||
| return errors.New("missing datastore in environment") | ||
| } | ||
|
|
||
| uniqAddressRefs := lo.UniqBy(input.AddressRefs, func(ar cldfdatastore.AddressRef) cldfdatastore.AddressRefKey { | ||
| return ar.Key() | ||
| }) | ||
| if len(uniqAddressRefs) != len(input.AddressRefs) { | ||
| return errors.New("duplicate address ref entries found in input") | ||
| } | ||
|
|
||
| for _, addressRef := range input.AddressRefs { | ||
| _, err := e.DataStore.Addresses().Get(addressRef.Key()) | ||
| if errors.Is(err, cldfdatastore.ErrAddressRefNotFound) { | ||
| return fmt.Errorf("address ref for chain selector %v, type %v, version %v and qualifier %q does not exist", | ||
| addressRef.ChainSelector, addressRef.Type, addressRef.Version, addressRef.Qualifier) | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("failed to retrieve address ref for chain selector %v, type %v, version %v and qualifier %q: %w", | ||
| addressRef.ChainSelector, addressRef.Type, addressRef.Version, addressRef.Qualifier, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Apply executes the changeset, updating the address refs in the Catalog service. | ||
| func (UpdateAddressRefChangeset) Apply( | ||
| e cldf.Environment, input UpdateAddressRefChangesetInput, | ||
| ) (cldf.ChangesetOutput, error) { | ||
| deps := operations.UpdateAddressRefDeps{DataStore: e.DataStore} | ||
| opInput := operations.UpdateAddressRefInput{AddressRefs: input.AddressRefs} | ||
|
|
||
| report, err := cldfops.ExecuteOperation(e.OperationsBundle, operations.UpdateAddressRefOp, deps, opInput) | ||
| out := cldf.ChangesetOutput{ | ||
| DataStore: report.Output.DataStore, | ||
| Reports: []cldfops.Report[any, any]{report.ToGenericReport()}, | ||
| } | ||
| if err != nil { | ||
| return out, err | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package changesets | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldfoperations "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
| cldflogger "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" | ||
|
|
||
| "github.com/smartcontractkit/cld-changesets/catalog/operations" | ||
| ) | ||
|
|
||
| func TestUpdateAddressRefChangeset_VerifyPreconditions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| version := semver.MustParse("1.0.0") | ||
| addressRef1 := cldfdatastore.AddressRef{Address: "0x01", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} | ||
| addressRef2 := cldfdatastore.AddressRef{Address: "0x02", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| env cldf.Environment | ||
| input UpdateAddressRefChangesetInput | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "success: valid preconditions", | ||
| env: cldf.Environment{DataStore: func() cldfdatastore.DataStore { | ||
| ds := cldfdatastore.NewMemoryDataStore() | ||
| err := ds.Addresses().Add(addressRef1) | ||
| require.NoError(t, err) | ||
|
|
||
| return ds.Seal() | ||
| }()}, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "failure: missing datastore", | ||
| env: cldf.Environment{}, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1}, | ||
| }, | ||
| wantErr: "missing datastore in environment", | ||
| }, | ||
| { | ||
| name: "failure: no address refs given", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{}, | ||
| }, | ||
| wantErr: "missing address refs input", | ||
| }, | ||
| { | ||
| name: "failure: duplicate entries", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1, addressRef2}, | ||
| }, | ||
| wantErr: "duplicate address ref entries found in input", | ||
| }, | ||
| { | ||
| name: "failure: address ref does not exist", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1}, | ||
| }, | ||
| wantErr: "address ref for chain selector 1234, type MyContract, version 1.0.0 and qualifier \"q1\" does not exist", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := UpdateAddressRefChangeset{}.VerifyPreconditions(tt.env, tt.input) | ||
|
|
||
| if tt.wantErr == "" { | ||
| require.NoError(t, err) | ||
| } else { | ||
| require.ErrorContains(t, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestUpdateAddressRefChangeset_Apply(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| version := semver.MustParse("1.0.0") | ||
| addressRef1 := cldfdatastore.AddressRef{Address: "0x01", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} | ||
| addressRef2 := cldfdatastore.AddressRef{Address: "0x02", ChainSelector: 5678, Type: "OtherContract", Version: version, Qualifier: "q2"} | ||
| addressRef1Updated := cldfdatastore.AddressRef{Address: "0x99", ChainSelector: 1234, Type: "MyContract", Version: version, Qualifier: "q1"} | ||
| addressRef2Updated := cldfdatastore.AddressRef{Address: "0x88", ChainSelector: 5678, Type: "OtherContract", Version: version, Qualifier: "q2"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| env cldf.Environment | ||
| input UpdateAddressRefChangesetInput | ||
| want cldf.ChangesetOutput | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "success: updates two entries in address refs", | ||
| env: cldf.Environment{ | ||
| DataStore: testDataStoreWithAddressRefs(t, addressRef1, addressRef2).Seal(), | ||
| OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), | ||
| }, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1Updated, addressRef2Updated}, | ||
| }, | ||
| want: cldf.ChangesetOutput{ | ||
| DataStore: testDataStoreWithAddressRefs(t, addressRef1Updated, addressRef2Updated), | ||
| Reports: []cldfoperations.Report[any, any]{{ | ||
| Def: cldfoperations.Definition{ | ||
| ID: "catalog-update-address-ref", | ||
| Version: semver.MustParse("1.0.0"), | ||
| Description: "Update address ref entries in the Catalog service", | ||
| }, | ||
| Input: operations.UpdateAddressRefInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1Updated, addressRef2Updated}, | ||
| }, | ||
| Output: operations.UpdateAddressRefOutput{ | ||
| DataStore: testDataStoreWithAddressRefs(t, addressRef1Updated, addressRef2Updated), | ||
| }, | ||
| }}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "failure: fails to update entry that does not exist", | ||
| env: cldf.Environment{ | ||
| DataStore: testDataStoreWithAddressRefs(t).Seal(), | ||
| OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), | ||
| }, | ||
| input: UpdateAddressRefChangesetInput{ | ||
| AddressRefs: []cldfdatastore.AddressRef{addressRef1Updated}, | ||
| }, | ||
| wantErr: "failed to update address ref entry 0 in catalog store: " + | ||
| "no such address ref can be found for the provided key", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := UpdateAddressRefChangeset{}.Apply(tt.env, tt.input) | ||
|
|
||
| if tt.wantErr == "" { | ||
| require.NoError(t, err) | ||
| require.Empty(t, | ||
| cmp.Diff(tt.want, got, | ||
| cmpopts.IgnoreFields(cldfoperations.Report[any, any]{}, "ID", "Timestamp"), | ||
| cmpopts.IgnoreUnexported(cldfdatastore.MemoryAddressRefStore{}, cldfdatastore.MemoryChainMetadataStore{}, | ||
| cldfdatastore.MemoryContractMetadataStore{}, cldfdatastore.MemoryEnvMetadataStore{}, | ||
| cldfdatastore.LabelSet{}))) | ||
| } else { | ||
| require.ErrorContains(t, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package operations | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
|
|
||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldfops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
| ) | ||
|
|
||
| // UpdateAddressRefDeps holds non-serializable dependencies for the | ||
| // UpdateAddressRefOp operation. | ||
| type UpdateAddressRefDeps struct { | ||
| DataStore cldfdatastore.DataStore | ||
| } | ||
|
|
||
| // UpdateAddressRefInput is the serializable input of an UpdateAddressRefOp invocation. | ||
| type UpdateAddressRefInput struct { | ||
| AddressRefs []cldfdatastore.AddressRef | ||
| } | ||
|
|
||
| // UpdateAddressRefOutput is the serializable output of an UpdateAddressRefOp invocation. | ||
| type UpdateAddressRefOutput struct { | ||
| DataStore cldfdatastore.MutableDataStore | ||
| } | ||
|
|
||
| // UpdateAddressRefOp updates existing address ref entries in the Catalog service. | ||
| var UpdateAddressRefOp = cldfops.NewOperation( | ||
| "catalog-update-address-ref", | ||
| semver.MustParse("1.0.0"), | ||
| "Update address ref entries in the Catalog service", | ||
| func(b cldfops.Bundle, deps UpdateAddressRefDeps, input UpdateAddressRefInput) (UpdateAddressRefOutput, error) { | ||
| dataStore := cldfdatastore.NewMemoryDataStore() | ||
| err := dataStore.Merge(deps.DataStore) | ||
| if err != nil { | ||
| return UpdateAddressRefOutput{}, fmt.Errorf("failed to create memory data store: %w", err) | ||
|
gustavogama-cll marked this conversation as resolved.
|
||
| } | ||
|
|
||
| for i, item := range input.AddressRefs { | ||
| err = dataStore.Addresses().Update(item) | ||
| if err != nil { | ||
| return UpdateAddressRefOutput{}, fmt.Errorf("failed to update address ref entry %d in catalog store: %w", i, err) | ||
| } | ||
| } | ||
|
|
||
| b.Logger.Infow("Catalog AddressRef updated successfully") | ||
|
|
||
| return UpdateAddressRefOutput{DataStore: dataStore}, nil | ||
| }, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.