Browse Source

Merge pull request #2824 from arkpar/az-refactor

Moved export state dialog to plugin
cl-refactor
Gav Wood 10 years ago
parent
commit
54bf061f44
  1. 3
      alethzero/CMakeLists.txt
  2. 6
      alethzero/Main.ui
  3. 7
      alethzero/MainWin.cpp
  4. 1
      alethzero/MainWin.h
  5. 91
      alethzero/plugins/blockchain/ExportState.cpp
  6. 15
      alethzero/plugins/blockchain/ExportState.h
  7. 0
      alethzero/plugins/blockchain/ExportState.ui

3
alethzero/CMakeLists.txt

@ -42,7 +42,6 @@ qt5_wrap_ui(ui_Main.h Main.ui)
qt5_wrap_ui(ui_Connect.h Connect.ui) qt5_wrap_ui(ui_Connect.h Connect.ui)
qt5_wrap_ui(ui_Debugger.h Debugger.ui) qt5_wrap_ui(ui_Debugger.h Debugger.ui)
qt5_wrap_ui(ui_Transact.h Transact.ui) qt5_wrap_ui(ui_Transact.h Transact.ui)
qt5_wrap_ui(ui_ExportState.h ExportState.ui)
qt5_wrap_ui(ui_GetPassword.h GetPassword.ui) qt5_wrap_ui(ui_GetPassword.h GetPassword.ui)
qt5_wrap_ui(ui_GasPricing.h GasPricing.ui) qt5_wrap_ui(ui_GasPricing.h GasPricing.ui)
@ -60,7 +59,7 @@ endif ()
# eth_add_executable is defined in cmake/EthExecutableHelper.cmake # eth_add_executable is defined in cmake/EthExecutableHelper.cmake
eth_add_executable(${EXECUTABLE} eth_add_executable(${EXECUTABLE}
ICON alethzero ICON alethzero
UI_RESOURCES ${PLUGIN_UI} alethzero.icns Main.ui Connect.ui Debugger.ui Transact.ui ExportState.ui GetPassword.ui GasPricing.ui UI_RESOURCES ${PLUGIN_UI} alethzero.icns Main.ui Connect.ui Debugger.ui Transact.ui GetPassword.ui GasPricing.ui
WIN_RESOURCES alethzero.rc WIN_RESOURCES alethzero.rc
) )

6
alethzero/Main.ui

@ -172,7 +172,6 @@
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="loadJS"/> <addaction name="loadJS"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="exportState"/>
</widget> </widget>
<widget class="QMenu" name="menuHelp"> <widget class="QMenu" name="menuHelp">
<property name="title"> <property name="title">
@ -1097,11 +1096,6 @@ font-size: 14pt</string>
<string>&amp;Load Javascript...</string> <string>&amp;Load Javascript...</string>
</property> </property>
</action> </action>
<action name="exportState">
<property name="text">
<string>&amp;Export State...</string>
</property>
</action>
<action name="debugStepBack"> <action name="debugStepBack">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>false</bool>

7
alethzero/MainWin.cpp

