Check for duplicate Contracts
Request Summary
fameio
checks for duplicates in contracts which may cause errors in fame-core
.
Use case
Users may copy/edit contracts and by mistake create duplicates of contracts.
The error message of fame-core
then can be cryptic and it is difficult to trace the cause of the error.
Solution proposal
First, we need to define equality of contracts, i.e. adding to contracts.py
:
Two contracts are equal, if they have identical sender_id, receiver_id, product_name, and delivery_interval.
In addition, they need to have overlapping times based on first_delivery_time, delivery_interval, and expiration_time.
Also, their attributes must match.
Thus, implement an equality check based on these criteria:
def __eq__(self, other):
...
For computationally efficient comparison, implement the hash method in contracts.py
:
def __hash__(self):
return hash((sender_id, receiver_id, product_name, ...))
Then, add each contract to a temporary dict, mapping contract hashes to contracts with the same hash
collision_dict = {}
for contract in contracts:
the_hash = hash(contract)
if the_hash in collision_dict:
for similar in collision_dict[the_hash]:
if contract == similar:
... # raise error
collision_dict[the_hash].append(contract)
else:
collision_dict[the_hash] = [contract]
Since hashes are not necessarily unique, additional checks for equality for object with same hashes is done.