mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-23 01:55:10 +00:00
Test Chaincode January 14 2023
This commit is contained in:
parent
35f98ea842
commit
6dce395fdf
1 changed files with 13 additions and 18 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
package chaincode
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -57,6 +57,18 @@ type Group struct {
|
||||||
GroupId string `json:"GroupId"`
|
GroupId string `json:"GroupId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Main function
|
||||||
|
func main() {
|
||||||
|
aChaincode, err := contractapi.NewChaincode(&SmartContract{})
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Error creating artifact chaincode: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := aChaincode.Start(); err != nil {
|
||||||
|
log.Panicf("Error starting artifact chaincode: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// InitLedger adds a base set of Data entries to the ledger
|
// InitLedger adds a base set of Data entries to the ledger
|
||||||
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, InitSchema string, InitData string) error {
|
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface, InitSchema string, InitData string) error {
|
||||||
|
|
||||||
|
|
@ -402,9 +414,7 @@ func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInter
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("the asset %s does not exist", Id)
|
return fmt.Errorf("the asset %s does not exist", Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// overwriting original asset with new asset
|
// overwriting original asset with new asset
|
||||||
|
|
||||||
data := Data{
|
data := Data{
|
||||||
Contributor: Contributor,
|
Contributor: Contributor,
|
||||||
ContributorId: ContributorId,
|
ContributorId: ContributorId,
|
||||||
|
|
@ -412,12 +422,10 @@ func (s *SmartContract) CreateDataSample(ctx contractapi.TransactionContextInter
|
||||||
Id: Id,
|
Id: Id,
|
||||||
Owner: Owner,
|
Owner: Owner,
|
||||||
}
|
}
|
||||||
|
|
||||||
assetJSON, err := json.Marshal(data)
|
assetJSON, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.GetStub().PutState(Id, assetJSON)
|
return ctx.GetStub().PutState(Id, assetJSON)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
@ -788,41 +796,31 @@ func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, i
|
||||||
if assetJSON == nil {
|
if assetJSON == nil {
|
||||||
return nil, fmt.Errorf("the asset %s does not exist", id)
|
return nil, fmt.Errorf("the asset %s does not exist", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var asset Asset
|
var asset Asset
|
||||||
err = json.Unmarshal(assetJSON, &asset)
|
err = json.Unmarshal(assetJSON, &asset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &asset, nil
|
return &asset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TransferAsset updates the owner field of asset with given id in world state, and returns the old owner.
|
// TransferAsset updates the owner field of asset with given id in world state, and returns the old owner.
|
||||||
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, id string, newOwner string) (string, error) {
|
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, id string, newOwner string) (string, error) {
|
||||||
asset, err := s.ReadAsset(ctx, id)
|
asset, err := s.ReadAsset(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
oldOwner := asset.Owner
|
oldOwner := asset.Owner
|
||||||
asset.Owner = newOwner
|
asset.Owner = newOwner
|
||||||
|
|
||||||
assetJSON, err := json.Marshal(asset)
|
assetJSON, err := json.Marshal(asset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ctx.GetStub().PutState(id, assetJSON)
|
err = ctx.GetStub().PutState(id, assetJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return oldOwner, nil
|
return oldOwner, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllAssets returns all assets found in world state
|
// GetAllAssets returns all assets found in world state
|
||||||
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Asset, error) {
|
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Asset, error) {
|
||||||
// range query with empty string for startKey and endKey does an
|
// range query with empty string for startKey and endKey does an
|
||||||
|
|
@ -832,14 +830,12 @@ func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resultsIterator.Close()
|
defer resultsIterator.Close()
|
||||||
|
|
||||||
var assets []*Asset
|
var assets []*Asset
|
||||||
for resultsIterator.HasNext() {
|
for resultsIterator.HasNext() {
|
||||||
queryResponse, err := resultsIterator.Next()
|
queryResponse, err := resultsIterator.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var asset Asset
|
var asset Asset
|
||||||
err = json.Unmarshal(queryResponse.Value, &asset)
|
err = json.Unmarshal(queryResponse.Value, &asset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -847,7 +843,6 @@ func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface
|
||||||
}
|
}
|
||||||
assets = append(assets, &asset)
|
assets = append(assets, &asset)
|
||||||
}
|
}
|
||||||
|
|
||||||
return assets, nil
|
return assets, nil
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue