Skip to content

Commit

Permalink
feat: making log messages more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed Nov 1, 2023
1 parent 80f3f28 commit 13f271a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
18 changes: 9 additions & 9 deletions bridge/tfchain_bridge/pkg/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (bridge *Bridge) Start(ctx context.Context) error {
Msg("the bridge instance has started")
height, err := bridge.blockPersistency.GetHeight()
if err != nil {
return errors.Wrap(err, "failed to get block height from persistency")
return errors.Wrap(err, "an error occurred while reading block height from persistency")
}

log.Debug().
Expand Down Expand Up @@ -114,7 +114,7 @@ func (bridge *Bridge) Start(ctx context.Context) error {
select {
case data := <-tfchainSub:
if data.Err != nil {
return errors.Wrap(err, "failed to process events")
return errors.Wrap(err, "failed to get tfchain events")
}
for _, withdrawCreatedEvent := range data.Events.WithdrawCreatedEvents {
err := bridge.handleWithdrawCreated(ctx, withdrawCreatedEvent)
Expand All @@ -123,13 +123,13 @@ func (bridge *Bridge) Start(ctx context.Context) error {
if errors.Is(err, pkg.ErrTransactionAlreadyBurned) || errors.Is(err, pkg.ErrTransactionAlreadyMinted) {
continue
}
return errors.Wrap(err, "failed to handle withdraw created")
return errors.Wrap(err, "an error occurred while handling WithdrawCreatedEvents")
}
}
for _, withdrawExpiredEvent := range data.Events.WithdrawExpiredEvents {
err := bridge.handleWithdrawExpired(ctx, withdrawExpiredEvent)
if err != nil {
return errors.Wrap(err, "failed to handle withdraw expired")
return errors.Wrap(err, "an error occurred while handling WithdrawExpiredEvents")
}
}
for _, withdawReadyEvent := range data.Events.WithdrawReadyEvents {
Expand All @@ -138,13 +138,13 @@ func (bridge *Bridge) Start(ctx context.Context) error {
if errors.Is(err, pkg.ErrTransactionAlreadyBurned) {
continue
}
return errors.Wrap(err, "failed to handle withdraw ready")
return errors.Wrap(err, "an error occurred while handling WithdrawReadyEvents")
}
}
for _, refundExpiredEvent := range data.Events.RefundExpiredEvents {
err := bridge.handleRefundExpired(ctx, refundExpiredEvent)
if err != nil {
return errors.Wrap(err, "failed to handle refund expired")
return errors.Wrap(err, "an error occurred while handling RefundExpiredEvents")
}
}
for _, refundReadyEvent := range data.Events.RefundReadyEvents {
Expand All @@ -153,12 +153,12 @@ func (bridge *Bridge) Start(ctx context.Context) error {
if errors.Is(err, pkg.ErrTransactionAlreadyRefunded) {
continue
}
return errors.Wrap(err, "failed to handle refund ready")
return errors.Wrap(err, "an error occurred while handling RefundReadyEvents")
}
}
case data := <-stellarSub:
if data.Err != nil {
return errors.Wrap(err, "failed to get mint events")
return errors.Wrap(err, "failed to get stellar payments")
}

for _, mEvent := range data.Events {
Expand All @@ -167,7 +167,7 @@ func (bridge *Bridge) Start(ctx context.Context) error {
if errors.Is(err, pkg.ErrTransactionAlreadyMinted) {
continue
}
return errors.Wrap(err, "failed to handle mint") // mint could be initiated already but there is a problem saving the cursor
return errors.Wrap(err, "an error occurred while processing the payment received") // mint could be initiated already but there is a problem saving the cursor
}
}
case <-ctx.Done():
Expand Down
6 changes: 3 additions & 3 deletions bridge/tfchain_bridge/pkg/bridge/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (bridge *Bridge) mint(ctx context.Context, senders map[string]*big.Int, tx
cursor := tx.PagingToken()
err := bridge.blockPersistency.SaveStellarCursor(cursor)
if err != nil {
return errors.Wrap(err, "error while saving cursor")
return errors.Wrap(err, "an error occurred while saving stellar cursor")
}
return nil
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (bridge *Bridge) mint(ctx context.Context, senders map[string]*big.Int, tx
}

logger.Info().
Str("type_event", "mint_proposed").
Str("event_type", "mint_proposed").
Dict("event", zerolog.Dict().
Int64("amount", depositedAmount.Int64()).
Str("tx_id", tx.Hash).
Expand All @@ -109,7 +109,7 @@ func (bridge *Bridge) mint(ctx context.Context, senders map[string]*big.Int, tx
// save cursor
cursor := tx.PagingToken()
if err = bridge.blockPersistency.SaveStellarCursor(cursor); err != nil {
return errors.Wrap(err, "error while saving cursor")
return errors.Wrap(err, "an error occurred while saving stellar cursor")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion bridge/tfchain_bridge/pkg/bridge/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (bridge *Bridge) refund(ctx context.Context, destination string, amount int
// save cursor
cursor := tx.PagingToken()
if err = bridge.blockPersistency.SaveStellarCursor(cursor); err != nil {
return errors.Wrap(err, "error while saving cursor")
return errors.Wrap(err, "an error occurred while saving stellar cursor")
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions bridge/tfchain_bridge/pkg/bridge/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (bridge *Bridge) handleWithdrawCreated(ctx context.Context, withdraw subpkg
return nil
}
logger.Info().
Str("type_event", "withdraw_proposed").
Str("event_type", "withdraw_proposed").
Dict("event", zerolog.Dict().
Int64("amount", int64(withdraw.Amount)).
Str("tx_id", fmt.Sprint(withdraw.ID)).
Expand Down Expand Up @@ -79,7 +79,7 @@ func (bridge *Bridge) handleWithdrawExpired(ctx context.Context, withdrawExpired
return nil
}
logger.Info().
Str("type_event", "withdraw_proposed").
Str("event_type", "withdraw_proposed").
Dict("event", zerolog.Dict().
Int64("amount", int64(withdrawExpired.Amount)).
Str("tx_id", fmt.Sprint(withdrawExpired.ID)).
Expand Down
File renamed without changes.
25 changes: 15 additions & 10 deletions bridge/tfchain_bridge/pkg/stellar/stellar.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (w *StellarWallet) CreatePaymentAndReturnSignature(ctx context.Context, tar
}

func (w *StellarWallet) CreatePaymentWithSignaturesAndSubmit(ctx context.Context, target string, amount uint64, txHash string, signatures []substrate.StellarSignature, sequenceNumber int64) error {
ctx_with_span_id := context.WithValue(ctx, "span_id", txHash)

txnBuild, err := w.generatePaymentOperation(amount, target, sequenceNumber)
if err != nil {
return err
Expand All @@ -116,10 +118,11 @@ func (w *StellarWallet) CreatePaymentWithSignaturesAndSubmit(ctx context.Context
}
}

return w.submitTransaction(ctx, txn)
return w.submitTransaction(ctx_with_span_id, txn)
}

func (w *StellarWallet) CreateRefundPaymentWithSignaturesAndSubmit(ctx context.Context, target string, amount uint64, txHash string, signatures []substrate.StellarSignature, sequenceNumber int64) error {
ctx_with_span_id := context.WithValue(ctx, "span_id", txHash)
txnBuild, err := w.generatePaymentOperation(amount, target, sequenceNumber)
if err != nil {
return err
Expand Down Expand Up @@ -153,7 +156,7 @@ func (w *StellarWallet) CreateRefundPaymentWithSignaturesAndSubmit(ctx context.C
}
}

return w.submitTransaction(ctx, txn)
return w.submitTransaction(ctx_with_span_id, txn)
}

func (w *StellarWallet) CreateRefundAndReturnSignature(ctx context.Context, target string, amount uint64, message string) (string, uint64, error) {
Expand Down Expand Up @@ -216,7 +219,7 @@ func (w *StellarWallet) generatePaymentOperation(amount uint64, destination stri

sourceAccount, err := w.getAccountDetails(w.config.StellarBridgeAccount)
if err != nil {
return txnbuild.TransactionParams{}, errors.Wrap(err, "failed to get source account")
return txnbuild.TransactionParams{}, errors.Wrap(err, "an error occurred while getting source account details")
}

asset := w.getAssetCodeAndIssuer()
Expand Down Expand Up @@ -253,7 +256,7 @@ func (w *StellarWallet) generatePaymentOperation(amount uint64, destination stri
func (w *StellarWallet) createTransaction(ctx context.Context, txn txnbuild.TransactionParams, sign bool) (*txnbuild.Transaction, error) {
tx, err := txnbuild.NewTransaction(txn)
if err != nil {
return nil, errors.Wrap(err, "failed to build transaction")
return nil, errors.Wrap(err, "an error occurred while building the transaction")
}

if sign {
Expand All @@ -262,7 +265,7 @@ func (w *StellarWallet) createTransaction(ctx context.Context, txn txnbuild.Tran
if hError, ok := err.(*horizonclient.Error); ok {
log.Error().Msgf("Error submitting tx %+v", hError.Problem.Extras)
}
return nil, errors.Wrap(err, "failed to sign transaction with keypair")
return nil, errors.Wrap(err, "an error occurred while signing the transaction with keypair")
}
}

Expand All @@ -272,7 +275,7 @@ func (w *StellarWallet) createTransaction(ctx context.Context, txn txnbuild.Tran
func (w *StellarWallet) submitTransaction(ctx context.Context, txn *txnbuild.Transaction) error {
client, err := w.getHorizonClient()
if err != nil {
return errors.Wrap(err, "failed to get horizon client")
return errors.Wrap(err, "an error occurred while getting horizon client")
}

// Submit the transaction
Expand All @@ -288,11 +291,13 @@ func (w *StellarWallet) submitTransaction(ctx context.Context, txn *txnbuild.Tra
if errSequence != nil {
return errSequence
}
return errors.Wrap(err, "error submitting transaction")
return errors.Wrap(err, "an error occurred while submitting the transaction")
}
log.Debug().
log.Info().
Str("span_id", fmt.Sprint(ctx.Value("span_id"))).
Str("event_type", "stellar_transaction_submitted").
Interface("event", txn).
Dict("event", zerolog.Dict().
Str("bridge_transaction_id", txResult.ID)).
Msgf("the transaction submitted to the Stellar network, and its unique identifier is %s", txResult.ID)
return nil
}
Expand Down Expand Up @@ -414,7 +419,7 @@ func (w *StellarWallet) processTransaction(tx hProtocol.Transaction) ([]MintEven

effects, err := w.getTransactionEffects(tx.Hash)
if err != nil {
return nil, errors.Wrapf(err, "error while fetching transaction effects for tx. tx id is %s", tx.ID)
return nil, errors.Wrapf(err, "failed to fetch transaction effects for transaction with id is %s", tx.ID)
}

asset := w.getAssetCodeAndIssuer()
Expand Down
14 changes: 8 additions & 6 deletions bridge/tfchain_bridge/pkg/substrate/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ type RefundTransactionExpiredEvent struct {
}

func (client *SubstrateClient) SubscribeTfchainBridgeEvents(ctx context.Context, eventChannel chan<- EventSubscription) error {
logger := log.Logger.With().Str("event_type", "FetchTfchainBridgeEvents").Logger()


cl, _, err := client.GetClient()
if err != nil {
return errors.Wrap(err, "failed to get client")
return errors.Wrap(err, "an error occurred while getting substrate client")
}

chainHeadsSub, err := cl.RPC.Chain.SubscribeFinalizedHeads()
if err != nil {
return errors.Wrap(err, "failed to subscribe to finalized heads")
return errors.Wrap(err, "an error occurred while subscribing to finalized heads")
}

for {
Expand All @@ -92,7 +91,10 @@ func (client *SubstrateClient) SubscribeTfchainBridgeEvents(ctx context.Context,
chainHeadsSub, err = cl.RPC.Chain.SubscribeFinalizedHeads()
return err
}, bo, func(err error, d time.Duration) {
logger.Warn().Err(err).Str("event_type", "fetch_finalizedHead_failed").Msgf("connection to chain lost, reopening connection in %s", d.String())
log.Warn().
Err(err).
Str("event_type", "fetch_finalized_Heads_failed").
Msgf("connection to chain lost, reopening connection in %s", d.String())
})

case <-ctx.Done():
Expand All @@ -110,7 +112,7 @@ func (client *SubstrateClient) processEventsForHeight(height uint32) (Events, er

records, err := client.GetEventsForBlock(height)
if err != nil {
return Events{}, errors.Wrapf(err, "error while decoding block for height %d", height)
return Events{}, errors.Wrapf(err, "an error occurred while decoding events for height %d", height)

}
log.Info().
Expand Down

0 comments on commit 13f271a

Please sign in to comment.