@ -73,7 +73,6 @@
#include "DappLoader.h" #include "DappLoader.h"
#include "DappHost.h" #include "DappHost.h"
#include "WebPage.h" #include "WebPage.h"
#include "ExportState.h"
#include "ui_Main.h" #include "ui_Main.h"
#include "ui_GetPassword.h" #include "ui_GetPassword.h"
#include "ui_GasPricing.h" #include "ui_GasPricing.h"
@ -1029,12 +1028,6 @@ void Main::on_exportKey_triggered()
} }
} }
void Main::on_exportState_triggered()
{
ExportStateDialog dialog(this);
dialog.exec();
}
void Main::on_usePrivate_triggered() void Main::on_usePrivate_triggered()
{ {
QString pc; QString pc;

1
alethzero/MainWin.h

@ -162,7 +162,6 @@ private slots:
// Tools // Tools
void on_newTransaction_triggered(); void on_newTransaction_triggered();
void on_loadJS_triggered(); void on_loadJS_triggered();
void on_exportState_triggered();
// Stuff concerning the blocks/transactions/accounts panels // Stuff concerning the blocks/transactions/accounts panels
void on_ourAccounts_itemClicked(QListWidgetItem* _i); void on_ourAccounts_itemClicked(QListWidgetItem* _i);

91
alethzero/ExportState.cpp → alethzero/plugins/blockchain/ExportState.cpp

@ -19,77 +19,86 @@
* @date 2015 * @date 2015
*/ */
#if ETH_FATDB
#include "ExportState.h" #include "ExportState.h"
#include <fstream>
#include <QFileDialog> #include <QFileDialog>
#include <QTextStream> #include <QTextStream>
#include <libethereum/Client.h> #include <libethereum/Client.h>
#include "MainWin.h"
#include "ui_ExportState.h" #include "ui_ExportState.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace az; using namespace az;
using namespace eth; using namespace eth;
ExportStateDialog::ExportStateDialog(Main* _parent): DEV_AZ_NOTE_PLUGIN(ExportStateDialog);
QDialog(_parent),
ui(new Ui::ExportState), ExportStateDialog::ExportStateDialog(MainFace* _m):
m_main(_parent) QDialog(_m),
Plugin(_m, "Export State"),
m_ui(new Ui::ExportState)
{ {
ui->setupUi(this); m_ui->setupUi(this);
connect(ui->close, &QPushButton::clicked, this, &ExportStateDialog::close); connect(m_ui->close, &QPushButton::clicked, this, &ExportStateDialog::close);
connect(ui->accounts, &QListWidget::itemSelectionChanged, this, &ExportStateDialog::generateJSON); connect(m_ui->accounts, &QListWidget::itemSelectionChanged, this, &ExportStateDialog::generateJSON);
connect(ui->contracts, &QListWidget::itemSelectionChanged, this, &ExportStateDialog::generateJSON); connect(m_ui->contracts, &QListWidget::itemSelectionChanged, this, &ExportStateDialog::generateJSON);
fillBlocks(); fillBlocks();
connect(addMenuItem("Export State...", "menuTools", true), SIGNAL(triggered()), SLOT(exec()));
} }
ExportStateDialog::~ExportStateDialog() ExportStateDialog::~ExportStateDialog()
{ {
}
Client* ExportStateDialog::ethereum() const }
void ExportStateDialog::showEvent(QShowEvent*)
{ {
return m_main->ethereum(); m_ui->block->clear();
m_ui->block->clearEditText();
m_ui->accounts->clear();
m_ui->contracts->clear();
fillBlocks();
} }
void ExportStateDialog::on_block_editTextChanged() void ExportStateDialog::on_block_editTextChanged()
{ {
QString text = ui->block->currentText(); QString text = m_ui->block->currentText();
int i = ui->block->count(); int i = m_ui->block->count();
while (i-- >= 0) while (i-- >= 0)
if (ui->block->itemText(i) == text) if (m_ui->block->itemText(i) == text)
return; return;
fillBlocks(); fillBlocks();
} }
void ExportStateDialog::on_block_currentIndexChanged(int _index) void ExportStateDialog::on_block_currentIndexChanged(int _index)
{ {
m_block = ui->block->itemData(_index).toUInt(); m_block = m_ui->block->itemData(_index).toUInt();
fillContracts(); fillContracts();
} }
void ExportStateDialog::fillBlocks() void ExportStateDialog::fillBlocks()
{ {
BlockChain const& bc = ethereum()->blockChain(); BlockChain const& bc = ethereum()->blockChain();
QStringList filters = ui->block->currentText().toLower().split(QRegExp("\\s+"), QString::SkipEmptyParts); QStringList filters = m_ui->block->currentText().toLower().split(QRegExp("\\s+"), QString::SkipEmptyParts);
const unsigned numLastBlocks = 10; const unsigned numLastBlocks = 10;
if (ui->block->count() == 0) if (m_ui->block->count() == 0)
{ {
unsigned i = numLastBlocks; unsigned i = numLastBlocks;
for (auto h = bc.currentHash(); bc.details(h) && i; h = bc.details(h).parent, --i) for (auto h = bc.currentHash(); bc.details(h) && i; h = bc.details(h).parent, --i)
{ {
auto d = bc.details(h); auto d = bc.details(h);
ui->block->addItem(QString("#%1 %2").arg(d.number).arg(h.abridged().c_str()), d.number); m_ui->block->addItem(QString("#%1 %2").arg(d.number).arg(h.abridged().c_str()), d.number);
if (h == bc.genesisHash()) if (h == bc.genesisHash())
break; break;
} }
if (ui->block->currentIndex() < 0) if (m_ui->block->currentIndex() < 0)
ui->block->setCurrentIndex(0); m_ui->block->setCurrentIndex(0);
m_recentBlocks = numLastBlocks - i; m_recentBlocks = numLastBlocks - i;
} }
int i = ui->block->count(); int i = m_ui->block->count();
while (i > 0 && i >= m_recentBlocks) while (i > 0 && i >= m_recentBlocks)
ui->block->removeItem(i--); m_ui->block->removeItem(i--);
h256Hash blocks; h256Hash blocks;
for (QString f: filters) for (QString f: filters)
@ -117,31 +126,31 @@ void ExportStateDialog::fillBlocks()
for (auto const& h: blocks) for (auto const& h: blocks)
{ {
auto d = bc.details(h); auto d = bc.details(h);
ui->block->addItem(QString("#%1 %2").arg(d.number).arg(h.abridged().c_str()), d.number); m_ui->block->addItem(QString("#%1 %2").arg(d.number).arg(h.abridged().c_str()), d.number);
} }
} }
void ExportStateDialog::fillContracts() void ExportStateDialog::fillContracts()
{ {
ui->accounts->clear(); m_ui->accounts->clear();
ui->contracts->clear(); m_ui->contracts->clear();
ui->accounts->setEnabled(true); m_ui->accounts->setEnabled(true);
ui->contracts->setEnabled(true); m_ui->contracts->setEnabled(true);
try try
{ {
for (auto i: ethereum()->addresses(m_block)) for (auto i: ethereum()->addresses(m_block))
{ {
string r = m_main->render(i); string r = main()->render(i);
(new QListWidgetItem(QString("%2: %1 [%3]").arg(formatBalance(ethereum()->balanceAt(i)).c_str()).arg(QString::fromStdString(r)).arg((unsigned)ethereum()->countAt(i)), ethereum()->codeAt(i).empty() ? ui->accounts : ui->contracts)) (new QListWidgetItem(QString("%2: %1 [%3]").arg(formatBalance(ethereum()->balanceAt(i)).c_str()).arg(QString::fromStdString(r)).arg((unsigned)ethereum()->countAt(i)), ethereum()->codeAt(i).empty() ? m_ui->accounts : m_ui->contracts))
->setData(Qt::UserRole, QByteArray((char const*)i.data(), Address::size)); ->setData(Qt::UserRole, QByteArray((char const*)i.data(), Address::size));
} }
} }
catch (InterfaceNotSupported const&) catch (InterfaceNotSupported const&)
{ {
ui->accounts->setEnabled(false); m_ui->accounts->setEnabled(false);
ui->contracts->setEnabled(false); m_ui->contracts->setEnabled(false);
ui->json->setEnabled(false); m_ui->json->setEnabled(false);
ui->json->setText(QString("This feature requires compilation with FATDB support.")); m_ui->json->setText(QString("This feature requires compilation with FATDB support."));
} }
} }
@ -150,14 +159,14 @@ void ExportStateDialog::generateJSON()
std::stringstream json; std::stringstream json;
json << "{\n"; json << "{\n";
std::string prefix; std::string prefix;
for(QListWidgetItem* item: ui->accounts->selectedItems()) for(QListWidgetItem* item: m_ui->accounts->selectedItems())
{ {
auto hba = item->data(Qt::UserRole).toByteArray(); auto hba = item->data(Qt::UserRole).toByteArray();
auto address = Address((byte const*)hba.data(), Address::ConstructFromPointer); auto address = Address((byte const*)hba.data(), Address::ConstructFromPointer);
json << prefix << "\t\"" << toHex(address.ref()) << "\": { \"wei\": \"" << ethereum()->balanceAt(address, m_block) << "\" }"; json << prefix << "\t\"" << toHex(address.ref()) << "\": { \"wei\": \"" << ethereum()->balanceAt(address, m_block) << "\" }";
prefix = ",\n"; prefix = ",\n";
} }
for(QListWidgetItem* item: ui->contracts->selectedItems()) for(QListWidgetItem* item: m_ui->contracts->selectedItems())
{ {
auto hba = item->data(Qt::UserRole).toByteArray(); auto hba = item->data(Qt::UserRole).toByteArray();
auto address = Address((byte const*)hba.data(), Address::ConstructFromPointer); auto address = Address((byte const*)hba.data(), Address::ConstructFromPointer);
@ -181,9 +190,9 @@ void ExportStateDialog::generateJSON()
json << "\n}"; json << "\n}";
json.flush(); json.flush();
ui->json->setEnabled(true); m_ui->json->setEnabled(true);
ui->json->setText(QString::fromStdString(json.str())); m_ui->json->setText(QString::fromStdString(json.str()));
ui->saveButton->setEnabled(true); m_ui->saveButton->setEnabled(true);
} }
void ExportStateDialog::on_saveButton_clicked() void ExportStateDialog::on_saveButton_clicked()
@ -193,5 +202,7 @@ void ExportStateDialog::on_saveButton_clicked()
fn = fn.append(".json"); fn = fn.append(".json");
ofstream file(fn.toStdString()); ofstream file(fn.toStdString());
if (file.is_open()) if (file.is_open())
file << ui->json->toPlainText().toStdString(); file << m_ui->json->toPlainText().toStdString();
} }
#endif //ETH_FATDB

