File size: 11,389 Bytes
4e4664a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
Blockchain Utilities
====================
Retry logic, error handling, and transaction monitoring for XRP Ledger operations.
"""

import time
from typing import Dict, Any, Optional, Callable
from xrpl.clients import JsonRpcClient
from xrpl.models import Response
from xrpl.wallet import Wallet
from xrpl.transaction import submit_and_wait
import logging

logger = logging.getLogger(__name__)


class BlockchainError(Exception):
    """Custom exception for blockchain-related errors with user-friendly messages"""
    def __init__(self, technical_msg: str, user_msg: str, retryable: bool = False):
        self.technical_msg = technical_msg
        self.user_msg = user_msg
        self.retryable = retryable
        super().__init__(technical_msg)


class BlockchainRetryHandler:
    """
    Handles retry logic for blockchain transactions with exponential backoff.
    """
    
    def __init__(
        self,
        max_retries: int = 3,
        base_delay: float = 1.0,
        max_delay: float = 10.0,
        exponential_base: float = 2.0
    ):
        """
        Initialize retry handler.
        
        Args:
            max_retries: Maximum number of retry attempts
            base_delay: Initial delay in seconds
            max_delay: Maximum delay between retries
            exponential_base: Base for exponential backoff calculation
        """
        self.max_retries = max_retries
        self.base_delay = base_delay
        self.max_delay = max_delay
        self.exponential_base = exponential_base
    
    def _calculate_delay(self, attempt: int) -> float:
        """Calculate delay with exponential backoff"""
        delay = self.base_delay * (self.exponential_base ** attempt)
        return min(delay, self.max_delay)
    
    def execute_with_retry(
        self,
        operation: Callable,
        operation_name: str,
        *args,
        **kwargs
    ) -> Any:
        """
        Execute a blockchain operation with retry logic.
        
        Args:
            operation: Function to execute
            operation_name: Human-readable name for logging
            *args, **kwargs: Arguments to pass to operation
            
        Returns:
            Result from the operation
            
        Raises:
            BlockchainError: If all retries fail
        """
        last_exception = None
        
        for attempt in range(self.max_retries + 1):
            try:
                logger.info(f"[BLOCKCHAIN] Attempting {operation_name} (attempt {attempt + 1}/{self.max_retries + 1})")
                result = operation(*args, **kwargs)
                
                if attempt > 0:
                    logger.info(f"[BLOCKCHAIN] ✅ {operation_name} succeeded after {attempt} retries")
                
                return result
                
            except Exception as e:
                last_exception = e
                error_msg = str(e)
                
                # Check if error is retryable
                is_retryable = self._is_retryable_error(error_msg)
                
                if not is_retryable or attempt >= self.max_retries:
                    logger.error(f"[BLOCKCHAIN] [ERROR] {operation_name} failed permanently: {error_msg}")
                    raise self._convert_to_user_friendly_error(e, operation_name)
                
                # Calculate delay and retry
                delay = self._calculate_delay(attempt)
                logger.warning(
                    f"[BLOCKCHAIN] [WARNING] {operation_name} failed (attempt {attempt + 1}): {error_msg}. "
                    f"Retrying in {delay:.1f}s..."
                )
                time.sleep(delay)
        
        # Should never reach here, but just in case
        raise self._convert_to_user_friendly_error(
            last_exception or Exception("Unknown error"),
            operation_name
        )
    
    def _is_retryable_error(self, error_msg: str) -> bool:
        """
        Determine if an error is retryable based on error message.
        
        Retryable errors include:
        - Network issues
        - Temporary server errors
        - Sequence number mismatches
        - Insufficient XRP (user might fund wallet)
        
        Non-retryable errors include:
        - Invalid signatures
        - Malformed transactions
        - Insufficient token balance (not XRP)
        """
        error_lower = error_msg.lower()
        
        # Retryable errors
        retryable_keywords = [
            "timeout",
            "connection",
            "network",
            "sequence",
            "telINSUF_FEE_P",  # Fee too low (can retry with higher fee)
            "tefPAST_SEQ",     # Sequence already used
            "terQUEUED",       # Transaction queued
        ]
        
        for keyword in retryable_keywords:
            if keyword in error_lower:
                return True
        
        # Non-retryable errors
        non_retryable_keywords = [
            "signature",
            "malformed",
            "tecUNFUNDED_PAYMENT",  # Insufficient token balance
            "tecNO_DST",            # Destination doesn't exist
            "tecNO_PERMISSION",     # Unauthorized
            "temBAD_FEE",           # Invalid fee
        ]
        
        for keyword in non_retryable_keywords:
            if keyword in error_lower:
                return False
        
        # Default: retry on unknown errors (conservative approach)
        return True
    
    def _convert_to_user_friendly_error(
        self,
        exception: Exception,
        operation_name: str
    ) -> BlockchainError:
        """
        Convert technical blockchain errors to user-friendly messages.
        """
        error_msg = str(exception).lower()
        
        # Map technical errors to user messages
        if "insuf" in error_msg and "fee" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Transaction fee too low. Please try again.",
                retryable=True
            )
        
        if "tecUNFUNDED_PAYMENT" in error_msg or "insufficient" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Insufficient balance to complete this transaction.",
                retryable=False
            )
        
        if "sequence" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Transaction ordering issue. Please try again.",
                retryable=True
            )
        
        if "timeout" in error_msg or "connection" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Network connection issue. Please check your internet and try again.",
                retryable=True
            )
        
        if "signature" in error_msg or "unauthorized" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Authentication failed. Please contact support.",
                retryable=False
            )
        
        if "tecNO_DST" in error_msg:
            return BlockchainError(
                technical_msg=str(exception),
                user_msg="Recipient wallet not activated. Please ensure the wallet is funded with XRP.",
                retryable=False
            )
        
        # Default user-friendly message
        return BlockchainError(
            technical_msg=str(exception),
            user_msg=f"Transaction failed: {operation_name}. Please try again or contact support if the issue persists.",
            retryable=True
        )


class TransactionMonitor:
    """
    Monitor blockchain transaction status and provide detailed feedback.
    """
    
    @staticmethod
    def submit_and_monitor(
        client: JsonRpcClient,
        transaction: Any,
        wallet: Wallet,
        operation_name: str = "Transaction"
    ) -> Response:
        """
        Submit a transaction and monitor its status with detailed logging.
        
        Args:
            client: XRP Ledger client
            transaction: Transaction to submit
            wallet: Wallet to sign with
            operation_name: Human-readable operation name
            
        Returns:
            Transaction response
            
        Raises:
            BlockchainError: If transaction fails
        """
        logger.info(f"[TX_MONITOR] 📤 Submitting {operation_name}...")
        logger.debug(f"[TX_MONITOR] Transaction details: {transaction.to_dict()}")
        
        try:
            # Submit and wait for validation
            response = submit_and_wait(transaction, client, wallet)
            
            # Check result
            result = response.result
            metadata = result.get("meta", {})
            tx_result = metadata.get("TransactionResult", "unknown")
            
            logger.info(f"[TX_MONITOR] Transaction hash: {result.get('hash', 'N/A')}")
            logger.info(f"[TX_MONITOR] Result code: {tx_result}")
            
            # Success codes start with "tes" (tesSUCCESS)
            if tx_result.startswith("tes"):
                logger.info(f"[TX_MONITOR] ✅ {operation_name} succeeded")
                return response
            
            # Error codes
            error_msg = f"{operation_name} failed with code: {tx_result}"
            logger.error(f"[TX_MONITOR] [ERROR] {error_msg}")
            
            raise BlockchainError(
                technical_msg=error_msg,
                user_msg=TransactionMonitor._get_user_message_for_code(tx_result),
                retryable=tx_result.startswith("ter")  # "ter" = retry, "tec" = claimed fee, "tem"/"tef" = malformed
            )
            
        except BlockchainError:
            raise
        except Exception as e:
            logger.error(f"[TX_MONITOR] [ERROR] {operation_name} exception: {str(e)}")
            raise BlockchainError(
                technical_msg=str(e),
                user_msg=f"{operation_name} failed. Please try again.",
                retryable=True
            )
    
    @staticmethod
    def _get_user_message_for_code(result_code: str) -> str:
        """Map XRP Ledger result codes to user-friendly messages"""
        
        code_map = {
            "tecUNFUNDED_PAYMENT": "Insufficient balance to complete this transaction.",
            "tecNO_DST": "Destination wallet not found or not activated.",
            "tecNO_LINE": "Trust line not established. Please enable the token first.",
            "tecNO_PERMISSION": "You don't have permission to perform this action.",
            "tecINSUFFICIENT_RESERVE": "Insufficient XRP reserve. You need at least 10 XRP in your wallet.",
            "tecPATH_DRY": "No available path for this transaction.",
            "terQUEUED": "Transaction queued. Please wait a moment and try again.",
            "tefPAST_SEQ": "Transaction already processed. Please refresh and check your balance.",
        }
        
        return code_map.get(result_code, f"Transaction failed with code {result_code}. Please contact support.")


# Global retry handler instance
retry_handler = BlockchainRetryHandler(
    max_retries=3,
    base_delay=1.0,
    max_delay=10.0
)