Skip to content

Commit

Permalink
Finish
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Oct 20, 2023
1 parent 579e000 commit 208d77b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 39 deletions.
7 changes: 5 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,12 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
eth.miner = miner.New(eth, &config.Miner, eth.blockchain.Config(), eth.EventMux(), eth.engine, eth.isLocalBlock)
eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData))

var confidentialStoreBackend suave.ConfidentialStoreBackend
var confidentialStoreBackend cstore.ConfidentialStorageBackend
if config.Suave.RedisStoreUri != "" {
confidentialStoreBackend = cstore.NewRedisStoreBackend(config.Suave.RedisStoreUri)
confidentialStoreBackend, err = cstore.NewRedisStoreBackend(config.Suave.RedisStoreUri)
if err != nil {
return nil, err
}
} else {
confidentialStoreBackend = cstore.NewLocalConfidentialStore()
}
Expand Down
2 changes: 1 addition & 1 deletion suave/cstore/backend_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)

func testBackendStore(t *testing.T, store suave.ConfidentialStoreBackend) {
func testBackendStore(t *testing.T, store ConfidentialStorageBackend) {
bid := suave.Bid{
Id: suave.RandomBidId(),
DecryptionCondition: 10,
Expand Down
45 changes: 27 additions & 18 deletions suave/cstore/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import (
"golang.org/x/exp/slices"
)

// ConfidentialStorageBackend is the interface that must be implemented by a
// storage backend for the confidential storage engine.
type ConfidentialStorageBackend interface {
InitializeBid(bid suave.Bid) error
Store(bid suave.Bid, caller common.Address, key string, value []byte) (suave.Bid, error)
Retrieve(bid suave.Bid, caller common.Address, key string) ([]byte, error)
FetchBidById(suave.BidId) (suave.Bid, error)
FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.Bid
Stop() error
}

// StoreTransportTopic is the interface that must be implemented by a
// transport engine for the confidential storage engine.
type StoreTransportTopic interface {
node.Lifecycle
Subscribe() (<-chan DAMessage, context.CancelFunc)
Expand Down Expand Up @@ -49,7 +62,7 @@ type ConfidentialStoreEngine struct {
ctx context.Context
cancel context.CancelFunc

backend suave.ConfidentialStoreBackend
storage ConfidentialStorageBackend
transportTopic StoreTransportTopic

daSigner DASigner
Expand All @@ -59,14 +72,14 @@ type ConfidentialStoreEngine struct {
localAddresses map[common.Address]struct{}
}

func NewConfidentialStoreEngine(backend suave.ConfidentialStoreBackend, transportTopic StoreTransportTopic, daSigner DASigner, chainSigner ChainSigner) *ConfidentialStoreEngine {
func NewConfidentialStoreEngine(backend ConfidentialStorageBackend, transportTopic StoreTransportTopic, daSigner DASigner, chainSigner ChainSigner) *ConfidentialStoreEngine {
localAddresses := make(map[common.Address]struct{})
for _, addr := range daSigner.LocalAddresses() {
localAddresses[addr] = struct{}{}
}

return &ConfidentialStoreEngine{
backend: backend,
storage: backend,
transportTopic: transportTopic,
daSigner: daSigner,
chainSigner: chainSigner,
Expand All @@ -84,10 +97,6 @@ func (e *ConfidentialStoreEngine) NewTransactionalStore(sourceTx *types.Transact
}

func (e *ConfidentialStoreEngine) Start() error {
if err := e.backend.Start(); err != nil {
return err
}

if err := e.transportTopic.Start(); err != nil {
return err
}
Expand Down Expand Up @@ -115,16 +124,16 @@ func (e *ConfidentialStoreEngine) Stop() error {
log.Warn("Confidential engine: error while stopping transport", "err", err)
}

if err := e.backend.Stop(); err != nil {
if err := e.storage.Stop(); err != nil {
log.Warn("Confidential engine: error while stopping transport", "err", err)
}

return nil
}

// For testing purposes!
func (e *ConfidentialStoreEngine) Backend() suave.ConfidentialStoreBackend {
return e.backend
func (e *ConfidentialStoreEngine) Backend() ConfidentialStorageBackend {
return e.storage
}

func (e *ConfidentialStoreEngine) ProcessMessages() {
Expand Down Expand Up @@ -188,15 +197,15 @@ func (e *ConfidentialStoreEngine) InitializeBid(bid types.Bid, creationTx *types
}

func (e *ConfidentialStoreEngine) FetchBidById(bidId suave.BidId) (suave.Bid, error) {
return e.backend.FetchBidById(bidId)
return e.storage.FetchBidById(bidId)
}

func (e *ConfidentialStoreEngine) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []suave.Bid {
return e.backend.FetchBidsByProtocolAndBlock(blockNumber, namespace)
return e.storage.FetchBidsByProtocolAndBlock(blockNumber, namespace)
}

func (e *ConfidentialStoreEngine) Retrieve(bidId suave.BidId, caller common.Address, key string) ([]byte, error) {
bid, err := e.backend.FetchBidById(bidId)
bid, err := e.storage.FetchBidById(bidId)
if err != nil {
return []byte{}, fmt.Errorf("confidential engine: could not fetch bid %x while retrieving: %w", bidId, err)
}
Expand All @@ -205,21 +214,21 @@ func (e *ConfidentialStoreEngine) Retrieve(bidId suave.BidId, caller common.Addr
return []byte{}, fmt.Errorf("confidential engine: %x not allowed to retrieve %s on %x", caller, key, bidId)
}

return e.backend.Retrieve(bid, caller, key)
return e.storage.Retrieve(bid, caller, key)
}

func (e *ConfidentialStoreEngine) Finalize(tx *types.Transaction, newBids map[suave.BidId]suave.Bid, stores []StoreWrite) error {
//
for _, bid := range newBids {
err := e.backend.InitializeBid(bid)
err := e.storage.InitializeBid(bid)
if err != nil {
// TODO: deinitialize!
return fmt.Errorf("confidential engine: store backend failed to initialize bid: %w", err)
}
}

for _, sw := range stores {
if _, err := e.backend.Store(sw.Bid, sw.Caller, sw.Key, sw.Value); err != nil {
if _, err := e.storage.Store(sw.Bid, sw.Caller, sw.Key, sw.Value); err != nil {
// TODO: deinitialize and deStore!
return fmt.Errorf("failed to store data: %w", err)
}
Expand Down Expand Up @@ -344,15 +353,15 @@ func (e *ConfidentialStoreEngine) NewMessage(message DAMessage) error {
}

for _, sw := range message.StoreWrites {
err = e.backend.InitializeBid(sw.Bid)
err = e.storage.InitializeBid(sw.Bid)
if err != nil {
if !errors.Is(err, suave.ErrBidAlreadyPresent) {
log.Error("confidential engine: unexpected error while initializing bid from transport: %w", err)
continue // Don't abandon!
}
}

_, err = e.backend.Store(sw.Bid, sw.Caller, sw.Key, sw.Value)
_, err = e.storage.Store(sw.Bid, sw.Caller, sw.Key, sw.Value)
if err != nil {
log.Error("confidential engine: unexpected error while storing: %w", err)
continue // Don't abandon!
Expand Down
6 changes: 1 addition & 5 deletions suave/cstore/local_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
suave "github.com/ethereum/go-ethereum/suave/core"
)

var _ suave.ConfidentialStoreBackend = &LocalConfidentialStore{}
var _ ConfidentialStorageBackend = &LocalConfidentialStore{}

type LocalConfidentialStore struct {
lock sync.Mutex
Expand All @@ -27,10 +27,6 @@ func NewLocalConfidentialStore() *LocalConfidentialStore {
}
}

func (l *LocalConfidentialStore) Start() error {
return nil
}

func (l *LocalConfidentialStore) Stop() error {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions suave/cstore/redis_backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func TestEngineOnRedis(t *testing.T) {
mrPubSub := mrStore1

redisPubSub1 := NewRedisPubSubTransport(mrPubSub.Addr())
redisStoreBackend1 := NewRedisStoreBackend(mrStore1.Addr())
redisStoreBackend1, _ := NewRedisStoreBackend(mrStore1.Addr())

engine1 := NewConfidentialStoreEngine(redisStoreBackend1, redisPubSub1, MockSigner{}, MockChainSigner{})
require.NoError(t, engine1.Start())
t.Cleanup(func() { engine1.Stop() })

redisPubSub2 := NewRedisPubSubTransport(mrPubSub.Addr())
redisStoreBackend2 := NewRedisStoreBackend(mrStore2.Addr())
redisStoreBackend2, _ := NewRedisStoreBackend(mrStore2.Addr())

engine2 := NewConfidentialStoreEngine(redisStoreBackend2, redisPubSub2, MockSigner{}, MockChainSigner{})
require.NoError(t, engine2.Start())
Expand All @@ -94,7 +94,7 @@ func TestEngineOnRedis(t *testing.T) {
bid, err := engine1.InitializeBid(types.Bid{
DecryptionCondition: uint64(13),
AllowedPeekers: []common.Address{{0x41, 0x39}},
AllowedStores: []common.Address{common.Address{}},
AllowedStores: []common.Address{{}},
Version: string("vv"),
}, dummyCreationTx)
require.NoError(t, err)
Expand Down
12 changes: 8 additions & 4 deletions suave/cstore/redis_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/go-redis/redis/v8"
)

var _ suave.ConfidentialStoreBackend = &RedisStoreBackend{}
var _ ConfidentialStorageBackend = &RedisStoreBackend{}

var (
formatRedisBidKey = func(bidId suave.BidId) string {
Expand All @@ -37,15 +37,19 @@ type RedisStoreBackend struct {
local *miniredis.Miniredis
}

func NewRedisStoreBackend(redisUri string) *RedisStoreBackend {
func NewRedisStoreBackend(redisUri string) (*RedisStoreBackend, error) {
r := &RedisStoreBackend{
cancel: nil,
redisUri: redisUri,
}
return r

if err := r.start(); err != nil {
return nil, err
}
return r, nil
}

func (r *RedisStoreBackend) Start() error {
func (r *RedisStoreBackend) start() error {
if r.redisUri == "" {
// create a mini-redis instance
localRedis, err := miniredis.Run()
Expand Down
6 changes: 1 addition & 5 deletions suave/cstore/redis_store_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package cstore

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRedis_StoreSuite(t *testing.T) {
store := NewRedisStoreBackend("")
require.NoError(t, store.Start())

store, _ := NewRedisStoreBackend("")
testBackendStore(t, store)
}
2 changes: 1 addition & 1 deletion suave/e2e/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ func (f *framework) NewSDKClient() *sdk.Client {
return sdk.NewClient(f.suethSrv.RPCNode(), testKey, f.ExecutionNode())
}

func (f *framework) ConfidentialStoreBackend() suave.ConfidentialStoreBackend {
func (f *framework) ConfidentialStoreBackend() cstore.ConfidentialStorageBackend {
return f.suethSrv.service.APIBackend.SuaveEngine().Backend()
}

Expand Down

0 comments on commit 208d77b

Please sign in to comment.