From e4c2c654bc148babf6e284ac9502ec3fb5810991 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 3 Mar 2015 12:46:09 +0100 Subject: [PATCH] pass Secret to the executing transaction. --- mix/ClientModel.cpp | 10 ++++-- mix/ClientModel.h | 4 +-- mix/MixClient.cpp | 16 +++------ mix/qml/StateDialog.qml | 66 +++++++++++++++++++---------------- mix/qml/StateListModel.qml | 18 ++++++---- mix/qml/TransactionDialog.qml | 33 ++++++++++++++++++ 6 files changed, 96 insertions(+), 51 deletions(-) diff --git a/mix/ClientModel.cpp b/mix/ClientModel.cpp index ef6963f32..fedb549c8 100644 --- a/mix/ClientModel.cpp +++ b/mix/ClientModel.cpp @@ -38,6 +38,7 @@ #include "QEther.h" #include "Web3Server.h" #include "ClientModel.h" +#include "MixClient.h" using namespace dev; using namespace dev::eth; @@ -47,6 +48,8 @@ namespace dev namespace mix { +const Secret c_defaultUserAccountSecret = Secret("cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169074"); + class RpcConnector: public jsonrpc::AbstractServerConnector { public: @@ -157,10 +160,11 @@ void ClientModel::debugDeployment() void ClientModel::setupState(QVariantMap _state) { - QVariantList balances = _state.value("balances").toList(); + QVariantList balances = _state.value("accounts").toList(); QVariantList transactions = _state.value("transactions").toList(); std::map accounts; + accounts.insert(std::make_pair(c_defaultUserAccountSecret, 10000 * ether)); //Default account, used to deploy config contracts. for (auto const& b: balances) { QVariantMap address = b.toMap(); @@ -186,6 +190,7 @@ void ClientModel::setupState(QVariantMap _state) transactionSettings.gasPrice = 10000000000000; transactionSettings.gas = 125000; transactionSettings.value = 0; + transactionSettings.sender = c_defaultUserAccountSecret; transactionSequence.push_back(transactionSettings); } else @@ -193,7 +198,8 @@ void ClientModel::setupState(QVariantMap _state) if (contractId.isEmpty() && m_context->codeModel()->hasContract()) //TODO: This is to support old project files, remove later contractId = m_context->codeModel()->contracts().keys()[0]; QVariantList qParams = transaction.value("qType").toList(); - TransactionSettings transactionSettings(contractId, functionId, value, gas, gasPrice); + QString sender = transaction.value("sender").toString(); + TransactionSettings transactionSettings(contractId, functionId, value, gas, gasPrice, Secret(sender.toStdString())); for (QVariant const& variant: qParams) { diff --git a/mix/ClientModel.h b/mix/ClientModel.h index f6863314c..2a3e041c1 100644 --- a/mix/ClientModel.h +++ b/mix/ClientModel.h @@ -47,8 +47,8 @@ class QVariableDefinition; struct TransactionSettings { TransactionSettings() {} - TransactionSettings(QString const& _contractId, QString const& _functionId, u256 _value, u256 _gas, u256 _gasPrice): - contractId(_contractId), functionId(_functionId), value(_value), gas(_gas), gasPrice(_gasPrice) {} + TransactionSettings(QString const& _contractId, QString const& _functionId, u256 _value, u256 _gas, u256 _gasPrice, Secret _sender): + contractId(_contractId), functionId(_functionId), value(_value), gas(_gas), gasPrice(_gasPrice), sender(_sender) {} TransactionSettings(QString const& _stdContractName, QString const& _stdContractUrl): contractId(_stdContractName), stdContractUrl(_stdContractUrl) {} diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index d7559e9b8..c0b165189 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -21,6 +21,7 @@ */ #include +#include #include #include #include @@ -40,7 +41,6 @@ namespace dev namespace mix { -const Secret c_defaultUserAccountSecret = Secret("cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169074"); const u256 c_mixGenesisDifficulty = (u256) 1 << 4; class MixBlockChain: public dev::eth::BlockChain @@ -83,19 +83,13 @@ void MixClient::resetState(std::map _accounts) accountState.init(); std::map genesisState; - if (_accounts.size() > 0) - for (auto account: _accounts) - { - KeyPair a = KeyPair(account.first); - m_userAccounts.push_back(a); - genesisState.insert(std::make_pair(a.address(), Account(account.second, Account::NormalCreation))); - } - else + for (auto account: _accounts) { - KeyPair a = KeyPair(c_defaultUserAccountSecret); + KeyPair a = KeyPair(account.first); m_userAccounts.push_back(a); - genesisState.insert(std::make_pair(a.address() , Account(u256(10000 * ether), Account::NormalCreation))); + genesisState.insert(std::make_pair(a.address(), Account(account.second, Account::NormalCreation))); } + dev::eth::commit(genesisState, static_cast(m_stateDB), accountState); h256 stateRoot = accountState.root(); m_bc.reset(); diff --git a/mix/qml/StateDialog.qml b/mix/qml/StateDialog.qml index 5f9100e9a..38a59a8a7 100644 --- a/mix/qml/StateDialog.qml +++ b/mix/qml/StateDialog.qml @@ -12,7 +12,7 @@ Window { id: modalStateDialog modality: Qt.ApplicationModal - width: 520 + width: 555 height: 480 title: qsTr("Edit State") visible: false @@ -41,8 +41,7 @@ Window { accountsModel.clear(); stateAccounts = []; for (var k = 0; k < item.accounts.length; k++) - { - accountsModel.append(item.accounts[k]); + { accountsModel.append(item.accounts[k]); stateAccounts.push(item.accounts[k]); } @@ -100,23 +99,35 @@ Window { RowLayout { Layout.fillWidth: true - DefaultLabel { - Layout.preferredWidth: 75 - text: qsTr("Accounts") - } - Button + Rectangle { - iconSource: "qrc:/qml/img/plus.png" - action: newAccountAction - width: 10 - height: 10 - } + Layout.preferredWidth: 75 + DefaultLabel { + id: accountsLabel + Layout.preferredWidth: 75 + text: qsTr("Accounts") + } - Action { - id: newAccountAction - tooltip: qsTr("Add new Account") - onTriggered: accountsModel.newAccount("10000", QEther.Ether); + Button + { + anchors.top: accountsLabel.bottom + anchors.topMargin: 10 + iconSource: "qrc:/qml/img/plus.png" + action: newAccountAction + } + + Action { + id: newAccountAction + tooltip: qsTr("Add new Account") + onTriggered: + { + var account = stateListModel.newAccount("1000000", QEther.Ether); + stateAccounts.push(account); + accountsModel.append(account); + + } + } } TableView @@ -124,9 +135,9 @@ Window { Layout.fillWidth: true model: accountsModel TableViewColumn { - role: "secret" - title: qsTr("Secret") - width: 100 + role: "name" + title: qsTr("Name") + width: 120 delegate: Item { Rectangle { @@ -134,7 +145,7 @@ Window { DefaultLabel { anchors.verticalCenter: parent.verticalCenter text: { - return styleData.value.substring(0, 5) + '...'; + return styleData.value } } } @@ -156,8 +167,8 @@ Window { } rowDelegate: Rectangle { - color: "transparent" - height: 40; + color: styleData.alternate ? "transparent" : "#f0f0f0" + height: 30; } } } @@ -259,13 +270,6 @@ Window { ListModel { id: accountsModel - function newAccount(_balance, _unit) - { - var secret = clientModel.newAddress(); - accountsModel.append({ secret: secret, balance: QEtherHelper.createEther(_balance, _unit) }); - stateAccounts.push({ secret: secret, balance: QEtherHelper.createEther(_balance, _unit) }); - } - function removeAccount(_i) { accountsModel.remove(_i); @@ -277,6 +281,7 @@ Window { id: transactionsModel function editTransaction(index) { + transactionDialog.stateAccounts = stateAccounts; transactionDialog.open(index, transactionsModel.get(index)); } @@ -286,6 +291,7 @@ Window { // https://bugreports.qt-project.org/browse/QTBUG-41327 // Second call to signal handler would just edit the item that was just created, no harm done var item = TransactionHelper.defaultTransaction(); + transactionDialog.stateAccounts = stateAccounts; transactionDialog.open(transactionsModel.count, item); } diff --git a/mix/qml/StateListModel.qml b/mix/qml/StateListModel.qml index e8f4a6b5e..b53871211 100644 --- a/mix/qml/StateListModel.qml +++ b/mix/qml/StateListModel.qml @@ -23,6 +23,7 @@ Item { function fromPlainAccountItem(t) { return { + name: t.name, secret: t.secret, balance: QEtherHelper.createEther(t.balance.value, t.balance.unit) }; @@ -91,6 +92,7 @@ Item { function toPlainAccountItem(t) { return { + name: t.name, secret: t.secret, balance: { value: t.balance.value, @@ -171,7 +173,6 @@ Item { ListModel { id: stateListModel - property string defaultSecret: "cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169075" property int defaultStateIndex: 0 signal defaultStateChanged; signal stateListModelReady; @@ -186,18 +187,23 @@ Item { }; } + function newAccount(_balance, _unit) + { + var secret = clientModel.newAddress(); + var name = qsTr("Account") + " - " + secret.substring(0, 5); + return { name: name, secret: secret, balance: QEtherHelper.createEther(_balance, _unit) }; + } + function createDefaultState() { - var ether = QEtherHelper.createEther("1000000", QEther.Ether); var item = { title: "", transactions: [], accounts: [] }; - item.accounts.push({ - secret: defaultSecret, - balance: ether - }); + item.accounts.push(newAccount("1000000", QEther.Ether)); + + console.log(JSON.stringify(item.accounts)); //add all stdc contracts for (var i = 0; i < contractLibrary.model.count; i++) { diff --git a/mix/qml/TransactionDialog.qml b/mix/qml/TransactionDialog.qml index 434f8a850..997f78779 100644 --- a/mix/qml/TransactionDialog.qml +++ b/mix/qml/TransactionDialog.qml @@ -25,6 +25,7 @@ Window { property var itemParams; property bool useTransactionDefaultValue: false property var qType; + property alias stateAccounts: senderComboBox.model signal accepted; @@ -44,6 +45,8 @@ Window { rowFunction.visible = true; itemParams = item.parameters !== undefined ? item.parameters : {}; + if (item.sender) + senderComboBox.select(item.sender); contractsModel.clear(); var contractIndex = -1; @@ -190,6 +193,7 @@ Window { item.functionId = transactionDialog.functionId; } + item.sender = senderComboBox.model[senderComboBox.currentIndex].secret; var orderedQType = []; for (var p = 0; p < transactionDialog.transactionParams.count; p++) { var parameter = transactionDialog.transactionParams.get(p); @@ -210,6 +214,35 @@ Window { id: dialogContent anchors.top: parent.top spacing: 10 + RowLayout + { + id: rowSender + Layout.fillWidth: true + height: 150 + DefaultLabel { + Layout.preferredWidth: 75 + text: qsTr("Sender") + } + ComboBox { + + function select(secret) + { + for (var i in model) + if (model[i].secret === secret) + { + currentIndex = i; + break; + } + } + + id: senderComboBox + Layout.preferredWidth: 350 + currentIndex: 0 + textRole: "name" + editable: false + } + } + RowLayout { id: rowContract