Skip to content

Commit

Permalink
[Tests] Fix arith in pedersen_hash_/skiplist_/pmt_/transaction_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 5, 2021
1 parent 86e177b commit 6488f24
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,6 @@ class arith_uint512 : public base_uint<512> {
/** End classes definitions */

const arith_uint256 ARITH_UINT256_ZERO = arith_uint256();
const arith_uint256 ARITH_UINT256_ONE = arith_uint256(1);

#endif // BITCOIN_UINT256_H
6 changes: 4 additions & 2 deletions src/test/librust/pedersen_hash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "test/test_pivx.h"

#include "arith_uint256.h"
#include "uint256.h"
#include "utilstrencodings.h"
#include "test/test_pivx.h"

#include <boost/test/unit_test.hpp>
#include <librustzcash.h>
Expand All @@ -26,7 +28,7 @@ BOOST_AUTO_TEST_CASE(pedersen_hash_testvectors)
BOOST_CHECK(result == expected_result);

// Simple bad scenario check
const uint256 c = uint256(1) + a;
const uint256& c = ArithToUint256(ARITH_UINT256_ONE + UintToArith256(a));
result.SetNull();
librustzcash_merkle_hash(25, c.begin(), b.begin(), result.begin());
BOOST_CHECK(result != expected_result);
Expand Down
3 changes: 1 addition & 2 deletions src/test/pmt_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree
void Damage() {
unsigned int n = InsecureRandRange(vHash.size());
int bit = InsecureRandBits(8);
uint256 &hash = vHash[n];
hash ^= ((uint256)1 << bit);
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/test/skiplist_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
std::vector<uint256> vHashMain(100000);
std::vector<CBlockIndex> vBlocksMain(100000);
for (unsigned int i=0; i<vBlocksMain.size(); i++) {
vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances.
vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
vBlocksMain[i].nHeight = i;
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : nullptr;
vBlocksMain[i].phashBlock = &vHashMain[i];
vBlocksMain[i].BuildSkip();
BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight);
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
BOOST_CHECK(vBlocksMain[i].pprev == nullptr || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
}

// Build a branch that splits off at block 49999, 50000 blocks long.
std::vector<uint256> vHashSide(50000);
std::vector<CBlockIndex> vBlocksSide(50000);
for (unsigned int i=0; i<vBlocksSide.size(); i++) {
vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
vHashSide[i] = ArithToUint256(i + 50000 + (ARITH_UINT256_ONE << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
vBlocksSide[i].nHeight = i + 50000;
vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
vBlocksSide[i].phashBlock = &vHashSide[i];
vBlocksSide[i].BuildSkip();
BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight);
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
BOOST_CHECK(vBlocksSide[i].pprev == nullptr || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
}

Expand All @@ -89,13 +89,13 @@ BOOST_AUTO_TEST_CASE(getlocator_test)

// Entries 1 through 11 (inclusive) go back one step each.
for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i);
BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i);
}

// The further ones (excluding the last one) go back with exponential steps.
unsigned int dist = 2;
for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist);
BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist);
dist *= 2;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/transaction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
break;
}

mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
mapprevOutScriptPubKeys[COutPoint(uint256S(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
}
if (!fValid)
{
Expand Down Expand Up @@ -202,7 +202,7 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
break;
}

mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
mapprevOutScriptPubKeys[COutPoint(uint256S(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
}
if (!fValid)
{
Expand Down

0 comments on commit 6488f24

Please sign in to comment.