# A guide to build a Dogecoin SPV node
This is a full guide to write a working Dogecoin SPV node. It will go through the P2P network messaging, the process of connecting to a peer and the process of syncing.
## Simplified Payment Verification
A Simplified Payment Verification (SPV) node is a node that only keep interesting transactions and the headers chain. It has the advantage of saving disk space and bandwith (in its improved version). It is ideal for wallet running on mobile on desktop user because it can gather the transactions and calculate your balance without the need of a third party service and without having to run a full node with all its inconvenients.
It also integrate the concept of [Bloom Filter](https://bitcoinops.org/en/topics/transaction-bloom-filtering/) which if parametized correctly helps saving bandwith and can speed up syncing as we will only ask for interesting transactions with a degree of false positive that allow a certain degree of privacy.
## P2P Messages
### Message structure
| Field Size | Description | Data type | Comments |
| ---------- | ----------- |:-------------------------- | ------------------------------------------------------------------------------------------------------------ |
| 4 | magic | Unsigned Integer (32 bits) | Magic value indicating message origin network, and used to seek to next message when stream state is unknown |
| 12 | command | String (12 char) | ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected) |
| 4 | length | Unsigned Integer (32 bits) | Length of payload in number of bytes |
| 4 | checksum | Unsigned Integer (32 bits) | First 4 bytes of sha256(sha256(payload)) |
| ? | payload | Bytes | The actual data |
[Source: Message structure](https://en.bitcoin.it/wiki/Protocol_documentation#Common_structures)
Dogecoin network values
| Network | Magic value | Source code |
| ------- | ----------- | ------------------------------------------------------------------------------------------ |
| Mainnet | 0xc0c0c0c0 | [chainparams.cpp L147-150](https://github.com/dogecoin/dogecoin/blob/master/src/chainparams.cpp#L147-L150) |
| Testnet | 0xfcc1b7dc | [chainparams.cpp L305-308](https://github.com/dogecoin/dogecoin/blob/master/src/chainparams.cpp#L305-L308) |
| Regtest | 0xfabfb5da | [chainparams.cpp L435-438](https://github.com/dogecoin/dogecoin/blob/master/src/chainparams.cpp#L435-L438) |
**Note**: When sent over the wire the magic data is reversed `0xfcc1b7dc` --> `DC B7 C1 FC`.
### commands
All the commands available :
* `version`
* `verack`
* `addr`
* `inv`
* `getdata`
* `merkleblock`
* `getblocks`
* `getheaders`
* `tx`
* `headers`
* `block`
* `getaddr`
* `mempool`
* `ping`
* `pong`
* `alert`
* `notfound`
* `filterload`
* `filteradd`
* `filterclear`
* `reject`
* `sendheaders`
* `feefilter`
* `sendcmpct`
* `cmpctblock`
* `getblocktxn`
* `blocktxn`
[protocol.cpp L15-L43](https://github.com/dogecoin/dogecoin/blob/f80bfe9068ac1a0619d48dad0d268894d926941e/src/protocol.cpp#L15-L43)
### Exercice
**Ping** command doesn't have any payload. Try to build a message for **regtest** that has the **ping** command.
### Solution
Next week.
Now I am waiting for you guys to actually send me a solution
## Connect to a peer
This part will cover how to connect to a peer. Only once this step is successfull we can start syncing our headers.
### `Version` message