-
Notifications
You must be signed in to change notification settings - Fork 12
[DO NOT SUBMIT] debug: add get_unstable_blocks #386
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
Draft
maksymar
wants to merge
15
commits into
master
Choose a base branch
from
maksym/debug-unstable-blocks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3832b5
add get_unstable_blocks
maksymar 0c683c9
MAX_TESTNET_UNSTABLE_DEPTH_DIFFERENCE
maksymar e642854
.
maksymar 78e968e
fix block data, add visualisation
maksymar 1c861f4
.
maksymar c9ec5ae
fix numbers with _
maksymar d2d8fc1
add sh file
maksymar 4fc18f2
.
maksymar 57aa59d
.
maksymar 87d4d65
vertical
maksymar 0cbe758
.
maksymar 686acb9
prefix date-time file name
maksymar 035b877
widen graph image
maksymar 264dfba
add no_difficulty_counter
maksymar be1f596
dpi 300
maksymar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,5 @@ chunk_hashes.txt | |
|
|
||
| *.log | ||
| *.csv | ||
|
|
||
| unstable_blocks/* | ||
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
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,16 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| # Generate date-time prefix: e.g. 20250326_103012 | ||
| PREFIX=$(date +"%Y%m%d_%H%M%S") | ||
| BASE="./unstable_blocks/${PREFIX}_output" | ||
|
|
||
| echo "Fetching unstable blocks to ${BASE}.txt ..." | ||
| dfx canister call --network testnet bitcoin_t get_unstable_blocks > "${BASE}.txt" | ||
|
|
||
| echo "Parsing ${BASE}.txt to ${BASE}.json ..." | ||
| ./unstable_blocks.py "${BASE}.txt" "${BASE}.json" | ||
|
|
||
| echo "Generating blockchain graph to ${BASE}.png ..." | ||
| ./visualize_blockchain_graphviz.py "${BASE}.json" "${BASE}.png" |
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,59 @@ | ||
| #!/usr/bin/env python3 | ||
| import re | ||
| import sys | ||
| import json | ||
|
|
||
| def parse_blob(blob_str): | ||
| """Convert blob with escaped characters to hex string.""" | ||
| matches = re.findall(r'\\([0-9a-fA-F]{2})', blob_str) | ||
| return ''.join(matches) | ||
|
|
||
| def parse_vec_block_data(text): | ||
| block_pattern = re.compile( | ||
| r'record\s*{\s*height\s*=\s*([\d_]+)\s*:\s*nat;\s*' | ||
| r'block_hash\s*=\s*blob\s*"([^"]+)";\s*' | ||
| r'difficulty\s*=\s*([\d_]+)\s*:\s*nat;\s*' | ||
| r'children\s*=\s*vec\s*{([^}]*)};\s*' | ||
| r'no_difficulty_counter\s*=\s*([\d_]+)\s*:\s*nat;\s*' | ||
| r'prev_block_hash\s*=\s*blob\s*"([^"]+)";\s*}', re.DOTALL) | ||
|
|
||
| blocks = [] | ||
| for match in block_pattern.finditer(text): | ||
| height = int(match.group(1).replace("_", "")) | ||
| block_hash = parse_blob(match.group(2)) | ||
| difficulty = int(match.group(3).replace("_", "")) | ||
| children_raw = match.group(4) | ||
| children_blobs = re.findall(r'blob\s*"([^"]+)"', children_raw) | ||
| children = [parse_blob(b) for b in children_blobs] | ||
| no_difficulty_counter = int(match.group(5).replace("_", "")) | ||
| prev_block_hash = parse_blob(match.group(6)) | ||
|
|
||
| blocks.append({ | ||
| "height": height, | ||
| "difficulty": difficulty, | ||
| "no_difficulty_counter": no_difficulty_counter, | ||
| "prev_block_hash": prev_block_hash, | ||
| "block_hash": block_hash, | ||
| "children": children, | ||
| }) | ||
| return {"data": blocks} | ||
|
|
||
| def main(): | ||
| if len(sys.argv) != 3: | ||
| print("Usage: unstable_blocks.py <input.txt> <output.json>") | ||
| sys.exit(1) | ||
|
|
||
| with open(sys.argv[1], "r") as f: | ||
| candid_text = f.read() | ||
|
|
||
| parsed = parse_vec_block_data(candid_text) | ||
|
|
||
| with open(sys.argv[2], "w") as f: | ||
| json.dump(parsed, f, indent=2) | ||
|
|
||
| print(f"Saved parsed output to {sys.argv[2]}") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
| # dfx canister call --network testnet bitcoin_t get_unstable_blocks > ./unstable_blocks/output.txt |
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,51 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import json | ||
| import matplotlib.pyplot as plt | ||
| import networkx as nx | ||
|
|
||
| def short_hash(hash_hex): | ||
| return hash_hex[:6] | ||
|
|
||
| def load_blocks(file_path): | ||
| with open(file_path, "r") as f: | ||
| data = json.load(f) | ||
| return data["data"] | ||
|
|
||
| def build_graph(blocks): | ||
| G = nx.DiGraph() | ||
|
|
||
| hash_to_block = {b["block_hash"]: b for b in blocks} | ||
|
|
||
| for block in blocks: | ||
| label = f'H:{block["height"]} D:{block["difficulty"]} #{short_hash(block["block_hash"])}' | ||
| G.add_node(block["block_hash"], label=label) | ||
|
|
||
| for block in blocks: | ||
| for child_hash in block["children"]: | ||
| if child_hash in hash_to_block: | ||
| G.add_edge(block["block_hash"], child_hash) | ||
|
|
||
| return G | ||
|
|
||
| def draw_graph(G): | ||
| pos = nx.spring_layout(G, seed=42) # You can try different layouts like graphviz_layout if you have pygraphviz | ||
| labels = nx.get_node_attributes(G, 'label') | ||
|
|
||
| plt.figure(figsize=(14, 10)) | ||
| nx.draw_networkx_nodes(G, pos, node_size=800, node_color='lightblue') | ||
| nx.draw_networkx_edges(G, pos, arrows=True, arrowstyle='->') | ||
| nx.draw_networkx_labels(G, pos, labels, font_size=8) | ||
|
|
||
| plt.title("Blockchain Visualization") | ||
| plt.axis("off") | ||
| plt.tight_layout() | ||
| plt.show() | ||
|
|
||
| def main(): | ||
| blocks = load_blocks("./unstable_blocks/output.json") | ||
| G = build_graph(blocks) | ||
| draw_graph(G) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
[nitpick] The function 'testnet_unstable_max_depth_difference' contains several commented-out lines which may reduce clarity. Consider removing or clearly documenting the purpose of this debug code once it is no longer needed.