Skip to content

Commit

Permalink
change storing type and add del state
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchanglew committed Jun 11, 2024
1 parent 5dab856 commit 05c5cd2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
50 changes: 32 additions & 18 deletions ecc_go/chaincode/enclave_go/skvs_stub_Interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package enclave_go

import (
"encoding/hex"
"encoding/json"

"github.com/hyperledger/fabric-chaincode-go/shim"
Expand All @@ -17,15 +16,15 @@ import (

type SkvsStubInterface struct {
*FpcStubInterface
allDataOld map[string]string
allDataNew map[string]string
allDataOld map[string][]byte
allDataNew map[string][]byte
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)
skvsStub := SkvsStubInterface{fpcStub, map[string]string{}, map[string]string{}, "SKVS"}
skvsStub := SkvsStubInterface{fpcStub, map[string][]byte{}, map[string][]byte{}, "SKVS"}
err := skvsStub.InitSKVS()
if err != nil {
logger.Warningf("Error!! Initializing SKVS failed")
Expand Down Expand Up @@ -59,39 +58,54 @@ func (s *SkvsStubInterface) InitSKVS() error {
}
}

logger.Warningf("SKVS Init finish, allDataOld = %s, allDataNew = %s", s.allDataOld, s.allDataNew)
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]
logger.Warningf("Calling Get State (Start), key: %s, alldataOld: %s", key, s.allDataOld)
value, found := s.allDataOld[key]
if found != true {

Check failure on line 68 in ecc_go/chaincode/enclave_go/skvs_stub_Interface.go

View workflow job for this annotation

GitHub Actions / docker (ubuntu-22.04, 22.04, jammy)

should omit comparison to bool constant, can be simplified to !found (S1002)
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
logger.Warningf("Calling Get State (End), key: %s, value: %x", key, value)
return value, nil
}

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)

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)
logger.Warningf("Calling Put State (Start), key: %s, value: %x, alldata: %s", key, value, s.allDataNew)

s.allDataNew[key] = value
byteAllData, err := json.Marshal(s.allDataNew)
if err != nil {
return err
}
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)
logger.Warningf("Calling Put State (End), put encValue: %x", encValue)

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

func (s *SkvsStubInterface) DelState(key string) error {
delete(s.allDataNew, key)
byteAllData, err := json.Marshal(s.allDataNew)
if err != nil {
return err
}
encValue, err := s.sep.EncryptState(byteAllData)
if err != nil {
return err
}
return s.PutPublicState(s.key, encValue)
}

func (s *SkvsStubInterface) GetStateByRange(startKey string, endKey string) (shim.StateQueryIteratorInterface, error) {
panic("not implemented") // TODO: Implement
}

func (s *SkvsStubInterface) GetStateByRangeWithPagination(startKey string, endKey string, pageSize int32, bookmark string) (shim.StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) {
panic("not implemented") // TODO: Implement
}
3 changes: 0 additions & 3 deletions samples/chaincode/secret-keeper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func main() {
// chaincode := fpc.NewPrivateChaincode(secretChaincode)
skvsChaincode := fpc.NewSkvsChaincode(secretChaincode)

// // single KVS
// skvsChaincode := &fpc.SKVSWrapper{chaincode}

// start chaincode as a service
server := &shim.ChaincodeServer{
CCID: ccid,
Expand Down

0 comments on commit 05c5cd2

Please sign in to comment.