我是区块链的新手,并且使用超级边缘结构(现在为v:0.6)来创建一个学习视角的应用程序。
我在区块链上保留金融交易分类账,一旦交易发生(基于网络的组件通知交易发生并调用链码)。
交易结构如下所示:
type Transactions struct {
ReferenceNumber string `json:"ReferenceNumber"`
BillNumber string `json:"BillNumber"`
BillingCompany string `json:"BillingCompany"`
Amount string `json:"Amount"`
Status string `json:"Status"`
}
我json编组并将其保存到以ReferenceNumber为键的状态。
现在我可以在ReferenceNumber的基础上从州获得交易。但是如果我想根据让我们说“状态”的话来从州获得交易怎么办?比如分类帐上有多少交易的状态为'已核对'。
有没有办法根据键而不是值来查询状态?
答案 0 :(得分:2)
Worldstate级别存储在{key,value}级别工作。显而易见,它仅用于指定键的单个值查找。我认为你正在寻找的是要求更高级别的WorldState抽象级别 - 称为Table构造。 fabric / examples / chaincode / go / asset_management_interactive / asset_management.go 有一个示例,说明如何使用所需的列创建表。 在定义数据结构的主键以保存事务时,您将Status作为其中一个键,并且您也可以根据状态检索数据。
创建表的一些示例代码如下所示
func createTableTwo(stub shim.ChaincodeStubInterface) error {
var columnDefsTableTwo []*shim.ColumnDefinition
columnOneTableTwoDef := shim.ColumnDefinition{Name: "colOneTableTwo",
Type: shim.ColumnDefinition_STRING, Key: true}
columnTwoTableTwoDef := shim.ColumnDefinition{Name: "colTwoTableTwo",
Type: shim.ColumnDefinition_INT32, Key: false}
columnThreeTableTwoDef := shim.ColumnDefinition{Name: "colThreeTableThree",
Type: shim.ColumnDefinition_INT32, Key: true}
columnFourTableTwoDef := shim.ColumnDefinition{Name: "colFourTableFour",
Type: shim.ColumnDefinition_STRING, Key: true}
columnDefsTableTwo = append(columnDefsTableTwo, &columnOneTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnTwoTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnThreeTableTwoDef)
columnDefsTableTwo = append(columnDefsTableTwo, &columnFourTableTwoDef)
return stub.CreateTable("tableTwo", columnDefsTableTwo)
}
现在将数据插入此表中,如图所示
if len(args) < 4 {
return nil, errors.New("insertRowTableTwo failed. Must include 4 column values")
}
col1Val := args[0]
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableTwo failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col3Int, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return nil, errors.New("insertRowTableTwo failed. arg[2] must be convertable to int32")
}
col3Val := int32(col3Int)
col4Val := args[3]
var columns []*shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
col3 := shim.Column{Value: &shim.Column_Int32{Int32: col3Val}}
col4 := shim.Column{Value: &shim.Column_String_{String_: col4Val}}
columns = append(columns, &col1)
columns = append(columns, &col2)
columns = append(columns, &col3)
columns = append(columns, &col4)
row := shim.Row{Columns: columns}
ok, err := stub.InsertRow("tableTwo", row)
if err != nil {
return nil, fmt.Errorf("insertRowTableTwo operation failed. %s", err)
}
if !ok {
return nil, errors.New("insertRowTableTwo operation failed. Row with given key already exists")
}
现在通过不指定所有键来查询此数据,请执行以下操作
if len(args) < 1 {
return nil, errors.New("getRowsTableTwo failed. Must include at least key values")
}
var columns []shim.Column
col1Val := args[0]
col1 := shim.Column{Value: &shim.Column_String_{String_: col1Val}}
columns = append(columns, col1)
if len(args) > 1 {
col2Int, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return nil, errors.New("getRowsTableTwo failed. arg[1] must be convertable to int32")
}
col2Val := int32(col2Int)
col2 := shim.Column{Value: &shim.Column_Int32{Int32: col2Val}}
columns = append(columns, col2)
}
rowChannel, err := stub.GetRows("tableTwo", columns)
if err != nil {
return nil, fmt.Errorf("getRowsTableTwo operation failed. %s", err)
}
var rows []shim.Row
for {
select {
case row, ok := <-rowChannel:
if !ok {
rowChannel = nil
} else {
rows = append(rows, row)
}
}
if rowChannel == nil {
break
}
}
jsonRows, err := json.Marshal(rows)
if err != nil {
return nil, fmt.Errorf("getRowsTableTwo operation failed. Error marshaling JSON: %s", err)
}
return jsonRows, nil
插入数据后,API stub.GetRows(“tableTwo”,列)可让您在不指定所有键列的情况下检索它。
上面的代码是从文件中引用的,该文件存在于以前路径中的Fabric github repo中gerrit / src / github.com / hyperledger / fabric / bddtests / chaincode / go / table / table.go
希望这会有所帮助。
答案 1 :(得分:0)
在Hyperledger Fabric v1.0中,您可以将数据建模为JSON,并将CouchDB用作状态数据库。在这种情况下,您可以直接查询JSON的任何字段。
marbles02 chaincode中有一个这样的数据模式的例子。
There are various other ledger and state database options in v1.0