-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from crytic/57-export-jsonrpc
Export raw RPC calls to a JSON file
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,32 @@ | ||
import json | ||
from typing import TextIO, Union | ||
|
||
from .etheno import EthenoPlugin | ||
|
||
class JSONRPCExportPlugin(EthenoPlugin): | ||
def __init__(self, out_stream: Union[str, TextIO]): | ||
self._was_path = isinstance(out_stream, str) | ||
if self._was_path: | ||
self._output = open(out_stream, 'w', encoding='utf8') | ||
else: | ||
self._output = out_stream | ||
self._output.write('[') | ||
self._count = 0 | ||
|
||
def before_post(self, post_data): | ||
if self._count > 0: | ||
self._output.write(',') | ||
self._count += 1 | ||
self._output.write('\n') | ||
json.dump(post_data, self._output) | ||
self._output.flush() | ||
|
||
def finalize(self): | ||
if self._count: | ||
self._output.write('\n') | ||
self._output.write(']') | ||
self._output.flush() | ||
if self._was_path: | ||
self._output.close() | ||
if hasattr(self._output, 'name'): | ||
self.logger.info(f'Raw JSON RPC messages dumped to {self._output.name}') |