Using docker and docker-compose Usage: network.sh <Mode> [Flags] Modes: up - Bring up Fabric orderer and peer nodes. No channel is created up createChannel - Bring up fabric network with one channel createChannel - Create and join a channel after the network is created deployCC - Deploy a chaincode to a channel (defaults to asset-transfer-basic) down - Bring down the network
Flags: Used with network.sh up, network.sh createChannel: -ca <use CAs> - Use Certificate Authorities to generate network crypto material -c <channel name> - Name of channel to create (defaults to "mychannel") -s <dbtype> - Peer state database to deploy: goleveldb (default) or couchdb -r <max retry> - CLI times out after certain number of attempts (defaults to 5) -d <delay> - CLI delays for a certain number of seconds (defaults to 3) -verbose - Verbose mode
Used with network.sh deployCC -c <channel name> - Name of channel to deploy chaincode to -ccn <name> - Chaincode name. -ccl <language> - Programming language of the chaincode to deploy: go, java, javascript, typescript -ccv <version> - Chaincode version. 1.0 (default), v2, version3.x, etc -ccs <sequence> - Chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc -ccp <path> - File path to the chaincode. -ccep <policy> - (Optional) Chaincode endorsement policy using signature policy syntax. The default policy requires an endorsement from Org1 and Org2 -cccg <collection-config> - (Optional) File path to private data collections configuration file -cci <fcn name> - (Optional) Name of chaincode initialization function. When a function is provided, the execution of init will be requested and the function will be invoked.
-h - Print this message
Possible Mode and flag combinations up -ca -r -d -s -verbose up createChannel -ca -c -r -d -s -verbose createChannel -c -r -d -verbose deployCC -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose
// Init is called during chaincode instantiation to initialize any // data. Note that chaincode upgrade also calls this function to reset // or to migrate data, so be careful to avoid a scenario where you // inadvertently clobber your ledger's data! func(t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { // Get the args from the transaction proposal args := stub.GetStringArgs() iflen(args) != 2 { return shim.Error("Incorrect arguments. Expecting a key and a value") }
// Set up any variables or assets here by calling stub.PutState()
// We store the key and the value on the ledger err := stub.PutState(args[0], []byte(args[1])) if err != nil { return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0])) } return shim.Success(nil) }
// Invoke is called per transaction on the chaincode. Each transaction is // either a 'get' or a 'set' on the asset created by Init function. The Set // method may create a new asset by specifying a new key-value pair. func(t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response { // Extract the function and args from the transaction proposal fn, args := stub.GetFunctionAndParameters()
var result string var err error if fn == "set" { result, err = set(stub, args) } else { result, err = get(stub, args) } if err != nil { return shim.Error(err.Error()) }
// Return the result as success payload return shim.Success([]byte(result)) }
// Set stores the asset (both key and value) on the ledger. If the key exists, // it will override the value with the new one funcset(stub shim.ChaincodeStubInterface, args []string) (string, error) { iflen(args) != 2 { return"", fmt.Errorf("Incorrect arguments. Expecting a key and a value") }
err := stub.PutState(args[0], []byte(args[1])) if err != nil { return"", fmt.Errorf("Failed to set asset: %s", args[0]) } return args[1], nil }
// Get returns the value of the specified asset key funcget(stub shim.ChaincodeStubInterface, args []string) (string, error) { iflen(args) != 1 { return"", fmt.Errorf("Incorrect arguments. Expecting a key") }
value, err := stub.GetState(args[0]) if err != nil { return"", fmt.Errorf("Failed to get asset: %s with error: %s", args[0], err) } if value == nil { return"", fmt.Errorf("Asset not found: %s", args[0]) } returnstring(value), nil }