Skip to content

Commit

Permalink
itcoin-fbft: apply clang-format rules to the code base
Browse files Browse the repository at this point in the history
  • Loading branch information
muxator committed Sep 29, 2023
1 parent 7f371a1 commit 03d0c1c
Show file tree
Hide file tree
Showing 87 changed files with 4,335 additions and 5,354 deletions.
137 changes: 45 additions & 92 deletions src/blockchain/BitcoinBlockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,140 +6,93 @@
#include <boost/format.hpp>
#include <boost/log/trivial.hpp>

#include "../transport/btcclient.h"
#include "config/FbftConfig.h"
#include "generate.h"
#include "../transport/btcclient.h"

using namespace std;
using namespace itcoin::blockchain;

namespace itcoin {
namespace blockchain {

BitcoinBlockchain::BitcoinBlockchain(const itcoin::FbftConfig& conf, transport::BtcClient& bitcoind):
Blockchain(conf), m_bitcoind(bitcoind)
{
BitcoinBlockchain::BitcoinBlockchain(const itcoin::FbftConfig& conf, transport::BtcClient& bitcoind)
: Blockchain(conf), m_bitcoind(bitcoind) {
m_reward_address = m_conf.replica_set_v().at(m_conf.id()).p2pkh();
BOOST_LOG_TRIVIAL(debug) << str(
boost::format("R%1% BitcoinBlockchain, using reward address %2%.")
% m_conf.id()
% m_reward_address
);
BOOST_LOG_TRIVIAL(debug) << str(boost::format("R%1% BitcoinBlockchain, using reward address %2%.") %
m_conf.id() % m_reward_address);
}

CBlock BitcoinBlockchain::GenerateBlock(uint32_t block_timestamp)
{
CBlock BitcoinBlockchain::GenerateBlock(uint32_t block_timestamp) {
return generateBlock(m_bitcoind, m_reward_address, block_timestamp);
}

bool BitcoinBlockchain::TestBlockValidity(const uint32_t height, const CBlock& block, bool check_signet_solution)
{
bool BitcoinBlockchain::TestBlockValidity(const uint32_t height, const CBlock& block,
bool check_signet_solution) {
auto block_ser = HexSerializableCBlock(block);
const uint32_t block_size_bytes = block_ser.GetHex().length() / 2;
const std::string block_hash = block.GetBlockHeader().GetHash().ToString();

BOOST_LOG_TRIVIAL(debug) << str(
boost::format("R%1% BitcoinBlockchain::TestBlockValidity invoking for "
"candidate block at height %2%, blocksize %3% bytes, block hash: %4%")
% m_conf.id()
% height
% block_size_bytes
% block_hash
);
boost::format("R%1% BitcoinBlockchain::TestBlockValidity invoking for "
"candidate block at height %2%, blocksize %3% bytes, block hash: %4%") %
m_conf.id() % height % block_size_bytes % block_hash);

Json::Value result;
try
{
try {
result = m_bitcoind.testblockvalidity(block_ser.GetHex(), check_signet_solution);
}
catch (jsonrpc::JsonRpcException& e) {
BOOST_LOG_TRIVIAL(warning) << str(
boost::format("R%1% BitcoinBlockchain::TestBlockValidity for candidate "
"block at height %2% with hash %3% raised %4%.")
% m_conf.id()
% height
% block_hash
% e.what()
);
} catch (jsonrpc::JsonRpcException& e) {
BOOST_LOG_TRIVIAL(warning) << str(boost::format("R%1% BitcoinBlockchain::TestBlockValidity for candidate "
"block at height %2% with hash %3% raised %4%.") %
m_conf.id() % height % block_hash % e.what());
return false;
}

BOOST_LOG_TRIVIAL(debug) << str(
boost::format("R%1% BitcoinBlockchain::TestBlockValidity for candidate "
"block at height %2% with hash %3%. Result = %4% (null means ok).")
% m_conf.id()
% height
% block_hash
% result
);
boost::format("R%1% BitcoinBlockchain::TestBlockValidity for candidate "
"block at height %2% with hash %3%. Result = %4% (null means ok).") %
m_conf.id() % height % block_hash % result);
return true;
}

void BitcoinBlockchain::SubmitBlock(const uint32_t height, const CBlock& block)
{
void BitcoinBlockchain::SubmitBlock(const uint32_t height, const CBlock& block) {
const auto block_ser = HexSerializableCBlock(block);
const std::string block_hash = block.GetBlockHeader().GetHash().ToString();
try {
const uint32_t block_size_bytes = block_ser.GetHex().length() / 2;
BOOST_LOG_TRIVIAL(debug) << str(
boost::format(
"R%1% BitcoinBlockchain::SubmitBlock submitting block at height %2% "
"block size: %3% bytes, block hash: %4%"
)
% m_conf.id()
% height
% block_size_bytes
% block_hash
);
boost::format("R%1% BitcoinBlockchain::SubmitBlock submitting block at height %2% "
"block size: %3% bytes, block hash: %4%") %
m_conf.id() % height % block_size_bytes % block_hash);

auto result = m_bitcoind.submitblock(block_ser.GetHex());
BOOST_LOG_TRIVIAL(debug) << str(
boost::format("R%1% BitcoinBlockchain::SubmitBlock for block at height "
"%2%, block hash: %3%. Result = %4% (null means ok).")
% m_conf.id()
% height
% block_hash
% result
);
}
catch (const jsonrpc::JsonRpcException& e)
{
if (e.GetMessage() == "The response is invalid: \"duplicate\"\n")
{
BOOST_LOG_TRIVIAL(debug) << str(boost::format("R%1% BitcoinBlockchain::SubmitBlock for block at height "
"%2%, block hash: %3%. Result = %4% (null means ok).") %
m_conf.id() % height % block_hash % result);
} catch (const jsonrpc::JsonRpcException& e) {
if (e.GetMessage() == "The response is invalid: \"duplicate\"\n") {
BOOST_LOG_TRIVIAL(warning) << str(
boost::format("R%1% BitcoinBlockchain::SubmitBlock the submitblock "
"invocation for block height %2% (hash %3%) failed because the block "
"was already in the blockchain. Most probably another replica already "
"submitted the same block and was propagated to the local node before "
"the submitblock call was attempted.")
% m_conf.id()
% height
% block_hash
);
}
else if (e.GetMessage() == "The response is invalid: \"inconclusive\"\n") {
boost::format("R%1% BitcoinBlockchain::SubmitBlock the submitblock "
"invocation for block height %2% (hash %3%) failed because the block "
"was already in the blockchain. Most probably another replica already "
"submitted the same block and was propagated to the local node before "
"the submitblock call was attempted.") %
m_conf.id() % height % block_hash);
} else if (e.GetMessage() == "The response is invalid: \"inconclusive\"\n") {
BOOST_LOG_TRIVIAL(warning) << str(
boost::format("R%1% BitcoinBlockchain::SubmitBlock the submitblock "
"invocation for height %2% (hash %3%) returned 'inconclusive'. This "
"problem is temporarily ignored.")
% m_conf.id()
% height
% block_hash
);
}
else {
boost::format("R%1% BitcoinBlockchain::SubmitBlock the submitblock "
"invocation for height %2% (hash %3%) returned 'inconclusive'. This "
"problem is temporarily ignored.") %
m_conf.id() % height % block_hash);
} else {
BOOST_LOG_TRIVIAL(error) << str(
boost::format("R%1% BitcoinBlockchain::SubmitBlock got exception "
"while trying to submit block at height %2% (hash %3%): %4%")
% m_conf.id()
% height
% block_hash
% e.what()
);
boost::format("R%1% BitcoinBlockchain::SubmitBlock got exception "
"while trying to submit block at height %2% (hash %3%): %4%") %
m_conf.id() % height % block_hash % e.what());
throw e;
}
}
}

}
}
} // namespace blockchain
} // namespace itcoin
9 changes: 3 additions & 6 deletions src/blockchain/Blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
namespace itcoin {
namespace blockchain {

Blockchain::Blockchain(const itcoin::FbftConfig& conf):
m_conf(conf)
{
}
Blockchain::Blockchain(const itcoin::FbftConfig& conf) : m_conf(conf) {}

}
}
} // namespace blockchain
} // namespace itcoin
30 changes: 8 additions & 22 deletions src/blockchain/HexSerializableCBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,22 @@ using namespace std;
namespace itcoin {
namespace blockchain {

HexSerializableCBlock::HexSerializableCBlock()
{
HexSerializableCBlock::HexSerializableCBlock() {}

}

HexSerializableCBlock::HexSerializableCBlock(CBlock block):
CBlock(block)
{

}
HexSerializableCBlock::HexSerializableCBlock(CBlock block) : CBlock(block) {}

HexSerializableCBlock::HexSerializableCBlock(std::string block_hex)
{
HexSerializableCBlock::HexSerializableCBlock(std::string block_hex) {
std::string block_str = utils::hexToString(block_hex);
Span<uint8_t> block_span {
reinterpret_cast<uint8_t*>(&block_str[0]),
block_str.size()
};
CDataStream dataStream {
block_span, SER_NETWORK, PROTOCOL_VERSION
};
Span<uint8_t> block_span{reinterpret_cast<uint8_t*>(&block_str[0]), block_str.size()};
CDataStream dataStream{block_span, SER_NETWORK, PROTOCOL_VERSION};
this->Unserialize(dataStream);
}

std::string HexSerializableCBlock::GetHex() const
{
std::string HexSerializableCBlock::GetHex() const {
CDataStream dataStream{SER_NETWORK, PROTOCOL_VERSION};
this->Serialize(dataStream);
return utils::stringToHex(dataStream.str());
}

}
}
} // namespace blockchain
} // namespace itcoin
63 changes: 31 additions & 32 deletions src/blockchain/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,52 @@
#include <psbt.h>

