Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update tx state post execution #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions db/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,29 @@ func (DBI *DB) PopTxs() (txs []core.Tx, err error) {

DBI.Logger.Info("Found pending txs", "txCount", len(pendingTxs))

// update the status fo pending transactions to processing
err = DBI.UpdateTxStatuses(pendingTxs, core.TX_STATUS_PROCESSING)
if err != nil {
tx.Rollback()
return txs, err
}

return pendingTxs, tx.Commit().Error
}

// UpdateTxStatuses updates the tx status
func (DBI *DB) UpdateTxStatuses(txList []core.Tx, status int) error {
var ids []string
for _, tx := range pendingTxs {
for _, tx := range txList {
ids = append(ids, tx.ID)
}

// update the transactions from pending to processing
errs := tx.Table("txes").Where("id IN (?)", ids).Updates(map[string]interface{}{"status": core.TX_STATUS_PROCESSING}).GetErrors()
err := DBI.Instance.Table("txes").Where("id IN (?)", ids).Updates(map[string]interface{}{"status": status}).Error
if err != nil {
tx.Rollback()
DBI.Logger.Error("errors while processing transactions", errs)
return
DBI.Logger.Error("errors while processing transactions", "error", err)
return err
}

return pendingTxs, tx.Commit().Error
return nil
}

// FetchTxType finds out transactions with highest count in the DB
Expand Down Expand Up @@ -306,7 +315,7 @@ func ProcessTxs(bz *bazooka.Bazooka, DBI *DB, txs []core.Tx, txsPerCommitment []
currentCommitmentIdx := 0

var processedTxs []core.Tx
// var revertedTxs []core.Tx
var revertedTxs []core.Tx

for i, tx := range txs {
// fetch the current root
Expand All @@ -322,7 +331,7 @@ func ProcessTxs(bz *bazooka.Bazooka, DBI *DB, txs []core.Tx, txsPerCommitment []
// and skip rest of the loop
if err != nil {
DBI.Logger.Info("Reverting tx", "hash", tx.TxHash, "error", err)
// revertedTxs = append(revertedTxs, tx)
revertedTxs = append(revertedTxs, tx)
continue
}

Expand Down Expand Up @@ -363,7 +372,19 @@ func ProcessTxs(bz *bazooka.Bazooka, DBI *DB, txs []core.Tx, txsPerCommitment []
}
}

// TODO status update for reverted and succesful txs
// update the statues for processed and reverted txs
// we dont bubble up the errors incase we arent able to update tx status
// we log the error and move on
err = DBI.UpdateTxStatuses(processedTxs, core.TX_STATUS_PROCESSED)
if err != nil {
DBI.Logger.Error("Unable to update tx status for processed txs", "count", len(processedTxs))
return commitments, nil
}
err = DBI.UpdateTxStatuses(revertedTxs, core.TX_STATUS_REVERTED)
if err != nil {
DBI.Logger.Error("Unable to update tx status for reverted txs", "count", len(revertedTxs))
return commitments, nil
}

return commitments, nil
}
Expand Down