Skip to content

Commit 3c2d14a

Browse files
committed
Add version 2 of BCubed
1 parent e676088 commit 3c2d14a

49 files changed

Lines changed: 1300 additions & 361 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@ version = "0.0.1"
88
readme = "README.md"
99
requires-python = ">=3.10"
1010

11-
[tool.setuptools]
12-
package-dir = {"" = "src"}
13-
1411
[tool.setuptools.packages.find]
1512
where = ["src/"]

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ pytest==8.3.2
44
pytest-cov==5.0.0
55
pyyaml==6.0.2
66
py-solc-x==2.0.3
7-
typeguard==4.3.0 # pyyaml dependency
7+
typeguard==4.4.1 # pyyaml dependency
88
web3==7.5.0

src/bcubed/bcubed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def __init__(self):
3131
self.__logger = Logger(
3232
self.__name + "-" + datetime.today().strftime('%Y-%m-%d'))
3333

34+
self.__setup()
35+
3436
web3 = Web3(self.__provider)
3537
self.__node = Node(Network(web3), Contract())
3638

37-
self.__setup()
38-
3939
self._logger.info("BCubed '%s' initialized.", self.__name)
4040

4141
def __setup(self):

src/bcubed/blockchain/contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def compile(self):
6565
Compiles the Solidity smart contracts and saves the compiled contract to a json file.
6666
"""
6767

68-
self.__logger.info("Compiling smart contracts...")
68+
self.__logger.info("Compiling smart contract...")
6969

7070
# Get the smart contract
7171
smart_contract = os.path.join(

src/bcubed/blockchain/network.py

Lines changed: 193 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
import sys
1212

1313
from web3 import Web3
14+
from web3.exceptions import (
15+
BadFunctionCallOutput,
16+
ContractLogicError,
17+
Web3AttributeError,
18+
Web3RPCError,
19+
Web3TypeError,
20+
Web3ValueError,
21+
)
22+
23+
from eth_abi.exceptions import InsufficientDataBytes
1424

1525
from bcubed.config.config import Config
1626
from bcubed.constants.config.config_categories import ConfigCategories
@@ -70,9 +80,13 @@ def __setup_configuration(self):
7080
self.__chain_id = config.get_property(
7181
ConfigKeys.CHAIN_ID, ConfigCategories.NETWORK
7282
)
73-
self.__account_address = config.get_property(
83+
account_address = config.get_property(
7484
ConfigKeys.ACCOUNT_ADDRESS, ConfigCategories.NETWORK
7585
)
86+
87+
# It is needed to support different blockchain simulators
88+
self.__account_address = self.__w3.to_checksum_address(account_address)
89+
7690
self.__private_key = config.get_property(
7791
ConfigKeys.PRIVATE_KEY, ConfigCategories.NETWORK
7892
)
@@ -138,10 +152,27 @@ def deploy_contract(
138152
transaction_container.contractAddress
139153
)
140154

141-
except Exception as ex:
155+
except Web3ValueError as ex:
142156
self.__logger.critical(
143-
"Exception when trying to deploy contract: %s", ex
144-
)
157+
"Web3ValueError when trying to deploy contract: %s", ex)
158+
159+
sys.exit()
160+
161+
except Web3AttributeError as ex:
162+
self.__logger.critical(
163+
"Web3AttributeError when trying to deploy contract: %s", ex)
164+
165+
sys.exit()
166+
167+
except Web3TypeError as ex:
168+
self.__logger.critical(
169+
"Web3TypeError when trying to deploy contract: %s", ex)
170+
171+
sys.exit()
172+
173+
except Web3RPCError as ex:
174+
self.__logger.critical(
175+
"Web3RPCError when trying to deploy contract: %s", ex)
145176

146177
sys.exit()
147178

@@ -192,17 +223,47 @@ def store_meta_data_record(self, meta_data_record: MetaDataRecord):
192223

193224
self.__sign_and_send_raw_transaction(transaction)
194225

195-
except AttributeError as ex:
226+
except Web3AttributeError as ex:
196227
self.__logger.error(
197-
"AttributeError when storing new MD record: %s", ex)
228+
"Web3AttributeError when storing new MD record: %s", ex)
198229

199230
return False
200231

201-
except Exception as ex:
202-
self.__logger.error("Exception when storing new MD record: %s", ex)
232+
except Web3TypeError as ex:
233+
self.__logger.error(
234+
"Web3TypeError when storing new MD record: %s", ex)
203235

204236
return False
205237

238+
except Web3RPCError as ex:
239+
if 'Insufficient funds for gas * price + value' in ex.args[0]:
240+
self.__logger.critical(
241+
"Web3RPCError when storing new MD record: %s", ex)
242+
sys.exit()
243+
else:
244+
self.__logger.error(
245+
"Web3RPCError when storing new MD record: %s", ex)
246+
247+
return False
248+
249+
except ContractLogicError as ex:
250+
self.__logger.critical(
251+
"ContractLogicError when storing new MD record: %s", ex.message)
252+
253+
return False
254+
255+
except InsufficientDataBytes as ex:
256+
self.__logger.critical(
257+
"InsufficientDataBytes when storing new MD record: %s", ex)
258+
259+
sys.exit()
260+
261+
except BadFunctionCallOutput as ex:
262+
self.__logger.critical(
263+
"BadFunctionCallOutput when storing new MD record: %s", ex)
264+
265+
sys.exit()
266+
206267
self.__logger.info("New MD record was stored")
207268
self.get_meta_data_record()
208269

@@ -220,10 +281,30 @@ def get_meta_data_record(self):
220281
meta_data_record = from_contract_tuple_to_meta_data_record(
221282
contract_tuple)
222283

223-
except Exception as ex:
224-
self.__logger.error("Exception when getting MD record: %s", ex)
284+
except Web3AttributeError as ex:
285+
self.__logger.error(
286+
"Web3AttributeError when getting MD record: %s", ex)
287+
288+
return None
289+
290+
except Web3TypeError as ex:
291+
self.__logger.error(
292+
"Web3TypeError when getting MD record: %s", ex)
293+
225294
return None
226295

296+
except InsufficientDataBytes as ex:
297+
self.__logger.critical(
298+
"InsufficientDataBytes when getting new MD record: %s", ex)
299+
300+
sys.exit()
301+
302+
except BadFunctionCallOutput as ex:
303+
self.__logger.critical(
304+
"BadFunctionCallOutput when getting new MD record: %s", ex)
305+
306+
sys.exit()
307+
227308
self.__logger.info("MD record was retrieved")
228309
self.__logger.info("%s", meta_data_record.to_string())
229310

@@ -251,15 +332,33 @@ def store_system_data_records(self, system_data_records: list, estimated_gas: in
251332

252333
self.__sign_and_send_raw_transaction(transaction)
253334

254-
except AttributeError as ex:
335+
except Web3AttributeError as ex:
255336
self.__logger.error(
256-
"AttributeError when storing new SD records: %s", ex)
337+
"Web3AttributeError when storing new SD records: %s", ex)
257338

258339
return False
259340

260-
except Exception as ex:
341+
except Web3TypeError as ex:
261342
self.__logger.error(
262-
"Exception when storing new SD records: %s", ex)
343+
"Web3TypeError when storing new SD records: %s", ex)
344+
345+
return False
346+
347+
except Web3RPCError as ex:
348+
if 'Insufficient funds for gas * price + value' in ex.args[0]:
349+
self.__logger.critical(
350+
"Web3RPCError when storing new SD records: %s", ex.message)
351+
sys.exit()
352+
353+
else:
354+
self.__logger.error(
355+
"Web3RPCError when storing new SD records: %s", ex.message)
356+
357+
return False
358+
359+
except ContractLogicError as ex:
360+
self.__logger.error(
361+
"ContractLogicError when storing new SD records: %s", ex.message)
263362

264363
return False
265364

@@ -288,9 +387,15 @@ def get_system_data_records_by_timestamp(
288387
system_data_records = from_contract_tuple_to_system_data_records(
289388
contract_tuples)
290389

291-
except Exception as ex:
390+
except Web3AttributeError as ex:
391+
self.__logger.error(
392+
"Web3AttributeError when getting SD records by timestamp: %s", ex
393+
)
394+
return []
395+
396+
except Web3TypeError as ex:
292397
self.__logger.error(
293-
"Exception when getting SD records by timestamp: %s", ex
398+
"Web3TypeError when getting SD records by timestamp: %s", ex
294399
)
295400
return []
296401

@@ -308,7 +413,9 @@ def store_overview_data_record(self, overview_data_record: OverviewDataRecord):
308413
contract_tuple = from_record_to_contract_tuple(overview_data_record)
309414

310415
try:
311-
transaction = self.__deployed_contract.functions.setOverviewDataRecord(tuple(contract_tuple.values())).build_transaction(
416+
tuple_values = tuple(contract_tuple.values())
417+
transaction = self.__deployed_contract.functions.setOverviewDataRecord(
418+
tuple_values).build_transaction(
312419
{
313420
TRANSACTION_CHAIN_ID: self.__chain_id,
314421
TRANSACTION_FROM: self.__account_address,
@@ -320,14 +427,33 @@ def store_overview_data_record(self, overview_data_record: OverviewDataRecord):
320427

321428
self.__sign_and_send_raw_transaction(transaction)
322429

323-
except AttributeError as ex:
430+
except Web3AttributeError as ex:
324431
self.__logger.error(
325-
"AttributeError when storing new OD record: %s", ex)
432+
"Web3AttributeError when storing new OD record: %s", ex)
326433

327434
return False
328435

329-
except Exception as ex:
330-
self.__logger.error("Exception when storing new OD record: %s", ex)
436+
except Web3TypeError as ex:
437+
self.__logger.error(
438+
"Web3TypeError when storing new OD record: %s", ex)
439+
440+
return False
441+
442+
except Web3RPCError as ex:
443+
if 'Insufficient funds for gas * price + value' in ex.args[0]:
444+
self.__logger.critical(
445+
"Web3RPCError when storing new OD record: %s", ex)
446+
sys.exit()
447+
448+
else:
449+
self.__logger.error(
450+
"Web3RPCError when storing new OD record: %s", ex)
451+
452+
return False
453+
454+
except ContractLogicError as ex:
455+
self.__logger.error(
456+
"ContractLogicError when storing new OD record: %s", ex.message)
331457

332458
return False
333459

@@ -348,8 +474,16 @@ def get_overview_data_record(self):
348474
overview_data_record = from_contract_tuple_to_overview_data_record(
349475
contract_tuple)
350476

351-
except Exception as ex:
352-
self.__logger.error("Exception when getting OD record: %s", ex)
477+
except Web3AttributeError as ex:
478+
self.__logger.error(
479+
"Web3AttributeError when getting OD record: %s", ex)
480+
481+
return None
482+
483+
except Web3TypeError as ex:
484+
self.__logger.error(
485+
"Web3TypeError when getting OD record: %s", ex)
486+
353487
return None
354488

355489
self.__logger.info("OD record was retrieved")
@@ -377,11 +511,29 @@ def get_estimated_gas(self, system_data_records: list):
377511
}
378512
)
379513

380-
except Exception as ex:
381-
self.__logger.error("Exception when estimating gas: %s", ex)
514+
except Web3AttributeError as ex:
515+
self.__logger.error(
516+
"Web3AttributeError when estimating gas: %s", ex)
517+
518+
return 0
519+
520+
except Web3TypeError as ex:
521+
self.__logger.error("Web3TypeError when estimating gas: %s", ex)
382522

383523
return 0
384524

525+
except Web3RPCError as ex:
526+
self.__logger.error(
527+
"Web3RPCError when estimating gas: %s", ex.message)
528+
529+
sys.exit()
530+
531+
except ContractLogicError as ex:
532+
self.__logger.error(
533+
"ContractLogicError when estimating gas: %s", ex.message)
534+
535+
sys.exit()
536+
385537
self.__logger.debug("Estimated gas: %d", gas)
386538

387539
return gas
@@ -394,9 +546,15 @@ def get_initial_timestamp(self):
394546
try:
395547
initial_timestamp = self.__deployed_contract.functions.getInitialTimestamp().call()
396548

397-
except Exception as ex:
549+
except Web3AttributeError as ex:
550+
self.__logger.error(
551+
"Web3AttributeError when getting the initial timestamp: %s", ex)
552+
553+
return 0
554+
555+
except Web3TypeError as ex:
398556
self.__logger.error(
399-
"Exception when getting the initial timestamp: %s", ex)
557+
"Web3TypeError when getting the initial timestamp: %s", ex)
400558

401559
return 0
402560

@@ -412,9 +570,15 @@ def get_final_timestamp(self):
412570
try:
413571
final_timestamp = self.__deployed_contract.functions.getFinalTimestamp().call()
414572

415-
except Exception as ex:
573+
except Web3AttributeError as ex:
574+
self.__logger.error(
575+
"Web3AttributeError when getting the final timestamp: %s", ex)
576+
577+
return 0
578+
579+
except Web3TypeError as ex:
416580
self.__logger.error(
417-
"Exception when getting the final timestamp: %s", ex)
581+
"Web3TypeError when getting the final timestamp: %s", ex)
418582

419583
return 0
420584

0 commit comments

Comments
 (0)