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 Nov 30, 2023
2 parents af38062 + 9475e4f commit b241f24
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 91 deletions.
91 changes: 42 additions & 49 deletions bcos-executor/src/executive/TransactionExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,38 +229,42 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << LOG_DESC("Execute begin")
<< LOG_KV("callParameters", callParameters->toFullString())
<< LOG_KV("blockNumber", m_blockContext.number());

}
m_storageWrapper->setRecoder(m_recoder);
std::unique_ptr<HostContext> hostContext;
CallParameters::UniquePtr callResults;
if (c_fileLogLevel <= LogLevel::TRACE)
{
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << LOG_DESC("Execute begin")
<< LOG_KV("feature_balance", m_blockContext.features().get(ledger::Features::Flag::feature_balance))
<< LOG_KV(
"feature_balance", m_blockContext.features().get(
ledger::Features::Flag::feature_balance))
<< LOG_KV("value", callParameters->value);
}

if (m_blockContext.features().get(ledger::Features::Flag::feature_balance) && callParameters->value > 0)
if (m_blockContext.features().get(ledger::Features::Flag::feature_balance) &&
callParameters->value > 0)
{
if (false == transferBalance(callParameters->origin, callParameters->senderAddress,
callParameters->receiveAddress, static_cast<u256>(callParameters->value), callParameters->gas))
{
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << LOG_DESC("transferBalance failed and will revet");
if (false == transferBalance(callParameters->origin, callParameters->senderAddress,
callParameters->receiveAddress, static_cast<u256>(callParameters->value),
callParameters->gas))
{
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute")
<< LOG_DESC("transferBalance failed and will revet");
revert();
callResults = std::move(callParameters);
callResults = std::move(callParameters);
callResults->type = CallParameters::REVERT;
callResults->status = (int32_t)TransactionStatus::RevertInstruction;
}
else
else
{
hostContext = nullptr;
callResults = std::move(callParameters);
hostContext = nullptr;
callResults = std::move(callParameters);
callResults->type = CallParameters::FINISHED;
callResults->status = (int32_t)TransactionStatus::None;
}
}

else if (callParameters->create)
{
std::tie(hostContext, callResults) = create(std::move(callParameters));
Expand Down Expand Up @@ -288,19 +292,16 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt
}


bool TransactionExecutive::transferBalance(std::string_view origin, std::string_view sender,
bool TransactionExecutive::transferBalance(std::string_view origin, std::string_view sender,
std::string_view receiver, const u256& value, int64_t gas)
{
// origin 是发送方
// sender 是合约地址
// receiver 是转账接收方
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute")
<< "now to transferBalance"
<< LOG_KV("subAccount", origin)
<< LOG_KV("addAccount", receiver)
<< LOG_KV("receiveAddress", ACCOUNT_ADDRESS)
<< LOG_KV("value", value)
<< LOG_KV("gas", gas);
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "now to transferBalance"
<< LOG_KV("subAccount", origin) << LOG_KV("addAccount", receiver)
<< LOG_KV("receiveAddress", ACCOUNT_ADDRESS) << LOG_KV("value", value)
<< LOG_KV("gas", gas);

// first subAccountBalance, then addAccountBalance
// origin = origin - value
Expand All @@ -310,21 +311,17 @@ bool TransactionExecutive::transferBalance(std::string_view origin, std::string_
std::vector<std::string> fromTableNameVector = {formTableName};
auto inputParams = codec.encode(fromTableNameVector, params);
auto subParams = codec.encode(std::string(ACCOUNT_ADDRESS), inputParams);
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute")
<< "transferBalance start, now is sub."
<< LOG_KV("tableName", formTableName)
<< LOG_KV("will sub balance", value);
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "transferBalance start, now is sub."
<< LOG_KV("tableName", formTableName) << LOG_KV("will sub balance", value);

auto reposeSub = externalRequest(shared_from_this(), ref(subParams), origin,
std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true);
s256 result;
codec.decode(ref(reposeSub->data), result);
if (result != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS))
{
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute")
<< LOG_DESC("transferBalance sub failed")
<< LOG_KV("subAccount", origin)
<< LOG_KV("tablename", formTableName);
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << LOG_DESC("transferBalance sub failed")
<< LOG_KV("subAccount", origin) << LOG_KV("tablename", formTableName);
return false;
}

Expand All @@ -336,45 +333,41 @@ bool TransactionExecutive::transferBalance(std::string_view origin, std::string_
auto inputParams1 = codec.encode(toTableNameVector, params1);
auto addParams = codec.encode(std::string(ACCOUNT_ADDRESS), inputParams1);

EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute")
<< "transferBalance start, now is add."
<< LOG_KV("tableName", toTableName)
<< LOG_KV("will add balance", value);
EXECUTIVE_LOG(TRACE) << LOG_BADGE("Execute") << "transferBalance start, now is add."
<< LOG_KV("tableName", toTableName) << LOG_KV("will add balance", value);
auto reposeAdd = externalRequest(shared_from_this(), ref(addParams), origin,
std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true);
std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true);
codec.decode(ref(reposeAdd->data), result);
if (result != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS))
{
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute")
<< LOG_DESC("transferBalance add failed, need to restore")
<< LOG_KV("addAccount", receiver)
<< LOG_KV("tablename", toTableName);
<< LOG_DESC("transferBalance add failed, need to restore")
<< LOG_KV("addAccount", receiver) << LOG_KV("tablename", toTableName);

// if receiver add failed, sender need to restore
// sender = sender + value
auto revertParams = codec.encode(fromTableNameVector, params1);
auto addParams1 = codec.encode(std::string(ACCOUNT_ADDRESS), revertParams);
auto reponseRestore = externalRequest(shared_from_this(), ref(addParams1), origin,
std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true);
std::string(EVM_BALANCE_SENDER_ADDRESS), receiver, false, false, gas, true);
s256 addResult;
codec.decode(ref(reponseRestore->data), addResult);
if (addResult != s256((int)bcos::precompiled::PrecompiledErrorCode::CODE_SUCCESS))
{
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute")
<< LOG_DESC("transferBalance to sub success but add failed, strike a balance failed.")
<< LOG_KV("restoreAccount", origin)
<< LOG_KV("tablename", formTableName);
BOOST_THROW_EXCEPTION(PrecompiledError("transferBalance to sub success but add failed, strike a balance failed."));
EXECUTIVE_LOG(DEBUG)
<< LOG_BADGE("Execute")
<< LOG_DESC(
"transferBalance to sub success but add failed, strike a balance failed.")
<< LOG_KV("restoreAccount", origin) << LOG_KV("tablename", formTableName);
BOOST_THROW_EXCEPTION(PrecompiledError(
"transferBalance to sub success but add failed, strike a balance failed."));
}
return false;
}
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute")
<< "transferBalance finished."
<< LOG_KV("subAccount", origin)
<< LOG_KV("addAccount", receiver)
<< LOG_KV("receiveAddress", ACCOUNT_ADDRESS)
<< LOG_KV("value", value)
<< LOG_KV("gas", gas);
EXECUTIVE_LOG(DEBUG) << LOG_BADGE("Execute") << "transferBalance finished."
<< LOG_KV("subAccount", origin) << LOG_KV("addAccount", receiver)
<< LOG_KV("receiveAddress", ACCOUNT_ADDRESS) << LOG_KV("value", value)
<< LOG_KV("gas", gas);
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions bcos-executor/src/executive/TransactionExecutive.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class TransactionExecutive : public std::enable_shared_from_this<TransactionExec
std::string_view _sender, uint32_t _version);

