Browse Source

misc changes

cl-refactor
yann300 10 years ago
parent
commit
bce5486554
  1. 51
      mix/ClientModel.cpp
  2. 2
      mix/ClientModel.h
  3. 54
      mix/qml/BlockChain.qml
  4. 9
      mix/qml/ScenarioButton.qml
  5. 88
      mix/qml/ScenarioLoader.qml

51
mix/ClientModel.cpp

@ -211,6 +211,7 @@ QVariantList ClientModel::gasCosts() const
void ClientModel::setupScenario(QVariantMap _scenario) void ClientModel::setupScenario(QVariantMap _scenario)
{ {
WriteGuard(x_queueTransactions);
m_queueTransactions.clear(); m_queueTransactions.clear();
m_running = true; m_running = true;
@ -220,7 +221,6 @@ void ClientModel::setupScenario(QVariantMap _scenario)
m_accounts.clear(); m_accounts.clear();
std::vector<KeyPair> userAccounts; std::vector<KeyPair> userAccounts;
for (auto const& b: stateAccounts) for (auto const& b: stateAccounts)
{ {
QVariantMap account = b.toMap(); QVariantMap account = b.toMap();
@ -239,23 +239,29 @@ void ClientModel::setupScenario(QVariantMap _scenario)
m_accounts[address] = Account(qvariant_cast<QEther*>(account.value("balance"))->toU256Wei(), Account::NormalCreation); m_accounts[address] = Account(qvariant_cast<QEther*>(account.value("balance"))->toU256Wei(), Account::NormalCreation);
} }
m_ethAccounts->setAccounts(userAccounts); m_ethAccounts->setAccounts(userAccounts);
bool trToExecute = false;
for (auto const& b: blocks) for (auto const& b: blocks)
{ {
QVariantList transactions = b.toMap().value("transactions").toList(); QVariantList transactions = b.toMap().value("transactions").toList();
m_queueTransactions.push_back(transactions); m_queueTransactions.push_back(transactions);
trToExecute = transactions.size() > 0;
} }
m_client->resetState(m_accounts, Secret(m_currentScenario.value("miner").toMap().value("secret").toString().toStdString())); m_client->resetState(m_accounts, Secret(m_currentScenario.value("miner").toMap().value("secret").toString().toStdString()));
if (m_queueTransactions.count() > 0 && trToExecute)
connect(this, &ClientModel::newBlock, this, &ClientModel::processNextBlock, Qt::QueuedConnection); {
connect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution, Qt::QueuedConnection); connect(this, &ClientModel::newBlock, this, &ClientModel::processNextTransactions, Qt::QueuedConnection);
connect(this, &ClientModel::runStateChanged, this, &ClientModel::finalizeBlock, Qt::QueuedConnection); connect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution, Qt::QueuedConnection);
processNextBlock(); connect(this, &ClientModel::runStateChanged, this, &ClientModel::finalizeBlock, Qt::QueuedConnection);
processNextTransactions();
}
else
m_running = false;
} }
void ClientModel::stopExecution() void ClientModel::stopExecution()
{ {
disconnect(this, &ClientModel::newBlock, this, &ClientModel::processNextBlock); disconnect(this, &ClientModel::newBlock, this, &ClientModel::processNextTransactions);
disconnect(this, &ClientModel::runStateChanged, this, &ClientModel::finalizeBlock); disconnect(this, &ClientModel::runStateChanged, this, &ClientModel::finalizeBlock);
disconnect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution); disconnect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution);
m_running = false; m_running = false;
@ -263,26 +269,19 @@ void ClientModel::stopExecution()
void ClientModel::finalizeBlock() void ClientModel::finalizeBlock()
{ {
m_queueTransactions.pop_front();// pop last execution group. The last block is never mined (pending block)
if (m_queueTransactions.size() > 0) if (m_queueTransactions.size() > 0)
mine(); mine();
else else
{ {
disconnect(this, &ClientModel::newBlock, this, &ClientModel::processNextBlock); stopExecution();
disconnect(this, &ClientModel::runStateChanged, this, &ClientModel::finalizeBlock);
disconnect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution);
m_running = false;
emit runComplete(); emit runComplete();
} }
} }
void ClientModel::processNextBlock()
{
processNextTransactions();
}
void ClientModel::processNextTransactions() void ClientModel::processNextTransactions()
{ {
WriteGuard(x_queueTransactions);
vector<TransactionSettings> transactionSequence; vector<TransactionSettings> transactionSequence;
for (auto const& t: m_queueTransactions.front()) for (auto const& t: m_queueTransactions.front())
{ {
@ -311,7 +310,6 @@ void ClientModel::processNextTransactions()
transactionSequence.push_back(transactionSettings); transactionSequence.push_back(transactionSettings);
} }
m_queueTransactions.pop_front();
executeSequence(transactionSequence); executeSequence(transactionSequence);
} }
@ -322,18 +320,13 @@ void ClientModel::executeSequence(vector<TransactionSettings> const& _sequence)
qWarning() << "Waiting for current execution to complete"; qWarning() << "Waiting for current execution to complete";
m_runFuture.waitForFinished(); m_runFuture.waitForFinished();
} }
emit runStarted(); emit runStarted();
//emit runStateChanged();
//run sequence //run sequence
m_runFuture = QtConcurrent::run([=]() m_runFuture = QtConcurrent::run([=]()
{ {
try try
{ {
vector<Address> deployedContracts; vector<Address> deployedContracts;
//onStateReset();
m_gasCosts.clear(); m_gasCosts.clear();
for (TransactionSettings const& transaction: _sequence) for (TransactionSettings const& transaction: _sequence)
{ {
@ -397,8 +390,8 @@ void ClientModel::executeSequence(vector<TransactionSettings> const& _sequence)
} }
m_gasCosts.append(m_client->lastExecution().gasUsed); m_gasCosts.append(m_client->lastExecution().gasUsed);
onNewTransaction(); onNewTransaction();
emit runComplete();
} }
emit runComplete();
} }
catch(boost::exception const&) catch(boost::exception const&)
{ {
@ -416,10 +409,16 @@ void ClientModel::executeSequence(vector<TransactionSettings> const& _sequence)
void ClientModel::executeTr(QVariantMap _tr) void ClientModel::executeTr(QVariantMap _tr)
{ {
WriteGuard(x_queueTransactions);
QVariantList trs; QVariantList trs;
trs.push_back(_tr); trs.push_back(_tr);
m_queueTransactions.push_back(trs); m_queueTransactions.push_back(trs);
processNextTransactions(); if (!m_running)
{
m_running = true;
connect(this, &ClientModel::runFailed, this, &ClientModel::stopExecution, Qt::QueuedConnection);
processNextTransactions();
}
} }

2
mix/ClientModel.h

@ -256,7 +256,6 @@ private:
std::pair<QString, int> resolvePair(QString const& _contractId); std::pair<QString, int> resolvePair(QString const& _contractId);
QVariant formatStorageValue(SolidityType const& _type, std::unordered_map<dev::u256, dev::u256> const& _storage, unsigned _offset, dev::u256 const& _slot); QVariant formatStorageValue(SolidityType const& _type, std::unordered_map<dev::u256, dev::u256> const& _storage, unsigned _offset, dev::u256 const& _slot);
void processNextTransactions(); void processNextTransactions();
void processNextBlock();
void finalizeBlock(); void finalizeBlock();
void stopExecution(); void stopExecution();
@ -276,6 +275,7 @@ private:
CodeModel* m_codeModel = nullptr; CodeModel* m_codeModel = nullptr;
QList<QVariantList> m_queueTransactions; QList<QVariantList> m_queueTransactions;
QVariantMap m_currentScenario; QVariantMap m_currentScenario;
mutable boost::shared_mutex x_queueTransactions;
}; };
} }

