Skip to content

Commit b271a9d

Browse files
yann300yann300
authored andcommitted
Manage solidity types:
- uint<N>, uint - int<N>, int - hash<N>, hash, address - bool - string<N>
1 parent 2c06e07 commit b271a9d

21 files changed

Lines changed: 327 additions & 238 deletions

libdevcore/CommonJS.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,32 @@ bytes padded(bytes _b, unsigned _l)
4545
return asBytes(asString(_b).substr(_b.size() - std::max(_l, _l)));
4646
}
4747

48+
bytes paddedRight(bytes _b, unsigned _l)
49+
{
50+
while (_b.size() < _l)
51+
_b.insert(_b.end(), 0);
52+
return asBytes(asString(_b).substr(_b.size() - std::max(_l, _l)));
53+
}
54+
4855
bytes unpadded(bytes _b)
4956
{
5057
auto p = asString(_b).find_last_not_of((char)0);
5158
_b.resize(p == std::string::npos ? 0 : (p + 1));
5259
return _b;
5360
}
5461

62+
std::string unpadRight(std::string _b)
63+
{
64+
while (true)
65+
{
66+
auto p = _b.find_last_of("0");
67+
if (p == _b.size() - 1)
68+
_b = _b.substr(0, _b.size() - 1);
69+
else
70+
return _b;
71+
}
72+
}
73+
5574
std::string unpadLeft(std::string _b)
5675
{
5776
auto p = _b.find_first_not_of('0');

libdevcore/CommonJS.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ inline std::string toJS(dev::bytes const& _n)
5050
bytes jsToBytes(std::string const& _s);
5151
/// Add '0' on the head of _b until _l.
5252
bytes padded(bytes _b, unsigned _l);
53+
/// Add '0' on the queue of _b until _l.
54+
bytes paddedRight(bytes _b, unsigned _l);
55+
/// Remove all trailing '0'
56+
std::string unpadRight(std::string _b);
5357
/// Removing all trailing '0'. Returns empty array if input contains only '0' char.
5458
bytes unpadded(bytes _s);
5559
/// Remove all '0' on the head of _s. Returns 0 if _s contains only '0'.

mix/AppContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void AppContext::load()
6666
qmlRegisterType<QRealType>("org.ethereum.qml.QRealType", 1, 0, "QRealType");
6767
qmlRegisterType<QStringType>("org.ethereum.qml.QStringType", 1, 0, "QStringType");
6868
qmlRegisterType<QHashType>("org.ethereum.qml.QHashType", 1, 0, "QHashType");
69+
qmlRegisterType<QBoolType>("org.ethereum.qml.QBoolType", 1, 0, "QBoolType");
6970
QQmlComponent projectModelComponent(m_applicationEngine, QUrl("qrc:/qml/ProjectModel.qml"));
7071
QObject* projectModel = projectModelComponent.create();
7172
if (projectModelComponent.isError())

mix/ClientModel.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,11 @@ void ClientModel::debugState(QVariantMap _state)
119119
u256 gas = (qvariant_cast<QEther*>(transaction.value("gas")))->toU256Wei();
120120
u256 value = (qvariant_cast<QEther*>(transaction.value("value")))->toU256Wei();
121121
u256 gasPrice = (qvariant_cast<QEther*>(transaction.value("gasPrice")))->toU256Wei();
122-
QVariantMap params = transaction.value("parameters").toMap();
123122
TransactionSettings transactionSettings(functionId, value, gas, gasPrice);
124-
125-
for (auto p = params.cbegin(); p != params.cend(); ++p)
123+
QVariantList qParams = transaction.value("qType").toList();
124+
for (QVariant const& variant: qParams)
126125
{
127-
QVariableDefinition* param = qvariant_cast<QVariableDefinition*>(p.value());
126+
QVariableDefinition* param = qvariant_cast<QVariableDefinition*>(variant);
128127
transactionSettings.parameterValues.push_back(param);
129128
}
130129

@@ -173,7 +172,12 @@ void ClientModel::executeSequence(std::vector<TransactionSettings> const& _seque
173172

174173
c.encode(f);
175174
for (int p = 0; p < t.parameterValues.size(); p++)
175+
{
176+
qDebug() << " encode input parameters : " + t.parameterValues.at(p)->declaration()->type();
177+
qDebug() << t.parameterValues.at(p)->declaration()->type();
178+
qDebug() << t.parameterValues.at(p)->value();
176179
c.push(t.parameterValues.at(p)->encodeValue());
180+
}
177181
transactonData.emplace_back(c.encodedData());
178182
}
179183

mix/CodeHighlighter.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
This file is part of cpp-ethereum.
2+
This file is part of cpp-ethereum.
33
4-
cpp-ethereum is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 3 of the License, or
7-
(at your option) any later version.
4+
cpp-ethereum is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
88
9-
cpp-ethereum is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
9+
cpp-ethereum is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
1313
14-
You should have received a copy of the GNU General Public License
15-
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
14+
You should have received a copy of the GNU General Public License
15+
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717
/** @file CodeHighlighter.cpp
1818
* @author Arkadiy Paronyan arkadiy@ethdev.com
@@ -102,7 +102,8 @@ void CodeHighlighter::processAST(dev::solidity::ASTNode const& _ast)
102102
void CodeHighlighter::processError(dev::Exception const& _exception)
103103
{
104104
Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
105-
m_formats.push_back(FormatRange(CodeHighlighterSettings::CompilationError, *location));
105+
if (location)
106+
m_formats.push_back(FormatRange(CodeHighlighterSettings::CompilationError, *location));
106107
}
107108

108109
void CodeHighlighter::processComments(std::string const& _source)

mix/ContractCallDataEncoder.cpp

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using namespace dev::mix;
3535

3636
bytes ContractCallDataEncoder::encodedData()
3737
{
38+
qDebug() << " encoded data " << QString::fromStdString(toJS(m_encodedData));
3839
return m_encodedData;
3940
}
4041

@@ -62,96 +63,16 @@ QList<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QVariableDecla
6263
def = new QIntType(dec, QString());
6364
else if (dec->type().contains("real"))
6465
def = new QRealType(dec, QString());
66+
else if (dec->type().contains("bool"))
67+
def = new QBoolType(dec, QString());
6568
else if (dec->type().contains("string") || dec->type().contains("text"))
6669
def = new QStringType(dec, QString());
6770
else if (dec->type().contains("hash") || dec->type().contains("address"))
6871
def = new QHashType(dec, QString());
69-
7072
def->decodeValue(returnValue);
7173
r.push_back(def);
72-
returnValue = returnValue.substr(def->length(), returnValue.length() - 1);
73-
74-
/*QStringList tLength = typeLength(dec->type());
75-
76-
QRegExp intTest("(uint|int|hash|address)");
77-
QRegExp stringTest("(string|text)");
78-
QRegExp realTest("(real|ureal)");
79-
if (intTest.indexIn(dec->type()) != -1)
80-
{
81-
std::string rawParam = returnValue.substr(0, (tLength.first().toInt() / 8) * 2);
82-
QString value = resolveNumber(QString::fromStdString(rawParam));
83-
r.append(new QVariableDefinition(dec, value));
84-
returnValue = returnValue.substr(rawParam.length(), returnValue.length() - 1);
85-
}
86-
else if (dec->type() == "bool")
87-
{
88-
std::string rawParam = returnValue.substr(0, 2);
89-
std::string unpadded = unpadLeft(rawParam);
90-
r.append(new QVariableDefinition(dec, QString::fromStdString(unpadded)));
91-
returnValue = returnValue.substr(rawParam.length(), returnValue.length() - 1);
92-
}
93-
else if (stringTest.indexIn(dec->type()) != -1)
94-
{
95-
if (tLength.length() == 0)
96-
{
97-
QString strLength = QString::fromStdString(returnValue.substr(0, 2));
98-
returnValue = returnValue.substr(2, returnValue.length() - 1);
99-
QString strValue = QString::fromStdString(returnValue.substr(0, strLength.toInt()));
100-
r.append(new QVariableDefinition(dec, strValue));
101-
returnValue = returnValue.substr(strValue.length(), returnValue.length() - 1);
102-
}
103-
else
104-
{
105-
std::string rawParam = returnValue.substr(0, (tLength.first().toInt() / 8) * 2);
106-
r.append(new QVariableDefinition(dec, QString::fromStdString(rawParam)));
107-
returnValue = returnValue.substr(rawParam.length(), returnValue.length() - 1);
108-
}
109-
}
110-
else if (realTest.indexIn(dec->type()) != -1)
111-
{
112-
QString value;
113-
for (QString str: tLength)
114-
{
115-
std::string rawParam = returnValue.substr(0, (str.toInt() / 8) * 2);
116-
QString value = resolveNumber(QString::fromStdString(rawParam));
117-
value += value + "x";
118-
returnValue = returnValue.substr(rawParam.length(), returnValue.length() - 1);
119-
}
120-
r.append(new QVariableDefinition(dec, value));
121-
}*/
74+
returnValue = returnValue.substr(32 * 2, returnValue.length() - 1);
75+
qDebug() << "decoded return value : " << dec->type() << " " << def->value();
12276
}
12377
return r;
12478
}
125-
126-
QString ContractCallDataEncoder::resolveNumber(QString const& _rawParam)
127-
{
128-
std::string unPadded = unpadLeft(_rawParam.toStdString());
129-
int x = std::stol(unPadded, nullptr, 16);
130-
std::stringstream ss;
131-
ss << std::dec << x;
132-
return QString::fromStdString(ss.str());
133-
}
134-
135-
QString ContractCallDataEncoder::convertToReadable(std::string _v, QVariableDeclaration* _dec)
136-
{
137-
if (_dec->type().indexOf("int") != -1)
138-
return convertToInt(_v);
139-
else if (_dec->type().indexOf("bool") != -1)
140-
return convertToBool(_v);
141-
else
142-
return QString::fromStdString(_v);
143-
}
144-
145-
QString ContractCallDataEncoder::convertToBool(std::string _v)
146-
{
147-
return _v == "1" ? "true" : "false";
148-
}
149-
150-
QString ContractCallDataEncoder::convertToInt(std::string _v)
151-
{
152-
//TO DO to be improve to manage all int, uint size (128, 256, ...) in ethereum QML types task #612.
153-
int x = std::stol(_v, nullptr, 16);
154-
std::stringstream ss;
155-
ss << std::dec << x;
156-
return QString::fromStdString(ss.str());
157-
}

