hash the value of a node before computing the hash of the node, to prevent a false value from being crafted which will result in the same node hash yet mask the existence of child nodes

This commit is contained in:
Jimmy Kiselak 2015-02-23 17:51:52 -05:00
parent 3e6efa8a2f
commit bc266a1677
2 changed files with 32 additions and 20 deletions

View file

@ -137,14 +137,6 @@ bool CNCCTrie::recursiveCheckConsistency(CNCCTrieNode* node)
{
std::string stringToHash;
CNodeValue val;
bool hasValue = node->getValue(val);
if (hasValue)
{
stringToHash += val.ToString();
}
for (nodeMapType::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
std::stringstream ss;
@ -157,6 +149,20 @@ bool CNCCTrie::recursiveCheckConsistency(CNCCTrieNode* node)
else
return false;
}
CNodeValue val;
bool hasValue = node->getValue(val);
if (hasValue)
{
CHash256 valHasher;
std::vector<unsigned char> vchValHash(valHasher.OUTPUT_SIZE);
valHasher.Write((const unsigned char*) val.ToString().data(), val.ToString().size());
valHasher.Finalize(&(vchValHash[0]));
uint256 valHash(vchValHash);
stringToHash += valHash.ToString();
}
CHash256 hasher;
std::vector<unsigned char> vchHash(hasher.OUTPUT_SIZE);
hasher.Write((const unsigned char*) stringToHash.data(), stringToHash.size());
@ -383,14 +389,6 @@ bool CNCCTrieCache::recursiveComputeMerkleHash(CNCCTrieNode* tnCurrent, std::str
}
std::string stringToHash;
CNodeValue val;
bool hasValue = tnCurrent->getValue(val);
if (hasValue)
{
stringToHash += val.ToString();
}
nodeCacheType::iterator cachedNode;
@ -415,6 +413,20 @@ bool CNCCTrieCache::recursiveComputeMerkleHash(CNCCTrieNode* tnCurrent, std::str
else
stringToHash += it->second->hash.ToString();
}
CNodeValue val;
bool hasValue = tnCurrent->getValue(val);
if (hasValue)
{
CHash256 valHasher;
std::vector<unsigned char> vchValHash(valHasher.OUTPUT_SIZE);
valHasher.Write((const unsigned char*) val.ToString().data(), val.ToString().size());
valHasher.Finalize(&(vchValHash[0]));
uint256 valHash(vchValHash);
stringToHash += valHash.ToString();
}
CHash256 hasher;
std::vector<unsigned char> vchHash(hasher.OUTPUT_SIZE);
hasher.Write((const unsigned char*) stringToHash.data(), stringToHash.size());

View file

@ -42,16 +42,16 @@ BOOST_AUTO_TEST_CASE(ncctrie_create_insert_remove)
CMutableTransaction tx6 = BuildTransaction(tx5.GetHash());
uint256 hash1;
hash1.SetHex("81727ef4dd44787941b9bba2c143a3607d92ee1f0d1d24d0da5aac6aa44ae12f");
hash1.SetHex("2f3ae25e923dfa6fc24f0ff5d4367239425effaf47b703857436947a2dbfdda1");
uint256 hash2;
hash2.SetHex("653304cb35e66280b52ee6c52fcf2b8b1032ced6fea7a6e1548f24c47b1a3910");
hash2.SetHex("833c0a897d1e32b16a7988c574dbcd750ed6695b947dea7b16e7782eac070a5d");
uint256 hash3;
hash3.SetHex("48c0f04ab06338b25c66a6f237084eec49a5d761b5bfbe125d213fce33948242");
hash3.SetHex("5ea4c62fb56bbca0cdf6fe4b2668c737641b4f48af8ef5041faa42df12aa3078");
uint256 hash4;
hash4.SetHex("a79e8a5b28f7fa5e8836a4b48da9988bdf56ce749f81f413cb754f963a516200");
hash4.SetHex("47ae7f3ab9c0e9b13d08d5535a76fd36cecad0529baca390cea77b5bd3d99290");
BOOST_CHECK(pnccTrie->empty());