Skip to content

Commit

Permalink
Merge branch 'release-3.6.0' into release-3.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Dec 28, 2023
2 parents a0178c8 + 2f9b2d0 commit e95384c
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 268 deletions.
79 changes: 31 additions & 48 deletions bcos-framework/bcos-framework/transaction-executor/StateKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ class StateKeyView;

class StateKey
{
private:
friend class StateKeyView;
public:
std::string m_tableAndKey;
size_t m_split{};

public:
StateKey() = default;
StateKey(std::string_view table, std::string_view key)
{
Expand Down Expand Up @@ -63,8 +61,8 @@ class StateKey
stream << stateKey.m_tableAndKey;
return stream;
}
const char* data() const& { return m_tableAndKey.data(); }
size_t size() const { return m_tableAndKey.size(); }
const char* data() const& noexcept { return m_tableAndKey.data(); }
size_t size() const noexcept { return m_tableAndKey.size(); }
};

class StateKeyView
Expand All @@ -74,59 +72,46 @@ class StateKeyView
std::string_view m_key;
friend class StateKey;

constexpr friend std::optional<std::string_view> continuousView(StateKeyView const& view)
{
if ((view.m_table.data() + view.m_table.size() + 1) == view.m_key.data())
{
return std::string_view(
view.m_table.data(), view.m_table.size() + 1 + view.m_key.size());
}
return {};
}

public:
explicit StateKeyView(const StateKey& stateKey)
{
std::string_view view(stateKey.m_tableAndKey);
m_table = view.substr(0, stateKey.m_split);
m_key = view.substr(stateKey.m_split + 1);
}
StateKeyView(std::string_view table, std::string_view key) : m_table(table), m_key(key) {}
explicit StateKeyView(const StateKey& stateKey) noexcept
: m_table(stateKey.data(), stateKey.m_split),
m_key(stateKey.data() + stateKey.m_split + 1, stateKey.size() - stateKey.m_split - 1)
{}
StateKeyView(std::string_view table, std::string_view key) noexcept : m_table(table), m_key(key)
{}

friend std::strong_ordering operator<=>(
const StateKeyView& lhs, const StateKeyView& rhs) noexcept
{
auto lhsContinuousView = continuousView(lhs);
auto rhsContinuousView = continuousView(rhs);
if (lhsContinuousView && rhsContinuousView)
{
return *lhsContinuousView <=> *rhsContinuousView;
}

auto cmp = lhs.m_table <=> rhs.m_table;
if (std::is_eq(cmp))
{
cmp = lhs.m_key <=> rhs.m_key;
}
return cmp;
}
friend bool operator==(const StateKeyView& lhs, const StateKeyView& rhs) = default;
friend std::strong_ordering operator<=>(const StateKeyView& lhs, const StateKey& rhs) noexcept
{
StateKeyView rhsView(rhs);
return lhs <=> rhsView;
}
friend bool operator==(const StateKeyView& lhs, const StateKeyView& rhs) noexcept = default;
friend ::std::ostream& operator<<(::std::ostream& stream, const StateKeyView& stateKeyView)
{
stream << stateKeyView.m_table << ":" << stateKeyView.m_key;
return stream;
}

size_t hash() const
size_t hash() const noexcept
{
auto result = std::hash<std::string_view>{}(m_table);
boost::hash_combine(result, std::hash<std::string_view>{}(m_key));
return result;
}

std::tuple<std::string_view, std::string_view> getTableAndKey() const
std::tuple<std::string_view, std::string_view> getTableAndKey() const noexcept
{
return std::make_tuple(m_table, m_key);
return {m_table, m_key};
}
};

