libbitcoin / libbitcoin-network

Bitcoin P2P Network Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimize message serialization.

evoskuil opened this issue · comments

// TODO: eliminate this method based on integrated heading serialization.
/// Serialize a message object to the Bitcoin wire protocol encoding.
template <typename Message>
data_chunk serialize(uint32_t version, const Message& packet,
    uint32_t magic)
{
    // TODO: parameterize all to_data() calls with header inclusion option.
    // Serialize the payload (required for header size).
    const auto payload = packet.to_data(version);

    // TODO: move this into message serialization.
    // Construct the payload header.
    heading head(magic, Message::command,
        safe_unsigned<uint32_t>(payload.size()), bitcoin_checksum(payload));

    // TODO: the above change eliminates this payload reallocation and copy.
    // Serialize header and copy the payload into a single message buffer.
    auto message = head.to_data();
    extend_data(message, payload);
    return message;
}

Implemented for v3.1