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

Fix delete signed txn #1463

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
55 changes: 37 additions & 18 deletions server/handler/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {

var rows *sql.Rows
if status == "PENDING" {
rows, err = h.DB.Query(`SELECT t.id,t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
rows, err = h.DB.Query(`SELECT t.id, COALESCE(t.signed_at, '0001-01-01 00:00:00'::timestamp) AS signed_at, t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
json_agg(jsonb_build_object('pubkey', p.pubkey, 'address', p.address, 'multisig_address',p.multisig_address)) AS pubkeys FROM transactions t JOIN multisig_accounts m ON t.multisig_address = m.address JOIN pubkeys p ON t.multisig_address = p.multisig_address WHERE t.multisig_address=$1 and t.status='PENDING' GROUP BY t.id, t.multisig_address, m.threshold, t.messages LIMIT $2 OFFSET $3`,
multisigAddress, limit, (page-1)*limit)
} else {
rows, err = h.DB.Query(`SELECT t.id,t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
rows, err = h.DB.Query(`SELECT t.id, COALESCE(t.signed_at, '0001-01-01 00:00:00'::timestamp) AS signed_at, t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
json_agg(jsonb_build_object('pubkey', p.pubkey, 'address', p.address, 'multisig_address',p.multisig_address)) AS pubkeys FROM transactions t JOIN multisig_accounts m ON t.multisig_address = m.address JOIN pubkeys p ON t.multisig_address = p.multisig_address WHERE t.multisig_address=$1 and t.status <> 'PENDING' GROUP BY t.id, t.multisig_address, m.threshold, t.messages LIMIT $2 OFFSET $3`,
multisigAddress, limit, (page-1)*limit)
}
Expand All @@ -272,8 +272,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {

for rows.Next() {
var transaction schema.AllTransactionResult
var signedAt time.Time

if err := rows.Scan(
&transaction.ID,
&signedAt,
&transaction.MultisigAddress,
&transaction.Status,
&transaction.CreatedAt,
Expand All @@ -293,6 +296,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {
Log: err.Error(),
})
}
if signedAt.IsZero() {
transaction.SignedAt = time.Time{}
} else {
transaction.SignedAt = signedAt
}
transactions = append(transactions, transaction)
}
rows.Close()
Expand Down Expand Up @@ -509,35 +517,46 @@ func (h *Handler) DeleteTransaction(c echo.Context) error {
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
Status: "error",
Message: "invalid transaction id ",
Message: "invalid transaction id",
})
}

_, err = h.DB.Exec(`DELETE from transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
// Fetch signed_at before attempting to delete, to avoid issues if the transaction does not exist
var signedAt time.Time
row := h.DB.QueryRow(`SELECT signed_at FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
err = row.Scan(&signedAt)
if err != nil {
if err == sql.ErrNoRows {
return c.JSON(http.StatusNotFound, model.ErrorResponse{
Status: "error",
Message: "transaction not found",
})
}
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: "failed to fetch transaction details",
Log: err.Error(),
})
}

// Delete the transaction
_, err = h.DB.Exec(`DELETE FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: "failed to delete transaction",
Log: err.Error(),
})
}

row := h.DB.QueryRow(`SELECT signed_at FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)

var transaction schema.Transaction
row.Scan(
&transaction.Signatures,
)

if !transaction.SignedAt.IsZero() {
txSignedAt := transaction.SignedAt

_, err = h.DB.Exec("UPDATE transactions SET signatures='[]'::jsonb WHERE multisig_address=$1 AND signed_at > $2", address, txSignedAt)
// Clear signatures for transactions with signed_at > txSignedAt
if !signedAt.IsZero() {
_, err = h.DB.Exec(`UPDATE transactions SET signatures='[]'::jsonb WHERE multisig_address=$1 AND signed_at > $2`, address, signedAt)
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: err.Error(),
Message: "failed to update transaction signatures",
Log: err.Error(),
})
}
}
Expand Down
Loading