Skip to content

Commit

Permalink
More deserialization fixes & code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
guilledk committed Sep 5, 2024
1 parent ec73ad4 commit 176044b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 65 deletions.
64 changes: 63 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pdbpp = {version = '^0.10.3'}
pytest = {version = '^7.4.2'}
docker = {version = '^6.1.3'}
pytest-trio = {version = "^0.8.0"}
yappi = "^1.6.0"

[tool.poetry.group.snaps]
optional = true
Expand Down
8 changes: 7 additions & 1 deletion src/leap/protocol/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ def unpack_type(self, type_name: str):

if desc.variant:
i = self.unpack_uint8()
desc.struct = self.resolve_struct(desc.variant.types[i])
sub_name = self.resolve_alias(desc.variant.types[i])

if sub_name not in STANDARD_TYPES:
desc.struct = self.resolve_struct(sub_name)

else:
desc.strip_name = sub_name

# find right unpacking function
unpack_partial = None
Expand Down
4 changes: 3 additions & 1 deletion src/leap/protocol/ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ def unpack_signature(self):
data = data + ripmed160(data + b"K1")[:4]
return "SIG_K1_" + b58encode(data).decode("ascii")
elif t == 1:
raise Exception("not implemented")
data = self.read(65)
data = data + ripmed160(data + b"R1")[:4]
return "SIG_R1_" + b58encode(data).decode("ascii")
else:
raise Exception("invalid binary signature")

Expand Down
130 changes: 68 additions & 62 deletions src/leap/ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,68 @@ def decode_block_result(
decoded_deltas = []
decoded_actions = []

def maybe_decode_account_row(row):
if not row['present']:
return

account_delta = abi_unpack('account', row['data'])
abi_raw = account_delta['abi']
if len(abi_raw) > 0:
contracts[account_delta['name']] = abi_unpack(
'abi', abi_raw, spec=ABI)

def maybe_decode_contract_row(row):
contract_delta = abi_unpack('contract_row', row['data'])
table = contract_delta['table']
code = contract_delta['code']

relevant_tables = delta_whitelist.get(code, [])
if table not in relevant_tables:
return

contract_abi = contracts.get(code, None)
if not contract_abi:
raise SerializationException(f'abi for contract {code} not found')

try:
decoded_deltas.append(
abi_unpack(
table,
contract_delta['value'],
abi=contract_abi
)
)

except Exception as e:
e.add_note(f'while decoding table delta {code}::{contract_delta["table"]}')
raise e

def maybe_decode_tx_trace(tx_trace):
for act_trace in tx_trace['action_traces']:
action = act_trace['act']
account = action['account']
name = action['name']

if account == 'eosio' and name == 'onblock':
continue

try:
relevant_actions = action_whitelist.get(account, [])
if name not in relevant_actions:
continue

contract_abi = contracts.get(account, None)
if not contract_abi:
raise SerializationException(f'abi for contract {account} not found')

act_data = abi_unpack(name, action['data'], abi=contract_abi)
action['data'] = act_data
decoded_actions.append(action)

except Exception as e:
e.add_note(f'while decoding action trace {account}::{name}')
raise e

try:
if result['block']:
block = abi_unpack('signed_block', result['block'])
Expand All @@ -43,77 +105,21 @@ def decode_block_result(
rows = delta['rows']
match delta['name']:
case 'account':
for row in rows:
if row['present']:
account_delta = abi_unpack('account', row['data'])
abi_raw = account_delta['abi']
if len(abi_raw) > 0:
contracts[account_delta['name']] = abi_unpack(
'abi', abi_raw, spec=ABI)

[maybe_decode_account_row(row) for row in rows]
break

case 'contract_row':
if len(delta_whitelist) == 0:
continue

for row in rows:
contract_delta = abi_unpack('contract_row', row['data'])
table = contract_delta['table']
code = contract_delta['code']

relevant_tables = delta_whitelist.get(code, [])
if table not in relevant_tables:
continue

contract_abi = contracts.get(code, None)
if not contract_abi:
raise SerializationException(f'abi for contract {code} not found')

try:
decoded_deltas.append(
abi_unpack(
table,
contract_delta['value'],
abi=contract_abi
)
)

except Exception as e:
e.add_note(f'while decoding table delta {code}::{contract_delta["table"]}')
raise e

[maybe_decode_contract_row(row) for row in rows]
break

if result['traces'] and not len(action_whitelist) == 0:
tx_traces = abi_unpack(
'transaction_trace[]', result['traces'])

for tx_trace in tx_traces:
for act_trace in tx_trace['action_traces']:
action = act_trace['act']
account = action['account']
name = action['name']

if account == 'eosio' and name == 'onblock':
continue

try:
relevant_actions = action_whitelist.get(account, [])
if name not in relevant_actions:
continue

contract_abi = contracts.get(account, None)
if not contract_abi:
raise SerializationException(f'abi for contract {account} not found')

act_data = abi_unpack(name, action['data'], abi=contract_abi)
action['data'] = act_data
decoded_actions.append(action)

except Exception as e:
e.add_note(f'while decoding action trace {account}::{name}')
raise e
[maybe_decode_tx_trace(tx_trace) for tx_trace in tx_traces]

except Exception as e:
e.add_note(f'while decoding block {block_num}')
Expand Down Expand Up @@ -171,7 +177,7 @@ async def open_state_history(
await ws.send_message(get_blocks_msg)

# receive blocks & manage acks
acked_block = max_messages_in_flight + 1
acked_block = start_block_num
block_num = start_block_num - 1
while block_num != end_block_num:
# receive get_blocks_result
Expand All @@ -183,8 +189,8 @@ async def open_state_history(
)
block_num = block.block_num

deltas_num = len(block.deltas)
actions_num = len(block.actions)
# deltas_num = len(block.deltas)
# actions_num = len(block.actions)
# print(f'[{block_num}]: deltas: {deltas_num}, actions: {actions_num}')

yield block
Expand Down

0 comments on commit 176044b

Please sign in to comment.