From 06279aff592417ae0cfb979d01502bfd68b982a8 Mon Sep 17 00:00:00 2001 From: wenlinli <1574249665@qq.com> Date: Thu, 4 Jan 2024 19:33:49 +0800 Subject: [PATCH] add precompiledAddress filter for balance interface --- .../extension/AccountPrecompiled.cpp | 22 ++++++++++++++++++- .../executor/PrecompiledTypeDef.h | 9 ++++++++ .../utilities/logger/LogInitializer.h | 3 --- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp b/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp index 6ac51e33a5..603567ea4b 100644 --- a/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp +++ b/bcos-executor/src/precompiled/extension/AccountPrecompiled.cpp @@ -276,7 +276,16 @@ void AccountPrecompiled::addAccountBalance(const std::string& accountTableName, getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec); return; } - + if (isPrecompiledAddressRange(accountTableName.substr(6))) + { + PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number()) + << LOG_BADGE("AccountPrecompiled, addAccountBalance") + << LOG_DESC("account is precompiled address") + << LOG_KV("account", accountTableName.substr(6)); + BOOST_THROW_EXCEPTION( + PrecompiledError("addBalance failed, toAddress is precompiledAddress!")); + return; + } // check account exist auto table = _executive->storage().openTable(accountTableName); if (!table.has_value()) [[unlikely]] @@ -353,6 +362,17 @@ void AccountPrecompiled::subAccountBalance(const std::string& accountTableName, getErrorCodeOut(_callParameters->mutableExecResult(), CODE_NO_AUTHORIZED, codec); return; } + // check account is precompiled address, if true, return + if (isPrecompiledAddressRange(accountTableName.substr(6))) + { + PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number()) + << LOG_BADGE("AccountPrecompiled, addAccountBalance") + << LOG_DESC("account is precompiled address") + << LOG_KV("account", accountTableName.substr(6)); + BOOST_THROW_EXCEPTION( + PrecompiledError("addBalance failed, toAddress is precompiledAddress!")); + return; + } // check account exist auto table = _executive->storage().openTable(accountTableName); diff --git a/bcos-framework/bcos-framework/executor/PrecompiledTypeDef.h b/bcos-framework/bcos-framework/executor/PrecompiledTypeDef.h index cb81daec92..b01019f899 100644 --- a/bcos-framework/bcos-framework/executor/PrecompiledTypeDef.h +++ b/bcos-framework/bcos-framework/executor/PrecompiledTypeDef.h @@ -21,6 +21,8 @@ #pragma once #include "bcos-framework/protocol/ProtocolTypeDef.h" #include +#include +#include namespace bcos { @@ -31,6 +33,13 @@ constexpr inline bool isSysContractDeploy(protocol::BlockNumber _number) return _number == SYS_CONTRACT_DEPLOY_NUMBER; } +constexpr inline bool isPrecompiledAddressRange(std::string_view _address) +{ + unsigned int address; + auto result = std::from_chars(_address.data(), _address.data() + _address.size(), address, 16); + return result.ec == std::errc() && (address >= 0x0 && address < 0x20000); +} + namespace precompiled { /// precompiled contract path for wasm diff --git a/bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.h b/bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.h index 2fb9ded0fe..e5eb32e5cd 100644 --- a/bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.h +++ b/bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.h @@ -46,9 +46,6 @@ class LogInitializer } catch (const std::exception& e) { - std::cerr << "Not found available log config(./clog.ini or ./conf/clog.ini), use " - "the default configuration items, disable log by default." - << std::endl; // log disable by default pt.put("log.enable", false); }