From 33f408582183a45ad06f94db14654c9dde4cc65c Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Sat, 30 Sep 2023 00:04:30 +0200 Subject: [PATCH] Merge mempool struct to confstore engine (#51) * Merge mempool struct to confstore engine * Remove legacy interface * Revert "Remove legacy interface" This reverts commit b921ff8fe22f91919373814dd927918dbdbc14d8. * Revert "Merge mempool struct to confstore engine" This reverts commit be07a6f0c1546f8c4075034c4dab07c220568326. * Merge mempool in store * Remove mempool impl --- core/vm/contracts_suave_test.go | 5 +- eth/backend.go | 3 +- suave/backends/mempool_on_cdas.go | 82 --------------------------- suave/backends/redis_backends_test.go | 4 +- suave/backends/redis_store_backend.go | 58 ++++++++++++++++++- suave/core/engine.go | 33 ++--------- suave/core/engine_test.go | 14 ++++- suave/core/types.go | 9 ++- 8 files changed, 85 insertions(+), 123 deletions(-) delete mode 100644 suave/backends/mempool_on_cdas.go diff --git a/core/vm/contracts_suave_test.go b/core/vm/contracts_suave_test.go index b295067e41..e320980123 100644 --- a/core/vm/contracts_suave_test.go +++ b/core/vm/contracts_suave_test.go @@ -77,7 +77,7 @@ func TestSuavePrecompileStub(t *testing.T) { // This test ensures that the Suave precompile stubs work as expected // for encoding/decoding. mockSuaveBackend := &mockSuaveBackend{} - stubEngine, err := suave.NewConfidentialStoreEngine(mockSuaveBackend, mockSuaveBackend, suave.MockMempool{}, suave.MockSigner{}, suave.MockChainSigner{}) + stubEngine, err := suave.NewConfidentialStoreEngine(mockSuaveBackend, mockSuaveBackend, suave.MockSigner{}, suave.MockChainSigner{}) require.NoError(t, err) suaveContext := SuaveContext{ @@ -141,8 +141,7 @@ func TestSuavePrecompileStub(t *testing.T) { func newTestBackend(t *testing.T) *suaveRuntime { confStore := backends.NewLocalConfidentialStore() - suaveMempool := backends.NewMempoolOnConfidentialStore(confStore) - confEngine, err := suave.NewConfidentialStoreEngine(confStore, &suave.MockTransport{}, suaveMempool, suave.MockSigner{}, suave.MockChainSigner{}) + confEngine, err := suave.NewConfidentialStoreEngine(confStore, &suave.MockTransport{}, suave.MockSigner{}, suave.MockChainSigner{}) require.NoError(t, err) require.NoError(t, confEngine.Start()) diff --git a/eth/backend.go b/eth/backend.go index 6836f3b989..03864871a3 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -248,10 +248,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { suaveEthBackend = &suave_backends.EthMock{} } - suaveBidMempool := suave_backends.NewMempoolOnConfidentialStore(confidentialStoreBackend) suaveDaSigner := &suave_backends.AccountManagerDASigner{Manager: eth.AccountManager()} - confidentialStoreEngine, err := suave.NewConfidentialStoreEngine(confidentialStoreBackend, confidentialStoreTransport, suaveBidMempool, suaveDaSigner, types.LatestSigner(chainConfig)) + confidentialStoreEngine, err := suave.NewConfidentialStoreEngine(confidentialStoreBackend, confidentialStoreTransport, suaveDaSigner, types.LatestSigner(chainConfig)) if err != nil { return nil, err } diff --git a/suave/backends/mempool_on_cdas.go b/suave/backends/mempool_on_cdas.go deleted file mode 100644 index 95a48b5ac3..0000000000 --- a/suave/backends/mempool_on_cdas.go +++ /dev/null @@ -1,82 +0,0 @@ -package backends - -import ( - "errors" - "fmt" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/log" - suave "github.com/ethereum/go-ethereum/suave/core" -) - -type MempoolOnConfidentialStore struct { - cs suave.ConfidentialStoreBackend -} - -func NewMempoolOnConfidentialStore(cs suave.ConfidentialStoreBackend) *MempoolOnConfidentialStore { - return &MempoolOnConfidentialStore{ - cs: cs, - } -} - -func (m *MempoolOnConfidentialStore) Start() error { - err := m.cs.InitializeBid(mempoolConfidentialStoreBid) - if err != nil && !errors.Is(err, suave.ErrBidAlreadyPresent) { - return fmt.Errorf("mempool: could not initialize: %w", err) - } - - return nil -} - -func (m *MempoolOnConfidentialStore) Stop() error { - return nil -} - -var ( - mempoolConfStoreId = types.BidId{0x39} - mempoolConfStoreAddr = common.HexToAddress("0x39") - mempoolConfidentialStoreBid = suave.Bid{Id: mempoolConfStoreId, AllowedPeekers: []common.Address{mempoolConfStoreAddr}} -) - -func (m *MempoolOnConfidentialStore) SubmitBid(bid types.Bid) error { - defer log.Info("bid submitted", "bid", bid, "store", m.cs.Store) - - var bidsByBlockAndProtocol []types.Bid - bidsByBlockAndProtocolBytes, err := m.cs.Retrieve(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", bid.Version, bid.DecryptionCondition)) - if err == nil { - bidsByBlockAndProtocol = suave.MustDecode[[]types.Bid](bidsByBlockAndProtocolBytes) - } - // store bid by block number and by protocol + block number - bidsByBlockAndProtocol = append(bidsByBlockAndProtocol, bid) - - m.cs.Store(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", bid.Version, bid.DecryptionCondition), suave.MustEncode(bidsByBlockAndProtocol)) - - return nil -} - -func (m *MempoolOnConfidentialStore) FetchBidById(bidId suave.BidId) (types.Bid, error) { - engineBid, err := m.cs.FetchEngineBidById(bidId) - if err != nil { - log.Error("bid missing!", "id", bidId, "err", err) - return types.Bid{}, errors.New("not found") - } - - return types.Bid{ - Id: engineBid.Id, - Salt: engineBid.Salt, - DecryptionCondition: engineBid.DecryptionCondition, - AllowedPeekers: engineBid.AllowedPeekers, - AllowedStores: engineBid.AllowedStores, - Version: engineBid.Version, - }, nil -} - -func (m *MempoolOnConfidentialStore) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid { - bidsByProtocolBytes, err := m.cs.Retrieve(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", namespace, blockNumber)) - if err != nil { - return nil - } - defer log.Info("bids fetched", "bids", string(bidsByProtocolBytes)) - return suave.MustDecode[[]types.Bid](bidsByProtocolBytes) -} diff --git a/suave/backends/redis_backends_test.go b/suave/backends/redis_backends_test.go index e13734dfa3..5d27271672 100644 --- a/suave/backends/redis_backends_test.go +++ b/suave/backends/redis_backends_test.go @@ -97,7 +97,7 @@ func TestEngineOnRedis(t *testing.T) { redisPubSub1 := NewRedisPubSubTransport(mrPubSub.Addr()) redisStoreBackend1 := NewRedisStoreBackend(mrStore1.Addr()) - engine1, err := suave.NewConfidentialStoreEngine(redisStoreBackend1, redisPubSub1, suave.MockMempool{}, suave.MockSigner{}, suave.MockChainSigner{}) + engine1, err := suave.NewConfidentialStoreEngine(redisStoreBackend1, redisPubSub1, suave.MockSigner{}, suave.MockChainSigner{}) require.NoError(t, err) require.NoError(t, engine1.Start()) @@ -106,7 +106,7 @@ func TestEngineOnRedis(t *testing.T) { redisPubSub2 := NewRedisPubSubTransport(mrPubSub.Addr()) redisStoreBackend2 := NewRedisStoreBackend(mrStore2.Addr()) - engine2, err := suave.NewConfidentialStoreEngine(redisStoreBackend2, redisPubSub2, suave.MockMempool{}, suave.MockSigner{}, suave.MockChainSigner{}) + engine2, err := suave.NewConfidentialStoreEngine(redisStoreBackend2, redisPubSub2, suave.MockSigner{}, suave.MockChainSigner{}) require.NoError(t, err) require.NoError(t, engine2.Start()) diff --git a/suave/backends/redis_store_backend.go b/suave/backends/redis_store_backend.go index 93568486c3..5b859e5622 100644 --- a/suave/backends/redis_store_backend.go +++ b/suave/backends/redis_store_backend.go @@ -9,6 +9,8 @@ import ( "github.com/alicebob/miniredis/v2" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" suave "github.com/ethereum/go-ethereum/suave/core" "github.com/go-redis/redis/v8" ) @@ -38,10 +40,11 @@ func NewLocalConfidentialStore() *RedisStoreBackend { } func NewRedisStoreBackend(redisUri string) *RedisStoreBackend { - return &RedisStoreBackend{ + r := &RedisStoreBackend{ cancel: nil, redisUri: redisUri, } + return r } func (r *RedisStoreBackend) Start() error { @@ -69,6 +72,11 @@ func (r *RedisStoreBackend) Start() error { } r.client = client + err = r.InitializeBid(mempoolConfidentialStoreBid) + if err != nil && !errors.Is(err, suave.ErrBidAlreadyPresent) { + return fmt.Errorf("mempool: could not initialize: %w", err) + } + return nil } @@ -143,3 +151,51 @@ func (r *RedisStoreBackend) Retrieve(bid suave.Bid, caller common.Address, key s return data, nil } + +var ( + mempoolConfStoreId = types.BidId{0x39} + mempoolConfStoreAddr = common.HexToAddress("0x39") + mempoolConfidentialStoreBid = suave.Bid{Id: mempoolConfStoreId, AllowedPeekers: []common.Address{mempoolConfStoreAddr}} +) + +func (r *RedisStoreBackend) SubmitBid(bid types.Bid) error { + defer log.Info("bid submitted", "bid", bid, "store", r.Store) + + var bidsByBlockAndProtocol []types.Bid + bidsByBlockAndProtocolBytes, err := r.Retrieve(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", bid.Version, bid.DecryptionCondition)) + if err == nil { + bidsByBlockAndProtocol = suave.MustDecode[[]types.Bid](bidsByBlockAndProtocolBytes) + } + // store bid by block number and by protocol + block number + bidsByBlockAndProtocol = append(bidsByBlockAndProtocol, bid) + + r.Store(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", bid.Version, bid.DecryptionCondition), suave.MustEncode(bidsByBlockAndProtocol)) + + return nil +} + +func (r *RedisStoreBackend) FetchBidById(bidId suave.BidId) (types.Bid, error) { + engineBid, err := r.FetchEngineBidById(bidId) + if err != nil { + log.Error("bid missing!", "id", bidId, "err", err) + return types.Bid{}, errors.New("not found") + } + + return types.Bid{ + Id: engineBid.Id, + Salt: engineBid.Salt, + DecryptionCondition: engineBid.DecryptionCondition, + AllowedPeekers: engineBid.AllowedPeekers, + AllowedStores: engineBid.AllowedStores, + Version: engineBid.Version, + }, nil +} + +func (r *RedisStoreBackend) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid { + bidsByProtocolBytes, err := r.Retrieve(mempoolConfidentialStoreBid, mempoolConfStoreAddr, fmt.Sprintf("protocol-%s-bn-%d", namespace, blockNumber)) + if err != nil { + return nil + } + defer log.Info("bids fetched", "bids", string(bidsByProtocolBytes)) + return suave.MustDecode[[]types.Bid](bidsByProtocolBytes) +} diff --git a/suave/core/engine.go b/suave/core/engine.go index 074355541b..aeb9cb63b5 100644 --- a/suave/core/engine.go +++ b/suave/core/engine.go @@ -19,7 +19,6 @@ type ConfidentialStoreEngine struct { backend ConfidentialStoreBackend transportTopic StoreTransportTopic - mempool MempoolBackend daSigner DASigner chainSigner ChainSigner @@ -37,10 +36,6 @@ func (e *ConfidentialStoreEngine) Start() error { return err } - if err := e.mempool.Start(); err != nil { - return err - } - if e.cancel != nil { e.cancel() } @@ -59,9 +54,6 @@ func (e *ConfidentialStoreEngine) Stop() error { } e.cancel() - if err := e.mempool.Stop(); err != nil { - log.Warn("Confidential engine: error while stopping mempool", "err", err) - } if err := e.transportTopic.Stop(); err != nil { log.Warn("Confidential engine: error while stopping transport", "err", err) @@ -84,7 +76,7 @@ type ChainSigner interface { Sender(tx *types.Transaction) (common.Address, error) } -func NewConfidentialStoreEngine(backend ConfidentialStoreBackend, transportTopic StoreTransportTopic, mempool MempoolBackend, daSigner DASigner, chainSigner ChainSigner) (*ConfidentialStoreEngine, error) { +func NewConfidentialStoreEngine(backend ConfidentialStoreBackend, transportTopic StoreTransportTopic, daSigner DASigner, chainSigner ChainSigner) (*ConfidentialStoreEngine, error) { localAddresses := make(map[common.Address]struct{}) for _, addr := range daSigner.LocalAddresses() { localAddresses[addr] = struct{}{} @@ -93,7 +85,6 @@ func NewConfidentialStoreEngine(backend ConfidentialStoreBackend, transportTopic engine := &ConfidentialStoreEngine{ backend: backend, transportTopic: transportTopic, - mempool: mempool, daSigner: daSigner, chainSigner: chainSigner, storeUUID: uuid.New(), @@ -180,7 +171,7 @@ func (e *ConfidentialStoreEngine) InitializeBid(bid types.Bid, creationTx *types } // send the bid to the internal mempool - if err := e.mempool.SubmitBid(bid); err != nil { + if err := e.backend.SubmitBid(bid); err != nil { return types.Bid{}, fmt.Errorf("failed to submit to mempool: %w", err) } @@ -188,15 +179,15 @@ func (e *ConfidentialStoreEngine) InitializeBid(bid types.Bid, creationTx *types } func (e *ConfidentialStoreEngine) SubmitBid(bid types.Bid) error { - return e.mempool.SubmitBid(bid) + return e.backend.SubmitBid(bid) } func (e *ConfidentialStoreEngine) FetchBidById(bidId BidId) (types.Bid, error) { - return e.mempool.FetchBidById(bidId) + return e.backend.FetchBidById(bidId) } func (e *ConfidentialStoreEngine) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid { - return e.mempool.FetchBidsByProtocolAndBlock(blockNumber, namespace) + return e.backend.FetchBidsByProtocolAndBlock(blockNumber, namespace) } func (e *ConfidentialStoreEngine) Store(bidId BidId, sourceTx *types.Transaction, caller common.Address, key string, value []byte) (Bid, error) { @@ -339,7 +330,7 @@ func (e *ConfidentialStoreEngine) NewMessage(message DAMessage) error { return fmt.Errorf("unexpected error while initializing bid from transport: %w", err) } } else { - e.mempool.SubmitBid(innerBid) + e.backend.SubmitBid(innerBid) } _, err = e.backend.Store(message.Bid, message.Caller, message.Key, message.Value) @@ -405,15 +396,3 @@ func (MockChainSigner) Sender(tx *types.Transaction) (common.Address, error) { return *tx.To(), nil } - -type MockMempool struct{} - -func (MockMempool) Start() error { return nil } -func (MockMempool) Stop() error { return nil } - -func (MockMempool) SubmitBid(types.Bid) error { return nil } - -func (MockMempool) FetchBidById(BidId) (types.Bid, error) { return types.Bid{}, nil } -func (MockMempool) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid { - return nil -} diff --git a/suave/core/engine_test.go b/suave/core/engine_test.go index 581d913d1b..c85e6459ea 100644 --- a/suave/core/engine_test.go +++ b/suave/core/engine_test.go @@ -42,6 +42,18 @@ func (*FakeStoreBackend) Retrieve(bid Bid, caller common.Address, key string) ([ return nil, errors.New("not implemented") } +func (*FakeStoreBackend) FetchBidById(BidId) (types.Bid, error) { + return types.Bid{}, nil +} + +func (*FakeStoreBackend) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid { + return nil +} + +func (*FakeStoreBackend) SubmitBid(types.Bid) error { + return nil +} + func TestOwnMessageDropping(t *testing.T) { var wasCalled *bool = new(bool) fakeStore := FakeStoreBackend{OnStore: func(bid Bid, caller common.Address, key string, value []byte) (Bid, error) { @@ -50,7 +62,7 @@ func TestOwnMessageDropping(t *testing.T) { }} fakeDaSigner := FakeDASigner{localAddresses: []common.Address{{0x42}}} - engine, err := NewConfidentialStoreEngine(&fakeStore, MockTransport{}, MockMempool{}, fakeDaSigner, MockChainSigner{}) + engine, err := NewConfidentialStoreEngine(&fakeStore, MockTransport{}, fakeDaSigner, MockChainSigner{}) require.NoError(t, err) dummyCreationTx := types.NewTx(&types.ConfidentialComputeRequest{ diff --git a/suave/core/types.go b/suave/core/types.go index c13aae049f..8a2ebbbdd8 100644 --- a/suave/core/types.go +++ b/suave/core/types.go @@ -36,17 +36,16 @@ var ErrBidAlreadyPresent = errors.New("bid already present") type ConfidentialStoreBackend interface { node.Lifecycle + InitializeBid(bid Bid) error FetchEngineBidById(bidId BidId) (Bid, error) Store(bid Bid, caller common.Address, key string, value []byte) (Bid, error) Retrieve(bid Bid, caller common.Address, key string) ([]byte, error) -} - -type MempoolBackend interface { - node.Lifecycle - SubmitBid(types.Bid) error FetchBidById(BidId) (types.Bid, error) FetchBidsByProtocolAndBlock(blockNumber uint64, namespace string) []types.Bid + + // TODO: remove this + SubmitBid(types.Bid) error } type ConfidentialEthBackend interface {