Skip to content

Commit

Permalink
final TSS changes (#74)
Browse files Browse the repository at this point in the history
* final TSS changes

* final changes

* uncomment
  • Loading branch information
gouthamp-stellar authored Nov 13, 2024
1 parent ee0fb0e commit 1ae6bf4
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 48 deletions.
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ FROM stellar/soroban-rpc
# Install bash or sh
RUN apt-get update && apt-get install -y bash


# Step 2: Install Stellar Core and copy over app binary
FROM ubuntu:jammy AS core-build

RUN apt-get update && \
apt-get install -y --no-install-recommends ca-certificates curl wget gnupg apt-utils gpg && \
curl -sSL https://apt.stellar.org/SDF.asc | gpg --dearmor >/etc/apt/trusted.gpg.d/SDF.gpg && \
echo "deb https://apt.stellar.org jammy stable" >/etc/apt/sources.list.d/SDF.list && \
echo "deb https://apt.stellar.org jammy testing" >/etc/apt/sources.list.d/SDF-testing.list && \
echo "deb https://apt.stellar.org jammy unstable" >/etc/apt/sources.list.d/SDF-unstable.list

COPY --from=api-build /bin/wallet-backend /app/

EXPOSE 8001
WORKDIR /app
ENTRYPOINT ["./wallet-backend"]


2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *serveCmd) Command() *cobra.Command {
Usage: "The minimum number of Channel Accounts that must exist in the database.",
OptType: types.Int,
ConfigKey: &cfg.NumberOfChannelAccounts,
FlagDefault: 5,
FlagDefault: 15,
Required: true,
},
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/utils/tss_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ErrorHandlerJitterChannelBufferSizeOption(configKey *int) *config.ConfigOpt
Usage: "Set the buffer size of the Error Handler Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 100,
FlagDefault: 1000,
Required: true,
}
}
Expand All @@ -44,7 +44,7 @@ func ErrorHandlerJitterChannelMaxWorkersOption(configKey *int) *config.ConfigOpt
Usage: "Set the maximum number of workers for the Error Handler Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 10,
FlagDefault: 100,
Required: true,
}
}
Expand All @@ -55,7 +55,7 @@ func ErrorHandlerNonJitterChannelBufferSizeOption(configKey *int) *config.Config
Usage: "Set the buffer size of the Error Handler Non Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 100,
FlagDefault: 1000,
Required: true,
}

Expand All @@ -67,7 +67,7 @@ func ErrorHandlerNonJitterChannelMaxWorkersOption(configKey *int) *config.Config
Usage: "Set the maximum number of workers for the Error Handler Non Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 10,
FlagDefault: 100,
Required: true,
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func ErrorHandlerJitterChannelMaxRetriesOptions(configKey *int) *config.ConfigOp
Usage: "Set the number of retries for each task in the Error Handler Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 10,
FlagDefault: 3,
Required: true,
}

