//it's always protected with mutex std::error_code InProcessNode::doRelayTransaction(const CryptoNote::Transaction& transaction) { { std::unique_lock<std::mutex> lock(mutex); if (state != INITIALIZED) { return make_error_code(CryptoNote::error::NOT_INITIALIZED); } } try { CryptoNote::BinaryArray transactionBinaryArray = toBinaryArray(transaction); CryptoNote::tx_verification_context tvc = boost::value_initialized<CryptoNote::tx_verification_context>(); if (!core.handle_incoming_tx(transactionBinaryArray, tvc, false)) { return make_error_code(CryptoNote::error::REQUEST_ERROR); } if(tvc.m_verifivation_failed) { return make_error_code(CryptoNote::error::REQUEST_ERROR); } if(!tvc.m_should_be_relayed) { return make_error_code(CryptoNote::error::REQUEST_ERROR); } CryptoNote::NOTIFY_NEW_TRANSACTIONS::request r; r.txs.push_back(asString(transactionBinaryArray)); core.get_protocol()->relay_transactions(r); } catch (std::system_error& e) { return e.code(); } catch (std::exception&) { return make_error_code(CryptoNote::error::INTERNAL_NODE_ERROR); } return std::error_code(); }
std::unique_ptr<IMainChainStorage> createVectorMainChainStorage(const Currency& currency) { std::unique_ptr<IMainChainStorage> storage(new VectorMainChainStorage()); RawBlock genesis; genesis.block = toBinaryArray(currency.genesisBlock()); storage->pushBlock(genesis); return std::move(storage); }
//it's always protected with mutex std::error_code InProcessNode::doGetNewBlocks(std::vector<Crypto::Hash>&& knownBlockIds, std::vector<CryptoNote::block_complete_entry>& newBlocks, uint32_t& startHeight) { { std::unique_lock<std::mutex> lock(mutex); if (state != INITIALIZED) { return make_error_code(CryptoNote::error::NOT_INITIALIZED); } } try { // TODO code duplication see RpcServer::on_get_blocks() if (knownBlockIds.empty()) { return make_error_code(CryptoNote::error::REQUEST_ERROR); } if (knownBlockIds.back() != core.getBlockIdByHeight(0)) { return make_error_code(CryptoNote::error::REQUEST_ERROR); } uint32_t totalBlockCount; std::vector<Crypto::Hash> supplement = core.findBlockchainSupplement(knownBlockIds, CryptoNote::COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT, totalBlockCount, startHeight); for (const auto& blockId : supplement) { assert(core.have_block(blockId)); auto completeBlock = core.getBlock(blockId); assert(completeBlock != nullptr); CryptoNote::block_complete_entry be; be.block = asString(toBinaryArray(completeBlock->getBlock())); be.txs.reserve(completeBlock->getTransactionCount()); for (size_t i = 0; i < completeBlock->getTransactionCount(); ++i) { be.txs.push_back(asString(toBinaryArray(completeBlock->getTransaction(i)))); } newBlocks.push_back(std::move(be)); } } catch (std::system_error& e) { return e.code(); } catch (std::exception&) { return make_error_code(CryptoNote::error::INTERNAL_NODE_ERROR); } return std::error_code(); }
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 get_block_hashing_blob(const Block& b, BinaryArray& ba) { if (!toBinaryArray(static_cast<const BlockHeader&>(b), ba)) { return false; } Hash treeRootHash = get_tx_tree_hash(b); ba.insert(ba.end(), treeRootHash.data, treeRootHash.data + 32); auto transactionCount = asBinaryArray(Tools::get_varint_data(b.transactionHashes.size() + 1)); ba.insert(ba.end(), transactionCount.begin(), transactionCount.end()); 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_hash(const Block& b, Hash& res) { BinaryArray ba; if (!get_block_hashing_blob(b, ba)) { return false; } // The header of block version 1 differs from headers of blocks starting from v.2 if (BLOCK_MAJOR_VERSION_2 == b.majorVersion || BLOCK_MAJOR_VERSION_3 == b.majorVersion) { BinaryArray parent_blob; auto serializer = makeParentBlockSerializer(b, true, false); if (!toBinaryArray(serializer, parent_blob)) return false; ba.insert(ba.end(), parent_blob.begin(), parent_blob.end()); } return getObjectHash(ba, res); }
bool MinerManager::submitBlock(const Block& minedBlock, const std::string& daemonHost, uint16_t daemonPort) { try { HttpClient client(m_dispatcher, daemonHost, daemonPort); COMMAND_RPC_SUBMITBLOCK::request request; request.emplace_back(Common::toHex(toBinaryArray(minedBlock))); COMMAND_RPC_SUBMITBLOCK::response response; System::EventLock lk(m_httpEvent); JsonRpc::invokeJsonRpcCommand(client, "submitblock", request, response); m_logger(Logging::INFO) << "Block has been successfully submitted. Block hash: " << Common::podToHex(get_block_hash(minedBlock)); return true; } catch (std::exception& e) { m_logger(Logging::WARNING) << "Couldn't submit block: " << Common::podToHex(get_block_hash(minedBlock)) << ", reason: " << e.what(); return false; } }
BinaryArray TransactionPrefixImpl::getTransactionData() const { return toBinaryArray(m_txPrefix); }
//----------------------------------------------------------------------- std::string getAccountAddressAsStr(uint64_t prefix, const AccountPublicAddress& adr) { BinaryArray ba; bool r = toBinaryArray(adr, ba); assert(r); return Tools::Base58::encode_addr(prefix, Common::asString(ba)); }
bool get_parent_block_hashing_blob(const Block& b, BinaryArray& blob) { auto serializer = makeParentBlockSerializer(b, true, true); return toBinaryArray(serializer, blob); }