mix/ContractCallDataEncoder.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,17 @@ class ContractCallDataEncoder
4141
{
4242
public:
4343
ContractCallDataEncoder() {}
44-
/// Encode variable in order to be sent as parameter.
45-
void encode(QVariableDeclaration const* _dec, QString _baseType, QStringList _length, QString _value);
46-
/// Encode variable in order to be sent as parameter.
47-
void encode(QVariableDeclaration const* _dec, QString _value);
48-
/// Encode variable in order to be sent as parameter.
49-
void encode(QVariableDeclaration const* _dec, bool _value);
5044
/// Encode hash of the function to call.
5145
void encode(QFunctionDefinition const* _function);
5246
/// Decode variable in order to be sent to QML view.
5347
QList<QVariableDefinition*> decode(QList<QVariableDeclaration*> _dec, bytes _value);
5448
/// Get all encoded data encoded by encode function.
5549
bytes encodedData();
56-
/// Encode the given list of parameters (@a _p)
57-
void encode(QVariableDefinition const* _dec);
58-
/// Push the given @ _b to the current stored bytes.
50+
/// Push the given @ _b to the current param context.
5951
void push(bytes _b);
6052

6153
private:
62-
//int padding(QString _type);
6354
bytes m_encodedData;
64-
static bytes encodeNumber(QString _value, QString _length);
65-
static QString convertToReadable(std::string _v, QVariableDeclaration* _dec);
66-
static QString convertToBool(std::string _v);
67-
static QString convertToInt(std::string _v);
68-
static int integerPadding(int _bitValue);
69-
static QString formatBool(bool _value);
70-
static QString resolveNumber(QString const& _rawParams);
7155
};
7256

