Skip to content

Commit

Permalink
Add datastore local store
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Sep 29, 2023
1 parent 870747f commit ae71f64
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
107 changes: 107 additions & 0 deletions suave/backends/local_store_backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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) {
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 {
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, err := l.FetchBidById(id)
if err != nil {
continue
}
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

0 comments on commit ae71f64

Please sign in to comment.