Skip to content

Commit

Permalink
fix personal tx sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
BedrockSquirrel committed Jun 28, 2024
1 parent 1360547 commit e92de20
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
7 changes: 5 additions & 2 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,14 @@ func BatchWasExecuted(ctx context.Context, db *sql.DB, hash common.L2BatchHash)
}

func GetTransactionsPerAddress(ctx context.Context, db *sql.DB, config *params.ChainConfig, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error) {
return selectReceipts(ctx, db, config, "where tx.sender_address = ? ORDER BY height DESC LIMIT ? OFFSET ? ", address.Bytes(), pagination.Size, pagination.Offset)
return selectReceipts(ctx, db, config, "join externally_owned_account eoa on tx.sender_address = eoa.id where eoa.address = ? ORDER BY height DESC LIMIT ? OFFSET ? ", address.Bytes(), pagination.Size, pagination.Offset)
}

func CountTransactionsPerAddress(ctx context.Context, db *sql.DB, address *gethcommon.Address) (uint64, error) {
row := db.QueryRowContext(ctx, "select count(1) from receipt join tx on tx.id=receipt.tx join batch on batch.sequence=receipt.batch "+" where tx.sender_address = ?", address.Bytes())
row := db.QueryRowContext(ctx, "select count(1) from receipt "+
"join tx on tx.id=receipt.tx "+
"join externally_owned_account eoa on eoa.id = tx.sender_address "+
"where eoa.address = ?", address.Bytes())

var count uint64
err := row.Scan(&count)
Expand Down
10 changes: 5 additions & 5 deletions go/obsclient/authclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,21 @@ func (ac *AuthObsClient) EstimateGasAndGasPrice(txData types.TxData) types.TxDat
}
}

// GetPrivateTransactions retrieves the receipts for the specified account (must be registered on this client)
func (ac *AuthObsClient) GetPrivateTransactions(ctx context.Context, address *gethcommon.Address, pagination common.QueryPagination) (types.Receipts, error) {
// GetPrivateTransactions retrieves the receipts for the specified account (must be registered on this client), returns requested range of receipts and the total number of receipts for that acc
func (ac *AuthObsClient) GetPrivateTransactions(ctx context.Context, address *gethcommon.Address, pagination common.QueryPagination) (types.Receipts, uint64, error) {
queryParam := &common.ListPrivateTransactionsQueryParams{
Address: *address,
Pagination: pagination,
}
queryParamStr, err := json.Marshal(queryParam)
if err != nil {
return nil, fmt.Errorf("unable to marshal query params - %w", err)
return nil, 0, fmt.Errorf("unable to marshal query params - %w", err)
}
var result common.PrivateTransactionsQueryResponse
err = ac.rpcClient.CallContext(ctx, &result, rpc.GetStorageAt, common.ListPrivateTransactionsCQMethod, string(queryParamStr), nil)
if err != nil {
return nil, err
return nil, 0, err
}

return result.Receipts, nil
return result.Receipts, result.Total, nil
}
7 changes: 6 additions & 1 deletion integration/networktest/tests/tenscan/tenscan_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestPersonalTransactions(t *testing.T) {
Offset: 0,
Size: 20,
}
personalTxs, err := user.GetPersonalTransactions(ctx, pagination)
personalTxs, total, err := user.GetPersonalTransactions(ctx, pagination)
if err != nil {
return fmt.Errorf("unable to get personal transactions - %w", err)
}
Expand All @@ -78,6 +78,11 @@ func TestPersonalTransactions(t *testing.T) {
if len(personalTxs) != 2 {
return fmt.Errorf("expected 2 transactions, got %d", len(personalTxs))
}

// verify total set
if total != 2 {
return fmt.Errorf("expected total receipts to be at least 2, got %d", total)
}
return nil
}),
),
Expand Down
2 changes: 1 addition & 1 deletion integration/networktest/userwallet/authclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (s *AuthClientUser) Wallet() wallet.Wallet {
return s.wal
}

func (s *AuthClientUser) GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, error) {
func (s *AuthClientUser) GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, uint64, error) {
address := s.wal.Address()
return s.client.GetPrivateTransactions(ctx, &address, pagination)
}
10 changes: 5 additions & 5 deletions integration/networktest/userwallet/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,27 @@ func (g *GatewayUser) NativeBalance(ctx context.Context) (*big.Int, error) {
return g.client.BalanceAt(ctx, g.wal.Address(), nil)
}

func (g *GatewayUser) GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, error) {
func (g *GatewayUser) GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, uint64, error) {
rpcClient := g.client.Client()
queryParams := &common.ListPrivateTransactionsQueryParams{
Address: g.wal.Address(),
Pagination: pagination,
}
queryParamsStr, err := json.Marshal(queryParams)
if err != nil {
return nil, fmt.Errorf("unable to marshal query params - %w", err)
return nil, 0, fmt.Errorf("unable to marshal query params - %w", err)
}
var resultBytes hexutil.Bytes
err = rpcClient.CallContext(ctx, &resultBytes, "eth_getStorageAt", common.ListPrivateTransactionsCQMethod, queryParamsStr, nil)
if err != nil {
return nil, fmt.Errorf("rpc call failed - %w", err)
return nil, 0, fmt.Errorf("rpc call failed - %w", err)
}
var result common.PrivateTransactionsQueryResponse
err = json.Unmarshal(resultBytes, &result)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal result - %w", err)
return nil, 0, fmt.Errorf("failed to unmarshal result - %w", err)
}
return result.Receipts, nil
return result.Receipts, result.Total, nil
}

func (g *GatewayUser) Wallet() wallet.Wallet {
Expand Down
2 changes: 1 addition & 1 deletion integration/networktest/userwallet/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ type User interface {
SendFunds(ctx context.Context, addr gethcommon.Address, value *big.Int) (*gethcommon.Hash, error)
AwaitReceipt(ctx context.Context, txHash *gethcommon.Hash) (*types.Receipt, error)
NativeBalance(ctx context.Context) (*big.Int, error)
GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, error)
GetPersonalTransactions(ctx context.Context, pagination common.QueryPagination) (types.Receipts, uint64, error)
}

0 comments on commit e92de20

Please sign in to comment.