Skip to content

Commit 4bfba4f

Browse files
author
siovanus
committed
fix examples chaincode, drop Tables
Change-Id: Id9498bdceda5ae6c2fc9ef9431f08b6ced78dc3a Signed-off-by: siovanus <[email protected]>
1 parent 99f50e8 commit 4bfba4f

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

examples/chaincode/go/asset_management_with_roles/asset_management_with_roles.go

+24-56
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/op/go-logging"
2929
)
3030

31-
var myLogger = logging.MustGetLogger("asset_mgm")
31+
var myLogger = logging.MustGetLogger("asset_mgmt")
3232

3333
// AssetManagementChaincode example simple Asset Management Chaincode implementation
3434
// with access control enforcement at chaincode level.
@@ -65,15 +65,6 @@ func (t *AssetManagementChaincode) Init(stub shim.ChaincodeStubInterface) pb.Res
6565
return shim.Error("Incorrect number of arguments. Expecting 0")
6666
}
6767

68-
// Create ownership table
69-
err := stub.CreateTable("AssetsOwnership", []*shim.ColumnDefinition{
70-
&shim.ColumnDefinition{Name: "Asset", Type: shim.ColumnDefinition_STRING, Key: true},
71-
&shim.ColumnDefinition{Name: "Owner", Type: shim.ColumnDefinition_BYTES, Key: false},
72-
})
73-
if err != nil {
74-
return shim.Error(fmt.Sprintf("Failed creating AssetsOnwership table, [%v]", err))
75-
}
76-
7768
// Set the role of the users that are allowed to assign assets
7869
// The metadata will contain the role of the users that are allowed to assign assets
7970
assignerRole, err := stub.GetCallerMetadata()
@@ -134,19 +125,19 @@ func (t *AssetManagementChaincode) assign(stub shim.ChaincodeStubInterface, args
134125
}
135126

136127
// Register assignment
128+
// 1.check if the Asset exists, if exists, return error
137129
myLogger.Debugf("New owner of [%s] is [% x]", asset, owner)
138130

139-
ok, err := stub.InsertRow("AssetsOwnership", shim.Row{
140-
Columns: []*shim.Column{
141-
&shim.Column{Value: &shim.Column_String_{String_: asset}},
142-
&shim.Column{Value: &shim.Column_Bytes{Bytes: account}}},
143-
})
144-
145-
if !ok && err == nil {
146-
fmt.Println("Error inserting row")
147-
return shim.Error("Asset was already assigned.")
131+
value, err := stub.GetState(asset)
132+
if err != nil {
133+
return shim.Error(err.Error())
134+
}
135+
if value != nil {
136+
return shim.Error(fmt.Sprintf("Asset %s is already assigned.", asset))
148137
}
149138

139+
// 2.insert into state
140+
err = stub.PutState(asset, account)
150141
if err != nil {
151142
return shim.Error(err.Error())
152143
}
@@ -169,20 +160,14 @@ func (t *AssetManagementChaincode) transfer(stub shim.ChaincodeStubInterface, ar
169160

170161
// Verify the identity of the caller
171162
// Only the owner can transfer one of his assets
172-
var columns []shim.Column
173-
col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}
174-
columns = append(columns, col1)
175-
176-
row, err := stub.GetRow("AssetsOwnership", columns)
163+
prvOwner, err := stub.GetState(asset)
177164
if err != nil {
178-
return shim.Error(fmt.Sprintf("Failed retrieving asset [%s]: [%s]", asset, err))
165+
return shim.Error(err.Error())
179166
}
180-
181-
prvOwner := row.Columns[1].GetBytes()
182-
myLogger.Debugf("Previous owener of [%s] is [% x]", asset, prvOwner)
183-
if len(prvOwner) == 0 {
167+
if prvOwner == nil {
184168
return shim.Error("Invalid previous owner. Nil")
185169
}
170+
myLogger.Debugf("Previous owener of [%s] is [% x]", asset, prvOwner)
186171

187172
// Verify ownership
188173
callerAccount, err := impl.NewAccessControlShim(stub).ReadCertAttribute("account")
@@ -200,24 +185,14 @@ func (t *AssetManagementChaincode) transfer(stub shim.ChaincodeStubInterface, ar
200185
}
201186

202187
// At this point, the proof of ownership is valid, then register transfer
203-
err = stub.DeleteRow(
204-
"AssetsOwnership",
205-
[]shim.Column{shim.Column{Value: &shim.Column_String_{String_: asset}}},
206-
)
188+
err = stub.DelState(asset)
207189
if err != nil {
208-
return shim.Error("Failed deliting row.")
190+
return shim.Error(err.Error())
209191
}
210192

211-
_, err = stub.InsertRow(
212-
"AssetsOwnership",
213-
shim.Row{
214-
Columns: []*shim.Column{
215-
&shim.Column{Value: &shim.Column_String_{String_: asset}},
216-
&shim.Column{Value: &shim.Column_Bytes{Bytes: newOwnerAccount}},
217-
},
218-
})
193+
err = stub.PutState(asset, newOwnerAccount)
219194
if err != nil {
220-
return shim.Error("Failed inserting row.")
195+
return shim.Error(err.Error())
221196
}
222197

223198
return shim.Success(nil)
@@ -253,25 +228,18 @@ func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args
253228

254229
fmt.Printf("ASSET: %v", string(asset))
255230

256-
var columns []shim.Column
257-
col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}
258-
columns = append(columns, col1)
259-
260-
row, err := stub.GetRow("AssetsOwnership", columns)
231+
owner, err := stub.GetState(asset)
261232
if err != nil {
262-
jsonResp := "{\"Error\":\"Failed retrieving asset " + asset + ". Error " + err.Error() + ". \"}"
263-
return shim.Error(jsonResp)
233+
return shim.Error("{\"Error\":\"Failed retrieving asset " + asset + ". Error " + err.Error() + ". \"}")
264234
}
265-
266-
if len(row.Columns) == 0 {
267-
jsonResp := "{\"Error\":\"Failed retrieving owner for " + asset + ". \"}"
268-
return shim.Error(jsonResp)
235+
if owner == nil {
236+
return shim.Error(fmt.Sprintf("{\"Error\":\"Failed retrieving owner for " + asset + ". \"}"))
269237
}
270238

271-
jsonResp := "{\"Owner\":\"" + string(row.Columns[1].GetBytes()) + "\"}"
239+
jsonResp := "{\"Owner\":\"" + string(owner) + "\"}"
272240
fmt.Printf("Query Response:%s\n", jsonResp)
273241

274-
return shim.Success(row.Columns[1].GetBytes())
242+
return shim.Success(owner)
275243
}
276244

277245
func main() {

0 commit comments

Comments
 (0)