@@ -47,6 +47,13 @@ const(
47
47
contractIdPrefix = "ContractId_" // prefix for the map, contractId --> asset-key
48
48
)
49
49
50
+ // helper functions to log and return errors
51
+ func logThenErrorf (format string , args ... interface {}) error {
52
+ errorMsg := fmt .Sprintf (format , args ... )
53
+ log .Error (errorMsg )
54
+ return errors .New (errorMsg )
55
+ }
56
+
50
57
// function to generate a "SHA256" hash in base64 format for a given preimage
51
58
func generateSHA256HashInBase64Form (preimage string ) string {
52
59
hasher := sha256 .New ()
@@ -68,9 +75,7 @@ func generateContractIdMapKey(contractId string) string {
68
75
func generateAssetLockKeyAndContractId (ctx contractapi.TransactionContextInterface , assetAgreement * common.AssetExchangeAgreement ) (string , string , error ) {
69
76
assetLockKey , err := ctx .GetStub ().CreateCompositeKey ("AssetExchangeContract" , []string {assetAgreement .Type , assetAgreement .Id })
70
77
if err != nil {
71
- errorMsg := fmt .Sprintf ("error while creating composite key: %+v" , err )
72
- log .Error (errorMsg )
73
- return "" , "" , errors .New (errorMsg )
78
+ return "" , "" , logThenErrorf ("error while creating composite key: %+v" , err )
74
79
}
75
80
76
81
contractId := generateSHA256HashInBase64Form (assetLockKey )
@@ -98,7 +103,7 @@ func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, a
98
103
return "" , err
99
104
}
100
105
//display the requested asset agreement
101
- log .Info ( fmt . Sprintf ( "assetExchangeAgreement: %+v\n " , assetAgreement ) )
106
+ log .Infof ( "assetExchangeAgreement: %+v\n " , assetAgreement )
102
107
103
108
lockInfoHTLC := & common.AssetLockHTLC {}
104
109
err = proto .Unmarshal ([]byte (lockInfoBytes ), lockInfoHTLC )
@@ -107,12 +112,10 @@ func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, a
107
112
return "" , err
108
113
}
109
114
//display the passed lock information
110
- log .Info ( fmt . Sprintf ( "lockInfoHTLC: %+v\n " , lockInfoHTLC ) )
115
+ log .Infof ( "lockInfoHTLC: %+v\n " , lockInfoHTLC )
111
116
112
117
if lockInfoHTLC .TimeSpec != common .AssetLockHTLC_EPOCH {
113
- errorMsg := "only EPOCH time is supported at present"
114
- log .Error (errorMsg )
115
- return "" , errors .New (errorMsg )
118
+ return "" , logThenErrorf ("only EPOCH time is supported at present" )
116
119
}
117
120
118
121
assetLockKey , contractId , err := generateAssetLockKeyAndContractId (ctx , assetAgreement )
@@ -121,7 +124,7 @@ func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, a
121
124
return "" , err
122
125
}
123
126
124
- assetLockVal := AssetLockValue {Locker : assetAgreement .Locker , Recipient : assetAgreement .Recipient , Hash : string (lockInfoHTLC .Hash ), ExpiryTimeSecs : lockInfoHTLC .ExpiryTimeSecs }
127
+ assetLockVal := AssetLockValue {Locker : assetAgreement .Locker , Recipient : assetAgreement .Recipient , Hash : string (lockInfoHTLC .HashBase64 ), ExpiryTimeSecs : lockInfoHTLC .ExpiryTimeSecs }
125
128
126
129
assetLockValBytes , err := ctx .GetStub ().GetState (assetLockKey )
127
130
if err != nil {
@@ -130,16 +133,12 @@ func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, a
130
133
}
131
134
132
135
if assetLockValBytes != nil {
133
- errorMsg := fmt .Sprintf ("asset of type %s and ID %s is already locked" , assetAgreement .Type , assetAgreement .Id )
134
- log .Error (errorMsg )
135
- return "" , errors .New (errorMsg )
136
+ return "" , logThenErrorf ("asset of type %s and ID %s is already locked" , assetAgreement .Type , assetAgreement .Id )
136
137
}
137
138
138
139
assetLockValBytes , err = json .Marshal (assetLockVal )
139
140
if err != nil {
140
- errorMsg := fmt .Sprintf ("marshal error: %s" , err )
141
- log .Error (errorMsg )
142
- return "" , errors .New (errorMsg )
141
+ return "" , logThenErrorf ("marshal error: %+v" , err )
143
142
}
144
143
145
144
err = ctx .GetStub ().PutState (assetLockKey , assetLockValBytes )
@@ -150,9 +149,7 @@ func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, a
150
149
151
150
assetLockKeyBytes , err := json .Marshal (assetLockKey )
152
151
if err != nil {
153
- errorMsg := fmt .Sprintf ("marshal error: %s" , err )
154
- log .Error (errorMsg )
155
- return "" , errors .New (errorMsg )
152
+ return "" , logThenErrorf ("marshal error: %+v" , err )
156
153
}
157
154
158
155
err = ctx .GetStub ().PutState (generateContractIdMapKey (string (contractId )), assetLockKeyBytes )
@@ -173,7 +170,7 @@ func (s *SmartContract) UnLockAsset(ctx contractapi.TransactionContextInterface,
173
170
return err
174
171
}
175
172
//display the requested asset agreement
176
- log .Info ( fmt . Sprintf ( "assetExchangeAgreement: %+v\n " , assetAgreement ) )
173
+ log .Infof ( "assetExchangeAgreement: %+v\n " , assetAgreement )
177
174
178
175
assetLockKey , _ , err := generateAssetLockKeyAndContractId (ctx , assetAgreement )
179
176
if err != nil {
@@ -187,7 +184,7 @@ func (s *SmartContract) UnLockAsset(ctx contractapi.TransactionContextInterface,
187
184
return err
188
185
}
189
186
190
- if assetLockValBytes == nil {
187
+ if assetLockValBytes == nil {
191
188
errorMsg := fmt .Sprintf ("no asset of type %s and ID %s is locked" , assetAgreement .Type , assetAgreement .Id )
192
189
log .Error (errorMsg )
193
190
return errors .New (errorMsg )
@@ -209,7 +206,7 @@ func (s *SmartContract) UnLockAsset(ctx contractapi.TransactionContextInterface,
209
206
210
207
// Check if expiry time is elapsed
211
208
currentTimeSecs := uint64 (time .Now ().Unix ())
212
- if uint64 ( currentTimeSecs ) < assetLockVal .ExpiryTimeSecs {
209
+ if currentTimeSecs < assetLockVal .ExpiryTimeSecs {
213
210
errorMsg := fmt .Sprintf ("cannot unlock asset of type %s and ID %s as the expiry time is not yet elapsed" , assetAgreement .Type , assetAgreement .Id )
214
211
log .Error (errorMsg )
215
212
return errors .New (errorMsg )
@@ -235,7 +232,7 @@ func (s *SmartContract) IsAssetLocked(ctx contractapi.TransactionContextInterfac
235
232
return false , err
236
233
}
237
234
//display the requested asset agreement
238
- log .Info ( fmt . Sprintf ( "assetExchangeAgreement: %+v\n " , assetAgreement ) )
235
+ log .Infof ( "assetExchangeAgreement: %+v\n " , assetAgreement )
239
236
240
237
assetLockKey , _ , err := generateAssetLockKeyAndContractId (ctx , assetAgreement )
241
238
if err != nil {
@@ -262,11 +259,11 @@ func (s *SmartContract) IsAssetLocked(ctx contractapi.TransactionContextInterfac
262
259
log .Error (errorMsg )
263
260
return false , errors .New (errorMsg )
264
261
}
265
- log .Info ( fmt . Sprintf ( "assetLockVal: %+v\n " , assetLockVal ) )
262
+ log .Infof ( "assetLockVal: %+v\n " , assetLockVal )
266
263
267
264
// Check if expiry time is elapsed
268
265
currentTimeSecs := uint64 (time .Now ().Unix ())
269
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
266
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
270
267
errorMsg := fmt .Sprintf ("expiry time for asset of type %s and ID %s is already elapsed" , assetAgreement .Type , assetAgreement .Id )
271
268
log .Error (errorMsg )
272
269
return false , errors .New (errorMsg )
@@ -307,9 +304,9 @@ func checkIfCorrectPreimage(preimageBase64 string, hashBase64 string) (bool, err
307
304
308
305
shaHashBase64 := generateSHA256HashInBase64Form (string (preimage ))
309
306
if shaHashBase64 == hashBase64 {
310
- log .Info ( fmt . Sprintf ( "%s: preimage %s is passed correctly.\n " , funName , preimage ) )
307
+ log .Infof ( "%s: preimage %s is passed correctly.\n " , funName , preimage )
311
308
} else {
312
- log .Info ( fmt . Sprintf ( "%s: preimage %s is not passed correctly.\n " , funName , preimage ) )
309
+ log .Infof ( "%s: preimage %s is not passed correctly.\n " , funName , preimage )
313
310
return false , nil
314
311
}
315
312
return true , nil
@@ -325,7 +322,7 @@ func (s *SmartContract) ClaimAsset(ctx contractapi.TransactionContextInterface,
325
322
return err
326
323
}
327
324
// display the requested asset agreement
328
- log .Info ( fmt . Sprintf ( "assetExchangeAgreement: %+v\n " , assetAgreement ) )
325
+ log .Infof ( "assetExchangeAgreement: %+v\n " , assetAgreement )
329
326
330
327
claimInfo := & common.AssetClaimHTLC {}
331
328
err = proto .Unmarshal ([]byte (claimInfoBytes ), claimInfo )
@@ -336,7 +333,7 @@ func (s *SmartContract) ClaimAsset(ctx contractapi.TransactionContextInterface,
336
333
}
337
334
338
335
// display the claim information
339
- log .Info ( fmt . Sprintf ( "claimInfo: %+v\n " , claimInfo ) )
336
+ log .Infof ( "claimInfo: %+v\n " , claimInfo )
340
337
341
338
assetLockKey , _ , err := generateAssetLockKeyAndContractId (ctx , assetAgreement )
342
339
if err != nil {
@@ -372,14 +369,14 @@ func (s *SmartContract) ClaimAsset(ctx contractapi.TransactionContextInterface,
372
369
373
370
// Check if expiry time is elapsed
374
371
currentTimeSecs := uint64 (time .Now ().Unix ())
375
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
372
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
376
373
errorMsg := fmt .Sprintf ("cannot claim asset of type %s and ID %s as the expiry time is already elapsed" , assetAgreement .Type , assetAgreement .Id )
377
374
log .Error (errorMsg )
378
375
return errors .New (errorMsg )
379
376
}
380
377
381
378
// compute the hash from the preimage
382
- isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimage ), string (assetLockVal .Hash ))
379
+ isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimageBase64 ), string (assetLockVal .Hash ))
383
380
if err != nil {
384
381
errorMsg := fmt .Sprintf ("claim asset of type %s and ID %s error: %v" , assetAgreement .Type , assetAgreement .Id , err )
385
382
log .Error (errorMsg )
@@ -452,7 +449,7 @@ func (s *SmartContract) UnLockAssetUsingContractId(ctx contractapi.TransactionCo
452
449
453
450
// Check if expiry time is elapsed
454
451
currentTimeSecs := uint64 (time .Now ().Unix ())
455
- if uint64 ( currentTimeSecs ) < assetLockVal .ExpiryTimeSecs {
452
+ if currentTimeSecs < assetLockVal .ExpiryTimeSecs {
456
453
errorMsg := fmt .Sprintf ("cannot unlock asset associated with the contractId %s as the expiry time is not yet elapsed" , contractId )
457
454
log .Error (errorMsg )
458
455
return errors .New (errorMsg )
@@ -493,18 +490,18 @@ func (s *SmartContract) ClaimAssetUsingContractId(ctx contractapi.TransactionCon
493
490
}
494
491
495
492
// display the claim information
496
- log .Info ( fmt . Sprintf ( "claimInfo: %+v\n " , claimInfo ) )
493
+ log .Infof ( "claimInfo: %+v\n " , claimInfo )
497
494
498
495
// Check if expiry time is elapsed
499
496
currentTimeSecs := uint64 (time .Now ().Unix ())
500
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
497
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
501
498
errorMsg := fmt .Sprintf ("cannot claim asset associated with contractId %s as the expiry time is already elapsed" , contractId )
502
499
log .Error (errorMsg )
503
500
return errors .New (errorMsg )
504
501
}
505
502
506
503
// compute the hash from the preimage
507
- isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimage ), string (assetLockVal .Hash ))
504
+ isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimageBase64 ), string (assetLockVal .Hash ))
508
505
if err != nil {
509
506
errorMsg := fmt .Sprintf ("claim asset associated with contractId %s failed with error: %v" , contractId , err )
510
507
log .Error (errorMsg )
@@ -545,7 +542,7 @@ func (s *SmartContract) IsAssetLockedQueryUsingContractId(ctx contractapi.Transa
545
542
546
543
// Check if expiry time is elapsed
547
544
currentTimeSecs := uint64 (time .Now ().Unix ())
548
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
545
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
549
546
errorMsg := fmt .Sprintf ("expiry time for asset associated with contractId %s is already elapsed" , contractId )
550
547
log .Error (errorMsg )
551
548
return false , errors .New (errorMsg )
@@ -577,7 +574,7 @@ func (s *SmartContract) LockFungibleAsset(ctx contractapi.TransactionContextInte
577
574
}
578
575
579
576
//display the passed lock information
580
- log .Info ( fmt . Sprintf ( "lockInfoHTLC: %+v\n " , lockInfoHTLC ) )
577
+ log .Infof ( "lockInfoHTLC: %+v\n " , lockInfoHTLC )
581
578
582
579
if lockInfoHTLC .TimeSpec != common .AssetLockHTLC_EPOCH {
583
580
errorMsg := "only EPOCH time is supported at present"
@@ -589,7 +586,7 @@ func (s *SmartContract) LockFungibleAsset(ctx contractapi.TransactionContextInte
589
586
contractId := generateFungibleAssetLockContractId (ctx , assetAgreement )
590
587
591
588
assetLockVal := FungibleAssetLockValue {Type : assetAgreement .Type , NumUnits : assetAgreement .NumUnits , Locker : assetAgreement .Locker ,
592
- Recipient : assetAgreement .Recipient , Hash : string (lockInfoHTLC .Hash ), ExpiryTimeSecs : lockInfoHTLC .ExpiryTimeSecs }
589
+ Recipient : assetAgreement .Recipient , Hash : string (lockInfoHTLC .HashBase64 ), ExpiryTimeSecs : lockInfoHTLC .ExpiryTimeSecs }
593
590
594
591
assetLockValBytes , err := ctx .GetStub ().GetState (contractId )
595
592
if err != nil {
@@ -660,7 +657,7 @@ func (s *SmartContract) IsFungibleAssetLocked(ctx contractapi.TransactionContext
660
657
661
658
// Check if expiry time is elapsed
662
659
currentTimeSecs := uint64 (time .Now ().Unix ())
663
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
660
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
664
661
errorMsg := fmt .Sprintf ("expiry time for fungible asset associated with contractId %s is already elapsed" , contractId )
665
662
log .Error (errorMsg )
666
663
return false , errors .New (errorMsg )
@@ -687,18 +684,18 @@ func (s *SmartContract) ClaimFungibleAsset(ctx contractapi.TransactionContextInt
687
684
}
688
685
689
686
// display the claim information
690
- log .Info ( fmt . Sprintf ( "claimInfo: %+v\n " , claimInfo ) )
687
+ log .Infof ( "claimInfo: %+v\n " , claimInfo )
691
688
692
689
// Check if expiry time is elapsed
693
690
currentTimeSecs := uint64 (time .Now ().Unix ())
694
- if uint64 ( currentTimeSecs ) >= assetLockVal .ExpiryTimeSecs {
691
+ if currentTimeSecs >= assetLockVal .ExpiryTimeSecs {
695
692
errorMsg := fmt .Sprintf ("cannot claim fungible asset associated with contractId %s as the expiry time is already elapsed" , contractId )
696
693
log .Error (errorMsg )
697
694
return errors .New (errorMsg )
698
695
}
699
696
700
697
// compute the hash from the preimage
701
- isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimage ), string (assetLockVal .Hash ))
698
+ isCorrectPreimage , err := checkIfCorrectPreimage (string (claimInfo .HashPreimageBase64 ), string (assetLockVal .Hash ))
702
699
if err != nil {
703
700
errorMsg := fmt .Sprintf ("claim fungible asset associated with contractId %s failed with error: %v" , contractId , err )
704
701
log .Error (errorMsg )
@@ -732,7 +729,7 @@ func (s *SmartContract) UnLockFungibleAsset(ctx contractapi.TransactionContextIn
732
729
733
730
// Check if expiry time is elapsed
734
731
currentTimeSecs := uint64 (time .Now ().Unix ())
735
- if uint64 ( currentTimeSecs ) < assetLockVal .ExpiryTimeSecs {
732
+ if currentTimeSecs < assetLockVal .ExpiryTimeSecs {
736
733
errorMsg := fmt .Sprintf ("cannot unlock fungible asset associated with the contractId %s as the expiry time is not yet elapsed" , contractId )
737
734
log .Error (errorMsg )
738
735
return errors .New (errorMsg )
0 commit comments