Merge remote-tracking branch 'zcash/master' into dPoW
This commit is contained in:
31
src/gtest/test_checkblock.cpp
Normal file
31
src/gtest/test_checkblock.cpp
Normal file
@@ -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));
|
||||
}
|
||||
@@ -94,6 +94,16 @@ TEST(checktransaction_tests, valid_transaction) {
|
||||
EXPECT_TRUE(CheckTransactionWithoutProofVerification(tx, state));
|
||||
}
|
||||
|
||||
TEST(checktransaction_tests, BadVersionTooLow) {
|
||||
CMutableTransaction mtx = GetValidTransaction();
|
||||
mtx.nVersion = 0;
|
||||
|
||||
CTransaction tx(mtx);
|
||||
MockCValidationState state;
|
||||
EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-txns-version-too-low", false)).Times(1);
|
||||
CheckTransactionWithoutProofVerification(tx, state);
|
||||
}
|
||||
|
||||
TEST(checktransaction_tests, bad_txns_vin_empty) {
|
||||
CMutableTransaction mtx = GetValidTransaction();
|
||||
mtx.vjoinsplit.resize(0);
|
||||
|
||||
27
src/gtest/test_random.cpp
Normal file
27
src/gtest/test_random.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "random.h"
|
||||
|
||||
extern int GenZero(int n);
|
||||
extern int GenMax(int n);
|
||||
|
||||
TEST(Random, MappedShuffle) {
|
||||
std::vector<int> a {8, 4, 6, 3, 5};
|
||||
std::vector<int> m {0, 1, 2, 3, 4};
|
||||
|
||||
auto a1 = a;
|
||||
auto m1 = m;
|
||||
MappedShuffle(a1.begin(), m1.begin(), a1.size(), GenZero);
|
||||
std::vector<int> ea1 {4, 6, 3, 5, 8};
|
||||
std::vector<int> em1 {1, 2, 3, 4, 0};
|
||||
EXPECT_EQ(ea1, a1);
|
||||
EXPECT_EQ(em1, m1);
|
||||
|
||||
auto a2 = a;
|
||||
auto m2 = m;
|
||||
MappedShuffle(a2.begin(), m2.begin(), a2.size(), GenMax);
|
||||
std::vector<int> ea2 {8, 4, 6, 3, 5};
|
||||
std::vector<int> em2 {0, 1, 2, 3, 4};
|
||||
EXPECT_EQ(ea2, a2);
|
||||
EXPECT_EQ(em2, m2);
|
||||
}
|
||||
85
src/gtest/test_transaction.cpp
Normal file
85
src/gtest/test_transaction.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "primitives/transaction.h"
|
||||
#include "zcash/Note.hpp"
|
||||
#include "zcash/Address.hpp"
|
||||
|
||||
extern ZCJoinSplit* params;
|
||||
extern int GenZero(int n);
|
||||
extern int GenMax(int n);
|
||||
|
||||
TEST(Transaction, JSDescriptionRandomized) {
|
||||
// construct a merkle tree
|
||||
ZCIncrementalMerkleTree merkleTree;
|
||||
|
||||
libzcash::SpendingKey k = libzcash::SpendingKey::random();
|
||||
libzcash::PaymentAddress addr = k.address();
|
||||
|
||||
libzcash::Note note(addr.a_pk, 100, uint256(), uint256());
|
||||
|
||||
// commitment from coin
|
||||
uint256 commitment = note.cm();
|
||||
|
||||
// insert commitment into the merkle tree
|
||||
merkleTree.append(commitment);
|
||||
|
||||
// compute the merkle root we will be working with
|
||||
uint256 rt = merkleTree.root();
|
||||
|
||||
auto witness = merkleTree.witness();
|
||||
|
||||
// create JSDescription
|
||||
uint256 pubKeyHash;
|
||||
boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs = {
|
||||
libzcash::JSInput(witness, note, k),
|
||||
libzcash::JSInput() // dummy input of zero value
|
||||
};
|
||||
boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs = {
|
||||
libzcash::JSOutput(addr, 50),
|
||||
libzcash::JSOutput(addr, 50)
|
||||
};
|
||||
boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
|
||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
|
||||
|
||||
{
|
||||
auto jsdesc = JSDescription::Randomized(
|
||||
*params, pubKeyHash, rt,
|
||||
inputs, outputs,
|
||||
inputMap, outputMap,
|
||||
0, 0, false);
|
||||
|
||||
std::set<size_t> inputSet(inputMap.begin(), inputMap.end());
|
||||
std::set<size_t> expectedInputSet {0, 1};
|
||||
EXPECT_EQ(expectedInputSet, inputSet);
|
||||
|
||||
std::set<size_t> outputSet(outputMap.begin(), outputMap.end());
|
||||
std::set<size_t> expectedOutputSet {0, 1};
|
||||
EXPECT_EQ(expectedOutputSet, outputSet);
|
||||
}
|
||||
|
||||
{
|
||||
auto jsdesc = JSDescription::Randomized(
|
||||
*params, pubKeyHash, rt,
|
||||
inputs, outputs,
|
||||
inputMap, outputMap,
|
||||
0, 0, false, GenZero);
|
||||
|
||||
boost::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {1, 0};
|
||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {1, 0};
|
||||
EXPECT_EQ(expectedInputMap, inputMap);
|
||||
EXPECT_EQ(expectedOutputMap, outputMap);
|
||||
}
|
||||
|
||||
{
|
||||
auto jsdesc = JSDescription::Randomized(
|
||||
*params, pubKeyHash, rt,
|
||||
inputs, outputs,
|
||||
inputMap, outputMap,
|
||||
0, 0, false, GenMax);
|
||||
|
||||
boost::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {0, 1};
|
||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {0, 1};
|
||||
EXPECT_EQ(expectedInputMap, inputMap);
|
||||
EXPECT_EQ(expectedOutputMap, outputMap);
|
||||
}
|
||||
}
|
||||
13
src/gtest/utils.cpp
Normal file
13
src/gtest/utils.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "zcash/JoinSplit.hpp"
|
||||
|
||||
ZCJoinSplit* params = ZCJoinSplit::Unopened();
|
||||
|
||||
int GenZero(int n)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int GenMax(int n)
|
||||
{
|
||||
return n-1;
|
||||
}
|
||||
Reference in New Issue
Block a user