@@ -12,97 +12,86 @@ import (
1212
1313 "github.com/hyperledger/fabric-chaincode-go/shim"
1414 pb "github.com/hyperledger/fabric-protos-go/peer"
15+ "github.com/pkg/errors"
1516)
1617
17- var SingleKey = "SingleKey"
18-
1918type SkvsStubInterface struct {
2019 * FpcStubInterface
20+ allDataOld map [string ]string
21+ allDataNew map [string ]string
22+ key string
2123}
2224
2325func NewSkvsStubInterface (stub shim.ChaincodeStubInterface , input * pb.ChaincodeInput , rwset * readWriteSet , sep StateEncryptionFunctions ) * SkvsStubInterface {
26+ logger .Warning ("==== Get New Skvs Interface =====" )
2427 fpcStub := NewFpcStubInterface (stub , input , rwset , sep )
25- return & SkvsStubInterface {fpcStub }
26- }
27-
28- func (s * SkvsStubInterface ) GetState (key string ) ([]byte , error ) {
29- logger .Warningf ("Calling Get State (Start), key = %s" , key )
30- encValue , err := s .GetPublicState (SingleKey )
28+ skvsStub := SkvsStubInterface {fpcStub , map [string ]string {}, map [string ]string {}, "SKVS" }
29+ err := skvsStub .InitSKVS ()
3130 if err != nil {
32- return nil , err
31+ logger . Warningf ( "Error!! Initializing SKVS failed" )
3332 }
34-
35- // in case the key does not exist, return early
36- if len (encValue ) == 0 {
37- logger .Warningf ("Calling Get State (End), data empty return." )
38- return nil , nil
39- }
40-
41- value , err := s .sep .DecryptState (encValue )
42- if err != nil {
43- return nil , err
44- }
45-
46- // Create an interface{} value to hold the unmarshalled data
47- var allData map [string ]string
48-
49- // Unmarshal the JSON data into the interface{} value
50- err = json .Unmarshal (value , & allData )
51- if err != nil {
52- logger .Errorf ("SKVS Json unmarshal error: %s" , err )
53- return nil , err
54- }
55- logger .Warningf ("Calling Get State (Mid), key = %s, decrypted done alldata = %s" , key , allData )
56-
57- targetHex := allData [key ]
58- targetBytes , err := hex .DecodeString (targetHex )
59- logger .Warningf ("Calling Get State (End), Target: %s, TargetBytes: %x, err: %s" , targetHex , targetBytes , err )
60- return targetBytes , err
61- // return s.sep.DecryptState(encValue)
33+ return & skvsStub
6234}
6335
64- func (s * SkvsStubInterface ) PutState (key string , value []byte ) error {
65- logger .Warningf ("Calling Put State (Start), key = %s, value = %x" , key , value )
66- // grab all data from the state.
67- encAllValue , err := s .GetPublicState (SingleKey )
36+ func (s * SkvsStubInterface ) InitSKVS () error {
37+ logger .Warningf (" === Initializing SKVS === " )
38+
39+ // get current state, this will only operate once
40+ encValue , err := s .GetPublicState (s .key )
6841 if err != nil {
69- return err
42+ return nil
7043 }
71- var allData map [string ]string
7244
73- if len (encAllValue ) == 0 {
74- // current world state is empty
75- allData = map [string ]string {}
45+ if len (encValue ) == 0 {
46+ logger .Warningf ("SKVS is empty, Initiating." )
7647 } else {
77- allValue , err := s .sep .DecryptState (encAllValue )
48+ value , err := s .sep .DecryptState (encValue )
7849 if err != nil {
7950 return err
8051 }
81- // Unmarshal the JSON data into the interface{} value
82- err = json .Unmarshal (allValue , & allData )
52+ logger .Warningf ("SKVS has default value, loading current value." )
53+
54+ err = json .Unmarshal (value , & s .allDataOld )
55+ err = json .Unmarshal (value , & s .allDataNew )
8356 if err != nil {
8457 logger .Errorf ("SKVS Json unmarshal error: %s" , err )
8558 return err
8659 }
8760 }
88- logger .Warningf ("Calling Put State (Mid-1), decrypt succeed, allData = %s" , allData )
8961
90- valueHex := hex .EncodeToString (value )
62+ logger .Warningf ("SKVS Init finish, allDataOld = %s, allDataNew = %s" , s .allDataOld , s .allDataNew )
63+ return nil
64+ }
65+
66+ func (s * SkvsStubInterface ) GetState (key string ) ([]byte , error ) {
67+ logger .Warningf ("Calling Get State (Start), key = %s, alldataOld = %s" , key , s .allDataOld )
68+ targetHex , found := s .allDataOld [key ]
69+ if found != true {
70+ return nil , errors .New ("skvs allDataOld key not found" )
71+ }
72+ targetBytes , err := hex .DecodeString (targetHex )
73+ logger .Warningf ("Calling Get State (End), TargetHex: %s, TargetBytes: %x, err: %s" , targetHex , targetBytes , err )
74+ return targetBytes , err
75+ }
76+
77+ func (s * SkvsStubInterface ) PutState (key string , value []byte ) error {
78+ logger .Warningf ("Calling Put State (Start), key = %s, value = %x, alldata = %s" , key , value , s .allDataNew )
9179
92- allData [key ] = valueHex
93- logger .Warningf ("Calling Put State (Mid-2), add need data key = %s, valueHex = %s, allData = %s" , key , valueHex , allData )
80+ valueHex := hex .EncodeToString (value )
81+ s .allDataNew [key ] = valueHex
82+ logger .Warningf ("Calling Put State (Mid-1), add need data key = %s, valueHex = %s, allData = %s" , key , valueHex , s .allDataNew )
9483
95- byteAllData , err := json .Marshal (allData )
84+ byteAllData , err := json .Marshal (s . allDataNew )
9685 if err != nil {
9786 return err
9887 }
99- logger .Warningf ("Calling Put State (Mid-3 ), successfull marshal allData, byteAlldata = %x" , byteAllData )
88+ logger .Warningf ("Calling Put State (Mid-2 ), successfull marshal allData, byteAlldata = %x" , byteAllData )
10089
10190 encValue , err := s .sep .EncryptState (byteAllData )
10291 if err != nil {
10392 return err
10493 }
10594 logger .Warningf ("Calling Put State (End), put encValue %x" , encValue )
10695
107- return s .PutPublicState (SingleKey , encValue )
96+ return s .PutPublicState (s . key , encValue )
10897}
0 commit comments