Skip to content

Commit

Permalink
change to only getState Once
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchanglew committed Jun 11, 2024
1 parent dfad2c1 commit 5dab856
Showing 1 changed file with 45 additions and 56 deletions.
101 changes: 45 additions & 56 deletions ecc_go/chaincode/enclave_go/skvs_stub_Interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,97 +12,86 @@ 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 {
return err
}
logger.Warningf("Calling Put State (End), put encValue %x", encValue)

return s.PutPublicState(SingleKey, encValue)
return s.PutPublicState(s.key, encValue)
}

0 comments on commit 5dab856

Please sign in to comment.