Skip to content

Commit

Permalink
Reject block versions lower than 4
Browse files Browse the repository at this point in the history
Closes zcash#1556
  • Loading branch information
str4d committed Oct 22, 2016
1 parent 7ac924c commit 80f4cdc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.gtest.include
Expand Up @@ -5,6 +5,7 @@ bin_PROGRAMS += zcash-gtest
zcash_gtest_SOURCES = \
gtest/main.cpp \
gtest/utils.cpp \
gtest/test_checkblock.cpp \
gtest/test_checktransaction.cpp \
gtest/json_test_vectors.cpp \
gtest/json_test_vectors.h \
Expand Down
2 changes: 2 additions & 0 deletions src/consensus/consensus.h
Expand Up @@ -6,6 +6,8 @@
#ifndef BITCOIN_CONSENSUS_CONSENSUS_H
#define BITCOIN_CONSENSUS_CONSENSUS_H

/** The minimum allowed block version (network rule) */
static const int32_t MIN_BLOCK_VERSION = 4;
/** The minimum allowed transaction version (network rule) */
static const int32_t MIN_TX_VERSION = 1;
/** The maximum allowed size for a serialized block, in bytes (network rule) */
Expand Down
31 changes: 31 additions & 0 deletions src/gtest/test_checkblock.cpp
@@ -0,0 +1,31 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "consensus/validation.h"
#include "main.h"

class MockCValidationState : public CValidationState {
public:
MOCK_METHOD5(DoS, bool(int level, bool ret,
unsigned char chRejectCodeIn, std::string strRejectReasonIn,
bool corruptionIn));
MOCK_METHOD3(Invalid, bool(bool ret,
unsigned char _chRejectCode, std::string _strRejectReason));
MOCK_METHOD1(Error, bool(std::string strRejectReasonIn));
MOCK_CONST_METHOD0(IsValid, bool());
MOCK_CONST_METHOD0(IsInvalid, bool());
MOCK_CONST_METHOD0(IsError, bool());
MOCK_CONST_METHOD1(IsInvalid, bool(int &nDoSOut));
MOCK_CONST_METHOD0(CorruptionPossible, bool());
MOCK_CONST_METHOD0(GetRejectCode, unsigned char());
MOCK_CONST_METHOD0(GetRejectReason, std::string());
};

TEST(CheckBlock, VersionTooLow) {
CBlock block;
block.nVersion = 1;

MockCValidationState state;
EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "version-too-low", false)).Times(1);
EXPECT_FALSE(CheckBlock(block, state, false, false));
}
5 changes: 5 additions & 0 deletions src/main.cpp
Expand Up @@ -2940,6 +2940,11 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne

bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
{
// Check block version
if (block.nVersion < MIN_BLOCK_VERSION)
return state.DoS(100, error("CheckBlockHeader(): block version too low"),
REJECT_INVALID, "version-too-low");

// Check Equihash solution is valid
if (fCheckPOW && !CheckEquihashSolution(&block, Params()))
return state.DoS(100, error("CheckBlockHeader(): Equihash solution invalid"),
Expand Down

0 comments on commit 80f4cdc

Please sign in to comment.