forked from xyephy/transactions_bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_scripts.py
More file actions
179 lines (149 loc) Β· 5.5 KB
/
Copy pathtest_scripts.py
File metadata and controls
179 lines (149 loc) Β· 5.5 KB
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
#!/usr/bin/env python3
"""
Pre-Class Testing Script
Run this to verify all Bitcoin fundamentals scripts work correctly
"""
import subprocess
import sys
import time
import requests
import os
def test_bitcoin_core_connection():
"""Test connection to Polar's Bitcoin Core node"""
print("π Testing Bitcoin Core connection...")
url = "http://polaruser:polarpass@localhost:18443"
payload = {
"jsonrpc": "2.0",
"id": "test",
"method": "getblockchaininfo",
"params": []
}
try:
response = requests.post(url, json=payload, timeout=5)
if response.status_code == 200:
result = response.json()
height = result['result']['blocks']
print(f"β
Connected to Bitcoin Core! Current height: {height}")
if height < 110:
print(f"β οΈ Warning: Only {height} blocks. Mine more blocks in Polar for mature coinbase rewards.")
print(f" π‘ Click Bitcoin Core node β Actions β Mine (generate 110 blocks)")
return True
else:
print(f"β Connection failed: HTTP {response.status_code}")
return False
except Exception as e:
print(f"β Connection failed: {e}")
print(f"π‘ Make sure Polar is running with Bitcoin Core node")
return False
def test_python_dependencies():
"""Test if required Python packages are available"""
print("\nπ Testing Python dependencies...")
required_packages = [
'requests',
'hashlib',
'json',
'time',
'dataclasses'
]
missing_packages = []
for package in required_packages:
try:
__import__(package)
print(f"β
{package}")
except ImportError:
print(f"β {package} - MISSING")
missing_packages.append(package)
# Test optional BDK
try:
import bdkpython
print(f"β
bdkpython (optional)")
except ImportError:
print(f"β οΈ bdkpython - Optional (will run in simulation mode)")
if missing_packages:
print(f"\nπ¦ Install missing packages:")
print(f"pip install {' '.join(missing_packages)}")
return False
return True
def test_script(script_path, timeout=60):
"""Test a single script with timeout"""
script_name = os.path.basename(script_path)
print(f"\nπ§ͺ Testing {script_name}...")
try:
# Run script in test mode (non-interactive)
env = os.environ.copy()
env['BITCOIN_TEST_MODE'] = '1' # Signal to scripts to run in test mode
# Use the virtual environment Python if available
python_exe = "bitcoin_class_env/bin/python" if os.path.exists("bitcoin_class_env/bin/python") else sys.executable
process = subprocess.Popen(
[python_exe, script_path],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Wait for completion with timeout
stdout, stderr = process.communicate(timeout=timeout)
if process.returncode == 0:
print(f"β
{script_name} - PASSED")
return True
else:
print(f"β {script_name} - FAILED")
if stderr:
# Show only first few lines of error
error_lines = stderr.strip().split('\n')
print(f"Error: {error_lines[-1] if error_lines else 'Unknown error'}")
return False
except subprocess.TimeoutExpired:
process.kill()
print(f"β° {script_name} - TIMEOUT (>{timeout}s)")
return False
except Exception as e:
print(f"β {script_name} - ERROR: {e}")
return False
def main():
"""Run all pre-class tests"""
print("π BITCOIN FUNDAMENTALS - PRE-CLASS TESTING")
print("=" * 50)
# Test 1: Python dependencies
if not test_python_dependencies():
print("\nβ Python dependency test failed. Fix dependencies first.")
return False
# Test 2: Bitcoin Core connection
if not test_bitcoin_core_connection():
print("\nβ Bitcoin Core connection failed. Start Polar first.")
return False
# Test 3: Individual scripts
scripts_to_test = [
"scripts/1_transactions.py",
"scripts/2_blockchain.py",
"scripts/3_proof_of_work.py",
"scripts/4_network_and_storage.py",
"scripts/5_mining_and_incentives.py",
"scripts/bdk_bitcoin_demo.py"
]
passed = 0
total = len(scripts_to_test)
for script in scripts_to_test:
if os.path.exists(script):
if test_script(script):
passed += 1
else:
print(f"β {script} - FILE NOT FOUND")
# Summary
print(f"\nπ TEST SUMMARY")
print(f"=" * 20)
print(f"β
Passed: {passed}/{total}")
print(f"β Failed: {total - passed}/{total}")
if passed == total:
print(f"\nπ ALL TESTS PASSED! Ready for class! π")
print(f"\nπ‘ Quick start guide:")
print(f" 1. Start Polar with Bitcoin Core node")
print(f" 2. Mine 110+ blocks for mature coinbase")
print(f" 3. Run: python scripts/1_transactions.py")
print(f" 4. Continue through all 5 modules")
return True
else:
print(f"\nβ οΈ Some tests failed. Fix issues before class.")
return False
if __name__ == "__main__":
main()