Skip to content

Commit

Permalink
Revert "Adjust 'stamp calculator' to be a 'tx simulator'" (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
duelingbenjos authored Nov 7, 2024
1 parent 5814878 commit 448c382
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 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 simulator.py --name simulator -f --wait-ready
cd ./src/xian/services/ && pm2 start stamp_calculator.py --name stamp_calculator -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/')
SIMULATOR_SOCKET = '/tmp/simulator.sock'
STAMPESTIMATOR_SOCKET = '/tmp/stampestimator.sock'

NONCE_FILENAME = '__n'
PENDING_NONCE_FILENAME = '__pn'
Expand Down
19 changes: 2 additions & 17 deletions src/xian/methods/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,10 @@ async def query(self, req) -> ResponseQuery:
except:
result = {"stdout": "", "stderr": ""}

# 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>"
# http://localhost:26657/abci_query?path="/calculate_stamps/<encoded_tx>"
elif path_parts[0] == "calculate_stamps":
connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
connection.connect(c.SIMULATOR_SOCKET)
connection.connect(c.STAMPESTIMATOR_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 Simulator:
class StampCalculator:
def __init__(self):
self.constants = Constants()

def setup_socket(self):
# If the socket file exists, remove it
simulator_socket = pathlib.Path(self.constants.SIMULATOR_SOCKET)
if simulator_socket.exists():
simulator_socket.unlink()
STAMPESTIMATOR_SOCKET = pathlib.Path(self.constants.STAMPESTIMATOR_SOCKET)
if STAMPESTIMATOR_SOCKET.exists():
STAMPESTIMATOR_SOCKET.unlink()

# Create a socket
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.bind(self.constants.SIMULATOR_SOCKET)
self.socket.bind(self.constants.STAMPESTIMATOR_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()}")

payload = data.decode()
payload = json.loads(payload)
tx = data.decode()
tx = json.loads(tx)
try:
response = self.execute(payload)
response = self.execute(tx)
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, payload, stamp_cost, environment: dict = {}, executor=None):

def execute_tx(self, transaction, stamp_cost, environment: dict = {}, driver = None, executor = None):
balance = 9999999
output = executor.execute(
sender=payload['sender'],
contract_name=payload['contract'],
function_name=payload['function'],
sender=transaction['payload']['sender'],
contract_name=transaction['payload']['contract'],
function_name=transaction['payload']['function'],
stamps=balance * stamp_cost,
stamp_cost=stamp_cost,
kwargs=convert_dict(payload['kwargs']),
kwargs=convert_dict(transaction['payload']['kwargs']),
environment=environment,
auto_commit=False,
metering=True
Expand All @@ -107,7 +107,7 @@ def execute_tx(self, payload, stamp_cost, environment: dict = {}, executor=None)
writes = [{'key': k, 'value': v} for k, v in output['writes'].items()]

tx_output = {
'payload': payload,
'transaction': transaction,
'status': output['status_code'],
'state': writes,
'stamps_used': output['stamps_used'],
Expand All @@ -118,7 +118,7 @@ def execute_tx(self, payload, stamp_cost, environment: dict = {}, executor=None)

return tx_output

def execute(self, payload):
def execute(self, transaction):
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, payload):
except:
stamp_cost = 20
return self.execute_tx(
payload=payload,
transaction=transaction,
environment=environment,
stamp_cost=stamp_cost,
driver= driver,
executor=executor
)


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

0 comments on commit 448c382

Please sign in to comment.