Skip to content

Commit 34e38e0

Browse files
authored
Conform getTraces to api standard (#147)
### TL;DR Updated the GetTraces function to return a QueryResult type instead of a raw array of traces. ### What changed? - Modified the GetTraces function signature to return `QueryResult[common.Trace]` instead of `[]common.Trace` - Extracted trace scanning logic into a separate `scanTrace` function - Updated the GetTraces implementation to use the generic `executeQuery` function - Updated the mock storage interface to reflect the new return type ### How to test? 1. Verify that existing trace queries continue to work as expected 2. Ensure that pagination and filtering functionality works correctly with traces 3. Confirm that the mock storage interface is properly implemented in test cases ### Why make this change? This change brings consistency to the codebase by making the GetTraces function return the same QueryResult type as other query functions. This enables better pagination support and maintains a uniform interface across all data retrieval methods.
2 parents 227fea9 + e885ea4 commit 34e38e0

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

internal/storage/clickhouse.go

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ func (c *ClickHouseConnector) GetLogs(qf QueryFilter) (QueryResult[common.Log],
306306
return executeQuery[common.Log](c, "logs", columns, qf, scanLog)
307307
}
308308

309+
func (c *ClickHouseConnector) GetTraces(qf QueryFilter) (traces QueryResult[common.Trace], err error) {
310+
columns := "chain_id, block_number, block_hash, block_timestamp, transaction_hash, transaction_index, subtraces, trace_address, type, call_type, error, from_address, to_address, gas, gas_used, input, output, value, author, reward_type, refund_address"
311+
return executeQuery[common.Trace](c, "traces", columns, qf, scanTrace)
312+
}
313+
309314
func (c *ClickHouseConnector) GetAggregations(table string, qf QueryFilter) (QueryResult[interface{}], error) {
310315
// Build the SELECT clause with aggregates
311316
selectColumns := strings.Join(append(qf.GroupBy, qf.Aggregates...), ", ")
@@ -596,6 +601,37 @@ func scanBlock(rows driver.Rows) (common.Block, error) {
596601
return block, nil
597602
}
598603

604+
func scanTrace(rows driver.Rows) (common.Trace, error) {
605+
var trace common.Trace
606+
err := rows.Scan(
607+
&trace.ChainID,
608+
&trace.BlockNumber,
609+
&trace.BlockHash,
610+
&trace.BlockTimestamp,
611+
&trace.TransactionHash,
612+
&trace.TransactionIndex,
613+
&trace.Subtraces,
614+
&trace.TraceAddress,
615+
&trace.TraceType,
616+
&trace.CallType,
617+
&trace.Error,
618+
&trace.FromAddress,
619+
&trace.ToAddress,
620+
&trace.Gas,
621+
&trace.GasUsed,
622+
&trace.Input,
623+
&trace.Output,
624+
&trace.Value,
625+
&trace.Author,
626+
&trace.RewardType,
627+
&trace.RefundAddress,
628+
)
629+
if err != nil {
630+
return common.Trace{}, fmt.Errorf("error scanning trace: %w", err)
631+
}
632+
return trace, nil
633+
}
634+
599635
func (c *ClickHouseConnector) GetMaxBlockNumber(chainId *big.Int) (maxBlockNumber *big.Int, err error) {
600636
query := fmt.Sprintf("SELECT number FROM %s.blocks WHERE is_deleted = 0", c.cfg.Database)
601637
if chainId.Sign() > 0 {
@@ -843,60 +879,6 @@ func (c *ClickHouseConnector) insertTraces(traces *[]common.Trace) error {
843879
return nil
844880
}
845881

846-
func (c *ClickHouseConnector) GetTraces(qf QueryFilter) (traces []common.Trace, err error) {
847-
columns := "chain_id, block_number, block_hash, block_timestamp, transaction_hash, transaction_index, subtraces, trace_address, type, call_type, error, from_address, to_address, gas, gas_used, input, output, value, author, reward_type, refund_address"
848-
query := fmt.Sprintf("SELECT %s FROM %s.traces WHERE block_number IN (%s) AND is_deleted = 0",
849-
columns, c.cfg.Database, getBlockNumbersStringArray(qf.BlockNumbers))
850-
851-
if qf.ChainId.Sign() > 0 {
852-
query += fmt.Sprintf(" AND chain_id = %s", qf.ChainId.String())
853-
}
854-
855-
query += getLimitClause(int(qf.Limit))
856-
857-
if err := common.ValidateQuery(query); err != nil {
858-
return nil, err
859-
}
860-
rows, err := c.conn.Query(context.Background(), query)
861-
if err != nil {
862-
return nil, err
863-
}
864-
defer rows.Close()
865-
866-
for rows.Next() {
867-
var trace common.Trace
868-
err := rows.Scan(
869-
&trace.ChainID,
870-
&trace.BlockNumber,
871-
&trace.BlockHash,
872-
&trace.BlockTimestamp,
873-
&trace.TransactionHash,
874-
&trace.TransactionIndex,
875-
&trace.Subtraces,
876-
&trace.TraceAddress,
877-
&trace.TraceType,
878-
&trace.CallType,
879-
&trace.Error,
880-
&trace.FromAddress,
881-
&trace.ToAddress,
882-
&trace.Gas,
883-
&trace.GasUsed,
884-
&trace.Input,
885-
&trace.Output,
886-
&trace.Value,
887-
&trace.Author,
888-
&trace.RewardType,
889-
&trace.RefundAddress,
890-
)
891-
if err != nil {
892-
zLog.Error().Err(err).Msg("Error scanning transaction")
893-
return nil, err
894-
}
895-
traces = append(traces, trace)
896-
}
897-
return traces, nil
898-
}
899-
900882
func (c *ClickHouseConnector) GetLastReorgCheckedBlockNumber(chainId *big.Int) (*big.Int, error) {
901883
query := fmt.Sprintf("SELECT cursor_value FROM %s.cursors FINAL WHERE cursor_type = 'reorg'", c.cfg.Database)
902884
if chainId.Sign() > 0 {

internal/storage/connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type IMainStorage interface {
5757
GetTransactions(qf QueryFilter) (transactions QueryResult[common.Transaction], err error)
5858
GetLogs(qf QueryFilter) (logs QueryResult[common.Log], err error)
5959
GetAggregations(table string, qf QueryFilter) (QueryResult[interface{}], error)
60-
GetTraces(qf QueryFilter) (traces []common.Trace, err error)
60+
GetTraces(qf QueryFilter) (traces QueryResult[common.Trace], err error)
6161
GetMaxBlockNumber(chainId *big.Int) (maxBlockNumber *big.Int, err error)
6262
/**
6363
* Get block headers ordered from latest to oldest.

test/mocks/MockIMainStorage.go

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)