Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
  2. const uint8_t IBLT_CELL_MINIMUM = 2;
  3. const uint16_t APPROX_ITEMS_THRESH = 600;
  4. const uint16_t APPROX_ITEMS_THRESH_REDUCE_CHECK = 500;
  5. const uint8_t APPROX_EXCESS_RATE = 4;
  6. const float IBLT_DEFAULT_OVERHEAD = 1.5;
  7. const uint8_t FILTER_CELL_SIZE = 1;
  8. const uint8_t IBLT_FIXED_CELL_SIZE = 13;
  9.  
  10. // Infer various receiver quantities
  11. uint64_t nReceiverExcessItems =
  12.      std::max((int)(nReceiverUniverseItems - nItems), (int)(nSenderUniverseItems - nItems));
  13. nReceiverExcessItems = std::max(0, (int)nReceiverExcessItems); // must be non-negative
  14. nReceiverExcessItems =
  15.         std::min((int)nReceiverUniverseItems, (int)nReceiverExcessItems); // must not exceed total mempool size
  16. uint64_t nReceiverMissingItems = std::max(1, (int)(nItems - (nReceiverUniverseItems - nReceiverExcessItems)));
  17.  
  18. // Optimal symmetric differences between receiver and sender IBLTs
  19. // This is the parameter "a" from the graphene paper
  20. double optSymDiff = nReceiverMissingItems;
  21. if (nItems <= nReceiverUniverseItems + nReceiverMissingItems)
  22.     optSymDiff = OptimalSymDiff(nItems, nReceiverUniverseItems, nReceiverExcessItems, nReceiverMissingItems);
  23.  
  24. // Set false positive rate for Bloom filter based on optSymDiff
  25. double fpr = BloomFalsePositiveRate(optSymDiff, nReceiverExcessItems);
  26.  
  27. // So far we have only made room for false positives in the IBLT
  28. // Make more room for missing items
  29. optSymDiff += nReceiverMissingItems;
  30. uint64_t nIbltCells = std::max((int)IBLT_CELL_MINIMUM, (int)ceil(optSymDiff));
  31.  
  32. double CGrapheneSet::OptimalSymDiff(uint64_t nBlockTxs,
  33.     uint64_t nReceiverPoolTx,
  34.     uint64_t nReceiverExcessTxs,
  35.     uint64_t nReceiverMissingTxs)
  36. {
  37.     uint16_t approx_items_thresh = version >= 4 ? APPROX_ITEMS_THRESH_REDUCE_CHECK : APPROX_ITEMS_THRESH;
  38.  
  39.     double optSymDiff;
  40.     if (nBlockTxs >= approx_items_thresh && nReceiverExcessTxs >= nBlockTxs / APPROX_EXCESS_RATE)
  41.        optSymDiff = ApproxOptimalSymDiff(nBlockTxs);
  42.     else
  43.         optSymDiff = BruteForceSymDiff(nBlockTxs, nReceiverPoolTx, nReceiverExcessTxs, nReceiverMissingTxs, MAX_CHECKSUM_BITS);
  44.     return optSymDiff
  45. }
  46.  
  47. double CGrapheneSet::ApproxOptimalSymDiff(uint64_t nBlockTxs)
  48. {
  49.     uint32_t nChecksumBits = 32;
  50.  
  51.     if (version >= 4)
  52.         assert(nBlockTxs >= APPROX_ITEMS_THRESH_REDUCE_CHECK);
  53.     else
  54.         assert(nBlockTxs >= APPROX_ITEMS_THRESH);
  55.  
  56.     return std::max(1.0, std::round(FILTER_CELL_SIZE * nBlockTxs /
  57.                                     ((nChecksumBits + 8 * IBLT_FIXED_CELL_SIZE) * IBLT_DEFAULT_OVERHEAD * LN2SQUARED)));
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement