Skip to content

Commit

Permalink
Bridge: fix daemon stop when encounter error submitting stellar tx (#920
Browse files Browse the repository at this point in the history
)

* update cargo.lock file

* fix: pre-check wallet and continue on stellar error

* fix go fmt
  • Loading branch information
sameh-farouk authored Dec 21, 2023
1 parent 55d2f8e commit 36c891c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
29 changes: 28 additions & 1 deletion bridge/docs/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ For example, if a customer is complaining that their deposit never bridged, you
- `event_burn_tx_ready_received`: The bridge instance has received TFChain `BurnTransactionReady` event which means all bridge validators signed a withdraw transaction.
- `event_burn_tx_expired_received`: The bridge instance has received TFChain `BurnTransactionExpired` event.
- `withdraw_skipped`: a refund request skipped by the bridge instance as it has already been refunded.
- `withdraw_proposed`: a withdraw has proposed or signed by the bridge instance.
- `withdraw_proposed`: a withdraw has proposed or signed by the bridge instance.
- `withdraw_postponed`: a withdraw has postponed due to a problem in sending this transaction to the stellar network and will be retried later.
- `withdraw_completed`: a withdraw has completed and received on the target stellar account.

##### Bridge vault account related
Expand Down Expand Up @@ -787,6 +788,32 @@ the source field set contains all fields which are included in the source object
</tbody>
</table>

##### withdraw_postponed

- kind: event

- category: withdraw

<table>
<thead>
<tr><th colspan="4"><div>withdraw_postponed Event Properties</div></th></tr>
<tr>
<th>Property</th>
<th>Type</th>
<th>Required</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>reason</code></td>
<td>string</td>
<td>yes</td>
<td>The reason behind the postponed of this transfer. </td>
</tr>
</tbody>
</table>

##### withdraw_completed

- kind: event
Expand Down
26 changes: 25 additions & 1 deletion bridge/tfchain_bridge/pkg/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bridge

import (
"context"
"strconv"
"time"

"github.com/pkg/errors"
Expand All @@ -13,7 +14,8 @@ import (
)

const (
BridgeNetwork = "stellar"
BridgeNetwork = "stellar"
MinimumBalance = 0
)

// Bridge is a high lvl structure which listens on contract events and bridge-related
Expand Down Expand Up @@ -73,7 +75,29 @@ func NewBridge(ctx context.Context, cfg pkg.BridgeConfig) (*Bridge, string, erro
return bridge, wallet.GetKeypair().Address(), nil
}

func (bridge *Bridge) preCheckBalance(ctx context.Context) error {
balance, err := bridge.wallet.StatBridgeAccount()

if err != nil {
return errors.Wrap(err, "can't retrieve the wallet balance at the moment")
}
s, err := strconv.ParseFloat(balance, 64)
if err != nil {
return errors.Wrap(err, "can't parse the wallet balance")
}

if s < MinimumBalance {
return errors.Errorf("wallet balance insufficient: %s", balance)
}
return nil
}

func (bridge *Bridge) Start(ctx context.Context) error {
// pre-check wallet balance
if err := bridge.preCheckBalance(ctx); err != nil {
return err
}

log.Info().
Str("event_action", "bridge_started").
Str("event_kind", "event").
Expand Down
11 changes: 10 additions & 1 deletion bridge/tfchain_bridge/pkg/bridge/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ func (bridge *Bridge) handleWithdrawReady(ctx context.Context, withdrawReady sub
// todo add memo hash
err = bridge.wallet.CreatePaymentWithSignaturesAndSubmit(ctx, burnTx.Target, uint64(burnTx.Amount), fmt.Sprint(withdrawReady.ID), burnTx.Signatures, int64(burnTx.SequenceNumber))
if err != nil {
return err
// we can log and skip here as we could depend on tfcahin retry mechanism
// to notify us again about related burn tx
logger.Info().
Str("event_action", "withdraw_postponed").
Str("event_kind", "event").
Str("category", "withdraw").
Dict("metadata", zerolog.Dict().
Str("reason", err.Error())).
Msgf("the withdraw has been postponed due to a problem in sending this transaction to the stellar network. error was %s", err.Error())
return nil
}
logger.Info().
Str("event_action", "withdraw_completed").
Expand Down
2 changes: 1 addition & 1 deletion bridge/tfchain_bridge/pkg/stellar/stellar.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,5 +579,5 @@ func (w *StellarWallet) StatBridgeAccount() (string, error) {
return balance.Balance, nil
}
}
return "", nil
return "", errors.New("source account does not have trustline")
}

0 comments on commit 36c891c

Please sign in to comment.