Skip to content

Commit

Permalink
fix(proposer): SQLLite write mutex (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani authored Oct 17, 2024
1 parent a907ba1 commit f6f599c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions proposer/op/proposer/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"time"

"entgo.io/ent/dialect/sql"
Expand All @@ -18,6 +19,7 @@ import (
type ProofDB struct {
writeClient *ent.Client
readClient *ent.Client
writeMutex sync.Mutex
}

// InitDB initializes the database and returns a handle to it.
Expand Down Expand Up @@ -84,6 +86,9 @@ func (db *ProofDB) CloseDB() error {

// NewEntry creates a new proof request entry in the database.
func (db *ProofDB) NewEntry(proofType proofrequest.Type, start, end uint64) error {
db.writeMutex.Lock()
defer db.writeMutex.Unlock()

now := uint64(time.Now().Unix())
_, err := db.writeClient.ProofRequest.
Create().
Expand All @@ -104,6 +109,9 @@ func (db *ProofDB) NewEntry(proofType proofrequest.Type, start, end uint64) erro

// UpdateProofStatus updates the status of a proof request in the database.
func (db *ProofDB) UpdateProofStatus(id int, proofStatus proofrequest.Status) error {
db.writeMutex.Lock()
defer db.writeMutex.Unlock()

_, err := db.writeClient.ProofRequest.Update().
Where(proofrequest.ID(id)).
SetStatus(proofStatus).
Expand All @@ -115,6 +123,9 @@ func (db *ProofDB) UpdateProofStatus(id int, proofStatus proofrequest.Status) er

// SetProverRequestID sets the prover request ID for a proof request in the database.
func (db *ProofDB) SetProverRequestID(id int, proverRequestID string) error {
db.writeMutex.Lock()
defer db.writeMutex.Unlock()

_, err := db.writeClient.ProofRequest.Update().
Where(proofrequest.ID(id)).
SetProverRequestID(proverRequestID).
Expand All @@ -131,6 +142,9 @@ func (db *ProofDB) SetProverRequestID(id int, proverRequestID string) error {

// AddFulfilledProof adds a proof to a proof request in the database and sets the status to COMPLETE.
func (db *ProofDB) AddFulfilledProof(id int, proof []byte) error {
db.writeMutex.Lock()
defer db.writeMutex.Unlock()

// Start a transaction
tx, err := db.writeClient.Tx(context.Background())
if err != nil {
Expand Down Expand Up @@ -194,6 +208,9 @@ func (db *ProofDB) GetNumberOfRequestsWithStatuses(statuses ...proofrequest.Stat

// AddL1BlockInfoToAggRequest adds the L1 block info to the existing AGG proof request.
func (db *ProofDB) AddL1BlockInfoToAggRequest(startBlock, endBlock, l1BlockNumber uint64, l1BlockHash string) (*ent.ProofRequest, error) {
db.writeMutex.Lock()
defer db.writeMutex.Unlock()

// Perform the update
rowsAffected, err := db.writeClient.ProofRequest.Update().
Where(
Expand Down

0 comments on commit f6f599c

Please sign in to comment.