@@ -19,15 +19,10 @@ package main
19
19
import (
20
20
"errors"
21
21
22
- "github.com/hyperledger/fabric/core/chaincode/shim "
23
- )
22
+ "encoding/json "
23
+ "fmt"
24
24
25
- // consts associated with chaincode table
26
- const (
27
- tableColumn = "AssetsOwnership"
28
- columnAccountID = "Account"
29
- columnContactInfo = "ContactInfo"
30
- columnAmount = "Amount"
25
+ "github.com/hyperledger/fabric/core/chaincode/shim"
31
26
)
32
27
33
28
//DepositoryHandler provides APIs used to perform operations on CC's KV store
@@ -39,17 +34,10 @@ func NewDepositoryHandler() *depositoryHandler {
39
34
return & depositoryHandler {}
40
35
}
41
36
42
- // createTable initiates a new asset depository table in the chaincode state
43
- // stub: chaincodestub
44
- func (t * depositoryHandler ) createTable (stub shim.ChaincodeStubInterface ) error {
45
-
46
- // Create asset depository table
47
- return stub .CreateTable (tableColumn , []* shim.ColumnDefinition {
48
- & shim.ColumnDefinition {Name : columnAccountID , Type : shim .ColumnDefinition_STRING , Key : true },
49
- & shim.ColumnDefinition {Name : columnContactInfo , Type : shim .ColumnDefinition_STRING , Key : false },
50
- & shim.ColumnDefinition {Name : columnAmount , Type : shim .ColumnDefinition_UINT64 , Key : false },
51
- })
52
-
37
+ type depositoryAccount struct {
38
+ AccountID string `json:"account_id"`
39
+ ContactInfo string `json:"contact_info"`
40
+ Amount uint64 `json:"amount"`
53
41
}
54
42
55
43
// assign allocates assets to account IDs in the chaincode state for each of the
@@ -64,21 +52,27 @@ func (t *depositoryHandler) assign(stub shim.ChaincodeStubInterface,
64
52
65
53
myLogger .Debugf ("insert accountID= %v" , accountID )
66
54
67
- //insert a new row for this account ID that includes contact information and balance
68
- ok , err := stub .InsertRow (tableColumn , shim.Row {
69
- Columns : []* shim.Column {
70
- & shim.Column {Value : & shim.Column_String_ {String_ : accountID }},
71
- & shim.Column {Value : & shim.Column_String_ {String_ : contactInfo }},
72
- & shim.Column {Value : & shim.Column_Uint64 {Uint64 : amount }}},
73
- })
74
-
75
55
// you can only assign balances to new account IDs
76
- if ! ok && err == nil {
56
+ accountBytes , err := stub .GetState (accountID )
57
+ if err == nil && len (accountBytes ) > 0 {
77
58
myLogger .Errorf ("system error %v" , err )
78
59
return errors .New ("Asset was already assigned." )
79
60
}
80
61
81
- return nil
62
+ account := depositoryAccount {
63
+ AccountID : accountID ,
64
+ ContactInfo : contactInfo ,
65
+ Amount : amount ,
66
+ }
67
+ accountBytes , err = json .Marshal (account )
68
+ if err != nil {
69
+ myLogger .Errorf ("account marshaling error %v" , err )
70
+ return errors .New ("Failed to serialize account info." + err .Error ())
71
+ }
72
+
73
+ //update this account that includes contact information and balance
74
+ err = stub .PutState (accountID , accountBytes )
75
+ return err
82
76
}
83
77
84
78
// updateAccountBalance updates the balance amount of an account ID
@@ -94,18 +88,20 @@ func (t *depositoryHandler) updateAccountBalance(stub shim.ChaincodeStubInterfac
94
88
myLogger .Debugf ("insert accountID= %v" , accountID )
95
89
96
90
//replace the old record row associated with the account ID with the new record row
97
- ok , err := stub .ReplaceRow (tableColumn , shim.Row {
98
- Columns : []* shim.Column {
99
- & shim.Column {Value : & shim.Column_String_ {String_ : accountID }},
100
- & shim.Column {Value : & shim.Column_String_ {String_ : contactInfo }},
101
- & shim.Column {Value : & shim.Column_Uint64 {Uint64 : amount }}},
102
- })
103
-
104
- if ! ok && err == nil {
105
- myLogger .Errorf ("system error %v" , err )
106
- return errors .New ("failed to replace row with account Id." + accountID )
91
+ account := depositoryAccount {
92
+ AccountID : accountID ,
93
+ ContactInfo : contactInfo ,
94
+ Amount : amount ,
107
95
}
108
- return nil
96
+ accountBytes , err := json .Marshal (account )
97
+ if err != nil {
98
+ myLogger .Errorf ("account marshaling error %v" , err )
99
+ return errors .New ("Failed to serialize account info." + err .Error ())
100
+ }
101
+
102
+ //update this account that includes contact information and balance
103
+ err = stub .PutState (accountID , accountBytes )
104
+ return err
109
105
}
110
106
111
107
// deleteAccountRecord deletes the record row associated with an account ID on the chaincode state table
@@ -116,10 +112,7 @@ func (t *depositoryHandler) deleteAccountRecord(stub shim.ChaincodeStubInterface
116
112
myLogger .Debugf ("insert accountID= %v" , accountID )
117
113
118
114
//delete record matching account ID passed in
119
- err := stub .DeleteRow (
120
- "AssetsOwnership" ,
121
- []shim.Column {shim.Column {Value : & shim.Column_String_ {String_ : accountID }}},
122
- )
115
+ err := stub .DelState (accountID )
123
116
124
117
if err != nil {
125
118
myLogger .Errorf ("system error %v" , err )
@@ -179,12 +172,12 @@ func (t *depositoryHandler) transfer(stub shim.ChaincodeStubInterface, fromAccou
179
172
// stub: chaincodestub
180
173
// accountID: account ID
181
174
func (t * depositoryHandler ) queryContactInfo (stub shim.ChaincodeStubInterface , accountID string ) (string , error ) {
182
- row , err := t .queryTable (stub , accountID )
175
+ account , err := t .queryTable (stub , accountID )
183
176
if err != nil {
184
177
return "" , err
185
178
}
186
179
187
- return row . Columns [ 1 ]. GetString_ () , nil
180
+ return account . ContactInfo , nil
188
181
}
189
182
190
183
// queryBalance queries the balance information matching a correponding account ID on the chaincode state table
@@ -194,40 +187,43 @@ func (t *depositoryHandler) queryBalance(stub shim.ChaincodeStubInterface, accou
194
187
195
188
myLogger .Debugf ("insert accountID= %v" , accountID )
196
189
197
- row , err := t .queryTable (stub , accountID )
190
+ account , err := t .queryTable (stub , accountID )
198
191
if err != nil {
199
192
return 0 , err
200
193
}
201
- if len (row .Columns ) == 0 || row .Columns [2 ] == nil {
202
- return 0 , errors .New ("row or column value not found" )
203
- }
204
194
205
- return row . Columns [ 2 ]. GetUint64 () , nil
195
+ return account . Amount , nil
206
196
}
207
197
208
198
// queryAccount queries the balance and contact information matching a correponding account ID on the chaincode state table
209
199
// stub: chaincodestub
210
200
// accountID: account ID
211
201
func (t * depositoryHandler ) queryAccount (stub shim.ChaincodeStubInterface , accountID string ) (string , uint64 , error ) {
212
- row , err := t .queryTable (stub , accountID )
202
+ account , err := t .queryTable (stub , accountID )
213
203
if err != nil {
214
204
return "" , 0 , err
215
205
}
216
- if len (row .Columns ) == 0 || row .Columns [2 ] == nil {
217
- return "" , 0 , errors .New ("row or column value not found" )
218
- }
219
206
220
- return row . Columns [ 1 ]. GetString_ (), row . Columns [ 2 ]. GetUint64 () , nil
207
+ return account . ContactInfo , account . Amount , nil
221
208
}
222
209
223
210
// queryTable returns the record row matching a correponding account ID on the chaincode state table
224
211
// stub: chaincodestub
225
212
// accountID: account ID
226
- func (t * depositoryHandler ) queryTable (stub shim.ChaincodeStubInterface , accountID string ) (shim. Row , error ) {
213
+ func (t * depositoryHandler ) queryTable (stub shim.ChaincodeStubInterface , accountID string ) (* depositoryAccount , error ) {
227
214
228
- var columns []shim.Column
229
- col1 := shim.Column {Value : & shim.Column_String_ {String_ : accountID }}
230
- columns = append (columns , col1 )
215
+ accountBytes , err := stub .GetState (accountID )
216
+ if err != nil {
217
+ return nil , errors .New ("Failed to get account." + err .Error ())
218
+ }
219
+ if len (accountBytes ) == 0 {
220
+ return nil , fmt .Errorf ("Account %s not exists." , accountID )
221
+ }
231
222
232
- return stub .GetRow (tableColumn , columns )
223
+ account := & depositoryAccount {}
224
+ err = json .Unmarshal (accountBytes , account )
225
+ if err != nil {
226
+ return nil , errors .New ("Failed to parse account Info. " + err .Error ())
227
+ }
228
+ return account , nil
233
229
}
0 commit comments