Module messages

Module messages 

Source
Expand description

Peer-to-peer network protocol messages

§Examples

Decode a network message

use bch::messages::Message;
use bch::network::Network;
use std::io::Cursor;

let bytes = [
    227, 225, 243, 232, 104, 101, 97, 100, 101, 114, 115,
    0, 0, 0, 0, 0, 1, 0, 0, 0, 20, 6, 224, 88, 0,
];
let magic = Network::Mainnet.magic();
let message = Message::read(&mut Cursor::new(&bytes), magic).unwrap();

match message {
    Message::Headers(headers) => { /* Handle headers message */ },
    _ => { /* All other messages */ }
}

Construct a transaction:

use bch::messages::{OutPoint, Tx, TxIn, TxOut};
use bch::transaction::p2pkh::{create_pk_script, create_sig_script};
use bch::util::{hash160, Amount, Hash256, Units};

// Use real values here
let signature = [0; 72];
let public_key = [0; 33];
let prev_output = OutPoint {
    hash: Hash256([0; 32]),
    index: 0,
};

let inputs = vec![TxIn {
    prev_output,
    sig_script: create_sig_script(&signature, &public_key),
    sequence: 0,
}];

let outputs = vec![TxOut {
    amount: Amount::from(1., Units::Bch),
    pk_script: create_pk_script(&hash160(&public_key)),
}];

let tx = Tx {
    version: 2,
    inputs,
    outputs,
    lock_time: 0,
};

Modules§

commands
Message commands for the header

Structs§

Addr
Known node addresses
Block
Block of transactions
BlockHeader
Block header
BlockLocator
Specifies which blocks to return
FeeFilter
Specifies the minimum transaction fee this node accepts
FilterAdd
Adds a data element to the bloom filter
FilterLoad
Loads a bloom filter using the specified parameters
Headers
Collection of block headers
Inv
Inventory payload describing objects a node knows about
InvVect
Inventory vector describing an object being requested or announced
MerkleBlock
A block header and partial merkle tree for SPV nodes to validate transactions
MessageHeader
Header that begins all messages
NodeAddr
Network address for a node on the network
NodeAddrEx
Node network address extended with a last connected time
OutPoint
Reference to a transaction output
Ping
Ping or pong payload
Reject
Rejected message
SendCmpct
Specifies whether compact blocks are supported
Tx
Bitcoin transaction
TxIn
Transaction input
TxOut
Transaction output
Version
Version payload defining a node’s capabilities

Enums§

Message
Bitcoin peer-to-peer message with its payload

Constants§

BLOOM_UPDATE_ALL
Filter is updated to include the serialized outpoint if any data elements matched in its script pubkey
BLOOM_UPDATE_NONE
Filter is not adjusted when a match is found
BLOOM_UPDATE_P2PUBKEY_ONLY
Filter is updated simialr to BLOOM_UPDATE_ALL but only for P2PK or multisig transactions
COINBASE_OUTPOINT_HASH
The coinbase transaction input will have this hash
COINBASE_OUTPOINT_INDEX
The coinbase transaction input will have this index
INV_VECT_BLOCK
Hash of a block header.
INV_VECT_COMPACT_BLOCK
Hash of a block header. Indicates the reply should be a cmpctblock message.
INV_VECT_ERROR
May be ignored
INV_VECT_FILTERED_BLOCK
Hash of a block header. Indicates the reply should be a merkleblock message.
INV_VECT_TX
Hash of a transaction
MAX_FILTER_ADD_DATA_SIZE
Maximum size of a data element in the FilterAdd message
MAX_INV_ENTRIES
Maximum number of objects in an inv message
MAX_PAYLOAD_SIZE
Max message payload size (32MB)
MAX_SATOSHIS
Maximum number of satoshis possible
MIN_SUPPORTED_PROTOCOL_VERSION
Minimum protocol version supported by this library
NODE_BITCOIN_CASH
Service flag that node is a full node and implements all protocol features
NODE_NETWORK
Service flag that node is a full node and implements all protocol features
NODE_NONE
Service flag that node is not a full node. Used for SPV wallets.
NO_CHECKSUM
Checksum to use when there is an empty payload
NO_HASH_STOP
Return results until either there are 2000 for getheaders or 500 or getblocks, or no more left
PROTOCOL_VERSION
Protocol version supported by this library
REJECT_CHECKPOINT
REJECT_DUPLICATE
REJECT_DUST
REJECT_INSUFFICIENT_FEE
REJECT_INVALID
REJECT_MALFORMED
REJECT_NONSTANDARD
REJECT_OBSOLETE
UNKNOWN_IP
Unknown IP address to use as a default

Traits§

Payload
Message payload that is writable to bytes

Functions§

header_hash
Returns the hash for a header at a particular index utilizing prev_hash if possible