Expand All @@ -138,19 +123,17 @@ template <>
struct std::less<bcos::transaction_executor::StateKey>
{
auto operator()(bcos::transaction_executor::StateKey const& left,
bcos::transaction_executor::StateKeyView const& rightView) const -> bool
bcos::transaction_executor::StateKeyView const& rightView) const noexcept -> bool
{
auto leftView = bcos::transaction_executor::StateKeyView(left);
return leftView < rightView;
return left < rightView;
}
auto operator()(bcos::transaction_executor::StateKeyView const& leftView,
bcos::transaction_executor::StateKey const& right) const -> bool
bcos::transaction_executor::StateKey const& right) const noexcept -> bool
{
auto rightView = bcos::transaction_executor::StateKeyView(right);
return leftView < rightView;
return leftView < right;
}
auto operator()(bcos::transaction_executor::StateKey const& lhs,
bcos::transaction_executor::StateKey const& rhs) const -> bool
bcos::transaction_executor::StateKey const& rhs) const noexcept -> bool
{
return lhs < rhs;
}
Expand All @@ -159,7 +142,7 @@ struct std::less<bcos::transaction_executor::StateKey>
template <>
struct std::hash<bcos::transaction_executor::StateKeyView>
{
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const noexcept
{
return stateKeyView.hash();
}
Expand All @@ -168,7 +151,7 @@ struct std::hash<bcos::transaction_executor::StateKeyView>
template <>
struct boost::hash<bcos::transaction_executor::StateKeyView>
{
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const noexcept
{
return stateKeyView.hash();
}
Expand All @@ -177,12 +160,12 @@ struct boost::hash<bcos::transaction_executor::StateKeyView>
template <>
struct std::hash<bcos::transaction_executor::StateKey>
{
size_t operator()(const bcos::transaction_executor::StateKey& stateKey) const
size_t operator()(const bcos::transaction_executor::StateKey& stateKey) const noexcept
{
auto view = bcos::transaction_executor::StateKeyView(stateKey);
return std::hash<bcos::transaction_executor::StateKeyView>{}(view);
}
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const noexcept
{
return stateKeyView.hash();
}
Expand All @@ -191,11 +174,11 @@ struct std::hash<bcos::transaction_executor::StateKey>
template <>
struct boost::hash<bcos::transaction_executor::StateKey>
{
size_t operator()(const bcos::transaction_executor::StateKey& stateKey) const
size_t operator()(const bcos::transaction_executor::StateKey& stateKey) const noexcept
{
return std::hash<bcos::transaction_executor::StateKey>{}(stateKey);
}
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const
size_t operator()(const bcos::transaction_executor::StateKeyView& stateKeyView) const noexcept
{
return stateKeyView.hash();
}
Expand All @@ -205,12 +188,12 @@ template <>
struct std::equal_to<bcos::transaction_executor::StateKey>
{
bool operator()(bcos::transaction_executor::StateKey const& lhs,
bcos::transaction_executor::StateKey const& rhs) const
bcos::transaction_executor::StateKey const& rhs) const noexcept
{
return std::is_eq(lhs <=> rhs);
}
bool operator()(bcos::transaction_executor::StateKeyView const& lhsView,
bcos::transaction_executor::StateKey const& rhs) const
bcos::transaction_executor::StateKey const& rhs) const noexcept
{
auto rhsView = bcos::transaction_executor::StateKeyView(rhs);
return std::is_eq(lhsView <=> rhsView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace bcos::transaction_executor
{

struct ExecuteTransaction
inline constexpr struct ExecuteTransaction
{
/**
* @brief Executes a transaction and returns a task that resolves to a transaction receipt.
Expand All @@ -38,8 +38,37 @@ struct ExecuteTransaction
co_return co_await tag_invoke(*this, executor, storage, blockHeader, transaction,
std::forward<decltype(args)>(args)...);
}
};
inline constexpr ExecuteTransaction executeTransaction{};
} executeTransaction{};

inline constexpr struct Execute3Step
{
/**
* @brief Executes a transaction in three steps.
*
* This function is a friend function of the TransactionExecutorImpl class.
* It executes a transaction in three steps and returns a generator that yields
* transaction receipts.
*
* 分三个步骤执行交易,可流水线执行
* Transaction are executed in three steps, which can be pipelined
*
* @param executor The reference to the TransactionExecutorImpl object.
* @param storage The reference to the storage object.
* @param blockHeader The reference to the block header object.
* @param transaction The reference to the transaction object.
* @param contextID The context ID.
* @param ledgerConfig The reference to the ledger configuration object.
* @param waitOperator The wait operator.
*
* @return A generator that yields transaction receipts.
*/
auto operator()(auto& executor, auto& storage, protocol::BlockHeader const& blockHeader,
protocol::Transaction const& transaction, auto&&... args) const
{
return tag_invoke(*this, executor, storage, blockHeader, transaction,
std::forward<decltype(args)>(args)...);
}
} execute3Step{};

template <auto& Tag>
using tag_t = std::decay_t<decltype(Tag)>;
Expand Down
12 changes: 3 additions & 9 deletions bcos-gateway/bcos-gateway/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ void Gateway::asyncSendMessageByNodeID(const std::string& _groupID, int _moduleI
return;
}

auto retry = std::make_shared<Retry>();
auto message =
std::static_pointer_cast<P2PMessage>(m_p2pInterface->messageFactory()->buildMessage());

Expand All @@ -206,14 +205,9 @@ void Gateway::asyncSendMessageByNodeID(const std::string& _groupID, int _moduleI
options->setSrcNodeID(_srcNodeID->encode());
options->dstNodeIDs().push_back(_dstNodeID->encode());

retry->m_p2pMessage = message;
retry->m_p2pIDs.insert(retry->m_p2pIDs.begin(), p2pIDs.begin(), p2pIDs.end());
retry->m_respFunc = _errorRespFunc;
retry->m_srcNodeID = _srcNodeID;
retry->m_dstNodeID = _dstNodeID;
retry->m_p2pInterface = m_p2pInterface;
retry->m_moduleID = _moduleID;

auto retry = std::make_shared<Retry>(std::move(_srcNodeID), std::move(_dstNodeID),
std::move(message), m_p2pInterface, std::move(_errorRespFunc), _moduleID);
retry->insertP2pIDs(p2pIDs);
retry->trySendMessage();
}

Expand Down
24 changes: 22 additions & 2 deletions bcos-gateway/bcos-gateway/Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#pragma once

#include "bcos-gateway/libratelimit/GatewayRateLimiter.h"
#include "filter/ReadOnlyFilter.h"
#include "bcos-utilities/ObjectAllocatorMonitor.h"
#include "filter/ReadOnlyFilter.h"
#include <bcos-framework/front/FrontServiceInterface.h>
#include <bcos-framework/gateway/GatewayInterface.h>
#include <bcos-framework/protocol/CommonError.h>
Expand All @@ -42,10 +42,21 @@ namespace gateway
class Retry : public std::enable_shared_from_this<Retry>, public ObjectCounter<Retry>
{
public:
Retry(crypto::NodeIDPtr _srcNodeID, crypto::NodeIDPtr _dstNodeID,
std::shared_ptr<P2PMessage> _p2pMessage, std::shared_ptr<P2PInterface> _p2pInterface,
ErrorRespFunc _respFunc, int _moduleID)
: m_srcNodeID(std::move(_srcNodeID)),
m_dstNodeID(std::move(_dstNodeID)),
m_p2pMessage(std::move(_p2pMessage)),
m_p2pInterface(std::move(_p2pInterface)),
m_respFunc(std::move(_respFunc)),
m_moduleID(_moduleID)
{}
// random choose one p2pID to send message
P2pID chooseP2pID()
{
auto p2pId = P2pID();
std::lock_guard<std::mutex> lock(x_mutex);
if (!m_p2pIDs.empty())
{
p2pId = *m_p2pIDs.begin();
Expand Down Expand Up @@ -168,7 +179,16 @@ class Retry : public std::enable_shared_from_this<Retry>, public ObjectCounter<R
m_p2pInterface->asyncSendMessageByNodeID(p2pID, m_p2pMessage, callback, Options(10000));
}

public:
// insert p2pIDs
void insertP2pIDs(RANGES::range auto const& _p2pIDs)
{
std::lock_guard<std::mutex> lock(x_mutex);
m_p2pIDs.insert(m_p2pIDs.end(), _p2pIDs.begin(), _p2pIDs.end());
}

private:
// mutex for p2pIDs
mutable std::mutex x_mutex;
std::vector<P2pID> m_p2pIDs;
crypto::NodeIDPtr m_srcNodeID;
crypto::NodeIDPtr m_dstNodeID;
Expand Down
15 changes: 13 additions & 2 deletions bcos-rpc/bcos-rpc/jsonrpc/JsonRpcImpl_2_0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ void bcos::rpc::toJsonResp(Json::Value& jResp, bcos::protocol::Transaction const
jResp["abi"] = std::string(transaction.abi());
jResp["signature"] = toHexStringWithPrefix(transaction.signatureData());
jResp["extraData"] = std::string(transaction.extraData());
if (transaction.version() == int32_t(bcos::protocol::TransactionVersion::V1_VERSION))
{
jResp["value"] = std::string(transaction.value());
jResp["gasPrice"] = std::string(transaction.gasPrice());
jResp["gasLimit"] = transaction.gasLimit();
jResp["maxFeePerGas"] = std::string(transaction.maxFeePerGas());
jResp["maxPriorityFeePerGas"] = std::string(transaction.maxPriorityFeePerGas());
}
}

void bcos::rpc::toJsonResp(Json::Value& jResp, std::string_view _txHash,
Expand Down Expand Up @@ -276,7 +284,6 @@ void bcos::rpc::toJsonResp(Json::Value& jResp, std::string_view _txHash,
{
jResp["hash"] = "0x";
}
jResp["effectiveGasPrice"] = std::string(transactionReceipt.effectiveGasPrice());

jResp["logEntries"] = Json::Value(Json::arrayValue);
for (const auto& logEntry : transactionReceipt.logEntries())
Expand All @@ -291,6 +298,10 @@ void bcos::rpc::toJsonResp(Json::Value& jResp, std::string_view _txHash,
jLog["data"] = toHexStringWithPrefix(logEntry.data());
jResp["logEntries"].append(jLog);
}
if (transactionReceipt.version() == int32_t(bcos::protocol::TransactionVersion::V1_VERSION))
{
jResp["effectiveGasPrice"] = std::string(transactionReceipt.effectiveGasPrice());
}
}


Expand Down Expand Up @@ -521,7 +532,7 @@ void JsonRpcImpl_2_0::sendTransaction(std::string_view groupID, std::string_view
catch (std::exception& e)
{
auto info = boost::diagnostic_information(e);
RPC_IMPL_LOG(WARNING) << "RPC common error: " << info;
RPC_IMPL_LOG(WARNING) << "RPC common exception: " << info;
respFunc(BCOS_ERROR_PTR(-1, std::move(info)), jResp);
}
}(this, groupID, nodeName, data, requireProof, std::move(respFunc)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class TransactionFactoryImpl : public bcos::protocol::TransactionFactory
!bcos::isHexStringV2(transaction->mutableInner().data.maxFeePerGas) ||
!bcos::isHexStringV2(transaction->mutableInner().data.maxPriorityFeePerGas))
{
BCOS_LOG(WARNING) << LOG_DESC(
"the transaction value or gasPrice or maxFeePerGas or "
"maxPriorityFeePerGas is not hex string")
<< LOG_KV("value", transaction->mutableInner().data.value)
<< LOG_KV("gasPrice", transaction->mutableInner().data.gasPrice)
<< LOG_KV("maxFeePerGas",
transaction->mutableInner().data.maxFeePerGas)
<< LOG_KV("maxPriorityFeePerGas",
transaction->mutableInner().data.maxPriorityFeePerGas);
BCOS_LOG(INFO) << LOG_DESC(
"the transaction value or gasPrice or maxFeePerGas or "
"maxPriorityFeePerGas is not hex string")
<< LOG_KV("value", transaction->mutableInner().data.value)
<< LOG_KV("gasPrice", transaction->mutableInner().data.gasPrice)
<< LOG_KV(
"maxFeePerGas", transaction->mutableInner().data.maxFeePerGas)
<< LOG_KV("maxPriorityFeePerGas",
transaction->mutableInner().data.maxPriorityFeePerGas);
BOOST_THROW_EXCEPTION(
std::invalid_argument("transaction value or gasPrice or maxFeePerGas or "
"maxPriorityFeePerGas is not hex string"));
Expand Down
8 changes: 2 additions & 6 deletions bcos-utilities/bcos-utilities/DataConvertUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@ bool bcos::isHexStringV2(string const& _string)
{
return true;
}
if (_string.length() % 2 == 0)
{
std::regex pattern("0x[0-9a-fA-F]*");
return std::regex_match(_string, pattern);
}
return false;
std::regex pattern("0x[0-9a-fA-F]*");
return std::regex_match(_string, pattern);
}

std::shared_ptr<bytes> bcos::fromHexString(std::string const& _hexedString)
Expand Down
Loading

0 comments on commit e95384c

Please sign in to comment.