Skip to content

Commit

Permalink
refactor: rename libstorage/State* ==> libstorage/*
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Sep 11, 2018
1 parent 05ec857 commit 0e28c2f
Show file tree
Hide file tree
Showing 40 changed files with 392 additions and 142 deletions.
2 changes: 1 addition & 1 deletion libconsole/ConsoleServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <libp2p/Common.h>
#include <libp2p/Host.h>
#include <libdevcore/FixedHash.h>
#include <libstorage/StateDB.h>
#include <libstorage/DB.h>

using namespace dev;
using namespace console;
Expand Down
6 changes: 3 additions & 3 deletions libconsole/ConsoleServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <libchannelserver/ChannelServer.h>
#include <libwebthree/WebThree.h>
#include <libethereum/Interface.h>
#include <libstorage/StateStorage.h>
#include <libstorage/Storage.h>

namespace dev {

Expand All @@ -29,7 +29,7 @@ class ConsoleServer: public std::enable_shared_from_this<ConsoleServer> {
void setChannelServer(std::shared_ptr<dev::channel::ChannelServer> server) { _server = server; }
void setInterface(dev::eth::Interface *interface) { _interface = interface; }
void setWebThreeDirect(dev::WebThreeDirect *webThreeDirect) { _webThreeDirect = webThreeDirect; }
void setStateStorage(dev::storage::StateStorage::Ptr stateStorage) { _stateStorage = stateStorage; }
void setStateStorage(dev::storage::Storage::Ptr stateStorage) { _stateStorage = stateStorage; }
void onConnect(dev::channel::ChannelException e, dev::channel::ChannelSession::Ptr session);
void onRequest(dev::channel::ChannelSession::Ptr session, dev::channel::ChannelException e, dev::channel::Message::Ptr message);

Expand All @@ -43,7 +43,7 @@ class ConsoleServer: public std::enable_shared_from_this<ConsoleServer> {
std::shared_ptr<dev::channel::ChannelServer> _server;
dev::eth::Interface *_interface = nullptr;
dev::WebThreeDirect *_webThreeDirect = nullptr;
dev::storage::StateStorage::Ptr _stateStorage = nullptr;
dev::storage::Storage::Ptr _stateStorage = nullptr;
bool _running = false;
};

Expand Down
10 changes: 5 additions & 5 deletions libinitializer/AMDBInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <libstorage/MemoryStateDBFactory.h>
#include <libstorage/DBFactoryPrecompiled.h>
#include <libprecompiled/StringFactoryPrecompiled.h>
#include <libstorage/AMOPStateStorage.h>
#include <libstorage/LevelDBStateStorage.h>
#include <libstorage/AMOPStorage.h>
#include <libstorage/LevelDBStorage.h>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>

Expand All @@ -28,7 +28,7 @@ void AMDBInitializer::initConfig(const boost::property_tree::ptree &pt) {
_amopDBConfig.retryInterval = pt.get<int>("statedb.retryInterval", 1);
_amopDBConfig.maxRetry = pt.get<int>("statedb.maxRetry", 0);

dev::storage::AMOPStateStorage::Ptr stateStorage = std::make_shared<dev::storage::AMOPStateStorage>();
dev::storage::AMOPStorage::Ptr stateStorage = std::make_shared<dev::storage::AMOPStorage>();
stateStorage->setChannelRPCServer(_channelRPCServer);
stateStorage->setTopic(_amopDBConfig.topic);
stateStorage->setMaxRetry(_amopDBConfig.maxRetry);
Expand Down Expand Up @@ -61,7 +61,7 @@ void AMDBInitializer::initConfig(const boost::property_tree::ptree &pt) {

auto db = std::shared_ptr<leveldb::DB>(dbPtr);

auto stateStorage = std::make_shared<dev::storage::LevelDBStateStorage>();
auto stateStorage = std::make_shared<dev::storage::LevelDBStorage>();
stateStorage->setDB(db);

_stateStorage = stateStorage;
Expand All @@ -85,7 +85,7 @@ void AMDBInitializer::initConfig(const boost::property_tree::ptree &pt) {
}
}

dev::storage::StateStorage::Ptr AMDBInitializer::stateStorage() {
dev::storage::Storage::Ptr AMDBInitializer::stateStorage() {
return _stateStorage;
}

Expand Down
4 changes: 2 additions & 2 deletions libinitializer/AMDBInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AMDBInitializer: public std::enable_shared_from_this<AMDBInitializer> {
typedef std::shared_ptr<AMDBInitializer> Ptr;

void initConfig(const boost::property_tree::ptree &pt);
dev::storage::StateStorage::Ptr stateStorage();
dev::storage::Storage::Ptr stateStorage();

void setChannelRPCServer(ChannelRPCServer::Ptr channelRPCServer);
dev::precompiled::PrecompiledContextFactory::Ptr precompiledContextFactory();
Expand Down Expand Up @@ -54,7 +54,7 @@ class AMDBInitializer: public std::enable_shared_from_this<AMDBInitializer> {

ChannelRPCServer::Ptr _channelRPCServer;
dev::precompiled::PrecompiledContextFactory::Ptr _precompiledContextFactory;
dev::storage::StateStorage::Ptr _stateStorage;
dev::storage::Storage::Ptr _stateStorage;
std::string _dataPath;
};

Expand Down
2 changes: 1 addition & 1 deletion libinitializer/PBFTInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void PBFTInitializer::initConfig(const boost::property_tree::ptree &pt) {
_pbft->init();
}

void PBFTInitializer::setStateStorage(dev::storage::StateStorage::Ptr stateStorage) {
void PBFTInitializer::setStateStorage(dev::storage::Storage::Ptr stateStorage) {
_stateStorage = stateStorage;
}

Expand Down
4 changes: 2 additions & 2 deletions libinitializer/PBFTInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class PBFTInitializer: public std::enable_shared_from_this<PBFTInitializer> {

void initConfig(const boost::property_tree::ptree &pt);

void setStateStorage(dev::storage::StateStorage::Ptr stateStorage);
void setStateStorage(dev::storage::Storage::Ptr stateStorage);

PBFT::Ptr pbft();

private:
PBFT::Ptr _pbft;
dev::storage::StateStorage::Ptr _stateStorage;
dev::storage::Storage::Ptr _stateStorage;
};

}
Expand Down
46 changes: 46 additions & 0 deletions libinitializer/SecureInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,49 @@ void SecureInitializer::setDataPath(std::string dataPath) {
void SecureInitializer::completePath(std::string &path) {
boost::algorithm::replace_first(path, "${DATAPATH}", _dataPath + "/");
}


void SecureInitializer::loadGmFile()
{

// EncryptFile _encryptFile = getEncryptFile();

// auto cacrt = contents(getDataDir() + "/gmca.crt");
// m_ca=asString(cacrt);
// auto agencycrt = contents(getDataDir() + "/gmagency.crt");
// m_agency = asString(agencycrt);
// auto nodecrt = contents(getDataDir() + "/gmnode.crt");
// m_node = asString(nodecrt);

// //auto nodekey = contents(getDataDir() + "/gmnode.key");
// auto nodekey = _encryptFile.getNodeCrtKey();
// m_nodePri = asString(nodekey);

// auto ennodecrt = contents(getDataDir() + "/gmennode.crt");
// m_enNode = asString(ennodecrt);

// //auto ennodekey = contents(getDataDir() + "/gmennode.key");
// auto ennodekey = _encryptFile.getEnNodeCrtKey();
// m_enNodePri = asString(ennodekey);

// if( m_ca.empty() || m_agency.empty() || nodecrt.empty() || nodekey.empty() || ennodecrt.empty() || ennodekey.empty())
// {
// LOG(ERROR) << "CertificateServer Init Fail! gmca.crt or gmagency.crt or gmnode.crt or gmnode.key or gmennode.crt or gmennode.key File !";
// exit(-1);
// }

// //auto nodeprivate = contents(getDataDir() + "/gmnode.private");
// auto nodeprivate = _encryptFile.getNodePrivateKey();
// string pri = asString(nodeprivate);

// if( pri.size() >= 64 )
// {
// m_keyPair = KeyPair(Secret(fromHex(pri.substr(0,64))) );
// LOG(INFO) << " CertificateServer Load KeyPair " << toPublic(m_keyPair.sec());
// }
// else
// {
// LOG(ERROR) << "CertificateServer Load KeyPair Fail! Please Check gmnode.private File.";
// exit(-1);
// }
}
2 changes: 1 addition & 1 deletion libinitializer/SecureInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SecureInitializer: public std::enable_shared_from_this<SecureInitializer>

private:
void completePath(std::string &path);

void loadGmFile();
KeyPair _key;
std::shared_ptr<boost::asio::ssl::context> _sslContext;

Expand Down
2 changes: 1 addition & 1 deletion libpbftseal/PBFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void PBFT::changeViewForEmptyBlockWithLock() {
m_signalled.notify_all();
}

void PBFT::setStorage(dev::storage::StateStorage::Ptr storage) {
void PBFT::setStorage(dev::storage::Storage::Ptr storage) {
_storage = storage;
}

Expand Down
6 changes: 3 additions & 3 deletions libpbftseal/PBFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <libethereum/CommonNet.h>
#include <libethereum/BlockQueue.h>
#include <libdevcore/FixedHash.h>
#include <libstorage/StateStorage.h>
#include <libstorage/Storage.h>
#include "Common.h"
#include "PBFTHost.h"

Expand Down Expand Up @@ -106,7 +106,7 @@ class PBFT: public SealEngineFace, Worker, public std::enable_shared_from_this<P
u256 quorum() { return getMinerNodeList().size() - m_f; }
uint64_t lastExecFinishTime() const { return m_last_exec_finish_time; }

void setStorage(dev::storage::StateStorage::Ptr storage);
void setStorage(dev::storage::Storage::Ptr storage);
private:
void initBackupDB();
void resetConfig();
Expand Down Expand Up @@ -220,7 +220,7 @@ class PBFT: public SealEngineFace, Worker, public std::enable_shared_from_this<P
u256 m_consensus_block_number; // 在等待共识的块

h512s m_miner_list; //static miner list
dev::storage::StateStorage::Ptr _storage;
dev::storage::Storage::Ptr _storage;

Mutex _current_miner_mutex; //miner list mutex
u256 _current_miner_num = u256(0); //cached miners block num
Expand Down
158 changes: 158 additions & 0 deletions libprecompiled/CRUDPrecompiled.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "CRUDPrecompiled.h"
#include "libstorage/DBFactoryPrecompiled.h"
#include "libstorage/DBPrecompiled.h"
#include "libstorage/EntriesPrecompiled.h"
#include "libstorage/MemoryDB.h"
#include <libdevcore/Hash.h>
#include <libdevcore/easylog.h>
#include <libdevcrypto/Common.h>
#include <libethcore/ABI.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

using namespace dev;
using namespace dev::precompiled;

void CRUDPrecompiled::beforeBlock(std::shared_ptr<PrecompiledContext> context) {}

void CRUDPrecompiled::afterBlock(std::shared_ptr<PrecompiledContext> context, bool commit)
{
if (!commit)
{
LOG(DEBUG) << "Clear _name2Table: " << _name2Table.size();
_name2Table.clear();

return;
}

LOG(DEBUG) << "Submiting CRUDPrecompiled";

//汇总所有表的数据并提交
std::vector<dev::storage::TableData::Ptr> datas;

for (auto &dbIt : _name2Table)
{
dev::storage::TableData::Ptr tableData = std::make_shared<dev::storage::TableData>();
tableData->tableName = dbIt.first;
for (auto &it : *(dbIt.second->data()))
{
if (it.second->dirty() || !_memoryDBFactory->stateStorage()->onlyDirty())
{
tableData->data.insert(std::make_pair(it.first, it.second));
}
}

if (!tableData->data.empty())
{
datas.push_back(tableData);
}
}

LOG(DEBUG) << "Total: " << datas.size() << " key";
if (!datas.empty())
{
if (_hash == h256())
{
hash(context);
}

LOG(DEBUG) << "Submit data:" << datas.size() << " hash:" << _hash;
_memoryDBFactory->stateStorage()->commit(context->blockInfo().hash,
context->blockInfo().number.convert_to<int>(), datas, context->blockInfo().hash);
}

_name2Table.clear();
}

std::string CRUDPrecompiled::toString(std::shared_ptr<PrecompiledContext>)
{
return "CRUD";
}

storage::DB::Ptr CRUDPrecompiled::openTable(
std::shared_ptr<PrecompiledContext> context, const std::string& tableName)
{
LOG(DEBUG) << "CRUD open table:" << tableName;
auto it = _name2Table.find(tableName);
if (it != _name2Table.end())
{
LOG(DEBUG) << "Table:" << it->first << " already opened";
return it->second;
}
dev::storage::DB::Ptr db = _memoryDBFactory->openTable(
context->blockInfo().hash, context->blockInfo().number.convert_to<int>(), tableName);

_name2Table.insert(std::make_pair(tableName, db));
return db;
}

bytes CRUDPrecompiled::call(std::shared_ptr<PrecompiledContext> context, bytesConstRef param)
{
LOG(TRACE) << "this: " << this << " call CRUD:" << toHex(param);

//解析出函数名
uint32_t func = getParamFunc(param);
bytesConstRef data = getParamData(param);

LOG(DEBUG) << "func:" << std::hex << func;

dev::eth::ContractABI abi;

bytes out;

switch (func)
{
case 0x73224cec:
{ // select(string,string)
std::string tableName, key;
abi.abiOut(data, tableName, key);
storage::MemoryDB::Ptr memoryDB =
std::dynamic_pointer_cast<storage::MemoryDB>(openTable(context, tableName));
auto entries = memoryDB->select(key, memoryDB->newCondition());
auto entriesPrecompiled = std::make_shared<EntriesPrecompiled>();
entriesPrecompiled->setEntries(entries);
// entriesPrecompiled->setStringFactoryPrecompiled(_stringFactoryPrecompiled);
auto newAddress = context->registerPrecompiled(entriesPrecompiled);
out = abi.abiIn("", newAddress);
break;
}
default:
{
break;
}
}

return out;
}

h256 CRUDPrecompiled::hash(std::shared_ptr<PrecompiledContext> context)
{
bytes data;

//汇总所有表的hash,算一个hash
LOG(DEBUG) << "this: " << this << " 总计表:" << _name2Table.size();
for (auto &it : _name2Table)
{
storage::DB::Ptr db = it.second;
h256 hash = db->hash();
LOG(DEBUG) << "表:" << it.first << " hash:" << hash;
if (hash == h256())
{
continue;
}

bytes dbHash = db->hash().asBytes();

data.insert(data.end(), dbHash.begin(), dbHash.end());
}

if (data.empty())
{
return h256();
}

LOG(DEBUG) << "CRUDPrecompiled data:" << data << " hash:" << dev::sha256(&data);

_hash = dev::sha256(&data);
return _hash;
}
Loading

0 comments on commit 0e28c2f

Please sign in to comment.