From 5dab856bca1c64500b3b8b77bbbde21dd05d19bd Mon Sep 17 00:00:00 2001 From: Flamewind97 Date: Fri, 14 Apr 2023 15:36:24 +0200 Subject: [PATCH] change to only getState Once --- .../enclave_go/skvs_stub_Interface.go | 101 ++++++++---------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go index 49b80f42b..86e1b2425 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go @@ -12,91 +12,80 @@ import ( "github.com/hyperledger/fabric-chaincode-go/shim" pb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/pkg/errors" ) -var SingleKey = "SingleKey" - type SkvsStubInterface struct { *FpcStubInterface + allDataOld map[string]string + allDataNew map[string]string + key string } func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { + logger.Warning("==== Get New Skvs Interface =====") fpcStub := NewFpcStubInterface(stub, input, rwset, sep) - return &SkvsStubInterface{fpcStub} -} - -func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { - logger.Warningf("Calling Get State (Start), key = %s", key) - encValue, err := s.GetPublicState(SingleKey) + skvsStub := SkvsStubInterface{fpcStub, map[string]string{}, map[string]string{}, "SKVS"} + err := skvsStub.InitSKVS() if err != nil { - return nil, err + logger.Warningf("Error!! Initializing SKVS failed") } - - // in case the key does not exist, return early - if len(encValue) == 0 { - logger.Warningf("Calling Get State (End), data empty return.") - return nil, nil - } - - value, err := s.sep.DecryptState(encValue) - if err != nil { - return nil, err - } - - // Create an interface{} value to hold the unmarshalled data - var allData map[string]string - - // Unmarshal the JSON data into the interface{} value - err = json.Unmarshal(value, &allData) - if err != nil { - logger.Errorf("SKVS Json unmarshal error: %s", err) - return nil, err - } - logger.Warningf("Calling Get State (Mid), key = %s, decrypted done alldata = %s", key, allData) - - targetHex := allData[key] - targetBytes, err := hex.DecodeString(targetHex) - logger.Warningf("Calling Get State (End), Target: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) - return targetBytes, err - // return s.sep.DecryptState(encValue) + return &skvsStub } -func (s *SkvsStubInterface) PutState(key string, value []byte) error { - logger.Warningf("Calling Put State (Start), key = %s, value = %x", key, value) - // grab all data from the state. - encAllValue, err := s.GetPublicState(SingleKey) +func (s *SkvsStubInterface) InitSKVS() error { + logger.Warningf(" === Initializing SKVS === ") + + // get current state, this will only operate once + encValue, err := s.GetPublicState(s.key) if err != nil { - return err + return nil } - var allData map[string]string - if len(encAllValue) == 0 { - // current world state is empty - allData = map[string]string{} + if len(encValue) == 0 { + logger.Warningf("SKVS is empty, Initiating.") } else { - allValue, err := s.sep.DecryptState(encAllValue) + value, err := s.sep.DecryptState(encValue) if err != nil { return err } - // Unmarshal the JSON data into the interface{} value - err = json.Unmarshal(allValue, &allData) + logger.Warningf("SKVS has default value, loading current value.") + + err = json.Unmarshal(value, &s.allDataOld) + err = json.Unmarshal(value, &s.allDataNew) if err != nil { logger.Errorf("SKVS Json unmarshal error: %s", err) return err } } - logger.Warningf("Calling Put State (Mid-1), decrypt succeed, allData = %s", allData) - valueHex := hex.EncodeToString(value) + logger.Warningf("SKVS Init finish, allDataOld = %s, allDataNew = %s", s.allDataOld, s.allDataNew) + return nil +} + +func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { + logger.Warningf("Calling Get State (Start), key = %s, alldataOld = %s", key, s.allDataOld) + targetHex, found := s.allDataOld[key] + if found != true { + return nil, errors.New("skvs allDataOld key not found") + } + targetBytes, err := hex.DecodeString(targetHex) + logger.Warningf("Calling Get State (End), TargetHex: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) + return targetBytes, err +} + +func (s *SkvsStubInterface) PutState(key string, value []byte) error { + logger.Warningf("Calling Put State (Start), key = %s, value = %x, alldata = %s", key, value, s.allDataNew) - allData[key] = valueHex - logger.Warningf("Calling Put State (Mid-2), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, allData) + valueHex := hex.EncodeToString(value) + s.allDataNew[key] = valueHex + logger.Warningf("Calling Put State (Mid-1), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, s.allDataNew) - byteAllData, err := json.Marshal(allData) + byteAllData, err := json.Marshal(s.allDataNew) if err != nil { return err } - logger.Warningf("Calling Put State (Mid-3), successfull marshal allData, byteAlldata = %x", byteAllData) + logger.Warningf("Calling Put State (Mid-2), successfull marshal allData, byteAlldata = %x", byteAllData) encValue, err := s.sep.EncryptState(byteAllData) if err != nil { @@ -104,5 +93,5 @@ func (s *SkvsStubInterface) PutState(key string, value []byte) error { } logger.Warningf("Calling Put State (End), put encValue %x", encValue) - return s.PutPublicState(SingleKey, encValue) + return s.PutPublicState(s.key, encValue) }