我正在进行一个概念验证,需要存储一些数据,例如对象的ID、价格、所有者等。是否可以使用智能合约将这些东西存储在区块链上?如果不行,有什么方法可以使用区块链来实现呢?(我做了一些研究,人们正在使用区块链在供应链管理行业中。他们一定已经存储了这些类型的数据。)
我正在进行一个概念验证,需要存储一些数据,例如对象的ID、价格、所有者等。是否可以使用智能合约将这些东西存储在区块链上?如果不行,有什么方法可以使用区块链来实现呢?(我做了一些研究,人们正在使用区块链在供应链管理行业中。他们一定已经存储了这些类型的数据。)
// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type Chaincode interface {
// Init is called during Instantiate transaction after the chaincode container
// has been established for the first time, allowing the chaincode to
// initialize its internal data
Init(stub ChaincodeStubInterface) pb.Response
// Invoke is called to update or query the ledger in a proposal transaction.
// Updated state variables are not committed to the ledger until the
// transaction is committed.
Invoke(stub ChaincodeStubInterface) pb.Response
}
type myStoreChaincode struct {
}
func (cc *myStoreChaincode) Init(stub ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
func (cc *myStoreChaincode) Invoke(stub ChaincodeStubInterface) pb.Response {
action, params = stub.GetFunctionAndParameters()
if action == "storeItem" {
cc.StoreItem(stub, params)
}
// Handle here other cases and possible parameters combinations
return shim.Success(nil)
}
func (cc *myStoreChaincode) StoreItem(stub ChaincodeStubInterface, params []string) {
// Store item on ledger, where params[0] is a key and params[1] actual value
stub.PutState(params[0], params[1])
}
这只是一个简化版本,如果您需要更复杂的内容,可以参考 "编写您的应用程序" 教程。
Composer使用建模语言定义网络的特性(成员类型、资产类型、事件和交易定义核心网络)。它具有强大的访问控制语言,可以精确指定谁可以访问哪些资产和哪些交易。它还有一个简化的查询语言,在执行查询时自动调用ACL(这意味着即使您要查看不应看到的信息,也无法看到),最后允许您使用单一语言(目前为JavaScript)编写所有代码,包括智能交易的链码。
在区块链中编码这种类型的数据是完全可能的。如果想详细了解此过程,请考虑访问Github上的这些存储库。
这些文件代表MIT和The Learning Lab在将获得的证书添加到区块链方面所做的努力。证书涉及您要编码的大部分数据,将其分解为多个过程组件:创建、查看、验证和存储证书。
您正在尝试做的事情相当新颖。因此,它可能会非常详细和复杂。MIT的实现很好,因为他们提供了Docker包,可以处理大量细节,从而有机会更全面地了解该过程。祝你好运!