From dbb495f22baf4e663fa14013881fbde99c1dd8ca Mon Sep 17 00:00:00 2001 From: rian Date: Thu, 9 Jan 2025 12:59:03 +0200 Subject: [PATCH] wip - encoder failing to unmarhsal for terminal test run, but NOT in vscode test run.. --- mempool/mempool.go | 30 ++++++++----------------- mempool/mempool_test.go | 49 +++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/mempool/mempool.go b/mempool/mempool.go index 031d844b30..9f0c976d41 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -150,20 +150,20 @@ func (p *Pool) handleTransaction(userTxn *BroadcastedTransaction) error { } tailValue = nil } - if err := p.putdbElem(dbTxn, userTxn.Transaction.Hash(), &storageElem{ Txn: *userTxn, }); err != nil { return err } - if tailValue != nil { // Update old tail to point to the new item var oldTailElem storageElem + fmt.Println(" - error here") oldTailElem, err := p.dbElem(dbTxn, tailValue) if err != nil { return err } + fmt.Println(" - error here solved") oldTailElem.NextHash = userTxn.Transaction.Hash() if err = p.putdbElem(dbTxn, tailValue, &oldTailElem); err != nil { return err @@ -174,11 +174,9 @@ func (p *Pool) handleTransaction(userTxn *BroadcastedTransaction) error { return err } } - if err := p.updateTail(dbTxn, userTxn.Transaction.Hash()); err != nil { return err } - pLen, err := p.lenDB(dbTxn) if err != nil { return err @@ -194,18 +192,17 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error { return err } - // todo: should db overloading block the in-memory mempool?? select { case p.dbWriteChan <- userTxn: default: select { case _, ok := <-p.dbWriteChan: if !ok { - return errors.New("transaction pool database write channel is closed") + p.log.Errorw("cannot store user transasction in persistent pool, database write channel is closed") } - return ErrTxnPoolFull + p.log.Errorw("cannot store user transasction in persistent pool, database is full") default: - return ErrTxnPoolFull + p.log.Errorw("cannot store user transasction in persistent pool, database is full") } } @@ -300,6 +297,8 @@ func (p *Pool) Len() uint16 { // Len returns the number of transactions in the persistent pool func (p *Pool) LenDB() (uint16, error) { + p.wg.Add(1) + defer p.wg.Done() txn, err := p.db.NewTransaction(false) if err != nil { return 0, err @@ -339,19 +338,6 @@ func (p *Pool) headHash(txn db.Transaction, head *felt.Felt) error { }) } -func (p *Pool) HeadHash() (*felt.Felt, error) { - txn, err := p.db.NewTransaction(false) - if err != nil { - return nil, err - } - var head *felt.Felt - err = txn.Get(Head.Key(), func(b []byte) error { - head = new(felt.Felt).SetBytes(b) - return nil - }) - return head, err -} - func (p *Pool) updateHead(txn db.Transaction, head *felt.Felt) error { return txn.Set(Head.Key(), head.Marshal()) } @@ -367,6 +353,8 @@ func (p *Pool) updateTail(txn db.Transaction, tail *felt.Felt) error { return txn.Set(Tail.Key(), tail.Marshal()) } +// todo : error when unmarshalling the core.Transasction... +// but unmarshalling core.Transaction works fine in TransactionsByBlockNumber... func (p *Pool) dbElem(txn db.Transaction, itemKey *felt.Felt) (storageElem, error) { var item storageElem keyBytes := itemKey.Bytes() diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index 72fd97c149..25b0fbfd5e 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -12,7 +12,6 @@ import ( "github.com/NethermindEth/juno/mempool" "github.com/NethermindEth/juno/mocks" "github.com/NethermindEth/juno/utils" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" ) @@ -50,7 +49,7 @@ func TestMempool(t *testing.T) { require.NoError(t, err) l := pool.Len() - assert.Equal(t, uint16(0), l) + require.Equal(t, uint16(0), l) _, err = pool.Pop() require.Equal(t, err.Error(), "transaction pool is empty") @@ -59,7 +58,7 @@ func TestMempool(t *testing.T) { for i := uint64(1); i < 4; i++ { senderAddress := new(felt.Felt).SetUint64(i) state.EXPECT().ContractNonce(senderAddress).Return(new(felt.Felt).SetUint64(0), nil) - assert.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ + require.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ Transaction: &core.InvokeTransaction{ TransactionHash: new(felt.Felt).SetUint64(i), Nonce: new(felt.Felt).SetUint64(1), @@ -68,23 +67,23 @@ func TestMempool(t *testing.T) { }, })) l := pool.Len() - assert.Equal(t, uint16(i), l) + require.Equal(t, uint16(i), l) } // consume some (remove 1,2, keep 3) for i := uint64(1); i < 3; i++ { txn, err := pool.Pop() require.NoError(t, err) - assert.Equal(t, i, txn.Transaction.Hash().Uint64()) + require.Equal(t, i, txn.Transaction.Hash().Uint64()) l := pool.Len() - assert.Equal(t, uint16(3-i), l) + require.Equal(t, uint16(3-i), l) } // push multiple to non empty (push 4,5. now have 3,4,5) for i := uint64(4); i < 6; i++ { senderAddress := new(felt.Felt).SetUint64(i) state.EXPECT().ContractNonce(senderAddress).Return(new(felt.Felt).SetUint64(0), nil) - assert.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ + require.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ Transaction: &core.InvokeTransaction{ TransactionHash: new(felt.Felt).SetUint64(i), Nonce: new(felt.Felt).SetUint64(1), @@ -93,11 +92,11 @@ func TestMempool(t *testing.T) { }, })) l := pool.Len() - assert.Equal(t, uint16(i-2), l) + require.Equal(t, uint16(i-2), l) } // push more than max - assert.ErrorIs(t, pool.Push(&mempool.BroadcastedTransaction{ + require.ErrorIs(t, pool.Push(&mempool.BroadcastedTransaction{ Transaction: &core.InvokeTransaction{ TransactionHash: new(felt.Felt).SetUint64(123), }, @@ -107,9 +106,9 @@ func TestMempool(t *testing.T) { for i := uint64(3); i < 6; i++ { txn, err := pool.Pop() require.NoError(t, err) - assert.Equal(t, i, txn.Transaction.Hash().Uint64()) + require.Equal(t, i, txn.Transaction.Hash().Uint64()) } - assert.Equal(t, uint16(0), l) + require.Equal(t, uint16(0), l) _, err = pool.Pop() require.Equal(t, err.Error(), "transaction pool is empty") @@ -122,8 +121,9 @@ func TestRestoreMempool(t *testing.T) { mockCtrl := gomock.NewController(t) t.Cleanup(mockCtrl.Finish) state := mocks.NewMockStateHistoryReader(mockCtrl) - testDB, _, err := setupDatabase("testrestoremempool", true) + testDB, dbCloser, err := setupDatabase("testrestoremempool", true) require.NoError(t, err) + defer dbCloser() pool, closer, err := mempool.New(testDB, state, 1024, log) require.NoError(t, err) @@ -131,14 +131,14 @@ func TestRestoreMempool(t *testing.T) { // Check both pools are empty lenDB, err := pool.LenDB() require.NoError(t, err) - assert.Equal(t, uint16(0), lenDB) - assert.Equal(t, uint16(0), pool.Len()) + require.Equal(t, uint16(0), lenDB) + require.Equal(t, uint16(0), pool.Len()) // push multiple transactions to empty mempool (1,2,3) for i := uint64(1); i < 4; i++ { senderAddress := new(felt.Felt).SetUint64(i) state.EXPECT().ContractNonce(senderAddress).Return(new(felt.Felt).SetUint64(0), nil) - assert.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ + require.NoError(t, pool.Push(&mempool.BroadcastedTransaction{ Transaction: &core.InvokeTransaction{ TransactionHash: new(felt.Felt).SetUint64(i), Version: new(core.TransactionVersion).SetUint64(1), @@ -146,27 +146,25 @@ func TestRestoreMempool(t *testing.T) { Nonce: new(felt.Felt).SetUint64(0), }, })) - assert.Equal(t, uint16(i), pool.Len()) + require.Equal(t, uint16(i), pool.Len()) } // check the db has stored the transactions - time.Sleep(100 * time.Millisecond) + time.Sleep(4000 * time.Millisecond) lenDB, err = pool.LenDB() require.NoError(t, err) - assert.Equal(t, uint16(3), lenDB) - + require.Equal(t, uint16(3), lenDB) // Close the mempool require.NoError(t, closer()) - testDB, dbCloser, err := setupDatabase("testrestoremempool", false) + testDB, _, err = setupDatabase("testrestoremempool", false) require.NoError(t, err) - defer dbCloser() poolRestored, closer2, err := mempool.New(testDB, state, 1024, log) require.NoError(t, err) lenDB, err = poolRestored.LenDB() require.NoError(t, err) - assert.Equal(t, uint16(3), lenDB) - assert.Equal(t, uint16(3), poolRestored.Len()) + require.Equal(t, uint16(3), lenDB) + require.Equal(t, uint16(3), poolRestored.Len()) // Remove transactions _, err = poolRestored.Pop() @@ -175,15 +173,14 @@ func TestRestoreMempool(t *testing.T) { require.NoError(t, err) lenDB, err = poolRestored.LenDB() require.NoError(t, err) - assert.Equal(t, uint16(3), lenDB) - assert.Equal(t, uint16(1), poolRestored.Len()) + require.Equal(t, uint16(3), lenDB) + require.Equal(t, uint16(1), poolRestored.Len()) require.NoError(t, closer2()) } func TestWait(t *testing.T) { log := utils.NewNopZapLogger() - testDB := pebble.NewMemTest(t) mockCtrl := gomock.NewController(t) t.Cleanup(mockCtrl.Finish)