Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Adaptive Block Size Limit #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -170,7 +170,8 @@ BITCOIN_CORE_H = \
zmq/zmqabstractnotifier.h \
zmq/zmqconfig.h\
zmq/zmqnotificationinterface.h \
zmq/zmqpublishnotifier.h
zmq/zmqpublishnotifier.h \
blocksizecalculator.h


obj/build.h: FORCE
Expand Down Expand Up @@ -213,6 +214,7 @@ libbitcoin_server_a_SOURCES = \
txdb.cpp \
txmempool.cpp \
validationinterface.cpp \
blocksizecalculator.cpp \
$(BITCOIN_CORE_H)

if ENABLE_ZMQ
Expand Down
88 changes: 88 additions & 0 deletions src/blocksizecalculator.cpp
@@ -0,0 +1,88 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "blocksizecalculator.h"

using namespace BlockSizeCalculator;
using namespace std;

unsigned int BlockSizeCalculator::ComputeBlockSize(void) {

unsigned int proposedBlockSize = ::GetMedianBlockSize();
return proposedBlockSize > MAX_BLOCK_SIZE ?
proposedBlockSize * MAX_BLOCK_SIZE_INCREASE_MULTIPLE :

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, shouldn't the multiplier be applied before comparing with MAX_BLOCK_SIZE?

MAX_BLOCK_SIZE;

}

unsigned int BlockSizeCalculator::GetMedianBlockSize() {

std::vector<unsigned int> blocksizes = GetBlockSizes();

unsigned int vsize = blocksizes.size();
if (vsize > 0) {
std::sort(blocksizes.begin(), blocksizes.end());
unsigned int median = 0;
if ((vsize % 2) == 0) {
median = (blocksizes[vsize/2] + blocksizes[(vsize/2) - 1])/2;
} else {
median = blocksizes[vsize/2];
}
return median;
} else {
return MAX_BLOCK_SIZE;
}

}

std::vector<unsigned int> BlockSizeCalculator::GetBlockSizes(void) {

std::vector<unsigned int> blocksizes;

unsigned int currentHeight = chainActive.Height();
unsigned int firstBlock = currentHeight - NUM_BLOCKS_FOR_MEDIAN_BLOCK;

if (firstBlock > 0) {

for (int i = firstBlock; i <= currentHeight; i++) {
unsigned int blocksize = GetBlockSize(i);
if (blocksize != -1) {
blocksizes.push_back(blocksize);
}
}

}

return blocksizes;

}

unsigned int BlockSizeCalculator::GetBlockSize(unsigned int height) {

CBlockIndex* pblockindex;

pblockindex = chainActive[height];
if (pblockindex == NULL) {
return -1;
}

const CDiskBlockPos& pos = pblockindex->GetBlockPos();

CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);

if (filein.IsNull()) {
return -1;
}

FILE* blockFile = filein.release();
long int filePos = ftell(blockFile);
fseek(blockFile, filePos - sizeof(uint32_t), SEEK_SET);

uint32_t size = 0;
fread(&size, sizeof(uint32_t), 1, blockFile);
fclose(blockFile);
return (unsigned int)size;

}
25 changes: 25 additions & 0 deletions src/blocksizecalculator.h
@@ -0,0 +1,25 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef blocksizecalculator_h
#define blocksizecalculator_h

#include <iostream>
#include <vector>
#include "streams.h"
#include "main.h"
#include "consensus/consensus.h"
#include "chain.h"
#include "clientversion.h"

using namespace std;

namespace BlockSizeCalculator {
unsigned int ComputeBlockSize(void);
unsigned int GetMedianBlockSize();
std::vector<unsigned int> GetBlockSizes(void);
unsigned int GetBlockSize(unsigned int height);
}
#endif
8 changes: 6 additions & 2 deletions src/consensus/consensus.h
Expand Up @@ -6,10 +6,14 @@
#ifndef BITCOIN_CONSENSUS_CONSENSUS_H
#define BITCOIN_CONSENSUS_CONSENSUS_H

/** The maximum allowed multiple for the computed block size */
static const unsigned int MAX_BLOCK_SIZE_INCREASE_MULTIPLE = 2;
/** The number of blocks to consider in the computation of median block size */
static const unsigned int NUM_BLOCKS_FOR_MEDIAN_BLOCK = 2016;
/** The maximum allowed size for a serialized block, in bytes (network rule) */
static const unsigned int MAX_BLOCK_SIZE = 1000000;
static unsigned int MAX_BLOCK_SIZE = 1000000;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
static unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 100;

Expand Down
4 changes: 4 additions & 0 deletions src/init.cpp
Expand Up @@ -35,6 +35,7 @@
#include "utilmoneystr.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#include "blocksizecalculator.h"
#ifdef ENABLE_WALLET
#include "wallet/db.h"
#include "wallet/wallet.h"
Expand Down Expand Up @@ -1593,6 +1594,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
MilliSleep(10);
}

// ********************************************************* Step 10a: Compute starting block size
BlockSizeCalculator::ComputeBlockSize();

// ********************************************************* Step 11: start node

if (!CheckDiskSpace())
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Expand Up @@ -34,6 +34,7 @@
#include "utilmoneystr.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#include "blocksizecalculator.h"

#include <sstream>

Expand Down Expand Up @@ -4915,6 +4916,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CInv inv(MSG_BLOCK, block.GetHash());
LogPrint("net", "received block %s peer=%d\n", inv.hash.ToString(), pfrom->id);

BlockSizeCalculator::ComputeBlockSize();

pfrom->AddInventoryKnown(inv);

CValidationState state;
Expand Down