Skip to content

Commit

Permalink
wip - encoder failing to unmarhsal for terminal test run, but NOT in …
Browse files Browse the repository at this point in the history
…vscode test run..
  • Loading branch information
rianhughes committed Jan 9, 2025
1 parent 151e945 commit dbb495f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
30 changes: 9 additions & 21 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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()
Expand Down
49 changes: 23 additions & 26 deletions mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
},
Expand All @@ -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")
Expand All @@ -122,51 +121,50 @@ 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)

// 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),
SenderAddress: senderAddress,
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()
Expand All @@ -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)
Expand Down

0 comments on commit dbb495f

Please sign in to comment.