protected:
bool transferBalance(std::string_view origin, std::string_view sender,
bool transferBalance(std::string_view origin, std::string_view sender,
std::string_view receiver, const u256& value, int64_t gas);
std::tuple<std::unique_ptr<HostContext>, CallParameters::UniquePtr> call(
CallParameters::UniquePtr callParameters);
Expand All @@ -163,10 +163,10 @@ class TransactionExecutive : public std::enable_shared_from_this<TransactionExec
CallParameters::UniquePtr callDynamicPrecompiled(
CallParameters::UniquePtr callParameters, const std::string& code);

// virtual TransactionExecutive::Ptr buildChildExecutive(const std::string& _contractAddress,
// int64_t contextID, int64_t seq, bool useCoroutine = true)
// virtual TransactionExecutive::Ptr buildChildExecutive(const std::string& _contractAddress,
// int64_t contextID, int64_t seq, bool useCoroutine = true)
virtual TransactionExecutive::Ptr buildChildExecutive(const std::string& _contractAddress,
int64_t contextID, int64_t seq, ExecutiveType execType = ExecutiveType::coroutine)
int64_t contextID, int64_t seq, ExecutiveType execType = ExecutiveType::coroutine)
{
auto executiveFactory = std::make_shared<ExecutiveFactory>(
m_blockContext, m_evmPrecompiled, m_precompiled, m_staticPrecompiled, m_gasInjector);
Expand Down
16 changes: 9 additions & 7 deletions bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ std::shared_ptr<PrecompiledExecResult> AccountPrecompiled::call(
uint32_t func = getParamFunc(originParam);
bytesConstRef data = getParamData(originParam);
auto table = _executive->storage().openTable(accountTableName);
// FIXME: for fake debug, temporary comment
// if (!table.has_value()) [[unlikely]]
// {
// BOOST_THROW_EXCEPTION(PrecompiledError(accountTableName + " does not exist"));
// }
// FIXME: for fake debug, temporary comment
// if (!table.has_value()) [[unlikely]]
// {
// BOOST_THROW_EXCEPTION(PrecompiledError(accountTableName + " does not exist"));
// }

if (func == name2Selector[AM_METHOD_SET_ACCOUNT_STATUS])
{
Expand Down Expand Up @@ -269,7 +269,8 @@ void AccountPrecompiled::addAccountBalance(const std::string& accountTableName,
// check sender
const auto* addAccountBalanceSender =
blockContext.isWasm() ? BALANCE_PRECOMPILED_NAME : BALANCE_PRECOMPILED_ADDRESS;
if (!(_callParameters->m_sender == addAccountBalanceSender || _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS))
if (!(_callParameters->m_sender == addAccountBalanceSender ||
_callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS))
{
getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec);
return;
Expand Down Expand Up @@ -333,7 +334,8 @@ void AccountPrecompiled::subAccountBalance(const std::string& accountTableName,
// check sender
const auto* subAccountBalanceSender =
blockContext.isWasm() ? BALANCE_PRECOMPILED_NAME : BALANCE_PRECOMPILED_ADDRESS;
if (!(_callParameters->m_sender == subAccountBalanceSender || _callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS))
if (!(_callParameters->m_sender == subAccountBalanceSender ||
_callParameters->m_sender == EVM_BALANCE_SENDER_ADDRESS))
{
getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec);
return;
Expand Down
Loading

0 comments on commit b241f24

Please sign in to comment.