15
alethzero/ExportState.h → alethzero/plugins/blockchain/ExportState.h

@ -21,9 +21,12 @@
#pragma once #pragma once
#if ETH_FATDB
#include <memory> #include <memory>
#include <QDialog> #include <QDialog>
#include <libethcore/Common.h> #include <libethcore/Common.h>
#include "MainFace.h"
namespace Ui { class ExportState; } namespace Ui { class ExportState; }
@ -35,14 +38,13 @@ namespace eth { class Client; }
namespace az namespace az
{ {
class Main;
class ExportStateDialog: public QDialog class ExportStateDialog: public QDialog, public Plugin
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ExportStateDialog(Main* _parent = 0); ExportStateDialog(MainFace* _m);
virtual ~ExportStateDialog(); virtual ~ExportStateDialog();
private slots: private slots:
@ -51,17 +53,18 @@ private slots:
void on_saveButton_clicked(); void on_saveButton_clicked();
private: private:
eth::Client* ethereum() const; void showEvent(QShowEvent* _event) override;
void fillBlocks(); void fillBlocks();
void fillContracts(); void fillContracts();
void generateJSON(); void generateJSON();
private: private:
std::unique_ptr<Ui::ExportState> ui; std::unique_ptr<Ui::ExportState> m_ui;
Main* m_main;
int m_recentBlocks = 0; int m_recentBlocks = 0;
eth::BlockNumber m_block = eth::LatestBlock; eth::BlockNumber m_block = eth::LatestBlock;
}; };
} }
} }
#endif //ETH_FATDB

0
alethzero/ExportState.ui → alethzero/plugins/blockchain/ExportState.ui

Loading…
Cancel
Save