Skip to content

Commit 70db337

Browse files
committed
fix lint + tests
1 parent 85aaa37 commit 70db337

File tree

3 files changed

+70
-82
lines changed

3 files changed

+70
-82
lines changed

mempool/init_test.go

-5
This file was deleted.

mempool/mempool.go

+28-41
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/NethermindEth/juno/core/felt"
1111
"github.com/NethermindEth/juno/db"
1212
"github.com/NethermindEth/juno/encoder"
13+
"github.com/NethermindEth/juno/utils"
1314
)
1415

1516
var ErrTxnPoolFull = errors.New("transaction pool is full")
@@ -34,6 +35,7 @@ type txnList struct {
3435

3536
// Pool stores the transactions in a linked list for its inherent FCFS behaviour
3637
type Pool struct {
38+
log utils.SimpleLogger
3739
state core.StateReader
3840
db db.DB // persistent mempool
3941
txPushed chan struct{}
@@ -43,20 +45,21 @@ type Pool struct {
4345
wg sync.WaitGroup
4446
}
4547

46-
// New initializes the Pool and starts the database writer goroutine.
48+
// New initialises the Pool and starts the database writer goroutine.
4749
// It is the responsibility of the user to call the cancel function if the context is cancelled
48-
func New(db db.DB, state core.StateReader, maxNumTxns uint16) (*Pool, func() error, error) {
50+
func New(persistentPool db.DB, state core.StateReader, maxNumTxns uint16, log utils.SimpleLogger) (*Pool, func() error, error) {
4951
pool := &Pool{
52+
log: log,
5053
state: state,
51-
db: db, // todo: txns should be deleted everytime a new block is stored (builder responsibility)
54+
db: persistentPool, // todo: txns should be deleted everytime a new block is stored (builder responsibility)
5255
txPushed: make(chan struct{}, 1),
5356
txnList: &txnList{},
5457
maxNumTxns: maxNumTxns,
5558
dbWriteChan: make(chan *BroadcastedTransaction, maxNumTxns),
5659
}
5760

5861
if err := pool.loadFromDB(); err != nil {
59-
return nil, nil, fmt.Errorf("failed to load transactions from database into the in-memory transaction list: %v\n", err)
62+
return nil, nil, fmt.Errorf("failed to load transactions from database into the in-memory transaction list: %v", err)
6063
}
6164

6265
pool.wg.Add(1)
@@ -74,25 +77,20 @@ func New(db db.DB, state core.StateReader, maxNumTxns uint16) (*Pool, func() err
7477

7578
func (p *Pool) dbWriter() {
7679
defer p.wg.Done()
77-
for {
78-
select {
79-
case txn, ok := <-p.dbWriteChan:
80-
if !ok {
81-
return
82-
}
83-
p.handleTransaction(txn)
84-
}
80+
for txn := range p.dbWriteChan {
81+
err := p.handleTransaction(txn)
82+
p.log.Errorw("error in handling user transaction in persistent mempool", "err", err)
8583
}
8684
}
8785

8886
// loadFromDB restores the in-memory transaction pool from the database
8987
func (p *Pool) loadFromDB() error {
9088
return p.db.View(func(txn db.Transaction) error {
91-
len, err := p.LenDB()
89+
lenDB, err := p.LenDB()
9290
if err != nil {
9391
return err
9492
}
95-
if len >= p.maxNumTxns {
93+
if lenDB >= p.maxNumTxns {
9694
return ErrTxnPoolFull
9795
}
9896
headValue := new(felt.Felt)
@@ -152,13 +150,11 @@ func (p *Pool) handleTransaction(userTxn *BroadcastedTransaction) error {
152150
}
153151
tailValue = nil
154152
}
155-
156153
if err := p.putdbElem(dbTxn, userTxn.Transaction.Hash(), &storageElem{
157154
Txn: *userTxn,
158155
}); err != nil {
159156
return err
160157
}
161-
162158
if tailValue != nil {
163159
// Update old tail to point to the new item
164160
var oldTailElem storageElem
@@ -176,16 +172,14 @@ func (p *Pool) handleTransaction(userTxn *BroadcastedTransaction) error {
176172
return err
177173
}
178174
}
179-
180175
if err := p.updateTail(dbTxn, userTxn.Transaction.Hash()); err != nil {
181176
return err
182177
}
183-
184178
pLen, err := p.lenDB(dbTxn)
185179
if err != nil {
186180
return err
187181
}
188-
return p.updateLen(dbTxn, uint16(pLen+1))
182+
return p.updateLen(dbTxn, pLen+1)
189183
})
190184
}
191185

@@ -196,18 +190,17 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
196190
return err
197191
}
198192

199-
// todo: should db overloading block the in-memory mempool??
200193
select {
201194
case p.dbWriteChan <- userTxn:
202195
default:
203196
select {
204197
case _, ok := <-p.dbWriteChan:
205198
if !ok {
206-
return errors.New("transaction pool database write channel is closed")
199+
p.log.Errorw("cannot store user transasction in persistent pool, database write channel is closed")
207200
}
208-
return ErrTxnPoolFull
201+
p.log.Errorw("cannot store user transasction in persistent pool, database is full")
209202
default:
210-
return ErrTxnPoolFull
203+
p.log.Errorw("cannot store user transasction in persistent pool, database is full")
211204
}
212205
}
213206

@@ -232,7 +225,7 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error {
232225
}
233226

234227
func (p *Pool) validate(userTxn *BroadcastedTransaction) error {
235-
if p.txnList.len+1 >= uint16(p.maxNumTxns) {
228+
if p.txnList.len+1 >= p.maxNumTxns {
236229
return ErrTxnPoolFull
237230
}
238231

@@ -246,7 +239,7 @@ func (p *Pool) validate(userTxn *BroadcastedTransaction) error {
246239
case *core.DeclareTransaction:
247240
nonce, err := p.state.ContractNonce(t.SenderAddress)
248241
if err != nil {
249-
return fmt.Errorf("validation failed, error when retrieving nonce, %v:", err)
242+
return fmt.Errorf("validation failed, error when retrieving nonce, %v", err)
250243
}
251244
if nonce.Cmp(t.Nonce) > 0 {
252245
return fmt.Errorf("validation failed, existing nonce %s, but received nonce %s", nonce, t.Nonce)
@@ -257,7 +250,7 @@ func (p *Pool) validate(userTxn *BroadcastedTransaction) error {
257250
}
258251
nonce, err := p.state.ContractNonce(t.SenderAddress)
259252
if err != nil {
260-
return fmt.Errorf("validation failed, error when retrieving nonce, %v:", err)
253+
return fmt.Errorf("validation failed, error when retrieving nonce, %v", err)
261254
}
262255
if nonce.Cmp(t.Nonce) > 0 {
263256
return fmt.Errorf("validation failed, existing nonce %s, but received nonce %s", nonce, t.Nonce)
@@ -302,12 +295,17 @@ func (p *Pool) Len() uint16 {
302295

303296
// Len returns the number of transactions in the persistent pool
304297
func (p *Pool) LenDB() (uint16, error) {
298+
p.wg.Add(1)
299+
defer p.wg.Done()
305300
txn, err := p.db.NewTransaction(false)
306301
if err != nil {
307302
return 0, err
308303
}
309-
defer txn.Discard()
310-
return p.lenDB(txn)
304+
lenDB, err := p.lenDB(txn)
305+
if err != nil {
306+
return 0, err
307+
}
308+
return lenDB, txn.Discard()
311309
}
312310

313311
func (p *Pool) lenDB(txn db.Transaction) (uint16, error) {
@@ -338,19 +336,6 @@ func (p *Pool) headHash(txn db.Transaction, head *felt.Felt) error {
338336
})
339337
}
340338

341-
func (p *Pool) HeadHash() (*felt.Felt, error) {
342-
txn, err := p.db.NewTransaction(false)
343-
if err != nil {
344-
return nil, err
345-
}
346-
var head *felt.Felt
347-
err = txn.Get(Head.Key(), func(b []byte) error {
348-
head = new(felt.Felt).SetBytes(b)
349-
return nil
350-
})
351-
return head, err
352-
}
353-
354339
func (p *Pool) updateHead(txn db.Transaction, head *felt.Felt) error {
355340
return txn.Set(Head.Key(), head.Marshal())
356341
}
@@ -366,6 +351,8 @@ func (p *Pool) updateTail(txn db.Transaction, tail *felt.Felt) error {
366351
return txn.Set(Tail.Key(), tail.Marshal())
367352
}
368353

354+
// todo : error when unmarshalling the core.Transasction...
355+
// but unmarshalling core.Transaction works fine in TransactionsByBlockNumber...
369356
func (p *Pool) dbElem(txn db.Transaction, itemKey *felt.Felt) (storageElem, error) {
370357
var item storageElem
371358
keyBytes := itemKey.Bytes()

0 commit comments

Comments
 (0)