El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.
Blockchain Service
Blockchain Service
- What's New
- Function Overview
- Service Overview
- Billing
- Getting Started
-
User Guide
- Enhanced Hyperledger Fabric BCS Management
- Best Practices
-
Developer Guide
- Overview
- Chaincode Development
- Application Development
- Demos
-
Blockchain Middleware APIs
- Overview
- Chaincode Invoking (OBT)
-
Chaincode Management
- Obtaining a Token
- Installing a Chaincode
- Instantiating a Chaincode
- Listing Installed Chaincodes
- Querying Version of a Specified Chaincode
- Querying Chaincode Installation Information
- Querying Chaincode Instantiation Information
- Querying an Appchain
- Listing Blocks
- Listing Transactions
- Querying Transaction Quantity
- Listing Block Transactions
- Querying Transaction Details
- Querying Peers
- Querying diskUsage of a Node
- Querying the System-Hosted Certificate Status
- Deleting a Chaincode
- Downloading a Report
- Distributed Identity (OBT)
- Trusted Data Exchange (OBT)
- Appendix
-
API Reference
- Before You Start
- API Overview
- Examples
- Calling APIs
-
APIs (Enhanced Hyperledger Fabric)
-
BCS Management
- Creating a BCS Service
- Querying Creation Status of a BCS Service
- Querying a BCS Service
- Modifying a BCS Service
- Creating Channels
- Querying Channel Information
- Adding Peers to a Channel
- Removing Organizations from a Channel
- Downloading Certificates
- Downloading the SDK Configuration
- Generating a User Certificate
- Unfreezing a User Certificate
- Freezing a User Certificate
- Querying Quotas
- Querying Flavors
- Querying Peer Information
- Querying Asynchronous Operation Results
- Querying the BCS Service List
- Deleting a BCS Service
- Removing a Peer from a Channel
- Deleting a Channel
- BCS Consortium
- BCS Monitoring
-
BCS Management
- Permissions Policies and Supported Actions
- Appendix
- Change History
- SDK Reference
-
FAQs
-
Enhanced Hyperledger Fabric
- Billing
-
Instance Management
-
Consultation
- How Do I Determine Whether a Blockchain Is Necessary?
- What Underlying Framework Is Used for Huawei Cloud BCS?
- Can BCS Instances Deployed on the Public Cloud Access Blockchain Nodes on Other Clouds?
- What Competitive Advantages Does Huawei Cloud BCS Have?
- In Which Direction and What Capabilities Will Huawei Cloud BCS Develop?
- What Are the Specifications of VMs to Be Purchased for BCS?
- How Do I Get Access to the Partners of Huawei Cloud BCS for More Services?
- What Are the Differences Between Channel Isolation and Privacy Protection?
- How Well Does BCS Perform?
- Does BCS Support Customized Development?
- When Do I Need to Hibernate or Wake an Instance?
-
Service Usage
- Which Ports of a Security Group Are Opened When I Create a BCS Instance?
- How Do I Check Whether the ICAgent Is Installed for the Cluster?
- What Can I Do If I Can't Open the Blockchain Management Console?
- What Should I Do If My BCS Instance Remains in the Creating State?
- What Should I Do If a Peer Restarts Frequently with the Error Message "PanicDB not exist"?
- What Can I Do If the CPU Usage of a Blockchain Node Reaches 100%?
- Why Can't I Log In to the Blockchain Management Console?
- BCS.4009100: System Error
- How Can I Obtain Private Keys and Certificates for Enhanced Hyperledger Fabric Blockchains?
- Why Does Chaincode Instantiation Fail When I Deploy a Fabric v1.4 Instance Using a v1.19 CCE Cluster?
- Can All Blocks Be Saved As More and More Blocks Are Created?
-
What Can I Do If I Fail to Purchase a BCS Instance?
- General Checks
-
Detailed Checks
- CCE Cluster Quota Used Up
- Failed to Create a Cluster
- Failed to Create a PVC
- Cluster Already In Use
- SFS Turbo File System Quota Exceeded
- No EIP Bound
- CCE Is Abnormal
- Cluster Status Is Abnormal
- Subnet Unavailable
- Quick Deployment in Progress
- CCE Status Check Times Out
- Insufficient Master Nodes in the AZ of the CCE Cluster
-
Abnormal Instance Statuses
- What Can I Do If a BCS Instance Is in the Abnormal State?
- What Can I Do If a BCS Instance Is in the Unknown State?
- What Can I Do If a BCS Instance Is in the EIP abnormal State?
- What Can I Do If a BCS Instance Is in the Frozen or Cluster frozen State?
- What Can I Do If the BCS Instance and the peer-xxx StatefulSet Are Abnormal After an Organization or a Peer Is Added?
- Other Issues
-
Consultation
- Chaincode Management
- Data Storage to the Blockchain
- Demos and APIs
- O&M and Monitoring
- Consortium Management
-
Enhanced Hyperledger Fabric
- Videos
-
More Documents
-
User Guide (ME-Abu Dhabi Region)
- Service Overview
- Managing Enhanced Hyperledger Fabric Instances
-
FAQs
-
BCS FAQs
-
Instance Management
-
Consultation
- How Do I Determine Whether a Blockchain Is Necessary?
- What Underlying Framework Is Used for BCS?
- What Competitive Advantages Does BCS Have?
- What Are the Specifications of VMs to Be Created for BCS?
- What Are the Differences Between Channel Isolation and Privacy Protection?
- How Well Does BCS Perform?
- When Do I Need to Hibernate or Wake an Instance?
-
Service Usage
- How Do I Check Whether the ICAgent Is Installed for the Cluster?
- What Can I Do If I Can't Open the Blockchain Management Console?
- What Should I Do If My BCS Instance Remains in the Creating State?
- What Should I Do If a Peer Restarts Frequently with the Error Message "PanicDB not exist"?
- What Can I Do If the CPU Usage of a Blockchain Node Reaches 100%?
- Why Can't I Log In to the Blockchain Management Console?
- BCS.4009100: System Error
- How Can I Obtain Private Keys and Certificates for Enhanced Hyperledger Fabric Blockchains?
- Can All Blocks Be Saved As More and More Blocks Are Created?
- Abnormal Instance Statuses
- Other Issues
-
Consultation
- Chaincode Management
- Data Storage to the Blockchain
- Demos and APIs
- O&M and Monitoring
- Consortium Management
-
Instance Management
-
BCS FAQs
- Change History
- Developer Guide (ME-Abu Dhabi Region)
-
User Guide (ME-Abu Dhabi Region)
- General Reference
On this page
Show all
Help Center/
Blockchain Service/
Developer Guide/
Chaincode Development/
Go Chaincode Development/
Sample Chaincode (1.4)
Copied.
Sample Chaincode (1.4)
The following is an example of installing and instantiating the account transfer chaincode (1.4). For details about how to debug this chaincode, see the official Fabric examples.
package main import ( "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" pb "github.com/hyperledger/fabric/protos/peer" ) type SimpleChaincode struct { } //Automatically invoked during chaincode instantiation or update to initialize the data status. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { //The output information of the println function is displayed in the logs of the chaincode container. fmt.Println("ex02 Init") //Obtain the parameters transferred by the user to invoke the chaincode. _, args := stub.GetFunctionAndParameters() var A, B string //Two accounts var Aval, Bval int //Balances of the two accounts var err error //Check whether the number of parameters is 4. If not, an error message is returned. if len(args) != 4 { return shim.Error("Incorrect number of arguments. Expecting 4") } A = args[0] //Username of account A Aval, err = strconv.Atoi(args[1]) //Balance in account A if err != nil { return shim.Error("Expecting integer value for asset holding") } B = args[2] //Username of account B Bval, err = strconv.Atoi(args[3]) //Balance in account B if err != nil { return shim.Error("Expecting integer value for asset holding") } fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) //Write the status of account A to the ledger. err = stub.PutState(A, []byte(strconv.Itoa(Aval))) if err != nil { return shim.Error(err.Error()) } //Write the status of account B to the ledger. err = stub.PutState(B, []byte(strconv.Itoa(Bval))) if err != nil { return shim.Error(err.Error()) } return shim.Success(nil) } //The function is automatically invoked to query or invoke the ledger data. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { fmt.Println("ex02 Invoke") //Obtain the function name and parameters transferred by the user to invoke the chaincode. function, args := stub.GetFunctionAndParameters() //Check the obtained function name. if function == "invoke" { //Invoke the invoke function to implement the money transfer operation. return t.invoke(stub, args) } else if function == "delete" { //Invoke the delete function to deregister the account. return t.delete(stub, args) } else if function == "query" { //Invoke the query interface to query the account. return t.query(stub, args) } //If the transferred function name is incorrect, shim.Error() is returned. return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"") } //Transfer money between accounts. func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { var A, B string //Accounts A and B var Aval, Bval int //Account balance var X int //Transfer amount var err error if len(args) != 3 { return shim.Error("Incorrect number of arguments. Expecting 3") } A = args[0] //Username of account A B = args[1] //Username of account B //Obtain the balance of account A from the ledger. Avalbytes, err := stub.GetState(A) if err != nil { return shim.Error("Failed to get state") } if Avalbytes == nil { return shim.Error("Entity not found") } Aval, _ = strconv.Atoi(string(Avalbytes)) //Obtain the balance of account B from the ledger. Bvalbytes, err := stub.GetState(B) if err != nil { return shim.Error("Failed to get state") } if Bvalbytes == nil { return shim.Error("Entity not found") } Bval, _ = strconv.Atoi(string(Bvalbytes)) //X indicates the transfer amount. X, err = strconv.Atoi(args[2]) if err != nil { return shim.Error("Invalid transaction amount, expecting a integer value") } //Transfer Aval = Aval - X Bval = Bval + X fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) //Update the balance of account A after the transfer. err = stub.PutState(A, []byte(strconv.Itoa(Aval))) if err != nil { return shim.Error(err.Error()) } //Update the balance of account B after the transfer. err = stub.PutState(B, []byte(strconv.Itoa(Bval))) if err != nil { return shim.Error(err.Error()) } return shim.Success(nil) } //Account deregistration func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } A = args[0] //Username //Delete the account status from the ledger. err := stub.DelState(A) if err != nil { return shim.Error("Failed to delete state") } return shim.Success(nil) } //Account query func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { var A string var err error if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting name of the person to query") } A = args[0] //Username //Obtain the balance of the account from the ledger. Avalbytes, err := stub.GetState(A) if err != nil { jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" return shim.Error(jsonResp) } if Avalbytes == nil { jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" return shim.Error(jsonResp) } jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" fmt.Printf("Query Response:%s\n", jsonResp) //Return the transfer amount. return shim.Success(Avalbytes) } func main() { err := shim.Start(new(SimpleChaincode)) if err != nil { fmt.Printf("Error starting Simple chaincode: %s", err) } }
Parent topic: Go Chaincode Development
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot