Browse Source

misc changes

cl-refactor
yann300 10 years ago
committed by yann300
parent
commit
a93319623a
  1. 28
      libdevcore/CommonJS.cpp
  2. 6
      libdevcore/CommonJS.h
  3. 6
      mix/ClientModel.cpp
  4. 13
      mix/ContractCallDataEncoder.cpp
  5. 2
      mix/ContractCallDataEncoder.h
  6. 4
      mix/QBigInt.h
  7. 2
      mix/QVariableDeclaration.h
  8. 65
      mix/QVariableDefinition.cpp
  9. 28
      mix/QVariableDefinition.h
  10. 14
      mix/qml/QBoolTypeView.qml
  11. 1
      mix/qml/QHashTypeView.qml
  12. 1
      mix/qml/QStringTypeView.qml
  13. 10
      mix/qml/TransactionDialog.qml

28
libdevcore/CommonJS.cpp

@ -47,9 +47,8 @@ bytes padded(bytes _b, unsigned _l)
bytes paddedRight(bytes _b, unsigned _l) bytes paddedRight(bytes _b, unsigned _l)
{ {
while (_b.size() < _l) _b.resize(_l);
_b.insert(_b.end(), 0); return _b;
return asBytes(asString(_b).substr(_b.size() - std::max(_l, _l)));
} }
bytes unpadded(bytes _b) bytes unpadded(bytes _b)
@ -59,24 +58,21 @@ bytes unpadded(bytes _b)
return _b; return _b;
} }
std::string unpadRight(std::string _b) bytes unpadLeft(bytes _b)
{ {
int i = 0;
if (_b.size() == 0)
return _b;
while (true) while (true)
{ {
auto p = _b.find_last_of("0"); if (_b.at(i) == byte(0))
if (p == _b.size() - 1 && p != std::string::npos) i++;
_b = _b.substr(0, _b.size() - 1);
else else
return _b; break;
} }
} if (i != 0)
_b.erase(_b.begin(), _b.begin() + i);
std::string unpadLeft(std::string _b) return _b;
{
auto p = _b.find_first_not_of('0');
if (p == std::string::npos)
return "0";
return _b.substr(p, _b.length() - 1);
} }
std::string prettyU256(u256 _n) std::string prettyU256(u256 _n)

6
libdevcore/CommonJS.h

@ -52,12 +52,10 @@ bytes jsToBytes(std::string const& _s);
bytes padded(bytes _b, unsigned _l); bytes padded(bytes _b, unsigned _l);
/// Add '0' on the queue of @a _b until @a _l. /// Add '0' on the queue of @a _b until @a _l.
bytes paddedRight(bytes _b, unsigned _l); bytes paddedRight(bytes _b, unsigned _l);
/// Remove all trailing '0'
std::string unpadRight(std::string _b);
/// Removing all trailing '0'. Returns empty array if input contains only '0' char. /// Removing all trailing '0'. Returns empty array if input contains only '0' char.
bytes unpadded(bytes _s); bytes unpadded(bytes _s);
/// Remove all '0' on the head of @a _s. Returns 0 if @a _s contains only '0'. /// Remove all 0 byte on the head of @a _s.
std::string unpadLeft(std::string _s); bytes unpadLeft(bytes _s);
/// Convert u256 into user-readable string. Returns int/hex value of 64 bits int, hex of 160 bits FixedHash. As a fallback try to handle input as h256. /// Convert u256 into user-readable string. Returns int/hex value of 64 bits int, hex of 160 bits FixedHash. As a fallback try to handle input as h256.
std::string prettyU256(u256 _n); std::string prettyU256(u256 _n);
/// Convert h256 into user-readable string (by directly using std::string constructor). /// Convert h256 into user-readable string (by directly using std::string constructor).

6
mix/ClientModel.cpp

