-
Notifications
You must be signed in to change notification settings - Fork 0
Listing integration #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e4579ec
725612f
91f0f2e
610eb1a
76fd57f
318296a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
""" | ||
import os | ||
import logging.config | ||
from time import sleep | ||
from time import sleep, time | ||
from web3 import Web3 | ||
from computable.contracts import Datatrust, Voting | ||
from computable.helpers.transaction import call, send | ||
|
@@ -57,9 +57,8 @@ def initialize_datatrust(self): | |
self.voting = Voting(self.datatrust_wallet) | ||
self.voting.at(self.w3, self.voting_contract) | ||
|
||
backend = call(self.datatrust.get_backend_address()) | ||
datatrust_hash = self.w3.sha3(text=self.datatrust_host) | ||
if backend == self.datatrust_wallet: | ||
if self.get_backend_address(): | ||
log.info('This server is the datatrust host. Resolving registration') | ||
resolve = send( | ||
self.w3, | ||
|
@@ -103,6 +102,16 @@ def initialize_datatrust(self): | |
self.wait_for_mining(resolve) | ||
register = self.register_host() | ||
|
||
def get_backend_address(self): | ||
""" | ||
Return True if the Ethereum address for the voted-in datatrust is this datatrust | ||
""" | ||
backend = call(self.datatrust.get_backend_address()) | ||
if backend == self.datatrust_wallet: | ||
return True | ||
else: | ||
return False | ||
|
||
def wait_for_vote(self): | ||
""" | ||
Check if this backend is registered as the datatrust | ||
|
@@ -137,15 +146,12 @@ def send_data_hash(self, listing, data_hash): | |
""" | ||
On a successful post to the API db, send the data hash to protocol | ||
""" | ||
datatrust_hash = self.w3.sha3(text=self.datatrust_host) | ||
is_candidate = call(self.voting.is_candidate(datatrust_hash)) | ||
candidate_is = call(self.voting.candidate_is(datatrust_hash, constants.PROTOCOL_APPLICATION)) | ||
if is_candidate and candidate_is: | ||
if self.get_backend_address(): | ||
receipt = send(self.w3, self.datatrust_key, self.datatrust.set_data_hash(listing, data_hash)) | ||
return receipt | ||
else: | ||
log.critical('This server is not the datatrust, unable to send data hash') | ||
raise ValueError('Server is not the datatrust, unable to send data hash') | ||
log.critical(constants.NOT_DATATRUST) | ||
raise ValueError(constants.NOT_DATATRUST) | ||
|
||
def wait_for_mining(self, tx): | ||
""" | ||
|
@@ -167,8 +173,35 @@ def create_file_hash(self, data): | |
""" | ||
sha3_hash = None | ||
with open(data, 'rb') as file_contents: | ||
file_contents.read() | ||
sha3_hash = self.w3.sha3(file_contents) | ||
b = file_contents.read(1024*1024) # read file in 1MB chunks | ||
while b: | ||
sha3_hash = self.w3.sha3(b) | ||
b = file_contents.read(1024*1024) | ||
return sha3_hash | ||
|
||
def validate_candidate(self, listing_hash): | ||
""" | ||
Verify a candidate has been submitted to Computable Protocol | ||
by parsing logs for 'CandidateAdded' event and matching listing hash | ||
Verify voteBy has not expired | ||
:params listing_hash: String listing hash for listing | ||
:return owner address if valid otherwise return None: | ||
:return type string: | ||
""" | ||
owner = None | ||
voting_filter = self.voting.deployed.eventFilter( | ||
constants.CANDIDATE_ADDED,{'fromBlock':0,'toBlock':'latest'} | ||
) | ||
events = voting_filter.get_all_entries() | ||
for evt in events: | ||
event_hash = '0x' + evt['args']['hash'].hex() | ||
print(f'Comparing event hash {event_hash} to listing hash {listing_hash}') | ||
if event_hash == listing_hash: | ||
log.info(f'Listing {listing_hash} has been listed as a candidate in protocol') | ||
voteBy = evt['args']['voteBy'] | ||
if voteBy > int(time.time()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is also already a depends on if we are gunna grep logs or not. for this i don't think we need to - can prob just get away with the 2 getters... we'll see |
||
owner = evt['args']['owner'] | ||
return owner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once we recieve the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one question this raises is should we implement twisted/celery/whatever to handle doing this in threads? |
||
|
||
|
||
deployed = Protocol() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can prob abstract out some
log_helpers
in datatrust