-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
comments: db_utils.go, inline, felt.Zero
- Loading branch information
1 parent
99b8ede
commit fd3959d
Showing
3 changed files
with
86 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package mempool | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/db" | ||
"github.com/NethermindEth/juno/encoder" | ||
) | ||
|
||
func headValue(txn db.Transaction, head *felt.Felt) error { | ||
return txn.Get(db.MempoolHead.Key(), func(b []byte) error { | ||
head.SetBytes(b) | ||
return nil | ||
}) | ||
} | ||
|
||
func tailValue(txn db.Transaction, tail *felt.Felt) error { | ||
return txn.Get(db.MempoolTail.Key(), func(b []byte) error { | ||
tail.SetBytes(b) | ||
return nil | ||
}) | ||
} | ||
|
||
func updateHead(txn db.Transaction, head *felt.Felt) error { | ||
return txn.Set(db.MempoolHead.Key(), head.Marshal()) | ||
} | ||
|
||
func updateTail(txn db.Transaction, tail *felt.Felt) error { | ||
return txn.Set(db.MempoolTail.Key(), tail.Marshal()) | ||
} | ||
|
||
func 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 setDBElem(txn db.Transaction, item *dbPoolTxn) error { | ||
itemBytes, err := encoder.Marshal(item) | ||
if err != nil { | ||
return err | ||
} | ||
keyBytes := item.Txn.Transaction.Hash().Bytes() | ||
return txn.Set(db.MempoolNode.Key(keyBytes[:]), itemBytes) | ||
} | ||
|
||
func lenDB(txn db.Transaction) (int, error) { | ||
var l int | ||
err := txn.Get(db.MempoolLength.Key(), func(b []byte) error { | ||
l = int(new(big.Int).SetBytes(b).Int64()) | ||
return nil | ||
}) | ||
|
||
if err != nil && errors.Is(err, db.ErrKeyNotFound) { | ||
return 0, nil | ||
} | ||
return l, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters