Skip to content

Commit

Permalink
chore!: rename algo to function (#139)
Browse files Browse the repository at this point in the history
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
  • Loading branch information
ThibaultFy authored Feb 10, 2023
1 parent 6e64433 commit 3151513
Show file tree
Hide file tree
Showing 85 changed files with 2,167 additions and 2,096 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]()

### Changed

- BREAKING: rename Algo to Function ([#139](https://github.com/Substra/orchestrator/pull/139))

## [0.32](https://github.com/Substra/orchestrator/releases/tag/0.32.0) - 2023-01-31

### Added
Expand Down
6 changes: 3 additions & 3 deletions chaincode/communication/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
)

func TestWrapUnwrap(t *testing.T) {
msg := &asset.NewAlgo{Key: "uuid"}
msg := &asset.NewFunction{Key: "uuid"}

wrapped, err := Wrap(context.Background(), msg)
assert.NoError(t, err)

out := new(asset.NewAlgo)
out := new(asset.NewFunction)
err = wrapped.Unwrap(out)
assert.NoError(t, err)
assert.Equal(t, msg, out)

serialized, err := json.Marshal(wrapped)
assert.NoError(t, err)

out = new(asset.NewAlgo)
out = new(asset.NewFunction)
err = Unwrap(serialized, out)
assert.NoError(t, err)
assert.Equal(t, msg, out)
Expand Down
6 changes: 3 additions & 3 deletions chaincode/contracts/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestIsEvaluateMethod(t *testing.T) {
p := NewContractCollection()
assert.True(t, p.IsEvaluateMethod("orchestrator.algo:QueryAlgos"))
assert.False(t, p.IsEvaluateMethod("orchestrator.algo:RegisterAlgo"))
assert.False(t, p.IsEvaluateMethod("orchestrator.algo:DoesntExist"))
assert.True(t, p.IsEvaluateMethod("orchestrator.function:QueryFunctions"))
assert.False(t, p.IsEvaluateMethod("orchestrator.function:RegisterFunction"))
assert.False(t, p.IsEvaluateMethod("orchestrator.function:DoesntExist"))
}
4 changes: 2 additions & 2 deletions chaincode/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package contracts

import (
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"github.com/substra/orchestrator/chaincode/algo"
"github.com/substra/orchestrator/chaincode/computeplan"
"github.com/substra/orchestrator/chaincode/computetask"
"github.com/substra/orchestrator/chaincode/datamanager"
"github.com/substra/orchestrator/chaincode/datasample"
"github.com/substra/orchestrator/chaincode/dataset"
"github.com/substra/orchestrator/chaincode/event"
"github.com/substra/orchestrator/chaincode/failurereport"
"github.com/substra/orchestrator/chaincode/function"
"github.com/substra/orchestrator/chaincode/info"
"github.com/substra/orchestrator/chaincode/model"
"github.com/substra/orchestrator/chaincode/organization"
Expand All @@ -20,7 +20,7 @@ import (
var AllContracts []contractapi.ContractInterface = []contractapi.ContractInterface{
organization.NewSmartContract(),
datasample.NewSmartContract(),
algo.NewSmartContract(),
function.NewSmartContract(),
datamanager.NewSmartContract(),
dataset.NewSmartContract(),
computetask.NewSmartContract(),
Expand Down
62 changes: 31 additions & 31 deletions chaincode/algo/contract.go → chaincode/function/contract.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package algo
package function

import (
"github.com/hyperledger/fabric-contract-api-go/contractapi"
Expand All @@ -11,7 +11,7 @@ import (
commonserv "github.com/substra/orchestrator/server/common"
)

// SmartContract manages algos
// SmartContract manages functions
type SmartContract struct {
contractapi.Contract
logger zerolog.Logger
Expand All @@ -20,7 +20,7 @@ type SmartContract struct {
// NewSmartContract creates a smart contract to be used in a chaincode
func NewSmartContract() *SmartContract {
contract := &SmartContract{}
contract.Name = "orchestrator.algo"
contract.Name = "orchestrator.function"
contract.TransactionContextHandler = ledger.NewContext()
contract.BeforeTransaction = ledger.GetBeforeTransactionHook(contract)
contract.AfterTransaction = ledger.AfterTransactionHook
Expand All @@ -30,16 +30,16 @@ func NewSmartContract() *SmartContract {
return contract
}

// RegisterAlgo creates a new algo in world state
// RegisterFunction creates a new function in world state
// If the key exists, it will override the existing value with the new one
func (s *SmartContract) RegisterAlgo(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
func (s *SmartContract) RegisterFunction(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
provider, err := ctx.GetProvider()
if err != nil {
return nil, err
}
service := provider.GetAlgoService()
service := provider.GetFunctionService()

params := new(asset.NewAlgo)
params := new(asset.NewFunction)
err = wrapper.Unwrap(params)
if err != nil {
s.logger.Error().Err(err).Msg("failed to unwrap param")
Expand All @@ -52,9 +52,9 @@ func (s *SmartContract) RegisterAlgo(ctx ledger.TransactionContext, wrapper *com
return nil, err
}

a, err := service.RegisterAlgo(params, owner)
a, err := service.RegisterFunction(params, owner)
if err != nil {
s.logger.Error().Err(err).Msg("failed to register algo")
s.logger.Error().Err(err).Msg("failed to register function")
return nil, err
}
response, err := communication.Wrap(ctx.GetContext(), a)
Expand All @@ -65,58 +65,58 @@ func (s *SmartContract) RegisterAlgo(ctx ledger.TransactionContext, wrapper *com
return response, nil
}

// GetAlgo returns the algo with given key
func (s *SmartContract) GetAlgo(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
// GetFunction returns the function with given key
func (s *SmartContract) GetFunction(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
provider, err := ctx.GetProvider()
if err != nil {
return nil, err
}
service := provider.GetAlgoService()
service := provider.GetFunctionService()

params := new(asset.GetAlgoParam)
params := new(asset.GetFunctionParam)
err = wrapper.Unwrap(params)
if err != nil {
s.logger.Error().Err(err).Msg("failed to unwrap param")
return nil, err
}

algo, err := service.GetAlgo(params.GetKey())
function, err := service.GetFunction(params.GetKey())
if err != nil {
s.logger.Error().Err(err).Msg("failed to query algo")
s.logger.Error().Err(err).Msg("failed to query function")
return nil, err
}

wrapped, err := communication.Wrap(ctx.GetContext(), algo)
wrapped, err := communication.Wrap(ctx.GetContext(), function)
if err != nil {
s.logger.Error().Err(err).Msg("failed to wrap response")
return nil, err
}
return wrapped, nil
}

// QueryAlgos returns the algos
func (s *SmartContract) QueryAlgos(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
// QueryFunctions returns the functions
func (s *SmartContract) QueryFunctions(ctx ledger.TransactionContext, wrapper *communication.Wrapper) (*communication.Wrapper, error) {
provider, err := ctx.GetProvider()
if err != nil {
return nil, err
}
service := provider.GetAlgoService()
service := provider.GetFunctionService()

params := new(asset.QueryAlgosParam)
params := new(asset.QueryFunctionsParam)
err = wrapper.Unwrap(params)
if err != nil {
s.logger.Error().Err(err).Msg("failed to unwrap param")
return nil, err
}

algos, nextPage, err := service.QueryAlgos(&common.Pagination{Token: params.GetPageToken(), Size: params.GetPageSize()}, params.Filter)
functions, nextPage, err := service.QueryFunctions(&common.Pagination{Token: params.GetPageToken(), Size: params.GetPageSize()}, params.Filter)
if err != nil {
s.logger.Error().Err(err).Msg("failed to query algos")
s.logger.Error().Err(err).Msg("failed to query functions")
return nil, err
}

resp := &asset.QueryAlgosResponse{
Algos: algos,
resp := &asset.QueryFunctionsResponse{
Functions: functions,
NextPageToken: nextPage,
}

Expand All @@ -128,16 +128,16 @@ func (s *SmartContract) QueryAlgos(ctx ledger.TransactionContext, wrapper *commu
return wrapped, nil
}

// UpdateAlgo updates an algo in world state
// UpdateFunction updates an function in world state
// If the key does not exist, it will throw an error
func (s *SmartContract) UpdateAlgo(ctx ledger.TransactionContext, wrapper *communication.Wrapper) error {
func (s *SmartContract) UpdateFunction(ctx ledger.TransactionContext, wrapper *communication.Wrapper) error {
provider, err := ctx.GetProvider()
if err != nil {
return err
}
service := provider.GetAlgoService()
service := provider.GetFunctionService()

params := new(asset.UpdateAlgoParam)
params := new(asset.UpdateFunctionParam)
err = wrapper.Unwrap(params)
if err != nil {
s.logger.Error().Err(err).Msg("failed to unwrap param")
Expand All @@ -150,9 +150,9 @@ func (s *SmartContract) UpdateAlgo(ctx ledger.TransactionContext, wrapper *commu
return err
}

err = service.UpdateAlgo(params, requester)
err = service.UpdateFunction(params, requester)
if err != nil {
s.logger.Error().Err(err).Msg("failed to update algo")
s.logger.Error().Err(err).Msg("failed to update function")
return err
}

Expand All @@ -161,5 +161,5 @@ func (s *SmartContract) UpdateAlgo(ctx ledger.TransactionContext, wrapper *commu

// GetEvaluateTransactions returns functions of SmartContract not to be tagged as submit
func (s *SmartContract) GetEvaluateTransactions() []string {
return commonserv.ReadOnlyMethods["Algo"]
return commonserv.ReadOnlyMethods["Function"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package algo
package function

import (
"context"
Expand All @@ -15,11 +15,11 @@ import (
)

// getMockedService returns a service mocks and make sure the provider returns the mock as well.
func getMockedService(ctx *ledger.MockTransactionContext) *service.MockAlgoAPI {
mockService := new(service.MockAlgoAPI)
func getMockedService(ctx *ledger.MockTransactionContext) *service.MockFunctionAPI {
mockService := new(service.MockFunctionAPI)

provider := new(service.MockDependenciesProvider)
provider.On("GetAlgoService").Return(mockService).Once()
provider.On("GetFunctionService").Return(mockService).Once()

ctx.On("GetProvider").Return(provider, nil).Once()
ctx.On("GetContext").Return(context.Background())
Expand All @@ -36,94 +36,94 @@ func TestRegistration(t *testing.T) {

mspid := "org"

newObj := &asset.NewAlgo{
newObj := &asset.NewFunction{
Key: "uuid1",
Name: "Algo name",
Name: "Function name",
Description: addressable,
Algorithm: addressable,
Function: addressable,
Metadata: metadata,
NewPermissions: newPerms,
}

params, err := communication.Wrap(context.Background(), newObj)
assert.NoError(t, err)

a := &asset.Algo{}
a := &asset.Function{}

ctx := new(ledger.MockTransactionContext)

service := getMockedService(ctx)
service.On("RegisterAlgo", newObj, mspid).Return(a, nil).Once()
service.On("RegisterFunction", newObj, mspid).Return(a, nil).Once()

stub := new(testHelper.MockedStub)
ctx.On("GetStub").Return(stub).Once()

stub.On("GetCreator").Return(testHelper.FakeTxCreator(t, mspid), nil).Once()

_, err = contract.RegisterAlgo(ctx, params)
_, err = contract.RegisterFunction(ctx, params)
assert.NoError(t, err)
}

func TestQueryAlgos(t *testing.T) {
func TestQueryFunctions(t *testing.T) {
contract := &SmartContract{}

computePlanKey := uuid.NewString()

algos := []*asset.Algo{
functions := []*asset.Function{
{Name: "test"},
{Name: "test2"},
}

filter := &asset.AlgoQueryFilter{
filter := &asset.FunctionQueryFilter{
ComputePlanKey: computePlanKey,
}

ctx := new(ledger.MockTransactionContext)
service := getMockedService(ctx)
service.On("QueryAlgos", &common.Pagination{Token: "", Size: 20}, filter).Return(algos, "", nil).Once()
service.On("QueryFunctions", &common.Pagination{Token: "", Size: 20}, filter).Return(functions, "", nil).Once()

param := &asset.QueryAlgosParam{Filter: filter, PageToken: "", PageSize: 20}
param := &asset.QueryFunctionsParam{Filter: filter, PageToken: "", PageSize: 20}
wrapper, err := communication.Wrap(context.Background(), param)
assert.NoError(t, err)

wrapped, err := contract.QueryAlgos(ctx, wrapper)
wrapped, err := contract.QueryFunctions(ctx, wrapper)
assert.NoError(t, err, "query should not fail")
resp := new(asset.QueryAlgosResponse)
resp := new(asset.QueryFunctionsResponse)
err = wrapped.Unwrap(resp)
assert.NoError(t, err)
assert.Len(t, resp.Algos, len(algos), "query should return all algos")
assert.Len(t, resp.Functions, len(functions), "query should return all functions")
}

func TestUpdate(t *testing.T) {
contract := &SmartContract{}

mspid := "org"
updateAlgoParam := &asset.UpdateAlgoParam{
updateFunctionParam := &asset.UpdateFunctionParam{
Key: "4c67ad88-309a-48b4-8bc4-c2e2c1a87a83",
Name: "Updated algo name",
Name: "Updated function name",
}
wrapper, err := communication.Wrap(context.Background(), updateAlgoParam)
wrapper, err := communication.Wrap(context.Background(), updateFunctionParam)
assert.NoError(t, err)

ctx := new(ledger.MockTransactionContext)

service := getMockedService(ctx)
service.On("UpdateAlgo", updateAlgoParam, mspid).Return(nil).Once()
service.On("UpdateFunction", updateFunctionParam, mspid).Return(nil).Once()

stub := new(testHelper.MockedStub)
ctx.On("GetStub").Return(stub).Once()
stub.On("GetCreator").Return(testHelper.FakeTxCreator(t, mspid), nil).Once()

err = contract.UpdateAlgo(ctx, wrapper)
err = contract.UpdateFunction(ctx, wrapper)
assert.NoError(t, err, "Smart contract execution should not fail")
}

func TestEvaluateTransactions(t *testing.T) {
contract := &SmartContract{}

queries := []string{
"GetAlgo",
"QueryAlgos",
"GetFunction",
"QueryFunctions",
}

assert.Equal(t, queries, contract.GetEvaluateTransactions(), "All non-commit transactions should be flagged")
Expand Down
4 changes: 2 additions & 2 deletions chaincode/ledger/dbal_computetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ func (db *DB) QueryComputeTasks(p *common.Pagination, filter *asset.TaskQueryFil
if filter.ComputePlanKey != "" {
assetFilter["compute_plan_key"] = filter.ComputePlanKey
}
if filter.AlgoKey != "" {
assetFilter["algo_key"] = json.RawMessage(fmt.Sprintf(`{"key": "%s"}`, filter.AlgoKey))
if filter.FunctionKey != "" {
assetFilter["function_key"] = json.RawMessage(fmt.Sprintf(`{"key": "%s"}`, filter.FunctionKey))
}

if len(assetFilter) > 0 {
Expand Down
Loading

0 comments on commit 3151513

Please sign in to comment.