Skip to content

Commit

Permalink
Add namespace to contract listener database queries
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Jun 22, 2022
1 parent adc2e48 commit 8ab22b3
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 318 deletions.
2 changes: 1 addition & 1 deletion internal/apiserver/route_delete_contract_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_delete_contract_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_contract_api_listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_contract_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_contract_listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_post_contract_api_listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_post_new_contract_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
72 changes: 32 additions & 40 deletions internal/contracts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
})
}

Expand All @@ -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)
}

Expand Down
Loading

0 comments on commit 8ab22b3

Please sign in to comment.