Skip to content

Commit

Permalink
refactor: add tx_index to account transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnt1811 committed Sep 6, 2023
1 parent 2a75303 commit af64cb9
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion infrastructure/httpapi/handlers/account_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (handler *AccountTransactions) SyncAccountInternalTxsByTxHash(ctx *fasthttp
}
tx.Messages = append(tx.Messages, tmpMessage)
txs = append(txs, tx)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTx()...)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTxOrTokenTransfer(index)...)
}
err = handler.accountTransactionsView.InsertAll(accountTransactionRows)
if err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func RunInternalTxsConsumer(rdbHandle *rdb.Handle, config *config.Config, logger
}
tx.Messages = append(tx.Messages, tmpMessage)
txs = append(txs, tx)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTx()...)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTxOrTokenTransfer(int(internalTx.Index))...)
}
if len(txs) == 0 {
// commit offset when no internal txs are valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func RunTokenTransfersConsumer(rdbHandle *rdb.Handle, config *config.Config, log
}
tx.Messages = append(tx.Messages, tmpMessage)
txs = append(txs, tx)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTx()...)
accountTransactionRows = append(accountTransactionRows, transactionInfo.ToRowsIncludingInternalTxOrTokenTransfer(int(tokenTransfer.LogIndex))...)

err = rdbAccountTransactionsView.InsertAll(accountTransactionRows)
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion projection/account_transaction/account_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,20 +628,22 @@ func (info *TransactionInfo) ToRows() []view.AccountTransactionBaseRow {
clonedRow := info.Row
clonedRow.Account = account
clonedRow.IsInternalTx = false
clonedRow.TxIndex = 0
rows = append(rows, clonedRow)
}

return rows
}

func (info *TransactionInfo) ToRowsIncludingInternalTx() []view.AccountTransactionBaseRow {
func (info *TransactionInfo) ToRowsIncludingInternalTxOrTokenTransfer(index int) []view.AccountTransactionBaseRow {
info.FillMessageTypes()

rows := make([]view.AccountTransactionBaseRow, 0)
for account := range info.involvedAccounts {
clonedRow := info.Row
clonedRow.Account = account
clonedRow.IsInternalTx = true
clonedRow.TxIndex = index
rows = append(rows, clonedRow)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ALTER TABLE view_account_transactions
ADD CONSTRAINT account_transactions_unique
UNIQUE (block_height, account, transaction_hash, success, from_address, to_address, is_internal_tx);
UNIQUE (block_height, account, transaction_hash, success, from_address, to_address, is_internal_tx, tx_index);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE view_account_transaction_data
ADD COLUMN tx_index SMALLINT NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE view_account_transactions
ADD COLUMN tx_index SMALLINT NOT NULL DEFAULT 0;
3 changes: 3 additions & 0 deletions projection/account_transaction/view/account_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (accountMessagesView *AccountTransactions) InsertAll(
"from_address",
"to_address",
"is_internal_tx",
"tx_index",
)
}

Expand All @@ -77,6 +78,7 @@ func (accountMessagesView *AccountTransactions) InsertAll(
row.FromAddress,
row.ToAddress,
row.IsInternalTx,
row.TxIndex,
)
pendingRowCount += 1

Expand Down Expand Up @@ -433,6 +435,7 @@ type AccountTransactionBaseRow struct {
FromAddress string `json:"from_address,omitempty"`
ToAddress string `json:"to_address,omitempty"`
IsInternalTx bool `json:"is_internal_tx,omitempty"`
TxIndex int `json:"tx_index,omitempty"`
}

type AccountTransactionReadRow struct {
Expand Down

0 comments on commit af64cb9

Please sign in to comment.