Skip to content

Commit

Permalink
BalancePrecompiled add listCaller interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee committed Dec 25, 2023
1 parent 821fdb0 commit 9c95273
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
68 changes: 55 additions & 13 deletions bcos-executor/src/precompiled/extension/BalancePrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const char* const BALANCE_METHOD_SUB_BALANCE = "subBalance(address,uint256)";
const char* const BALANCE_METHOD_TRANSFER = "transfer(address,address,uint256)";
const char* const BALANCE_METHOD_REGISTER_CALLER = "registerCaller(address)";
const char* const BALANCE_METHOD_UNREGISTER_CALLER = "unregisterCaller(address)";
const char* const BALANCE_METHOD_LIST_CALLER = "listCaller()";

BalancePrecompiled::BalancePrecompiled(crypto::Hash::Ptr _hashImpl) : Precompiled(_hashImpl)
{
Expand All @@ -54,6 +55,8 @@ BalancePrecompiled::BalancePrecompiled(crypto::Hash::Ptr _hashImpl) : Precompile
getFuncSelector(BALANCE_METHOD_REGISTER_CALLER, _hashImpl);
name2Selector[BALANCE_METHOD_UNREGISTER_CALLER] =
getFuncSelector(BALANCE_METHOD_UNREGISTER_CALLER, _hashImpl);
name2Selector[BALANCE_METHOD_LIST_CALLER] =
getFuncSelector(BALANCE_METHOD_LIST_CALLER, _hashImpl);
}

std::shared_ptr<PrecompiledExecResult> BalancePrecompiled::call(
Expand Down Expand Up @@ -88,6 +91,10 @@ std::shared_ptr<PrecompiledExecResult> BalancePrecompiled::call(
{
unregisterCaller(_executive, _callParameters);
}
else if (func == name2Selector[BALANCE_METHOD_LIST_CALLER])
{
listCaller(_executive, _callParameters);
}
else
{
BOOST_THROW_EXCEPTION(protocol::PrecompiledError("invalid function selector"));
Expand Down Expand Up @@ -132,18 +139,6 @@ void BalancePrecompiled::getBalance(
codec.decode(_callParameters->params(), account);
std::string accountStr = account.hex();


auto usrTableName = getAccountTableName(accountStr);
auto usrTable = _executive->storage().openTable(usrTableName);
auto appsTableName = getContractTableName(executor::USER_APPS_PREFIX, accountStr);
auto appsTable = _executive->storage().openTable(appsTableName);
if (!usrTable && !appsTable)
{
BOOST_THROW_EXCEPTION(protocol::PrecompiledError(
"account appsTable and usrTable not exist, getBalance failed"));
return;
}

// get balance from account table
auto params = codec.encodeWithSig("getAccountBalance()");
auto tableName = getContractTableName(executor::USER_APPS_PREFIX, accountStr);
Expand Down Expand Up @@ -507,7 +502,7 @@ void BalancePrecompiled::unregisterCaller(
if (!table)
{
std::string tableStr(SYS_BALANCE_CALLER);
_executive->storage().createTable(tableStr, "caller_address");
_executive->storage().createTable(tableStr, "value");
PRECOMPILED_LOG(WARNING) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("BalancePrecompiled") << LOG_DESC("unregisterCaller")
<< LOG_KV("account", accountStr);
Expand All @@ -527,3 +522,50 @@ void BalancePrecompiled::unregisterCaller(
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number()) << LOG_BADGE("BalancePrecompiled")
<< LOG_DESC("unregisterCaller success") << LOG_KV("account", accountStr);
}

void BalancePrecompiled::listCaller(
const std::shared_ptr<executor::TransactionExecutive>& _executive,
const PrecompiledExecResult::Ptr& _callParameters)
{
// listCaller
const auto& blockContext = _executive->blockContext();
auto codec = CodecWrapper(blockContext.hashHandler(), blockContext.isWasm());

auto table = _executive->storage().openTable(SYS_BALANCE_CALLER);
if (!table)
{
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("BalancePrecompiled")
<< LOG_DESC("listCaller failed, caller table not exist");
BOOST_THROW_EXCEPTION(protocol::PrecompiledError("caller table not exist."));
}

auto keyCondition = std::make_shared<storage::Condition>();
keyCondition->limit(0, USER_TABLE_MAX_LIMIT_COUNT);
auto tableKeyList = table->getPrimaryKeys(*keyCondition);
if (tableKeyList.size() == 0)
{
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("BalancePrecompiled")
<< LOG_DESC("listCaller failed, caller table is empty");
BOOST_THROW_EXCEPTION(protocol::PrecompiledError("caller table is empty."));
}
Addresses addresses;
for (auto& it : tableKeyList)
{
auto entry = table->getRow(it);
if (entry && entry->get() == "1")
{
it = "0x" + it;
addresses.push_back(Address(it));
PRECOMPILED_LOG(DEBUG) << BLOCK_NUMBER(blockContext.number())
<< LOG_BADGE("BalancePrecompiled") << LOG_DESC("listCaller")
<< LOG_KV("caller", it) << LOG_KV("address", Address(it).hex());
}
}
PRECOMPILED_LOG(INFO) << BLOCK_NUMBER(blockContext.number()) << LOG_BADGE("BalancePrecompiled")
<< LOG_DESC("listCaller success")
<< LOG_KV("addresses size", addresses.size());
auto encodeCallers = codec.encode(addresses);
_callParameters->setExecResult(std::move(encodeCallers));
}
3 changes: 3 additions & 0 deletions bcos-executor/src/precompiled/extension/BalancePrecompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class BalancePrecompiled : public bcos::precompiled::Precompiled
void unregisterCaller(const std::shared_ptr<executor::TransactionExecutive>& _executive,
PrecompiledExecResult::Ptr const& _callParameters);

void listCaller(const std::shared_ptr<executor::TransactionExecutive>& _executive,
PrecompiledExecResult::Ptr const& _callParameters);

void createAccount(const std::shared_ptr<executor::TransactionExecutive>& _executive,
PrecompiledExecResult::Ptr const& _callParameters, const CodecWrapper& codec,
std::string_view accountHex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.0;

contract BalancePrecompiled {
function getBalance(address account) public returns (uint256) {}
function getBalance(address account) public view returns (uint256) {}

function addBalance(address account, uint256 amount) public {}

Expand All @@ -13,4 +13,6 @@ contract BalancePrecompiled {
function registerCaller(address account) public {}

function unregisterCaller(address account) public {}

function listCaller() public view returns (address[] memory) {}
}

0 comments on commit 9c95273

Please sign in to comment.