changeset 23735:c7833a5d5e9f

Add class CThinBlockStats to keep track of thin block statistics.
author Simon <simon@bitcartel.com>
date Tue, 08 Mar 2016 13:54:41 -0800
parents 7ec35f979a30
children 94901e3d8c5f
files src/main.cpp src/thinblock.cpp src/thinblock.h
diffstat 3 files changed, 72 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.cpp	Mon Mar 07 20:49:11 2016 -0500
+++ b/src/main.cpp	Tue Mar 08 13:54:41 2016 -0800
@@ -5214,6 +5214,12 @@
                      nSizeThinBlock,
                      ((float) blockSize) / ((float) nSizeThinBlock)
                      );
+
+            // Update run-time statistics of thin block bandwidth savings
+            CThinBlockStats::Update(nSizeThinBlock, blockSize);
+            std::string ss = CThinBlockStats::ToString();
+            LogPrint("thin", "thin block stats: %s\n", ss.c_str());
+
             HandleBlockMessage(pfrom, strCommand, pfrom->thinBlock, inv);  // clears the thin block
             BOOST_FOREACH(uint64_t &cheapHash, thinBlock.vTxHashes)
                 EraseOrphanTx(mapPartialTxHash[cheapHash]);
@@ -5307,6 +5313,12 @@
                      nSizeThinBlock,
                      ((float) blockSize) / ((float) nSizeThinBlock)
                      );
+
+            // Update run-time statistics of thin block bandwidth savings
+            CThinBlockStats::Update(nSizeThinBlock, blockSize);
+            std::string ss = CThinBlockStats::ToString();
+            LogPrint("thin", "thin block stats: %s\n", ss.c_str());
+
             HandleBlockMessage(pfrom, strCommand, pfrom->thinBlock, inv);
             BOOST_FOREACH(uint256 &hash, thinBlock.vTxHashes)
                 EraseOrphanTx(hash);
@@ -5359,6 +5371,12 @@
                      nSizeThinBlockTx,
                      ((float) blockSize) / ( (float) pfrom->nSizeThinBlock + (float) nSizeThinBlockTx )
                      );
+
+            // Update run-time statistics of thin block bandwidth savings
+            CThinBlockStats::Update(nSizeThinBlockTx + pfrom->nSizeThinBlock, blockSize);
+            std::string ss = CThinBlockStats::ToString();
+            LogPrint("thin", "thin block stats: %s\n", ss.c_str());
+
             std::vector<CTransaction> vTx = pfrom->thinBlock.vtx;
             HandleBlockMessage(pfrom, strCommand, pfrom->thinBlock, inv);
             for (unsigned int i = 0; i < vTx.size(); i++)
--- a/src/thinblock.cpp	Mon Mar 07 20:49:11 2016 -0500
+++ b/src/thinblock.cpp	Tue Mar 08 13:54:41 2016 -0800
@@ -3,6 +3,14 @@
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
 #include "thinblock.h"
+#include <sstream>
+#include <iomanip>
+
+// Start statistics at zero
+uint64_t CThinBlockStats::nOriginalSize = 0;
+uint64_t CThinBlockStats::nThinSize = 0;
+uint64_t CThinBlockStats::nBlocks = 0;
+
 
 CThinBlock::CThinBlock(const CBlock& block, CBloomFilter& filter)
 {
@@ -87,3 +95,32 @@
     for (unsigned int i = 0; i < n; i++)
         mapTx[vHashesToRequest[i]] = tx;
 }
+
+
+
+void CThinBlockStats::Update(uint64_t nThinBlockSize, uint64_t nOriginalBlockSize)
+{
+	CThinBlockStats::nOriginalSize += nOriginalBlockSize;
+	CThinBlockStats::nThinSize += nThinBlockSize;
+	CThinBlockStats::nBlocks++;
+}
+
+
+std::string CThinBlockStats::ToString()
+{
+	static const char *units[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
+	int i = 0;
+	double size = double( CThinBlockStats::nOriginalSize - CThinBlockStats::nThinSize );
+	while (size > 1024) {
+		size /= 1024;
+		i++;
+	}
+
+	std::ostringstream ss;
+	ss << std::fixed << std::setprecision(2);
+	ss << CThinBlockStats::nBlocks << " thin " << ((CThinBlockStats::nBlocks>1) ? "blocks have" : "block has") << " saved " << size << units[i] << " of bandwidth";
+	std::string s = ss.str();
+	return s;
+}
+
+
--- a/src/thinblock.h	Mon Mar 07 20:49:11 2016 -0500
+++ b/src/thinblock.h	Tue Mar 08 13:54:41 2016 -0800
@@ -78,4 +78,21 @@
         READWRITE(mapTx);
     }
 };
+
+// This class stores statistics for thin block derived protocols.
+class CThinBlockStats
+{
+private:
+	static uint64_t nOriginalSize;
+	static uint64_t nThinSize;
+	static uint64_t nBlocks;
+public:
+	static void Update(uint64_t nThinBlockSize, uint64_t nOriginalBlockSize);
+	static std::string ToString();
+};
+
+
+
+
+
 #endif // BITCOIN_THINBLOCK_H