From 8ab22b3af6061557cca6baf219bc07e801281830 Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Wed, 22 Jun 2022 14:43:42 -0400 Subject: [PATCH] Add namespace to contract listener database queries Signed-off-by: Andrew Richardson --- .../route_delete_contract_listener.go | 2 +- .../route_delete_contract_listener_test.go | 2 +- .../route_get_contract_api_listeners.go | 2 +- .../route_get_contract_api_listeners_test.go | 2 +- ...ute_get_contract_listener_by_name_or_id.go | 2 +- ...et_contract_listener_by_name_or_id_test.go | 2 +- .../route_get_contract_listener_test.go | 2 +- .../apiserver/route_get_contract_listeners.go | 2 +- .../route_post_contract_api_listeners.go | 2 +- .../route_post_contract_api_listeners_test.go | 2 +- .../route_post_contract_interface_generate.go | 2 +- ...e_post_contract_interface_generate_test.go | 2 +- .../route_post_new_contract_listener.go | 2 +- .../route_post_new_contract_listener_test.go | 2 +- internal/contracts/manager.go | 72 ++++----- internal/contracts/manager_test.go | 140 +++++++++--------- .../sqlcommon/contractlisteners_sql.go | 20 +-- .../sqlcommon/contractlisteners_sql_test.go | 30 ++-- internal/events/batch_pin_complete.go | 2 +- internal/events/blockchain_event.go | 41 ++--- internal/events/blockchain_event_test.go | 51 +------ internal/events/network_action.go | 2 +- internal/events/token_pool_created.go | 2 +- internal/events/tokens_approved.go | 2 +- internal/events/tokens_transferred.go | 2 +- mocks/contractmocks/manager.go | 102 ++++++------- mocks/databasemocks/plugin.go | 56 +++---- pkg/database/plugin.go | 20 +-- 28 files changed, 252 insertions(+), 318 deletions(-) diff --git a/internal/apiserver/route_delete_contract_listener.go b/internal/apiserver/route_delete_contract_listener.go index 1befaf364..38a9a4677 100644 --- a/internal/apiserver/route_delete_contract_listener.go +++ b/internal/apiserver/route_delete_contract_listener.go @@ -37,7 +37,7 @@ var deleteContractListener = &ffapi.Route{ JSONOutputCodes: []int{http.StatusNoContent}, // Sync operation, no output Extensions: &coreExtensions{ CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - err = cr.or.Contracts().DeleteContractListenerByNameOrID(cr.ctx, extractNamespace(r.PP), r.PP["nameOrId"]) + err = cr.or.Contracts().DeleteContractListenerByNameOrID(cr.ctx, r.PP["nameOrId"]) return nil, err }, }, diff --git a/internal/apiserver/route_delete_contract_listener_test.go b/internal/apiserver/route_delete_contract_listener_test.go index 3872049b7..4ed6362a2 100644 --- a/internal/apiserver/route_delete_contract_listener_test.go +++ b/internal/apiserver/route_delete_contract_listener_test.go @@ -35,7 +35,7 @@ func TestDeleteContractListenerByID(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("DeleteContractListenerByNameOrID", mock.Anything, "mynamespace", id.String()). + mcm.On("DeleteContractListenerByNameOrID", mock.Anything, id.String()). Return(nil, nil) r.ServeHTTP(res, req) diff --git a/internal/apiserver/route_get_contract_api_listeners.go b/internal/apiserver/route_get_contract_api_listeners.go index c0283b090..818716dda 100644 --- a/internal/apiserver/route_get_contract_api_listeners.go +++ b/internal/apiserver/route_get_contract_api_listeners.go @@ -41,7 +41,7 @@ var getContractAPIListeners = &ffapi.Route{ Extensions: &coreExtensions{ FilterFactory: database.ContractListenerQueryFactory, CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - return filterResult(cr.or.Contracts().GetContractAPIListeners(cr.ctx, extractNamespace(r.PP), r.PP["apiName"], r.PP["eventPath"], cr.filter)) + return filterResult(cr.or.Contracts().GetContractAPIListeners(cr.ctx, r.PP["apiName"], r.PP["eventPath"], cr.filter)) }, }, } diff --git a/internal/apiserver/route_get_contract_api_listeners_test.go b/internal/apiserver/route_get_contract_api_listeners_test.go index aadf4869f..133cb0960 100644 --- a/internal/apiserver/route_get_contract_api_listeners_test.go +++ b/internal/apiserver/route_get_contract_api_listeners_test.go @@ -39,7 +39,7 @@ func TestGetContractAPIListeners(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("GetContractAPIListeners", mock.Anything, "ns1", "banana", "peeled", mock.Anything). + mcm.On("GetContractAPIListeners", mock.Anything, "banana", "peeled", mock.Anything). Return([]*core.ContractListener{}, nil, nil) r.ServeHTTP(res, req) diff --git a/internal/apiserver/route_get_contract_listener_by_name_or_id.go b/internal/apiserver/route_get_contract_listener_by_name_or_id.go index 732ab09a7..1704e4cbb 100644 --- a/internal/apiserver/route_get_contract_listener_by_name_or_id.go +++ b/internal/apiserver/route_get_contract_listener_by_name_or_id.go @@ -38,7 +38,7 @@ var getContractListenerByNameOrID = &ffapi.Route{ JSONOutputCodes: []int{http.StatusOK}, Extensions: &coreExtensions{ CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - return cr.or.Contracts().GetContractListenerByNameOrID(cr.ctx, extractNamespace(r.PP), r.PP["nameOrId"]) + return cr.or.Contracts().GetContractListenerByNameOrID(cr.ctx, r.PP["nameOrId"]) }, }, } diff --git a/internal/apiserver/route_get_contract_listener_by_name_or_id_test.go b/internal/apiserver/route_get_contract_listener_by_name_or_id_test.go index d8a306fa8..360ab2bfd 100644 --- a/internal/apiserver/route_get_contract_listener_by_name_or_id_test.go +++ b/internal/apiserver/route_get_contract_listener_by_name_or_id_test.go @@ -36,7 +36,7 @@ func TestGetContractListenerByNameOrID(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("GetContractListenerByNameOrID", mock.Anything, "mynamespace", id.String()). + mcm.On("GetContractListenerByNameOrID", mock.Anything, id.String()). Return(&core.ContractListener{}, nil) r.ServeHTTP(res, req) diff --git a/internal/apiserver/route_get_contract_listener_test.go b/internal/apiserver/route_get_contract_listener_test.go index a7c448a5c..23cf97b7b 100644 --- a/internal/apiserver/route_get_contract_listener_test.go +++ b/internal/apiserver/route_get_contract_listener_test.go @@ -34,7 +34,7 @@ func TestGetContractListener(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("GetContractListeners", mock.Anything, "mynamespace", mock.Anything). + mcm.On("GetContractListeners", mock.Anything, mock.Anything). Return([]*core.ContractListener{}, nil, nil) r.ServeHTTP(res, req) diff --git a/internal/apiserver/route_get_contract_listeners.go b/internal/apiserver/route_get_contract_listeners.go index 03d32c576..baab05240 100644 --- a/internal/apiserver/route_get_contract_listeners.go +++ b/internal/apiserver/route_get_contract_listeners.go @@ -38,7 +38,7 @@ var getContractListeners = &ffapi.Route{ Extensions: &coreExtensions{ FilterFactory: database.ContractListenerQueryFactory, CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - return filterResult(cr.or.Contracts().GetContractListeners(cr.ctx, extractNamespace(r.PP), cr.filter)) + return filterResult(cr.or.Contracts().GetContractListeners(cr.ctx, cr.filter)) }, }, } diff --git a/internal/apiserver/route_post_contract_api_listeners.go b/internal/apiserver/route_post_contract_api_listeners.go index 212e32975..c2130fe87 100644 --- a/internal/apiserver/route_post_contract_api_listeners.go +++ b/internal/apiserver/route_post_contract_api_listeners.go @@ -39,7 +39,7 @@ var postContractAPIListeners = &ffapi.Route{ JSONOutputCodes: []int{http.StatusOK}, Extensions: &coreExtensions{ CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - return cr.or.Contracts().AddContractAPIListener(cr.ctx, extractNamespace(r.PP), r.PP["apiName"], r.PP["eventPath"], r.Input.(*core.ContractListener)) + return cr.or.Contracts().AddContractAPIListener(cr.ctx, r.PP["apiName"], r.PP["eventPath"], r.Input.(*core.ContractListener)) }, }, } diff --git a/internal/apiserver/route_post_contract_api_listeners_test.go b/internal/apiserver/route_post_contract_api_listeners_test.go index 723459520..ff70fcb53 100644 --- a/internal/apiserver/route_post_contract_api_listeners_test.go +++ b/internal/apiserver/route_post_contract_api_listeners_test.go @@ -39,7 +39,7 @@ func TestPostContractAPIListen(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("AddContractAPIListener", mock.Anything, "ns1", "banana", "peeled", mock.AnythingOfType("*core.ContractListener")).Return(&core.ContractListener{}, nil) + mcm.On("AddContractAPIListener", mock.Anything, "banana", "peeled", mock.AnythingOfType("*core.ContractListener")).Return(&core.ContractListener{}, nil) r.ServeHTTP(res, req) assert.Equal(t, 200, res.Result().StatusCode) diff --git a/internal/apiserver/route_post_contract_interface_generate.go b/internal/apiserver/route_post_contract_interface_generate.go index 3d3d1805a..a2577dc8f 100644 --- a/internal/apiserver/route_post_contract_interface_generate.go +++ b/internal/apiserver/route_post_contract_interface_generate.go @@ -37,7 +37,7 @@ var postContractInterfaceGenerate = &ffapi.Route{ Extensions: &coreExtensions{ CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { generationRequest := r.Input.(*core.FFIGenerationRequest) - return cr.or.Contracts().GenerateFFI(cr.ctx, extractNamespace(r.PP), generationRequest) + return cr.or.Contracts().GenerateFFI(cr.ctx, generationRequest) }, }, } diff --git a/internal/apiserver/route_post_contract_interface_generate_test.go b/internal/apiserver/route_post_contract_interface_generate_test.go index 64073cb60..e0329ddec 100644 --- a/internal/apiserver/route_post_contract_interface_generate_test.go +++ b/internal/apiserver/route_post_contract_interface_generate_test.go @@ -39,7 +39,7 @@ func TestPostContractInterfaceGenerate(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("GenerateFFI", mock.Anything, "ns1", mock.Anything). + mcm.On("GenerateFFI", mock.Anything, mock.Anything). Return(&core.FFI{}, nil) r.ServeHTTP(res, req) diff --git a/internal/apiserver/route_post_new_contract_listener.go b/internal/apiserver/route_post_new_contract_listener.go index 1e1f709d0..2afcfba9c 100644 --- a/internal/apiserver/route_post_new_contract_listener.go +++ b/internal/apiserver/route_post_new_contract_listener.go @@ -36,7 +36,7 @@ var postNewContractListener = &ffapi.Route{ JSONOutputCodes: []int{http.StatusOK}, Extensions: &coreExtensions{ CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) { - return cr.or.Contracts().AddContractListener(cr.ctx, extractNamespace(r.PP), r.Input.(*core.ContractListenerInput)) + return cr.or.Contracts().AddContractListener(cr.ctx, r.Input.(*core.ContractListenerInput)) }, }, } diff --git a/internal/apiserver/route_post_new_contract_listener_test.go b/internal/apiserver/route_post_new_contract_listener_test.go index e95fbff2e..25e39e69a 100644 --- a/internal/apiserver/route_post_new_contract_listener_test.go +++ b/internal/apiserver/route_post_new_contract_listener_test.go @@ -39,7 +39,7 @@ func TestPostNewContractListener(t *testing.T) { req.Header.Set("Content-Type", "application/json; charset=utf-8") res := httptest.NewRecorder() - mcm.On("AddContractListener", mock.Anything, "mynamespace", mock.AnythingOfType("*core.ContractListenerInput")). + mcm.On("AddContractListener", mock.Anything, mock.AnythingOfType("*core.ContractListenerInput")). Return(&core.ContractListener{}, nil, nil) r.ServeHTTP(res, req) diff --git a/internal/contracts/manager.go b/internal/contracts/manager.go index f393285a3..3d6537f76 100644 --- a/internal/contracts/manager.go +++ b/internal/contracts/manager.go @@ -54,13 +54,13 @@ type Manager interface { ValidateFFIAndSetPathnames(ctx context.Context, ffi *core.FFI) error - AddContractListener(ctx context.Context, ns string, listener *core.ContractListenerInput) (output *core.ContractListener, err error) - AddContractAPIListener(ctx context.Context, ns, apiName, eventPath string, listener *core.ContractListener) (output *core.ContractListener, err error) - GetContractListenerByNameOrID(ctx context.Context, ns, nameOrID string) (*core.ContractListener, error) - GetContractListeners(ctx context.Context, ns string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) - GetContractAPIListeners(ctx context.Context, ns string, apiName, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) - DeleteContractListenerByNameOrID(ctx context.Context, ns, nameOrID string) error - GenerateFFI(ctx context.Context, ns string, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) + AddContractListener(ctx context.Context, listener *core.ContractListenerInput) (output *core.ContractListener, err error) + AddContractAPIListener(ctx context.Context, apiName, eventPath string, listener *core.ContractListener) (output *core.ContractListener, err error) + GetContractListenerByNameOrID(ctx context.Context, nameOrID string) (*core.ContractListener, error) + GetContractListeners(ctx context.Context, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) + GetContractAPIListeners(ctx context.Context, apiName, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) + DeleteContractListenerByNameOrID(ctx context.Context, nameOrID string) error + GenerateFFI(ctx context.Context, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) // From operations.OperationHandler PrepareOperation(ctx context.Context, op *core.Operation) (*core.PreparedOperation, error) @@ -147,10 +147,6 @@ func (cm *contractManager) BroadcastFFI(ctx context.Context, ffi *core.FFI, wait return ffi, nil } -func (cm *contractManager) scopeNS(ns string, filter database.AndFilter) database.AndFilter { - return filter.Condition(filter.Builder().Eq("namespace", ns)) -} - func (cm *contractManager) GetFFI(ctx context.Context, name, version string) (*core.FFI, error) { return cm.database.GetFFI(ctx, cm.namespace, name, version) } @@ -483,11 +479,11 @@ func (cm *contractManager) validateInvokeContractRequest(ctx context.Context, re return nil } -func (cm *contractManager) resolveEvent(ctx context.Context, ns string, ffi *core.FFIReference, eventPath string) (*core.FFISerializedEvent, error) { +func (cm *contractManager) resolveEvent(ctx context.Context, ffi *core.FFIReference, eventPath string) (*core.FFISerializedEvent, error) { if err := cm.resolveFFIReference(ctx, ffi); err != nil { return nil, err } - event, err := cm.database.GetFFIEvent(ctx, ns, ffi.ID, eventPath) + event, err := cm.database.GetFFIEvent(ctx, cm.namespace, ffi.ID, eventPath) if err != nil { return nil, err } else if event == nil { @@ -496,13 +492,10 @@ func (cm *contractManager) resolveEvent(ctx context.Context, ns string, ffi *cor return &core.FFISerializedEvent{FFIEventDefinition: event.FFIEventDefinition}, nil } -func (cm *contractManager) AddContractListener(ctx context.Context, ns string, listener *core.ContractListenerInput) (output *core.ContractListener, err error) { +func (cm *contractManager) AddContractListener(ctx context.Context, listener *core.ContractListenerInput) (output *core.ContractListener, err error) { listener.ID = fftypes.NewUUID() - listener.Namespace = ns + listener.Namespace = cm.namespace - if err := core.ValidateFFNameField(ctx, ns, "namespace"); err != nil { - return nil, err - } if listener.Name != "" { if err := core.ValidateFFNameField(ctx, listener.Name, "name"); err != nil { return nil, err @@ -524,10 +517,10 @@ func (cm *contractManager) AddContractListener(ctx context.Context, ns string, l err = cm.database.RunAsGroup(ctx, func(ctx context.Context) (err error) { // Namespace + Name must be unique if listener.Name != "" { - if existing, err := cm.database.GetContractListener(ctx, ns, listener.Name); err != nil { + if existing, err := cm.database.GetContractListener(ctx, cm.namespace, listener.Name); err != nil { return err } else if existing != nil { - return i18n.NewError(ctx, coremsgs.MsgContractListenerNameExists, ns, listener.Name) + return i18n.NewError(ctx, coremsgs.MsgContractListenerNameExists, cm.namespace, listener.Name) } } @@ -536,7 +529,7 @@ func (cm *contractManager) AddContractListener(ctx context.Context, ns string, l return i18n.NewError(ctx, coremsgs.MsgListenerNoEvent) } // Copy the event definition into the listener - if listener.Event, err = cm.resolveEvent(ctx, ns, listener.Interface, listener.EventPath); err != nil { + if listener.Event, err = cm.resolveEvent(ctx, listener.Interface, listener.EventPath); err != nil { return err } } else { @@ -546,8 +539,7 @@ func (cm *contractManager) AddContractListener(ctx context.Context, ns string, l // Namespace + Topic + Location + Signature must be unique listener.Signature = cm.blockchain.GenerateEventSignature(ctx, &listener.Event.FFIEventDefinition) fb := database.ContractListenerQueryFactory.NewFilter(ctx) - if existing, _, err := cm.database.GetContractListeners(ctx, fb.And( - fb.Eq("namespace", listener.Namespace), + if existing, _, err := cm.database.GetContractListeners(ctx, cm.namespace, fb.And( fb.Eq("topic", listener.Topic), fb.Eq("location", listener.Location.Bytes()), fb.Eq("signature", listener.Signature), @@ -578,8 +570,8 @@ func (cm *contractManager) AddContractListener(ctx context.Context, ns string, l return &listener.ContractListener, err } -func (cm *contractManager) AddContractAPIListener(ctx context.Context, ns, apiName, eventPath string, listener *core.ContractListener) (output *core.ContractListener, err error) { - api, err := cm.database.GetContractAPIByName(ctx, ns, apiName) +func (cm *contractManager) AddContractAPIListener(ctx context.Context, apiName, eventPath string, listener *core.ContractListener) (output *core.ContractListener, err error) { + api, err := cm.database.GetContractAPIByName(ctx, cm.namespace, apiName) if err != nil { return nil, err } else if api == nil || api.Interface == nil { @@ -593,19 +585,19 @@ func (cm *contractManager) AddContractAPIListener(ctx context.Context, ns, apiNa input.Location = api.Location } - return cm.AddContractListener(ctx, ns, input) + return cm.AddContractListener(ctx, input) } -func (cm *contractManager) GetContractListenerByNameOrID(ctx context.Context, ns, nameOrID string) (listener *core.ContractListener, err error) { +func (cm *contractManager) GetContractListenerByNameOrID(ctx context.Context, nameOrID string) (listener *core.ContractListener, err error) { id, err := fftypes.ParseUUID(ctx, nameOrID) if err != nil { if err := core.ValidateFFNameField(ctx, nameOrID, "name"); err != nil { return nil, err } - if listener, err = cm.database.GetContractListener(ctx, ns, nameOrID); err != nil { + if listener, err = cm.database.GetContractListener(ctx, cm.namespace, nameOrID); err != nil { return nil, err } - } else if listener, err = cm.database.GetContractListenerByID(ctx, id); err != nil { + } else if listener, err = cm.database.GetContractListenerByID(ctx, cm.namespace, id); err != nil { return nil, err } if listener == nil { @@ -614,18 +606,18 @@ func (cm *contractManager) GetContractListenerByNameOrID(ctx context.Context, ns return listener, nil } -func (cm *contractManager) GetContractListeners(ctx context.Context, ns string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { - return cm.database.GetContractListeners(ctx, cm.scopeNS(ns, filter)) +func (cm *contractManager) GetContractListeners(ctx context.Context, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { + return cm.database.GetContractListeners(ctx, cm.namespace, filter) } -func (cm *contractManager) GetContractAPIListeners(ctx context.Context, ns string, apiName, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { - api, err := cm.database.GetContractAPIByName(ctx, ns, apiName) +func (cm *contractManager) GetContractAPIListeners(ctx context.Context, apiName, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { + api, err := cm.database.GetContractAPIByName(ctx, cm.namespace, apiName) if err != nil { return nil, nil, err } else if api == nil || api.Interface == nil { return nil, nil, i18n.NewError(ctx, coremsgs.Msg404NotFound) } - event, err := cm.resolveEvent(ctx, ns, api.Interface, eventPath) + event, err := cm.resolveEvent(ctx, api.Interface, eventPath) if err != nil { return nil, nil, err } @@ -640,19 +632,19 @@ func (cm *contractManager) GetContractAPIListeners(ctx context.Context, ns strin if !api.Location.IsNil() { f = fb.And(f, fb.Eq("location", api.Location.Bytes())) } - return cm.database.GetContractListeners(ctx, cm.scopeNS(ns, f)) + return cm.database.GetContractListeners(ctx, cm.namespace, f) } -func (cm *contractManager) DeleteContractListenerByNameOrID(ctx context.Context, ns, nameOrID string) error { +func (cm *contractManager) DeleteContractListenerByNameOrID(ctx context.Context, nameOrID string) error { return cm.database.RunAsGroup(ctx, func(ctx context.Context) (err error) { - listener, err := cm.GetContractListenerByNameOrID(ctx, ns, nameOrID) + listener, err := cm.GetContractListenerByNameOrID(ctx, nameOrID) if err != nil { return err } if err = cm.blockchain.DeleteContractListener(ctx, listener); err != nil { return err } - return cm.database.DeleteContractListenerByID(ctx, listener.ID) + return cm.database.DeleteContractListenerByID(ctx, cm.namespace, listener.ID) }) } @@ -673,8 +665,8 @@ func (cm *contractManager) checkParamSchema(ctx context.Context, input interface return nil } -func (cm *contractManager) GenerateFFI(ctx context.Context, ns string, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) { - generationRequest.Namespace = ns +func (cm *contractManager) GenerateFFI(ctx context.Context, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) { + generationRequest.Namespace = cm.namespace return cm.blockchain.GenerateFFI(ctx, generationRequest) } diff --git a/internal/contracts/manager_test.go b/internal/contracts/manager_test.go index b98a03488..d23b8aefc 100644 --- a/internal/contracts/manager_test.go +++ b/internal/contracts/manager_test.go @@ -589,11 +589,11 @@ func TestAddContractListenerInline(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) mbi.On("AddContractListener", context.Background(), sub).Return(nil) mdi.On("InsertContractListener", context.Background(), &sub.ContractListener).Return(nil) - result, err := cm.AddContractListener(context.Background(), "ns", sub) + result, err := cm.AddContractListener(context.Background(), sub) assert.NoError(t, err) assert.NotNil(t, result.ID) assert.NotNil(t, result.Event) @@ -638,13 +638,13 @@ func TestAddContractListenerByEventPath(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) mbi.On("AddContractListener", context.Background(), sub).Return(nil) mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, sub.EventPath).Return(event, nil) mdi.On("InsertContractListener", context.Background(), &sub.ContractListener).Return(nil) - result, err := cm.AddContractListener(context.Background(), "ns1", sub) + result, err := cm.AddContractListener(context.Background(), sub) assert.NoError(t, err) assert.NotNil(t, result.ID) assert.NotNil(t, result.Event) @@ -672,7 +672,7 @@ func TestAddContractListenerBadLocation(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(nil, fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns1", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -701,7 +701,7 @@ func TestAddContractListenerFFILookupFail(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(nil, fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns1", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -732,7 +732,7 @@ func TestAddContractListenerEventLookupFail(t *testing.T) { mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, sub.EventPath).Return(nil, fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns1", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -763,7 +763,7 @@ func TestAddContractListenerEventLookupNotFound(t *testing.T) { mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, sub.EventPath).Return(nil, nil) - _, err := cm.AddContractListener(context.Background(), "ns1", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF10370", err) mbi.AssertExpectations(t) @@ -785,20 +785,12 @@ func TestAddContractListenerMissingEventOrID(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) - _, err := cm.AddContractListener(context.Background(), "ns2", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF10317", err) mbi.AssertExpectations(t) } -func TestAddContractListenerBadNamespace(t *testing.T) { - cm := newTestContractManager() - sub := &core.ContractListenerInput{} - - _, err := cm.AddContractListener(context.Background(), "!bad", sub) - assert.Regexp(t, "FF00140.*'namespace'", err) -} - func TestAddContractListenerBadName(t *testing.T) { cm := newTestContractManager() sub := &core.ContractListenerInput{ @@ -807,7 +799,7 @@ func TestAddContractListenerBadName(t *testing.T) { }, } - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF00140.*'name'", err) } @@ -817,7 +809,7 @@ func TestAddContractListenerMissingTopic(t *testing.T) { ContractListener: core.ContractListener{}, } - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF00140.*'topic'", err) } @@ -838,9 +830,9 @@ func TestAddContractListenerNameConflict(t *testing.T) { } mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(&core.ContractListener{}, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(&core.ContractListener{}, nil) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF10312", err) mbi.AssertExpectations(t) @@ -864,9 +856,9 @@ func TestAddContractListenerNameError(t *testing.T) { } mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(nil, fmt.Errorf("pop")) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(nil, fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -890,9 +882,9 @@ func TestAddContractListenerTopicConflict(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return([]*core.ContractListener{{}}, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return([]*core.ContractListener{{}}, nil, nil) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "FF10383", err) mbi.AssertExpectations(t) @@ -916,9 +908,9 @@ func TestAddContractListenerTopicError(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, fmt.Errorf("pop")) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -952,9 +944,9 @@ func TestAddContractListenerValidateFail(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.Regexp(t, "does not validate", err) mbi.AssertExpectations(t) @@ -988,10 +980,10 @@ func TestAddContractListenerBlockchainFail(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) mbi.On("AddContractListener", context.Background(), sub).Return(fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -1025,11 +1017,11 @@ func TestAddContractListenerUpsertSubFail(t *testing.T) { mbi.On("NormalizeContractLocation", context.Background(), sub.Location).Return(sub.Location, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) mbi.On("AddContractListener", context.Background(), sub).Return(nil) mdi.On("InsertContractListener", context.Background(), &sub.ContractListener).Return(fmt.Errorf("pop")) - _, err := cm.AddContractListener(context.Background(), "ns", sub) + _, err := cm.AddContractListener(context.Background(), sub) assert.EqualError(t, err, "pop") mbi.AssertExpectations(t) @@ -1059,12 +1051,12 @@ func TestAddContractAPIListener(t *testing.T) { }, } - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(api, nil) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(api, nil) mbi.On("NormalizeContractLocation", context.Background(), api.Location).Return(listener.Location, nil) mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) - mdi.On("GetFFIEvent", context.Background(), "ns", interfaceID, "changed").Return(event, nil) + mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, "changed").Return(event, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) mbi.On("AddContractListener", context.Background(), mock.MatchedBy(func(l *core.ContractListenerInput) bool { return *l.Interface.ID == *interfaceID && l.EventPath == "changed" && l.Topic == "test-topic" })).Return(nil) @@ -1072,7 +1064,7 @@ func TestAddContractAPIListener(t *testing.T) { return *l.Interface.ID == *interfaceID && l.Event.Name == "changed" && l.Topic == "test-topic" })).Return(nil) - _, err := cm.AddContractAPIListener(context.Background(), "ns", "simple", "changed", listener) + _, err := cm.AddContractAPIListener(context.Background(), "simple", "changed", listener) assert.NoError(t, err) mbi.AssertExpectations(t) @@ -1090,9 +1082,9 @@ func TestAddContractAPIListenerNotFound(t *testing.T) { Topic: "test-topic", } - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(nil, nil) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(nil, nil) - _, err := cm.AddContractAPIListener(context.Background(), "ns", "simple", "changed", listener) + _, err := cm.AddContractAPIListener(context.Background(), "simple", "changed", listener) assert.Regexp(t, "FF10109", err) mdi.AssertExpectations(t) @@ -1109,9 +1101,9 @@ func TestAddContractAPIListenerFail(t *testing.T) { Topic: "test-topic", } - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(nil, fmt.Errorf("pop")) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(nil, fmt.Errorf("pop")) - _, err := cm.AddContractAPIListener(context.Background(), "ns", "simple", "changed", listener) + _, err := cm.AddContractAPIListener(context.Background(), "simple", "changed", listener) assert.EqualError(t, err, "pop") mdi.AssertExpectations(t) @@ -1563,9 +1555,9 @@ func TestGetContractListenerByNameOrID(t *testing.T) { mdi := cm.database.(*databasemocks.Plugin) id := fftypes.NewUUID() - mdi.On("GetContractListenerByID", context.Background(), id).Return(&core.ContractListener{}, nil) + mdi.On("GetContractListenerByID", context.Background(), "ns1", id).Return(&core.ContractListener{}, nil) - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", id.String()) + _, err := cm.GetContractListenerByNameOrID(context.Background(), id.String()) assert.NoError(t, err) } @@ -1574,9 +1566,9 @@ func TestGetContractListenerByNameOrIDFail(t *testing.T) { mdi := cm.database.(*databasemocks.Plugin) id := fftypes.NewUUID() - mdi.On("GetContractListenerByID", context.Background(), id).Return(nil, fmt.Errorf("pop")) + mdi.On("GetContractListenerByID", context.Background(), "ns1", id).Return(nil, fmt.Errorf("pop")) - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", id.String()) + _, err := cm.GetContractListenerByNameOrID(context.Background(), id.String()) assert.EqualError(t, err, "pop") } @@ -1584,16 +1576,16 @@ func TestGetContractListenerByName(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(&core.ContractListener{}, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(&core.ContractListener{}, nil) - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", "sub1") + _, err := cm.GetContractListenerByNameOrID(context.Background(), "sub1") assert.NoError(t, err) } func TestGetContractListenerBadName(t *testing.T) { cm := newTestContractManager() - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", "!bad") + _, err := cm.GetContractListenerByNameOrID(context.Background(), "!bad") assert.Regexp(t, "FF00140", err) } @@ -1601,9 +1593,9 @@ func TestGetContractListenerByNameFail(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(nil, fmt.Errorf("pop")) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(nil, fmt.Errorf("pop")) - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", "sub1") + _, err := cm.GetContractListenerByNameOrID(context.Background(), "sub1") assert.EqualError(t, err, "pop") } @@ -1611,9 +1603,9 @@ func TestGetContractListenerNotFound(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(nil, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(nil, nil) - _, err := cm.GetContractListenerByNameOrID(context.Background(), "ns", "sub1") + _, err := cm.GetContractListenerByNameOrID(context.Background(), "sub1") assert.Regexp(t, "FF10109", err) } @@ -1621,10 +1613,10 @@ func TestGetContractListeners(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) f := database.ContractListenerQueryFactory.NewFilter(context.Background()) - _, _, err := cm.GetContractListeners(context.Background(), "ns", f.And()) + _, _, err := cm.GetContractListeners(context.Background(), f.And()) assert.NoError(t, err) } @@ -1648,14 +1640,14 @@ func TestGetContractAPIListeners(t *testing.T) { }, } - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(api, nil) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(api, nil) mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) - mdi.On("GetFFIEvent", context.Background(), "ns", interfaceID, "changed").Return(event, nil) + mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, "changed").Return(event, nil) mbi.On("GenerateEventSignature", context.Background(), mock.Anything).Return("changed") - mdi.On("GetContractListeners", context.Background(), mock.Anything).Return(nil, nil, nil) + mdi.On("GetContractListeners", context.Background(), "ns1", mock.Anything).Return(nil, nil, nil) f := database.ContractListenerQueryFactory.NewFilter(context.Background()) - _, _, err := cm.GetContractAPIListeners(context.Background(), "ns", "simple", "changed", f.And()) + _, _, err := cm.GetContractAPIListeners(context.Background(), "simple", "changed", f.And()) assert.NoError(t, err) mbi.AssertExpectations(t) @@ -1666,10 +1658,10 @@ func TestGetContractAPIListenersNotFound(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(nil, nil) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(nil, nil) f := database.ContractListenerQueryFactory.NewFilter(context.Background()) - _, _, err := cm.GetContractAPIListeners(context.Background(), "ns", "simple", "changed", f.And()) + _, _, err := cm.GetContractAPIListeners(context.Background(), "simple", "changed", f.And()) assert.Regexp(t, "FF10109", err) mdi.AssertExpectations(t) @@ -1679,10 +1671,10 @@ func TestGetContractAPIListenersFail(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(nil, fmt.Errorf("pop")) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(nil, fmt.Errorf("pop")) f := database.ContractListenerQueryFactory.NewFilter(context.Background()) - _, _, err := cm.GetContractAPIListeners(context.Background(), "ns", "simple", "changed", f.And()) + _, _, err := cm.GetContractAPIListeners(context.Background(), "simple", "changed", f.And()) assert.EqualError(t, err, "pop") mdi.AssertExpectations(t) @@ -1702,12 +1694,12 @@ func TestGetContractAPIListenersEventNotFound(t *testing.T) { }.String()), } - mdi.On("GetContractAPIByName", context.Background(), "ns", "simple").Return(api, nil) + mdi.On("GetContractAPIByName", context.Background(), "ns1", "simple").Return(api, nil) mdi.On("GetFFIByID", context.Background(), "ns1", interfaceID).Return(&core.FFI{}, nil) - mdi.On("GetFFIEvent", context.Background(), "ns", interfaceID, "changed").Return(nil, nil) + mdi.On("GetFFIEvent", context.Background(), "ns1", interfaceID, "changed").Return(nil, nil) f := database.ContractListenerQueryFactory.NewFilter(context.Background()) - _, _, err := cm.GetContractAPIListeners(context.Background(), "ns", "simple", "changed", f.And()) + _, _, err := cm.GetContractAPIListeners(context.Background(), "simple", "changed", f.And()) assert.Regexp(t, "FF10370", err) mdi.AssertExpectations(t) @@ -1722,11 +1714,11 @@ func TestDeleteContractListener(t *testing.T) { ID: fftypes.NewUUID(), } - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(sub, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(sub, nil) mbi.On("DeleteContractListener", context.Background(), sub).Return(nil) - mdi.On("DeleteContractListenerByID", context.Background(), sub.ID).Return(nil) + mdi.On("DeleteContractListenerByID", context.Background(), "ns1", sub.ID).Return(nil) - err := cm.DeleteContractListenerByNameOrID(context.Background(), "ns", "sub1") + err := cm.DeleteContractListenerByNameOrID(context.Background(), "sub1") assert.NoError(t, err) } @@ -1739,11 +1731,11 @@ func TestDeleteContractListenerBlockchainFail(t *testing.T) { ID: fftypes.NewUUID(), } - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(sub, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(sub, nil) mbi.On("DeleteContractListener", context.Background(), sub).Return(fmt.Errorf("pop")) - mdi.On("DeleteContractListenerByID", context.Background(), sub.ID).Return(nil) + mdi.On("DeleteContractListenerByID", context.Background(), "ns1", sub.ID).Return(nil) - err := cm.DeleteContractListenerByNameOrID(context.Background(), "ns", "sub1") + err := cm.DeleteContractListenerByNameOrID(context.Background(), "sub1") assert.EqualError(t, err, "pop") } @@ -1751,9 +1743,9 @@ func TestDeleteContractListenerNotFound(t *testing.T) { cm := newTestContractManager() mdi := cm.database.(*databasemocks.Plugin) - mdi.On("GetContractListener", context.Background(), "ns", "sub1").Return(nil, nil) + mdi.On("GetContractListener", context.Background(), "ns1", "sub1").Return(nil, nil) - err := cm.DeleteContractListenerByNameOrID(context.Background(), "ns", "sub1") + err := cm.DeleteContractListenerByNameOrID(context.Background(), "sub1") assert.Regexp(t, "FF10109", err) } @@ -2347,7 +2339,7 @@ func TestGenerateFFI(t *testing.T) { mbi.On("GenerateFFI", mock.Anything, mock.Anything).Return(&core.FFI{ Name: "generated", }, nil) - ffi, err := cm.GenerateFFI(context.Background(), "default", &core.FFIGenerationRequest{}) + ffi, err := cm.GenerateFFI(context.Background(), &core.FFIGenerationRequest{}) assert.NoError(t, err) assert.NotNil(t, ffi) assert.Equal(t, "generated", ffi.Name) diff --git a/internal/database/sqlcommon/contractlisteners_sql.go b/internal/database/sqlcommon/contractlisteners_sql.go index 47e5fd994..3acbb97e5 100644 --- a/internal/database/sqlcommon/contractlisteners_sql.go +++ b/internal/database/sqlcommon/contractlisteners_sql.go @@ -138,22 +138,22 @@ func (s *SQLCommon) getContractListenerPred(ctx context.Context, desc string, pr return sub, nil } -func (s *SQLCommon) GetContractListener(ctx context.Context, ns, name string) (sub *core.ContractListener, err error) { - return s.getContractListenerPred(ctx, fmt.Sprintf("%s:%s", ns, name), sq.Eq{"namespace": ns, "name": name}) +func (s *SQLCommon) GetContractListener(ctx context.Context, namespace, name string) (sub *core.ContractListener, err error) { + return s.getContractListenerPred(ctx, fmt.Sprintf("%s:%s", namespace, name), sq.Eq{"namespace": namespace, "name": name}) } -func (s *SQLCommon) GetContractListenerByID(ctx context.Context, id *fftypes.UUID) (sub *core.ContractListener, err error) { - return s.getContractListenerPred(ctx, id.String(), sq.Eq{"id": id}) +func (s *SQLCommon) GetContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) (sub *core.ContractListener, err error) { + return s.getContractListenerPred(ctx, id.String(), sq.Eq{"id": id, "namespace": namespace}) } -func (s *SQLCommon) GetContractListenerByBackendID(ctx context.Context, id string) (sub *core.ContractListener, err error) { - return s.getContractListenerPred(ctx, id, sq.Eq{"backend_id": id}) +func (s *SQLCommon) GetContractListenerByBackendID(ctx context.Context, namespace, id string) (sub *core.ContractListener, err error) { + return s.getContractListenerPred(ctx, id, sq.Eq{"backend_id": id, "namespace": namespace}) } -func (s *SQLCommon) GetContractListeners(ctx context.Context, filter database.Filter) ([]*core.ContractListener, *database.FilterResult, error) { +func (s *SQLCommon) GetContractListeners(ctx context.Context, namespace string, filter database.Filter) ([]*core.ContractListener, *database.FilterResult, error) { query, fop, fi, err := s.filterSelect(ctx, "", sq.Select(contractListenerColumns...).From(contractlistenersTable), - filter, contractListenerFilterFieldMap, []interface{}{"sequence"}) + filter, contractListenerFilterFieldMap, []interface{}{"sequence"}, sq.Eq{"namespace": namespace}) if err != nil { return nil, nil, err } @@ -176,14 +176,14 @@ func (s *SQLCommon) GetContractListeners(ctx context.Context, filter database.Fi return subs, s.queryRes(ctx, contractlistenersTable, tx, fop, fi), err } -func (s *SQLCommon) DeleteContractListenerByID(ctx context.Context, id *fftypes.UUID) (err error) { +func (s *SQLCommon) DeleteContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) (err error) { ctx, tx, autoCommit, err := s.beginOrUseTx(ctx) if err != nil { return err } defer s.rollbackTx(ctx, tx, autoCommit) - sub, err := s.GetContractListenerByID(ctx, id) + sub, err := s.GetContractListenerByID(ctx, namespace, id) if err == nil && sub != nil { err = s.deleteTx(ctx, contractlistenersTable, tx, sq.Delete(contractlistenersTable).Where(sq.Eq{"id": id}), func() { diff --git a/internal/database/sqlcommon/contractlisteners_sql_test.go b/internal/database/sqlcommon/contractlisteners_sql_test.go index fb4c61d8a..06dfddb95 100644 --- a/internal/database/sqlcommon/contractlisteners_sql_test.go +++ b/internal/database/sqlcommon/contractlisteners_sql_test.go @@ -71,7 +71,7 @@ func TestContractListenerE2EWithDB(t *testing.T) { filter := fb.And( fb.Eq("backendid", sub.BackendID), ) - subs, res, err := s.GetContractListeners(ctx, filter.Count(true)) + subs, res, err := s.GetContractListeners(ctx, "ns", filter.Count(true)) assert.NoError(t, err) assert.Equal(t, 1, len(subs)) assert.Equal(t, int64(1), *res.TotalCount) @@ -85,13 +85,13 @@ func TestContractListenerE2EWithDB(t *testing.T) { assert.Equal(t, string(subJson), string(subReadJson)) // Query back the listener (by ID) - subRead, err = s.GetContractListenerByID(ctx, sub.ID) + subRead, err = s.GetContractListenerByID(ctx, "ns", sub.ID) assert.NoError(t, err) subReadJson, _ = json.Marshal(subRead) assert.Equal(t, string(subJson), string(subReadJson)) // Query back the listener (by protocol ID) - subRead, err = s.GetContractListenerByBackendID(ctx, sub.BackendID) + subRead, err = s.GetContractListenerByBackendID(ctx, "ns", sub.BackendID) assert.NoError(t, err) subReadJson, _ = json.Marshal(subRead) assert.Equal(t, string(subJson), string(subReadJson)) @@ -100,7 +100,7 @@ func TestContractListenerE2EWithDB(t *testing.T) { filter = fb.And( fb.Eq("backendid", sub.BackendID), ) - subs, res, err = s.GetContractListeners(ctx, filter.Count(true)) + subs, res, err = s.GetContractListeners(ctx, "ns", filter.Count(true)) assert.NoError(t, err) assert.Equal(t, 1, len(subs)) assert.Equal(t, int64(1), *res.TotalCount) @@ -108,9 +108,9 @@ func TestContractListenerE2EWithDB(t *testing.T) { assert.Equal(t, string(subJson), string(subReadJson)) // Test delete, and refind no return - err = s.DeleteContractListenerByID(ctx, sub.ID) + err = s.DeleteContractListenerByID(ctx, "ns", sub.ID) assert.NoError(t, err) - subs, _, err = s.GetContractListeners(ctx, filter) + subs, _, err = s.GetContractListeners(ctx, "ns", filter) assert.NoError(t, err) assert.Equal(t, 0, len(subs)) } @@ -146,7 +146,7 @@ func TestUpsertContractListenerFailCommit(t *testing.T) { func TestGetContractListenerByIDSelectFail(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectQuery("SELECT .*").WillReturnError(fmt.Errorf("pop")) - _, err := s.GetContractListenerByID(context.Background(), fftypes.NewUUID()) + _, err := s.GetContractListenerByID(context.Background(), "ns", fftypes.NewUUID()) assert.Regexp(t, "FF10115", err) assert.NoError(t, mock.ExpectationsWereMet()) } @@ -154,7 +154,7 @@ func TestGetContractListenerByIDSelectFail(t *testing.T) { func TestGetContractListenerByIDNotFound(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectQuery("SELECT .*").WillReturnRows(sqlmock.NewRows([]string{"backendid"})) - msg, err := s.GetContractListenerByID(context.Background(), fftypes.NewUUID()) + msg, err := s.GetContractListenerByID(context.Background(), "ns", fftypes.NewUUID()) assert.NoError(t, err) assert.Nil(t, msg) assert.NoError(t, mock.ExpectationsWereMet()) @@ -163,7 +163,7 @@ func TestGetContractListenerByIDNotFound(t *testing.T) { func TestGetContractListenerByIDScanFail(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectQuery("SELECT .*").WillReturnRows(sqlmock.NewRows([]string{"backendid"}).AddRow("only one")) - _, err := s.GetContractListenerByID(context.Background(), fftypes.NewUUID()) + _, err := s.GetContractListenerByID(context.Background(), "ns", fftypes.NewUUID()) assert.Regexp(t, "FF10121", err) assert.NoError(t, mock.ExpectationsWereMet()) } @@ -172,7 +172,7 @@ func TestGetContractListenersQueryFail(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectQuery("SELECT .*").WillReturnError(fmt.Errorf("pop")) f := database.ContractListenerQueryFactory.NewFilter(context.Background()).Eq("backendid", "") - _, _, err := s.GetContractListeners(context.Background(), f) + _, _, err := s.GetContractListeners(context.Background(), "ns", f) assert.Regexp(t, "FF10115", err) assert.NoError(t, mock.ExpectationsWereMet()) } @@ -180,7 +180,7 @@ func TestGetContractListenersQueryFail(t *testing.T) { func TestGetContractListenersBuildQueryFail(t *testing.T) { s, _ := newMockProvider().init() f := database.ContractListenerQueryFactory.NewFilter(context.Background()).Eq("backendid", map[bool]bool{true: false}) - _, _, err := s.GetContractListeners(context.Background(), f) + _, _, err := s.GetContractListeners(context.Background(), "ns", f) assert.Regexp(t, "FF00143.*id", err) } @@ -188,7 +188,7 @@ func TestGetContractListenersScanFail(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectQuery("SELECT .*").WillReturnRows(sqlmock.NewRows([]string{"backendid"}).AddRow("only one")) f := database.ContractListenerQueryFactory.NewFilter(context.Background()).Eq("backendid", "") - _, _, err := s.GetContractListeners(context.Background(), f) + _, _, err := s.GetContractListeners(context.Background(), "ns", f) assert.Regexp(t, "FF10121", err) assert.NoError(t, mock.ExpectationsWereMet()) } @@ -196,7 +196,7 @@ func TestGetContractListenersScanFail(t *testing.T) { func TestContractListenerDeleteBeginFail(t *testing.T) { s, mock := newMockProvider().init() mock.ExpectBegin().WillReturnError(fmt.Errorf("pop")) - err := s.DeleteContractListenerByID(context.Background(), fftypes.NewUUID()) + err := s.DeleteContractListenerByID(context.Background(), "ns", fftypes.NewUUID()) assert.Regexp(t, "FF10114", err) } @@ -207,7 +207,7 @@ func TestContractListenerDeleteFail(t *testing.T) { fftypes.NewUUID(), nil, []byte("{}"), "ns1", "sub1", "123", "{}", "sig", "topic1", nil, fftypes.Now()), ) mock.ExpectExec("DELETE .*").WillReturnError(fmt.Errorf("pop")) - err := s.DeleteContractListenerByID(context.Background(), fftypes.NewUUID()) + err := s.DeleteContractListenerByID(context.Background(), "ns", fftypes.NewUUID()) assert.Regexp(t, "FF10118", err) } @@ -231,7 +231,7 @@ func TestContractListenerOptions(t *testing.T) { err := s.InsertContractListener(ctx, l) assert.NoError(t, err) - li, err := s.GetContractListenerByID(ctx, l.ID) + li, err := s.GetContractListenerByID(ctx, "ns", l.ID) assert.NoError(t, err) assert.Equal(t, l.Options, li.Options) diff --git a/internal/events/batch_pin_complete.go b/internal/events/batch_pin_complete.go index 5c2eeb856..8ff025762 100644 --- a/internal/events/batch_pin_complete.go +++ b/internal/events/batch_pin_complete.go @@ -65,7 +65,7 @@ func (em *eventManager) BatchPinComplete(batchPin *blockchain.BatchPin, signingK ID: batchPin.TransactionID, BlockchainID: batchPin.Event.BlockchainTXID, }) - if err := em.maybePersistBlockchainEvent(ctx, chainEvent); err != nil { + if err := em.maybePersistBlockchainEvent(ctx, chainEvent, nil); err != nil { return err } em.emitBlockchainEventMetric(&batchPin.Event) diff --git a/internal/events/blockchain_event.go b/internal/events/blockchain_event.go index a944fa807..aac2914ce 100644 --- a/internal/events/blockchain_event.go +++ b/internal/events/blockchain_event.go @@ -46,13 +46,7 @@ func buildBlockchainEvent(ns string, subID *fftypes.UUID, event *blockchain.Even func (em *eventManager) getChainListenerByProtocolIDCached(ctx context.Context, protocolID string) (*core.ContractListener, error) { return em.getChainListenerCached(fmt.Sprintf("pid:%s", protocolID), func() (*core.ContractListener, error) { - return em.database.GetContractListenerByBackendID(ctx, protocolID) - }) -} - -func (em *eventManager) getChainListenerByIDCached(ctx context.Context, id *fftypes.UUID) (*core.ContractListener, error) { - return em.getChainListenerCached(fmt.Sprintf("id:%s", id), func() (*core.ContractListener, error) { - return em.database.GetContractListenerByID(ctx, id) + return em.database.GetContractListenerByBackendID(ctx, em.namespace, protocolID) }) } @@ -70,24 +64,20 @@ func (em *eventManager) getChainListenerCached(cacheKey string, getter func() (* return listener, err } -func (em *eventManager) getTopicForChainListener(ctx context.Context, listenerID *fftypes.UUID) (string, error) { - if listenerID == nil { - return core.SystemBatchPinTopic, nil - } - listener, err := em.getChainListenerByIDCached(ctx, listenerID) - if err != nil { - return "", err +func (em *eventManager) getTopicForChainListener(listener *core.ContractListener) string { + if listener == nil { + return core.SystemBatchPinTopic } var topic string if listener != nil && listener.Topic != "" { topic = listener.Topic } else { - topic = listenerID.String() + topic = listener.ID.String() } - return topic, nil + return topic } -func (em *eventManager) maybePersistBlockchainEvent(ctx context.Context, chainEvent *core.BlockchainEvent) error { +func (em *eventManager) maybePersistBlockchainEvent(ctx context.Context, chainEvent *core.BlockchainEvent, listener *core.ContractListener) error { if existing, err := em.database.GetBlockchainEventByProtocolID(ctx, chainEvent.Namespace, chainEvent.Listener, chainEvent.ProtocolID); err != nil { return err } else if existing != nil { @@ -99,10 +89,7 @@ func (em *eventManager) maybePersistBlockchainEvent(ctx context.Context, chainEv if err := em.txHelper.InsertBlockchainEvent(ctx, chainEvent); err != nil { return err } - topic, err := em.getTopicForChainListener(ctx, chainEvent.Listener) - if err != nil { - return err - } + topic := em.getTopicForChainListener(listener) ffEvent := core.NewEvent(core.EventTypeBlockchainEventReceived, chainEvent.Namespace, chainEvent.ID, chainEvent.TX.ID, topic) if err := em.database.InsertEvent(ctx, ffEvent); err != nil { return err @@ -119,23 +106,23 @@ func (em *eventManager) emitBlockchainEventMetric(event *blockchain.Event) { func (em *eventManager) BlockchainEvent(event *blockchain.EventWithSubscription) error { return em.retry.Do(em.ctx, "persist blockchain event", func(attempt int) (bool, error) { err := em.database.RunAsGroup(em.ctx, func(ctx context.Context) error { - sub, err := em.getChainListenerByProtocolIDCached(ctx, event.Subscription) + listener, err := em.getChainListenerByProtocolIDCached(ctx, event.Subscription) if err != nil { return err } - if sub == nil { + if listener == nil { log.L(ctx).Warnf("Event received from unknown subscription %s", event.Subscription) return nil // no retry } - if sub.Namespace != em.namespace { - log.L(em.ctx).Debugf("Ignoring blockchain event from different namespace '%s'", sub.Namespace) + if listener.Namespace != em.namespace { + log.L(em.ctx).Debugf("Ignoring blockchain event from different namespace '%s'", listener.Namespace) return nil } - chainEvent := buildBlockchainEvent(sub.Namespace, sub.ID, &event.Event, &core.BlockchainTransactionRef{ + chainEvent := buildBlockchainEvent(listener.Namespace, listener.ID, &event.Event, &core.BlockchainTransactionRef{ BlockchainID: event.BlockchainTXID, }) - if err := em.maybePersistBlockchainEvent(ctx, chainEvent); err != nil { + if err := em.maybePersistBlockchainEvent(ctx, chainEvent, listener); err != nil { return err } em.emitBlockchainEventMetric(&event.Event) diff --git a/internal/events/blockchain_event_test.go b/internal/events/blockchain_event_test.go index c48f13c62..a9fe04176 100644 --- a/internal/events/blockchain_event_test.go +++ b/internal/events/blockchain_event_test.go @@ -56,8 +56,8 @@ func TestContractEventWithRetries(t *testing.T) { var eventID *fftypes.UUID mdi := em.database.(*databasemocks.Plugin) - mdi.On("GetContractListenerByBackendID", mock.Anything, "sb-1").Return(nil, fmt.Errorf("pop")).Once() - mdi.On("GetContractListenerByBackendID", mock.Anything, "sb-1").Return(sub, nil).Times(1) // cached + mdi.On("GetContractListenerByBackendID", mock.Anything, "ns1", "sb-1").Return(nil, fmt.Errorf("pop")).Once() + mdi.On("GetContractListenerByBackendID", mock.Anything, "ns1", "sb-1").Return(sub, nil).Times(1) // cached mth := em.txHelper.(*txcommonmocks.Helper) mdi.On("GetBlockchainEventByProtocolID", mock.Anything, "ns1", sub.ID, ev.ProtocolID).Return(nil, nil) mth.On("InsertBlockchainEvent", mock.Anything, mock.Anything).Return(fmt.Errorf("pop")).Once() @@ -65,7 +65,6 @@ func TestContractEventWithRetries(t *testing.T) { eventID = e.ID return *e.Listener == *sub.ID && e.Name == "Changed" && e.Namespace == "ns1" })).Return(nil).Times(2) - mdi.On("GetContractListenerByID", mock.Anything, sub.ID).Return(sub, nil) mdi.On("InsertEvent", mock.Anything, mock.Anything).Return(fmt.Errorf("pop")).Once() mdi.On("InsertEvent", mock.Anything, mock.MatchedBy(func(e *core.Event) bool { return e.Type == core.EventTypeBlockchainEventReceived && e.Reference != nil && e.Reference == eventID && e.Topic == "topic1" @@ -97,7 +96,7 @@ func TestContractEventUnknownSubscription(t *testing.T) { } mdi := em.database.(*databasemocks.Plugin) - mdi.On("GetContractListenerByBackendID", mock.Anything, "sb-1").Return(nil, nil) + mdi.On("GetContractListenerByBackendID", mock.Anything, "ns1", "sb-1").Return(nil, nil) err := em.BlockchainEvent(ev) assert.NoError(t, err) @@ -129,7 +128,7 @@ func TestContractEventWrongNS(t *testing.T) { } mdi := em.database.(*databasemocks.Plugin) - mdi.On("GetContractListenerByBackendID", mock.Anything, "sb-1").Return(sub, nil) + mdi.On("GetContractListenerByBackendID", mock.Anything, "ns1", "sb-1").Return(sub, nil) err := em.BlockchainEvent(ev) assert.NoError(t, err) @@ -157,7 +156,7 @@ func TestPersistBlockchainEventDuplicate(t *testing.T) { mdi := em.database.(*databasemocks.Plugin) mdi.On("GetBlockchainEventByProtocolID", mock.Anything, "ns1", ev.Listener, ev.ProtocolID).Return(&core.BlockchainEvent{}, nil) - err := em.maybePersistBlockchainEvent(em.ctx, ev) + err := em.maybePersistBlockchainEvent(em.ctx, ev, nil) assert.NoError(t, err) mdi.AssertExpectations(t) @@ -183,42 +182,12 @@ func TestPersistBlockchainEventLookupFail(t *testing.T) { mdi := em.database.(*databasemocks.Plugin) mdi.On("GetBlockchainEventByProtocolID", mock.Anything, "ns1", ev.Listener, ev.ProtocolID).Return(nil, fmt.Errorf("pop")) - err := em.maybePersistBlockchainEvent(em.ctx, ev) + err := em.maybePersistBlockchainEvent(em.ctx, ev, nil) assert.EqualError(t, err, "pop") mdi.AssertExpectations(t) } -func TestPersistBlockchainEventChainListenerLookupFail(t *testing.T) { - em, cancel := newTestEventManager(t) - defer cancel() - - ev := &core.BlockchainEvent{ - Name: "Changed", - Namespace: "ns1", - ProtocolID: "10/20/30", - Output: fftypes.JSONObject{ - "value": "1", - }, - Info: fftypes.JSONObject{ - "blockNumber": "10", - }, - Listener: fftypes.NewUUID(), - } - - mdi := em.database.(*databasemocks.Plugin) - mth := em.txHelper.(*txcommonmocks.Helper) - mdi.On("GetBlockchainEventByProtocolID", mock.Anything, "ns1", ev.Listener, ev.ProtocolID).Return(nil, nil) - mth.On("InsertBlockchainEvent", mock.Anything, mock.Anything).Return(nil) - mdi.On("GetContractListenerByID", mock.Anything, ev.Listener).Return(nil, fmt.Errorf("pop")) - - err := em.maybePersistBlockchainEvent(em.ctx, ev) - assert.Regexp(t, "pop", err) - - mdi.AssertExpectations(t) - mth.AssertExpectations(t) -} - func TestGetTopicForChainListenerFallback(t *testing.T) { em, cancel := newTestEventManager(t) defer cancel() @@ -229,14 +198,8 @@ func TestGetTopicForChainListenerFallback(t *testing.T) { Topic: "", } - mdi := em.database.(*databasemocks.Plugin) - mdi.On("GetContractListenerByID", mock.Anything, mock.Anything).Return(sub, nil) - - topic, err := em.getTopicForChainListener(em.ctx, sub.ID) - assert.NoError(t, err) + topic := em.getTopicForChainListener(sub) assert.Equal(t, sub.ID.String(), topic) - - mdi.AssertExpectations(t) } func TestBlockchainEventMetric(t *testing.T) { diff --git a/internal/events/network_action.go b/internal/events/network_action.go index e3006c5ed..8337a48c8 100644 --- a/internal/events/network_action.go +++ b/internal/events/network_action.go @@ -69,7 +69,7 @@ func (em *eventManager) BlockchainNetworkAction(action string, event *blockchain chainEvent := buildBlockchainEvent(core.LegacySystemNamespace, nil, event, &core.BlockchainTransactionRef{ BlockchainID: event.BlockchainTXID, }) - err = em.maybePersistBlockchainEvent(em.ctx, chainEvent) + err = em.maybePersistBlockchainEvent(em.ctx, chainEvent, nil) } return true, err }) diff --git a/internal/events/token_pool_created.go b/internal/events/token_pool_created.go index 911127d3d..2ba21c771 100644 --- a/internal/events/token_pool_created.go +++ b/internal/events/token_pool_created.go @@ -57,7 +57,7 @@ func (em *eventManager) confirmPool(ctx context.Context, pool *core.TokenPool, e Type: pool.TX.Type, BlockchainID: ev.BlockchainTXID, }) - if err := em.maybePersistBlockchainEvent(ctx, chainEvent); err != nil { + if err := em.maybePersistBlockchainEvent(ctx, chainEvent, nil); err != nil { return err } em.emitBlockchainEventMetric(ev) diff --git a/internal/events/tokens_approved.go b/internal/events/tokens_approved.go index 059d91c31..185eac364 100644 --- a/internal/events/tokens_approved.go +++ b/internal/events/tokens_approved.go @@ -109,7 +109,7 @@ func (em *eventManager) persistTokenApproval(ctx context.Context, approval *toke Type: approval.TX.Type, BlockchainID: approval.Event.BlockchainTXID, }) - if err := em.maybePersistBlockchainEvent(ctx, chainEvent); err != nil { + if err := em.maybePersistBlockchainEvent(ctx, chainEvent, nil); err != nil { return false, err } em.emitBlockchainEventMetric(&approval.Event) diff --git a/internal/events/tokens_transferred.go b/internal/events/tokens_transferred.go index 36bc33e8e..070d05d29 100644 --- a/internal/events/tokens_transferred.go +++ b/internal/events/tokens_transferred.go @@ -109,7 +109,7 @@ func (em *eventManager) persistTokenTransfer(ctx context.Context, transfer *toke Type: transfer.TX.Type, BlockchainID: transfer.Event.BlockchainTXID, }) - if err := em.maybePersistBlockchainEvent(ctx, chainEvent); err != nil { + if err := em.maybePersistBlockchainEvent(ctx, chainEvent, nil); err != nil { return false, err } em.emitBlockchainEventMetric(&transfer.Event) diff --git a/mocks/contractmocks/manager.go b/mocks/contractmocks/manager.go index d3bb9803f..5a65fedde 100644 --- a/mocks/contractmocks/manager.go +++ b/mocks/contractmocks/manager.go @@ -19,13 +19,13 @@ type Manager struct { mock.Mock } -// AddContractAPIListener provides a mock function with given fields: ctx, ns, apiName, eventPath, listener -func (_m *Manager) AddContractAPIListener(ctx context.Context, ns string, apiName string, eventPath string, listener *core.ContractListener) (*core.ContractListener, error) { - ret := _m.Called(ctx, ns, apiName, eventPath, listener) +// AddContractAPIListener provides a mock function with given fields: ctx, apiName, eventPath, listener +func (_m *Manager) AddContractAPIListener(ctx context.Context, apiName string, eventPath string, listener *core.ContractListener) (*core.ContractListener, error) { + ret := _m.Called(ctx, apiName, eventPath, listener) var r0 *core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, *core.ContractListener) *core.ContractListener); ok { - r0 = rf(ctx, ns, apiName, eventPath, listener) + if rf, ok := ret.Get(0).(func(context.Context, string, string, *core.ContractListener) *core.ContractListener); ok { + r0 = rf(ctx, apiName, eventPath, listener) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.ContractListener) @@ -33,8 +33,8 @@ func (_m *Manager) AddContractAPIListener(ctx context.Context, ns string, apiNam } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, string, string, *core.ContractListener) error); ok { - r1 = rf(ctx, ns, apiName, eventPath, listener) + if rf, ok := ret.Get(1).(func(context.Context, string, string, *core.ContractListener) error); ok { + r1 = rf(ctx, apiName, eventPath, listener) } else { r1 = ret.Error(1) } @@ -42,13 +42,13 @@ func (_m *Manager) AddContractAPIListener(ctx context.Context, ns string, apiNam return r0, r1 } -// AddContractListener provides a mock function with given fields: ctx, ns, listener -func (_m *Manager) AddContractListener(ctx context.Context, ns string, listener *core.ContractListenerInput) (*core.ContractListener, error) { - ret := _m.Called(ctx, ns, listener) +// AddContractListener provides a mock function with given fields: ctx, listener +func (_m *Manager) AddContractListener(ctx context.Context, listener *core.ContractListenerInput) (*core.ContractListener, error) { + ret := _m.Called(ctx, listener) var r0 *core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string, *core.ContractListenerInput) *core.ContractListener); ok { - r0 = rf(ctx, ns, listener) + if rf, ok := ret.Get(0).(func(context.Context, *core.ContractListenerInput) *core.ContractListener); ok { + r0 = rf(ctx, listener) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.ContractListener) @@ -56,8 +56,8 @@ func (_m *Manager) AddContractListener(ctx context.Context, ns string, listener } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, *core.ContractListenerInput) error); ok { - r1 = rf(ctx, ns, listener) + if rf, ok := ret.Get(1).(func(context.Context, *core.ContractListenerInput) error); ok { + r1 = rf(ctx, listener) } else { r1 = ret.Error(1) } @@ -111,13 +111,13 @@ func (_m *Manager) BroadcastFFI(ctx context.Context, ffi *core.FFI, waitConfirm return r0, r1 } -// DeleteContractListenerByNameOrID provides a mock function with given fields: ctx, ns, nameOrID -func (_m *Manager) DeleteContractListenerByNameOrID(ctx context.Context, ns string, nameOrID string) error { - ret := _m.Called(ctx, ns, nameOrID) +// DeleteContractListenerByNameOrID provides a mock function with given fields: ctx, nameOrID +func (_m *Manager) DeleteContractListenerByNameOrID(ctx context.Context, nameOrID string) error { + ret := _m.Called(ctx, nameOrID) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) error); ok { - r0 = rf(ctx, ns, nameOrID) + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, nameOrID) } else { r0 = ret.Error(0) } @@ -125,13 +125,13 @@ func (_m *Manager) DeleteContractListenerByNameOrID(ctx context.Context, ns stri return r0 } -// GenerateFFI provides a mock function with given fields: ctx, ns, generationRequest -func (_m *Manager) GenerateFFI(ctx context.Context, ns string, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) { - ret := _m.Called(ctx, ns, generationRequest) +// GenerateFFI provides a mock function with given fields: ctx, generationRequest +func (_m *Manager) GenerateFFI(ctx context.Context, generationRequest *core.FFIGenerationRequest) (*core.FFI, error) { + ret := _m.Called(ctx, generationRequest) var r0 *core.FFI - if rf, ok := ret.Get(0).(func(context.Context, string, *core.FFIGenerationRequest) *core.FFI); ok { - r0 = rf(ctx, ns, generationRequest) + if rf, ok := ret.Get(0).(func(context.Context, *core.FFIGenerationRequest) *core.FFI); ok { + r0 = rf(ctx, generationRequest) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.FFI) @@ -139,8 +139,8 @@ func (_m *Manager) GenerateFFI(ctx context.Context, ns string, generationRequest } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, *core.FFIGenerationRequest) error); ok { - r1 = rf(ctx, ns, generationRequest) + if rf, ok := ret.Get(1).(func(context.Context, *core.FFIGenerationRequest) error); ok { + r1 = rf(ctx, generationRequest) } else { r1 = ret.Error(1) } @@ -194,13 +194,13 @@ func (_m *Manager) GetContractAPIInterface(ctx context.Context, apiName string) return r0, r1 } -// GetContractAPIListeners provides a mock function with given fields: ctx, ns, apiName, eventPath, filter -func (_m *Manager) GetContractAPIListeners(ctx context.Context, ns string, apiName string, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { - ret := _m.Called(ctx, ns, apiName, eventPath, filter) +// GetContractAPIListeners provides a mock function with given fields: ctx, apiName, eventPath, filter +func (_m *Manager) GetContractAPIListeners(ctx context.Context, apiName string, eventPath string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { + ret := _m.Called(ctx, apiName, eventPath, filter) var r0 []*core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, database.AndFilter) []*core.ContractListener); ok { - r0 = rf(ctx, ns, apiName, eventPath, filter) + if rf, ok := ret.Get(0).(func(context.Context, string, string, database.AndFilter) []*core.ContractListener); ok { + r0 = rf(ctx, apiName, eventPath, filter) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*core.ContractListener) @@ -208,8 +208,8 @@ func (_m *Manager) GetContractAPIListeners(ctx context.Context, ns string, apiNa } var r1 *database.FilterResult - if rf, ok := ret.Get(1).(func(context.Context, string, string, string, database.AndFilter) *database.FilterResult); ok { - r1 = rf(ctx, ns, apiName, eventPath, filter) + if rf, ok := ret.Get(1).(func(context.Context, string, string, database.AndFilter) *database.FilterResult); ok { + r1 = rf(ctx, apiName, eventPath, filter) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(*database.FilterResult) @@ -217,8 +217,8 @@ func (_m *Manager) GetContractAPIListeners(ctx context.Context, ns string, apiNa } var r2 error - if rf, ok := ret.Get(2).(func(context.Context, string, string, string, database.AndFilter) error); ok { - r2 = rf(ctx, ns, apiName, eventPath, filter) + if rf, ok := ret.Get(2).(func(context.Context, string, string, database.AndFilter) error); ok { + r2 = rf(ctx, apiName, eventPath, filter) } else { r2 = ret.Error(2) } @@ -258,13 +258,13 @@ func (_m *Manager) GetContractAPIs(ctx context.Context, httpServerURL string, fi return r0, r1, r2 } -// GetContractListenerByNameOrID provides a mock function with given fields: ctx, ns, nameOrID -func (_m *Manager) GetContractListenerByNameOrID(ctx context.Context, ns string, nameOrID string) (*core.ContractListener, error) { - ret := _m.Called(ctx, ns, nameOrID) +// GetContractListenerByNameOrID provides a mock function with given fields: ctx, nameOrID +func (_m *Manager) GetContractListenerByNameOrID(ctx context.Context, nameOrID string) (*core.ContractListener, error) { + ret := _m.Called(ctx, nameOrID) var r0 *core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string, string) *core.ContractListener); ok { - r0 = rf(ctx, ns, nameOrID) + if rf, ok := ret.Get(0).(func(context.Context, string) *core.ContractListener); ok { + r0 = rf(ctx, nameOrID) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.ContractListener) @@ -272,8 +272,8 @@ func (_m *Manager) GetContractListenerByNameOrID(ctx context.Context, ns string, } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok { - r1 = rf(ctx, ns, nameOrID) + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, nameOrID) } else { r1 = ret.Error(1) } @@ -281,13 +281,13 @@ func (_m *Manager) GetContractListenerByNameOrID(ctx context.Context, ns string, return r0, r1 } -// GetContractListeners provides a mock function with given fields: ctx, ns, filter -func (_m *Manager) GetContractListeners(ctx context.Context, ns string, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { - ret := _m.Called(ctx, ns, filter) +// GetContractListeners provides a mock function with given fields: ctx, filter +func (_m *Manager) GetContractListeners(ctx context.Context, filter database.AndFilter) ([]*core.ContractListener, *database.FilterResult, error) { + ret := _m.Called(ctx, filter) var r0 []*core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string, database.AndFilter) []*core.ContractListener); ok { - r0 = rf(ctx, ns, filter) + if rf, ok := ret.Get(0).(func(context.Context, database.AndFilter) []*core.ContractListener); ok { + r0 = rf(ctx, filter) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*core.ContractListener) @@ -295,8 +295,8 @@ func (_m *Manager) GetContractListeners(ctx context.Context, ns string, filter d } var r1 *database.FilterResult - if rf, ok := ret.Get(1).(func(context.Context, string, database.AndFilter) *database.FilterResult); ok { - r1 = rf(ctx, ns, filter) + if rf, ok := ret.Get(1).(func(context.Context, database.AndFilter) *database.FilterResult); ok { + r1 = rf(ctx, filter) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(*database.FilterResult) @@ -304,8 +304,8 @@ func (_m *Manager) GetContractListeners(ctx context.Context, ns string, filter d } var r2 error - if rf, ok := ret.Get(2).(func(context.Context, string, database.AndFilter) error); ok { - r2 = rf(ctx, ns, filter) + if rf, ok := ret.Get(2).(func(context.Context, database.AndFilter) error); ok { + r2 = rf(ctx, filter) } else { r2 = ret.Error(2) } diff --git a/mocks/databasemocks/plugin.go b/mocks/databasemocks/plugin.go index cbc06e45e..1d81a0d04 100644 --- a/mocks/databasemocks/plugin.go +++ b/mocks/databasemocks/plugin.go @@ -51,13 +51,13 @@ func (_m *Plugin) DeleteBlob(ctx context.Context, sequence int64) error { return r0 } -// DeleteContractListenerByID provides a mock function with given fields: ctx, id -func (_m *Plugin) DeleteContractListenerByID(ctx context.Context, id *fftypes.UUID) error { - ret := _m.Called(ctx, id) +// DeleteContractListenerByID provides a mock function with given fields: ctx, namespace, id +func (_m *Plugin) DeleteContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) error { + ret := _m.Called(ctx, namespace, id) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, *fftypes.UUID) error); ok { - r0 = rf(ctx, id) + if rf, ok := ret.Get(0).(func(context.Context, string, *fftypes.UUID) error); ok { + r0 = rf(ctx, namespace, id) } else { r0 = ret.Error(0) } @@ -493,13 +493,13 @@ func (_m *Plugin) GetContractListener(ctx context.Context, namespace string, nam return r0, r1 } -// GetContractListenerByBackendID provides a mock function with given fields: ctx, id -func (_m *Plugin) GetContractListenerByBackendID(ctx context.Context, id string) (*core.ContractListener, error) { - ret := _m.Called(ctx, id) +// GetContractListenerByBackendID provides a mock function with given fields: ctx, namespace, id +func (_m *Plugin) GetContractListenerByBackendID(ctx context.Context, namespace string, id string) (*core.ContractListener, error) { + ret := _m.Called(ctx, namespace, id) var r0 *core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, string) *core.ContractListener); ok { - r0 = rf(ctx, id) + if rf, ok := ret.Get(0).(func(context.Context, string, string) *core.ContractListener); ok { + r0 = rf(ctx, namespace, id) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.ContractListener) @@ -507,8 +507,8 @@ func (_m *Plugin) GetContractListenerByBackendID(ctx context.Context, id string) } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { - r1 = rf(ctx, id) + if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok { + r1 = rf(ctx, namespace, id) } else { r1 = ret.Error(1) } @@ -516,13 +516,13 @@ func (_m *Plugin) GetContractListenerByBackendID(ctx context.Context, id string) return r0, r1 } -// GetContractListenerByID provides a mock function with given fields: ctx, id -func (_m *Plugin) GetContractListenerByID(ctx context.Context, id *fftypes.UUID) (*core.ContractListener, error) { - ret := _m.Called(ctx, id) +// GetContractListenerByID provides a mock function with given fields: ctx, namespace, id +func (_m *Plugin) GetContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) (*core.ContractListener, error) { + ret := _m.Called(ctx, namespace, id) var r0 *core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, *fftypes.UUID) *core.ContractListener); ok { - r0 = rf(ctx, id) + if rf, ok := ret.Get(0).(func(context.Context, string, *fftypes.UUID) *core.ContractListener); ok { + r0 = rf(ctx, namespace, id) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*core.ContractListener) @@ -530,8 +530,8 @@ func (_m *Plugin) GetContractListenerByID(ctx context.Context, id *fftypes.UUID) } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, *fftypes.UUID) error); ok { - r1 = rf(ctx, id) + if rf, ok := ret.Get(1).(func(context.Context, string, *fftypes.UUID) error); ok { + r1 = rf(ctx, namespace, id) } else { r1 = ret.Error(1) } @@ -539,13 +539,13 @@ func (_m *Plugin) GetContractListenerByID(ctx context.Context, id *fftypes.UUID) return r0, r1 } -// GetContractListeners provides a mock function with given fields: ctx, filter -func (_m *Plugin) GetContractListeners(ctx context.Context, filter database.Filter) ([]*core.ContractListener, *database.FilterResult, error) { - ret := _m.Called(ctx, filter) +// GetContractListeners provides a mock function with given fields: ctx, namespace, filter +func (_m *Plugin) GetContractListeners(ctx context.Context, namespace string, filter database.Filter) ([]*core.ContractListener, *database.FilterResult, error) { + ret := _m.Called(ctx, namespace, filter) var r0 []*core.ContractListener - if rf, ok := ret.Get(0).(func(context.Context, database.Filter) []*core.ContractListener); ok { - r0 = rf(ctx, filter) + if rf, ok := ret.Get(0).(func(context.Context, string, database.Filter) []*core.ContractListener); ok { + r0 = rf(ctx, namespace, filter) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*core.ContractListener) @@ -553,8 +553,8 @@ func (_m *Plugin) GetContractListeners(ctx context.Context, filter database.Filt } var r1 *database.FilterResult - if rf, ok := ret.Get(1).(func(context.Context, database.Filter) *database.FilterResult); ok { - r1 = rf(ctx, filter) + if rf, ok := ret.Get(1).(func(context.Context, string, database.Filter) *database.FilterResult); ok { + r1 = rf(ctx, namespace, filter) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(*database.FilterResult) @@ -562,8 +562,8 @@ func (_m *Plugin) GetContractListeners(ctx context.Context, filter database.Filt } var r2 error - if rf, ok := ret.Get(2).(func(context.Context, database.Filter) error); ok { - r2 = rf(ctx, filter) + if rf, ok := ret.Get(2).(func(context.Context, string, database.Filter) error); ok { + r2 = rf(ctx, namespace, filter) } else { r2 = ret.Error(2) } diff --git a/pkg/database/plugin.go b/pkg/database/plugin.go index 789e2f63a..61e5be8b3 100644 --- a/pkg/database/plugin.go +++ b/pkg/database/plugin.go @@ -472,23 +472,23 @@ type iContractAPICollection interface { } type iContractListenerCollection interface { - // InsertContractListener - upsert a subscription to an external smart contract + // InsertContractListener - upsert a listener to an external smart contract InsertContractListener(ctx context.Context, sub *core.ContractListener) (err error) - // GetContractListener - get smart contract subscription by name + // GetContractListener - get contract listener by name GetContractListener(ctx context.Context, namespace, name string) (sub *core.ContractListener, err error) - // GetContractListenerByID - get smart contract subscription by ID - GetContractListenerByID(ctx context.Context, id *fftypes.UUID) (sub *core.ContractListener, err error) + // GetContractListenerByID - get contract listener by ID + GetContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) (sub *core.ContractListener, err error) - // GetContractListenerByBackendID - get smart contract subscription by backend ID - GetContractListenerByBackendID(ctx context.Context, id string) (sub *core.ContractListener, err error) + // GetContractListenerByBackendID - get contract listener by backend ID + GetContractListenerByBackendID(ctx context.Context, namespace, id string) (sub *core.ContractListener, err error) - // GetContractListeners - get smart contract subscriptions - GetContractListeners(ctx context.Context, filter Filter) ([]*core.ContractListener, *FilterResult, error) + // GetContractListeners - get contract listeners + GetContractListeners(ctx context.Context, namespace string, filter Filter) ([]*core.ContractListener, *FilterResult, error) - // DeleteContractListener - delete a subscription to an external smart contract - DeleteContractListenerByID(ctx context.Context, id *fftypes.UUID) (err error) + // DeleteContractListener - delete a contract listener + DeleteContractListenerByID(ctx context.Context, namespace string, id *fftypes.UUID) (err error) } type iBlockchainEventCollection interface {