Skip to content

Commit

Permalink
[INTERNAL] /transaction/{transactionhash} returns TSSResponse (#108)
Browse files Browse the repository at this point in the history
* updates GetTransaction to return a TSSResponse

* updates GetTransaction test for new response type

* uses GetTryLatest instead of GetTry to get most recent timestamp for hash, updates test

* tweaks readme docker command to match new syntax

* uses correct created at date from Try instead of from tx

* removes stray debug log left in test

* renames var to better describe response type

* adds a try for the test transaction, asserts on result xdr from try

* adds unit test for empty result from latest try for transaction
  • Loading branch information
aristidesstaffieri authored Jan 24, 2025
1 parent 9a2358c commit a4edb30
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ you can set it to any value. For production, you should set it to a secure passp
3. Start the containers:

```bash
docker-compose up
docker compose up
```

If things are set up correctly, you will see 4 containers started under the wallet-backend docker service: `api`, `db`,
Expand Down
18 changes: 14 additions & 4 deletions internal/serve/httphandler/tss_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httphandler

import (
"fmt"
"net/http"

"github.com/stellar/go/support/log"
Expand Down Expand Up @@ -155,9 +156,18 @@ func (t *TSSHandler) GetTransaction(w http.ResponseWriter, r *http.Request) {
httperror.NotFound.Render(w)
}

httpjson.Render(w, GetTransactionResponse{
Hash: tx.Hash,
XDR: tx.XDR,
Status: tx.Status,
tssTry, err := t.Store.GetLatestTry(ctx, tx.Hash)
if err != nil {
httperror.InternalServerError(ctx, "unable to get tx try "+tx.Hash, err, nil, t.AppTracker).Render(w)
return
}

httpjson.Render(w, tss.TSSResponse{
TransactionHash: tx.Hash,
TransactionResultCode: fmt.Sprint(tssTry.Code),
Status: tx.Status,
CreatedAt: tssTry.CreatedAt.Unix(),
EnvelopeXDR: tssTry.XDR,
ResultXDR: tssTry.ResultXDR,
}, httpjson.JSON)
}
49 changes: 39 additions & 10 deletions internal/serve/httphandler/tss_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,38 @@ func TestGetTransaction(t *testing.T) {
require.NoError(t, err)
}

t.Run("returns_empty_try", func(t *testing.T) {
txHash := "hash"
ctx := context.Background()
_ = store.UpsertTransaction(ctx, "localhost:8080/webhook", txHash, "xdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus})
req, err := http.NewRequest(http.MethodGet, path.Join(endpoint, txHash), nil)
require.NoError(t, err)

// Serve request
rw := httptest.NewRecorder()
r.ServeHTTP(rw, req)
resp := rw.Result()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var tssResp tss.TSSResponse
_ = json.Unmarshal(respBody, &tssResp)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, txHash, tssResp.TransactionHash)
assert.Equal(t, fmt.Sprint(tss.NoCode), tssResp.TransactionResultCode)
assert.Equal(t, fmt.Sprint(tss.NewStatus), tssResp.Status)
assert.Equal(t, "", tssResp.ResultXDR)
assert.Equal(t, "", tssResp.EnvelopeXDR)

clearTransactions(ctx)
})

t.Run("returns_transaction", func(t *testing.T) {
txHash := "hash"
resultXdr := "resultXdr"
ctx := context.Background()
_ = store.UpsertTransaction(ctx, "localhost:8080/webhook", txHash, "xdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus})
_ = store.UpsertTry(ctx, txHash, "feebumphash", "feebumpxdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus}, tss.RPCTXCode{OtherCodes: tss.NewCode}, resultXdr)
req, err := http.NewRequest(http.MethodGet, path.Join(endpoint, txHash), nil)
require.NoError(t, err)

Expand All @@ -268,13 +296,14 @@ func TestGetTransaction(t *testing.T) {
resp := rw.Result()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var getTxResp GetTransactionResponse
_ = json.Unmarshal(respBody, &getTxResp)
var tssResp tss.TSSResponse
_ = json.Unmarshal(respBody, &tssResp)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "hash", getTxResp.Hash)
assert.Equal(t, "xdr", getTxResp.XDR)
assert.Equal(t, "NEW", getTxResp.Status)
assert.Equal(t, txHash, tssResp.TransactionHash)
assert.Equal(t, fmt.Sprint(tss.NewCode), tssResp.TransactionResultCode)
assert.Equal(t, fmt.Sprint(tss.NewStatus), tssResp.Status)
assert.Equal(t, resultXdr, tssResp.ResultXDR)

clearTransactions(ctx)
})
Expand All @@ -290,13 +319,13 @@ func TestGetTransaction(t *testing.T) {
resp := rw.Result()
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var getTxResp GetTransactionResponse
_ = json.Unmarshal(respBody, &getTxResp)
var tssResp tss.TSSResponse
_ = json.Unmarshal(respBody, &tssResp)

assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Empty(t, getTxResp.Hash)
assert.Empty(t, getTxResp.XDR)
assert.Empty(t, getTxResp.Status)
assert.Empty(t, tssResp.TransactionHash)
assert.Empty(t, tssResp.EnvelopeXDR)
assert.Empty(t, tssResp.Status)

})

Expand Down

0 comments on commit a4edb30

Please sign in to comment.