88env_path = Path (__file__ ).parent .parent / '.env'
99load_dotenv (dotenv_path = env_path )
1010
11- from flask import Flask , jsonify , request
11+ from flask import Flask , jsonify , request , Response
1212from flask_cors import CORS
13+ import json
1314
1415from backend .blockchain .blockchain import Blockchain
1516from backend .wallet .wallet import Wallet
1920
2021app = Flask (__name__ )
2122CORS (app , resources = { r'/*' : { 'origins' : 'http://localhost:3000' } })
23+
24+ def json_response (data , status = 200 ):
25+ """
26+ Create a JSON response that preserves large integers.
27+ Flask's default jsonify converts large ints to floats, which breaks
28+ cryptographic signatures. This function ensures integers are preserved.
29+ """
30+ return Response (
31+ json .dumps (data , separators = (',' , ':' )),
32+ status = status ,
33+ mimetype = 'application/json'
34+ )
2235blockchain = Blockchain ()
2336wallet = Wallet (blockchain )
2437transaction_pool = TransactionPool ()
@@ -30,7 +43,7 @@ def route_default():
3043
3144@app .route ('/blockchain' )
3245def route_blockchain ():
33- return jsonify (blockchain .to_json ())
46+ return json_response (blockchain .to_json ())
3447
3548@app .route ('/blockchain/range' )
3649def route_blockchain_range ():
@@ -53,7 +66,7 @@ def route_blockchain_mine():
5366 pubsub .broadcast_block (block )
5467 transaction_pool .clear_blockchain_transactions (blockchain )
5568
56- return jsonify (block .to_json ())
69+ return json_response (block .to_json ())
5770
5871@app .route ('/wallet/transact' , methods = ['POST' ])
5972def route_wallet_transact ():
@@ -102,7 +115,9 @@ def route_transactions():
102115if os .environ .get ('PEER' ) == 'True' :
103116 PORT = random .randint (5051 , 6000 )
104117
105- result = requests .get (f'http://localhost:{ ROOT_PORT } /blockchain' )
118+ # In Docker, use service name instead of localhost
119+ ROOT_HOST = os .environ .get ('ROOT_BACKEND_HOST' , 'localhost' )
120+ result = requests .get (f'http://{ ROOT_HOST } :{ ROOT_PORT } /blockchain' )
106121 result_blockchain = Blockchain .from_json (result .json ())
107122
108123 try :
@@ -119,9 +134,10 @@ def route_transactions():
119134 ])
120135
121136 for i in range (3 ):
122- transaction_pool . set_transaction (
123- Transaction ( Wallet (), Wallet (). address , random . randint ( 2 , 50 ) )
124- )
137+ transaction = Transaction ( Wallet (), Wallet (). address , random . randint ( 2 , 50 ))
138+ pubsub . broadcast_transaction ( transaction )
139+ transaction_pool . set_transaction ( transaction )
125140
126- app .run (port = PORT )
141+ if __name__ == "__main__" :
142+ app .run (host = "0.0.0.0" , port = PORT , debug = True )
127143
0 commit comments