什么是智能合约?
智能合约即链码,就是可执行的代码。本章主要以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。