Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chaincode/algo_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAggregateAlgo(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
assert.EqualValuesf(t, 200, resp.Status, "when querying an aggregate algo with status %d and message %s", resp.Status, resp.Message)
algo := outputAggregateAlgo{}
err = bytesToStruct(resp.Payload, &algo)
err = json.Unmarshal(resp.Payload, &algo)
assert.NoError(t, err, "when unmarshalling queried aggregate algo")
expectedAlgo := outputAggregateAlgo{
outputAlgo: outputAlgo{
Expand Down
2 changes: 1 addition & 1 deletion chaincode/algo_composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCompositeAlgo(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
assert.EqualValuesf(t, 200, resp.Status, "when querying a composite algo with status %d and message %s", resp.Status, resp.Message)
algo := outputCompositeAlgo{}
err = bytesToStruct(resp.Payload, &algo)
err = json.Unmarshal(resp.Payload, &algo)
assert.NoError(t, err, "when unmarshalling queried composite algo")
expectedAlgo := outputCompositeAlgo{
outputAlgo: outputAlgo{
Expand Down
2 changes: 1 addition & 1 deletion chaincode/algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAlgo(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
assert.EqualValuesf(t, 200, resp.Status, "when querying an algo with status %d and message %s", resp.Status, resp.Message)
algo := outputAlgo{}
err = bytesToStruct(resp.Payload, &algo)
err = json.Unmarshal(resp.Payload, &algo)
assert.NoError(t, err, "when unmarshalling queried objective")
expectedAlgo := outputAlgo{
Key: algoKey,
Expand Down
4 changes: 2 additions & 2 deletions chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"fmt"
"chaincode/errors"
"strings"
)

Expand All @@ -39,7 +39,7 @@ func queryFilter(db *LedgerDB, args []string) (elements interface{}, err error)
"aggregatetuple~tag",
}
if !stringInSlice(inp.IndexName, validIndexNames) {
err = fmt.Errorf("invalid indexName filter query: %s", inp.IndexName)
err = errors.BadRequest("invalid indexName filter query: %s", inp.IndexName)
return
}
indexName := inp.IndexName + "~key"
Expand Down
11 changes: 5 additions & 6 deletions chaincode/compute_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"chaincode/errors"
"fmt"
"strconv"
)

Expand All @@ -31,7 +30,7 @@ func (inpTraintuple *inputTraintuple) Fill(inpCP inputComputePlanTraintuple, tra
for _, InModelID := range inpCP.InModelsIDs {
inModelKey, ok := traintupleKeysByID[InModelID]
if !ok {
return fmt.Errorf("model ID %s not found", InModelID)
return errors.BadRequest("model ID %s not found", InModelID)
}
inpTraintuple.InModels = append(inpTraintuple.InModels, inModelKey)
}
Expand All @@ -49,7 +48,7 @@ func (inpAggregatetuple *inputAggregatetuple) Fill(inpCP inputComputePlanAggrega
for _, InModelID := range inpCP.InModelsIDs {
inModelKey, ok := aggregatetupleKeysByID[InModelID]
if !ok {
return fmt.Errorf("model ID %s not found", InModelID)
return errors.BadRequest("model ID %s not found", InModelID)
}
inpAggregatetuple.InModels = append(inpAggregatetuple.InModels, inModelKey)
}
Expand All @@ -70,14 +69,14 @@ func (inpCompositeTraintuple *inputCompositeTraintuple) Fill(inpCP inputComputeP
var ok bool
inpCompositeTraintuple.InHeadModelKey, ok = traintupleKeysByID[inpCP.InHeadModelID]
if !ok {
return fmt.Errorf("head model ID %s not found", inpCP.InHeadModelID)
return errors.BadRequest("head model ID %s not found", inpCP.InHeadModelID)
}
}
if inpCP.InTrunkModelID != "" {
var ok bool
inpCompositeTraintuple.InTrunkModelKey, ok = traintupleKeysByID[inpCP.InTrunkModelID]
if !ok {
return fmt.Errorf("trunk model ID %s not found", inpCP.InTrunkModelID)
return errors.BadRequest("trunk model ID %s not found", inpCP.InTrunkModelID)
}
}
return nil
Expand All @@ -86,7 +85,7 @@ func (inpCompositeTraintuple *inputCompositeTraintuple) Fill(inpCP inputComputeP
func (inpTesttuple *inputTesttuple) Fill(inpCP inputComputePlanTesttuple, traintupleKeysByID map[string]string) error {
traintupleKey, ok := traintupleKeysByID[inpCP.TraintupleID]
if !ok {
return fmt.Errorf("traintuple ID %s not found", inpCP.TraintupleID)
return errors.BadRequest("traintuple ID %s not found", inpCP.TraintupleID)
}
inpTesttuple.TraintupleKey = traintupleKey
inpTesttuple.DataManagerKey = inpCP.DataManagerKey
Expand Down
6 changes: 3 additions & 3 deletions chaincode/compute_plan_dag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "fmt"
import "chaincode/errors"

// TrainingTask is a node of a ComputeDAG. It represents a training task
// (i.e. a Traintuple, a CompositeTraintuple or an Aggregatetuple)
Expand Down Expand Up @@ -75,7 +75,7 @@ func (dag *ComputeDAG) sort() error {
current[i].Depth = depth
final = append(final, current[i])
if _, ok := IDPresents[current[i].ID]; ok {
return fmt.Errorf("compute plan error: Duplicate training task ID: %s", current[i].ID)
return errors.BadRequest("compute plan error: Duplicate training task ID: %s", current[i].ID)
}
IDPresents[current[i].ID] = current[i].Depth
} else {
Expand All @@ -90,7 +90,7 @@ func (dag *ComputeDAG) sort() error {
for _, c := range current {
errorIDs = append(errorIDs, c.ID)
}
return fmt.Errorf("compute plan error: Cyclic or missing dependency among inModels IDs: %v", errorIDs)
return errors.BadRequest("compute plan error: Cyclic or missing dependency among inModels IDs: %v", errorIDs)
}
i = 0
current = temp
Expand Down
3 changes: 1 addition & 2 deletions chaincode/compute_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -240,7 +239,7 @@ func validateTupleRank(t *testing.T, db *LedgerDB, expectedRank int, key string,
assert.NoError(t, err)
rank = tuple.Rank
default:
assert.NoError(t, fmt.Errorf("not implemented: %v", assetType))
t.Errorf("not implemented: %s", assetType)
}
assert.Equal(t, expectedRank, rank, "Rank for tuple of type %v with key \"%s\" should be %d", assetType, key, expectedRank)
}
Expand Down
14 changes: 2 additions & 12 deletions chaincode/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"chaincode/errors"
"fmt"
"strconv"
"strings"
)
Expand Down Expand Up @@ -53,10 +52,6 @@ func (dataManager *DataManager) Set(db *LedgerDB, inp inputDataManager) (string,
// and returning corresponding dataSample hashes, associated dataManagers, testOnly and errors
func setDataSample(db *LedgerDB, inp inputDataSample) (dataSampleHashes []string, dataSample DataSample, err error) {
dataSampleHashes = inp.Hashes
if err = checkHashes(dataSampleHashes); err != nil {
err = errors.BadRequest(err)
return
}
// check dataSample is not already in the ledger
if existingKeys := checkDataSamplesExist(db, dataSampleHashes); existingKeys != nil {
err = errors.Conflict("data samples with keys %s already exist", existingKeys).WithKeys(existingKeys)
Expand Down Expand Up @@ -92,11 +87,6 @@ func setDataSample(db *LedgerDB, inp inputDataSample) (dataSampleHashes []string
// one or more dataSamplef
func validateUpdateDataSample(db *LedgerDB, inp inputUpdateDataSample) (dataSampleHashes []string, dataManagerKeys []string, err error) {
// TODO return full dataSample
// check validity of dataSampleHashes
if err = checkHashes(inp.Hashes); err != nil {
err = errors.BadRequest(err)
return
}
// check dataManagers exist and are owned by the transaction requester
if err = checkDataManagerOwner(db, inp.DataManagerKeys); err != nil {
return
Expand Down Expand Up @@ -323,7 +313,7 @@ func queryDataset(db *LedgerDB, args []string) (outputDataset, error) {
func queryDataSamples(db *LedgerDB, args []string) ([]outputDataSample, error) {
outDataSamples := []outputDataSample{}
if len(args) != 0 {
err := fmt.Errorf("incorrect number of arguments, expecting nothing")
err := errors.BadRequest("incorrect number of arguments, expecting nothing")
return outDataSamples, err
}
elementsKeys, err := db.GetIndexKeys("dataSample~dataManager~key", []string{"dataSample"})
Expand Down Expand Up @@ -393,7 +383,7 @@ func checkSameDataManager(db *LedgerDB, dataManagerKey string, dataSampleKeys []
return testOnly, trainOnly, err
}
if !stringInSlice(dataManagerKey, dataSample.DataManagerKeys) {
err = fmt.Errorf("dataSample do not belong to the same dataManager")
err = errors.BadRequest("dataSample do not belong to the same dataManager")
return testOnly, trainOnly, err
}
testOnly = testOnly && dataSample.TestOnly
Expand Down
2 changes: 1 addition & 1 deletion chaincode/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestDataManager(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
assert.EqualValuesf(t, 200, resp.Status, "when querying the dataManager, status %d and message %s", resp.Status, resp.Message)
dataManager := outputDataManager{}
err = bytesToStruct(resp.Payload, &dataManager)
err = json.Unmarshal(resp.Payload, &dataManager)
assert.NoError(t, err, "when unmarshalling queried dataManager")
expectedDataManager := outputDataManager{
ObjectiveKey: inpDataManager.ObjectiveKey,
Expand Down
4 changes: 2 additions & 2 deletions chaincode/generate_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestPipeline(t *testing.T) {
assert.EqualValuesf(t, 200, resp.Status, "when adding traintuple with status %d and message %s", resp.Status, resp.Message)
traintuple := outputTraintuple{}
respTraintuple := resp.Payload
if err := bytesToStruct(respTraintuple, &traintuple); err != nil {
if err := json.Unmarshal(respTraintuple, &traintuple); err != nil {
t.Errorf("when unmarshalling queried traintuple with error %s", err)
}
trainWorker := traintuple.Dataset.Worker
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestPipeline(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
respTesttuple := resp.Payload
testtuple := outputTesttuple{}
if err := bytesToStruct(respTesttuple, &testtuple); err != nil {
if err := json.Unmarshal(respTesttuple, &testtuple); err != nil {
t.Errorf("when unmarshalling queried testtuple with error %s", err)
}
testWorker := testtuple.Dataset.Worker
Expand Down
13 changes: 6 additions & 7 deletions chaincode/ledger_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"chaincode/errors"
"encoding/json"
"fmt"
"sync"

"github.com/hyperledger/fabric/core/chaincode/shim"
Expand Down Expand Up @@ -129,11 +128,11 @@ func (db *LedgerDB) Add(key string, object interface{}) error {
func (db *LedgerDB) CreateIndex(index string, attributes []string) error {
compositeKey, err := db.cc.CreateCompositeKey(index, attributes)
if err != nil {
return fmt.Errorf("cannot create index %s: %s", index, err.Error())
return errors.Internal("cannot create index %s: %s", index, err.Error())
}
value := []byte{0x00}
if err = db.cc.PutState(compositeKey, value); err != nil {
return fmt.Errorf("cannot create index %s: %s", index, err.Error())
return errors.Internal("cannot create index %s: %s", index, err.Error())
}
return nil
}
Expand All @@ -160,7 +159,7 @@ func (db *LedgerDB) GetIndexKeys(index string, attributes []string) ([]string, e
keys := make([]string, 0)
iterator, err := db.cc.GetStateByPartialCompositeKey(index, attributes)
if err != nil {
return nil, fmt.Errorf("get index %s failed: %s", index, err.Error())
return nil, errors.Internal("get index %s failed: %s", index, err.Error())
}
defer iterator.Close()
for iterator.HasNext() {
Expand All @@ -170,7 +169,7 @@ func (db *LedgerDB) GetIndexKeys(index string, attributes []string) ([]string, e
}
_, keyParts, err := db.cc.SplitCompositeKey(compositeKey.Key)
if err != nil {
return nil, fmt.Errorf("get index %s failed: cannot split key %s: %s", index, compositeKey.Key, err.Error())
return nil, errors.Internal("get index %s failed: cannot split key %s: %s", index, compositeKey.Key, err.Error())
}
keys = append(keys, keyParts[len(keyParts)-1])
}
Expand Down Expand Up @@ -393,7 +392,7 @@ func (db *LedgerDB) GetOutModelHashDress(traintupleKey string, modelType Composi
case TrunkType:
return tuple.OutTrunkModel.OutModel, nil
default:
return nil, fmt.Errorf("GetOutModelHashDress: Unsupported composite model type %s", modelType)
return nil, errors.Internal("GetOutModelHashDress: Unsupported composite model type %s", modelType)
}

case TraintupleType:
Expand All @@ -407,7 +406,7 @@ func (db *LedgerDB) GetOutModelHashDress(traintupleKey string, modelType Composi
return tuple.OutModel, nil
}
default:
return nil, fmt.Errorf("GetOutModelHashDress: Unsupported asset type %s", assetType)
return nil, errors.Internal("GetOutModelHashDress: Unsupported asset type %s", assetType)
}
}

Expand Down
6 changes: 3 additions & 3 deletions chaincode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (t *SubstraChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Respons
case "queryNodes":
result, err = queryNodes(db, args)
default:
err = fmt.Errorf("function \"%s\" not implemented", fn)
err = errors.BadRequest("function \"%s\" not implemented", fn)
}
logger.Infof("Response from chaincode: %#v, error: %s", result, err)
// Return the result as success payload
Expand All @@ -178,12 +178,12 @@ func (t *SubstraChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Respons
// one event per call
err = db.SendTuplesEvent()
if err != nil {
return formatErrorResponse(fmt.Errorf("could not send event: %s", err.Error()))
return formatErrorResponse(errors.Internal("could not send event: %s", err.Error()))
}
// Marshal to json the smartcontract result
resp, err := json.Marshal(result)
if err != nil {
return formatErrorResponse(fmt.Errorf("could not format response: %s", err.Error()))
return formatErrorResponse(errors.Internal("could not format response: %s", err.Error()))
}

return shim.Success(resp)
Expand Down
3 changes: 1 addition & 2 deletions chaincode/objective.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"chaincode/errors"
"fmt"
"sort"
)

Expand Down Expand Up @@ -116,7 +115,7 @@ func queryObjective(db *LedgerDB, args []string) (out outputObjective, err error
func queryObjectives(db *LedgerDB, args []string) (outObjectives []outputObjective, err error) {
outObjectives = []outputObjective{}
if len(args) != 0 {
err = fmt.Errorf("incorrect number of arguments, expecting nothing")
err = errors.BadRequest("incorrect number of arguments, expecting nothing")
return
}
elementsKeys, err := db.GetIndexKeys("objective~owner~key", []string{"objective"})
Expand Down
2 changes: 1 addition & 1 deletion chaincode/objective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestObjective(t *testing.T) {
resp = mockStub.MockInvoke("42", args)
assert.EqualValuesf(t, 200, resp.Status, "when querying a dataManager with status %d and message %s", resp.Status, resp.Message)
objective := outputObjective{}
err = bytesToStruct(resp.Payload, &objective)
err = json.Unmarshal(resp.Payload, &objective)
assert.NoError(t, err, "when unmarshalling queried objective")
expectedObjective := outputObjective{
Key: objectiveKey,
Expand Down
18 changes: 9 additions & 9 deletions chaincode/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"fmt"
"chaincode/errors"
"math"
)

Expand Down Expand Up @@ -151,7 +151,7 @@ func (outputTraintuple *outputTraintuple) Fill(db *LedgerDB, traintuple Traintup
// fill algo
algo, err := db.GetAlgo(traintuple.AlgoKey)
if err != nil {
err = fmt.Errorf("could not retrieve algo with key %s - %s", traintuple.AlgoKey, err.Error())
err = errors.Internal("could not retrieve algo with key %s - %s", traintuple.AlgoKey, err.Error())
return
}
outputTraintuple.Algo = &HashDressName{
Expand All @@ -166,7 +166,7 @@ func (outputTraintuple *outputTraintuple) Fill(db *LedgerDB, traintuple Traintup
}
parentTraintuple, err := db.GetTraintuple(inModelKey)
if err != nil {
return fmt.Errorf("could not retrieve parent traintuple with key %s - %s", inModelKey, err.Error())
return errors.Internal("could not retrieve parent traintuple with key %s - %s", inModelKey, err.Error())
}
inModel := &Model{
TraintupleKey: inModelKey,
Expand Down Expand Up @@ -219,7 +219,7 @@ func (out *outputTesttuple) Fill(db *LedgerDB, key string, in Testtuple) error {
// fill type
traintupleType, err := db.GetAssetType(in.TraintupleKey)
if err != nil {
return fmt.Errorf("could not retrieve traintuple type with key %s - %s", in.TraintupleKey, err.Error())
return errors.Internal("could not retrieve traintuple type with key %s - %s", in.TraintupleKey, err.Error())
}
out.TraintupleType = LowerFirst(traintupleType.String())

Expand All @@ -229,18 +229,18 @@ func (out *outputTesttuple) Fill(db *LedgerDB, key string, in Testtuple) error {
case TraintupleType:
algo, err = db.GetAlgo(in.AlgoKey)
if err != nil {
return fmt.Errorf("could not retrieve algo with key %s - %s", in.AlgoKey, err.Error())
return errors.Internal("could not retrieve algo with key %s - %s", in.AlgoKey, err.Error())
}
case CompositeTraintupleType:
compositeAlgo, err := db.GetCompositeAlgo(in.AlgoKey)
if err != nil {
return fmt.Errorf("could not retrieve composite algo with key %s - %s", in.AlgoKey, err.Error())
return errors.Internal("could not retrieve composite algo with key %s - %s", in.AlgoKey, err.Error())
}
algo = compositeAlgo.Algo
case AggregatetupleType:
aggregateAlgo, err := db.GetAggregateAlgo(in.AlgoKey)
if err != nil {
return fmt.Errorf("could not retrieve aggregate algo with key %s - %s", in.AlgoKey, err.Error())
return errors.Internal("could not retrieve aggregate algo with key %s - %s", in.AlgoKey, err.Error())
}
algo = aggregateAlgo.Algo
}
Expand All @@ -252,10 +252,10 @@ func (out *outputTesttuple) Fill(db *LedgerDB, key string, in Testtuple) error {
// fill objective
objective, err := db.GetObjective(in.ObjectiveKey)
if err != nil {
return fmt.Errorf("could not retrieve associated objective with key %s- %s", in.ObjectiveKey, err.Error())
return errors.Internal("could not retrieve associated objective with key %s- %s", in.ObjectiveKey, err.Error())
}
if objective.Metrics == nil {
return fmt.Errorf("objective %s is missing metrics values", in.ObjectiveKey)
return errors.Internal("objective %s is missing metrics values", in.ObjectiveKey)
}
metrics := HashDress{
Hash: objective.Metrics.Hash,
Expand Down
Loading