bool BaseFunctionalTests::prepareAndSubmitBlock(TestNode& node, CryptoNote::Block&& blockTemplate) { blockTemplate.timestamp = m_nextTimestamp; m_nextTimestamp += 2 * m_currency.difficultyTarget(); BinaryArray blockBlob = CryptoNote::toBinaryArray(blockTemplate); return node.submitBlock(::Common::toHex(blockBlob.data(), blockBlob.size())); }
bool RpcServer::on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res) { std::vector<Hash> vh; for (const auto& tx_hex_str : req.txs_hashes) { BinaryArray b; if (!fromHex(tx_hex_str, b)) { res.status = "Failed to parse hex representation of transaction hash"; return true; } if (b.size() != sizeof(Hash)) { res.status = "Failed, size of data mismatch"; } vh.push_back(*reinterpret_cast<const Hash*>(b.data())); } std::list<Hash> missed_txs; std::list<Transaction> txs; m_core.getTransactions(vh, txs, missed_txs); for (auto& tx : txs) { res.txs_as_hex.push_back(toHex(toBinaryArray(tx))); } for (const auto& miss_tx : missed_txs) { res.missed_tx.push_back(Common::podToHex(miss_tx)); } res.status = CORE_RPC_STATUS_OK; return true; }
bool parseAndValidateTransactionFromBinaryArray(const BinaryArray& tx_blob, Transaction& tx, Hash& tx_hash, Hash& tx_prefix_hash) { if (!fromBinaryArray(tx, tx_blob)) { return false; } //TODO: validate tx cn_fast_hash(tx_blob.data(), tx_blob.size(), tx_hash); getObjectHash(*static_cast<TransactionPrefix*>(&tx), tx_prefix_hash); return true; }
void LevinProtocol::sendReply(uint32_t command, const BinaryArray& out, int32_t returnCode) { bucket_head2 head = { 0 }; head.m_signature = LEVIN_SIGNATURE; head.m_cb = out.size(); head.m_have_to_return_data = false; head.m_command = command; head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_flags = LEVIN_PACKET_RESPONSE; head.m_return_code = returnCode; BinaryArray writeBuffer; writeBuffer.reserve(sizeof(head) + out.size()); Common::VectorOutputStream stream(writeBuffer); stream.writeSome(&head, sizeof(head)); stream.writeSome(out.data(), out.size()); writeStrict(writeBuffer.data(), writeBuffer.size()); }
void LevinProtocol::sendMessage(uint32_t command, const BinaryArray& out, bool needResponse) { bucket_head2 head = { 0 }; head.m_signature = LEVIN_SIGNATURE; head.m_cb = out.size(); head.m_have_to_return_data = needResponse; head.m_command = command; head.m_protocol_version = LEVIN_PROTOCOL_VER_1; head.m_flags = LEVIN_PACKET_REQUEST; // write header and body in one operation BinaryArray writeBuffer; writeBuffer.reserve(sizeof(head) + out.size()); Common::VectorOutputStream stream(writeBuffer); stream.writeSome(&head, sizeof(head)); stream.writeSome(out.data(), out.size()); writeStrict(writeBuffer.data(), writeBuffer.size()); }
bool appendMergeMiningTagToExtra(std::vector<uint8_t>& tx_extra, const TransactionExtraMergeMiningTag& mm_tag) { BinaryArray blob; if (!toBinaryArray(mm_tag, blob)) { return false; } tx_extra.push_back(TX_EXTRA_MERGE_MINING_TAG); std::copy(reinterpret_cast<const uint8_t*>(blob.data()), reinterpret_cast<const uint8_t*>(blob.data() + blob.size()), std::back_inserter(tx_extra)); return true; }
bool append_message_to_extra(std::vector<uint8_t>& tx_extra, const tx_extra_message& message) { BinaryArray blob; if (!toBinaryArray(message, blob)) { return false; } tx_extra.reserve(tx_extra.size() + 1 + blob.size()); tx_extra.push_back(TX_EXTRA_MESSAGE_TAG); std::copy(reinterpret_cast<const uint8_t*>(blob.data()), reinterpret_cast<const uint8_t*>(blob.data() + blob.size()), std::back_inserter(tx_extra)); return true; }
bool get_block_longhash(cn_context &context, const Block& b, Hash& res) { BinaryArray bd; if (b.majorVersion == BLOCK_MAJOR_VERSION_1 || b.majorVersion >= BLOCK_MAJOR_VERSION_4) { if (!get_block_hashing_blob(b, bd)) { return false; } } else if (b.majorVersion == BLOCK_MAJOR_VERSION_2 || b.majorVersion == BLOCK_MAJOR_VERSION_3) { if (!get_parent_block_hashing_blob(b, bd)) { return false; } } else { return false; } cn_slow_hash(context, bd.data(), bd.size(), res); return true; }
bool addExtraNonceToTransactionExtra(std::vector<uint8_t>& tx_extra, const BinaryArray& extra_nonce) { if (extra_nonce.size() > TX_EXTRA_NONCE_MAX_COUNT) { return false; } size_t start_pos = tx_extra.size(); tx_extra.resize(tx_extra.size() + 2 + extra_nonce.size()); //write tag tx_extra[start_pos] = TX_EXTRA_NONCE; //write len ++start_pos; tx_extra[start_pos] = static_cast<uint8_t>(extra_nonce.size()); //write data ++start_pos; memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size()); return true; }
bool BaseFunctionalTests::prepareAndSubmitBlock(TestNode& node, CryptoNote::Block&& blockTemplate) { blockTemplate.timestamp = m_nextTimestamp; m_nextTimestamp += 2 * m_currency.difficultyTarget(); if (blockTemplate.majorVersion >= BLOCK_MAJOR_VERSION_2) { blockTemplate.parentBlock.majorVersion = BLOCK_MAJOR_VERSION_1; blockTemplate.parentBlock.minorVersion = BLOCK_MINOR_VERSION_0; blockTemplate.parentBlock.transactionCount = 1; CryptoNote::TransactionExtraMergeMiningTag mmTag; mmTag.depth = 0; if (!CryptoNote::get_aux_block_header_hash(blockTemplate, mmTag.merkleRoot)) { return false; } blockTemplate.parentBlock.baseTransaction.extra.clear(); if (!CryptoNote::appendMergeMiningTagToExtra(blockTemplate.parentBlock.baseTransaction.extra, mmTag)) { return false; } } BinaryArray blockBlob = CryptoNote::toBinaryArray(blockTemplate); return node.submitBlock(::Common::toHex(blockBlob.data(), blockBlob.size())); }
void getBinaryArrayHash(const BinaryArray& binaryArray, Crypto::Hash& hash) { cn_fast_hash(binaryArray.data(), binaryArray.size(), hash); }