Skip to content

Commit

Permalink
Merge pull request #312 from xian-network/stamp_calc_payload
Browse files Browse the repository at this point in the history
Adjust 'stamp calculator' to be a 'tx simulator'
  • Loading branch information
Endogen authored Oct 24, 2024
2 parents ebded2f + 1f1c0d4 commit 0cde32b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/xian/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 17 additions & 2 deletions src/xian/methods/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,25 @@ async def query(self, req) -> ResponseQuery:
except:
result = {"stdout": "", "stderr": ""}

# http://localhost:26657/abci_query?path="/calculate_stamps/<encoded_tx>"
# http://localhost:26657/abci_query?path="/simulate_tx/<encoded_payload>"
elif path_parts[0] == "simulate_tx":
connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
connection.connect(c.SIMULATOR_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/<encoded_payload>"
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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))
Expand Down Expand Up @@ -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 = {}, 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
Expand All @@ -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'],
Expand All @@ -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()
Expand All @@ -127,14 +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,
executor=executor
)


if __name__ == '__main__':
sc = StampCalculator()
sc = Simulator()
sc.setup_socket()
sc.listen()

0 comments on commit 0cde32b

Please sign in to comment.