Browse Source

WIP: merged mining.

cn_merge
Jianping Wu 6 years ago
parent
commit
1aa390ff47
  1. 8
      src/primitives/block.cpp
  2. 28
      src/primitives/block.h
  3. 9
      src/rpc/mining.cpp

8
src/primitives/block.cpp

@ -46,7 +46,7 @@ uint256 CBlockHeader::GetPoWHash() const
// Merged mining. // Merged mining.
cryptonote::tx_extra_keva_blockhash keva_blockhash; cryptonote::tx_extra_keva_blockhash keva_blockhash;
if (!cryptonote::get_keva_blockhash_from_extra(cnHeader.aux_pow->miner_tx.extra, keva_blockhash)) { if (!cryptonote::get_keva_blockhash_from_extra(cnHeader.aux_pow_ptr->miner_tx.extra, keva_blockhash)) {
return DIFFICULTY_1; return DIFFICULTY_1;
} }
uint256 actualHash = GetHash(); uint256 actualHash = GetHash();
@ -55,14 +55,14 @@ uint256 CBlockHeader::GetPoWHash() const
} }
crypto::hash miner_tx_hash; crypto::hash miner_tx_hash;
if (!cryptonote::get_transaction_hash(cnHeader.aux_pow->miner_tx, miner_tx_hash)) { if (!cryptonote::get_transaction_hash(cnHeader.aux_pow_ptr->miner_tx, miner_tx_hash)) {
return DIFFICULTY_1; return DIFFICULTY_1;
} }
crypto::hash aux_blocks_merkle_root; crypto::hash aux_blocks_merkle_root;
crypto::tree_hash_from_branch( crypto::tree_hash_from_branch(
reinterpret_cast<const char (*)[32]>(cnHeader.aux_pow->merkle_branch.data()), reinterpret_cast<const char (*)[32]>(cnHeader.aux_pow_ptr->merkle_branch.data()),
cnHeader.aux_pow->merkle_branch.size(), cnHeader.aux_pow_ptr->merkle_branch.size(),
reinterpret_cast<char*>(&miner_tx_hash), 0, reinterpret_cast<char*>(&miner_tx_hash), 0,
reinterpret_cast<char*>(&aux_blocks_merkle_root)); reinterpret_cast<char*>(&aux_blocks_merkle_root));

28
src/primitives/block.h

@ -9,6 +9,7 @@
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <serialize.h> #include <serialize.h>
#include <uint256.h> #include <uint256.h>
#include <utilstrencodings.h>
#include <cryptonote_basic/cryptonote_format_utils.h> #include <cryptonote_basic/cryptonote_format_utils.h>
@ -22,27 +23,6 @@ public:
// merkel tree whose root is merkle_root. // merkel tree whose root is merkle_root.
std::vector<crypto::hash> merkle_branch; std::vector<crypto::hash> merkle_branch;
uint256 CheckMerkleBranch (uint256 hash,
const std::vector<uint256>& vMerkleBranch,
int nIndex)
{
if (nIndex == -1) {
return uint256();
}
#if 0
for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin());
it != vMerkleBranch.end(); ++it) {
if (nIndex & 1) {
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
} else {
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
}
nIndex >>= 1;
}
return hash;
#endif
}
// load // load
template <template <bool> class Archive> template <template <bool> class Archive>
bool do_serialize(Archive<false>& ar) bool do_serialize(Archive<false>& ar)
@ -82,7 +62,7 @@ private:
bool is_aux_block; bool is_aux_block;
public: public:
std::unique_ptr<CAuxPow> aux_pow; // It is used only is_aux_block is true. std::shared_ptr<CAuxPow> aux_pow_ptr; // It is used only is_aux_block is true.
CryptoNoteHeader() : is_aux_block(false) CryptoNoteHeader() : is_aux_block(false)
{ {
@ -131,7 +111,7 @@ public:
memcpy(merkle_root.begin(), &merkle_hash, merkle_root.size()); memcpy(merkle_root.begin(), &merkle_hash, merkle_root.size());
VARINT_FIELD(nTxes) VARINT_FIELD(nTxes)
if (is_aux_block) { if (is_aux_block) {
aux_pow_ptr = std::make_unique<CAuxPow>(); aux_pow_ptr = std::shared_ptr<CAuxPow>(new CAuxPow());
FIELD(*aux_pow_ptr) FIELD(*aux_pow_ptr)
} }
return true; return true;
@ -153,7 +133,7 @@ public:
FIELD(merkle_hash) FIELD(merkle_hash)
VARINT_FIELD(nTxes) VARINT_FIELD(nTxes)
if (is_aux_block) { if (is_aux_block) {
FIELD(*aux_pow) FIELD(*aux_pow_ptr)
} }
return true; return true;
} }

9
src/rpc/mining.cpp

@ -621,7 +621,7 @@ static uint256 CryptoHashToUint256(const crypto::hash& hash)
// Cryptonote RPC call, only one parameter allowed. // Cryptonote RPC call, only one parameter allowed.
UniValue submitblock(const JSONRPCRequest& request) UniValue submitblock(const JSONRPCRequest& request)
{ {
if (request.fHelp || request.params.size() < 2) { if (request.fHelp || request.params.size() > 2 || request.params.size() < 1) {
throw std::runtime_error( throw std::runtime_error(
"submitblock \"hexdata\"\n" "submitblock \"hexdata\"\n"
"\nAttempts to submit new block to network.\n" "\nAttempts to submit new block to network.\n"
@ -637,7 +637,7 @@ UniValue submitblock(const JSONRPCRequest& request)
); );
} }
std::unique_ptr<CAuxPow> auxPow = std::make_unique<CAuxPow>(); std::shared_ptr<CAuxPow> auxPow;
bool isAuxPow = false; bool isAuxPow = false;
if (request.params.size() == 2) { if (request.params.size() == 2) {
if (request.params[1].getType() != UniValue::VSTR) { if (request.params[1].getType() != UniValue::VSTR) {
@ -647,6 +647,7 @@ UniValue submitblock(const JSONRPCRequest& request)
ss << request.params[1].get_str(); ss << request.params[1].get_str();
// load // load
binary_archive<false> ba(ss); binary_archive<false> ba(ss);
auxPow = std::shared_ptr<CAuxPow>(new CAuxPow());
isAuxPow = ::serialization::serialize(ba, *auxPow); isAuxPow = ::serialization::serialize(ba, *auxPow);
} }
@ -698,7 +699,7 @@ UniValue submitblock(const JSONRPCRequest& request)
block.cnHeader.SetAuxBlock(isAuxPow); block.cnHeader.SetAuxBlock(isAuxPow);
if (isAuxPow) { if (isAuxPow) {
block.cnHeader.aux_pow = std::move(auxPow); block.cnHeader.aux_pow_ptr = auxPow;
} }
// TODO: fix the return message. // TODO: fix the return message.
@ -954,7 +955,7 @@ static const CRPCCommand commands[] =
{ "mining", "getmininginfo", &getmininginfo, {} }, { "mining", "getmininginfo", &getmininginfo, {} },
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} }, { "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
{ "mining", "getblocktemplate", &getblocktemplate, {"reserve_size", "wallet_address", "is_aux_block"} }, { "mining", "getblocktemplate", &getblocktemplate, {"reserve_size", "wallet_address", "is_aux_block"} },
{ "mining", "submitblock", &submitblock, {"hexdata","auxpow"} }, { "mining", "submitblock", &submitblock, {"hexdata", "auxpow"} },
{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} }, { "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },

Loading…
Cancel
Save