From a704d8b851e7e29473d12aafb9b2ecd983750a8f Mon Sep 17 00:00:00 2001 From: root Date: Fri, 15 Jun 2018 15:52:05 -0500 Subject: [PATCH 01/12] add bc_explorer functions --- src/Rpc/RpcServer.cpp | 367 ++++++++++++++++++++++++++++++++++++++++++ src/Rpc/RpcServer.h | 7 + 2 files changed, 374 insertions(+) diff --git a/src/Rpc/RpcServer.cpp b/src/Rpc/RpcServer.cpp index 1bca07ae..11d0c5d6 100755 --- a/src/Rpc/RpcServer.cpp +++ b/src/Rpc/RpcServer.cpp @@ -130,6 +130,11 @@ bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse& jsonResponse.setId(jsonRequest.getId()); // copy id static std::unordered_map> jsonRpcHandlers = { + { "f_blocks_list_json", { makeMemberMethod(&RpcServer::f_on_blocks_list_json), false } }, + { "f_block_json", { makeMemberMethod(&RpcServer::f_on_block_json), false } }, + { "f_transaction_json", { makeMemberMethod(&RpcServer::f_on_transaction_json), false } }, + { "f_on_transactions_pool_json", { makeMemberMethod(&RpcServer::f_on_transactions_pool_json), false } }, + { "f_get_blockchain_settings", { makeMemberMethod(&RpcServer::f_on_get_blockchain_settings), true } }, { "getblockcount", { makeMemberMethod(&RpcServer::on_getblockcount), true } }, { "on_getblockhash", { makeMemberMethod(&RpcServer::on_getblockhash), false } }, { "getblocktemplate", { makeMemberMethod(&RpcServer::on_getblocktemplate), false } }, @@ -444,6 +449,368 @@ bool RpcServer::on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMM //------------------------------------------------------------------------------------------------------------------------------ // JSON RPC methods //------------------------------------------------------------------------------------------------------------------------------ +bool RpcServer::f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::request& req, F_COMMAND_RPC_GET_BLOCKS_LIST::response& res) { + + if (m_core.getTopBlockIndex() + 1 <= req.height) { + throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT, + std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.getTopBlockIndex() + 1) }; + } + + uint32_t print_blocks_count = 30; + uint32_t last_height = req.height - print_blocks_count; + if (req.height <= print_blocks_count) { + last_height = 0; + } + + for (uint32_t i = req.height; i >= last_height; i--) { + Hash block_hash = m_core.getBlockHashByIndex(static_cast(i)); + if (!m_core.hasBlock(block_hash)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_INTERNAL_ERROR, + "Internal error: can't get block by height. Height = " + std::to_string(i) + '.' }; + } + BlockTemplate blk = m_core.getBlockByHash(block_hash); + BlockDetails blkDetails = m_core.getBlockDetails(block_hash); + + f_block_short_response block_short; + block_short.cumul_size = blkDetails.blockSize; + block_short.timestamp = blk.timestamp; + block_short.height = i; + block_short.hash = Common::podToHex(block_hash); + block_short.tx_count = blk.transactionHashes.size() + 1; + + res.blocks.push_back(block_short); + + if (i == 0) + break; + } + + res.status = CORE_RPC_STATUS_OK; + return true; +} + +bool RpcServer::f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& req, F_COMMAND_RPC_GET_BLOCK_DETAILS::response& res) { + + Hash hash; + + try { + uint32_t height = boost::lexical_cast(req.hash); + hash = m_core.getBlockHashByIndex(height); + } catch (boost::bad_lexical_cast &) { + if (!parse_hash256(req.hash, hash)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_WRONG_PARAM, + "Failed to parse hex representation of block hash. Hex = " + req.hash + '.' }; + } + } + + if (!m_core.hasBlock(hash)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_INTERNAL_ERROR, + "Internal error: can't get block by hash. Hash = " + req.hash + '.' }; + } + BlockTemplate blk = m_core.getBlockByHash(hash); + BlockDetails blkDetails = m_core.getBlockDetails(hash); + + if (blk.baseTransaction.inputs.front().type() != typeid(BaseInput)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_INTERNAL_ERROR, + "Internal error: coinbase transaction in the block has the wrong type" }; + } + + block_header_response block_header; + res.block.height = boost::get(blk.baseTransaction.inputs.front()).blockIndex; + fill_block_header_response(blk, false, res.block.height, hash, block_header); + + res.block.major_version = block_header.major_version; + res.block.minor_version = block_header.minor_version; + res.block.timestamp = block_header.timestamp; + res.block.prev_hash = block_header.prev_hash; + res.block.nonce = block_header.nonce; + res.block.hash = Common::podToHex(hash); + res.block.depth = m_core.getTopBlockIndex() - res.block.height; + res.block.difficulty = m_core.getBlockDifficulty(res.block.height); + res.block.transactionsCumulativeSize = blkDetails.transactionsCumulativeSize; + res.block.alreadyGeneratedCoins = std::to_string(blkDetails.alreadyGeneratedCoins); + res.block.alreadyGeneratedTransactions = blkDetails.alreadyGeneratedTransactions; + res.block.reward = block_header.reward; + res.block.sizeMedian = blkDetails.sizeMedian; + res.block.blockSize = blkDetails.blockSize; + res.block.orphan_status = blkDetails.isAlternative; + + uint64_t maxReward = 0; + uint64_t currentReward = 0; + int64_t emissionChange = 0; + size_t blockGrantedFullRewardZone = m_core.getCurrency().blockGrantedFullRewardZoneByBlockVersion(block_header.major_version); + res.block.effectiveSizeMedian = std::max(res.block.sizeMedian, blockGrantedFullRewardZone); + + res.block.baseReward = blkDetails.baseReward; + res.block.penalty = blkDetails.penalty; + + // Base transaction adding + f_transaction_short_response transaction_short; + transaction_short.hash = Common::podToHex(getObjectHash(blk.baseTransaction)); + transaction_short.fee = 0; + transaction_short.amount_out = getOutputAmount(blk.baseTransaction); + transaction_short.size = getObjectBinarySize(blk.baseTransaction); + res.block.transactions.push_back(transaction_short); + + std::vector missed_txs; + std::vector txs; + m_core.getTransactions(blk.transactionHashes, txs, missed_txs); + + res.block.totalFeeAmount = 0; + + for (const BinaryArray& ba : txs) { + Transaction tx; + if (!fromBinaryArray(tx, ba)) { + throw std::runtime_error("Couldn't deserialize transaction"); + } + f_transaction_short_response transaction_short; + uint64_t amount_in = getInputAmount(tx); + uint64_t amount_out = getOutputAmount(tx); + + transaction_short.hash = Common::podToHex(getObjectHash(tx)); + transaction_short.fee = amount_in - amount_out; + transaction_short.amount_out = amount_out; + transaction_short.size = getObjectBinarySize(tx); + res.block.transactions.push_back(transaction_short); + + res.block.totalFeeAmount += transaction_short.fee; + } + + res.status = CORE_RPC_STATUS_OK; + return true; +} + +bool RpcServer::f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAILS::request& req, F_COMMAND_RPC_GET_TRANSACTION_DETAILS::response& res) { + + Hash hash; + + if (!parse_hash256(req.hash, hash)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_WRONG_PARAM, + "Failed to parse hex representation of transaction hash. Hex = " + req.hash + '.' }; + } + + std::vector tx_ids; + tx_ids.push_back(hash); + + std::vector missed_txs; + std::vector txs; + m_core.getTransactions(tx_ids, txs, missed_txs); + + if (1 == txs.size()) { + Transaction transaction; + if (!fromBinaryArray(transaction, txs.front())) { + throw std::runtime_error("Couldn't deserialize transaction"); + } + res.tx = transaction; + } else { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_WRONG_PARAM, + "transaction wasn't found. Hash = " + req.hash + '.' }; + } + TransactionDetails transactionDetails = m_core.getTransactionDetails(hash); + + Crypto::Hash blockHash; + if (transactionDetails.inBlockchain) { + uint32_t blockHeight = transactionDetails.blockIndex; + if (!blockHeight) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_INTERNAL_ERROR, + "Internal error: can't get transaction by hash. Hash = " + Common::podToHex(hash) + '.' }; + } + blockHash = m_core.getBlockHashByIndex(blockHeight); + BlockTemplate blk = m_core.getBlockByHash(blockHash); + BlockDetails blkDetails = m_core.getBlockDetails(blockHash); + + f_block_short_response block_short; + + block_short.cumul_size = blkDetails.blockSize; + block_short.timestamp = blk.timestamp; + block_short.height = blockHeight; + block_short.hash = Common::podToHex(blockHash); + block_short.tx_count = blk.transactionHashes.size() + 1; + res.block = block_short; + } + + uint64_t amount_in = getInputAmount(res.tx); + uint64_t amount_out = getOutputAmount(res.tx); + + res.txDetails.hash = Common::podToHex(getObjectHash(res.tx)); + res.txDetails.fee = amount_in - amount_out; + if (amount_in == 0) + res.txDetails.fee = 0; + res.txDetails.amount_out = amount_out; + res.txDetails.size = getObjectBinarySize(res.tx); + + uint64_t mixin; + if (!f_getMixin(res.tx, mixin)) { + return false; + } + res.txDetails.mixin = mixin; + + Crypto::Hash paymentId; + if (CryptoNote::getPaymentIdFromTxExtra(res.tx.extra, paymentId)) { + res.txDetails.paymentId = Common::podToHex(paymentId); + } else { + res.txDetails.paymentId = ""; + } + + res.status = CORE_RPC_STATUS_OK; + return true; +} + + +bool RpcServer::f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res) { + + auto pool = m_core.getPoolTransactions(); + for (const Transaction tx : pool) { + f_transaction_short_response transaction_short; + uint64_t amount_in = getInputAmount(tx); + uint64_t amount_out = getOutputAmount(tx); + + transaction_short.hash = Common::podToHex(getObjectHash(tx)); + transaction_short.fee = amount_in - amount_out; + transaction_short.amount_out = amount_out; + transaction_short.size = getObjectBinarySize(tx); + res.transactions.push_back(transaction_short); + } + + res.status = CORE_RPC_STATUS_OK; + return true; +} + +bool RpcServer::f_getMixin(const Transaction& transaction, uint64_t& mixin) { + mixin = 0; + for (const TransactionInput& txin : transaction.inputs) { + if (txin.type() != typeid(KeyInput)) { + continue; + } + uint64_t currentMixin = boost::get(txin).outputIndexes.size(); + if (currentMixin > mixin) { + mixin = currentMixin; + } + } + return true; +} + +bool RpcServer::f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res) { + res.base_coin.name = "bytecoin"; + res.base_coin.git = "https://github.com/amjuarez/bytecoin.git"; + + // Hardcoded plugins, refactor this + res.extensions.push_back("core/bytecoin.json"); + res.extensions.push_back("bug-fixes.json"); + res.extensions.push_back("print-genesis-tx.json"); + + if (m_core.getCurrency().minMixin() != 0 || m_core.getCurrency().mandatoryMixinBlockVersion() != 0) { + res.extensions.push_back("mix-mixin.json"); + } + if (m_core.getCurrency().mixinStartHeight() != 0) { + res.extensions.push_back("mixin-start-height.json"); + } + if (m_core.getCurrency().mandatoryTransaction() == 1) { + res.extensions.push_back("mandatory-transaction-in-block.json"); + } + if (m_core.getCurrency().killHeight() != 0) { + res.extensions.push_back("kill-height.json"); + } + if (m_core.getCurrency().tailEmissionReward() != 0) { + res.extensions.push_back("tail-emission-reward.json"); + } + if (m_core.getCurrency().cryptonoteCoinVersion() != 0) { + res.extensions.push_back("cryptonote-coin-clones-support.json"); + } + if (m_core.getCurrency().genesisBlockReward() != 0) { + res.extensions.push_back("genesis-block-reward.json"); + } + if (m_core.getCurrency().difficultyWindowByBlockVersion(1) != m_core.getCurrency().difficultyWindowByBlockVersion(2) || m_core.getCurrency().difficultyWindowByBlockVersion(1) != m_core.getCurrency().difficultyWindowByBlockVersion(3) || + m_core.getCurrency().difficultyLagByBlockVersion(1) != m_core.getCurrency().difficultyLagByBlockVersion(2) || m_core.getCurrency().difficultyLagByBlockVersion(1) != m_core.getCurrency().difficultyLagByBlockVersion(3) || + m_core.getCurrency().difficultyCutByBlockVersion(1) != m_core.getCurrency().difficultyCutByBlockVersion(2) || m_core.getCurrency().difficultyCutByBlockVersion(1) != m_core.getCurrency().difficultyCutByBlockVersion(3)) { + res.extensions.push_back("versionized-parameters.json"); + } + if (m_core.getCurrency().zawyDifficultyBlockIndex() != 0 ) { + res.extensions.push_back("zawy-difficulty-algorithm.json"); + } + if (m_core.getCurrency().zawyLWMADifficultyBlockIndex() != 0 ) { + res.extensions.push_back("zawy-lwma-difficulty-algorithm.json"); + } + if (m_core.getCurrency().buggedZawyDifficultyBlockIndex() != 0 ) { + res.extensions.push_back("bugged-zawy-difficulty-algorithm.json"); + } + res.core.CRYPTONOTE_NAME = m_core.getCurrency().cryptonoteName(); + + res.core.EMISSION_SPEED_FACTOR = m_core.getCurrency().emissionSpeedFactor(); + res.core.DIFFICULTY_TARGET = m_core.getCurrency().difficultyTarget(); + res.core.CRYPTONOTE_DISPLAY_DECIMAL_POINT = m_core.getCurrency().numberOfDecimalPlaces(); + res.core.MONEY_SUPPLY = std::to_string(m_core.getCurrency().moneySupply()); + res.core.EXPECTED_NUMBER_OF_BLOCKS_PER_DAY = m_core.getCurrency().expectedNumberOfBlocksPerDay(); + res.core.DEFAULT_DUST_THRESHOLD = m_core.getCurrency().defaultDustThreshold(); + res.core.MINIMUM_FEE = m_core.getCurrency().minimumFee(); + res.core.CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW = m_core.getCurrency().minedMoneyUnlockWindow(); + res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE = m_core.getCurrency().blockGrantedFullRewardZone(); + res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 = m_core.getCurrency().blockGrantedFullRewardZoneV1(); + res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2 = m_core.getCurrency().blockGrantedFullRewardZoneV2(); + res.core.CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX = m_core.getCurrency().publicAddressBase58Prefix(); + res.core.MAX_BLOCK_SIZE_INITIAL = m_core.getCurrency().maxBlockSizeInitial(); + res.core.UPGRADE_HEIGHT_V2 = m_core.getCurrency().upgradeHeight(2); + res.core.UPGRADE_HEIGHT_V3 = m_core.getCurrency().upgradeHeight(3); + res.core.DIFFICULTY_WINDOW = m_core.getCurrency().difficultyWindow(); + res.core.DIFFICULTY_CUT = m_core.getCurrency().difficultyCut(); + res.core.DIFFICULTY_LAG = m_core.getCurrency().difficultyLag(); + + if (m_core.getCurrency().minMixin() != 0 || m_core.getCurrency().mandatoryMixinBlockVersion() != 0) { + res.core.MIN_MIXIN = m_core.getCurrency().minMixin(); + res.core.MANDATORY_MIXIN_BLOCK_VERSION = m_core.getCurrency().mandatoryMixinBlockVersion(); + } + if (m_core.getCurrency().mixinStartHeight() != 0) { + res.core.MIXIN_START_HEIGHT = m_core.getCurrency().mixinStartHeight(); + } + res.core.MANDATORY_TRANSACTION = m_core.getCurrency().mandatoryTransaction(); + res.core.KILL_HEIGHT = m_core.getCurrency().killHeight(); + res.core.TAIL_EMISSION_REWARD = m_core.getCurrency().tailEmissionReward(); + res.core.CRYPTONOTE_COIN_VERSION = m_core.getCurrency().cryptonoteCoinVersion(); + res.core.GENESIS_BLOCK_REWARD = std::to_string(m_core.getCurrency().genesisBlockReward()); + res.core.DIFFICULTY_WINDOW_V1 = m_core.getCurrency().difficultyWindowV1(); + res.core.DIFFICULTY_WINDOW_V2 = m_core.getCurrency().difficultyWindowV2(); + res.core.DIFFICULTY_CUT_V1 = m_core.getCurrency().difficultyCutV1(); + res.core.DIFFICULTY_CUT_V2 = m_core.getCurrency().difficultyCutV2(); + res.core.DIFFICULTY_LAG_V1 = m_core.getCurrency().difficultyLagV1(); + res.core.DIFFICULTY_LAG_V2 = m_core.getCurrency().difficultyLagV2(); + res.core.ZAWY_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().zawyDifficultyBlockIndex(); + res.core.ZAWY_DIFFICULTY_LAST_BLOCK = m_core.getCurrency().zawyDifficultyLastBlock(); + res.core.ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().zawyLWMADifficultyBlockIndex(); + res.core.ZAWY_LWMA_DIFFICULTY_LAST_BLOCK = m_core.getCurrency().zawyLWMADifficultyLastBlock(); + res.core.ZAWY_LWMA_DIFFICULTY_N = m_core.getCurrency().zawyLWMADifficultyN(); + res.core.BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().buggedZawyDifficultyBlockIndex(); + res.core.P2P_DEFAULT_PORT = m_p2p.get_this_peer_port(); + // Not real. Change + res.core.RPC_DEFAULT_PORT = m_p2p.get_this_peer_port() + 1; + + for (const NetworkAddress& na : m_p2p.get_seed_nodes()) { + std::string na_string = Common::ipAddressToString(na.ip) + ":" + std::to_string(na.port); + + res.core.SEED_NODES.push_back(na_string); + } + + res.core.BYTECOIN_NETWORK = boost::lexical_cast(m_p2p.get_network_id()); + + std::map cp; + cp = m_core.get_checkpoints().get_checkpoints(); + for (auto i : cp) { + std::string cp_string = std::to_string(i.first) + ":" + Common::podToHex(i.second); + + res.core.CHECKPOINTS.push_back(cp_string); + } + + res.core.GENESIS_COINBASE_TX_HEX = Common::toHex(CryptoNote::toBinaryArray(m_core.getCurrency().genesisBlock().baseTransaction)); + + res.status = CORE_RPC_STATUS_OK; + return true; +} + bool RpcServer::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res) { res.count = m_core.get_current_blockchain_height(); res.status = CORE_RPC_STATUS_OK; diff --git a/src/Rpc/RpcServer.h b/src/Rpc/RpcServer.h index f3c43cd7..39007ef6 100755 --- a/src/Rpc/RpcServer.h +++ b/src/Rpc/RpcServer.h @@ -68,6 +68,13 @@ class RpcServer : public HttpServer { void fill_block_header_response(const Block& blk, bool orphan_status, uint64_t height, const Crypto::Hash& hash, block_header_response& responce); + bool f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::request& req, F_COMMAND_RPC_GET_BLOCKS_LIST::response& res); + bool f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& req, F_COMMAND_RPC_GET_BLOCK_DETAILS::response& res); + bool f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAILS::request& req, F_COMMAND_RPC_GET_TRANSACTION_DETAILS::response& res); + bool f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res); + bool f_getMixin(const Transaction& transaction, uint64_t& mixin); + bool f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res); + Logging::LoggerRef logger; core& m_core; NodeServer& m_p2p; From c2c69950d0762f128ea0ded2c53b4bbaf90d3366 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 15 Jun 2018 22:05:53 +0000 Subject: [PATCH 02/12] update --- src/Rpc/CoreRpcServerCommandsDefinitions.h | 294 +++++++++++++++++++++ 1 file changed, 294 insertions(+) diff --git a/src/Rpc/CoreRpcServerCommandsDefinitions.h b/src/Rpc/CoreRpcServerCommandsDefinitions.h index 40d415cb..4545fc34 100755 --- a/src/Rpc/CoreRpcServerCommandsDefinitions.h +++ b/src/Rpc/CoreRpcServerCommandsDefinitions.h @@ -402,6 +402,206 @@ struct BLOCK_HEADER_RESPONSE { } }; +struct f_transaction_short_response { + std::string hash; + uint64_t fee; + uint64_t amount_out; + uint64_t size; + + void serialize(ISerializer &s) { + KV_MEMBER(hash) + KV_MEMBER(fee) + KV_MEMBER(amount_out) + KV_MEMBER(size) + } +}; + +struct f_transaction_details_response { + std::string hash; + size_t size; + std::string paymentId; + uint64_t mixin; + uint64_t fee; + uint64_t amount_out; + + void serialize(ISerializer &s) { + KV_MEMBER(hash) + KV_MEMBER(size) + KV_MEMBER(paymentId) + KV_MEMBER(mixin) + KV_MEMBER(fee) + KV_MEMBER(amount_out) + } +}; + +struct f_block_short_response { + uint64_t timestamp; + uint32_t height; + std::string hash; + uint64_t tx_count; + uint64_t cumul_size; + + void serialize(ISerializer &s) { + KV_MEMBER(timestamp) + KV_MEMBER(height) + KV_MEMBER(hash) + KV_MEMBER(cumul_size) + KV_MEMBER(tx_count) + } +}; + +struct f_block_details_response { + uint8_t major_version; + uint8_t minor_version; + uint64_t timestamp; + std::string prev_hash; + uint32_t nonce; + bool orphan_status; + uint32_t height; + uint64_t depth; + std::string hash; + uint64_t difficulty; + uint64_t reward; + uint64_t blockSize; + size_t sizeMedian; + uint64_t effectiveSizeMedian; + uint64_t transactionsCumulativeSize; + std::string alreadyGeneratedCoins; + uint64_t alreadyGeneratedTransactions; + uint64_t baseReward; + double penalty; + uint64_t totalFeeAmount; + std::vector transactions; + + void serialize(ISerializer &s) { + KV_MEMBER(major_version) + KV_MEMBER(minor_version) + KV_MEMBER(timestamp) + KV_MEMBER(prev_hash) + KV_MEMBER(nonce) + KV_MEMBER(orphan_status) + KV_MEMBER(height) + KV_MEMBER(depth) + KV_MEMBER(hash) + KV_MEMBER(difficulty) + KV_MEMBER(reward) + KV_MEMBER(blockSize) + KV_MEMBER(sizeMedian) + KV_MEMBER(effectiveSizeMedian) + KV_MEMBER(transactionsCumulativeSize) + KV_MEMBER(alreadyGeneratedCoins) + KV_MEMBER(alreadyGeneratedTransactions) + KV_MEMBER(baseReward) + KV_MEMBER(penalty) + KV_MEMBER(transactions) + KV_MEMBER(totalFeeAmount) + } +}; +struct currency_base_coin { + std::string name; + std::string git; + + void serialize(ISerializer &s) { + KV_MEMBER(name) + KV_MEMBER(git) + } +}; + +struct currency_core { + std::vector SEED_NODES; + unsigned EMISSION_SPEED_FACTOR; + uint64_t DIFFICULTY_TARGET; + size_t CRYPTONOTE_DISPLAY_DECIMAL_POINT; + std::string MONEY_SUPPLY; + uint64_t DEFAULT_DUST_THRESHOLD; + uint64_t MINIMUM_FEE; + uint32_t CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; + size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE; + size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1; + size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2; + uint64_t CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; + int P2P_DEFAULT_PORT; + int RPC_DEFAULT_PORT; + size_t MAX_BLOCK_SIZE_INITIAL; + uint64_t EXPECTED_NUMBER_OF_BLOCKS_PER_DAY; + uint32_t UPGRADE_HEIGHT_V2; + uint32_t UPGRADE_HEIGHT_V3; + size_t DIFFICULTY_WINDOW; + size_t DIFFICULTY_CUT; + size_t DIFFICULTY_LAG; + uint16_t MIN_MIXIN; + uint8_t MANDATORY_MIXIN_BLOCK_VERSION; + uint32_t MIXIN_START_HEIGHT; + uint32_t MANDATORY_TRANSACTION; + uint32_t KILL_HEIGHT; + uint64_t TAIL_EMISSION_REWARD; + uint8_t CRYPTONOTE_COIN_VERSION; + std::string GENESIS_BLOCK_REWARD; + size_t DIFFICULTY_WINDOW_V1; + size_t DIFFICULTY_WINDOW_V2; + size_t DIFFICULTY_CUT_V1; + size_t DIFFICULTY_CUT_V2; + size_t DIFFICULTY_LAG_V1; + size_t DIFFICULTY_LAG_V2; + uint32_t ZAWY_DIFFICULTY_BLOCK_INDEX; + uint32_t ZAWY_DIFFICULTY_LAST_BLOCK; + uint32_t ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX; + uint32_t ZAWY_LWMA_DIFFICULTY_LAST_BLOCK; + uint32_t ZAWY_LWMA_DIFFICULTY_N; + uint32_t BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX; + std::string BYTECOIN_NETWORK; + std::string CRYPTONOTE_NAME; + std::string GENESIS_COINBASE_TX_HEX; + std::vector CHECKPOINTS; + + void serialize(ISerializer &s) { + KV_MEMBER(SEED_NODES) + KV_MEMBER(EMISSION_SPEED_FACTOR) + KV_MEMBER(DIFFICULTY_TARGET) + KV_MEMBER(CRYPTONOTE_DISPLAY_DECIMAL_POINT) + KV_MEMBER(MONEY_SUPPLY) + KV_MEMBER(DEFAULT_DUST_THRESHOLD) + KV_MEMBER(MINIMUM_FEE) + KV_MEMBER(CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW) + KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) + KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1) + KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2) + KV_MEMBER(CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) + KV_MEMBER(P2P_DEFAULT_PORT) + KV_MEMBER(RPC_DEFAULT_PORT) + KV_MEMBER(MAX_BLOCK_SIZE_INITIAL) + KV_MEMBER(EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) + KV_MEMBER(UPGRADE_HEIGHT_V2) + KV_MEMBER(UPGRADE_HEIGHT_V3) + KV_MEMBER(DIFFICULTY_WINDOW) + KV_MEMBER(DIFFICULTY_CUT) + KV_MEMBER(DIFFICULTY_LAG) + KV_MEMBER(MIN_MIXIN) + KV_MEMBER(MANDATORY_MIXIN_BLOCK_VERSION) + KV_MEMBER(MIXIN_START_HEIGHT) + KV_MEMBER(MANDATORY_TRANSACTION) + KV_MEMBER(KILL_HEIGHT) + KV_MEMBER(TAIL_EMISSION_REWARD) + KV_MEMBER(CRYPTONOTE_COIN_VERSION) + KV_MEMBER(GENESIS_BLOCK_REWARD) + KV_MEMBER(DIFFICULTY_WINDOW_V1) + KV_MEMBER(DIFFICULTY_WINDOW_V2) + KV_MEMBER(DIFFICULTY_CUT_V1) + KV_MEMBER(DIFFICULTY_CUT_V2) + KV_MEMBER(DIFFICULTY_LAG_V1) + KV_MEMBER(DIFFICULTY_LAG_V2) + KV_MEMBER(ZAWY_DIFFICULTY_BLOCK_INDEX) + KV_MEMBER(ZAWY_DIFFICULTY_LAST_BLOCK) + KV_MEMBER(ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX) + KV_MEMBER(ZAWY_LWMA_DIFFICULTY_LAST_BLOCK) + KV_MEMBER(ZAWY_LWMA_DIFFICULTY_N) + KV_MEMBER(BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX) + KV_MEMBER(BYTECOIN_NETWORK) + KV_MEMBER(CRYPTONOTE_NAME) + KV_MEMBER(GENESIS_COINBASE_TX_HEX) + KV_MEMBER(CHECKPOINTS) + } +}; struct COMMAND_RPC_GET_LAST_BLOCK_HEADER { typedef EMPTY_STRUCT request; @@ -432,6 +632,100 @@ struct COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT { typedef BLOCK_HEADER_RESPONSE response; }; +struct F_COMMAND_RPC_GET_BLOCKS_LIST { + struct request { + uint64_t height; + + void serialize(ISerializer &s) { + KV_MEMBER(height) + } + }; + + struct response { + std::vector blocks; //transactions blobs as hex + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(blocks) + KV_MEMBER(status) + } + }; +}; + +struct F_COMMAND_RPC_GET_BLOCK_DETAILS { + struct request { + std::string hash; + + void serialize(ISerializer &s) { + KV_MEMBER(hash) + } + }; + + struct response { + f_block_details_response block; + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(block) + KV_MEMBER(status) + } + }; +}; + +struct F_COMMAND_RPC_GET_TRANSACTION_DETAILS { + struct request { + std::string hash; + + void serialize(ISerializer &s) { + KV_MEMBER(hash) + } + }; + + struct response { + Transaction tx; + f_transaction_details_response txDetails; + f_block_short_response block; + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(tx) + KV_MEMBER(txDetails) + KV_MEMBER(block) + KV_MEMBER(status) + } + }; +}; + +struct F_COMMAND_RPC_GET_POOL { + typedef EMPTY_STRUCT request; + + struct response { + std::vector transactions; //transactions blobs as hex + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(transactions) + KV_MEMBER(status) + } + }; +}; +struct F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS { + typedef EMPTY_STRUCT request; + struct response { + currency_base_coin base_coin; + currency_core core; + std::vector extensions; + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(base_coin) + KV_MEMBER(core) + KV_MEMBER(extensions) + KV_MEMBER(status) + } + }; +}; + struct COMMAND_RPC_QUERY_BLOCKS { struct request { std::vector block_ids; //*first 10 blocks id goes sequential, next goes in pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */ From 421245f1257abf38e0f633d2732edd38415fd7c9 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 18:58:03 -0700 Subject: [PATCH 03/12] merge SDN code --- src/Rpc/CoreRpcServerCommandsDefinitions.h | 113 ++++++--------------- 1 file changed, 32 insertions(+), 81 deletions(-) diff --git a/src/Rpc/CoreRpcServerCommandsDefinitions.h b/src/Rpc/CoreRpcServerCommandsDefinitions.h index 4545fc34..f4bd6a51 100755 --- a/src/Rpc/CoreRpcServerCommandsDefinitions.h +++ b/src/Rpc/CoreRpcServerCommandsDefinitions.h @@ -47,7 +47,7 @@ struct COMMAND_RPC_GET_BLOCKS_FAST { struct request { std::vector block_ids; //*first 10 blocks id goes sequential, next goes in pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */ - + void serialize(ISerializer &s) { serializeAsBinary(block_ids, "block_ids", s); } @@ -85,7 +85,7 @@ struct COMMAND_RPC_GET_TRANSACTIONS { void serialize(ISerializer &s) { KV_MEMBER(txs_as_hex) KV_MEMBER(missed_tx) - KV_MEMBER(status) + KV_MEMBER(status) } }; }; @@ -144,7 +144,7 @@ struct COMMAND_RPC_GET_POOL_CHANGES_LITE { //----------------------------------------------- struct COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES { - + struct request { Crypto::Hash txid; @@ -402,6 +402,7 @@ struct BLOCK_HEADER_RESPONSE { } }; + struct f_transaction_short_response { std::string hash; uint64_t fee; @@ -452,15 +453,15 @@ struct f_block_short_response { struct f_block_details_response { uint8_t major_version; - uint8_t minor_version; + uint8_t minor_version; uint64_t timestamp; std::string prev_hash; uint32_t nonce; bool orphan_status; - uint32_t height; + uint64_t height; uint64_t depth; std::string hash; - uint64_t difficulty; + difficulty_type difficulty; uint64_t reward; uint64_t blockSize; size_t sizeMedian; @@ -509,47 +510,25 @@ struct currency_base_coin { struct currency_core { std::vector SEED_NODES; - unsigned EMISSION_SPEED_FACTOR; + uint64_t EMISSION_SPEED_FACTOR; uint64_t DIFFICULTY_TARGET; - size_t CRYPTONOTE_DISPLAY_DECIMAL_POINT; + uint64_t CRYPTONOTE_DISPLAY_DECIMAL_POINT; std::string MONEY_SUPPLY; +//uint64_t GENESIS_BLOCK_REWARD; uint64_t DEFAULT_DUST_THRESHOLD; uint64_t MINIMUM_FEE; - uint32_t CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; - size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE; - size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1; - size_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2; + uint64_t CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW; + uint64_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE; +// uint64_t CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1; uint64_t CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX; - int P2P_DEFAULT_PORT; - int RPC_DEFAULT_PORT; - size_t MAX_BLOCK_SIZE_INITIAL; + uint64_t P2P_DEFAULT_PORT; + uint64_t RPC_DEFAULT_PORT; + uint64_t MAX_BLOCK_SIZE_INITIAL; uint64_t EXPECTED_NUMBER_OF_BLOCKS_PER_DAY; - uint32_t UPGRADE_HEIGHT_V2; - uint32_t UPGRADE_HEIGHT_V3; - size_t DIFFICULTY_WINDOW; - size_t DIFFICULTY_CUT; - size_t DIFFICULTY_LAG; - uint16_t MIN_MIXIN; - uint8_t MANDATORY_MIXIN_BLOCK_VERSION; - uint32_t MIXIN_START_HEIGHT; - uint32_t MANDATORY_TRANSACTION; - uint32_t KILL_HEIGHT; - uint64_t TAIL_EMISSION_REWARD; - uint8_t CRYPTONOTE_COIN_VERSION; - std::string GENESIS_BLOCK_REWARD; - size_t DIFFICULTY_WINDOW_V1; - size_t DIFFICULTY_WINDOW_V2; - size_t DIFFICULTY_CUT_V1; - size_t DIFFICULTY_CUT_V2; - size_t DIFFICULTY_LAG_V1; - size_t DIFFICULTY_LAG_V2; - uint32_t ZAWY_DIFFICULTY_BLOCK_INDEX; - uint32_t ZAWY_DIFFICULTY_LAST_BLOCK; - uint32_t ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX; - uint32_t ZAWY_LWMA_DIFFICULTY_LAST_BLOCK; - uint32_t ZAWY_LWMA_DIFFICULTY_N; - uint32_t BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX; - std::string BYTECOIN_NETWORK; + uint64_t UPGRADE_HEIGHT; + uint64_t DIFFICULTY_CUT; + uint64_t DIFFICULTY_LAG; + //std::string BYTECOIN_NETWORK; std::string CRYPTONOTE_NAME; std::string GENESIS_COINBASE_TX_HEX; std::vector CHECKPOINTS; @@ -560,49 +539,31 @@ struct currency_core { KV_MEMBER(DIFFICULTY_TARGET) KV_MEMBER(CRYPTONOTE_DISPLAY_DECIMAL_POINT) KV_MEMBER(MONEY_SUPPLY) +// KV_MEMBER(GENESIS_BLOCK_REWARD) KV_MEMBER(DEFAULT_DUST_THRESHOLD) KV_MEMBER(MINIMUM_FEE) KV_MEMBER(CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW) KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) - KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1) - KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2) +// KV_MEMBER(CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1) KV_MEMBER(CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) KV_MEMBER(P2P_DEFAULT_PORT) KV_MEMBER(RPC_DEFAULT_PORT) KV_MEMBER(MAX_BLOCK_SIZE_INITIAL) KV_MEMBER(EXPECTED_NUMBER_OF_BLOCKS_PER_DAY) - KV_MEMBER(UPGRADE_HEIGHT_V2) - KV_MEMBER(UPGRADE_HEIGHT_V3) - KV_MEMBER(DIFFICULTY_WINDOW) + KV_MEMBER(UPGRADE_HEIGHT) KV_MEMBER(DIFFICULTY_CUT) KV_MEMBER(DIFFICULTY_LAG) - KV_MEMBER(MIN_MIXIN) - KV_MEMBER(MANDATORY_MIXIN_BLOCK_VERSION) - KV_MEMBER(MIXIN_START_HEIGHT) - KV_MEMBER(MANDATORY_TRANSACTION) - KV_MEMBER(KILL_HEIGHT) - KV_MEMBER(TAIL_EMISSION_REWARD) - KV_MEMBER(CRYPTONOTE_COIN_VERSION) - KV_MEMBER(GENESIS_BLOCK_REWARD) - KV_MEMBER(DIFFICULTY_WINDOW_V1) - KV_MEMBER(DIFFICULTY_WINDOW_V2) - KV_MEMBER(DIFFICULTY_CUT_V1) - KV_MEMBER(DIFFICULTY_CUT_V2) - KV_MEMBER(DIFFICULTY_LAG_V1) - KV_MEMBER(DIFFICULTY_LAG_V2) - KV_MEMBER(ZAWY_DIFFICULTY_BLOCK_INDEX) - KV_MEMBER(ZAWY_DIFFICULTY_LAST_BLOCK) - KV_MEMBER(ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX) - KV_MEMBER(ZAWY_LWMA_DIFFICULTY_LAST_BLOCK) - KV_MEMBER(ZAWY_LWMA_DIFFICULTY_N) - KV_MEMBER(BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX) - KV_MEMBER(BYTECOIN_NETWORK) +// KV_MEMBER(BYTECOIN_NETWORK) KV_MEMBER(CRYPTONOTE_NAME) KV_MEMBER(GENESIS_COINBASE_TX_HEX) KV_MEMBER(CHECKPOINTS) } }; + + + + struct COMMAND_RPC_GET_LAST_BLOCK_HEADER { typedef EMPTY_STRUCT request; typedef BLOCK_HEADER_RESPONSE response; @@ -632,6 +593,8 @@ struct COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT { typedef BLOCK_HEADER_RESPONSE response; }; + + struct F_COMMAND_RPC_GET_BLOCKS_LIST { struct request { uint64_t height; @@ -695,20 +658,6 @@ struct F_COMMAND_RPC_GET_TRANSACTION_DETAILS { } }; }; - -struct F_COMMAND_RPC_GET_POOL { - typedef EMPTY_STRUCT request; - - struct response { - std::vector transactions; //transactions blobs as hex - std::string status; - - void serialize(ISerializer &s) { - KV_MEMBER(transactions) - KV_MEMBER(status) - } - }; -}; struct F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS { typedef EMPTY_STRUCT request; struct response { @@ -726,6 +675,8 @@ struct F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS { }; }; + + struct COMMAND_RPC_QUERY_BLOCKS { struct request { std::vector block_ids; //*first 10 blocks id goes sequential, next goes in pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */ From 1b267e4ee7e2fa285fcdafde0ed30e01492fa057 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 18:58:39 -0700 Subject: [PATCH 04/12] merge SDN code --- src/Rpc/RpcServer.cpp | 337 +++++++++++++++--------------------------- 1 file changed, 122 insertions(+), 215 deletions(-) diff --git a/src/Rpc/RpcServer.cpp b/src/Rpc/RpcServer.cpp index 11d0c5d6..c4b72c4c 100755 --- a/src/Rpc/RpcServer.cpp +++ b/src/Rpc/RpcServer.cpp @@ -68,9 +68,9 @@ RpcServer::HandlerFunction jsonMethod(bool (RpcServer::*handler)(typename Comman } } - + std::unordered_map> RpcServer::s_handlers = { - + // binary handlers { "/getblocks.bin", { binMethod(&RpcServer::on_get_blocks), false } }, { "/queryblocks.bin", { binMethod(&RpcServer::on_query_blocks), false } }, @@ -133,8 +133,7 @@ bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse& { "f_blocks_list_json", { makeMemberMethod(&RpcServer::f_on_blocks_list_json), false } }, { "f_block_json", { makeMemberMethod(&RpcServer::f_on_block_json), false } }, { "f_transaction_json", { makeMemberMethod(&RpcServer::f_on_transaction_json), false } }, - { "f_on_transactions_pool_json", { makeMemberMethod(&RpcServer::f_on_transactions_pool_json), false } }, - { "f_get_blockchain_settings", { makeMemberMethod(&RpcServer::f_on_get_blockchain_settings), true } }, + // { "f_get_blockchain_settings", { makeMemberMethod(&RpcServer::f_on_get_blockchain_settings), true } }, { "getblockcount", { makeMemberMethod(&RpcServer::on_getblockcount), true } }, { "on_getblockhash", { makeMemberMethod(&RpcServer::on_getblockhash), false } }, { "getblocktemplate", { makeMemberMethod(&RpcServer::on_getblocktemplate), false } }, @@ -450,33 +449,36 @@ bool RpcServer::on_stop_daemon(const COMMAND_RPC_STOP_DAEMON::request& req, COMM // JSON RPC methods //------------------------------------------------------------------------------------------------------------------------------ bool RpcServer::f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::request& req, F_COMMAND_RPC_GET_BLOCKS_LIST::response& res) { - - if (m_core.getTopBlockIndex() + 1 <= req.height) { + if (m_core.get_current_blockchain_height() <= req.height) { throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT, - std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.getTopBlockIndex() + 1) }; + std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height()) }; } uint32_t print_blocks_count = 30; uint32_t last_height = req.height - print_blocks_count; if (req.height <= print_blocks_count) { last_height = 0; - } + } for (uint32_t i = req.height; i >= last_height; i--) { - Hash block_hash = m_core.getBlockHashByIndex(static_cast(i)); - if (!m_core.hasBlock(block_hash)) { - throw JsonRpc::JsonRpcError{ - CORE_RPC_ERROR_CODE_INTERNAL_ERROR, + Hash block_hash = m_core.getBlockIdByHeight(static_cast(i)); + Block blk; + if (!m_core.getBlockByHash(block_hash, blk)) { + throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: can't get block by height. Height = " + std::to_string(i) + '.' }; } - BlockTemplate blk = m_core.getBlockByHash(block_hash); - BlockDetails blkDetails = m_core.getBlockDetails(block_hash); + + size_t tx_cumulative_block_size; + m_core.getBlockSize(block_hash, tx_cumulative_block_size); + size_t blokBlobSize = getObjectBinarySize(blk); + size_t minerTxBlobSize = getObjectBinarySize(blk.baseTransaction); f_block_short_response block_short; - block_short.cumul_size = blkDetails.blockSize; + block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; block_short.timestamp = blk.timestamp; block_short.height = i; block_short.hash = Common::podToHex(block_hash); + block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; block_short.tx_count = blk.transactionHashes.size() + 1; res.blocks.push_back(block_short); @@ -490,27 +492,20 @@ bool RpcServer::f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::reque } bool RpcServer::f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& req, F_COMMAND_RPC_GET_BLOCK_DETAILS::response& res) { - Hash hash; - try { - uint32_t height = boost::lexical_cast(req.hash); - hash = m_core.getBlockHashByIndex(height); - } catch (boost::bad_lexical_cast &) { - if (!parse_hash256(req.hash, hash)) { - throw JsonRpc::JsonRpcError{ - CORE_RPC_ERROR_CODE_WRONG_PARAM, - "Failed to parse hex representation of block hash. Hex = " + req.hash + '.' }; - } + if (!parse_hash256(req.hash, hash)) { + throw JsonRpc::JsonRpcError{ + CORE_RPC_ERROR_CODE_WRONG_PARAM, + "Failed to parse hex representation of block hash. Hex = " + req.hash + '.' }; } - if (!m_core.hasBlock(hash)) { + Block blk; + if (!m_core.getBlockByHash(hash, blk)) { throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: can't get block by hash. Hash = " + req.hash + '.' }; } - BlockTemplate blk = m_core.getBlockByHash(hash); - BlockDetails blkDetails = m_core.getBlockDetails(hash); if (blk.baseTransaction.inputs.front().type() != typeid(BaseInput)) { throw JsonRpc::JsonRpcError{ @@ -528,47 +523,99 @@ bool RpcServer::f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& res.block.prev_hash = block_header.prev_hash; res.block.nonce = block_header.nonce; res.block.hash = Common::podToHex(hash); - res.block.depth = m_core.getTopBlockIndex() - res.block.height; - res.block.difficulty = m_core.getBlockDifficulty(res.block.height); - res.block.transactionsCumulativeSize = blkDetails.transactionsCumulativeSize; - res.block.alreadyGeneratedCoins = std::to_string(blkDetails.alreadyGeneratedCoins); - res.block.alreadyGeneratedTransactions = blkDetails.alreadyGeneratedTransactions; + res.block.depth = m_core.get_current_blockchain_height() - res.block.height - 1; + m_core.getBlockDifficulty(static_cast(res.block.height), res.block.difficulty); + res.block.reward = block_header.reward; - res.block.sizeMedian = blkDetails.sizeMedian; - res.block.blockSize = blkDetails.blockSize; - res.block.orphan_status = blkDetails.isAlternative; + std::vector blocksSizes; + if (!m_core.getBackwardBlocksSizes(res.block.height, blocksSizes, parameters::CRYPTONOTE_REWARD_BLOCKS_WINDOW)) { + return false; + } + res.block.sizeMedian = Common::medianValue(blocksSizes); + + size_t blockSize = 0; + if (!m_core.getBlockSize(hash, blockSize)) { + return false; + } + res.block.transactionsCumulativeSize = blockSize; + + size_t blokBlobSize = getObjectBinarySize(blk); + size_t minerTxBlobSize = getObjectBinarySize(blk.baseTransaction); + res.block.blockSize = blokBlobSize + res.block.transactionsCumulativeSize - minerTxBlobSize; + + uint64_t alreadyGeneratedCoins; + if (!m_core.getAlreadyGeneratedCoins(hash, alreadyGeneratedCoins)) { + return false; + } + res.block.alreadyGeneratedCoins = std::to_string(alreadyGeneratedCoins); + + if (!m_core.getGeneratedTransactionsNumber(res.block.height, res.block.alreadyGeneratedTransactions)) { + return false; + } + + uint64_t prevBlockGeneratedCoins = 0; + if (res.block.height > 0) { + if (!m_core.getAlreadyGeneratedCoins(blk.previousBlockHash, prevBlockGeneratedCoins)) { + return false; + } + } uint64_t maxReward = 0; uint64_t currentReward = 0; int64_t emissionChange = 0; - size_t blockGrantedFullRewardZone = m_core.getCurrency().blockGrantedFullRewardZoneByBlockVersion(block_header.major_version); + bool penalizeFee = blk.majorVersion >= 2; + size_t blockGrantedFullRewardZone = penalizeFee ? + m_core.currency().blockGrantedFullRewardZone() : + //m_core.currency().blockGrantedFullRewardZoneV1(); res.block.effectiveSizeMedian = std::max(res.block.sizeMedian, blockGrantedFullRewardZone); - res.block.baseReward = blkDetails.baseReward; - res.block.penalty = blkDetails.penalty; + // virtual bool getBlockReward(size_t medianSize, size_t currentBlockSize, uint64_t alreadyGeneratedCoins, uint64_t fee, uint32_t height, + // uint64_t& reward, int64_t& emissionChange) = 0; + + if (!m_core.getBlockReward(res.block.sizeMedian, 0, prevBlockGeneratedCoins, 0, res.block.height, maxReward, emissionChange)) { + return false; + } + if (!m_core.getBlockReward(res.block.sizeMedian, res.block.transactionsCumulativeSize, prevBlockGeneratedCoins, 0, res.block.height, currentReward, emissionChange)) { + return false; + } + + // if (!m_core.getBlockReward(res.block.sizeMedian, 0, prevBlockGeneratedCoins, 0, penalizeFee, maxReward, emissionChange)) { + // return false; + // } + // if (!m_core.getBlockReward(res.block.sizeMedian, res.block.transactionsCumulativeSize, prevBlockGeneratedCoins, 0, penalizeFee, currentReward, emissionChange)) { + // return false; + // } + + res.block.baseReward = maxReward; + if (maxReward == 0 && currentReward == 0) { + res.block.penalty = static_cast(0); + } else { + if (maxReward < currentReward) { + return false; + } + res.block.penalty = static_cast(maxReward - currentReward) / static_cast(maxReward); + } // Base transaction adding f_transaction_short_response transaction_short; transaction_short.hash = Common::podToHex(getObjectHash(blk.baseTransaction)); transaction_short.fee = 0; - transaction_short.amount_out = getOutputAmount(blk.baseTransaction); + transaction_short.amount_out = get_outs_money_amount(blk.baseTransaction); transaction_short.size = getObjectBinarySize(blk.baseTransaction); res.block.transactions.push_back(transaction_short); - std::vector missed_txs; - std::vector txs; + + std::list missed_txs; + std::list txs; m_core.getTransactions(blk.transactionHashes, txs, missed_txs); res.block.totalFeeAmount = 0; - for (const BinaryArray& ba : txs) { - Transaction tx; - if (!fromBinaryArray(tx, ba)) { - throw std::runtime_error("Couldn't deserialize transaction"); - } + for (const Transaction& tx : txs) { f_transaction_short_response transaction_short; - uint64_t amount_in = getInputAmount(tx); - uint64_t amount_out = getOutputAmount(tx); + uint64_t amount_in = 0; + get_inputs_money_amount(tx, amount_in); + uint64_t amount_out = get_outs_money_amount(tx); transaction_short.hash = Common::podToHex(getObjectHash(tx)); transaction_short.fee = amount_in - amount_out; @@ -584,7 +631,6 @@ bool RpcServer::f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& } bool RpcServer::f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAILS::request& req, F_COMMAND_RPC_GET_TRANSACTION_DETAILS::response& res) { - Hash hash; if (!parse_hash256(req.hash, hash)) { @@ -596,47 +642,42 @@ bool RpcServer::f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAIL std::vector tx_ids; tx_ids.push_back(hash); - std::vector missed_txs; - std::vector txs; + std::list missed_txs; + std::list txs; m_core.getTransactions(tx_ids, txs, missed_txs); if (1 == txs.size()) { - Transaction transaction; - if (!fromBinaryArray(transaction, txs.front())) { - throw std::runtime_error("Couldn't deserialize transaction"); - } - res.tx = transaction; + res.tx = txs.front(); } else { throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_WRONG_PARAM, "transaction wasn't found. Hash = " + req.hash + '.' }; } - TransactionDetails transactionDetails = m_core.getTransactionDetails(hash); Crypto::Hash blockHash; - if (transactionDetails.inBlockchain) { - uint32_t blockHeight = transactionDetails.blockIndex; - if (!blockHeight) { - throw JsonRpc::JsonRpcError{ - CORE_RPC_ERROR_CODE_INTERNAL_ERROR, - "Internal error: can't get transaction by hash. Hash = " + Common::podToHex(hash) + '.' }; + uint32_t blockHeight; + if (m_core.getBlockContainingTx(hash, blockHash, blockHeight)) { + Block blk; + if (m_core.getBlockByHash(blockHash, blk)) { + size_t tx_cumulative_block_size; + m_core.getBlockSize(blockHash, tx_cumulative_block_size); + size_t blokBlobSize = getObjectBinarySize(blk); + size_t minerTxBlobSize = getObjectBinarySize(blk.baseTransaction); + f_block_short_response block_short; + + block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; + block_short.timestamp = blk.timestamp; + block_short.height = blockHeight; + block_short.hash = Common::podToHex(blockHash); + block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; + block_short.tx_count = blk.transactionHashes.size() + 1; + res.block = block_short; } - blockHash = m_core.getBlockHashByIndex(blockHeight); - BlockTemplate blk = m_core.getBlockByHash(blockHash); - BlockDetails blkDetails = m_core.getBlockDetails(blockHash); - - f_block_short_response block_short; - - block_short.cumul_size = blkDetails.blockSize; - block_short.timestamp = blk.timestamp; - block_short.height = blockHeight; - block_short.hash = Common::podToHex(blockHash); - block_short.tx_count = blk.transactionHashes.size() + 1; - res.block = block_short; } - uint64_t amount_in = getInputAmount(res.tx); - uint64_t amount_out = getOutputAmount(res.tx); + uint64_t amount_in = 0; + get_inputs_money_amount(res.tx, amount_in); + uint64_t amount_out = get_outs_money_amount(res.tx); res.txDetails.hash = Common::podToHex(getObjectHash(res.tx)); res.txDetails.fee = amount_in - amount_out; @@ -662,26 +703,6 @@ bool RpcServer::f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAIL return true; } - -bool RpcServer::f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res) { - - auto pool = m_core.getPoolTransactions(); - for (const Transaction tx : pool) { - f_transaction_short_response transaction_short; - uint64_t amount_in = getInputAmount(tx); - uint64_t amount_out = getOutputAmount(tx); - - transaction_short.hash = Common::podToHex(getObjectHash(tx)); - transaction_short.fee = amount_in - amount_out; - transaction_short.amount_out = amount_out; - transaction_short.size = getObjectBinarySize(tx); - res.transactions.push_back(transaction_short); - } - - res.status = CORE_RPC_STATUS_OK; - return true; -} - bool RpcServer::f_getMixin(const Transaction& transaction, uint64_t& mixin) { mixin = 0; for (const TransactionInput& txin : transaction.inputs) { @@ -696,120 +717,6 @@ bool RpcServer::f_getMixin(const Transaction& transaction, uint64_t& mixin) { return true; } -bool RpcServer::f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res) { - res.base_coin.name = "bytecoin"; - res.base_coin.git = "https://github.com/amjuarez/bytecoin.git"; - - // Hardcoded plugins, refactor this - res.extensions.push_back("core/bytecoin.json"); - res.extensions.push_back("bug-fixes.json"); - res.extensions.push_back("print-genesis-tx.json"); - - if (m_core.getCurrency().minMixin() != 0 || m_core.getCurrency().mandatoryMixinBlockVersion() != 0) { - res.extensions.push_back("mix-mixin.json"); - } - if (m_core.getCurrency().mixinStartHeight() != 0) { - res.extensions.push_back("mixin-start-height.json"); - } - if (m_core.getCurrency().mandatoryTransaction() == 1) { - res.extensions.push_back("mandatory-transaction-in-block.json"); - } - if (m_core.getCurrency().killHeight() != 0) { - res.extensions.push_back("kill-height.json"); - } - if (m_core.getCurrency().tailEmissionReward() != 0) { - res.extensions.push_back("tail-emission-reward.json"); - } - if (m_core.getCurrency().cryptonoteCoinVersion() != 0) { - res.extensions.push_back("cryptonote-coin-clones-support.json"); - } - if (m_core.getCurrency().genesisBlockReward() != 0) { - res.extensions.push_back("genesis-block-reward.json"); - } - if (m_core.getCurrency().difficultyWindowByBlockVersion(1) != m_core.getCurrency().difficultyWindowByBlockVersion(2) || m_core.getCurrency().difficultyWindowByBlockVersion(1) != m_core.getCurrency().difficultyWindowByBlockVersion(3) || - m_core.getCurrency().difficultyLagByBlockVersion(1) != m_core.getCurrency().difficultyLagByBlockVersion(2) || m_core.getCurrency().difficultyLagByBlockVersion(1) != m_core.getCurrency().difficultyLagByBlockVersion(3) || - m_core.getCurrency().difficultyCutByBlockVersion(1) != m_core.getCurrency().difficultyCutByBlockVersion(2) || m_core.getCurrency().difficultyCutByBlockVersion(1) != m_core.getCurrency().difficultyCutByBlockVersion(3)) { - res.extensions.push_back("versionized-parameters.json"); - } - if (m_core.getCurrency().zawyDifficultyBlockIndex() != 0 ) { - res.extensions.push_back("zawy-difficulty-algorithm.json"); - } - if (m_core.getCurrency().zawyLWMADifficultyBlockIndex() != 0 ) { - res.extensions.push_back("zawy-lwma-difficulty-algorithm.json"); - } - if (m_core.getCurrency().buggedZawyDifficultyBlockIndex() != 0 ) { - res.extensions.push_back("bugged-zawy-difficulty-algorithm.json"); - } - res.core.CRYPTONOTE_NAME = m_core.getCurrency().cryptonoteName(); - - res.core.EMISSION_SPEED_FACTOR = m_core.getCurrency().emissionSpeedFactor(); - res.core.DIFFICULTY_TARGET = m_core.getCurrency().difficultyTarget(); - res.core.CRYPTONOTE_DISPLAY_DECIMAL_POINT = m_core.getCurrency().numberOfDecimalPlaces(); - res.core.MONEY_SUPPLY = std::to_string(m_core.getCurrency().moneySupply()); - res.core.EXPECTED_NUMBER_OF_BLOCKS_PER_DAY = m_core.getCurrency().expectedNumberOfBlocksPerDay(); - res.core.DEFAULT_DUST_THRESHOLD = m_core.getCurrency().defaultDustThreshold(); - res.core.MINIMUM_FEE = m_core.getCurrency().minimumFee(); - res.core.CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW = m_core.getCurrency().minedMoneyUnlockWindow(); - res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE = m_core.getCurrency().blockGrantedFullRewardZone(); - res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 = m_core.getCurrency().blockGrantedFullRewardZoneV1(); - res.core.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2 = m_core.getCurrency().blockGrantedFullRewardZoneV2(); - res.core.CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX = m_core.getCurrency().publicAddressBase58Prefix(); - res.core.MAX_BLOCK_SIZE_INITIAL = m_core.getCurrency().maxBlockSizeInitial(); - res.core.UPGRADE_HEIGHT_V2 = m_core.getCurrency().upgradeHeight(2); - res.core.UPGRADE_HEIGHT_V3 = m_core.getCurrency().upgradeHeight(3); - res.core.DIFFICULTY_WINDOW = m_core.getCurrency().difficultyWindow(); - res.core.DIFFICULTY_CUT = m_core.getCurrency().difficultyCut(); - res.core.DIFFICULTY_LAG = m_core.getCurrency().difficultyLag(); - - if (m_core.getCurrency().minMixin() != 0 || m_core.getCurrency().mandatoryMixinBlockVersion() != 0) { - res.core.MIN_MIXIN = m_core.getCurrency().minMixin(); - res.core.MANDATORY_MIXIN_BLOCK_VERSION = m_core.getCurrency().mandatoryMixinBlockVersion(); - } - if (m_core.getCurrency().mixinStartHeight() != 0) { - res.core.MIXIN_START_HEIGHT = m_core.getCurrency().mixinStartHeight(); - } - res.core.MANDATORY_TRANSACTION = m_core.getCurrency().mandatoryTransaction(); - res.core.KILL_HEIGHT = m_core.getCurrency().killHeight(); - res.core.TAIL_EMISSION_REWARD = m_core.getCurrency().tailEmissionReward(); - res.core.CRYPTONOTE_COIN_VERSION = m_core.getCurrency().cryptonoteCoinVersion(); - res.core.GENESIS_BLOCK_REWARD = std::to_string(m_core.getCurrency().genesisBlockReward()); - res.core.DIFFICULTY_WINDOW_V1 = m_core.getCurrency().difficultyWindowV1(); - res.core.DIFFICULTY_WINDOW_V2 = m_core.getCurrency().difficultyWindowV2(); - res.core.DIFFICULTY_CUT_V1 = m_core.getCurrency().difficultyCutV1(); - res.core.DIFFICULTY_CUT_V2 = m_core.getCurrency().difficultyCutV2(); - res.core.DIFFICULTY_LAG_V1 = m_core.getCurrency().difficultyLagV1(); - res.core.DIFFICULTY_LAG_V2 = m_core.getCurrency().difficultyLagV2(); - res.core.ZAWY_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().zawyDifficultyBlockIndex(); - res.core.ZAWY_DIFFICULTY_LAST_BLOCK = m_core.getCurrency().zawyDifficultyLastBlock(); - res.core.ZAWY_LWMA_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().zawyLWMADifficultyBlockIndex(); - res.core.ZAWY_LWMA_DIFFICULTY_LAST_BLOCK = m_core.getCurrency().zawyLWMADifficultyLastBlock(); - res.core.ZAWY_LWMA_DIFFICULTY_N = m_core.getCurrency().zawyLWMADifficultyN(); - res.core.BUGGED_ZAWY_DIFFICULTY_BLOCK_INDEX = m_core.getCurrency().buggedZawyDifficultyBlockIndex(); - res.core.P2P_DEFAULT_PORT = m_p2p.get_this_peer_port(); - // Not real. Change - res.core.RPC_DEFAULT_PORT = m_p2p.get_this_peer_port() + 1; - - for (const NetworkAddress& na : m_p2p.get_seed_nodes()) { - std::string na_string = Common::ipAddressToString(na.ip) + ":" + std::to_string(na.port); - - res.core.SEED_NODES.push_back(na_string); - } - - res.core.BYTECOIN_NETWORK = boost::lexical_cast(m_p2p.get_network_id()); - - std::map cp; - cp = m_core.get_checkpoints().get_checkpoints(); - for (auto i : cp) { - std::string cp_string = std::to_string(i.first) + ":" + Common::podToHex(i.second); - - res.core.CHECKPOINTS.push_back(cp_string); - } - - res.core.GENESIS_COINBASE_TX_HEX = Common::toHex(CryptoNote::toBinaryArray(m_core.getCurrency().genesisBlock().baseTransaction)); - - res.status = CORE_RPC_STATUS_OK; - return true; -} bool RpcServer::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res) { res.count = m_core.get_current_blockchain_height(); @@ -825,7 +732,7 @@ bool RpcServer::on_getblockhash(const COMMAND_RPC_GETBLOCKHASH::request& req, CO uint32_t h = static_cast(req[0]); Crypto::Hash blockId = m_core.getBlockIdByHeight(h); if (blockId == NULL_HASH) { - throw JsonRpc::JsonRpcError{ + throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT, std::string("To big height: ") + std::to_string(h) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height()) }; @@ -955,14 +862,14 @@ void RpcServer::fill_block_header_response(const Block& blk, bool orphan_status, bool RpcServer::on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res) { uint32_t last_block_height; Hash last_block_hash; - + m_core.get_blockchain_top(last_block_height, last_block_hash); Block last_block; if (!m_core.getBlockByHash(last_block_hash, last_block)) { throw JsonRpc::JsonRpcError{ CORE_RPC_ERROR_CODE_INTERNAL_ERROR, "Internal error: can't get last block hash." }; } - + fill_block_header_response(last_block, false, last_block_height, last_block_hash, res.block_header); res.status = CORE_RPC_STATUS_OK; return true; From 1fa5a0bc162d50ca7b1b5f040ba8a2b370ca3200 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 18:59:05 -0700 Subject: [PATCH 05/12] merge SDN code --- src/Rpc/RpcServer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Rpc/RpcServer.h b/src/Rpc/RpcServer.h index 39007ef6..d5953ec0 100755 --- a/src/Rpc/RpcServer.h +++ b/src/Rpc/RpcServer.h @@ -9,6 +9,7 @@ #include #include +#include "Common/Math.h" #include "CoreRpcServerCommandsDefinitions.h" namespace CryptoNote { @@ -71,9 +72,8 @@ class RpcServer : public HttpServer { bool f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::request& req, F_COMMAND_RPC_GET_BLOCKS_LIST::response& res); bool f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& req, F_COMMAND_RPC_GET_BLOCK_DETAILS::response& res); bool f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAILS::request& req, F_COMMAND_RPC_GET_TRANSACTION_DETAILS::response& res); - bool f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res); bool f_getMixin(const Transaction& transaction, uint64_t& mixin); - bool f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res); + // bool f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res); Logging::LoggerRef logger; core& m_core; From dff97bf51df072d84dd665b20e49f56ab7709bca Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 19:34:32 -0700 Subject: [PATCH 06/12] merge sdn code --- src/CryptoNoteCore/CryptoNoteFormatUtils.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/CryptoNoteCore/CryptoNoteFormatUtils.cpp b/src/CryptoNoteCore/CryptoNoteFormatUtils.cpp index 8f93ea08..e8772d9b 100644 --- a/src/CryptoNoteCore/CryptoNoteFormatUtils.cpp +++ b/src/CryptoNoteCore/CryptoNoteFormatUtils.cpp @@ -224,6 +224,23 @@ bool constructTransaction( return true; } +bool get_inputs_money_amount(const Transaction& tx, uint64_t& money) { + money = 0; + + for (const auto& in : tx.inputs) { + uint64_t amount = 0; + + if (in.type() == typeid(KeyInput)) { + amount = boost::get(in).amount; + } else if (in.type() == typeid(MultisignatureInput)) { + amount = boost::get(in).amount; + } + + money += amount; + } + return true; +} + uint32_t get_block_height(const Block& b) { if (b.baseTransaction.inputs.size() != 1) { return 0; From 71112b069b00b68dd11551e3c2269e7868ea4d9a Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 19:35:01 -0700 Subject: [PATCH 07/12] merge sdn code --- src/CryptoNoteCore/CryptoNoteFormatUtils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CryptoNoteCore/CryptoNoteFormatUtils.h b/src/CryptoNoteCore/CryptoNoteFormatUtils.h index 3df5a350..647e80af 100755 --- a/src/CryptoNoteCore/CryptoNoteFormatUtils.h +++ b/src/CryptoNoteCore/CryptoNoteFormatUtils.h @@ -75,6 +75,7 @@ bool get_aux_block_header_hash(const Block& b, Crypto::Hash& res); bool get_block_hash(const Block& b, Crypto::Hash& res); Crypto::Hash get_block_hash(const Block& b); bool get_block_longhash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res); +bool get_inputs_money_amount(const Transaction& tx, uint64_t& money); uint64_t get_outs_money_amount(const Transaction& tx); bool check_inputs_types_supported(const TransactionPrefix& tx); bool check_outs_valid(const TransactionPrefix& tx, std::string* error = 0); From 932b3e7b777965a0efa3227cebf952cf3ca8e9e8 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:31:03 -0700 Subject: [PATCH 08/12] add transaction pool --- src/Rpc/RpcServer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Rpc/RpcServer.h b/src/Rpc/RpcServer.h index d5953ec0..f89cfdf1 100755 --- a/src/Rpc/RpcServer.h +++ b/src/Rpc/RpcServer.h @@ -72,6 +72,7 @@ class RpcServer : public HttpServer { bool f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::request& req, F_COMMAND_RPC_GET_BLOCKS_LIST::response& res); bool f_on_block_json(const F_COMMAND_RPC_GET_BLOCK_DETAILS::request& req, F_COMMAND_RPC_GET_BLOCK_DETAILS::response& res); bool f_on_transaction_json(const F_COMMAND_RPC_GET_TRANSACTION_DETAILS::request& req, F_COMMAND_RPC_GET_TRANSACTION_DETAILS::response& res); + bool f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res); bool f_getMixin(const Transaction& transaction, uint64_t& mixin); // bool f_on_get_blockchain_settings(const F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::request& req, F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS::response& res); From 134307be4b962590269bce72c660316219445e82 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:31:25 -0700 Subject: [PATCH 09/12] add transaction pool --- src/Rpc/RpcServer.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Rpc/RpcServer.cpp b/src/Rpc/RpcServer.cpp index c4b72c4c..7e4a3f48 100755 --- a/src/Rpc/RpcServer.cpp +++ b/src/Rpc/RpcServer.cpp @@ -133,6 +133,7 @@ bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse& { "f_blocks_list_json", { makeMemberMethod(&RpcServer::f_on_blocks_list_json), false } }, { "f_block_json", { makeMemberMethod(&RpcServer::f_on_block_json), false } }, { "f_transaction_json", { makeMemberMethod(&RpcServer::f_on_transaction_json), false } }, + { "f_on_transactions_pool_json", { makeMemberMethod(&RpcServer::f_on_transactions_pool_json), false } }, // { "f_get_blockchain_settings", { makeMemberMethod(&RpcServer::f_on_get_blockchain_settings), true } }, { "getblockcount", { makeMemberMethod(&RpcServer::on_getblockcount), true } }, { "on_getblockhash", { makeMemberMethod(&RpcServer::on_getblockhash), false } }, @@ -326,7 +327,15 @@ bool RpcServer::on_get_info(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RP res.white_peerlist_size = m_p2p.getPeerlistManager().get_white_peers_count(); res.grey_peerlist_size = m_p2p.getPeerlistManager().get_gray_peers_count(); res.last_known_block_index = std::max(static_cast(1), m_protocolQuery.getObservedHeight()) - 1; - res.full_deposit_amount = m_core.fullDepositAmount(); + + + uint64_t height = m_core.get_current_blockchain_height() - 1; + uint64_t totalCoinsInNetwork = m_core.coinsEmittedAtHeight(height); + uint64_t totalCoinsOnDeposits = m_core.depositAmountAtHeight(height); + if(totalCoinsOnDeposits > totalCoinsInNetwork){ + totalCoinsOnDeposits = totalCoinsInNetwork - (totalCoinsOnDeposits - totalCoinsInNetwork ) - (totalCoinsInNetwork * 0.0375 ); + } + res.full_deposit_amount = totalCoinsOnDeposits; res.full_deposit_interest = m_core.fullDepositInterest(); res.status = CORE_RPC_STATUS_OK; return true; @@ -477,6 +486,7 @@ bool RpcServer::f_on_blocks_list_json(const F_COMMAND_RPC_GET_BLOCKS_LIST::reque block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; block_short.timestamp = blk.timestamp; block_short.height = i; + m_core.getBlockDifficulty(static_cast(block_short.height), block_short.difficulty); block_short.hash = Common::podToHex(block_hash); block_short.cumul_size = blokBlobSize + tx_cumulative_block_size - minerTxBlobSize; block_short.tx_count = blk.transactionHashes.size() + 1; @@ -717,6 +727,26 @@ bool RpcServer::f_getMixin(const Transaction& transaction, uint64_t& mixin) { return true; } +bool RpcServer::f_on_transactions_pool_json(const F_COMMAND_RPC_GET_POOL::request& req, F_COMMAND_RPC_GET_POOL::response& res) { + auto pool = m_core.getPoolTransactions(); + for (const Transaction tx : pool) { + f_transaction_short_response transaction_short; + uint64_t amount_in = getInputAmount(tx); + uint64_t amount_out = getOutputAmount(tx); + + transaction_short.hash = Common::podToHex(getObjectHash(tx)); + transaction_short.fee = + amount_in < amount_out + parameters::MINIMUM_FEE //account for interest in output, it always has minimum fee + ? parameters::MINIMUM_FEE + : amount_in - amount_out; + transaction_short.amount_out = amount_out; + transaction_short.size = getObjectBinarySize(tx); + res.transactions.push_back(transaction_short); + } + + res.status = CORE_RPC_STATUS_OK; + return true; +} bool RpcServer::on_getblockcount(const COMMAND_RPC_GETBLOCKCOUNT::request& req, COMMAND_RPC_GETBLOCKCOUNT::response& res) { res.count = m_core.get_current_blockchain_height(); From e00a891a3dcb3efc91b88f7a861cb3f1a7769ce7 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:44:45 -0700 Subject: [PATCH 10/12] add F_COMMAND_RPC_GET_POOL --- src/Rpc/CoreRpcServerCommandsDefinitions.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Rpc/CoreRpcServerCommandsDefinitions.h b/src/Rpc/CoreRpcServerCommandsDefinitions.h index f4bd6a51..93818e6e 100755 --- a/src/Rpc/CoreRpcServerCommandsDefinitions.h +++ b/src/Rpc/CoreRpcServerCommandsDefinitions.h @@ -438,6 +438,7 @@ struct f_transaction_details_response { struct f_block_short_response { uint64_t timestamp; uint32_t height; + difficulty_type difficulty; std::string hash; uint64_t tx_count; uint64_t cumul_size; @@ -445,6 +446,7 @@ struct f_block_short_response { void serialize(ISerializer &s) { KV_MEMBER(timestamp) KV_MEMBER(height) + KV_MEMBER(difficulty) KV_MEMBER(hash) KV_MEMBER(cumul_size) KV_MEMBER(tx_count) @@ -658,6 +660,21 @@ struct F_COMMAND_RPC_GET_TRANSACTION_DETAILS { } }; }; + +struct F_COMMAND_RPC_GET_POOL { + typedef EMPTY_STRUCT request; + + struct response { + std::vector transactions; //transactions blobs as hex + std::string status; + + void serialize(ISerializer &s) { + KV_MEMBER(transactions) + KV_MEMBER(status) + } + }; +}; + struct F_COMMAND_RPC_GET_BLOCKCHAIN_SETTINGS { typedef EMPTY_STRUCT request; struct response { From f3dcc7e98010accee5a604bf27019fb7241cc788 Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:52:25 -0700 Subject: [PATCH 11/12] add getInputAmount --- src/CryptoNoteCore/CryptoNoteTools.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CryptoNoteCore/CryptoNoteTools.h b/src/CryptoNoteCore/CryptoNoteTools.h index 6c3e28f1..d756bc65 100755 --- a/src/CryptoNoteCore/CryptoNoteTools.h +++ b/src/CryptoNoteCore/CryptoNoteTools.h @@ -32,7 +32,7 @@ bool toBinaryArray(const T& object, BinaryArray& binaryArray) { } template<> -bool toBinaryArray(const BinaryArray& object, BinaryArray& binaryArray); +bool toBinaryArray(const BinaryArray& object, BinaryArray& binaryArray); template BinaryArray toBinaryArray(const T& object) { @@ -107,6 +107,7 @@ Crypto::Hash getObjectHash(const T& object) { return hash; } +uint64_t getInputAmount(const Transaction& transaction); std::vector getInputsAmounts(const Transaction& transaction); uint64_t getOutputAmount(const Transaction& transaction); void decomposeAmount(uint64_t amount, uint64_t dustThreshold, std::vector& decomposedAmounts); From e982224955eb054aa79c2b801b35cb4ec29251bc Mon Sep 17 00:00:00 2001 From: miningapps <37049063+miningapps@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:53:34 -0700 Subject: [PATCH 12/12] add getInputAmount --- src/CryptoNoteCore/CryptoNoteTools.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/CryptoNoteCore/CryptoNoteTools.cpp b/src/CryptoNoteCore/CryptoNoteTools.cpp index fa27c2b0..5fee8d9d 100755 --- a/src/CryptoNoteCore/CryptoNoteTools.cpp +++ b/src/CryptoNoteCore/CryptoNoteTools.cpp @@ -31,6 +31,19 @@ Crypto::Hash getBinaryArrayHash(const BinaryArray& binaryArray) { return hash; } +uint64_t getInputAmount(const Transaction& transaction) { + uint64_t amount = 0; + for (auto& input : transaction.inputs) { + if (input.type() == typeid(KeyInput)) { + amount += boost::get(input).amount; + } else if (input.type() == typeid(MultisignatureInput)) { + amount += boost::get(input).amount; + } + } + + return amount; +} + std::vector getInputsAmounts(const Transaction& transaction) { std::vector inputsAmounts; inputsAmounts.reserve(transaction.inputs.size());