{"type":"rich","version":"1.0","author_name":"npub1tjephawh7fdf6358jufuh5eyxwauzrjqa7qn50pglee4tayc2ntqcjtl6r","author_url":"https://nostr.ae/npub1tjephawh7fdf6358jufuh5eyxwauzrjqa7qn50pglee4tayc2ntqcjtl6r","provider_name":"njump","provider_url":"https://nostr.ae","html":"📅 Original date posted:2016-05-09\n📝 Original message:On 05/03/2016 12:13 AM, lf-lists at mattcorallo.com (Matt Corallo) wrote:\n\u003e Hi all,\n\u003e \n\u003e The following is a BIP-formatted design spec for compact block relay\n\u003e designed to limit on wire bytes during block relay. You can find the\n\u003e latest version of this document at\n\u003e https://github.com/TheBlueMatt/bips/blob/master/bip-TODO.mediawiki.\n\nHi Matt,\n\nthank you for working on this!\n\n\u003e ===New data structures===\n\u003e Several new data structures are added to the P2P network to relay\n\u003e compact blocks: PrefilledTransaction, HeaderAndShortIDs,\n\u003e BlockTransactionsRequest, and BlockTransactions. Additionally, we\n\u003e introduce a new variable-length integer encoding for use in these data\n\u003e structures.\n\u003e \n\u003e For the purposes of this section, CompactSize refers to the\n\u003e variable-length integer encoding used across the existing P2P protocol\n\u003e to encode array lengths, among other things, in 1, 3, 5 or 9 bytes.\n\nThis is a not, but I think it's a bit strange to have two separate\nvariable length integers in the same specification. I understand is one\nis already the default for variable-length integers currently, and there\nare reasons to use the other one for efficiency reasons in some places,\nbut perhaps we should aim to get everything using the latter?\n\n\u003e ====New VarInt====\n\u003e Variable-length integers: bytes are a MSB base-128 encoding of the number.\n\u003e The high bit in each byte signifies whether another digit follows. To make\n\u003e sure the encoding is one-to-one, one is subtracted from all but the last\n\u003e digit.\n\nMaybe it's worth mentioning that it is based on ASN.1 BER's compressed\ninteger format (see\nhttps://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf\nsection 8.1.3.5), though with a small modification to make every integer\nhave a single unique encoding.\n\n\u003e ====HeaderAndShortIDs====\n\u003e A HeaderAndShortIDs structure is used to relay a block header, the short\n\u003e transactions IDs used for matching already-available transactions, and a\n\u003e select few transactions which we expect a peer may be missing.\n\u003e \n\u003e |shortids||List of uint64_ts||8*shortids_length bytes||Little\n\u003e Endian||The short transaction IDs calculated from the transactions which\n\u003e were not provided explicitly in prefilledtxn\n\nI tried to derive what length of short ids is actually necessary (some\nwrite-up is on\nhttps://gist.github.com/sipa/b2eb2e486156b5509ac711edd16153ed but it's\nincomplete).\n\nFor any reasonable numbers I can come up with (in a very wide range),\nthe number of bits needed is very well approximated by:\n\n  log2(#receiver_mempool_txn * #block_txn_not_in_receiver_mempool /\nacceptable_per_block_failure_rate)\n\nFor example, with 20000 mempool transactions, 2500 transactions in a\nblock, 95% hitrate, and a chance of 1 in 10000 blocks to fail to\nreconstruct, needed_bits = log2(20000 * 2500 * (1 - 0.95) / 0.0001) =\n34.54, or 5 byte txids would suffice.\n\nNote that 1 in 10000 failures may sound like a lot, but this is for each\nindividual connection, and since every transmission uses separately\nsalted identifiers, occasional failures should not affect global\npropagation. Given that transmission failures due to timeouts, network\nconnectivity, ... already occur much more frequently than once every few\ngigabytes (what 10000 blocks corresponds to), that's probably already\nmore than enough.\n\nIn short: I believe 5 or 6 byte txids should be enough, but perhaps it\nmakes sense to allow the sender to choose (so he can weigh trying\nmultiple nonces against increasing the short txid length).\n\n\u003e ====Short transaction IDs====\n\u003e Short transaction IDs are used to represent a transaction without\n\u003e sending a full 256-bit hash. They are calculated by:\n\u003e # single-SHA256 hashing the block header with the nonce appended (in\n\u003e little-endian)\n\u003e # XORing each 8-byte chunk of the double-SHA256 transaction hash with\n\u003e each corresponding 8-byte chunk of the hash from the previous step\n\u003e # Adding each of the XORed 8-byte chunks together (in little-endian)\n\u003e iteratively to find the short transaction ID\n\nAn alternative would be using SipHash-1-3 (a form of SipHash with\nreduced iteration counts; the default is SipHash-2-4). SipHash was\ndesigned as a Message Authentication Code, where the security\nrequirements are much stronger than in our case (in particular, we don't\ncare about observers being able to finding the key, as the key is just\npublic knowledge here). One of the designers of SipHash has commented\nthat SipHash-1-3 for collision resistance in hash tables may be enough:\nhttps://github.com/rust-lang/rust/issues/29754#issuecomment-156073946\n\nUsing SipHash-1-3 on modern hardware would take ~32 CPU cycles per txid.\n\n\u003e ===Implementation Notes===\n\nThere are a few more heuristics that MAY be used to improve performance:\n\n* Receivers should treat short txids in blocks that match multiple\nmempool transactions as non-matches, and request the transactions. This\nsignificantly reduces the failure to reconstruct.\n\n* When constructing a compact block to send, the sender can verify it\nagainst its own mempool to check for collisions, and if so, choose to\neither try another nonce, or increase the short txid length.\n\nCheers,\n\n-- \nPieter"}
