Safe Haskell | None |
---|---|
Language | Haskell98 |
This package provides basic types used for the Bitcoin networking protocol together with Data.Binary instances for efficiently serializing and de-serializing them. More information on the bitcoin protocol is available here: http://en.bitcoin.it/wiki/Protocol_specification
- data GetData = GetData {
- getDataList :: ![InvVector]
- data Inv = Inv {}
- data InvVector = InvVector {}
- data InvType
- data NotFound = NotFound {
- notFoundList :: ![InvVector]
- newtype VarInt = VarInt {}
- newtype VarString = VarString {}
- data NetworkAddress = NetworkAddress {
- naServices :: !Word64
- naAddress :: !SockAddr
- data Addr = Addr {
- addrList :: ![NetworkAddressTime]
- type NetworkAddressTime = (Word32, NetworkAddress)
- data Version = Version {}
- newtype Ping = Ping {}
- newtype Pong = Pong {}
- data Alert = Alert {}
- data Reject = Reject {}
- data RejectCode
- reject :: MessageCommand -> RejectCode -> ByteString -> Reject
- data Message
- = MVersion !Version
- | MVerAck
- | MAddr !Addr
- | MInv !Inv
- | MGetData !GetData
- | MNotFound !NotFound
- | MGetBlocks !GetBlocks
- | MGetHeaders !GetHeaders
- | MTx !Tx
- | MBlock !Block
- | MMerkleBlock !MerkleBlock
- | MHeaders !Headers
- | MGetAddr
- | MFilterLoad !FilterLoad
- | MFilterAdd !FilterAdd
- | MFilterClear
- | MPing !Ping
- | MPong !Pong
- | MAlert !Alert
- | MMempool
- | MReject !Reject
- data MessageHeader = MessageHeader {
- headMagic :: !Word32
- headCmd :: !MessageCommand
- headPayloadSize :: !Word32
- headChecksum :: !CheckSum32
- data MessageCommand
- data BloomFlags
- data BloomFilter = BloomFilter {
- bloomData :: !(Seq Word8)
- bloomHashFuncs :: !Word32
- bloomTweak :: !Word32
- bloomFlags :: !BloomFlags
- newtype FilterLoad = FilterLoad {}
- newtype FilterAdd = FilterAdd {}
- bloomCreate :: Int -> Double -> Word32 -> BloomFlags -> BloomFilter
- bloomInsert :: BloomFilter -> ByteString -> BloomFilter
- bloomContains :: BloomFilter -> ByteString -> Bool
- isBloomValid :: BloomFilter -> Bool
- isBloomEmpty :: BloomFilter -> Bool
- isBloomFull :: BloomFilter -> Bool
Requesting data
The GetData
type is used to retrieve information on a specific object
(Block
or Tx
) identified by the objects hash. The payload of a GetData
request is a list of InvVector
which represent all the hashes for which a
node wants to request information. The response to a GetBlock
message
wille be either a Block
or a Tx
message depending on the type of the
object referenced by the hash. Usually, GetData
messages are sent after a
node receives an Inv
message to obtain information on unknown object
hashes.
GetData | |
|
Invectory vectors represent hashes identifying objects such as a Block
or a Tx
. They are sent inside messages to notify other peers about
new data or data they have requested.
Data type identifying the type of an inventory vector.
InvError | Error. Data containing this type can be ignored. |
InvTx | InvVector hash is related to a transaction |
InvBlock | InvVector hash is related to a block |
InvMerkleBlock | InvVector has is related to a merkle block |
A NotFound
message is returned as a response to a GetData
message
whe one of the requested objects could not be retrieved. This could happen,
for example, if a tranasaction was requested and was not available in the
memory pool of the receiving node.
NotFound | |
|
Network types
Data type representing a variable length integer. The VarInt
type
usually precedes an array or a string that can vary in length.
Data type for variable length strings. Variable length strings are
serialized as a VarInt
followed by a bytestring.
data NetworkAddress Source
Data type describing a bitcoin network address. Addresses are stored in
IPv6. IPv4 addresses are mapped to IPv6 using IPv4 mapped IPv6 addresses:
http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses. Sometimes,
timestamps are sent together with the NetworkAddress
such as in the Addr
data type.
NetworkAddress | |
|
Provides information on known nodes in the bitcoin network. An Addr
type is sent inside a Message
as a response to a GetAddr
message.
Addr | |
|
type NetworkAddressTime = (Word32, NetworkAddress) Source
Network address with a timestamp
When a bitcoin node creates an outgoing connection to another node,
the first message it will send is a Version
message. The other node
will similarly respond with it's own Version
message.
Version | |
|
A Ping message is sent to bitcoin peers to check if a TCP/IP connection is still valid.
A Pong message is sent as a response to a ping message.
Data type describing signed messages that can be sent between bitcoin nodes to display important notifications to end users about the health of the network.
Alert | |
|
The reject message is sent when messages are rejected by a peer.
Reject | |
|
data RejectCode Source
reject :: MessageCommand -> RejectCode -> ByteString -> Reject Source
Convenience function to build a Reject message
Messages
The Message
type is used to identify all the valid messages that can be
sent between bitcoin peers. Only values of type Message
will be accepted
by other bitcoin peers as bitcoin protocol messages need to be correctly
serialized with message headers. Serializing a Message
value will
include the MessageHeader
with the correct checksum value automatically.
No need to add the MessageHeader
separately.
data MessageHeader Source
Data type representing the header of a Message
. All messages sent between
nodes contain a message header.
MessageHeader | |
|
data MessageCommand Source
A MessageCommand
is included in a MessageHeader
in order to identify
the type of message present in the payload. This allows the message
de-serialization code to know how to decode a particular message payload.
Every valid Message
constructor has a corresponding MessageCommand
constructor.
Bloom filters
data BloomFlags Source
The bloom flags are used to tell the remote peer how to auto-update the provided bloom filter.
BloomUpdateNone | Never update |
BloomUpdateAll | Auto-update on all outputs |
BloomUpdateP2PubKeyOnly | Only auto-update on outputs that are pay-to-pubkey or pay-to-multisig. This is the default setting. |
data BloomFilter Source
A bloom filter is a probabilistic data structure that SPV clients send to other peers to filter the set of transactions received from them. Bloom filters are probabilistic and have a false positive rate. Some transactions that pass the filter may not be relevant to the receiving peer. By controlling the false positive rate, SPV nodes can trade off bandwidth versus privacy.
BloomFilter | |
|
newtype FilterLoad Source
Set a new bloom filter on the peer connection.
Add the given data element to the connections current filter without requiring a completely new one to be set.
:: Int | Number of elements |
-> Double | False positive rate |
-> Word32 | A random nonce (tweak) for the hash function. It should be a random number but the secureness of the random value is not of geat consequence. |
-> BloomFlags | Bloom filter flags |
-> BloomFilter | Bloom filter |
Build a bloom filter that will provide the given false positive rate when the given number of elements have been inserted.
:: BloomFilter | Original bloom filter |
-> ByteString | New data to insert |
-> BloomFilter | Bloom filter containing the new data |
Insert arbitrary data into a bloom filter. Returns the new bloom filter containing the new data.
:: BloomFilter | Bloom filter |
-> ByteString | Data that will be checked against the given bloom filter |
-> Bool | Returns True if the data matches the filter |
Tests if some arbitrary data matches the filter. This can be either because the data was inserted into the filter or because it is a false positive.
:: BloomFilter | Bloom filter to test |
-> Bool | True if the given filter is valid |
Tests if a given bloom filter is valid.
isBloomEmpty :: BloomFilter -> Bool Source
Returns True if the filter is empty (all bytes set to 0x00)
isBloomFull :: BloomFilter -> Bool Source
Returns True if the filter is full (all bytes set to 0xff)