File size: 6,505 Bytes
edbc049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
BNB Transfer Validator

Validate correctness of BNB native token transfer transaction
"""

from typing import Dict, Any, List


class BNBTransferValidator:
    """BNB Transfer Validator"""
    
    def __init__(self, to_address: str, amount: float):
        """
        Initialize validator
        
        Args:
            to_address: Expected recipient address
            amount: Expected transfer amount in BNB (float)
        """
        from decimal import Decimal
        
        self.expected_to = to_address.lower()
        # Use Decimal for precise BNB to wei conversion
        self.expected_amount = int(Decimal(str(amount)) * Decimal(10**18))
        self.max_score = 100
    
    def validate(
        self,
        tx: Dict[str, Any],
        receipt: Dict[str, Any],
        state_before: Dict[str, Any],
        state_after: Dict[str, Any]
    ) -> Dict[str, Any]:
        """
        Validate transaction
        
        Args:
            tx: Transaction object
            receipt: Transaction receipt
            state_before: State before transaction
            state_after: State after transaction
            
        Returns:
            Validation result dictionary
        """
        checks = []
        score = 0
        
        # Check 1: Transaction success (30 points)
        tx_success = receipt.get('status') == 1
        checks.append({
            'name': 'Transaction Success',
            'passed': tx_success,
            'message': 'Transaction executed successfully' if tx_success else 'Transaction failed',
            'score': 30 if tx_success else 0
        })
        if tx_success:
            score += 30
        
        # Check 2: Target address correct (25 points)
        actual_to = tx.get('to', '').lower() if tx.get('to') else ''
        to_correct = actual_to == self.expected_to
        checks.append({
            'name': 'Target Address Correct',
            'passed': to_correct,
            'message': f"Expected: {self.expected_to}, Actual: {actual_to}",
            'score': 25 if to_correct else 0
        })
        if to_correct:
            score += 25
        
        # Check 3: Transfer amount correct (25 points)
        actual_value = int(tx.get('value', 0))
        
        # Allow small margin of error (considering potential precision issues)
        tolerance = int(self.expected_amount * 0.001)  # 0.1% tolerance
        amount_correct = abs(actual_value - self.expected_amount) <= tolerance
        
        checks.append({
            'name': 'Transfer Amount Correct',
            'passed': amount_correct,
            'message': f"Expected: {self.expected_amount} wei ({self.expected_amount / 10**18} BNB), "
                      f"Actual: {actual_value} wei ({actual_value / 10**18} BNB)",
            'score': 25 if amount_correct else 0
        })
        if amount_correct:
            score += 25
        
        # Check 4: Gas settings reasonable (10 points)
        gas_used = receipt.get('gasUsed', 0)
        # Support both 'gas' (web3.py style) and 'gasLimit' (ethers.js style)
        gas_limit = tx.get('gas', 0) or tx.get('gasLimit', 0)
        
        # Basic transfer gas should be around 21000
        # gas limit should be sufficient and not excessively high
        # Note: system_config suggests gasLimit: 100000n as default, so we use 500000 as upper bound
        gas_reasonable = (
            gas_used >= 21000 and 
            gas_limit >= gas_used and 
            gas_limit <= 500000
        )
        
        checks.append({
            'name': 'Gas Settings Reasonable',
            'passed': gas_reasonable,
            'message': f"Gas Used: {gas_used}, Gas Limit: {gas_limit}",
            'score': 10 if gas_reasonable else 0
        })
        if gas_reasonable:
            score += 10
        
        # Check 5: Balance change correct (10 points)
        balance_before = state_before.get('balance', 0)
        balance_after = state_after.get('balance', 0)
        balance_change = balance_before - balance_after
        
        # Balance change should equal transfer amount + gas fee
        gas_cost = gas_used * receipt.get('effectiveGasPrice', 0)
        expected_balance_change = actual_value + gas_cost
        
        # Allow small margin of error
        balance_tolerance = int(expected_balance_change * 0.001)  # 0.1% tolerance
        balance_correct = abs(balance_change - expected_balance_change) <= balance_tolerance
        
        checks.append({
            'name': 'Balance Change Correct',
            'passed': balance_correct,
            'message': f"Balance decrease: {balance_change} wei (Transfer {actual_value} + Gas {gas_cost})",
            'score': 10 if balance_correct else 0
        })
        if balance_correct:
            score += 10
        
        # Determine if Passed
        passed = all(check['passed'] for check in checks)
        
        # Generate Feedback
        feedback = self._generate_feedback(checks, score, passed)
        
        return {
            'passed': passed,
            'score': score,
            'max_score': self.max_score,
            'checks': checks,
            'feedback': feedback,
            'details': {
                'expected_to': self.expected_to,
                'expected_amount': self.expected_amount,
                'actual_to': actual_to,
                'actual_amount': actual_value,
                'gas_used': gas_used,
                'balance_change': balance_change
            }
        }
    
    def _generate_feedback(
        self,
        checks: List[Dict[str, Any]],
        score: int,
        passed: bool
    ) -> str:
        """
        Generate feedback message
        
        Args:
            checks: List of check items
            score: Total score
            passed: Whether validation passed
            
        Returns:
            Feedback string
        """
        lines = []
        
        if passed:
            lines.append("🎉 Congratulations! All validation checks passed!")
            lines.append(f"Final Score: {score}/{self.max_score}")
        else:
            lines.append("❌ Validation failed, please check the following issues:")
            failed_checks = [c for c in checks if not c['passed']]
            for check in failed_checks:
                lines.append(f"  - {check['name']}: {check['message']}")
            lines.append(f"\nCurrent Score: {score}/{self.max_score}")
        
        return '\n'.join(lines)