Expand All @@ -112,7 +112,7 @@ func ErrorHandlerNonJitterChannelMaxRetriesOption(configKey *int) *config.Config
Usage: "Set the number of retries for each task in the Error Handler Service Jitter channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 10,
FlagDefault: 3,
Required: true,
}
}
Expand All @@ -123,7 +123,7 @@ func WebhookHandlerChannelMaxBufferSizeOption(configKey *int) *config.ConfigOpti
Usage: "Set the buffer size of the webhook channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 100,
FlagDefault: 1000,
Required: true,
}
}
Expand All @@ -134,7 +134,7 @@ func WebhookHandlerChannelMaxWorkersOptions(configKey *int) *config.ConfigOption
Usage: "Set the max number of workers for the webhook channel.",
OptType: types.Int,
ConfigKey: configKey,
FlagDefault: 10,
FlagDefault: 100,
Required: true,
}
}
Expand Down
24 changes: 12 additions & 12 deletions internal/tss/channels/webhook_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ func (p *webhookPool) Receive(payload tss.Payload) {
httpResp, err := p.HTTPClient.Post(payload.WebhookURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Errorf("%s: error making POST request to webhook: %e", WebhookChannelName, err)
}
defer httpResp.Body.Close()

if httpResp.StatusCode == http.StatusOK {
sent = true
err := p.Store.UpsertTransaction(
ctx, payload.WebhookURL, payload.TransactionHash, payload.TransactionXDR, tss.RPCTXStatus{OtherStatus: tss.SentStatus})
if err != nil {
log.Errorf("%s: error updating transaction status: %e", WebhookChannelName, err)
} else {
defer httpResp.Body.Close()
if httpResp.StatusCode == http.StatusOK {
sent = true
err := p.Store.UpsertTransaction(
ctx, payload.WebhookURL, payload.TransactionHash, payload.TransactionXDR, tss.RPCTXStatus{OtherStatus: tss.SentStatus})
if err != nil {
log.Errorf("%s: error updating transaction status: %e", WebhookChannelName, err)
}
break
}
break
currentBackoff := p.MinWaitBtwnRetriesMS * (1 << i)
time.Sleep(jitter(time.Duration(currentBackoff)) * time.Millisecond)
}
currentBackoff := p.MinWaitBtwnRetriesMS * (1 << i)
time.Sleep(jitter(time.Duration(currentBackoff)) * time.Millisecond)
}
if !sent {
err := p.Store.UpsertTransaction(
Expand Down
2 changes: 2 additions & 0 deletions internal/tss/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (r *router) Route(payload tss.Payload) error {
}
} else if payload.RpcGetIngestTxResponse.Status != "" {
channel = r.WebhookChannel
} else {
channel = r.RPCCallerChannel
}
if channel == nil {
return fmt.Errorf("payload could not be routed - channel is nil")
Expand Down
11 changes: 8 additions & 3 deletions internal/tss/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ func TestRouter(t *testing.T) {
assert.NoError(t, err)
webhookChannel.AssertCalled(t, "Send", payload)
})
t.Run("nil_channel_does_not_route", func(t *testing.T) {
t.Run("empty_payload_routes_to_rpc_caller_channel", func(t *testing.T) {
payload := tss.Payload{}

rpcCallerChannel.
On("Send", payload).
Return().
Once()

err := router.Route(payload)

errorJitterChannel.AssertNotCalled(t, "Send", payload)
assert.Equal(t, "payload could not be routed - channel is nil", err.Error())
assert.NoError(t, err)
rpcCallerChannel.AssertCalled(t, "Send", payload)
})
}
40 changes: 36 additions & 4 deletions internal/tss/services/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/support/log"
"github.com/stellar/go/txnbuild"
"github.com/stellar/wallet-backend/internal/signing"
tsserror "github.com/stellar/wallet-backend/internal/tss/errors"
Expand Down Expand Up @@ -67,6 +68,25 @@ func (t *transactionService) NetworkPassphrase() string {
return t.DistributionAccountSignatureClient.NetworkPassphrase()
}

func buildPayments(srcAccount string, operations []txnbuild.Operation) ([]txnbuild.Operation, error) {
var payments []txnbuild.Operation
for _, op := range operations {
origPayment, ok := op.(*txnbuild.Payment)
if !ok {
return nil, fmt.Errorf("unable to convert operation to payment op")
}
payment := &txnbuild.Payment{
SourceAccount: srcAccount,
Amount: origPayment.Amount,
Destination: origPayment.Destination,
Asset: origPayment.Asset,
}
payments = append(payments, payment)

}
return payments, nil
}

func (t *transactionService) SignAndBuildNewFeeBumpTransaction(ctx context.Context, origTxXdr string) (*txnbuild.FeeBumpTransaction, error) {
genericTx, err := txnbuild.TransactionFromXDR(origTxXdr)
if err != nil {
Expand All @@ -84,10 +104,22 @@ func (t *transactionService) SignAndBuildNewFeeBumpTransaction(ctx context.Conte
if err != nil {
return nil, fmt.Errorf("getting channel account details from horizon: %w", err)
}

distributionAccountPublicKey, err := t.DistributionAccountSignatureClient.GetAccountPublicKey(ctx)
if err != nil {
return nil, fmt.Errorf("getting distribution account public key: %w", err)
}

operations, err := buildPayments(distributionAccountPublicKey, originalTx.Operations())
if err != nil {
return nil, fmt.Errorf("building payment operations: %w", err)
}
log.Info(operations)

tx, err := txnbuild.NewTransaction(
txnbuild.TransactionParams{
SourceAccount: &channelAccount,
Operations: originalTx.Operations(),
Operations: operations,
BaseFee: int64(t.BaseFee),
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewTimeout(120),
Expand All @@ -102,10 +134,10 @@ func (t *transactionService) SignAndBuildNewFeeBumpTransaction(ctx context.Conte
if err != nil {
return nil, fmt.Errorf("signing transaction with channel account: %w", err)
}
// Wrap the transaction in a fee bump tx, signed by the distribution account
distributionAccountPublicKey, err := t.DistributionAccountSignatureClient.GetAccountPublicKey(ctx)

tx, err = t.DistributionAccountSignatureClient.SignStellarTransaction(ctx, tx, distributionAccountPublicKey)
if err != nil {
return nil, fmt.Errorf("getting distribution account public key: %w", err)
return nil, fmt.Errorf("signing transaction with distribution account: %w", err)
}

feeBumpTx, err := txnbuild.NewFeeBumpTransaction(
Expand Down
Loading

0 comments on commit 1ae6bf4

Please sign in to comment.