Skip to content

Instantly share code, notes, and snippets.

@bissias
Created October 12, 2018 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bissias/b306439b21b022238716efd13053691f to your computer and use it in GitHub Desktop.
Save bissias/b306439b21b022238716efd13053691f to your computer and use it in GitHub Desktop.
diff --git a/src/blockrelay/graphene.cpp b/src/blockrelay/graphene.cpp
index 7f49162..1fed274 100644
--- a/src/blockrelay/graphene.cpp
+++ b/src/blockrelay/graphene.cpp
@@ -27,12 +27,13 @@ static bool ReconstructBlock(CNode *pfrom, const bool fXVal, int &missingCount,
CMemPoolInfo::CMemPoolInfo(uint64_t _nTx) : nTx(_nTx) {}
CMemPoolInfo::CMemPoolInfo() { this->nTx = 0; }
-CGrapheneBlock::CGrapheneBlock(const CBlockRef pblock, uint64_t nReceiverMemPoolTx)
+CGrapheneBlock::CGrapheneBlock(const CBlockRef pblock, uint64_t nReceiverMemPoolTx, std::vector<CTransactionRef> vMissingTxs)
{
header = pblock->GetBlockHeader();
nBlockTxs = pblock->vtx.size();
-
std::vector<uint256> blockHashes;
+
+ // Add coinbase
for (auto &tx : pblock->vtx)
{
blockHashes.push_back(tx->GetHash());
@@ -41,6 +42,10 @@ CGrapheneBlock::CGrapheneBlock(const CBlockRef pblock, uint64_t nReceiverMemPool
vAdditionalTxs.push_back(tx);
}
+ // Add missing txs
+ for (auto &tx : vMissingTxs)
+ vAdditionalTxs.push_back(tx);
+
pGrapheneSet = new CGrapheneSet(nReceiverMemPoolTx, blockHashes, true);
}
@@ -502,15 +507,26 @@ bool CGrapheneBlock::process(CNode *pfrom,
// Add full transactions included in the block
if (!collision)
{
+ int nReceiverNotMissing = 0;
for (auto &tx : vAdditionalTxs)
{
const uint256 &hash = tx->GetHash();
uint64_t cheapHash = hash.GetCheapHash();
- auto ir = mapPartialTxHash.insert(std::make_pair(cheapHash, hash));
- if (!ir.second)
- collision = true;
+ auto it = mapPartialTxHash.find(cheapHash);
+ if (it != mapPartialTxHash.end())
+ {
+ // Only mark as collision if the actual hash values differ
+ if (it->second != hash)
+ collision = true;
+ else
+ nReceiverNotMissing++;
+ }
+ else
+ mapPartialTxHash.insert(std::make_pair(cheapHash, hash));
}
+
+ LOG(GRAPHENE, "receiver not missing %d sent txs\n", nReceiverNotMissing);
}
if (!collision)
@@ -1396,7 +1412,29 @@ void SendGrapheneBlock(CBlockRef pblock, CNode *pfrom, const CInv &inv, const CM
{
try
{
- CGrapheneBlock grapheneBlock(MakeBlockRef(*pblock), mempoolinfo.nTx);
+ // Fill missing txs only if sender's mempool size is greater than receiver's
+ std::vector<CTransactionRef> vMissingTxs;
+ size_t nSenderMempoolTxs = GetGrapheneMempoolInfo().nTx + pblock->vtx.size() - 1;
+ if (nSenderMempoolTxs > mempoolinfo.nTx) {
+ {
+ LOCK(pfrom->cs_inventory);
+
+ for (auto tx : pblock->vtx)
+ {
+ if (tx->IsCoinBase())
+ continue;
+
+ if (!pfrom->filterInventoryKnown.contains(tx->GetHash()))
+ vMissingTxs.push_back(tx);
+ }
+ }
+ }
+
+ size_t nFalsePositives = pblock->vtx.size() * FILTER_INVENTORY_KNOWN_FPR;
+ LOG(GRAPHENE, "peer %s missing txs: %d; expecting %d FPs\n", pfrom->GetLogName(), vMissingTxs.size(), nFalsePositives);
+ LOG(GRAPHENE, "receiver mempool %d, this mempool %d\n", mempoolinfo.nTx, nSenderMempoolTxs);
+
+ CGrapheneBlock grapheneBlock(MakeBlockRef(*pblock), mempoolinfo.nTx, vMissingTxs);
int nSizeBlock = pblock->GetBlockSize();
int nSizeGrapheneBlock = ::GetSerializeSize(grapheneBlock, SER_NETWORK, PROTOCOL_VERSION);
diff --git a/src/blockrelay/graphene.h b/src/blockrelay/graphene.h
index a4278b3..356d279 100644
--- a/src/blockrelay/graphene.h
+++ b/src/blockrelay/graphene.h
@@ -22,6 +22,8 @@
#include <vector>
const unsigned char MIN_MEMPOOL_INFO_BYTES = 8;
+// Below should match the default for the CNode constructor in net.cpp
+const float FILTER_INVENTORY_KNOWN_FPR = 0.0078125;
class CDataStream;
class CNode;
@@ -53,7 +55,7 @@ public:
CGrapheneSet *pGrapheneSet;
public:
- CGrapheneBlock(const CBlockRef pblock, uint64_t nReceiverMemPoolTx);
+ CGrapheneBlock(const CBlockRef pblock, uint64_t nReceiverMemPoolTx, std::vector<CTransactionRef> vMissingTxs);
CGrapheneBlock() : pGrapheneSet(nullptr) {}
~CGrapheneBlock();
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment