Skip to content

Commit

Permalink
Refactoring: enable c++20 support (#1968)
Browse files Browse the repository at this point in the history
* BEAM_LOG_WARNING

BEAM_LOG_UNHANDLED_EXCEPTION
BEAM_LOG_CRITICAL
BEAM_LOG_ERROR
BEAM_LOG_INFO

BEAM_LOG_MESSAGE

* try c++20

* WIP: tests refactoring

fixed atomic_swap.cpp

* (c)'ts

* README.md

* strict c++20 (this capture)

* fixed ;

---------

Co-authored-by: Anatol Sevastsyan <anatol@beam-mw.com>
  • Loading branch information
Tatsujin1978 and anatolse authored May 11, 2024
1 parent 008bcdd commit 902be6b
Show file tree
Hide file tree
Showing 189 changed files with 1,734 additions and 1,715 deletions.
53 changes: 30 additions & 23 deletions 3rdparty/libbitcoin/examples/atomic_swap.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Beam Team
// Copyright 2018-2024 The Beam Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -167,16 +167,16 @@ namespace
request.body(requestData.data(), requestData.size());
//request.body(data, strlen(data));

LOG_INFO() << requestData << "\n";
BEAM_LOG_INFO() << requestData << "\n";

request.callback([callback](uint64_t id, const HttpMsgReader::Message& msg) -> bool {
LOG_INFO() << "response from " << id;
BEAM_LOG_INFO() << "response from " << id;
size_t sz = 0;
const void* body = msg.msg->get_body(sz);
if (sz > 0 && body)
{
json j = json::parse(std::string(static_cast<const char*>(body), sz));
LOG_INFO() << j.dump(JSON_FORMAT_INDENT);
BEAM_LOG_INFO() << j.dump(JSON_FORMAT_INDENT);

callback(j);
}
Expand Down Expand Up @@ -305,8 +305,8 @@ void AtomicSwapHelper::OnFundRawTransaction(const json& reply)
m_fee = result["fee"].get<float>(); // calculate fee!
m_valuePos = changePos ? 0 : 1;

LOG_INFO() << "error: " << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << "result: " << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << "error: " << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << "result: " << result.dump(JSON_FORMAT_INDENT) << "\n";

// signrawtransaction
m_rpcClient->CallRPC("signrawtransactionwithwallet", { "\"" + hexTx + "\"" }, BIND_THIS_MEMFN(OnSignRawContactTx));
Expand All @@ -319,8 +319,8 @@ void AtomicSwapHelper::OnSignRawContactTx(const json& reply)
const auto& result = reply["result"];
auto hexTx = result["hex"].get<std::string>();

LOG_INFO() << "error: " << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << "result: " << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << "error: " << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << "result: " << result.dump(JSON_FORMAT_INDENT) << "\n";

// sendrawtransaction
m_rpcClient->CallRPC("sendrawtransaction", { "\"" + hexTx + "\"" }, BIND_THIS_MEMFN(OnSendContractTx));
Expand All @@ -331,8 +331,8 @@ void AtomicSwapHelper::OnSendContractTx(const json& reply)
// Parse reply
const auto& error = reply["error"];
const auto& result = reply["result"];
LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
}

void AtomicSwapHelper::CreateRefundTx(const std::string& withdrawAddress
Expand All @@ -356,8 +356,8 @@ void AtomicSwapHelper::OnCreateRawRefundTx(const json& reply)
const auto& error = reply["error"];
const auto& result = reply["result"];

LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";

libbitcoin::data_chunk tx_data;
libbitcoin::decode_base16(tx_data, result.get<std::string>());
Expand All @@ -372,8 +372,8 @@ void AtomicSwapHelper::OnDumpSenderPrivateKey(const json& reply)
const auto& error = reply["error"];
const auto& result = reply["result"];

LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";

libbitcoin::wallet::ec_private wallet_key(result.get<std::string>(), libbitcoin::wallet::ec_private::testnet_wif);
libbitcoin::endorsement sig;
Expand Down Expand Up @@ -422,8 +422,8 @@ void AtomicSwapHelper::OnCreateRawRedeemTx(const json& reply)
const auto& error = reply["error"];
const auto& result = reply["result"];

LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";

libbitcoin::data_chunk tx_data;
libbitcoin::decode_base16(tx_data, result.get<std::string>());
Expand All @@ -438,8 +438,8 @@ void AtomicSwapHelper::OnDumpReceiverPrivateKey(const json& reply)
const auto& error = reply["error"];
const auto& result = reply["result"];

LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << error.dump(JSON_FORMAT_INDENT) << "\n";
BEAM_LOG_INFO() << result.dump(JSON_FORMAT_INDENT) << "\n";

libbitcoin::wallet::ec_private wallet_key(result.get<std::string>(), libbitcoin::wallet::ec_private::testnet_wif);
libbitcoin::endorsement sig;
Expand Down Expand Up @@ -543,13 +543,16 @@ void testAtomicSwap(const boost::program_options::variables_map& vm)
reactor->run();
}

int main(int argc, char* argv[]) {
int logLevel = LOG_LEVEL_DEBUG;
int main(int argc, char* argv[]) try {
int logLevel{};

#if LOG_VERBOSE_ENABLED
logLevel = LOG_LEVEL_VERBOSE;
logLevel = BEAM_LOG_LEVEL_VERBOSE;
#else
logLevel = BEAM_LOG_LEVEL_DEBUG;
#endif
auto logger = Logger::create(logLevel, logLevel);

auto logger = Logger::create(logLevel, logLevel);
/*
-create senderAddress receiverAddress secretStr amount locktime
-refund address txID amount locktime outputIndex redeemscript
Expand Down Expand Up @@ -614,4 +617,8 @@ int main(int argc, char* argv[]) {
testAtomicSwap(vm);

return 0;
}
}
catch (const std::exception& e) {
BEAM_LOG_ERROR() << e.what();
return 0;
}
63 changes: 34 additions & 29 deletions 3rdparty/libbitcoin/examples/electrum_get_balance.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Beam Team
// Copyright 2018-2024 The Beam Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace

void on_connected(uint64_t tag, std::unique_ptr<TcpStream>&& newStream, ErrorCode status)
{
LOG_INFO() << "on_connected";
BEAM_LOG_INFO() << "on_connected";
if (newStream) {
assert(status == EC_OK);
newStream->enable_read(on_recv);
Expand All @@ -70,17 +70,17 @@ namespace
}
}

LOG_INFO() << reverseHash;
BEAM_LOG_INFO() << reverseHash;
std::string request = R"({"method":"blockchain.scripthash.get_balance","params":[")" + reverseHash +R"("], "id": "teste"})";
request += "\n";

LOG_INFO() << request;
BEAM_LOG_INFO() << request;
Result res = newStream->write(request.data(), request.size());
if (!res) {
LOG_ERROR() << error_str(res.error());
BEAM_LOG_ERROR() << error_str(res.error());
}

LOG_INFO() << "after write";
BEAM_LOG_INFO() << "after write";
streams.emplace_back(move(newStream));
}
else {
Expand All @@ -90,32 +90,37 @@ namespace
}
}

int main() {
int logLevel = LOG_LEVEL_DEBUG;
int main() try {
int logLevel{};

#if LOG_VERBOSE_ENABLED
logLevel = LOG_LEVEL_VERBOSE;
logLevel = BEAM_LOG_LEVEL_VERBOSE;
#else
logLevel = BEAM_LOG_LEVEL_DEBUG;
#endif

auto logger = Logger::create(logLevel, logLevel);
io::Reactor::Ptr reactor = io::Reactor::create();
io::Reactor::Scope scope(*reactor);
Address address;

try {
io::Reactor::Ptr reactor = io::Reactor::create();
io::Reactor::Scope scope(*reactor);

Address address;
/*address.resolve("testnet.hsmiths.com");
address.port(53012);*/

/*address.resolve("testnet.qtornado.com");
address.port(51002);*/
address.resolve("testnet1.bauerj.eu");
address.port(50002);

reactor->tcp_connect(address, tag_ok, on_connected, 2000, io::TlsConfig(true));
reactor->run();
}
catch (const std::exception& e) {
LOG_ERROR() << e.what();
}
#if 0
address.resolve("testnet.hsmiths.com");
address.port(53012);

address.resolve("testnet.qtornado.com");
address.port(51002);
#endif

address.resolve("testnet1.bauerj.eu");
address.port(50002);

reactor->tcp_connect(address, tag_ok, on_connected, 2000, io::TlsConfig(true));
reactor->run();

return 0;
}
}
catch (const std::exception& e) {
BEAM_LOG_ERROR() << e.what();
return 0;
}
Loading

0 comments on commit 902be6b

Please sign in to comment.