33import random
44
55from flask import Flask , jsonify , request
6+ from flask_cors import CORS
67
78from backend .blockchain .blockchain import Blockchain
89from backend .wallet .wallet import Wallet
1112from backend .pubsub import PubSub
1213
1314app = Flask (__name__ )
15+ CORS (app , resources = { r'/*' : { 'origins' : 'http://localhost:3000' } })
1416blockchain = Blockchain ()
15- wallet = Wallet ()
17+ wallet = Wallet (blockchain )
1618transaction_pool = TransactionPool ()
1719pubsub = PubSub (blockchain , transaction_pool )
1820
@@ -24,9 +26,23 @@ def route_default():
2426def route_blockchain ():
2527 return jsonify (blockchain .to_json ())
2628
29+ @app .route ('/blockchain/range' )
30+ def route_blockchain_range ():
31+ # http://localhost:5050/blockchain/range?start=2&end=5
32+ start = int (request .args .get ('start' ))
33+ end = int (request .args .get ('end' ))
34+
35+ return jsonify (blockchain .to_json ()[::- 1 ][start :end ])
36+
37+ @app .route ('/blockchain/length' )
38+ def route_blockchain_length ():
39+ return jsonify (len (blockchain .chain ))
40+
2741@app .route ('/blockchain/mine' )
2842def route_blockchain_mine ():
29- blockchain .add_block (transaction_pool .transaction_data ())
43+ transaction_data = transaction_pool .transaction_data ()
44+ transaction_data .append (Transaction .reward_transaction (wallet ).to_json ())
45+ blockchain .add_block (transaction_data )
3046 block = blockchain .chain [- 1 ]
3147 pubsub .broadcast_block (block )
3248 transaction_pool .clear_blockchain_transactions (blockchain )
@@ -52,9 +68,28 @@ def route_wallet_transact():
5268 )
5369
5470 pubsub .broadcast_transaction (transaction )
71+ transaction_pool .set_transaction (transaction )
5572
5673 return jsonify (transaction .to_json ())
5774
75+ @app .route ('/wallet/info' )
76+ def route_wallet_info ():
77+ return jsonify ({ 'address' : wallet .address , 'balance' : wallet .balance })
78+
79+ @app .route ('/known-addresses' )
80+ def route_known_addresses ():
81+ known_addresses = set ()
82+
83+ for block in blockchain .chain :
84+ for transaction in block .data :
85+ known_addresses .update (transaction ['output' ].keys ())
86+
87+ return jsonify (list (known_addresses ))
88+
89+ @app .route ('/transactions' )
90+ def route_transactions ():
91+ return jsonify (transaction_pool .transaction_data ())
92+
5893ROOT_PORT = 5050
5994PORT = ROOT_PORT
6095
@@ -70,5 +105,17 @@ def route_wallet_transact():
70105 except Exception as e :
71106 print (f'\n -- Error synchronizing: { e } ' )
72107
73- app .run (port = 5050 )
108+ if os .environ .get ('SEED_DATA' ) == 'True' :
109+ for i in range (10 ):
110+ blockchain .add_block ([
111+ Transaction (Wallet (), Wallet ().address , random .randint (2 , 50 )).to_json (),
112+ Transaction (Wallet (), Wallet ().address , random .randint (2 , 50 )).to_json ()
113+ ])
114+
115+ for i in range (3 ):
116+ transaction_pool .set_transaction (
117+ Transaction (Wallet (), Wallet ().address , random .randint (2 , 50 ))
118+ )
119+
120+ app .run (port = PORT )
74121
0 commit comments