Browse Source

WIP: fixed block serialization.

cn_merge
Jianping Wu 6 years ago
parent
commit
9e442eb109
  1. 13
      src/primitives/block.cpp
  2. 48
      src/primitives/block.h
  3. 2
      src/rpc/mining.cpp

13
src/primitives/block.cpp

@ -16,7 +16,9 @@ extern "C" void cn_slow_hash(const void *data, size_t length, char *hash, int va @@ -16,7 +16,9 @@ extern "C" void cn_slow_hash(const void *data, size_t length, char *hash, int va
uint256 CBlockHeader::GetHash() const
{
return SerializeHash(*this);
CHashWriter hashWriter(SER_GETHASH, PROTOCOL_VERSION);
hashWriter.write(BEGIN(nVersion), 80);
return hashWriter.GetHash();
}
uint256 CBlockHeader::GetPoWHash() const
@ -33,13 +35,10 @@ uint256 CBlockHeader::GetPoWHash() const @@ -33,13 +35,10 @@ uint256 CBlockHeader::GetPoWHash() const
// following XOR operatiosn, the value of expectedHash is not changed.
blockHash ^= expectedHash;
expectedHash ^= blockHash;
CryptoNoteHeader header = cnHeader;
CryptoNoteHeader cnHeaderCompute = cnHeader;
// Cryptonote prev_id is used to store kevacoin block hash.
header.prev_id = ArithToUint256(expectedHash);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << header;
std::string blob = stream.str();
cnHeaderCompute.prev_id = ArithToUint256(expectedHash);
cryptonote::blobdata blob = cryptonote::t_serializable_object_to_blob(cnHeaderCompute);
cn_slow_hash(blob.data(), blob.size(), BEGIN(thash), 2, 0, 0);
return thash;
}

48
src/primitives/block.h

@ -49,7 +49,28 @@ public: @@ -49,7 +49,28 @@ public:
return (timestamp == 0);
}
BEGIN_SERIALIZE()
// load
template <template <bool> class Archive>
bool do_serialize(Archive<false>& ar)
{
VARINT_FIELD(major_version)
VARINT_FIELD(minor_version)
VARINT_FIELD(timestamp)
crypto::hash prev_hash;
FIELD(prev_hash)
memcpy(prev_id.begin(), &prev_hash, prev_id.size());
FIELD(nonce)
crypto::hash merkle_hash;
FIELD(merkle_hash)
memcpy(merkle_root.begin(), &merkle_hash, merkle_root.size());
VARINT_FIELD(nTxes)
return true;
}
// store
template <template <bool> class Archive>
bool do_serialize(Archive<true>& ar)
{
VARINT_FIELD(major_version)
VARINT_FIELD(minor_version)
VARINT_FIELD(timestamp)
@ -57,17 +78,34 @@ public: @@ -57,17 +78,34 @@ public:
memcpy(&prev_hash, prev_id.begin(), prev_id.size());
FIELD(prev_hash)
FIELD(nonce)
END_SERIALIZE()
crypto::hash merkle_hash;
memcpy(&merkle_hash, merkle_root.begin(), merkle_root.size());
FIELD(merkle_hash)
VARINT_FIELD(nTxes)
return true;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
std::string blob = cryptonote::t_serializable_object_to_blob(*this);
blob.append(reinterpret_cast<const char*>(merkle_root.begin()), merkle_root.size());
blob.append(tools::get_varint_data(nTxes + 1));
if (ser_action.ForRead()) {
std::string blob;
READWRITE(blob);
std::stringstream ss;
ss << blob;
// load
binary_archive<false> ba(ss);
::serialization::serialize(ba, *this);
} else {
std::stringstream ss;
// store
binary_archive<true> ba(ss);
::serialization::serialize(ba, *this);
std::string blob = ss.str();
READWRITE(blob);
}
}
};
/** Nodes collect new transactions into a block, hash them into a hash tree,

2
src/rpc/mining.cpp

@ -661,7 +661,7 @@ UniValue submitblock(const JSONRPCRequest& request) @@ -661,7 +661,7 @@ UniValue submitblock(const JSONRPCRequest& request)
block.cnHeader.prev_id = CryptoHashToUint256(cnblock.prev_id);
block.cnHeader.nonce = cnblock.nonce;
crypto::hash tree_root_hash = get_tx_tree_hash(cnblock);
crypto::hash tree_root_hash = cryptonote::get_tx_tree_hash(cnblock);
block.cnHeader.merkle_root = CryptoHashToUint256(tree_root_hash);
uint256 hash = block.GetHash();

Loading…
Cancel
Save