Skip to content

Commit

Permalink
Merge branch 'release-3.12.0' into feat-web3-txpool
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Aug 30, 2024
2 parents 672e0c5 + 3692e9f commit 855ff63
Show file tree
Hide file tree
Showing 54 changed files with 426 additions and 338 deletions.
2 changes: 1 addition & 1 deletion bcos-executor/src/executive/ExecutiveFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ExecutiveFactory
m_staticPrecompiled(std::move(staticPrecompiled)),
m_blockContext(blockContext),
m_gasInjector(gasInjector),
m_isTiKVStorage(boost::iequals("tikv", g_BCOSConfig.storageType()))
m_isTiKVStorage(boost::iequals("tikv", protocol::g_BCOSConfig.storageType()))
{
if (m_isTiKVStorage)
{
Expand Down
181 changes: 90 additions & 91 deletions bcos-executor/src/precompiled/ConsensusPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ using namespace bcos::precompiled;
using namespace bcos::ledger;

constexpr const char* const CSS_METHOD_ADD_SEALER = "addSealer(string,uint256)";
constexpr const char* const CSS_METHOD_ADD_SEALER2 = "addSealer(string,uint256,uint256)";
constexpr const char* const CSS_METHOD_ADD_SER = "addObserver(string)";
constexpr const char* const CSS_METHOD_REMOVE = "remove(string)";
constexpr const char* const CSS_METHOD_SET_WEIGHT = "setWeight(string,uint256)";
constexpr const char* const CSS_METHOD_SET_TERM_WEIGHT = "setWeight(string,uint256)";
const auto NODE_LENGTH = 128U;

ConsensusPrecompiled::ConsensusPrecompiled(const crypto::Hash::Ptr& _hashImpl)
: Precompiled(_hashImpl)
ConsensusPrecompiled::ConsensusPrecompiled(crypto::Hash::Ptr _hashImpl) : Precompiled(_hashImpl)
{
name2Selector[CSS_METHOD_ADD_SEALER] = getFuncSelector(CSS_METHOD_ADD_SEALER, _hashImpl);
name2Selector[CSS_METHOD_ADD_SEALER2] = getFuncSelector(CSS_METHOD_ADD_SEALER2, _hashImpl);
name2Selector[CSS_METHOD_ADD_SER] = getFuncSelector(CSS_METHOD_ADD_SER, _hashImpl);
name2Selector[CSS_METHOD_REMOVE] = getFuncSelector(CSS_METHOD_REMOVE, _hashImpl);
name2Selector[CSS_METHOD_SET_WEIGHT] = getFuncSelector(CSS_METHOD_SET_WEIGHT, _hashImpl);
Expand Down Expand Up @@ -83,6 +85,11 @@ std::shared_ptr<PrecompiledExecResult> ConsensusPrecompiled::call(
// addSealer(string, uint256)
result = addSealer(_executive, data, codec);
}
else if (func == name2Selector[CSS_METHOD_ADD_SEALER2])
{
// addSealer(string, uint256, uint256)
result = addSealer2(_executive, data, codec);
}
else if (func == name2Selector[CSS_METHOD_ADD_SER])
{
// addObserver(string)
Expand Down Expand Up @@ -115,16 +122,11 @@ std::shared_ptr<PrecompiledExecResult> ConsensusPrecompiled::call(
return _callParameters;
}

int ConsensusPrecompiled::addSealer(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec)
static int innerAddSealer(bool isConsensus,
const std::shared_ptr<executor::TransactionExecutive>& _executive, std::string nodeID,
u256 voteWeight, uint64_t termWeight)
{
// addSealer(string, uint256)
std::string nodeID;
u256 weight;
const auto& blockContext = _executive->blockContext();
codec.decode(_data, nodeID, weight);
// Uniform lowercase nodeID
boost::to_lower(nodeID);

PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number())
Expand All @@ -138,14 +140,6 @@ int ConsensusPrecompiled::addSealer(
<< LOG_DESC("nodeID length mistake") << LOG_KV("nodeID", nodeID);
return CODE_INVALID_NODE_ID;
}
if (weight == 0)
{
// u256 weight be then 0
PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled") << LOG_DESC("weight is 0")
<< LOG_KV("nodeID", nodeID);
return CODE_INVALID_WEIGHT;
}

auto& storage = _executive->storage();

ConsensusNodeList consensusList;
Expand All @@ -161,106 +155,111 @@ int ConsensusPrecompiled::addSealer(

auto node = std::find_if(consensusList.begin(), consensusList.end(),
[&nodeID](const ConsensusNode& node) { return node.nodeID == nodeID; });
if (node != consensusList.end())
if (isConsensus)
{
// exist
node->weight = weight;
if (blockContext.features().get(Features::Flag::feature_rpbft))
if (voteWeight == 0)
{
node->type = ledger::CONSENSUS_CANDIDATE_SEALER;
// u256 weight be then 0
PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled") << LOG_DESC("weight is 0")
<< LOG_KV("nodeID", nodeID);
return CODE_INVALID_WEIGHT;
}
if (node != consensusList.end())
{
// exist
node->voteWeight = voteWeight;
if (blockContext.features().get(Features::Flag::feature_rpbft))
{
node->type = ledger::CONSENSUS_CANDIDATE_SEALER;
}
else
{
node->type = ledger::CONSENSUS_SEALER;
}
}
else
{
node->type = ledger::CONSENSUS_SEALER;
// no exist
if (blockContext.blockVersion() >= (uint32_t)protocol::BlockVersion::V3_1_VERSION)
{
// version >= 3.1.0, only allow adding sealer in observer list
return CODE_ADD_SEALER_SHOULD_IN_OBSERVER;
}
consensusList.emplace_back(nodeID, voteWeight, std::string{ledger::CONSENSUS_SEALER},
boost::lexical_cast<std::string>(blockContext.number() + 1), termWeight);
}
node->enableNumber = boost::lexical_cast<std::string>(blockContext.number() + 1);
}
else
{
// no exist
if (blockContext.blockVersion() >= (uint32_t)protocol::BlockVersion::V3_1_VERSION)
if (node != consensusList.end())
{
// version >= 3.1.0, only allow adding sealer in observer list
return CODE_ADD_SEALER_SHOULD_IN_OBSERVER;
// find it in consensus list
auto sealerCount = std::count_if(consensusList.begin(), consensusList.end(),
[](auto&& node) { return node.type == ledger::CONSENSUS_SEALER; });
if (sealerCount == 1)
{
PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled")
<< LOG_DESC("addObserver failed, because last sealer");
return CODE_LAST_SEALER;
}
node->voteWeight = 0;
node->type = ledger::CONSENSUS_OBSERVER;
node->enableNumber = boost::lexical_cast<std::string>(blockContext.number() + 1);
}
else
{
consensusList.emplace_back(nodeID, 0, std::string{ledger::CONSENSUS_OBSERVER},
boost::lexical_cast<std::string>(blockContext.number() + 1), 0);
}
consensusList.emplace_back(nodeID, weight, std::string{ledger::CONSENSUS_SEALER},
boost::lexical_cast<std::string>(blockContext.number() + 1));
}

entry->setObject(consensusList);

storage.setRow(SYS_CONSENSUS, "key", std::move(*entry));

PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled")
<< LOG_DESC("addSealer successfully insert") << LOG_KV("nodeID", nodeID)
<< LOG_KV("weight", weight);
<< LOG_DESC("addSealer successfully insert")
<< LOG_KV("isConsensus", isConsensus) << LOG_KV("nodeID", nodeID)
<< LOG_KV("weight", voteWeight);
return 0;
}

int ConsensusPrecompiled::addObserver(
int ConsensusPrecompiled::addSealer(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec)
{
// addObserver(string)
// addSealer(string, uint256)
std::string nodeID;
u256 voteWeight;
const auto& blockContext = _executive->blockContext();
codec.decode(_data, nodeID);
// Uniform lowercase nodeID
boost::to_lower(nodeID);
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("ConsensusPrecompiled") << LOG_DESC("addObserver")
<< LOG_KV("nodeID", nodeID);

if (nodeID.size() != NODE_LENGTH ||
std::count_if(nodeID.begin(), nodeID.end(),
[](unsigned char c) { return std::isxdigit(c); }) != NODE_LENGTH)
{
PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled")
<< LOG_DESC("nodeID length mistake") << LOG_KV("nodeID", nodeID);
return CODE_INVALID_NODE_ID;
}
codec.decode(_data, nodeID, voteWeight);

auto& storage = _executive->storage();

auto entry = storage.getRow(SYS_CONSENSUS, "key");

ConsensusNodeList consensusList;
if (entry)
{
consensusList = entry->getObject<ConsensusNodeList>();
}
else
{
entry.emplace(Entry());
}
auto node = std::find_if(consensusList.begin(), consensusList.end(),
[&nodeID](const ConsensusNode& node) { return node.nodeID == nodeID; });
if (node != consensusList.end())
{
// find it in consensus list
auto sealerCount = std::count_if(consensusList.begin(), consensusList.end(),
[](auto&& node) { return node.type == ledger::CONSENSUS_SEALER; });
if (sealerCount == 1)
{
PRECOMPILED_LOG(DEBUG) << LOG_BADGE("ConsensusPrecompiled")
<< LOG_DESC("addObserver failed, because last sealer");
return CODE_LAST_SEALER;
}
node->weight = 0;
node->type = ledger::CONSENSUS_OBSERVER;
node->enableNumber = boost::lexical_cast<std::string>(blockContext.number() + 1);
}
else
{
consensusList.emplace_back(nodeID, 0, std::string{ledger::CONSENSUS_OBSERVER},
boost::lexical_cast<std::string>(blockContext.number() + 1));
}
return innerAddSealer(true, _executive, std::move(nodeID), voteWeight, 0);
}

entry->setObject(consensusList);
int ConsensusPrecompiled::addSealer2(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec)
{
// addSealer(string, uint256, uint256)
std::string nodeID;
u256 voteWeight;
u256 termWeight;
const auto& blockContext = _executive->blockContext();
codec.decode(_data, nodeID, voteWeight, termWeight);

storage.setRow(SYS_CONSENSUS, "key", std::move(*entry));
return innerAddSealer(
true, _executive, std::move(nodeID), voteWeight, termWeight.convert_to<uint64_t>());
}

return 0;
int ConsensusPrecompiled::addObserver(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec)
{
// addObserver(string)
std::string nodeID;
codec.decode(_data, nodeID);
return innerAddSealer(false, _executive, std::move(nodeID), 0, 0);
}

int ConsensusPrecompiled::removeNode(
Expand Down Expand Up @@ -367,7 +366,7 @@ int ConsensusPrecompiled::setWeight(
{
BOOST_THROW_EXCEPTION(protocol::PrecompiledError("Cannot set weight to observer."));
}
node->weight = weight;
node->voteWeight = weight;
node->enableNumber = boost::lexical_cast<std::string>(blockContext.number() + 1);
}
else
Expand Down Expand Up @@ -444,10 +443,10 @@ void ConsensusPrecompiled::showConsensusTable(
consensusTable << "ConsensusPrecompiled show table:\n";
for (auto& node : consensusList)
{
auto& [nodeID, weight, type, enableNumber] = node;
auto& [nodeID, voteWeight, type, enableNumber, termWeight] = node;

consensusTable << "ConsensusPrecompiled: " << nodeID << "," << type << "," << enableNumber
<< "," << weight << "\n";
<< "," << voteWeight << "\n";
}
PRECOMPILED_LOG(TRACE) << LOG_BADGE("ConsensusPrecompiled") << LOG_DESC("showConsensusTable")
<< LOG_KV("consensusTable", consensusTable.str());
Expand Down
32 changes: 21 additions & 11 deletions bcos-executor/src/precompiled/ConsensusPrecompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,39 @@ class ConsensusPrecompiled : public bcos::precompiled::Precompiled
{
public:
using Ptr = std::shared_ptr<ConsensusPrecompiled>;
explicit ConsensusPrecompiled(const crypto::Hash::Ptr& _hashImpl);
explicit ConsensusPrecompiled(crypto::Hash::Ptr _hashImpl);
~ConsensusPrecompiled() override = default;

std::shared_ptr<PrecompiledExecResult> call(
std::shared_ptr<executor::TransactionExecutive> _executive,
PrecompiledExecResult::Ptr _callParameters) override;

private:
[[nodiscard]] int addSealer(const std::shared_ptr<executor::TransactionExecutive>& _executive,
bytesConstRef& _data, const CodecWrapper& codec);
[[nodiscard]] static int addSealer(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec);

[[nodiscard]] int addObserver(const std::shared_ptr<executor::TransactionExecutive>& _executive,
bytesConstRef& _data, const CodecWrapper& codec);
[[nodiscard]] static int addSealer2(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec);

[[nodiscard]] int removeNode(const std::shared_ptr<executor::TransactionExecutive>& _executive,
bytesConstRef& _data, const CodecWrapper& codec);
[[nodiscard]] static int addObserver(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec);

[[nodiscard]] int setWeight(const std::shared_ptr<executor::TransactionExecutive>& _executive,
bytesConstRef& _data, const CodecWrapper& codec);
[[nodiscard]] static int removeNode(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec);

void rotateWorkingSealer(const std::shared_ptr<executor::TransactionExecutive>& _executive,
[[nodiscard]] static int setWeight(
const std::shared_ptr<executor::TransactionExecutive>& _executive, bytesConstRef& _data,
const CodecWrapper& codec);

static void rotateWorkingSealer(
const std::shared_ptr<executor::TransactionExecutive>& _executive,
const PrecompiledExecResult::Ptr& _callParameters, const CodecWrapper& codec);

void showConsensusTable(const std::shared_ptr<executor::TransactionExecutive>& _executive);
static void showConsensusTable(
const std::shared_ptr<executor::TransactionExecutive>& _executive);
};
} // namespace bcos::precompiled
18 changes: 11 additions & 7 deletions bcos-framework/bcos-framework/consensus/ConsensusNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ class ConsensusNode : public ConsensusNodeInterface
{
public:
using Ptr = std::shared_ptr<ConsensusNode>;
ConsensusNode(const ConsensusNode&) = default;
ConsensusNode(ConsensusNode&&) = default;
ConsensusNode& operator=(const ConsensusNode&) = default;
ConsensusNode& operator=(ConsensusNode&&) = default;
explicit ConsensusNode(bcos::crypto::PublicPtr _nodeID) : m_nodeID(std::move(_nodeID)) {}

ConsensusNode(bcos::crypto::PublicPtr _nodeID, uint64_t _weight)
: m_nodeID(std::move(_nodeID)), m_weight(_weight)
ConsensusNode(bcos::crypto::PublicPtr nodeID, uint64_t voteWeight, uint64_t termWeight)
: m_nodeID(std::move(nodeID)), m_voteWeight(voteWeight), m_termWeight(termWeight)
{}

~ConsensusNode() override {}

~ConsensusNode() override = default;
bcos::crypto::PublicPtr nodeID() const override { return m_nodeID; }
uint64_t weight() const override { return m_weight; }
uint64_t voteWeight() const override { return m_voteWeight; }
uint64_t termWeight() const override { return m_termWeight; }

private:
bcos::crypto::PublicPtr m_nodeID;
uint64_t m_weight = 100;
uint64_t m_voteWeight = defaultVoteWeight;
uint64_t m_termWeight = defaultTermWeight;
};
} // namespace bcos::consensus
Loading

0 comments on commit 855ff63

Please sign in to comment.