commit 2d0582e8adaee34243d2dc1952aaab2b24f77c34 Author: snsd0805 Date: Sat May 11 20:46:38 2024 +0800 feat: scheduler contract's caller for testing diff --git a/main.py b/main.py new file mode 100644 index 0000000..e7ac84f --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +import json +from web3 import Web3 +from src.scheduler import Scheduler + +SCHEDULER_ADDR = "0x13D69Cf7d6CE4218F646B759Dcf334D82c023d8e" +SCHEDULER_ABI_FILE = "../gpu-contract/abi/Scheduler.abi" + +PROVIDER1_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" +PROVIDER2_KEY = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d" +PROVIDER3_KEY = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a" + +CLIENT_KEY = "0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6" + +w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545')) + + +if __name__ == '__main__': + if w3.is_connected(): + scheduler = Scheduler(w3, SCHEDULER_ADDR, SCHEDULER_ABI_FILE) + + provider1 = w3.eth.account.from_key(PROVIDER1_KEY) + provider2 = w3.eth.account.from_key(PROVIDER2_KEY) + provider3 = w3.eth.account.from_key(PROVIDER3_KEY) + client = w3.eth.account.from_key(CLIENT_KEY) + + print(scheduler.getClusters()) + + else: + print("cannot connected to the chain") + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2603854 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +web3==6.18.0 diff --git a/src/__pycache__/scheduler.cpython-310.pyc b/src/__pycache__/scheduler.cpython-310.pyc new file mode 100644 index 0000000..6ad8881 Binary files /dev/null and b/src/__pycache__/scheduler.cpython-310.pyc differ diff --git a/src/__pycache__/utils.cpython-310.pyc b/src/__pycache__/utils.cpython-310.pyc new file mode 100644 index 0000000..6162ae5 Binary files /dev/null and b/src/__pycache__/utils.cpython-310.pyc differ diff --git a/src/scheduler.py b/src/scheduler.py new file mode 100644 index 0000000..7c385c2 --- /dev/null +++ b/src/scheduler.py @@ -0,0 +1,29 @@ +from web3 import Web3 +from src.utils import load_abi + +class Scheduler(): + def __init__(self, w3: Web3, address: str, abi_file: str): + self.w3 = w3 + abi = load_abi(abi_file) + self.contract = self.w3.eth.contract(address=address, abi=abi) + + def registerCluster(self, account, gpu_id: int, gpu_num: int): + tx = self.contract.functions.registerCluster(gpu_id, gpu_num).build_transaction({ + 'from': account.address, + 'nonce': self.w3.eth.get_transaction_count(account.address) + }) + signed_tx = self.w3.eth.account.sign_transaction(tx, private_key=account.key) + + tx_hash = self.w3.eth.send_raw_transaction(signed_tx.rawTransaction) + tx_receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash) + print(tx_receipt) + _hash = tx_receipt['transactionHash'] + print(self.w3.eth.get_transaction(_hash)) + + def getClusters(self): + return self.contract.functions.getClusters().call() + + def getStartRunEvent(self): + event_filter = self.contract.events.StartRun.create_filter(fromBlock=0) + events = event_filter.get_new_entries() + print(events) diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..f1e56df --- /dev/null +++ b/src/utils.py @@ -0,0 +1,4 @@ +def load_abi(file): + with open(file) as fp: + abi = fp.read() + return abi