-
Notifications
You must be signed in to change notification settings - Fork 52
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
Add local confidential data store #54
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4b3746
Create engine store api
ferranbt 9706ae6
Merge conflicts
ferranbt 870747f
Fix test
ferranbt ae71f64
Add datastore local store
ferranbt f74e792
Merge branch 'main' into local-datastore
ferranbt ee72ced
Add locks
ferranbt 3241e77
Avoid deadlock
ferranbt 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 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,112 @@ | ||
package backends | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/log" | ||
suave "github.com/ethereum/go-ethereum/suave/core" | ||
) | ||
|
||
var _ suave.ConfidentialStoreBackend = &LocalConfidentialStore{} | ||
|
||
type LocalConfidentialStore struct { | ||
lock sync.Mutex | ||
bids map[suave.BidId]suave.Bid | ||
dataMap map[string][]byte | ||
index map[string][]suave.BidId | ||
} | ||
|
||
func NewLocalConfidentialStore() *LocalConfidentialStore { | ||
return &LocalConfidentialStore{ | ||
bids: make(map[suave.BidId]suave.Bid), | ||
dataMap: make(map[string][]byte), | ||
index: make(map[string][]suave.BidId), | ||
} | ||
} | ||
|
||
func (l *LocalConfidentialStore) Start() error { | ||
return nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) Stop() error { | ||
return nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) InitializeBid(bid suave.Bid) error { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
_, found := l.bids[bid.Id] | ||
if found { | ||
return suave.ErrBidAlreadyPresent | ||
} | ||
|
||
l.bids[bid.Id] = bid | ||
|
||
// index the bid by (protocol, block number) | ||
indexKey := fmt.Sprintf("protocol-%s-bn-%d", bid.Version, bid.DecryptionCondition) | ||
bidIds := l.index[indexKey] | ||
bidIds = append(bidIds, bid.Id) | ||
l.index[indexKey] = bidIds | ||
|
||
return nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) Store(bid suave.Bid, caller common.Address, key string, value []byte) (suave.Bid, error) { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
l.dataMap[fmt.Sprintf("%x-%s", bid.Id, key)] = append(make([]byte, 0, len(value)), value...) | ||
|
||
defer log.Trace("CSSW", "caller", caller, "key", key, "value", value, "stored", l.dataMap[fmt.Sprintf("%x-%s", bid.Id, key)]) | ||
return bid, nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) Retrieve(bid suave.Bid, caller common.Address, key string) ([]byte, error) { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
data, found := l.dataMap[fmt.Sprintf("%x-%s", bid.Id, key)] | ||
if !found { | ||
return []byte{}, fmt.Errorf("data for key %s not found", key) | ||
} | ||
|
||
log.Trace("CSRW", "caller", caller, "key", key, "data", data) | ||
return append(make([]byte, 0, len(data)), data...), nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) FetchBidById(bidId suave.BidId) (suave.Bid, error) { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
bid, found := l.bids[bidId] | ||
if !found { | ||
return suave.Bid{}, errors.New("bid not found") | ||
} | ||
|
||
return bid, nil | ||
} | ||
|
||
func (l *LocalConfidentialStore) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.Bid { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
indexKey := fmt.Sprintf("protocol-%s-bn-%d", namespace, blockNumber) | ||
bidIDs, ok := l.index[indexKey] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
res := []suave.Bid{} | ||
for _, id := range bidIDs { | ||
bid, found := l.bids[id] | ||
if found { | ||
res = append(res, bid) | ||
} | ||
} | ||
|
||
return res | ||
} |
This file contains 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,10 @@ | ||
package backends | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLocal_StoreSuite(t *testing.T) { | ||
store := NewLocalConfidentialStore() | ||
testBackendStore(t, store) | ||
} |
This file contains 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
Oops, something went wrong.
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.
Does this need a lock too?
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.
Fixed, also missing in the other method.