From be61bf7163aa88e697ebe2cf3b04a667e4b8449a Mon Sep 17 00:00:00 2001 From: Endogen Date: Wed, 23 Oct 2024 00:46:18 +0200 Subject: [PATCH 1/7] Adjust to accept just pure encoded payload without the need to sign the tx --- src/xian/services/stamp_calculator.py | 31 ++++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/xian/services/stamp_calculator.py b/src/xian/services/stamp_calculator.py index ae7885a..1b0a9ea 100644 --- a/src/xian/services/stamp_calculator.py +++ b/src/xian/services/stamp_calculator.py @@ -42,7 +42,7 @@ def listen(self): if not raw_msglen: break msglen = struct.unpack('>I', raw_msglen)[0] - + # Read the message data data = connection.recv(msglen) if not data: @@ -51,11 +51,11 @@ def listen(self): break print(f"Received: {data.decode()}") - - tx = data.decode() - tx = json.loads(tx) + + payload = data.decode() + payload = json.loads(payload) try: - response = self.execute(tx) + response = self.execute(payload) response = json.dumps(response) response = response.encode() message_length = struct.pack('>I', len(response)) @@ -87,16 +87,16 @@ def generate_random_hex_string(self, length=64): # Generate a random number with `length//2` bytes and convert to hex return secrets.token_hex(nbytes=length // 2) - def execute_tx(self, transaction, stamp_cost, environment: dict = {}, driver = None, executor = None): - + def execute_tx(self, payload, stamp_cost, environment: dict = {}, driver=None, executor=None): + balance = 9999999 output = executor.execute( - sender=transaction['payload']['sender'], - contract_name=transaction['payload']['contract'], - function_name=transaction['payload']['function'], + sender=payload['sender'], + contract_name=payload['contract'], + function_name=payload['function'], stamps=balance * stamp_cost, stamp_cost=stamp_cost, - kwargs=convert_dict(transaction['payload']['kwargs']), + kwargs=convert_dict(payload['kwargs']), environment=environment, auto_commit=False, metering=True @@ -107,7 +107,7 @@ def execute_tx(self, transaction, stamp_cost, environment: dict = {}, driver = N writes = [{'key': k, 'value': v} for k, v in output['writes'].items()] tx_output = { - 'transaction': transaction, + 'payload': payload, 'status': output['status_code'], 'state': writes, 'stamps_used': output['stamps_used'], @@ -118,7 +118,7 @@ def execute_tx(self, transaction, stamp_cost, environment: dict = {}, driver = N return tx_output - def execute(self, transaction): + def execute(self, payload): driver = Driver(storage_home=self.constants.STORAGE_HOME) executor = Executor(metering=False, bypass_balance_amount=True, bypass_cache=True, driver=driver) environment = self.generate_environment() @@ -127,13 +127,14 @@ def execute(self, transaction): except: stamp_cost = 20 return self.execute_tx( - transaction=transaction, + payload=payload, environment=environment, stamp_cost=stamp_cost, - driver= driver, + driver=driver, executor=executor ) + if __name__ == '__main__': sc = StampCalculator() sc.setup_socket() From 9daaef14fc93cb86b44483bfb6a10425ffc05812 Mon Sep 17 00:00:00 2001 From: Endogen Date: Wed, 23 Oct 2024 01:18:20 +0200 Subject: [PATCH 2/7] Adjust description --- src/xian/methods/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xian/methods/query.py b/src/xian/methods/query.py index 5881871..8b7bacf 100644 --- a/src/xian/methods/query.py +++ b/src/xian/methods/query.py @@ -157,7 +157,7 @@ async def query(self, req) -> ResponseQuery: except: result = {"stdout": "", "stderr": ""} - # http://localhost:26657/abci_query?path="/calculate_stamps/" + # http://localhost:26657/abci_query?path="/calculate_stamps/" elif path_parts[0] == "calculate_stamps": connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) connection.connect(c.STAMPESTIMATOR_SOCKET) From 19a0bde7c826443060b626da9cc028f2f5985da8 Mon Sep 17 00:00:00 2001 From: Endogen Date: Thu, 24 Oct 2024 01:56:16 +0200 Subject: [PATCH 3/7] Added new 'simulate_tx' endpoint Added TODO with deprecation message for 'calculate_stamps' --- src/xian/methods/query.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/xian/methods/query.py b/src/xian/methods/query.py index 8b7bacf..cbedb9f 100644 --- a/src/xian/methods/query.py +++ b/src/xian/methods/query.py @@ -157,6 +157,21 @@ async def query(self, req) -> ResponseQuery: except: result = {"stdout": "", "stderr": ""} + # http://localhost:26657/abci_query?path="/simulate_tx/" + elif path_parts[0] == "simulate_tx": + connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + connection.connect(c.STAMPESTIMATOR_SOCKET) + + raw_tx = path_parts[1] + byte_data = bytes.fromhex(raw_tx) + message_length = struct.pack('>I', len(byte_data)) + connection.sendall(message_length + byte_data) + recv_length = connection.recv(4) + length = struct.unpack('>I', recv_length)[0] + recv = connection.recv(length) + result = recv.decode() + + # TODO: Deprecated - Remove after wallet and tools are reworked to use 'simulate_tx' # http://localhost:26657/abci_query?path="/calculate_stamps/" elif path_parts[0] == "calculate_stamps": connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) From 311135191f11a1b93251088e7498f76c61c2f122 Mon Sep 17 00:00:00 2001 From: Endogen Date: Thu, 24 Oct 2024 02:07:48 +0200 Subject: [PATCH 4/7] Renamed to 'simulator' --- src/xian/constants.py | 2 +- src/xian/methods/query.py | 4 ++-- .../services/{stamp_calculator.py => simulator.py} | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) rename src/xian/services/{stamp_calculator.py => simulator.py} (94%) diff --git a/src/xian/constants.py b/src/xian/constants.py index e58ba5c..a2a168c 100644 --- a/src/xian/constants.py +++ b/src/xian/constants.py @@ -5,7 +5,7 @@ class Constants: COMETBFT_CONFIG = COMETBFT_HOME / Path("config/config.toml") COMETBFT_GENESIS = COMETBFT_HOME / Path("config/genesis.json") STORAGE_HOME = COMETBFT_HOME / Path('xian/') - STAMPESTIMATOR_SOCKET = '/tmp/stampestimator.sock' + SIMULATOR_SOCKET = '/tmp/simulator.sock' NONCE_FILENAME = '__n' PENDING_NONCE_FILENAME = '__pn' diff --git a/src/xian/methods/query.py b/src/xian/methods/query.py index cbedb9f..a712ad1 100644 --- a/src/xian/methods/query.py +++ b/src/xian/methods/query.py @@ -160,7 +160,7 @@ async def query(self, req) -> ResponseQuery: # http://localhost:26657/abci_query?path="/simulate_tx/" elif path_parts[0] == "simulate_tx": connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - connection.connect(c.STAMPESTIMATOR_SOCKET) + connection.connect(c.SIMULATOR_SOCKET) raw_tx = path_parts[1] byte_data = bytes.fromhex(raw_tx) @@ -175,7 +175,7 @@ async def query(self, req) -> ResponseQuery: # http://localhost:26657/abci_query?path="/calculate_stamps/" elif path_parts[0] == "calculate_stamps": connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - connection.connect(c.STAMPESTIMATOR_SOCKET) + connection.connect(c.SIMULATOR_SOCKET) raw_tx = path_parts[1] byte_data = bytes.fromhex(raw_tx) diff --git a/src/xian/services/stamp_calculator.py b/src/xian/services/simulator.py similarity index 94% rename from src/xian/services/stamp_calculator.py rename to src/xian/services/simulator.py index 1b0a9ea..ae3568c 100644 --- a/src/xian/services/stamp_calculator.py +++ b/src/xian/services/simulator.py @@ -13,19 +13,19 @@ import struct -class StampCalculator: +class Simulator: def __init__(self): self.constants = Constants() def setup_socket(self): # If the socket file exists, remove it - STAMPESTIMATOR_SOCKET = pathlib.Path(self.constants.STAMPESTIMATOR_SOCKET) - if STAMPESTIMATOR_SOCKET.exists(): - STAMPESTIMATOR_SOCKET.unlink() + simulator_socket = pathlib.Path(self.constants.SIMULATOR_SOCKET) + if simulator_socket.exists(): + simulator_socket.unlink() # Create a socket self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - self.socket.bind(self.constants.STAMPESTIMATOR_SOCKET) + self.socket.bind(self.constants.SIMULATOR_SOCKET) self.socket.listen(1) def listen(self): @@ -136,6 +136,6 @@ def execute(self, payload): if __name__ == '__main__': - sc = StampCalculator() + sc = Simulator() sc.setup_socket() sc.listen() From c072b4955ed30c14885ee0c42d1eef860f7bd195 Mon Sep 17 00:00:00 2001 From: Endogen Date: Thu, 24 Oct 2024 02:08:11 +0200 Subject: [PATCH 5/7] Removed unused parameter --- src/xian/services/simulator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xian/services/simulator.py b/src/xian/services/simulator.py index ae3568c..28aeba9 100644 --- a/src/xian/services/simulator.py +++ b/src/xian/services/simulator.py @@ -87,7 +87,7 @@ def generate_random_hex_string(self, length=64): # Generate a random number with `length//2` bytes and convert to hex return secrets.token_hex(nbytes=length // 2) - def execute_tx(self, payload, stamp_cost, environment: dict = {}, driver=None, executor=None): + def execute_tx(self, payload, stamp_cost, environment: dict = {}, executor=None): balance = 9999999 output = executor.execute( From 329ca4eb90ff874619f80af3e95478cc060f2c64 Mon Sep 17 00:00:00 2001 From: Endogen Date: Thu, 24 Oct 2024 02:10:00 +0200 Subject: [PATCH 6/7] Renamed to 'simulator' --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index fd64f1e..686d181 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ up: pm2 start "../cometbft node --rpc.laddr tcp://0.0.0.0:26657" --name cometbft -f up-bds: - cd ./src/xian/services/ && pm2 start stamp_calculator.py --name stamp_calculator -f --wait-ready + cd ./src/xian/services/ && pm2 start simulator.py --name simulator -f --wait-ready cd ./src/xian && pm2 start xian_abci.py --name xian -f pm2 start "../cometbft node --rpc.laddr tcp://0.0.0.0:26657" --name cometbft -f From 1f1c0d4af13871682d274cb2b31c960ab017af92 Mon Sep 17 00:00:00 2001 From: Endogen Date: Thu, 24 Oct 2024 02:21:33 +0200 Subject: [PATCH 7/7] Removed unexpected argument --- src/xian/services/simulator.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xian/services/simulator.py b/src/xian/services/simulator.py index 28aeba9..92f639f 100644 --- a/src/xian/services/simulator.py +++ b/src/xian/services/simulator.py @@ -130,7 +130,6 @@ def execute(self, payload): payload=payload, environment=environment, stamp_cost=stamp_cost, - driver=driver, executor=executor )