@ -170,6 +170,12 @@ void ClientModel::executeSequence(std::vector<TransactionSettings> const& _seque
if (!f) if (!f)
throw std::runtime_error("function " + t.functionId.toStdString() + " not found"); throw std::runtime_error("function " + t.functionId.toStdString() + " not found");
for (int k = 0; k < f->parametersList() .size(); k++)
{
if (f->parametersList().at(k)->type() != t.parameterValues.at(k)->declaration()->type())
throw std::runtime_error("list of parameters has been changed. Need to update this transaction");
}
c.encode(f); c.encode(f);
for (int p = 0; p < t.parameterValues.size(); p++) for (int p = 0; p < t.parameterValues.size(); p++)
c.push(t.parameterValues.at(p)->encodeValue()); c.push(t.parameterValues.at(p)->encodeValue());

13
mix/ContractCallDataEncoder.cpp

@ -49,11 +49,9 @@ void ContractCallDataEncoder::push(bytes const& _b)
m_encodedData.insert(m_encodedData.end(), _b.begin(), _b.end()); m_encodedData.insert(m_encodedData.end(), _b.begin(), _b.end());
} }
QList<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QVariableDeclaration*> const& _returnParameters, bytes const& _value) QList<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QVariableDeclaration*> const& _returnParameters, bytes _value)
{ {
QList<QVariableDefinition*> r; QList<QVariableDefinition*> r;
std::string returnValue = toJS(_value);
returnValue = returnValue.substr(2, returnValue.length() - 1);
for (int k = 0; k <_returnParameters.length(); k++) for (int k = 0; k <_returnParameters.length(); k++)
{ {
QVariableDeclaration* dec = (QVariableDeclaration*)_returnParameters.at(k); QVariableDeclaration* dec = (QVariableDeclaration*)_returnParameters.at(k);
@ -68,9 +66,14 @@ QList<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QVariableDecla
def = new QStringType(dec, QString()); def = new QStringType(dec, QString());
else if (dec->type().contains("hash") || dec->type().contains("address")) else if (dec->type().contains("hash") || dec->type().contains("address"))
def = new QHashType(dec, QString()); def = new QHashType(dec, QString());
def->decodeValue(returnValue); else
BOOST_THROW_EXCEPTION(Exception() << errinfo_comment("Parameter declaration not found"));
bytes rawParam(_value.begin(), _value.begin() + 32);
def->decodeValue(rawParam);
r.push_back(def); r.push_back(def);
returnValue = returnValue.substr(32 * 2, returnValue.length() - 1); if (_value.size() > 32)
_value = bytes(_value.begin() + 32, _value.end());
qDebug() << "decoded return value : " << dec->type() << " " << def->value(); qDebug() << "decoded return value : " << dec->type() << " " << def->value();
} }
return r; return r;

2
mix/ContractCallDataEncoder.h

@ -44,7 +44,7 @@ public:
/// Encode hash of the function to call. /// Encode hash of the function to call.
void encode(QFunctionDefinition const* _function); void encode(QFunctionDefinition const* _function);
/// Decode variable in order to be sent to QML view. /// Decode variable in order to be sent to QML view.
QList<QVariableDefinition*> decode(QList<QVariableDeclaration*> const& _dec, bytes const& _value); QList<QVariableDefinition*> decode(QList<QVariableDeclaration*> const& _dec, bytes _value);
/// Get all encoded data encoded by encode function. /// Get all encoded data encoded by encode function.
bytes encodedData(); bytes encodedData();
/// Push the given @a _b to the current param context. /// Push the given @a _b to the current param context.

4
mix/QBigInt.h

@ -36,7 +36,7 @@ namespace dev
namespace mix namespace mix
{ {
using BigIntVariant = boost::variant<dev::u256, dev::bigint>; using BigIntVariant = boost::variant<dev::u256, dev::bigint, dev::s256>;
struct add: public boost::static_visitor<BigIntVariant> struct add: public boost::static_visitor<BigIntVariant>
{ {
@ -75,7 +75,7 @@ public:
QBigInt(dev::u256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } QBigInt(dev::u256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
QBigInt(dev::bigint const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } QBigInt(dev::bigint const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
QBigInt(BigIntVariant const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value){ QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } QBigInt(BigIntVariant const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value){ QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
QBigInt(QString const& _value, QObject* _parent = 0): QObject(_parent) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); setValue(_value); } QBigInt(dev::s256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); }
~QBigInt() {} ~QBigInt() {}
/// @returns the current used big integer. /// @returns the current used big integer.

2
mix/QVariableDeclaration.h

@ -40,7 +40,7 @@ public:
QVariableDeclaration() {} QVariableDeclaration() {}
QVariableDeclaration(solidity::VariableDeclaration const* _v): QBasicNodeDefinition(_v), m_type(QString::fromStdString(_v->getType()->toString())) {} QVariableDeclaration(solidity::VariableDeclaration const* _v): QBasicNodeDefinition(_v), m_type(QString::fromStdString(_v->getType()->toString())) {}
QVariableDeclaration(solidity::ParamDescription const& _v): QBasicNodeDefinition(_v.getName()), m_type(QString::fromStdString(_v.getType())) {} QVariableDeclaration(solidity::ParamDescription const& _v): QBasicNodeDefinition(_v.getName()), m_type(QString::fromStdString(_v.getType())) {}
Q_INVOKABLE QString type() const { return m_type; } QString type() const { return m_type; }
private: private:
QString m_type; QString m_type;

65
mix/QVariableDefinition.cpp

@ -59,25 +59,30 @@ QVariableDefinition* QVariableDefinitionList::val(int _idx)
/* /*
* QIntType * QIntType
*/ */
void QIntType::setValue(dev::bigint _value)
{
m_bigIntvalue = _value;
std::stringstream str;
str << std::dec << m_bigIntvalue;
m_value = QString::fromStdString(str.str());
}
dev::bytes QIntType::encodeValue() dev::bytes QIntType::encodeValue()
{ {
dev::bigint i(value().toStdString()); dev::bigint i(value().toStdString());
if (i < 0) if (i < 0)
i = i + dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + 1; i = i + dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + 1;
std::ostringstream s; bytes ret(32);
s << std::hex << "0x" << i; toBigEndian(i, ret);
return padded(jsToBytes(s.str()), 32); return ret;
} }
void QIntType::decodeValue(std::string const& _rawValue) void QIntType::decodeValue(dev::bytes const& _rawValue)
{ {
std::string rawParam = _rawValue.substr(0, 32 * 2); dev::bigint bigint = dev::fromBigEndian<dev::bigint>(_rawValue);
dev::bigint bigint = dev::bigint("0x" + rawParam);
if (((bigint >> 32) & 1) == 1) if (((bigint >> 32) & 1) == 1)
bigint = bigint - dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") - 1; bigint = bigint - dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") - 1;
std::ostringstream s; setValue(bigint);
s << std::dec << bigint;
setValue(QString::fromStdString(s.str()));
} }
/* /*
@ -85,14 +90,15 @@ void QIntType::decodeValue(std::string const& _rawValue)
*/ */
dev::bytes QHashType::encodeValue() dev::bytes QHashType::encodeValue()
{ {
return padded(jsToBytes("0x" + value().toStdString()), 32); QByteArray b = value().toUtf8();
bytes r = bytes(b.begin(), b.end());
return padded(r, 32);
} }
void QHashType::decodeValue(std::string const& _rawValue) void QHashType::decodeValue(dev::bytes const& _rawValue)
{ {
std::string rawParam = _rawValue.substr(0, 32 * 2); std::string _ret = asString(unpadLeft(_rawValue));
std::string unPadded = unpadLeft(rawParam); setValue(QString::fromStdString(_ret));
setValue(QString::fromStdString(unPadded));
} }
/* /*
@ -103,7 +109,7 @@ dev::bytes QRealType::encodeValue()
return bytes(); return bytes();
} }
void QRealType::decodeValue(std::string const& _rawValue) void QRealType::decodeValue(dev::bytes const& _rawValue)
{ {
Q_UNUSED(_rawValue); Q_UNUSED(_rawValue);
} }
@ -113,24 +119,14 @@ void QRealType::decodeValue(std::string const& _rawValue)
*/ */
dev::bytes QStringType::encodeValue() dev::bytes QStringType::encodeValue()
{ {
qDebug() << QString::fromStdString(toJS(paddedRight(asBytes(value().toStdString()), 32))); QByteArray b = value().toUtf8();
return paddedRight(asBytes(value().toStdString()), 32); bytes r = bytes(b.begin(), b.end());
return paddedRight(r, 32);
} }
void QStringType::decodeValue(std::string const& _rawValue) void QStringType::decodeValue(dev::bytes const& _rawValue)
{ {
std::string rawParam = _rawValue.substr(0, 32 * 2); setValue(QString::fromUtf8((char*)_rawValue.data()));
rawParam = unpadRight(rawParam);
std::string res;
res.reserve(rawParam.size() / 2);
for (unsigned int i = 0; i < rawParam.size(); i += 2)
{
std::istringstream iss(rawParam.substr(i, 2));
int temp;
iss >> std::hex >> temp;
res += static_cast<char>(temp);
}
setValue(QString::fromStdString(res));
} }
/* /*
@ -141,9 +137,10 @@ dev::bytes QBoolType::encodeValue()
return padded(jsToBytes(value().toStdString()), 32); return padded(jsToBytes(value().toStdString()), 32);
} }
void QBoolType::decodeValue(std::string const& _rawValue) void QBoolType::decodeValue(dev::bytes const& _rawValue)
{ {
std::string rawParam = _rawValue.substr(0, 32 * 2); byte ret = _rawValue.at(_rawValue.size() - 1);
std::string unpadded = unpadLeft(rawParam); bool boolRet = (ret == byte(1));
setValue(QString::fromStdString(unpadded)); m_boolValue = boolRet;
m_value = m_boolValue ? "1" : "0";
} }

28
mix/QVariableDefinition.h

@ -52,10 +52,12 @@ public:
/// Encode the current value in order to be used as function parameter. /// Encode the current value in order to be used as function parameter.
virtual bytes encodeValue() = 0; virtual bytes encodeValue() = 0;
/// Decode the return value @a _rawValue. /// Decode the return value @a _rawValue.
virtual void decodeValue(std::string const& _rawValue) = 0; virtual void decodeValue(dev::bytes const& _rawValue) = 0;
private: protected:
QString m_value; QString m_value;
private:
QVariableDeclaration* m_dec; QVariableDeclaration* m_dec;
}; };
@ -85,9 +87,14 @@ public:
QIntType() {} QIntType() {}
QIntType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} QIntType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {}
dev::bytes encodeValue() override; dev::bytes encodeValue() override;
void decodeValue(std::string const& _rawValue) override; void decodeValue(dev::bytes const& _rawValue) override;
/// @returns an instance of QBigInt for the current value. /// @returns an instance of QBigInt for the current value.
QBigInt* toBigInt() { return new QBigInt(value()); } QBigInt* toBigInt() { return new QBigInt(m_bigIntvalue); }
dev::bigint bigInt() { return m_bigIntvalue; }
void setValue(dev::bigint _value);
private:
dev::bigint m_bigIntvalue;
}; };
class QRealType: public QVariableDefinition class QRealType: public QVariableDefinition
@ -98,7 +105,7 @@ public:
QRealType() {} QRealType() {}
QRealType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} QRealType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {}
dev::bytes encodeValue() override; dev::bytes encodeValue() override;
void decodeValue(std::string const& _rawValue) override; void decodeValue(dev::bytes const& _rawValue) override;
}; };
class QStringType: public QVariableDefinition class QStringType: public QVariableDefinition
@ -109,7 +116,7 @@ public:
QStringType() {} QStringType() {}
QStringType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} QStringType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {}
dev::bytes encodeValue() override; dev::bytes encodeValue() override;
void decodeValue(std::string const& _rawValue) override; void decodeValue(dev::bytes const& _rawValue) override;
}; };
class QHashType: public QVariableDefinition class QHashType: public QVariableDefinition
@ -120,7 +127,7 @@ public:
QHashType() {} QHashType() {}
QHashType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} QHashType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {}
dev::bytes encodeValue() override; dev::bytes encodeValue() override;
void decodeValue(std::string const& _rawValue) override; void decodeValue(dev::bytes const& _rawValue) override;
}; };
class QBoolType: public QVariableDefinition class QBoolType: public QVariableDefinition
@ -131,9 +138,12 @@ public:
QBoolType() {} QBoolType() {}
QBoolType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} QBoolType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {}
dev::bytes encodeValue() override; dev::bytes encodeValue() override;
void decodeValue(std::string const& _rawValue) override; void decodeValue(dev::bytes const& _rawValue) override;
/// @returns the boolean value for the current definition. /// @returns the boolean value for the current definition.
bool toBool() { return value() != "0"; } bool toBool() { return m_boolValue; }
private:
bool m_boolValue;
}; };
} }

14
mix/qml/QBoolTypeView.qml

@ -5,22 +5,28 @@ Item
{ {
id: editRoot id: editRoot
property string text property string text
property bool defaultValue property string defaultValue
Rectangle { Rectangle {
anchors.fill: parent anchors.fill: parent
ComboBox ComboBox
{ {
property bool inited: false
Component.onCompleted: Component.onCompleted:
{ {
text = (defaultValue ? "1" : "0"); if (text === "")
currentIndex = parseInt(text); currentIndex = parseInt(defaultValue);
else
currentIndex = parseInt(text);
inited = true
} }
id: boolCombo id: boolCombo
anchors.fill: parent anchors.fill: parent
onCurrentIndexChanged: onCurrentIndexChanged:
{ {
text = comboModel.get(currentIndex).value; if (inited)
text = comboModel.get(currentIndex).value;
} }
model: ListModel model: ListModel
{ {

1
mix/qml/QHashTypeView.qml

@ -10,6 +10,7 @@ Item
id: textinput id: textinput
text: text text: text
anchors.fill: parent anchors.fill: parent
wrapMode: Text.WrapAnywhere
MouseArea { MouseArea {
id: mouseArea id: mouseArea
anchors.fill: parent anchors.fill: parent

1
mix/qml/QStringTypeView.qml

@ -10,6 +10,7 @@ Item
id: textinput id: textinput
text: text text: text
anchors.fill: parent anchors.fill: parent
wrapMode: Text.WrapAnywhere
MouseArea { MouseArea {
id: mouseArea id: mouseArea
anchors.fill: parent anchors.fill: parent

10
mix/qml/TransactionDialog.qml

@ -356,19 +356,19 @@ Window {
} }
} }
Component Component
{ {
id: boolViewComp id: boolViewComp
QBoolTypeView QBoolTypeView
{ {
id: boolView id: boolView
text: styleData.value defaultValue: "1"
defaultValue: true
Component.onCompleted: Component.onCompleted:
{ {
//default value loaderEditor.updateValue(styleData.row, styleData.role,
loaderEditor.updateValue(styleData.row, styleData.role, "1"); (paramsModel.get(styleData.row).value === "" ? defaultValue :
paramsModel.get(styleData.row).value));
text = (paramsModel.get(styleData.row).value === "" ? defaultValue : paramsModel.get(styleData.row).value);
} }
} }
} }

Loading…
Cancel
Save