Skip to content

Commit

Permalink
api: v2alpha1 tx service: Use address field instead of principal & re…
Browse files Browse the repository at this point in the history
…ceiver (#6034)

Use address field to query transactions by looking at touched addresses instead of principal / result decoding.
  • Loading branch information
kacpersaw committed Jun 12, 2024
1 parent d36b66b commit 539e323
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
23 changes: 11 additions & 12 deletions api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ func toTransactionOperations(filter *spacemeshv2alpha1.TransactionRequest) (buil
return ops, nil
}

if filter.GetPrincipal() != "" {
addr, err := types.StringToAddress(filter.GetPrincipal())
if filter.GetAddress() != "" {
addr, err := types.StringToAddress(filter.GetAddress())
if err != nil {
return builder.Operations{}, err
}
ops.Filter = append(ops.Filter, builder.Op{
Field: builder.Principal,
Field: builder.Address,
Token: builder.Eq,
Value: addr.Bytes(),
})
Expand Down Expand Up @@ -280,10 +280,6 @@ func toTransactionOperations(filter *spacemeshv2alpha1.TransactionRequest) (buil
})
}

if len(ops.Filter) > 0 {
ops.StartWith = "and"
}

ops.Modifiers = append(ops.Modifiers, builder.Modifier{
Key: builder.OrderBy,
Value: "layer asc, id",
Expand Down Expand Up @@ -330,7 +326,7 @@ func toTx(tx *types.MeshTransaction, result *types.TransactionResult,
t.Contents = contents
}

if includeResult {
if includeResult && result != nil {
rst.TxResult = &spacemeshv2alpha1.TransactionResult{
Status: convertTxResult(result),
Message: result.Message,
Expand Down Expand Up @@ -368,14 +364,17 @@ func convertTxResult(result *types.TransactionResult) spacemeshv2alpha1.Transact
}

// TODO: REJECTED, INSUFFICIENT_FUNDS, CONFLICTING, MESH.
func convertTxState(tx *types.MeshTransaction) spacemeshv2alpha1.TransactionState {
func convertTxState(tx *types.MeshTransaction) *spacemeshv2alpha1.TransactionState {
switch tx.State {
case types.MEMPOOL:
return spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_MEMPOOL
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_MEMPOOL
return &state
case types.APPLIED:
return spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_PROCESSED
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_PROCESSED
return &state
default:
return spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_UNSPECIFIED
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_UNSPECIFIED
return &state
}
}

Expand Down
25 changes: 19 additions & 6 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,28 @@ func TestTransactionService_List(t *testing.T) {
require.Len(t, list.Transactions, len(txsList))
})

t.Run("principal", func(t *testing.T) {
principal := txsList[0].TxHeader.Principal.String()
t.Run("address", func(t *testing.T) {
address := txsList[0].Principal.String()
var expectedTxs []types.TransactionWithResult
for _, tx := range txsList {
found := false
for _, addr := range tx.TransactionResult.Addresses {
if addr.String() == address {
found = true
break
}
}
if found {
expectedTxs = append(expectedTxs, tx)
}
}

list, err := client.List(ctx, &spacemeshv2alpha1.TransactionRequest{
Principal: &principal,
Limit: 1,
Address: &address,
Limit: 100,
})
require.NoError(t, err)
require.Len(t, list.Transactions, 1)
require.Equal(t, list.Transactions[0].Tx.Principal, principal)
require.Len(t, list.Transactions, len(expectedTxs))
})

t.Run("tx id", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/rs/cors v1.11.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/seehuhn/mt19937 v1.0.0
github.com/spacemeshos/api/release/go v1.42.0
github.com/spacemeshos/api/release/go v1.44.0
github.com/spacemeshos/economics v0.1.3
github.com/spacemeshos/fixed v0.1.1
github.com/spacemeshos/go-scale v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:Udh
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/spacemeshos/api/release/go v1.42.0 h1:K85zw+KZA1UA3VNwvXD2UIND7NLyAiJo4Kz6ZznFEEc=
github.com/spacemeshos/api/release/go v1.42.0/go.mod h1:aCDRfna5MA7LJWZPa4k+vTRvBUf1Swz8kcziPcdp6i8=
github.com/spacemeshos/api/release/go v1.44.0 h1:bqo95CDksk8wPNQQidusjwCamx5HMUuMNSVwX08l+dw=
github.com/spacemeshos/api/release/go v1.44.0/go.mod h1:8pxGN6/di8iBpQReiOgY+Cppi7bhJ+qJ3QiRQtJfoag=
github.com/spacemeshos/economics v0.1.3 h1:ACkq3mTebIky4Zwbs9SeSSRZrUCjU/Zk0wq9Z0BTh2A=
github.com/spacemeshos/economics v0.1.3/go.mod h1:FH7u0FzTIm6Kpk+X5HOZDvpkgNYBKclmH86rVwYaDAo=
github.com/spacemeshos/fixed v0.1.1 h1:N1y4SUpq1EV+IdJrWJwUCt1oBFzeru/VKVcBsvPc2Fk=
Expand Down
17 changes: 10 additions & 7 deletions sql/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ func IterateTransactionsOps(
var derr error
_, err := db.Exec(`select distinct tx, header, layer, block, timestamp, id, result
from transactions
left join transactions_results_addresses on id=tid
where result is not null`+builder.FilterFrom(operations),
left join transactions_results_addresses on id=tid`+builder.FilterFrom(operations),
builder.BindingsFrom(operations),
func(stmt *sql.Statement) bool {
var txId types.TransactionID
Expand All @@ -431,13 +430,17 @@ func IterateTransactionsOps(
if derr != nil {
return false
}
var txResult types.TransactionResult
_, derr = codec.DecodeFrom(stmt.ColumnReader(6), &txResult)
if derr != nil {
return false
var txResult *types.TransactionResult
r := stmt.ColumnReader(6)
if r.Len() > 0 {
txResult = &types.TransactionResult{}
_, derr = codec.DecodeFrom(r, txResult)
if derr != nil {
return false
}
}

return fn(meshTx, &txResult)
return fn(meshTx, txResult)
})
if err == nil {
return err
Expand Down

0 comments on commit 539e323

Please sign in to comment.