Skip to content

Commit

Permalink
Merge pull request #158 from AlexV525/chore/readable-validate-message
Browse files Browse the repository at this point in the history
🚸 Produce readable `AtomicalsValidationError`
  • Loading branch information
atomicals authored Apr 25, 2024
2 parents a90c3a3 + 6802140 commit 3b7212a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- 'master'
- 'develop'
paths-ignore:
- '**/*.md'
- '**/*.rst'
Expand Down
36 changes: 36 additions & 0 deletions electrumx/lib/util_atomicals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,42 @@ def auto_encode_bytes_elements(state):
return state


# Auto-detect any bytes keys and values and encoded it.
def auto_encode_bytes_items(state):
if isinstance(state, bytes):
return {
'$b': state.hex(),
'$len': sys.getsizeof(state),
'$auto': True
}

if isinstance(state, CBORTag):
dumped_bytes = dumps(state)
return auto_encode_bytes_elements(dumped_bytes)

if isinstance(state, list):
reformatted_list = []
for item in state:
reformatted_list.append(auto_encode_bytes_elements(item))
return reformatted_list

cloned_state = {}
try:
if isinstance(state, dict):
items = state.items()
else:
items = state.__dict__.items()
except AttributeError:
return state
for key, value in items:
if isinstance(key, bytes):
cloned_state[key.hex()] = auto_encode_bytes_items(value)
else:
cloned_state[auto_encode_bytes_items(key)] = auto_encode_bytes_items(value)

return cloned_state


# Base atomical commit to reveal delay allowed
def is_within_acceptable_blocks_for_general_reveal(commit_height, reveal_location_height):
return commit_height >= reveal_location_height - MINT_GENERAL_COMMIT_REVEAL_DELAY_BLOCKS
Expand Down
16 changes: 13 additions & 3 deletions electrumx/server/block_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
is_event_operation,
encode_atomical_ids_hex,
is_mint_pow_valid,
is_txid_valid_for_perpetual_bitwork
is_txid_valid_for_perpetual_bitwork,
auto_encode_bytes_items
)

from electrumx.lib.atomicals_blueprint_builder import AtomicalsTransferBlueprintBuilder
Expand Down Expand Up @@ -546,8 +547,17 @@ def validate_ft_rules_raw_tx(self, raw_tx):
# Log that there were tokens burned due to not being cleanly assigned
if blueprint_builder.get_are_fts_burned() or not blueprint_builder.cleanly_assigned:
encoded_atomicals_spent_at_inputs = encode_atomical_ids_hex(atomicals_spent_at_inputs)
encoded_ft_output_blueprint = encode_atomical_ids_hex(ft_output_blueprint)
raise AtomicalsValidationError(f'detected invalid ft token inputs and outputs for tx_hash={hash_to_hex_str(tx_hash)}, operations_found_at_inputs={operations_found_at_inputs}, atomicals_spent_at_inputs={encoded_atomicals_spent_at_inputs}, ft_output_blueprint.outputs={encoded_ft_output_blueprint.outputs} ft_output_blueprint.fts_burned={encoded_ft_output_blueprint.fts_burned}')
encoded_ft_output_blueprint = auto_encode_bytes_items(encode_atomical_ids_hex(ft_output_blueprint))
outputs = encoded_ft_output_blueprint['outputs']
fts_burned = encoded_ft_output_blueprint['fts_burned']
raise AtomicalsValidationError(
f'Invalid FT token inputs/outputs:\n'
f'tx_hash={hash_to_hex_str(tx_hash)}\n'
f'operations_found_at_inputs={operations_found_at_inputs}\n'
f'atomicals_spent_at_inputs={encoded_atomicals_spent_at_inputs}\n'
f'ft_output_blueprint.outputs={outputs}\n'
f'ft_output_blueprint.fts_burned={fts_burned}'
)

# Query general data including the cache
def get_general_data_with_cache(self, key):
Expand Down

0 comments on commit 3b7212a

Please sign in to comment.