Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Use random UUIDs for asset keys (instead of content hashes) (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Gasser authored Nov 3, 2020
1 parent c5cd878 commit a418512
Show file tree
Hide file tree
Showing 62 changed files with 2,992 additions and 950 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# fabric
# fabric
*.block

# OSX
.DS_Store


# IDE config files
.idea
.idea
.vscode
545 changes: 301 additions & 244 deletions EXAMPLES.md

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions chaincode/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (

// Set is a method of the receiver Algo. It uses inputAlgo fields to set the Algo
// Returns the algoKey
func (algo *Algo) Set(db *LedgerDB, inp inputAlgo) (algoKey string, err error) {
algoKey = inp.Hash
func (algo *Algo) Set(db *LedgerDB, inp inputAlgo) (err error) {
// find associated owner
owner, err := GetTxCreator(db.cc)
if err != nil {
Expand All @@ -33,8 +32,10 @@ func (algo *Algo) Set(db *LedgerDB, inp inputAlgo) (algoKey string, err error) {
return
}

algo.Key = inp.Key
algo.AssetType = AlgoType
algo.Name = inp.Name
algo.Hash = inp.Hash
algo.StorageAddress = inp.StorageAddress
algo.Description = &HashDress{
Hash: inp.DescriptionHash,
Expand All @@ -59,21 +60,21 @@ func registerAlgo(db *LedgerDB, args []string) (resp outputKey, err error) {
}
// check validity of input args and convert it to Algo
algo := Algo{}
algoKey, err := algo.Set(db, inp)
err = algo.Set(db, inp)
if err != nil {
return
}
// submit to ledger
err = db.Add(algoKey, algo)
err = db.Add(algo.Key, algo)
if err != nil {
return
}
// create composite key
err = db.CreateIndex("algo~owner~key", []string{"algo", algo.Owner, algoKey})
err = db.CreateIndex("algo~owner~key", []string{"algo", algo.Owner, algo.Key})
if err != nil {
return
}
return outputKey{Key: algoKey}, nil
return outputKey{Key: algo.Key}, nil
}

// queryAlgo returns an algo of the ledger given its key
Expand All @@ -87,7 +88,7 @@ func queryAlgo(db *LedgerDB, args []string) (out outputAlgo, err error) {
if err != nil {
return
}
out.Fill(inp.Key, algo)
out.Fill(algo)
return
}

Expand All @@ -109,7 +110,7 @@ func queryAlgos(db *LedgerDB, args []string) (outAlgos []outputAlgo, err error)
return outAlgos, err
}
var out outputAlgo
out.Fill(key, algo)
out.Fill(algo)
outAlgos = append(outAlgos, out)
}
return
Expand Down
17 changes: 9 additions & 8 deletions chaincode/algo_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (

// Set is a method of the receiver Aggregate. It uses inputAggregateAlgo fields to set the AggregateAlgo
// Returns the aggregateAlgoKey
func (algo *AggregateAlgo) Set(db *LedgerDB, inp inputAggregateAlgo) (algoKey string, err error) {
algoKey = inp.Hash
func (algo *AggregateAlgo) Set(db *LedgerDB, inp inputAggregateAlgo) (err error) {
// find associated owner
owner, err := GetTxCreator(db.cc)
if err != nil {
Expand All @@ -33,8 +32,10 @@ func (algo *AggregateAlgo) Set(db *LedgerDB, inp inputAggregateAlgo) (algoKey st
return
}

algo.Key = inp.Key
algo.AssetType = AggregateAlgoType
algo.Name = inp.Name
algo.Hash = inp.Hash
algo.StorageAddress = inp.StorageAddress
algo.Description = &HashDress{
Hash: inp.DescriptionHash,
Expand All @@ -59,21 +60,21 @@ func registerAggregateAlgo(db *LedgerDB, args []string) (resp outputKey, err err
}
// check validity of input args and convert it to CompositeAlgo
algo := AggregateAlgo{}
algoKey, err := algo.Set(db, inp)
err = algo.Set(db, inp)
if err != nil {
return
}
// submit to ledger
err = db.Add(algoKey, algo)
err = db.Add(inp.Key, algo)
if err != nil {
return
}
// create aggregate key
err = db.CreateIndex("aggregateAlgo~owner~key", []string{"aggregateAlgo", algo.Owner, algoKey})
err = db.CreateIndex("aggregateAlgo~owner~key", []string{"aggregateAlgo", algo.Owner, inp.Key})
if err != nil {
return
}
return outputKey{Key: algoKey}, nil
return outputKey{Key: inp.Key}, nil
}

// queryAggregateAlgo returns an algo of the ledger given its key
Expand All @@ -87,7 +88,7 @@ func queryAggregateAlgo(db *LedgerDB, args []string) (out outputAggregateAlgo, e
if err != nil {
return
}
out.Fill(inp.Key, algo)
out.Fill(algo)
return
}

Expand All @@ -109,7 +110,7 @@ func queryAggregateAlgos(db *LedgerDB, args []string) (outAlgos []outputAggregat
return outAlgos, err
}
var out outputAggregateAlgo
out.Fill(key, algo)
out.Fill(algo)
outAlgos = append(outAlgos, out)
}
return
Expand Down
5 changes: 2 additions & 3 deletions chaincode/algo_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestAggregateAlgo(t *testing.T) {
err := json.Unmarshal(resp.Payload, &res)
assert.NoError(t, err, "should unmarshal without problem")
algoKey := res.Key
assert.Equalf(t, inpAlgo.Hash, algoKey, "when adding aggregate algo, key does not corresponds to its hash - key: %s and hash %s", algoKey, inpAlgo.Hash)

// Query algo from key and check the consistency of returned arguments
args = [][]byte{[]byte("queryAggregateAlgo"), keyToJSON(algoKey)}
Expand All @@ -56,8 +55,8 @@ func TestAggregateAlgo(t *testing.T) {
outputAlgo: outputAlgo{
Key: algoKey,
Name: inpAlgo.Name,
Content: HashDress{
Hash: algoKey,
Content: &HashDress{
Hash: inpAlgo.Hash,
StorageAddress: inpAlgo.StorageAddress,
},
Description: &HashDress{
Expand Down
17 changes: 9 additions & 8 deletions chaincode/algo_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (

// Set is a method of the receiver CompositeAlgo. It uses inputCompositeAlgo fields to set the CompositeAlgo
// Returns the compositeAlgoKey
func (algo *CompositeAlgo) Set(db *LedgerDB, inp inputCompositeAlgo) (algoKey string, err error) {
algoKey = inp.Hash
func (algo *CompositeAlgo) Set(db *LedgerDB, inp inputCompositeAlgo) (err error) {
// find associated owner
owner, err := GetTxCreator(db.cc)
if err != nil {
Expand All @@ -33,8 +32,10 @@ func (algo *CompositeAlgo) Set(db *LedgerDB, inp inputCompositeAlgo) (algoKey st
return
}

algo.Key = inp.Key
algo.AssetType = CompositeAlgoType
algo.Name = inp.Name
algo.Hash = inp.Hash
algo.StorageAddress = inp.StorageAddress
algo.Description = &HashDress{
Hash: inp.DescriptionHash,
Expand All @@ -59,21 +60,21 @@ func registerCompositeAlgo(db *LedgerDB, args []string) (resp outputKey, err err
}
// check validity of input args and convert it to CompositeAlgo
algo := CompositeAlgo{}
algoKey, err := algo.Set(db, inp)
err = algo.Set(db, inp)
if err != nil {
return
}
// submit to ledger
err = db.Add(algoKey, algo)
err = db.Add(algo.Key, algo)
if err != nil {
return
}
// create composite key
err = db.CreateIndex("compositeAlgo~owner~key", []string{"compositeAlgo", algo.Owner, algoKey})
err = db.CreateIndex("compositeAlgo~owner~key", []string{"compositeAlgo", algo.Owner, algo.Key})
if err != nil {
return
}
return outputKey{Key: algoKey}, nil
return outputKey{Key: algo.Key}, nil
}

// queryCompositeAlgo returns an algo of the ledger given its key
Expand All @@ -87,7 +88,7 @@ func queryCompositeAlgo(db *LedgerDB, args []string) (out outputCompositeAlgo, e
if err != nil {
return
}
out.Fill(inp.Key, algo)
out.Fill(algo)
return
}

Expand All @@ -109,7 +110,7 @@ func queryCompositeAlgos(db *LedgerDB, args []string) (outAlgos []outputComposit
return outAlgos, err
}
var out outputCompositeAlgo
out.Fill(key, algo)
out.Fill(algo)
outAlgos = append(outAlgos, out)
}
return
Expand Down
5 changes: 2 additions & 3 deletions chaincode/algo_composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestCompositeAlgo(t *testing.T) {
err := json.Unmarshal(resp.Payload, &res)
assert.NoError(t, err, "should unmarshal without problem")
algoKey := res.Key
assert.Equalf(t, inpAlgo.Hash, algoKey, "when adding composite algo, key does not corresponds to its hash - key: %s and hash %s", algoKey, inpAlgo.Hash)

// Query algo from key and check the consistency of returned arguments
args = [][]byte{[]byte("queryCompositeAlgo"), keyToJSON(algoKey)}
Expand All @@ -56,8 +55,8 @@ func TestCompositeAlgo(t *testing.T) {
outputAlgo: outputAlgo{
Key: algoKey,
Name: inpAlgo.Name,
Content: HashDress{
Hash: algoKey,
Content: &HashDress{
Hash: inpAlgo.Hash,
StorageAddress: inpAlgo.StorageAddress,
},
Description: &HashDress{
Expand Down
6 changes: 2 additions & 4 deletions chaincode/algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func TestAlgo(t *testing.T) {
res := outputKey{}
err := json.Unmarshal(resp.Payload, &res)
assert.NoError(t, err, "should unmarshal without problem")
algoKey := res.Key
assert.Equalf(t, inpAlgo.Hash, algoKey, "when adding algo, key does not corresponds to its hash - key: %s and hash %s", algoKey, inpAlgo.Hash)

// Query algo from key and check the consistency of returned arguments
args = [][]byte{[]byte("queryAlgo"), keyToJSON(algoKey)}
Expand All @@ -53,8 +51,8 @@ func TestAlgo(t *testing.T) {
expectedAlgo := outputAlgo{
Key: algoKey,
Name: inpAlgo.Name,
Content: HashDress{
Hash: algoKey,
Content: &HashDress{
Hash: inpAlgo.Hash,
StorageAddress: inpAlgo.StorageAddress,
},
Description: &HashDress{
Expand Down
Loading

0 comments on commit a418512

Please sign in to comment.