From 31ac055c65de37e0e5b8b478064e6bdee6bd8c1c Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Mon, 12 Apr 2021 16:16:32 +0200 Subject: [PATCH] update model::gradido::Transaction::createTransfer for using with blockchain mysql, update return type update functions calling model::gradido::Transaction::createTransfer to handle on transaction as return instead of vector --- .../HTTPInterface/AdminNodeServerTestPage.cpp | 12 +- .../JSONInterface/JsonCreateTransaction.cpp | 12 +- .../src/cpp/model/gradido/Transaction.cpp | 111 +++++++++--------- .../src/cpp/model/gradido/Transaction.h | 4 +- .../src/cpsp/adminNodeServerTest.cpsp | 12 +- 5 files changed, 77 insertions(+), 74 deletions(-) diff --git a/login_server/src/cpp/HTTPInterface/AdminNodeServerTestPage.cpp b/login_server/src/cpp/HTTPInterface/AdminNodeServerTestPage.cpp index 0adc857fb..e623f9baf 100644 --- a/login_server/src/cpp/HTTPInterface/AdminNodeServerTestPage.cpp +++ b/login_server/src/cpp/HTTPInterface/AdminNodeServerTestPage.cpp @@ -657,8 +657,8 @@ void AdminNodeServerTestPage::handleRequest(Poco::Net::HTTPServerRequest& reques auto transaction4 = model::gradido::Transaction::createTransfer(user_2, user_1_pubkey, user_group, 5000000, "Test Transfer", model::gradido::BLOCKCHAIN_HEDERA); // wait before sending fourth transaction, gn seems to crash by more than 3 transaction at nearly the same time Poco::Thread::sleep(sleep_ms_between_transactions); - transaction4[0]->sign(user_2); - auto transaction4_json = transaction4[0]->getTransactionAsJson(true); + transaction4->sign(user_2); + auto transaction4_json = transaction4->getTransactionAsJson(true); responseStream << "\n"; responseStream << "\t\t\t

"; #line 321 "F:\\Gradido\\gradido_stage2_local\\login_server\\src\\cpsp\\adminNodeServerTest.cpsp" @@ -714,7 +714,7 @@ void AdminNodeServerTestPage::handleRequest(Poco::Net::HTTPServerRequest& reques } else { auto user_3_pubkey = user_3->getModel()->getPublicKeyCopy(); auto transaction6 = model::gradido::Transaction::createTransfer(user_2, user_3_pubkey, user_group2, 4000000, "Test Group Transfer", model::gradido::BLOCKCHAIN_HEDERA, false); - if(!transaction6.size()) { + if(transaction6.isNull()) { responseStream << "\n"; responseStream << "\t\t\t\t

\n"; responseStream << "\t\t\t\t\treport_problem\n"; @@ -725,9 +725,9 @@ void AdminNodeServerTestPage::handleRequest(Poco::Net::HTTPServerRequest& reques } else { Poco::Thread::sleep(sleep_ms_between_transactions); - transaction6[0]->sign(user_2); - auto transaction6_json = transaction6[0]->getTransactionAsJson(true); - auto paired_transaction = transaction6[0]->getPairedTransaction(); + transaction6->sign(user_2); + auto transaction6_json = transaction6->getTransactionAsJson(true); + auto paired_transaction = transaction6->getPairedTransaction(); responseStream << "\n"; responseStream << "\t\t\t\t\t

"; #line 364 "F:\\Gradido\\gradido_stage2_local\\login_server\\src\\cpsp\\adminNodeServerTest.cpsp" diff --git a/login_server/src/cpp/JSONInterface/JsonCreateTransaction.cpp b/login_server/src/cpp/JSONInterface/JsonCreateTransaction.cpp index 69c12cc87..8c4814fc9 100644 --- a/login_server/src/cpp/JSONInterface/JsonCreateTransaction.cpp +++ b/login_server/src/cpp/JSONInterface/JsonCreateTransaction.cpp @@ -122,15 +122,13 @@ Poco::JSON::Object* JsonCreateTransaction::transfer(Poco::Dynamic::Var params) } } if (!result) { - auto transactions = model::gradido::Transaction::createTransfer(mSession->getNewUser(), target_pubkey, mTargetGroup, amount, mMemo, mBlockchainType); + auto transaction = model::gradido::Transaction::createTransfer(mSession->getNewUser(), target_pubkey, mTargetGroup, amount, mMemo, mBlockchainType); if (mAutoSign) { - Poco::JSON::Array errors; - for (auto it = transactions.begin(); it != transactions.end(); it++) { - (*it)->sign(user); - if ((*it)->errorCount() > 0) { - errors.add((*it)->getErrorsArray()); - } + Poco::JSON::Array errors; + transaction->sign(user); + if (transaction->errorCount() > 0) { + errors.add(transaction->getErrorsArray()); } if (errors.size() > 0) { diff --git a/login_server/src/cpp/model/gradido/Transaction.cpp b/login_server/src/cpp/model/gradido/Transaction.cpp index 9cc9cf658..c8708ce35 100644 --- a/login_server/src/cpp/model/gradido/Transaction.cpp +++ b/login_server/src/cpp/model/gradido/Transaction.cpp @@ -124,7 +124,7 @@ namespace model { return result; } - std::vector> Transaction::createTransfer( + Poco::AutoPtr Transaction::createTransfer( Poco::AutoPtr sender, const MemoryBin* receiverPubkey, Poco::AutoPtr receiverGroup, @@ -134,53 +134,64 @@ namespace model { bool inbound/* = true*/ ) { - std::vector> results; + Poco::AutoPtr transaction; + Poco::AutoPtr transaction_body; + Poco::AutoPtr topic_id; auto em = ErrorManager::getInstance(); static const char* function_name = "Transaction::create transfer"; if (sender.isNull() || !sender->getModel() || !receiverPubkey || !amount) { - return results; + return transaction; } - //std::vector> bodys; auto sender_model = sender->getModel(); - auto network_type = ServerConfig::g_HederaNetworkType; - // LOCAL Transfer - if (receiverGroup.isNull() || sender_model->getGroupId() == receiverGroup->getModel()->getID()) - { - auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType); - Poco::AutoPtr transaction = new Transaction(body); - auto topic_id = controller::HederaId::find(sender_model->getGroupId(), network_type); - if (topic_id.isNull()) { - em->addError(new ParamError(function_name, "could'n find topic for group: ", sender_model->getGroupId())); - em->addError(new ParamError(function_name, "network type: ", network_type)); - em->sendErrorsAsEmail(); - return results; - } - transaction->getModel()->setHederaId(topic_id->getModel()->getID()); - results.push_back(transaction); + + + if (blockchainType == BLOCKCHAIN_MYSQL) { + transaction_body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType); + transaction = new Transaction(transaction_body); } - else + else if (blockchainType == BLOCKCHAIN_HEDERA) { - auto sender_group = controller::Group::load(sender_model->getGroupId()); - if (sender_group.isNull()) + auto network_type = ServerConfig::g_HederaNetworkType; + + + // LOCAL Transfer + if (receiverGroup.isNull() || sender_model->getGroupId() == receiverGroup->getModel()->getID()) { - em->addError(new ParamError(function_name, "couldn't find group with id: ", sender_model->getGroupId())); - em->sendErrorsAsEmail(); - return results; + topic_id = controller::HederaId::find(sender_model->getGroupId(), network_type); + + if (topic_id.isNull()) + { + em->addError(new ParamError(function_name, "could'n find topic for group: ", sender_model->getGroupId())); + em->addError(new ParamError(function_name, "network type: ", network_type)); + em->sendErrorsAsEmail(); + return transaction; + } + transaction_body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType); + transaction = new Transaction(transaction_body); } - Poco::AutoPtr transaction_group; - Poco::AutoPtr topic_group; - // default constructor set it to now - Poco::Timestamp pairedTransactionId; - // create only inbound transaction, and outbound before sending to hedera - //for (int i = 0; i < 1; i++) { + else + { + auto sender_group = controller::Group::load(sender_model->getGroupId()); + if (sender_group.isNull()) + { + em->addError(new ParamError(function_name, "couldn't find group with id: ", sender_model->getGroupId())); + em->sendErrorsAsEmail(); + return transaction; + } + Poco::AutoPtr transaction_group; + Poco::AutoPtr topic_group; + // default constructor set it to now + Poco::Timestamp pairedTransactionId; + // create only inbound transaction, and outbound before sending to hedera + //for (int i = 0; i < 1; i++) { if (!inbound) { transaction_group = receiverGroup; topic_group = sender_group; } // transaction send to receiver blockchain - else if(inbound) { + else if (inbound) { transaction_group = sender_group; topic_group = receiverGroup; } @@ -189,38 +200,32 @@ namespace model { em->addError(new ParamError(function_name, "could'n find topic for group: ", sender_model->getGroupId())); em->addError(new ParamError(function_name, "network type: ", network_type)); em->sendErrorsAsEmail(); - return results; + return transaction; } if (transaction_group.isNull()) { em->addError(new ParamError(function_name, "transaction group is zero, inbound", inbound)); em->sendErrorsAsEmail(); - return results; + return transaction; } auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType, pairedTransactionId, transaction_group); Poco::AutoPtr transaction = new Transaction(body); - transaction->getModel()->setHederaId(topic_id->getModel()->getID()); - + auto transfer_transaction = transaction->getTransactionBody()->getTransferTransaction(); transfer_transaction->setOwnGroupAlias(sender_group->getModel()->getAlias()); transfer_transaction->setTargetGroupAlias(receiverGroup->getModel()->getAlias()); - - results.push_back(transaction); - // } - } - - - for (auto it = results.begin(); it != results.end(); it++) { - if (!(*it)->getTransactionBody()->getTransferTransaction()->isInbound()) { - (*it)->setParam("blockchain_type", (int)blockchainType); - (*it)->insertPendingTaskIntoDB(sender, model::table::TASK_TYPE_TRANSFER); - PendingTasksManager::getInstance()->addTask(*it); - } - } - - - return results; + } + auto transaction_model = transaction->getModel(); + transaction_model->setHederaId(topic_id->getModel()->getID()); + + } + + transaction->setParam("blockchain_type", (int)blockchainType); + transaction->insertPendingTaskIntoDB(sender, model::table::TASK_TYPE_TRANSFER); + PendingTasksManager::getInstance()->addTask(transaction); + + return transaction; } bool Transaction::setTopicIdByGroup(const std::string& alias) @@ -326,7 +331,7 @@ namespace model { finished = true; } } - // try not finished but sign transactions again + // try not finished but signed transactions again if (!finished) { transaction->ifEnoughSignsProceed(nullptr); } diff --git a/login_server/src/cpp/model/gradido/Transaction.h b/login_server/src/cpp/model/gradido/Transaction.h index 5988b250e..9ff59100e 100644 --- a/login_server/src/cpp/model/gradido/Transaction.h +++ b/login_server/src/cpp/model/gradido/Transaction.h @@ -32,8 +32,8 @@ namespace model { // groupMemberUpdate static Poco::AutoPtr createGroupMemberUpdate(Poco::AutoPtr user, Poco::AutoPtr group); //! \brief transfer - //! \return for cross group transaction return two transactions - static std::vector> createTransfer( + //! \return + static Poco::AutoPtr createTransfer( Poco::AutoPtr sender, const MemoryBin* receiverPubkey, Poco::AutoPtr receiverGroup, diff --git a/login_server/src/cpsp/adminNodeServerTest.cpsp b/login_server/src/cpsp/adminNodeServerTest.cpsp index c1ea3ff0d..e821a9f43 100644 --- a/login_server/src/cpsp/adminNodeServerTest.cpsp +++ b/login_server/src/cpsp/adminNodeServerTest.cpsp @@ -315,8 +315,8 @@ enum PageType auto transaction4 = model::gradido::Transaction::createTransfer(user_2, user_1_pubkey, user_group, 5000000, "Test Transfer", model::gradido::BLOCKCHAIN_HEDERA); // wait before sending fourth transaction, gn seems to crash by more than 3 transaction at nearly the same time Poco::Thread::sleep(sleep_ms_between_transactions); - transaction4[0]->sign(user_2); - auto transaction4_json = transaction4[0]->getTransactionAsJson(true); + transaction4->sign(user_2); + auto transaction4_json = transaction4->getTransactionAsJson(true); %>

<%= DataTypeConverter::replaceNewLineWithBr(transaction4_json) %>

<% } %> @@ -348,7 +348,7 @@ enum PageType <% } else { auto user_3_pubkey = user_3->getModel()->getPublicKeyCopy(); auto transaction6 = model::gradido::Transaction::createTransfer(user_2, user_3_pubkey, user_group2, 4000000, "Test Group Transfer", model::gradido::BLOCKCHAIN_HEDERA, false); - if(!transaction6.size()) { + if(transaction6.isNull()) { %>