Skip to content

Commit

Permalink
add policy1 start check and add balance overflow check (FISCO-BCOS#4183)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Jan 18, 2024
1 parent 1a059a2 commit b4bc5d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
11 changes: 11 additions & 0 deletions bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ void AccountPrecompiled::addAccountBalance(const std::string& accountTableName,
if (entry.has_value())
{
u256 balance = u256(std::string(entry->get()));
if (balance >= (u256(-1) - value))
{
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("AccountPrecompiled, addAccountBalance")
<< LOG_DESC("account balance overflow")
<< LOG_KV("account", accountTableName)
<< LOG_KV("balance", to_string(balance))
<< LOG_KV("add balance", to_string(value));
BOOST_THROW_EXCEPTION(PrecompiledError("Account balance overflow!"));
return;
}
balance += value;
Entry Balance;
Balance.importFields({boost::lexical_cast<std::string>(balance)});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ BOOST_AUTO_TEST_CASE(getAndSetFeature)
setInput = codec.encodeWithSig(
"setValueByKey(string,string)", std::string("feature_balance_policy1"), std::string("1"));
setParameters->m_input = bcos::ref(setInput);
auto featureBalancePolResult = systemConfigPrecompiled.call(newExecutive, setParameters);

codec.decode(bcos::ref(featureBalancePolResult->execResult()), code);
BOOST_CHECK_EQUAL(code, 0);
BOOST_CHECK_THROW(systemConfigPrecompiled.call(newExecutive, setParameters), PrecompiledError);
}

BOOST_AUTO_TEST_CASE(upgradeVersion)
Expand Down
8 changes: 6 additions & 2 deletions bcos-framework/bcos-framework/ledger/Features.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ class Features

void validate(Flag flag) const
{
if ((flag == Flag::feature_balance_policy1 || flag == Flag::feature_balance_precompiled) &&
!get(Flag::feature_balance))
if (flag == Flag::feature_balance_precompiled && !get(Flag::feature_balance))
{
BOOST_THROW_EXCEPTION(
PreconditionMismatchError{} << errinfo_comment("must set feature_balance first"));
}
if (flag == Flag::feature_balance_policy1 && !get(Flag::feature_balance_precompiled))
{
BOOST_THROW_EXCEPTION(PreconditionMismatchError{}
<< errinfo_comment("must set feature_balance_precompiled first"));
}
}

bool get(Flag flag) const
Expand Down

0 comments on commit b4bc5d7

Please sign in to comment.