namespace itcoin {
class FbftConfig;
class FbftConfig;
}

namespace itcoin { namespace transport {
class BtcClient;
}} // namespace itcoin::transport
namespace itcoin {
namespace transport {
class BtcClient;
}
} // namespace itcoin

namespace itcoin {
namespace blockchain {

class HexSerializableCBlock: public CBlock
{
public:
HexSerializableCBlock();
HexSerializableCBlock(CBlock block);
HexSerializableCBlock(std::string block_hex);
std::string GetHex() const;
class HexSerializableCBlock : public CBlock {
public:
HexSerializableCBlock();
HexSerializableCBlock(CBlock block);
HexSerializableCBlock(std::string block_hex);
std::string GetHex() const;
};

class Blockchain
{
public:
Blockchain(const itcoin::FbftConfig& conf);
class Blockchain {
public:
Blockchain(const itcoin::FbftConfig& conf);

virtual CBlock GenerateBlock(uint32_t block_timestamp) = 0;
virtual bool TestBlockValidity(const uint32_t height, const CBlock&, bool check_signet_solution) = 0;
virtual void SubmitBlock(const uint32_t height, const CBlock&) = 0;
virtual CBlock GenerateBlock(uint32_t block_timestamp) = 0;
virtual bool TestBlockValidity(const uint32_t height, const CBlock&, bool check_signet_solution) = 0;
virtual void SubmitBlock(const uint32_t height, const CBlock&) = 0;

protected:
const itcoin::FbftConfig& m_conf;
protected:
const itcoin::FbftConfig& m_conf;
};

class BitcoinBlockchain: public Blockchain
{
public:
BitcoinBlockchain(const itcoin::FbftConfig& conf, transport::BtcClient& bitcoind);
class BitcoinBlockchain : public Blockchain {
public:
BitcoinBlockchain(const itcoin::FbftConfig& conf, transport::BtcClient& bitcoind);

CBlock GenerateBlock(uint32_t block_timestamp);
bool TestBlockValidity(const uint32_t height, const CBlock& block, bool check_signet_solution);
void SubmitBlock(const uint32_t height, const CBlock& block);
CBlock GenerateBlock(uint32_t block_timestamp);
bool TestBlockValidity(const uint32_t height, const CBlock& block, bool check_signet_solution);
void SubmitBlock(const uint32_t height, const CBlock& block);

protected:
transport::BtcClient& m_bitcoind;
std::string m_reward_address;
protected:
transport::BtcClient& m_bitcoind;
std::string m_reward_address;
};

}
}
} // namespace blockchain
} // namespace itcoin

#endif // ITCOIN_BLOCKCHAIN_BLOCKCHAIN_H
16 changes: 7 additions & 9 deletions src/blockchain/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
#include <util/strencodings.h>
#include <version.h>

namespace itcoin {
namespace blockchain {

namespace itcoin { namespace blockchain {

void appendSignetSolution(CBlock *block, std::vector<unsigned char> signetSolution)
{
void appendSignetSolution(CBlock* block, std::vector<unsigned char> signetSolution) {
/*
* Append the signet solution
*
Expand Down Expand Up @@ -51,9 +50,8 @@ void appendSignetSolution(CBlock *block, std::vector<unsigned char> signetSoluti
block->vtx[0] = MakeTransactionRef(tx);
}


std::pair<CMutableTransaction, CMutableTransaction> signetTxs(const CBlock& block, const std::string& signetChallengeHex)
{
std::pair<CMutableTransaction, CMutableTransaction> signetTxs(const CBlock& block,
const std::string& signetChallengeHex) {
// assumes signet solution has not been added yet so does not need to be removed
// ITCOIN_SPECIFIC START
// assumes SIGNET_HEADER has already been added so does not need to be added here
Expand Down Expand Up @@ -112,5 +110,5 @@ std::pair<CMutableTransaction, CMutableTransaction> signetTxs(const CBlock& bloc
return std::make_pair(spend, to_spend);
} // signetTxs()


}} // namespace itcoin::blockchain
} // namespace blockchain
} // namespace itcoin
19 changes: 12 additions & 7 deletions src/blockchain/extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

#include <primitives/block.h>

namespace itcoin { namespace transport {
class BtcClient;
}} // namespace itcoin::transport
namespace itcoin {
namespace transport {
class BtcClient;
}
} // namespace itcoin

namespace itcoin { namespace blockchain {
namespace itcoin {
namespace blockchain {

void appendSignetSolution(CBlock *block, std::vector<unsigned char> signetSolution);
std::pair<CMutableTransaction, CMutableTransaction> signetTxs(const CBlock& block, const std::string& signetChallengeHex);
void appendSignetSolution(CBlock* block, std::vector<unsigned char> signetSolution);
std::pair<CMutableTransaction, CMutableTransaction> signetTxs(const CBlock& block,
const std::string& signetChallengeHex);

}} // namespace itcoin::blockchain
} // namespace blockchain
} // namespace itcoin

#endif // ITCOIN_BLOCKCHAIN_EXTRACT_H
Loading

0 comments on commit 03d0c1c

Please sign in to comment.