From 0e3be98d24bffd3f98cc64d7ccfdb4d22ffdd62a Mon Sep 17 00:00:00 2001 From: Victor Grau Serrat Date: Wed, 7 Feb 2018 01:44:16 -0700 Subject: [PATCH] BLD: mmarketplace: switched encoding to Web3.toHex() --- catalyst/marketplace/marketplace.py | 61 ++++++++++++++----------- catalyst/marketplace/utils/eth_utils.py | 46 +++++++++---------- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/catalyst/marketplace/marketplace.py b/catalyst/marketplace/marketplace.py index 2eced9ee..99385171 100644 --- a/catalyst/marketplace/marketplace.py +++ b/catalyst/marketplace/marketplace.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import glob import json import os @@ -27,7 +29,7 @@ from catalyst.marketplace.marketplace_errors import ( from catalyst.marketplace.utils.auth_utils import get_key_secret, \ get_signed_headers from catalyst.marketplace.utils.bundle_utils import merge_bundles -from catalyst.marketplace.utils.eth_utils import bytes32, b32_str, bin_hex +from catalyst.marketplace.utils.eth_utils import bin_hex from catalyst.marketplace.utils.path_utils import get_bundle_folder, \ get_data_source_folder, get_marketplace_folder, \ get_user_pubaddr, get_temp_bundles_folder, extract_bundle @@ -142,14 +144,13 @@ class Marketplace: 'Gas Limit:\t\t{gas}\n' 'Nonce:\t\t\t{nonce}\n' 'Data:\t\t\t{data}\n'.format( - _from=from_address, - to=tx['to'], - value=tx['value'], - gas=tx['gas'], - nonce=tx['nonce'], - data=tx['data'], - ) - ) + _from=from_address, + to=tx['to'], + value=tx['value'], + gas=tx['gas'], + nonce=tx['nonce'], + data=tx['data'],) + ) signed_tx = input('Copy and Paste the "Signed Transaction" ' 'field here:\n') @@ -188,7 +189,7 @@ class Marketplace: if index > 0: data.append( dict( - dataset=data_source.decode('utf-8').rstrip('\0') + dataset=Web3.toText(data_source) ) ) @@ -197,12 +198,12 @@ class Marketplace: print(df) def subscribe(self, dataset): - # TODO: what happens if we are already subscribed? + dataset = dataset.lower() address = self.choose_pubaddr()[0] provider_info = self.mkt_contract.functions.getDataProviderInfo( - bytes32(dataset) + Web3.toHex(dataset) ).call() if not provider_info[4]: @@ -212,6 +213,15 @@ class Marketplace: price = provider_info[1] + subscribed = self.mkt_contract.functions.checkAddressSubscription( + address, Web3.toHex(dataset) + ).call() + + if subscribed[5]: + print('You are already subscribed to the "{}" dataset.\n' + 'Your subscription started on {}, and is valid until' + '{}'.format(dataset, subscribed[3], subscribed[4])) + print('\nThe price for a monthly subscription to this dataset is' ' {} ENG'.format(price)) @@ -228,10 +238,7 @@ class Marketplace: wallet_address ) }) - try: - balance = int(balance[2:], 16) // 10 ** 8 - except ValueError: - balance = int(bin_hex(balance), 16) // 10 ** 8 + balance = Web3.toInt(hexstr=balance) // 10 ** 8 if balance > price: print('OK.') @@ -247,7 +254,7 @@ class Marketplace: agree_pay = input('Please confirm that you agree to pay {} ENG ' 'for a monthly subscription to the dataset "{}" ' 'starting today. [default: Y] '.format( - price, dataset)) or 'y' + price, dataset)) or 'y' if agree_pay.lower() not in ('y', 'n'): print("Please answer Y or N.") else: @@ -302,14 +309,14 @@ class Marketplace: except AttributeError: pass for i in range(0, 10): - # print('.', end='', flush=True) + print('.', end='', flush=True) time.sleep(1) print('\nFirst transaction successful!\n' 'Now processing second transaction.') tx = self.mkt_contract.functions.subscribe( - bytes32(dataset), + Web3.toHex(dataset), ).buildTransaction( {'nonce': self.web3.eth.getTransactionCount(address)}) @@ -349,7 +356,7 @@ class Marketplace: except AttributeError: pass for i in range(0, 10): - # print('.', end='', flush=True) + print('.', end='', flush=True) time.sleep(1) print('\nSecond transaction successful!\n' @@ -358,7 +365,7 @@ class Marketplace: 'You can now ingest this dataset anytime during the ' 'next month by running the following command:\n' 'catalyst marketplace ingest --dataset={}'.format( - dataset, address, dataset)) + dataset, address, dataset)) def process_temp_bundle(self, ds_name, path): """ @@ -391,7 +398,7 @@ class Marketplace: ds_name = ds_name.lower() # TODO: catch error conditions provider_info = self.mkt_contract.functions.getDataProviderInfo( - bytes32(ds_name) + Web3.toHex(ds_name) ).call() if not provider_info[4]: @@ -402,10 +409,10 @@ class Marketplace: address, address_i = self.choose_pubaddr() fns = self.mkt_contract.functions check_sub = fns.checkAddressSubscription( - address, bytes32(ds_name) + address, Web3.toHex(ds_name) ).call() - if check_sub[0] != address or b32_str(check_sub[1]) != ds_name: + if check_sub[0] != address or Web3.toText(check_sub[1]) != ds_name: raise MarketplaceContractDataNoMatch( params='address: {}, dataset: {}'.format( address, ds_name @@ -541,7 +548,7 @@ class Marketplace: desc = input('Enter the name of the dataset to register: ') dataset = desc.lower() provider_info = self.mkt_contract.functions.getDataProviderInfo( - bytes32(dataset) + Web3.toHex(dataset) ).call() if provider_info[4]: @@ -598,7 +605,7 @@ class Marketplace: key, secret = get_key_secret(address) tx = self.mkt_contract.functions.register( - bytes32(dataset), + Web3.toHex(dataset), price, address, ).buildTransaction( @@ -631,7 +638,7 @@ class Marketplace: def publish(self, dataset, datadir, watch): dataset = dataset.lower() provider_info = self.mkt_contract.functions.getDataProviderInfo( - bytes32(dataset) + Web3.toHex(dataset) ).call() if not provider_info[4]: diff --git a/catalyst/marketplace/utils/eth_utils.py b/catalyst/marketplace/utils/eth_utils.py index f5fe8b7b..35e8b300 100644 --- a/catalyst/marketplace/utils/eth_utils.py +++ b/catalyst/marketplace/utils/eth_utils.py @@ -1,37 +1,37 @@ import binascii -def bytes32(string): - """ - Convert string to bytes32 data type for smart contract +# def bytes32(string): +# """ +# Convert string to bytes32 data type for smart contract - Parameters - ---------- - string: str +# Parameters +# ---------- +# string: str - Returns - ------- - list +# Returns +# ------- +# list - """ - return binascii.hexlify(string.encode('utf-8')) +# """ +# return binascii.hexlify(string.encode('utf-8')) -def b32_str(bytes32): - """ - Convert bytes32 to string +# def b32_str(bytes32): +# """ +# Convert bytes32 to string - Parameters - ---------- - input: bytes object +# Parameters +# ---------- +# input: bytes object - Returns - ------- - str +# Returns +# ------- +# str - """ - return binascii.unhexlify( - bytes32.decode('utf-8').rstrip('\0')).decode('ascii') +# """ +# return binascii.unhexlify( +# bytes32.decode('utf-8').rstrip('\0')).decode('ascii') def bin_hex(binary):