fix: dup tx

Remove duplicate gas only TX
Remove Send and Receive from self, only the send row is kept
This commit is contained in:
Anthony Laibe 2024-02-28 11:44:47 +01:00 committed by Anthony Laibe
parent 390d706b3f
commit 011a170a03
2 changed files with 31 additions and 11 deletions

View File

@ -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 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
}

View File

@ -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) {