55 Star 302 Fork 0

FISCO-BCOS / FISCO-BCOS

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
HelloWorldPrecompiled.cpp 6.10 KB
Copy Edit Raw Blame History
章鱼哥 authored 2021-06-08 21:27 . update boost version (#1948)
/*
* @CopyRight:
* FISCO-BCOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FISCO-BCOS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FISCO-BCOS. If not, see <http://www.gnu.org/licenses/>
* (c) 2016-2018 fisco-dev contributors.
*/
/** @file HelloWorldPrecompiled.cpp
* @author octopuswang
* @date 20190111
*/
#include "HelloWorldPrecompiled.h"
#include <libblockverifier/ExecutiveContext.h>
#include <libethcore/ABI.h>
#include <libprecompiled/TableFactoryPrecompiled.h>
using namespace dev;
using namespace dev::blockverifier;
using namespace dev::storage;
using namespace dev::precompiled;
/*
contract HelloWorld {
function get() public constant returns(string);
function set(string _m) public;
}
*/
// HelloWorldPrecompiled table name
const std::string HELLO_WORLD_TABLE_NAME = "hello_world";
// key field
const std::string HELLOWORLD_KEY_FIELD = "key";
const std::string HELLOWORLD_KEY_FIELD_NAME = "hello_key";
// value field
const std::string HELLOWORLD_VALUE_FIELD = "value";
// get interface
const char* const HELLO_WORLD_METHOD_GET = "get()";
// set interface
const char* const HELLO_WORLD_METHOD_SET = "set(string)";
HelloWorldPrecompiled::HelloWorldPrecompiled()
{
name2Selector[HELLO_WORLD_METHOD_GET] = getFuncSelector(HELLO_WORLD_METHOD_GET);
name2Selector[HELLO_WORLD_METHOD_SET] = getFuncSelector(HELLO_WORLD_METHOD_SET);
}
std::string HelloWorldPrecompiled::toString()
{
return "HelloWorld";
}
PrecompiledExecResult::Ptr HelloWorldPrecompiled::call(
dev::blockverifier::ExecutiveContext::Ptr _context, bytesConstRef _param,
Address const& _origin, Address const&)
{
PRECOMPILED_LOG(TRACE) << LOG_BADGE("HelloWorldPrecompiled") << LOG_DESC("call")
<< LOG_KV("param", toHex(_param));
// parse function name
uint32_t func = getParamFunc(_param);
bytesConstRef data = getParamData(_param);
auto callResult = m_precompiledExecResultFactory->createPrecompiledResult();
callResult->gasPricer()->setMemUsed(_param.size());
dev::eth::ContractABI abi;
Table::Ptr table = openTable(_context, precompiled::getTableName(HELLO_WORLD_TABLE_NAME));
callResult->gasPricer()->appendOperation(InterfaceOpcode::OpenTable);
if (!table)
{
// table is not exist, create it.
table = createTable(_context, precompiled::getTableName(HELLO_WORLD_TABLE_NAME),
HELLOWORLD_KEY_FIELD, HELLOWORLD_VALUE_FIELD, _origin);
callResult->gasPricer()->appendOperation(InterfaceOpcode::CreateTable);
if (!table)
{
PRECOMPILED_LOG(ERROR) << LOG_BADGE("HelloWorldPrecompiled") << LOG_DESC("set")
<< LOG_DESC("open table failed.");
getErrorCodeOut(callResult->mutableExecResult(), storage::CODE_NO_AUTHORIZED);
return callResult;
}
}
if (func == name2Selector[HELLO_WORLD_METHOD_GET])
{ // get() function call
// default retMsg
std::string retValue = "Hello World!";
auto entries = table->select(HELLOWORLD_KEY_FIELD_NAME, table->newCondition());
if (0u != entries->size())
{
callResult->gasPricer()->updateMemUsed(getEntriesCapacity(entries));
callResult->gasPricer()->appendOperation(InterfaceOpcode::Select, entries->size());
auto entry = entries->get(0);
retValue = entry->getField(HELLOWORLD_VALUE_FIELD);
PRECOMPILED_LOG(ERROR) << LOG_BADGE("HelloWorldPrecompiled") << LOG_DESC("get")
<< LOG_KV("value", retValue);
}
callResult->setExecResult(abi.abiIn("", retValue));
}
else if (func == name2Selector[HELLO_WORLD_METHOD_SET])
{ // set(string) function call
std::string strValue;
abi.abiOut(data, strValue);
auto entries = table->select(HELLOWORLD_KEY_FIELD_NAME, table->newCondition());
callResult->gasPricer()->updateMemUsed(getEntriesCapacity(entries));
callResult->gasPricer()->appendOperation(InterfaceOpcode::Select, entries->size());
auto entry = table->newEntry();
entry->setField(HELLOWORLD_KEY_FIELD, HELLOWORLD_KEY_FIELD_NAME);
entry->setField(HELLOWORLD_VALUE_FIELD, strValue);
int count = 0;
if (0u != entries->size())
{ // update
count = table->update(HELLOWORLD_KEY_FIELD_NAME, entry, table->newCondition(),
std::make_shared<AccessOptions>(_origin));
if (count > 0)
{
callResult->gasPricer()->appendOperation(InterfaceOpcode::Update, count);
callResult->gasPricer()->updateMemUsed(entry->capacity() * count);
}
}
else
{ // insert
count = table->insert(
HELLOWORLD_KEY_FIELD_NAME, entry, std::make_shared<AccessOptions>(_origin));
if (count > 0)
{
callResult->gasPricer()->updateMemUsed(entry->capacity() * count);
callResult->gasPricer()->appendOperation(InterfaceOpcode::Insert, count);
}
}
if (count == storage::CODE_NO_AUTHORIZED)
{ // permission denied
PRECOMPILED_LOG(ERROR) << LOG_BADGE("HelloWorldPrecompiled") << LOG_DESC("set")
<< LOG_DESC("permission denied");
}
getErrorCodeOut(callResult->mutableExecResult(), count);
}
else
{ // unknown function call
PRECOMPILED_LOG(ERROR) << LOG_BADGE("HelloWorldPrecompiled") << LOG_DESC(" unknown func ")
<< LOG_KV("func", func);
callResult->setExecResult(abi.abiIn("", u256(int32_t(CODE_UNKNOW_FUNCTION_CALL))));
}
return callResult;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/FISCO-BCOS/FISCO-BCOS.git
git@gitee.com:FISCO-BCOS/FISCO-BCOS.git
FISCO-BCOS
FISCO-BCOS
FISCO-BCOS
master-2.0

Search

344bd9b3 5694891 D2dac590 5694891