From 208d77bb2f1a588c5753767ee64c8367b093641c Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Fri, 20 Oct 2023 11:56:26 +0200 Subject: [PATCH] Finish --- eth/backend.go | 7 ++-- suave/cstore/backend_testing.go | 2 +- suave/cstore/engine.go | 45 ++++++++++++++---------- suave/cstore/local_store_backend.go | 6 +--- suave/cstore/redis_backends_test.go | 6 ++-- suave/cstore/redis_store_backend.go | 12 ++++--- suave/cstore/redis_store_backend_test.go | 6 +--- suave/e2e/workflow_test.go | 2 +- 8 files changed, 47 insertions(+), 39 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index cf6bb44a4c..db249455b1 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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() } diff --git a/suave/cstore/backend_testing.go b/suave/cstore/backend_testing.go index f4dc09a0a4..2adebffd4d 100644 --- a/suave/cstore/backend_testing.go +++ b/suave/cstore/backend_testing.go @@ -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, diff --git a/suave/cstore/engine.go b/suave/cstore/engine.go index d25870f36f..6104a56959 100644 --- a/suave/cstore/engine.go +++ b/suave/cstore/engine.go @@ -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) @@ -49,7 +62,7 @@ type ConfidentialStoreEngine struct { ctx context.Context cancel context.CancelFunc - backend suave.ConfidentialStoreBackend + storage ConfidentialStorageBackend transportTopic StoreTransportTopic daSigner DASigner @@ -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, @@ -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 } @@ -115,7 +124,7 @@ 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) } @@ -123,8 +132,8 @@ func (e *ConfidentialStoreEngine) Stop() error { } // For testing purposes! -func (e *ConfidentialStoreEngine) Backend() suave.ConfidentialStoreBackend { - return e.backend +func (e *ConfidentialStoreEngine) Backend() ConfidentialStorageBackend { + return e.storage } func (e *ConfidentialStoreEngine) ProcessMessages() { @@ -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) } @@ -205,13 +214,13 @@ 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) @@ -219,7 +228,7 @@ func (e *ConfidentialStoreEngine) Finalize(tx *types.Transaction, newBids map[su } 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) } @@ -344,7 +353,7 @@ 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) @@ -352,7 +361,7 @@ func (e *ConfidentialStoreEngine) NewMessage(message DAMessage) error { } } - _, 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! diff --git a/suave/cstore/local_store_backend.go b/suave/cstore/local_store_backend.go index 1a147a4a76..872a92d383 100644 --- a/suave/cstore/local_store_backend.go +++ b/suave/cstore/local_store_backend.go @@ -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 @@ -27,10 +27,6 @@ func NewLocalConfidentialStore() *LocalConfidentialStore { } } -func (l *LocalConfidentialStore) Start() error { - return nil -} - func (l *LocalConfidentialStore) Stop() error { return nil } diff --git a/suave/cstore/redis_backends_test.go b/suave/cstore/redis_backends_test.go index 2b6e23fa3e..c7186c455f 100644 --- a/suave/cstore/redis_backends_test.go +++ b/suave/cstore/redis_backends_test.go @@ -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()) @@ -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) diff --git a/suave/cstore/redis_store_backend.go b/suave/cstore/redis_store_backend.go index 268b77c30f..55ac60aa81 100644 --- a/suave/cstore/redis_store_backend.go +++ b/suave/cstore/redis_store_backend.go @@ -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 { @@ -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() diff --git a/suave/cstore/redis_store_backend_test.go b/suave/cstore/redis_store_backend_test.go index 17431c4a59..2962584c8f 100644 --- a/suave/cstore/redis_store_backend_test.go +++ b/suave/cstore/redis_store_backend_test.go @@ -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) } diff --git a/suave/e2e/workflow_test.go b/suave/e2e/workflow_test.go index bea9dc8531..ed8b32ec9a 100644 --- a/suave/e2e/workflow_test.go +++ b/suave/e2e/workflow_test.go @@ -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() }