diff --git a/web3-py-impl/Publisher.sol b/web3-py-impl/Publisher.sol deleted file mode 100644 index 775fd41..0000000 --- a/web3-py-impl/Publisher.sol +++ /dev/null @@ -1,97 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - - - -interface InterfacePubSubService { - function register() external returns(InterfaceEventManager); -} - - -interface InterfaceEventManager { - function addSubscriber(address publisher_addr, uint subscribeContractAddr) external payable; - function notify(bytes memory data) external; -} - -contract Publisher { - enum Action { ADD_TO_BLACKLIST, DELETE_FROM_BLACKLIST } // Types of actions - - struct BlackList { - address[] memberList; // keep list of blacklist addresses - mapping(address => bool) members; // easy lookup into in memberList (publisher => bool) - } - - bool public registeredToPubSub = false; - address public PubSubAddress; - address public eventManagerAddress; - InterfacePubSubService pubService; - - BlackList officialBL; // getting weird errors, so the key is eventManagerAddress - - - // mapping(address => eventManager) eventManagers; // Keep track of eventManagers (eventManagerAddr => eventManager) - // address[] eventManagersList; // List of eventManagers - - function registerToPubSubService(address pubSubAddr) payable external returns(address) { - - require(registeredToPubSub == false, "Publisher already registered to PubSubService"); // Assumes publisher can only register to a single pubsub service - pubService = InterfacePubSubService(pubSubAddr); - eventManagerAddress = address(pubService.register()); // TODO: Specify what kind of event types. e.g., - registeredToPubSub = true; - PubSubAddress = pubSubAddr; - return eventManagerAddress; - } - - - function addToBlackList(address user) public { - require(registeredToPubSub == true, "Publisher not registered yet"); - require(officialBL.members[user] == false, "User is already in blacklist"); - officialBL.memberList.push(user); - officialBL.members[user] = true; - InterfaceEventManager(eventManagerAddress).notify(encodeAction(Action.ADD_TO_BLACKLIST, user)); - } - - function viewBlackList() public view returns(address[] memory) { - return officialBL.memberList; - } - - - function removeFromBlackList(address member) public { - require(registeredToPubSub == true, "Publisher not registered yet"); - require(officialBL.members[member] == true, "User is not on blacklist"); - remove(officialBL.memberList, member); - officialBL.members[member] = false; - InterfaceEventManager(eventManagerAddress).notify(encodeAction(Action.DELETE_FROM_BLACKLIST, member)); - } - - - // helper function to remove element from array - function remove(address[] storage publisherList, address publisher) private { - require(publisherList.length > 0, "Received empty publisher list"); - for (uint i = 0; i < publisherList.length; i++){ - if (publisherList[i] == publisher) { - publisherList[i] = publisherList[publisherList.length-1]; // remove element by overwriting previous index and removing the last index - publisherList.pop(); - } - } - officialBL.members[publisher] = false; - } - - function getContractBalance() external view returns (uint256) { - return address(this).balance; - } - - function encodeAction(Action action, address user) internal pure returns (bytes memory) { - bytes memory data = abi.encode(action, user); - return data; - } - - - // Receive function - called when ether is sent directly to the contract address - receive() external payable { - // payable(owen_wallet).transfer(msg.value); - } - - // // Fallback function - called when no other function matches the function signature - // fallback() external payable {} -} diff --git a/web3-py-impl/deploy_contracts.py b/web3-py-impl/deploy_contracts.py deleted file mode 100644 index 191ada0..0000000 --- a/web3-py-impl/deploy_contracts.py +++ /dev/null @@ -1,186 +0,0 @@ -# import sys -# import time -# import pprint -import os -# import subprocess -# from subprocess import PIPE, Popen -# import glob -import json - -import web3.logs -# -# import solcx -from web3.providers import HTTPProvider -from web3 import Web3 -import matplotlib.pyplot as plt -import numpy as np - -import importlib.util -file_path = '/Users/royshadmon/Web3-Ethereum-Interface/src/web3_eth.py' - -# Specify the module name -module_name = 'Web3_Eth' - -# Load the module from the file path -spec = importlib.util.spec_from_file_location(module_name, file_path) -module = importlib.util.module_from_spec(spec) -spec.loader.exec_module(module) - - -def load_contract_abi_bin(root_directory, contract_name, solidity_version="0.8.17"): - with open( - os.path.join(root_directory, f"contracts/compiled_contracts/{solidity_version}/{contract_name}/{contract_name}.abi")) as f: - - abi = f.read() - with open( - os.path.join(root_directory, f"contracts/compiled_contracts/{solidity_version}/{contract_name}/{contract_name}.bin")) as f: - - bin = f.read() - return abi, bin - - -def compile_contract(web3_interface, root_dir, contract_name, output_dir=None, solidity_version="0.8.17"): - contract_path = os.path.join(root_dir, f"contracts/{contract_name}") - contract_name = contract_name.split('.')[0] - if not output_dir: - output_dir = os.path.join(root_dir, f"contracts/compiled_contracts/{solidity_version}/{contract_name}") - - id, interface = web3_interface.compile_contract(contract_path, output_dir, solidity_version) - return id, interface - -# self.connection.eth.contract(address=contract_address, abi=abi_) -def get_contract_instance(web3_interface, contract_addr, abi): - - contract_instance = web3_interface.w3.eth.contract(address=web3_interface.toCheckSumAddress(contract_addr), abi=abi) - return contract_instance - - -def deploy_contract(web3_interface, contract_name, account_addr, private_key, solidity_version="0.8.17"): - abi, bin = load_contract_abi_bin(contract_name, solidity_version) - if web3_interface.verify_acct_addr_matches_p_key(account_addr, private_key): - contract_address = web3_interface.deploy_contract(abi, bin, private_key, account_addr) - else: - print(f"Account address does not match private key") - exit(-1) - return contract_address - -def submit_transaction(web3_interface, transaction, private_key): - signed_tx = web3_interface.signTransaction(transaction, private_key=private_key) - tx_hash = web3_interface.sendRawTransaction(signed_tx) - tx_receipt = web3_interface.waitForTransactionReceipt(tx_hash) - return tx_receipt - - -def main(): - # Get the directory where the current Python script is located - script_directory = os.path.dirname(os.path.abspath(__file__)) - # Get the root directory of the project (assuming the root directory is the parent directory of the script directory) - root_directory = os.path.dirname(script_directory) - ganache_url = "http://127.0.0.1:8545" - web3_interface = module.Web3_Eth(ganache_url) - - # acct_address = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1" - # user_private_key = "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d" - - # Get Private Keys - with open(os.path.join(root_directory, "eth_accounts/keys.json")) as f: - private_keys = f.read() - private_keys = json.loads(private_keys)['private_keys'] - - private_keys_list = list(private_keys.values()) - - user_private_key = private_keys_list[0] - acct_address = web3_interface.get_eth_user(user_private_key).address - - - print(web3_interface.w3.is_connected()) - - # # Compile Contracts - publisher_id, publisher_interface = compile_contract(web3_interface, root_directory, "Publisher.sol") - id, interface = compile_contract(web3_interface, root_directory, "PubSubService.sol") - subscriber_id, subscriber_interface = compile_contract(web3_interface, root_directory, "Subscriber.sol") - - notify_add_cost = [] - notify_remove_cost = [] - - for i in range(1, len(private_keys_list)+1): - publisher_addr, publisher_contract = web3_interface.deploy_contract(publisher_interface, user_private_key) - print(f"PUBLISHER ADDRESS {web3_interface.toCheckSumAddress(publisher_addr)}") - - pubsub_addr, pubsub_contract = web3_interface.deploy_contract(interface, user_private_key) - - # Publisher register to PubSubService - transaction = publisher_contract.functions.registerToPubSubService(pubsub_addr).build_transaction( - { - 'from': acct_address, - 'chainId': 1337, # Ganache chain id - 'nonce': web3_interface.w3.eth.get_transaction_count(acct_address), - # 'value': 0, - } - ) - tx_receipt = submit_transaction(web3_interface, transaction, private_key=user_private_key) - - # Get event manager contract address - em_addr = publisher_contract.events.EM_CREATED().process_receipt(tx_receipt)[0].args.em_addr - print(f"EVENT MANAGER {em_addr}") - - # Deploy Subscribers - for p_key in private_keys_list[:i]: - user = web3_interface.get_eth_user(p_key) - subscriber_addr, subscriber_contract = web3_interface.deploy_contract(subscriber_interface, p_key, publisher_addr, pubsub_addr, 100000010101, value=100000010101) - print(f'SUBSCRIBER_ADDR {subscriber_addr}') - - - # Add to blacklist - - - user = web3_interface.get_eth_user(p_key) - print(f'ADDING {web3_interface.toCheckSumAddress(user.address)}') - transaction = publisher_contract.functions.addToBlackList(web3_interface.toCheckSumAddress(user.address)).build_transaction({ - 'from': web3_interface.toCheckSumAddress(user.address), - 'chainId': 1337, # Ganache chain id - 'nonce': web3_interface.getTransactionCount(user), - }) - tx_receipt = submit_transaction(web3_interface, transaction, p_key) - print(f"TX_RECEIPT") - print(f"=======================") - notify_add_cost.append(publisher_contract.events.GAS_COST().process_receipt(tx_receipt, errors=web3.logs.DISCARD)[0].args.gas) # Discarding warnings due to "MismatchedABI(The event signature did not match the provided ABI)". Reason from https://github.com/oceanprotocol/ocean.py/issues/348 - - - # Remove single element from blacklist - - print(f'REMOVING {web3_interface.toCheckSumAddress(user.address)}') - - - transaction = publisher_contract.functions.removeFromBlackList(web3_interface.toCheckSumAddress(user.address)).build_transaction({ - 'from': web3_interface.toCheckSumAddress(user.address), - 'chainId': 1337, # Ganache chain id - 'nonce': web3_interface.getTransactionCount(user), - }) - tx_receipt = submit_transaction(web3_interface, transaction, p_key) - print(f"TX_RECEIPT") - print(f"=======================") - notify_remove_cost.append(publisher_contract.events.GAS_COST().process_receipt(tx_receipt, errors=web3.logs.DISCARD)[0].args.gas) - - - scale_by = 6 - scale = np.power(10, scale_by) - plt.plot(np.arange(1, len(notify_add_cost) + 1), np.array(notify_add_cost)/scale) - plt.xticks(np.arange(1,len(notify_add_cost)+1, 1)) - plt.title(f"Gas cost adding to blacklist") - plt.xlabel("Number of subscribing contracts") - plt.ylabel(f"Gas cost (1e{scale_by})") - plt.show() - print(notify_add_cost) - - plt.plot(np.arange(1, len(notify_remove_cost) + 1), np.array(notify_remove_cost)/scale) - plt.xticks(np.arange(1, len(notify_remove_cost) + 1, 1)) - plt.title(f"Gas cost removing from blacklist starting with {len(notify_remove_cost)} items") - plt.xlabel("Number of subscribing contracts") - plt.ylabel(f"Gas cost (1e{scale_by})") - plt.show() - exit(-1) - - -if __name__ == "__main__": - main() \ No newline at end of file