Browse Source

pass Secret to the executing transaction.

cl-refactor
yann300 10 years ago
parent
commit
e4c2c654bc
  1. 10
      mix/ClientModel.cpp
  2. 4
      mix/ClientModel.h
  3. 16
      mix/MixClient.cpp
  4. 66
      mix/qml/StateDialog.qml
  5. 18
      mix/qml/StateListModel.qml
  6. 33
      mix/qml/TransactionDialog.qml

10
mix/ClientModel.cpp

@ -38,6 +38,7 @@
#include "QEther.h" #include "QEther.h"
#include "Web3Server.h" #include "Web3Server.h"
#include "ClientModel.h" #include "ClientModel.h"
#include "MixClient.h"
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
@ -47,6 +48,8 @@ namespace dev
namespace mix namespace mix
{ {
const Secret c_defaultUserAccountSecret = Secret("cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169074");
class RpcConnector: public jsonrpc::AbstractServerConnector class RpcConnector: public jsonrpc::AbstractServerConnector
{ {
public: public:
@ -157,10 +160,11 @@ void ClientModel::debugDeployment()
void ClientModel::setupState(QVariantMap _state) void ClientModel::setupState(QVariantMap _state)
{ {
QVariantList balances = _state.value("balances").toList(); QVariantList balances = _state.value("accounts").toList();
QVariantList transactions = _state.value("transactions").toList(); QVariantList transactions = _state.value("transactions").toList();
std::map<Secret, u256> accounts; std::map<Secret, u256> accounts;
accounts.insert(std::make_pair(c_defaultUserAccountSecret, 10000 * ether)); //Default account, used to deploy config contracts.
for (auto const& b: balances) for (auto const& b: balances)
{ {
QVariantMap address = b.toMap(); QVariantMap address = b.toMap();
@ -186,6 +190,7 @@ void ClientModel::setupState(QVariantMap _state)
transactionSettings.gasPrice = 10000000000000; transactionSettings.gasPrice = 10000000000000;
transactionSettings.gas = 125000; transactionSettings.gas = 125000;
transactionSettings.value = 0; transactionSettings.value = 0;
transactionSettings.sender = c_defaultUserAccountSecret;
transactionSequence.push_back(transactionSettings); transactionSequence.push_back(transactionSettings);
} }
else 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 if (contractId.isEmpty() && m_context->codeModel()->hasContract()) //TODO: This is to support old project files, remove later
contractId = m_context->codeModel()->contracts().keys()[0]; contractId = m_context->codeModel()->contracts().keys()[0];
QVariantList qParams = transaction.value("qType").toList(); 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) for (QVariant const& variant: qParams)
{ {

4
mix/ClientModel.h

@ -47,8 +47,8 @@ class QVariableDefinition;
struct TransactionSettings struct TransactionSettings
{ {
TransactionSettings() {} TransactionSettings() {}
TransactionSettings(QString const& _contractId, QString const& _functionId, u256 _value, u256 _gas, u256 _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) {} contractId(_contractId), functionId(_functionId), value(_value), gas(_gas), gasPrice(_gasPrice), sender(_sender) {}
TransactionSettings(QString const& _stdContractName, QString const& _stdContractUrl): TransactionSettings(QString const& _stdContractName, QString const& _stdContractUrl):
contractId(_stdContractName), stdContractUrl(_stdContractUrl) {} contractId(_stdContractName), stdContractUrl(_stdContractUrl) {}

16
mix/MixClient.cpp

@ -21,6 +21,7 @@
*/ */
#include <vector> #include <vector>
#include <QDebug>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libethereum/CanonBlockChain.h> #include <libethereum/CanonBlockChain.h>
#include <libethereum/Transaction.h> #include <libethereum/Transaction.h>
@ -40,7 +41,6 @@ namespace dev
namespace mix namespace mix
{ {
const Secret c_defaultUserAccountSecret = Secret("cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169074");
const u256 c_mixGenesisDifficulty = (u256) 1 << 4; const u256 c_mixGenesisDifficulty = (u256) 1 << 4;
class MixBlockChain: public dev::eth::BlockChain class MixBlockChain: public dev::eth::BlockChain
@ -83,19 +83,13 @@ void MixClient::resetState(std::map<Secret, u256> _accounts)
accountState.init(); accountState.init();
std::map<Address, Account> genesisState; std::map<Address, Account> genesisState;
if (_accounts.size() > 0) for (auto account: _accounts)
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
{ {
KeyPair a = KeyPair(c_defaultUserAccountSecret); KeyPair a = KeyPair(account.first);
m_userAccounts.push_back(a); 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<MemoryDB&>(m_stateDB), accountState); dev::eth::commit(genesisState, static_cast<MemoryDB&>(m_stateDB), accountState);
h256 stateRoot = accountState.root(); h256 stateRoot = accountState.root();
m_bc.reset(); m_bc.reset();

66
mix/qml/StateDialog.qml

@ -12,7 +12,7 @@ Window {
id: modalStateDialog id: modalStateDialog
modality: Qt.ApplicationModal modality: Qt.ApplicationModal
width: 520 width: 555
height: 480 height: 480
title: qsTr("Edit State") title: qsTr("Edit State")
visible: false visible: false
@ -41,8 +41,7 @@ Window {
accountsModel.clear(); accountsModel.clear();
stateAccounts = []; stateAccounts = [];
for (var k = 0; k < item.accounts.length; k++) for (var k = 0; k < item.accounts.length; k++)
{ { accountsModel.append(item.accounts[k]);
accountsModel.append(item.accounts[k]);
stateAccounts.push(item.accounts[k]); stateAccounts.push(item.accounts[k]);
} }
@ -100,23 +99,35 @@ Window {
RowLayout RowLayout
{ {
Layout.fillWidth: true Layout.fillWidth: true
DefaultLabel {
Layout.preferredWidth: 75
text: qsTr("Accounts")
}
Button Rectangle
{ {
iconSource: "qrc:/qml/img/plus.png" Layout.preferredWidth: 75
action: newAccountAction DefaultLabel {
width: 10 id: accountsLabel
height: 10 Layout.preferredWidth: 75
} text: qsTr("Accounts")
}
Action { Button
id: newAccountAction {
tooltip: qsTr("Add new Account") anchors.top: accountsLabel.bottom
onTriggered: accountsModel.newAccount("10000", QEther.Ether); 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 TableView
@ -124,9 +135,9 @@ Window {
Layout.fillWidth: true Layout.fillWidth: true
model: accountsModel model: accountsModel
TableViewColumn { TableViewColumn {
role: "secret" role: "name"
title: qsTr("Secret") title: qsTr("Name")
width: 100 width: 120
delegate: Item { delegate: Item {
Rectangle Rectangle
{ {
@ -134,7 +145,7 @@ Window {
DefaultLabel { DefaultLabel {
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
text: { text: {
return styleData.value.substring(0, 5) + '...'; return styleData.value
} }
} }
} }
@ -156,8 +167,8 @@ Window {
} }
rowDelegate: rowDelegate:
Rectangle { Rectangle {
color: "transparent" color: styleData.alternate ? "transparent" : "#f0f0f0"
height: 40; height: 30;
} }
} }
} }
@ -259,13 +270,6 @@ Window {
ListModel { ListModel {
id: accountsModel 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) function removeAccount(_i)
{ {
accountsModel.remove(_i); accountsModel.remove(_i);
@ -277,6 +281,7 @@ Window {
id: transactionsModel id: transactionsModel
function editTransaction(index) { function editTransaction(index) {
transactionDialog.stateAccounts = stateAccounts;
transactionDialog.open(index, transactionsModel.get(index)); transactionDialog.open(index, transactionsModel.get(index));
} }
@ -286,6 +291,7 @@ Window {
// https://bugreports.qt-project.org/browse/QTBUG-41327 // 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 // Second call to signal handler would just edit the item that was just created, no harm done
var item = TransactionHelper.defaultTransaction(); var item = TransactionHelper.defaultTransaction();
transactionDialog.stateAccounts = stateAccounts;
transactionDialog.open(transactionsModel.count, item); transactionDialog.open(transactionsModel.count, item);
} }

18
mix/qml/StateListModel.qml

@ -23,6 +23,7 @@ Item {
function fromPlainAccountItem(t) function fromPlainAccountItem(t)
{ {
return { return {
name: t.name,
secret: t.secret, secret: t.secret,
balance: QEtherHelper.createEther(t.balance.value, t.balance.unit) balance: QEtherHelper.createEther(t.balance.value, t.balance.unit)
}; };
@ -91,6 +92,7 @@ Item {
function toPlainAccountItem(t) function toPlainAccountItem(t)
{ {
return { return {
name: t.name,
secret: t.secret, secret: t.secret,
balance: { balance: {
value: t.balance.value, value: t.balance.value,
@ -171,7 +173,6 @@ Item {
ListModel { ListModel {
id: stateListModel id: stateListModel
property string defaultSecret: "cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169075"
property int defaultStateIndex: 0 property int defaultStateIndex: 0
signal defaultStateChanged; signal defaultStateChanged;
signal stateListModelReady; 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() { function createDefaultState() {
var ether = QEtherHelper.createEther("1000000", QEther.Ether);
var item = { var item = {
title: "", title: "",
transactions: [], transactions: [],
accounts: [] accounts: []
}; };
item.accounts.push({ item.accounts.push(newAccount("1000000", QEther.Ether));
secret: defaultSecret,
balance: ether console.log(JSON.stringify(item.accounts));
});
//add all stdc contracts //add all stdc contracts
for (var i = 0; i < contractLibrary.model.count; i++) { for (var i = 0; i < contractLibrary.model.count; i++) {

33
mix/qml/TransactionDialog.qml

@ -25,6 +25,7 @@ Window {
property var itemParams; property var itemParams;
property bool useTransactionDefaultValue: false property bool useTransactionDefaultValue: false
property var qType; property var qType;
property alias stateAccounts: senderComboBox.model
signal accepted; signal accepted;
@ -44,6 +45,8 @@ Window {
rowFunction.visible = true; rowFunction.visible = true;
itemParams = item.parameters !== undefined ? item.parameters : {}; itemParams = item.parameters !== undefined ? item.parameters : {};
if (item.sender)
senderComboBox.select(item.sender);
contractsModel.clear(); contractsModel.clear();
var contractIndex = -1; var contractIndex = -1;
@ -190,6 +193,7 @@ Window {
item.functionId = transactionDialog.functionId; item.functionId = transactionDialog.functionId;
} }
item.sender = senderComboBox.model[senderComboBox.currentIndex].secret;
var orderedQType = []; var orderedQType = [];
for (var p = 0; p < transactionDialog.transactionParams.count; p++) { for (var p = 0; p < transactionDialog.transactionParams.count; p++) {
var parameter = transactionDialog.transactionParams.get(p); var parameter = transactionDialog.transactionParams.get(p);
@ -210,6 +214,35 @@ Window {
id: dialogContent id: dialogContent
anchors.top: parent.top anchors.top: parent.top
spacing: 10 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 RowLayout
{ {
id: rowContract id: rowContract

Loading…
Cancel
Save