# Tips on writing with rust

1. Attend the lecture or watch the recording on how to use tokio.
2. make sure to await on the futures.
3. Divide the tasks into small helper functions for easier debug.
4. Test on regtest first with `-degug=net` option set, this will help you debug errors in your message serialization.
5. Check the endianness of the messages
6. Use `tracing::info or error` for logging. Don't use `dbg` or `println` as it won't give readable logs in multi threaded environment as we saw in last week.

## How to use TcpStream

It is like an `Iterator` except that not all the data are present in the beginning. It is a `Future` in the essence that you have to keep polling using `read` method to look for data. Use network magic bytes to mark the start of the message you received from the peer. Use `bitcoin::consensus::{serialize, deserialize}` to send and receive messages in your rust code.

## Summary

I have added some code structure and pointers to make it easier for you. You can add more functions and unit tests.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bitcoin-dev-project.gitbook.io/rust-for-bitcoiners/rust-for-bitcoiners-2/assignments/a-8/rust.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
