Skip to content

Commit

Permalink
Inject more context's
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Smet <[email protected]>
  • Loading branch information
LeeSmet committed Jan 9, 2025
1 parent 5370449 commit 89bbea8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bridges/stellar-solana/api/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (bridge *Bridge) Start(ctx context.Context) error {
// - Monitor the Contract for Withdrawal events and initiate a Withdrawal transaction accordingly
if !bridge.config.Follower {
// Scan bridge account for outgoing transactions to avoid double withdraws or refunds
if err := bridge.wallet.ScanBridgeAccount(); err != nil {
if err := bridge.wallet.ScanBridgeAccount(ctx); err != nil {
panic(err)
}

Expand Down
16 changes: 8 additions & 8 deletions bridges/stellar-solana/api/bridge/signer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *SignerService) SignMint(ctx context.Context, request SolanaRequest, res
}

// Check in transaction storage if the deposit transaction exists
tx, err := s.stellarWallet.TransactionStorage.GetTransactionWithID(request.TxId)
tx, err := s.stellarWallet.TransactionStorage.GetTransactionWithID(ctx, request.TxId)
if err != nil {
log.Info().Str("txid", request.TxId).Msg("transaction not found")
return err
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *SignerService) Sign(ctx context.Context, request multisig.StellarSignRe
} else if request.Message != "" {
// If the signrequest has a message attached we know it's a refund transaction
log.Info().Str("deposit", request.Message).Msg("Validating refund signing request")
err := s.validateRefundTransaction(request, txn)
err := s.validateRefundTransaction(ctx, request, txn)
if err != nil {
if errors.Is(err, ErrInvalidTransaction) {
log.Warn().Err(err).Msg("Refund validation error")
Expand All @@ -189,7 +189,7 @@ func (s *SignerService) Sign(ctx context.Context, request multisig.StellarSignRe
// If the signrequest is not a withdrawal request and a refund request
// then it's most likely a transfer to fee wallet transaction from a deposit
log.Info().Msg("Validating fee transfer signing request")
err := s.validateDepositFeeTransfer(request, txn)
err = s.validateDepositFeeTransfer(ctx, request, txn)
if err != nil {
if errors.Is(err, ErrInvalidTransaction) {
log.Info().Err(err).Msg("Fee transfer validation error")
Expand Down Expand Up @@ -233,7 +233,7 @@ func (s *SignerService) validateWithdrawal(ctx context.Context, request multisig
amount := int64(withdraw.RawAmount())
receiver := withdraw.Memo()
log.Info().Str("amount", stellar.StroopsToDecimal(amount).String()).Str("receiver", receiver).Str("tx", withdraw.TxID().String()).Msg("validating withdrawal")
withdrawalAlreadyExecuted, err := s.stellarWallet.TransactionStorage.TransactionWithShortTxIDExists(shortTxID)
withdrawalAlreadyExecuted, err := s.stellarWallet.TransactionStorage.TransactionWithShortTxIDExists(ctx, shortTxID)
if err != nil {
return err
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (s *SignerService) validateWithdrawal(ctx context.Context, request multisig
return nil
}

func (s *SignerService) validateRefundTransaction(request multisig.StellarSignRequest, txn *txnbuild.Transaction) error {
func (s *SignerService) validateRefundTransaction(ctx context.Context, request multisig.StellarSignRequest, txn *txnbuild.Transaction) error {
// check if a refund already happened
memo, err := stellar.ExtractMemoFromTx(txn)
if err != nil {
Expand All @@ -296,7 +296,7 @@ func (s *SignerService) validateRefundTransaction(request multisig.StellarSignRe
if memo != request.Message {
return errors.Wrap(ErrInvalidTransaction, "The transaction memo and the signrequest message do not match")
}
alreadyRefunded, err := s.stellarWallet.TransactionStorage.TransactionWithMemoExists(memo)
alreadyRefunded, err := s.stellarWallet.TransactionStorage.TransactionWithMemoExists(ctx, memo)
if err != nil {
return err
}
Expand Down Expand Up @@ -378,14 +378,14 @@ func (s *SignerService) validateRefundTransaction(request multisig.StellarSignRe
return nil
}

func (s *SignerService) validateDepositFeeTransfer(request multisig.StellarSignRequest, txn *txnbuild.Transaction) (err error) {
func (s *SignerService) validateDepositFeeTransfer(ctx context.Context, request multisig.StellarSignRequest, txn *txnbuild.Transaction) (err error) {
// Check if a fee transfer for this already happened
memo, err := stellar.ExtractMemoFromTx(txn)
if err != nil {
log.Warn().Err(err).Msg("Failed to extract memo")
return ErrInvalidTransaction
}
alreadyExists, err := s.stellarWallet.TransactionStorage.TransactionWithMemoExists(memo)
alreadyExists, err := s.stellarWallet.TransactionStorage.TransactionWithMemoExists(ctx, memo)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions bridges/stellar-solana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func main() {
}

txStorage := stellar.NewTransactionStorage(stellarCfg.StellarNetwork, bridgeMasterAddress)
err = txStorage.ScanBridgeAccount()
err = txStorage.ScanBridgeAccount(ctx)
if err != nil {
panic(err)
}
Expand All @@ -248,7 +248,7 @@ func main() {
}
log.Info().Str("network", stellarCfg.StellarNetwork).Str("wallet", stellarWallet.GetAddress()).Msg("Stellar wallet loaded")

sol, err := solana.New(context.Background(), &solCfg)
sol, err := solana.New(ctx, &solCfg)
if err != nil {
fmt.Println(err)
panic(err)
Expand All @@ -266,7 +266,7 @@ func main() {

// Start the signer server
if bridgeCfg.Follower {
err := bridge.NewSignerServer(host, bridgeMasterAddress, sol, stellarWallet, bridgeCfg.DepositFee)
err = bridge.NewSignerServer(host, bridgeMasterAddress, sol, stellarWallet, bridgeCfg.DepositFee)
if err != nil {
panic(err)
}
Expand Down
21 changes: 10 additions & 11 deletions bridges/stellar-solana/stellar/transaction_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func NewTransactionStorage(network, addressToScan string) *TransactionStorage {

// GetTransactionWithId returns a transaction with the given id (hash)
// returns error if the transaction is not found
func (s *TransactionStorage) GetTransactionWithID(txid string) (tx *hProtocol.Transaction, err error) {
func (s *TransactionStorage) GetTransactionWithID(ctx context.Context, txid string) (tx *hProtocol.Transaction, err error) {
// trigger a rescan
// will not rescan from start since we saved the cursor
err = s.ScanBridgeAccount()
err = s.ScanBridgeAccount(ctx)
if err != nil {
return
}
Expand All @@ -57,10 +57,10 @@ func (s *TransactionStorage) GetTransactionWithID(txid string) (tx *hProtocol.Tr
// TransactionExists checks if a transaction exists on the stellar network
// it hashes the transaction and checks if the hash is in the list of known transactions
// this can be used to check if a transaction was already submitted to the stellar network
func (s *TransactionStorage) TransactionExists(txn *txnbuild.Transaction) (exists bool, err error) {
func (s *TransactionStorage) TransactionExists(ctx context.Context, txn *txnbuild.Transaction) (exists bool, err error) {
// trigger a rescan
// will not rescan from start since we saved the cursor
err = s.ScanBridgeAccount()
err = s.ScanBridgeAccount(ctx)
if err != nil {
return
}
Expand All @@ -76,13 +76,13 @@ func (s *TransactionStorage) TransactionExists(txn *txnbuild.Transaction) (exist
}

// TransactionWithShortTxIDExists checks if a transaction is already executed with the given short tx id as memo.
func (s *TransactionStorage) TransactionWithShortTxIDExists(shortID solana.ShortTxID) (bool, error) {
return s.TransactionWithMemoExists(shortID.String())
func (s *TransactionStorage) TransactionWithShortTxIDExists(ctx context.Context, shortID solana.ShortTxID) (bool, error) {
return s.TransactionWithMemoExists(ctx, shortID.String())
}

// TransactionWithMemoExists checks if a transaction with the given memo exists
func (s *TransactionStorage) TransactionWithMemoExists(memo string) (exists bool, err error) {
err = s.ScanBridgeAccount()
func (s *TransactionStorage) TransactionWithMemoExists(ctx context.Context, memo string) (exists bool, err error) {
err = s.ScanBridgeAccount(ctx)
if err != nil {
return
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *TransactionStorage) StoreTransaction(tx hProtocol.Transaction) {
}
}

func (s *TransactionStorage) ScanBridgeAccount() error {
func (s *TransactionStorage) ScanBridgeAccount(ctx context.Context) error {
if s.addressToScan == "" {
return errors.New("no account set, aborting now")
}
Expand All @@ -134,8 +134,7 @@ func (s *TransactionStorage) ScanBridgeAccount() error {
}

log.Debug().Str("account", s.addressToScan).Str("cursor", s.stellarCursor).Msg("start fetching stellar transactions")
// TODO: we should not use the background context here
return fetchTransactions(context.Background(), client, s.addressToScan, s.stellarCursor, transactionHandler)
return fetchTransactions(ctx, client, s.addressToScan, s.stellarCursor, transactionHandler)
}

// GetHorizonClient gets the horizon client based on the transaction storage's network
Expand Down
10 changes: 5 additions & 5 deletions bridges/stellar-solana/stellar/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (w *Wallet) signAndSubmitTransaction(ctx context.Context, txn txnbuild.Tran
log.Error().Err(err).Msg("Failed to extract memo")
return err
}
exists, err := w.TransactionStorage.TransactionWithMemoExists(memo)
exists, err := w.TransactionStorage.TransactionWithMemoExists(ctx, memo)
if err != nil {
return errors.Wrapf(err, "failed to check if transaction exists with memo %s", memo)
}
Expand Down Expand Up @@ -490,7 +490,7 @@ func (w *Wallet) StreamBridgeStellarTransactions(ctx context.Context, cursor str
return
}

log.Info().Str("horizon", client.HorizonURL).Str("account", w.keypair.Address()).Str("cursor", cursor).Msg("Start watching stellar account transactions")
log.Info().Str("horizon", client.HorizonURL).Str("account", w.TransactionStorage.addressToScan).Str("cursor", cursor).Msg("Start watching stellar account transactions")

for {
if ctx.Err() != nil {
Expand All @@ -501,7 +501,7 @@ func (w *Wallet) StreamBridgeStellarTransactions(ctx context.Context, cursor str
handler(tx)
cursor = tx.PagingToken()
}
err = fetchTransactions(ctx, client, w.GetAddress(), cursor, internalHandler)
err = fetchTransactions(ctx, client, w.TransactionStorage.addressToScan, cursor, internalHandler)
if err != nil {
return
}
Expand All @@ -514,8 +514,8 @@ func (w *Wallet) StreamBridgeStellarTransactions(ctx context.Context, cursor str
}
}

func (w *Wallet) ScanBridgeAccount() error {
return w.TransactionStorage.ScanBridgeAccount()
func (w *Wallet) ScanBridgeAccount(ctx context.Context) error {
return w.TransactionStorage.ScanBridgeAccount(ctx)
}

// TODO: is this function really needed?
Expand Down

0 comments on commit 89bbea8

Please sign in to comment.