Skip to content

Commit

Permalink
comment: split runtime and persistent types
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Jan 14, 2025
1 parent 52dafd1 commit 99b8ede
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ type BroadcastedTransaction struct {
DeclaredClass core.Class
}

// storageElem defines a node for both the
// in-memory and persistent linked-list
type storageElem struct {
// runtime mempool txn
type memPoolTxn struct {
Txn BroadcastedTransaction
Next *memPoolTxn
}

// persistent db txn value
type dbPoolTxn struct {
Txn BroadcastedTransaction
NextHash *felt.Felt // persistent db
Next *storageElem // in-memory
NextHash *felt.Felt
}

// memTxnList represents a linked list of user transactions at runtime"
// memTxnList represents a linked list of user transactions at runtime
type memTxnList struct {
head *storageElem
tail *storageElem
head *memPoolTxn
tail *memPoolTxn
len int
mu sync.Mutex
}

func (t *memTxnList) push(newNode *storageElem) {
func (t *memTxnList) push(newNode *memPoolTxn) {
t.mu.Lock()
defer t.mu.Unlock()
if t.tail != nil {
Expand Down Expand Up @@ -130,24 +134,24 @@ func (p *Pool) LoadFromDB() error {
// loop through the persistent pool and push nodes to the in-memory pool
currentHash := headValue
for currentHash != nil {
curElem, err := p.readDBElem(txn, currentHash)
curDBElem, err := p.readDBElem(txn, currentHash)
if err != nil {
return err
}

Check warning on line 140 in mempool/mempool.go

View check run for this annotation

Codecov / codecov/patch

mempool/mempool.go#L139-L140

Added lines #L139 - L140 were not covered by tests
newNode := &storageElem{
Txn: curElem.Txn,
newMemPoolTxn := &memPoolTxn{
Txn: curDBElem.Txn,
}
if curElem.NextHash != nil {
nxtElem, err := p.readDBElem(txn, curElem.NextHash)
if curDBElem.NextHash != nil {
nextDBTxn, err := p.readDBElem(txn, curDBElem.NextHash)
if err != nil {
return err
}

Check warning on line 148 in mempool/mempool.go

View check run for this annotation

Codecov / codecov/patch

mempool/mempool.go#L147-L148

Added lines #L147 - L148 were not covered by tests
newNode.Next = &storageElem{
Txn: nxtElem.Txn,
newMemPoolTxn.Next = &memPoolTxn{
Txn: nextDBTxn.Txn,
}
}
p.memTxnList.push(newNode)
currentHash = curElem.NextHash
p.memTxnList.push(newMemPoolTxn)
currentHash = curDBElem.NextHash
}
return nil
})
Expand All @@ -163,12 +167,12 @@ func (p *Pool) writeToDB(userTxn *BroadcastedTransaction) error {
}

Check warning on line 167 in mempool/mempool.go

View check run for this annotation

Codecov / codecov/patch

mempool/mempool.go#L166-L167

Added lines #L166 - L167 were not covered by tests
tailValue = nil
}
if err := p.setDBElem(dbTxn, &storageElem{Txn: *userTxn}); err != nil {
if err := p.setDBElem(dbTxn, &dbPoolTxn{Txn: *userTxn}); err != nil {
return err
}

Check warning on line 172 in mempool/mempool.go

View check run for this annotation

Codecov / codecov/patch

mempool/mempool.go#L171-L172

Added lines #L171 - L172 were not covered by tests
if tailValue != nil {
// Update old tail to point to the new item
var oldTailElem storageElem
var oldTailElem dbPoolTxn
oldTailElem, err := p.readDBElem(dbTxn, tailValue)
if err != nil {
return err
Expand Down Expand Up @@ -215,7 +219,7 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
}
}

newNode := &storageElem{Txn: *userTxn, Next: nil}
newNode := &memPoolTxn{Txn: *userTxn, Next: nil}
p.memTxnList.push(newNode)

select {
Expand Down Expand Up @@ -339,16 +343,16 @@ func (p *Pool) updateTail(txn db.Transaction, tail *felt.Felt) error {
return txn.Set(db.MempoolTail.Key(), tail.Marshal())
}

func (p *Pool) readDBElem(txn db.Transaction, itemKey *felt.Felt) (storageElem, error) {
var item storageElem
func (p *Pool) readDBElem(txn db.Transaction, itemKey *felt.Felt) (dbPoolTxn, error) {
var item dbPoolTxn
keyBytes := itemKey.Bytes()
err := txn.Get(db.MempoolNode.Key(keyBytes[:]), func(b []byte) error {
return encoder.Unmarshal(b, &item)
})
return item, err
}

func (p *Pool) setDBElem(txn db.Transaction, item *storageElem) error {
func (p *Pool) setDBElem(txn db.Transaction, item *dbPoolTxn) error {
itemBytes, err := encoder.Marshal(item)
if err != nil {
return err
Expand Down

0 comments on commit 99b8ede

Please sign in to comment.