Skip to content

Commit a4edb30

Browse files
[INTERNAL] /transaction/{transactionhash} returns TSSResponse (#108)
* 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
1 parent 9a2358c commit a4edb30

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ you can set it to any value. For production, you should set it to a secure passp
6464
3. Start the containers:
6565

6666
```bash
67-
docker-compose up
67+
docker compose up
6868
```
6969

7070
If things are set up correctly, you will see 4 containers started under the wallet-backend docker service: `api`, `db`,

internal/serve/httphandler/tss_handler.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httphandler
22

33
import (
4+
"fmt"
45
"net/http"
56

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

158-
httpjson.Render(w, GetTransactionResponse{
159-
Hash: tx.Hash,
160-
XDR: tx.XDR,
161-
Status: tx.Status,
159+
tssTry, err := t.Store.GetLatestTry(ctx, tx.Hash)
160+
if err != nil {
161+
httperror.InternalServerError(ctx, "unable to get tx try "+tx.Hash, err, nil, t.AppTracker).Render(w)
162+
return
163+
}
164+
165+
httpjson.Render(w, tss.TSSResponse{
166+
TransactionHash: tx.Hash,
167+
TransactionResultCode: fmt.Sprint(tssTry.Code),
168+
Status: tx.Status,
169+
CreatedAt: tssTry.CreatedAt.Unix(),
170+
EnvelopeXDR: tssTry.XDR,
171+
ResultXDR: tssTry.ResultXDR,
162172
}, httpjson.JSON)
163173
}

internal/serve/httphandler/tss_handler_test.go

+39-10
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,38 @@ func TestGetTransaction(t *testing.T) {
255255
require.NoError(t, err)
256256
}
257257

258+
t.Run("returns_empty_try", func(t *testing.T) {
259+
txHash := "hash"
260+
ctx := context.Background()
261+
_ = store.UpsertTransaction(ctx, "localhost:8080/webhook", txHash, "xdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus})
262+
req, err := http.NewRequest(http.MethodGet, path.Join(endpoint, txHash), nil)
263+
require.NoError(t, err)
264+
265+
// Serve request
266+
rw := httptest.NewRecorder()
267+
r.ServeHTTP(rw, req)
268+
resp := rw.Result()
269+
respBody, err := io.ReadAll(resp.Body)
270+
require.NoError(t, err)
271+
var tssResp tss.TSSResponse
272+
_ = json.Unmarshal(respBody, &tssResp)
273+
274+
assert.Equal(t, http.StatusOK, resp.StatusCode)
275+
assert.Equal(t, txHash, tssResp.TransactionHash)
276+
assert.Equal(t, fmt.Sprint(tss.NoCode), tssResp.TransactionResultCode)
277+
assert.Equal(t, fmt.Sprint(tss.NewStatus), tssResp.Status)
278+
assert.Equal(t, "", tssResp.ResultXDR)
279+
assert.Equal(t, "", tssResp.EnvelopeXDR)
280+
281+
clearTransactions(ctx)
282+
})
283+
258284
t.Run("returns_transaction", func(t *testing.T) {
259285
txHash := "hash"
286+
resultXdr := "resultXdr"
260287
ctx := context.Background()
261288
_ = store.UpsertTransaction(ctx, "localhost:8080/webhook", txHash, "xdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus})
289+
_ = store.UpsertTry(ctx, txHash, "feebumphash", "feebumpxdr", tss.RPCTXStatus{OtherStatus: tss.NewStatus}, tss.RPCTXCode{OtherCodes: tss.NewCode}, resultXdr)
262290
req, err := http.NewRequest(http.MethodGet, path.Join(endpoint, txHash), nil)
263291
require.NoError(t, err)
264292

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

274302
assert.Equal(t, http.StatusOK, resp.StatusCode)
275-
assert.Equal(t, "hash", getTxResp.Hash)
276-
assert.Equal(t, "xdr", getTxResp.XDR)
277-
assert.Equal(t, "NEW", getTxResp.Status)
303+
assert.Equal(t, txHash, tssResp.TransactionHash)
304+
assert.Equal(t, fmt.Sprint(tss.NewCode), tssResp.TransactionResultCode)
305+
assert.Equal(t, fmt.Sprint(tss.NewStatus), tssResp.Status)
306+
assert.Equal(t, resultXdr, tssResp.ResultXDR)
278307

279308
clearTransactions(ctx)
280309
})
@@ -290,13 +319,13 @@ func TestGetTransaction(t *testing.T) {
290319
resp := rw.Result()
291320
respBody, err := io.ReadAll(resp.Body)
292321
require.NoError(t, err)
293-
var getTxResp GetTransactionResponse
294-
_ = json.Unmarshal(respBody, &getTxResp)
322+
var tssResp tss.TSSResponse
323+
_ = json.Unmarshal(respBody, &tssResp)
295324

296325
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
297-
assert.Empty(t, getTxResp.Hash)
298-
assert.Empty(t, getTxResp.XDR)
299-
assert.Empty(t, getTxResp.Status)
326+
assert.Empty(t, tssResp.TransactionHash)
327+
assert.Empty(t, tssResp.EnvelopeXDR)
328+
assert.Empty(t, tssResp.Status)
300329

301330
})
302331

0 commit comments

Comments
 (0)