Skip to content

Commit

Permalink
some heap optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Dec 19, 2024
1 parent 58a69c7 commit 0518efb
Showing 1 changed file with 9 additions and 32 deletions.
41 changes: 9 additions & 32 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mempool
import (
"encoding/binary"
"errors"
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -51,36 +50,16 @@ func (p *Pool) WithValidator(validator ValidatorFunc) *Pool {
return p
}

func (p *Pool) rejectDuplicateTxn(userTxn *BroadcastedTransaction) error {
txHash := userTxn.Transaction.Hash().Marshal()
err := p.db.View(func(txn db.Transaction) error {
return txn.Get(txHash, func(val []byte) error {
if val != nil {
return fmt.Errorf("transaction already exists in the mempool: %x", txHash)
}
return nil
})
})
if errors.Is(err, db.ErrKeyNotFound) {
return nil
}
return err
}

// Push queues a transaction to the pool
func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
err := p.validator(userTxn)
if err != nil {
return err
}

err = p.rejectDuplicateTxn(userTxn)
if err != nil {
return err
}

if err := p.db.Update(func(txn db.Transaction) error {
tail, err := p.tailHash(txn)
var tail *felt.Felt
tail, err := p.tailHash(txn, tail)
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
return err
}
Expand All @@ -90,7 +69,6 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
}); err != nil {
return err
}
fmt.Println("tail", tail)
if tail != nil {
var oldTail storageElem
oldTail, err = p.elem(txn, tail)
Expand Down Expand Up @@ -135,7 +113,8 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
func (p *Pool) Pop() (BroadcastedTransaction, error) {
var nextTxn BroadcastedTransaction
return nextTxn, p.db.Update(func(txn db.Transaction) error {
headHash, err := p.headHash(txn)
var headHash *felt.Felt
headHash, err := p.headHash(txn, headHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,10 +191,9 @@ func (p *Pool) updateLen(txn db.Transaction, l uint64) error {
return txn.Set([]byte(poolLengthKey), binary.BigEndian.AppendUint64(nil, l))
}

func (p *Pool) headHash(txn db.Transaction) (*felt.Felt, error) {
var head *felt.Felt
return head, txn.Get([]byte(headKey), func(b []byte) error {
head = new(felt.Felt).SetBytes(b)
func (p *Pool) headHash(txn db.Transaction, headHash *felt.Felt) (*felt.Felt, error) {
return headHash, txn.Get([]byte(headKey), func(b []byte) error {
headHash = headHash.SetBytes(b)
return nil
})
}
Expand All @@ -224,10 +202,9 @@ func (p *Pool) updateHead(txn db.Transaction, head *felt.Felt) error {
return txn.Set([]byte(headKey), head.Marshal())
}

func (p *Pool) tailHash(txn db.Transaction) (*felt.Felt, error) {
var tail *felt.Felt
func (p *Pool) tailHash(txn db.Transaction, tail *felt.Felt) (*felt.Felt, error) {
return tail, txn.Get([]byte(tailKey), func(b []byte) error {
tail = new(felt.Felt).SetBytes(b)
tail = tail.SetBytes(b)
return nil
})
}
Expand Down

0 comments on commit 0518efb

Please sign in to comment.