Skip to content
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 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions suave/backends/local_store_backend.go
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]
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

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
}
10 changes: 10 additions & 0 deletions suave/backends/local_store_backend_test.go
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)
}
6 changes: 2 additions & 4 deletions suave/backends/redis_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/go-redis/redis/v8"
)

var _ suave.ConfidentialStoreBackend = &RedisStoreBackend{}

var (
formatRedisBidKey = func(bidId suave.BidId) string {
return fmt.Sprintf("bid-%x", bidId)
Expand All @@ -35,10 +37,6 @@ type RedisStoreBackend struct {
local *miniredis.Miniredis
}

func NewLocalConfidentialStore() *RedisStoreBackend {
return NewRedisStoreBackend("")
}

func NewRedisStoreBackend(redisUri string) *RedisStoreBackend {
r := &RedisStoreBackend{
cancel: nil,
Expand Down
Loading