Skip to content

Commit 7a0de46

Browse files
committed
[doc] sample code for test framework p2p objects
1 parent 784f757 commit 7a0de46

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

test/functional/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ P2P messages. These can be found in the following source files:
8787

8888
#### Using the P2P interface
8989

90-
- [messages.py](test_framework/messages.py) contains all the definitions for objects that pass
90+
- `P2P`s can be used to test specific P2P protocol behavior.
91+
[p2p.py](test_framework/p2p.py) contains test framework p2p objects and
92+
[messages.py](test_framework/messages.py) contains all the definitions for objects passed
9193
over the network (`CBlock`, `CTransaction`, etc, along with the network-level
9294
wrappers for them, `msg_block`, `msg_tx`, etc).
9395

@@ -100,8 +102,22 @@ contains the higher level logic for processing P2P payloads and connecting to
100102
the Bitcoin Core node application logic. For custom behaviour, subclass the
101103
P2PInterface object and override the callback methods.
102104

103-
- Can be used to write tests where specific P2P protocol behavior is tested.
104-
Examples tests are [p2p_unrequested_blocks.py](p2p_unrequested_blocks.py),
105+
`P2PConnection`s can be used as such:
106+
107+
```python
108+
p2p_conn = node.add_p2p_connection(P2PInterface())
109+
p2p_conn.send_and_ping(msg)
110+
```
111+
112+
They can also be referenced by indexing into a `TestNode`'s `p2ps` list, which
113+
contains the list of test framework `p2p` objects connected to itself
114+
(it does not include any `TestNode`s):
115+
116+
```python
117+
node.p2ps[0].sync_with_ping()
118+
```
119+
120+
More examples can be found in [p2p_unrequested_blocks.py](p2p_unrequested_blocks.py),
105121
[p2p_compactblocks.py](p2p_compactblocks.py).
106122

107123
#### Prototyping tests
@@ -157,7 +173,7 @@ way is the use the `profile_with_perf` context manager, e.g.
157173
with node.profile_with_perf("send-big-msgs"):
158174
# Perform activity on the node you're interested in profiling, e.g.:
159175
for _ in range(10000):
160-
node.p2p.send_message(some_large_message)
176+
node.p2ps[0].send_message(some_large_message)
161177
```
162178

163179
To see useful textual output, run

0 commit comments

Comments
 (0)