7357
}

mix/QBigInt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <QQmlEngine>
2929
#include <libdevcore/CommonJS.h>
3030
#include <libdevcore/Common.h>
31-
#include "QVariableDefinition.h"
3231

3332
using namespace dev;
3433

@@ -76,6 +75,7 @@ class QBigInt: public QObject
7675
QBigInt(dev::u256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
7776
QBigInt(dev::bigint const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
7877
QBigInt(BigIntVariant const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value){ QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
78+
QBigInt(QString const& _value, QObject* _parent = 0): QObject(_parent) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); setValue(_value); }
7979
~QBigInt() {}
8080

8181
/// @returns the current used big integer.

mix/QFunctionDefinition.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ QFunctionDefinition::QFunctionDefinition(dev::solidity::FunctionDefinition const
3333
for (unsigned i = 0; i < parameters.size(); i++)
3434
m_parameters.append(new QVariableDeclaration(parameters.at(i).get()));
3535

36-
37-
3836
std::vector<std::shared_ptr<VariableDeclaration>> returnParameters = _f->getReturnParameters();
3937
for (unsigned i = 0; i < returnParameters.size(); i++)
4038
m_returnParameters.append(new QVariableDeclaration(returnParameters.at(i).get()));

mix/QVariableDeclaration.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @date 2014
2020
*/
2121

22+
#include <QDebug>
2223
#include <QStringList>
2324
#include <libsolidity/AST.h>
2425
#include "QBasicNodeDefinition.h"
@@ -39,13 +40,6 @@ class QVariableDeclaration: public QBasicNodeDefinition
3940
QVariableDeclaration() {}
4041
QVariableDeclaration(solidity::VariableDeclaration const* _v): QBasicNodeDefinition(_v), m_type(QString::fromStdString(_v->getType()->toString())) {}
4142
Q_INVOKABLE QString type() const { return m_type; }
42-
QStringList typeLength()
43-
{
44-
QRegExp rules("\\d");
45-
int pos = 0;
46-
while ((pos = rules.indexIn(m_type, pos)) != -1) {}
47-
return rules.capturedTexts();
48-
}
4943

5044
private:
5145
QString m_type;

0 commit comments

Comments
 (0)