-
Notifications
You must be signed in to change notification settings - Fork 43
feat(nano): Improve Nano History HTTP API #1486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| # Copyright 2021 Hathor Labs | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import TYPE_CHECKING, Any, Optional | ||
|
|
||
| from hathor.transaction import BaseTransaction, Transaction | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hathor.nanocontracts.nc_exec_logs import NCLogStorage | ||
| from hathor.transaction.storage import TransactionStorage | ||
|
|
||
|
|
||
| class VertexJsonSerializer: | ||
| """Helper class for vertex/transaction serialization.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| storage: 'TransactionStorage', | ||
| nc_log_storage: Optional['NCLogStorage'] = None, | ||
| ) -> None: | ||
| self.tx_storage = storage | ||
| self.nc_log_storage = nc_log_storage | ||
|
|
||
| def to_json( | ||
| self, | ||
| tx: BaseTransaction, | ||
| decode_script: bool = False, | ||
| include_metadata: bool = False, | ||
| include_nc_logs: bool = False, | ||
| include_nc_events: bool = False, | ||
| ) -> dict[str, Any]: | ||
| """Serialize transaction to JSON.""" | ||
| # Get base JSON from transaction | ||
| data = tx.to_json(decode_script=decode_script, include_metadata=include_metadata) | ||
|
|
||
| # Add nano contract logs if requested | ||
| if include_nc_logs: | ||
| self._add_nc_logs_to_dict(tx, data) | ||
|
|
||
| # Add nano contract events if requested | ||
| if include_nc_events: | ||
| self._add_nc_events_to_dict(tx, data) | ||
|
|
||
| return data | ||
|
|
||
| def to_json_extended( | ||
| self, | ||
| tx: BaseTransaction, | ||
| include_nc_logs: bool = False, | ||
| include_nc_events: bool = False, | ||
| ) -> dict[str, Any]: | ||
| """Serialize transaction to extended JSON format.""" | ||
| data = tx.to_json_extended() | ||
|
|
||
| # Add nano contract logs if requested | ||
| if include_nc_logs: | ||
| self._add_nc_logs_to_dict(tx, data) | ||
|
|
||
| # Add nano contract events if requested | ||
| if include_nc_events: | ||
| self._add_nc_events_to_dict(tx, data) | ||
|
|
||
| # Add decoded arguments if applicable | ||
| self._add_nc_args_decoded(tx, data) | ||
|
|
||
| return data | ||
|
|
||
| def _add_nc_logs_to_dict(self, tx: BaseTransaction, data: dict[str, Any]) -> None: | ||
| """Add nano contract execution logs to the data dictionary.""" | ||
| if not tx.is_nano_contract(): | ||
| return | ||
|
|
||
| nc_logs: dict[str, Any] | None | ||
| if self.nc_log_storage is None: | ||
| nc_logs = {} | ||
| else: | ||
| nc_logs = self.nc_log_storage.get_json_logs(tx.hash) | ||
|
|
||
| data['nc_logs'] = nc_logs | ||
|
|
||
| def _add_nc_events_to_dict(self, tx: BaseTransaction, data: dict[str, Any]) -> None: | ||
| """Add nano contract events to the data dictionary.""" | ||
| if not tx.is_nano_contract(): | ||
| return | ||
|
|
||
| meta = tx.get_metadata() | ||
| if meta.nc_events is None: | ||
| nc_events = [] | ||
| else: | ||
| nc_events = [ | ||
| {'nc_id': nc_id.hex(), 'data': event_data.hex()} | ||
| for nc_id, event_data in meta.nc_events | ||
| ] | ||
|
|
||
| data['nc_events'] = nc_events | ||
|
|
||
| def _add_nc_args_decoded(self, tx: BaseTransaction, data: dict[str, Any]) -> None: | ||
| if not tx.is_nano_contract(): | ||
| return | ||
|
|
||
| assert isinstance(tx, Transaction) | ||
| nc_args_decoded = self.decode_nc_args(tx) | ||
| if nc_args_decoded is not None: | ||
| data['nc_args_decoded'] = nc_args_decoded | ||
|
|
||
| def decode_nc_args(self, tx: 'Transaction') -> Any: | ||
| """Decode nano contract arguments. | ||
|
|
||
| Returns a list of JSON-serialized argument strings, or None if decoding is not applicable. | ||
| """ | ||
| from hathor.nanocontracts.exception import NCFail | ||
| from hathor.nanocontracts.method import Method | ||
| from hathor.nanocontracts.types import BlueprintId | ||
|
|
||
| meta = tx.get_metadata() | ||
| nano_header = tx.get_nano_header() | ||
|
|
||
| if meta.nc_calls and len(meta.nc_calls) > 0: | ||
| # Get blueprint_id from the first nc_calls record | ||
| blueprint_id = BlueprintId(meta.nc_calls[0].blueprint_id) | ||
| else: | ||
| # Get blueprint_id from NanoHeader | ||
| blueprint_id = nano_header.get_blueprint_id(accept_failed_execution=True) | ||
|
|
||
| try: | ||
| blueprint_class = self.tx_storage.get_blueprint_class(blueprint_id) | ||
| except NCFail: | ||
| return None | ||
|
|
||
| method_callable = getattr(blueprint_class, nano_header.nc_method, None) | ||
| if method_callable is None: | ||
| return None | ||
|
|
||
| method = Method.from_callable(method_callable) | ||
|
|
||
| try: | ||
| args_tuple = method.deserialize_args_bytes(nano_header.nc_args_bytes) | ||
| except NCFail: | ||
| return None | ||
|
|
||
| return method.args._value_to_json(args_tuple) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a limit to the number of stored events?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be stored in metadata since it's completely useless for most full nodes, we could either store it in files just like the nc logs, or for quicker solution, just ignore events and require blueprint devs to write logs when they write events — that is, events would not be available though the APIs, only logs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can refactor this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also not problematic while OCB is restricted, so we should review for unreasonably large event generation. We can also improve it after removing it from metadata.