From 011a170a03439b04768c793361565c8178f6061f Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 28 Feb 2024 11:44:47 +0100 Subject: [PATCH] fix: dup tx Remove duplicate gas only TX Remove Send and Receive from self, only the send row is kept --- services/wallet/transfer/database.go | 36 +++++++++++++++++++---- services/wallet/transfer/database_test.go | 6 +--- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/services/wallet/transfer/database.go b/services/wallet/transfer/database.go index b4445203a..9f93bffcb 100644 --- a/services/wallet/transfer/database.go +++ b/services/wallet/transfer/database.go @@ -492,7 +492,9 @@ func updateOrInsertTransfersDBFields(creator statementCreator, transfers []trans log.Error("can't save transfer", "b-hash", t.blockHash, "b-n", t.blockNumber, "a", t.address, "h", t.id) return err } + } + for _, t := range transfers { err = removeGasOnlyEthTransfer(creator, t) if err != nil { log.Error("can't remove gas only eth transfer", "b-hash", t.blockHash, "b-n", t.blockNumber, "a", t.address, "h", t.id, "err", err) @@ -503,18 +505,40 @@ func updateOrInsertTransfersDBFields(creator statementCreator, transfers []trans } func removeGasOnlyEthTransfer(creator statementCreator, t transferDBFields) error { - if t.transferType != w_common.EthTransfer { - query, err := creator.Prepare(`DELETE FROM transfers WHERE tx_hash = ? AND address = ? AND network_id = ? - AND account_nonce = ? AND type = 'eth' AND amount_padded128hex = '00000000000000000000000000000000'`) + if t.transferType == w_common.EthTransfer { + countQuery, err := creator.Prepare(`SELECT COUNT(*) FROM transfers WHERE tx_hash = ?`) + if err != nil { + return err + } + defer countQuery.Close() + + var count int + err = countQuery.QueryRow(t.txHash).Scan(&count) if err != nil { return err } - _, err = query.Exec(t.txHash, t.address, t.chainID, t.txNonce) - if err != nil { - return err + // If there's only one (or none), return without deleting + if count <= 1 { + log.Debug("Only one or no transfer found with the same tx_hash, skipping deletion.") + return nil } } + query, err := creator.Prepare(`DELETE FROM transfers WHERE tx_hash = ? AND address = ? AND network_id = ? AND account_nonce = ? AND type = 'eth' AND amount_padded128hex = '00000000000000000000000000000000'`) + if err != nil { + return err + } + defer query.Close() + + res, err := query.Exec(t.txHash, t.address, t.chainID, t.txNonce) + if err != nil { + return err + } + count, err := res.RowsAffected() + if err != nil { + return err + } + log.Debug("removeGasOnlyEthTransfer row deleted ", count) return nil } diff --git a/services/wallet/transfer/database_test.go b/services/wallet/transfer/database_test.go index a411aceae..c83e2b3d8 100644 --- a/services/wallet/transfer/database_test.go +++ b/services/wallet/transfer/database_test.go @@ -118,11 +118,7 @@ func TestDBGetTransfersFromBlock(t *testing.T) { require.NoError(t, saveTransfersMarkBlocksLoaded(db.client, 777, address, transfers, blockNumbers)) rst, err := db.GetTransfers(777, big.NewInt(7), nil) require.NoError(t, err) - require.Len(t, rst, 3) - - rst, err = db.GetTransfers(777, big.NewInt(2), big.NewInt(5)) - require.NoError(t, err) - require.Len(t, rst, 4) + require.Len(t, rst, 1) } func TestGetTransfersForIdentities(t *testing.T) {