54
mix/qml/BlockChain.qml

@ -104,7 +104,6 @@ ColumnLayout {
{ {
Layout.preferredHeight: 500 Layout.preferredHeight: 500
Layout.preferredWidth: parent.width Layout.preferredWidth: parent.width
//height: 500
border.color: "#cccccc" border.color: "#cccccc"
border.width: 2 border.width: 2
color: "white" color: "white"
@ -209,9 +208,10 @@ ColumnLayout {
Layout.preferredWidth: parent.width Layout.preferredWidth: parent.width
RowLayout RowLayout
{ {
width: parent.width width: 4 * 100
anchors.top: parent.top anchors.top: parent.top
anchors.topMargin: 10 anchors.topMargin: 10
spacing: 0
ScenarioButton { ScenarioButton {
id: rebuild id: rebuild
text: qsTr("Rebuild") text: qsTr("Rebuild")
@ -219,22 +219,40 @@ ColumnLayout {
{ {
if (ensureNotFuturetime.running) if (ensureNotFuturetime.running)
return; return;
var retBlocks = [];
for (var j = 0; j < model.blocks.length; j++) for (var j = 0; j < model.blocks.length; j++)
{ {
var b = model.blocks[j];
var block = {
hash: b.hash,
number: b.number,
transactions: [],
status: b.status
}
for (var k = 0; k < model.blocks[j].transactions.length; k++) for (var k = 0; k < model.blocks[j].transactions.length; k++)
{ {
if (!blockModel.get(j).transactions.get(k).saveStatus) if (blockModel.get(j).transactions.get(k).saveStatus)
{ {
model.blocks[j].transactions.splice(k, 1) block.transactions.push(model.blocks[j].transactions[k]);
blockModel.removeTransaction(j, k)
if (model.blocks[j].transactions.length === 0)
{
model.blocks.splice(j, 1);
blockModel.removeBlock(j);
}
} }
} }
if (block.transactions.length > 0)
{
retBlocks.push(block)
}
}
if (retBlocks.length === 0)
{
retBlocks.push(projectModel.stateListModel.createEmptyBlock())
}
model.blocks = retBlocks
blockModel.clear()
for (var j = 0; j < model.blocks.length; j++)
{
blockModel.append(model.blocks[j])
} }
ensureNotFuturetime.start() ensureNotFuturetime.start()
clientModel.setupScenario(model); clientModel.setupScenario(model);
} }
@ -280,14 +298,21 @@ ColumnLayout {
return return
if (clientModel.mining || clientModel.running) if (clientModel.mining || clientModel.running)
return return
var lastBlock = model.blocks[model.blocks.length - 1] console.log("lllll");
if (lastBlock.status === "pending") if (model.blocks.length > 0)
{ {
ensureNotFuturetime.start() var lastBlock = model.blocks[model.blocks.length - 1]
clientModel.mine() if (lastBlock.status === "pending")
{
ensureNotFuturetime.start()
clientModel.mine()
}
else
addNewBlock()
} }
else else
addNewBlock() addNewBlock()
} }
function addNewBlock() function addNewBlock()
@ -355,6 +380,7 @@ ColumnLayout {
itemTr.isFunctionCall = itemTr.functionId !== "" itemTr.isFunctionCall = itemTr.functionId !== ""
itemTr.returned = _r.returned itemTr.returned = _r.returned
itemTr.value = QEtherHelper.createEther(_r.value, QEther.Wei) itemTr.value = QEtherHelper.createEther(_r.value, QEther.Wei)
console.log("sender " + _r.sender)
itemTr.sender = _r.sender itemTr.sender = _r.sender
itemTr.recordIndex = _r.recordIndex itemTr.recordIndex = _r.recordIndex
itemTr.logs = _r.logs itemTr.logs = _r.logs

9
mix/qml/ScenarioButton.qml

@ -19,9 +19,12 @@ Rectangle {
Image { Image {
id: debugImage id: debugImage
anchors { anchors {
centerIn: parent left: parent.left
bottomMargin: debugImg.pressed ? 0 : 1; right: parent.right
topMargin: debugImg.pressed ? 1 : 0; top: parent.top
bottom: parent.bottom
bottomMargin: debugImg.pressed ? 0 : 2;
topMargin: debugImg.pressed ? 2 : 0;
} }
source: sourceImg source: sourceImg
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit

88
mix/qml/ScenarioLoader.qml

@ -14,7 +14,7 @@ RowLayout
signal saved(variant scenario) signal saved(variant scenario)
signal duplicated(variant scenario) signal duplicated(variant scenario)
signal loaded(variant scenario) signal loaded(variant scenario)
spacing: 0
function init() function init()
{ {
scenarioList.load() scenarioList.load()
@ -38,51 +38,57 @@ RowLayout
} }
} }
ScenarioButton { Row
id: restoreScenario {
Layout.preferredWidth: 100 Layout.preferredWidth: 100 * 3
Layout.preferredHeight: 30 Layout.preferredHeight: 30
buttonShortcut: "" spacing: 0
sourceImg: "qrc:/qml/img/restoreIcon@2x.png" ScenarioButton {
onClicked: { id: restoreScenario
restore() width: 100
} height: 30
text: qsTr("Restore") buttonShortcut: ""
function restore() sourceImg: "qrc:/qml/img/restoreIcon@2x.png"
{ onClicked: {
var state = projectModel.stateListModel.reloadStateFromFromProject(scenarioList.currentIndex) restore()
restored(state) }
loaded(state) text: qsTr("Restore")
function restore()
{
var state = projectModel.stateListModel.reloadStateFromFromProject(scenarioList.currentIndex)
restored(state)
loaded(state)
}
} }
}
ScenarioButton { ScenarioButton {
id: saveScenario id: saveScenario
text: qsTr("Save") text: qsTr("Save")
onClicked: { onClicked: {
projectModel.saveProjectFile() projectModel.saveProjectFile()
saved(state) saved(state)
}
width: 100
height: 30
buttonShortcut: ""
sourceImg: "qrc:/qml/img/saveIcon@2x.png"
} }
Layout.preferredWidth: 100
Layout.preferredHeight: 30
buttonShortcut: ""
sourceImg: "qrc:/qml/img/saveIcon@2x.png"
}
ScenarioButton ScenarioButton
{ {
id: duplicateScenario id: duplicateScenario
text: qsTr("Duplicate") text: qsTr("Duplicate")
onClicked: { onClicked: {
var state = JSON.parse(JSON.stringify(projectModel.stateListModel.getState(scenarioList.currentIndex))) var state = JSON.parse(JSON.stringify(projectModel.stateListModel.getState(scenarioList.currentIndex)))
state.title = qsTr("Copy of ") + state.title; state.title = qsTr("Copy of ") + state.title;
projectModel.stateListModel.appendState(state) projectModel.stateListModel.appendState(state)
projectModel.stateListModel.save() projectModel.stateListModel.save()
duplicated(state) duplicated(state)
}
width: 100
height: 30
buttonShortcut: ""
sourceImg: "qrc:/qml/img/duplicateIcon@2x.png"
} }
Layout.preferredWidth: 100
Layout.preferredHeight: 30
buttonShortcut: ""
sourceImg: "qrc:/qml/img/duplicateIcon@2x.png"
} }
} }

Loading…
Cancel
Save