The P2P Puppet Master: Testing Network Logic with Python
The P2P Puppet Master: Testing Network Logic with Python
In our next 1,500 words, we perform a granular audit of the Shadow Protocol. Sometimes, you don't want to test a real node talking to a real node. You want to test a real node talking to a "Malicious Puppet." This is done using the P2PInterface class.
Analyzing the Puppet: The P2PConnection
# PEDAGOGICAL ANALYSIS: THE LIAR
# This Python class pretends to be a Bitcoin node.
# But it is actually a "Script" that can lie.
class MaliciousNode(P2PInterface):
def on_version(self, message):
# 1. Receive the "Version" message from the real node.
# 2. Send back a fake "Version" that says we are from the future.
self.send_message(msg_version(nVersion=99999))
def on_inv(self, message):
# 3. If the real node tells us about a block...
# 4. Just ignore it. Don't say anything back.
pass
Explaining the Puppet: The Deception of the Mesh
-
"The Raw Byte Control": Unlike a real node (which is limited by the C++ rules), the Python Puppet can send Any Bytes it wants. It can send broken blocks, ancient versions, or infinite loops of data. It is the Creativity of the Sovereign.
-
"The
on_messageHandlers": These are "Event Listeners." When the real node says something, the Puppet "Reacts" according to the script. This allows us to test "What happens if a peer is incredibly slow?" or "What happens if a peer sends us a block from a different chain?" It is the Reactiveness of the Machine. -
"The Falsification of Protocol": The Puppet can "Skip" steps in the handshake. It can send a
GETDATAbefore it has even saidVERSION. This tests the "Sanity" of the C++ code's state machine. It is the Rigidity of the Protocol. -
"The Stealth of the Script": Because the Puppet is just a Python script, it uses very little memory. We can start 1,000 Puppets and "Swarm" a single real node to see if it crashes. It is the Pressure of the Core.
The Sovereignty of the Puppet
The P2P Puppet Master is the "Inquisitor of the Node." It asks the hardest questions and tells the most complex lies to see if the node can find the truth. As a Sovereign Architect, you know that "The truth is only found by questioning every lie." By running a node that has survived a "Swarm of Liars," you are ensuring your machine is an "Incorruptible Judge" of economic reality. You are the Master of the Puppet.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: