-
Notifications
You must be signed in to change notification settings - Fork 98
Feature/solution skvs #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mbrandenburger
merged 13 commits into
hyperledger:main
from
chenchanglew:feature/solution-skvs
May 29, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6273974
add singleKVS wrapper
chenchanglew 4929293
able to save into single key
chenchanglew 9ecb879
change to only getState Once
chenchanglew 114d803
change storing type and add del state
chenchanglew a13d1a3
refactor: clean up code
chenchanglew ed22ff2
lint: make checks
chenchanglew dc8bdb2
chore: change filename to small letters
chenchanglew 61d3af6
chore: refactor SkvsStubInterface
chenchanglew 46e0ffb
feat: change secret-keeper-go makefile
chenchanglew 3006ec9
fix: edit makefile
chenchanglew 873aeae
refactor: use functional options pattern to apply skvs option
chenchanglew b99495f
chore: remove logging and cleanup
chenchanglew f6c2ef5
fix: resolves comments
chenchanglew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package enclave_go | ||
|
|
||
| import ( | ||
| "github.com/hyperledger/fabric-chaincode-go/shim" | ||
| pb "github.com/hyperledger/fabric-protos-go/peer" | ||
| ) | ||
|
|
||
| func NewSkvsStub(cc shim.Chaincode) *EnclaveStub { | ||
| enclaveStub := NewEnclaveStub(cc) | ||
| enclaveStub.stubProvider = func(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) shim.ChaincodeStubInterface { | ||
| return NewSkvsStubInterface(stub, input, rwset, sep) | ||
| } | ||
| return enclaveStub | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package enclave_go | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/hyperledger/fabric-chaincode-go/shim" | ||
| pb "github.com/hyperledger/fabric-protos-go/peer" | ||
| ) | ||
|
|
||
| const SKVSKey = "SKVS" | ||
|
|
||
| type SkvsStubInterface struct { | ||
| *FpcStubInterface | ||
| allDataOld map[string][]byte | ||
| allDataNew map[string][]byte | ||
| key string | ||
| } | ||
|
|
||
| func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { | ||
| fpcStub := NewFpcStubInterface(stub, input, rwset, sep) | ||
| skvsStub := &SkvsStubInterface{ | ||
| FpcStubInterface: fpcStub, | ||
| allDataOld: make(map[string][]byte), | ||
| allDataNew: make(map[string][]byte), | ||
| key: SKVSKey, | ||
| } | ||
| err := skvsStub.initSKVS() | ||
| if err != nil { | ||
| panic(fmt.Sprintf("Initializing SKVS failed, err: %v", err)) | ||
| } | ||
| return skvsStub | ||
| } | ||
|
|
||
| func (s *SkvsStubInterface) initSKVS() error { | ||
|
|
||
| // get current state, this will only operate once | ||
| encValue, err := s.GetPublicState(s.key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // return if the key initially does not exist | ||
| if len(encValue) == 0 { | ||
| logger.Warningf("SKVS is empty, Initiating.") | ||
|
Comment on lines
+50
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also handle this error differently? Can we actually continue if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we can continue if the encValue is empty. |
||
| return nil | ||
| } | ||
|
|
||
| value, err := s.sep.DecryptState(encValue) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| logger.Debug("SKVS has default value, loading current value.") | ||
|
|
||
| err = json.Unmarshal(value, &s.allDataOld) | ||
| if err != nil { | ||
| logger.Errorf("SKVS Json unmarshal error: %s", err) | ||
| return err | ||
| } | ||
| err = json.Unmarshal(value, &s.allDataNew) | ||
| if err != nil { | ||
| logger.Errorf("SKVS Json unmarshal error: %s", err) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { | ||
| value, found := s.allDataOld[key] | ||
| if !found { | ||
| logger.Errorf("skvs allDataOld key: %s, not found", key) | ||
| return nil, nil | ||
| } | ||
| return value, nil | ||
| } | ||
|
|
||
| func (s *SkvsStubInterface) PutState(key string, value []byte) error { | ||
|
|
||
| s.allDataNew[key] = value | ||
| 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) 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
| Copyright 2020 Intel Corporation | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/hyperledger/fabric-chaincode-go/shim" | ||
| "github.com/hyperledger/fabric-contract-api-go/contractapi" | ||
| fpc "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode" | ||
| "github.com/hyperledger/fabric-private-chaincode/samples/chaincode/secret-keeper-go/chaincode" | ||
| ) | ||
|
|
||
| func main() { | ||
|
|
||
| ccid := os.Getenv("CHAINCODE_PKG_ID") | ||
| addr := os.Getenv("CHAINCODE_SERVER_ADDRESS") | ||
|
|
||
| // create chaincode | ||
| secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) | ||
| skvsChaincode := fpc.NewPrivateChaincode(secretChaincode, fpc.WithSKVS()) | ||
|
|
||
| // start chaincode as a service | ||
| server := &shim.ChaincodeServer{ | ||
| CCID: ccid, | ||
| Address: addr, | ||
| CC: skvsChaincode, | ||
| TLSProps: shim.TLSProperties{ | ||
| Disabled: true, // just for testing good enough | ||
| }, | ||
| } | ||
|
|
||
| if err := server.Start(); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it makes sense to protect the entire Init method with
sync.Once?https://pkg.go.dev/sync#Once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function is safe even it runs multiple time, since the
encValue, err := s.GetPublicState(s.key)will always fetch the latest value, if someone try to do something malicious by calling this init function they will just get the latest public state.Plus I already change the init function into a private function thus I think it should be okay without the sync.Once.