1. 程式人生 > 其它 >Hyperledger Fabric入門(3)-智慧合約

Hyperledger Fabric入門(3)-智慧合約

什麼是智慧合約?
智慧合約即鏈碼,就是可執行的程式碼。本章主要以fabric-samples/asset-transfer-basic/chaincode-go工程,瞭解具體什麼是智慧合約。

進入目錄/usr/local/go/src/github.com/hyperledger/fabric-samples/asset-transfer-basic。
在這裡插入圖片描述
我們可以看到
chaincode-就是智慧合約;
application-就是應用程式。
可以java、go、js語言進行開發。
我們以chaincode-go工程為例,瞭解一下智慧合約。

主要看
/usr/local/go/src/github.com/hyperledger fabric-samples/asset-transfer-basic/chaincode-go/chaincode目錄下的smartcontract.go原始檔。

在這裡插入圖片描述
開啟原始檔我們可以看到主要包含的函式:

// InitLedger adds a base set of assets to the ledger
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error

// CreateAsset issues a new asset to the world state with given details.
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface,
id string, color string, size int, owner string, appraisedValue int) error // ReadAsset returns the asset stored in the world state with given id. func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, id string) (*Asset, error) // UpdateAsset updates an existing asset in the world state with provided parameters.
func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) error // DeleteAsset deletes an given asset from the world state. func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, id string) error // AssetExists returns true when asset with given ID exists in world state func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) // GetAllAssets returns all assets found in world state func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Asset, error)

可以看到在chaincode-go這個工程中,智慧合約其實就是對資料的CRUD。