Browse Source

WIP: fixed block serialization.

cn_merge
Jianping Wu 6 years ago
parent
commit
9e442eb109
  1. 13
      src/primitives/block.cpp
  2. 50
      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
uint256 CBlockHeader::GetHash() const 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 uint256 CBlockHeader::GetPoWHash() const
@ -33,13 +35,10 @@ uint256 CBlockHeader::GetPoWHash() const
// following XOR operatiosn, the value of expectedHash is not changed. // following XOR operatiosn, the value of expectedHash is not changed.
blockHash ^= expectedHash; blockHash ^= expectedHash;
expectedHash ^= blockHash; expectedHash ^= blockHash;
CryptoNoteHeader header = cnHeader; CryptoNoteHeader cnHeaderCompute = cnHeader;
// Cryptonote prev_id is used to store kevacoin block hash. // Cryptonote prev_id is used to store kevacoin block hash.
header.prev_id = ArithToUint256(expectedHash); cnHeaderCompute.prev_id = ArithToUint256(expectedHash);
cryptonote::blobdata blob = cryptonote::t_serializable_object_to_blob(cnHeaderCompute);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << header;
std::string blob = stream.str();
cn_slow_hash(blob.data(), blob.size(), BEGIN(thash), 2, 0, 0); cn_slow_hash(blob.data(), blob.size(), BEGIN(thash), 2, 0, 0);
return thash; return thash;
} }

50
src/primitives/block.h

@ -49,7 +49,28 @@ public:
return (timestamp == 0); 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(major_version)
VARINT_FIELD(minor_version) VARINT_FIELD(minor_version)
VARINT_FIELD(timestamp) VARINT_FIELD(timestamp)
@ -57,16 +78,33 @@ public:
memcpy(&prev_hash, prev_id.begin(), prev_id.size()); memcpy(&prev_hash, prev_id.begin(), prev_id.size());
FIELD(prev_hash) FIELD(prev_hash)
FIELD(nonce) 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; ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) { inline void SerializationOp(Stream& s, Operation ser_action) {
std::string blob = cryptonote::t_serializable_object_to_blob(*this); if (ser_action.ForRead()) {
blob.append(reinterpret_cast<const char*>(merkle_root.begin()), merkle_root.size()); std::string blob;
blob.append(tools::get_varint_data(nTxes + 1)); READWRITE(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);
}
} }
}; };

2
src/rpc/mining.cpp

@ -661,7 +661,7 @@ UniValue submitblock(const JSONRPCRequest& request)
block.cnHeader.prev_id = CryptoHashToUint256(cnblock.prev_id); block.cnHeader.prev_id = CryptoHashToUint256(cnblock.prev_id);
block.cnHeader.nonce = cnblock.nonce; 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); block.cnHeader.merkle_root = CryptoHashToUint256(tree_root_hash);
uint256 hash = block.GetHash(); uint256 hash = block.GetHash();

Loading…
Cancel
Save