Fabric smart contract: smart contract code structure analysis (Windows system)

1. Find the smart contract path deployed earlier:

 2.go.mod: Modular development of Go language

Modularity in Fabric smart contracts (chain codes) is a must, otherwise smart contracts cannot be deployed

Previously, Go code could only be written under GOPATH, after enabling modularization, it can be written anywhere

Use the command line: go mod init + module name to generate go.mod file

When the modular management is turned on, we run the code with: go run + module name instead of a file with a dot suffix.

 3.go.sum: Help us manage the current module's dependence on other third parties

 (For example: we have introduced some dependencies, but they are not actually used, then using the go mod tidy command to generate go.sum can help us clear those unused third-party libraries; but there are some that need to be used that have not been downloaded , it will automatically download to the local for us)

4. Smart contract related code

 (1) The warehouse referenced in the assetTransfer.go code

 The three warehouses required to develop the smart contract of Hyperledger: fabric-chaincode-go, fabric-contract-api-go, fabric-protos-go (pay attention to GO111MODULE ="auto" when pulling, and ensure that the network is good )

(2) main function

The role of the main function here: create a chaincode object; start the chaincode (starting the chaincode here is to start after the chaincode is deployed)

The error on line 16 is an error in creating the asset transfer base chain code; the error on line 20 is an error in starting the asset transfer base chain code.

(3) SmartContract object

In modular development, it is necessary to refer to the chanincode package by import to call the SmartContract object

 (4) InitLedger() method

Initialize the ledger : Create a series of related data and store these data in the super ledger

Tip: If you want to interact with the blockchain or access the blockchain network, let the method be bound to the smart contract structure.

World State maintains the current state of variables for each particular chaincode. The two types of world state databases currently supported by Fabric include LevelDB and CouchDB. LevelDB is the default key-value database based on Fabric Peer, while CouchDB is a JSON-based database that supports rich query operations based on JSON objects. 

(5) CreateAsset() method

CreateAsset : Emits a new Asset to the world state with the given details.

(6) ReadAsset () method

Read Asset : Returns the asset stored in the world state for the given id.

 (7) UpdateAsset() method

Update Asset : Updates an existing asset in the world state with the provided parameters.

(8) DeleteAsset() method

Remove Asset : Removes the given asset from the world state.

 (9) AssetExists () method

Determine whether the asset exists: the asset with the given ID exists in the world state

 (10) TransferAsset () method

Asset Transfer: Update the owner field of the asset with the given id in the world state

 (11) GetAllAssets () method

Get List of All Assets: Returns all assets found in the world state

 5. smartcontract_test.go test file

For testing the smartcontract.go file

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/126764869