From a93319623a1df9ab64ed831356b9a8b6cdd93d6c Mon Sep 17 00:00:00 2001 From: yann300 Date: Sun, 1 Feb 2015 17:23:13 +0100 Subject: [PATCH] misc changes --- libdevcore/CommonJS.cpp | 28 ++++++-------- libdevcore/CommonJS.h | 6 +-- mix/ClientModel.cpp | 6 +++ mix/ContractCallDataEncoder.cpp | 13 ++++--- mix/ContractCallDataEncoder.h | 2 +- mix/QBigInt.h | 4 +- mix/QVariableDeclaration.h | 2 +- mix/QVariableDefinition.cpp | 65 ++++++++++++++++----------------- mix/QVariableDefinition.h | 28 +++++++++----- mix/qml/QBoolTypeView.qml | 14 +++++-- mix/qml/QHashTypeView.qml | 1 + mix/qml/QStringTypeView.qml | 1 + mix/qml/TransactionDialog.qml | 10 ++--- 13 files changed, 99 insertions(+), 81 deletions(-) diff --git a/libdevcore/CommonJS.cpp b/libdevcore/CommonJS.cpp index aa5ce2802..8cdb27954 100644 --- a/libdevcore/CommonJS.cpp +++ b/libdevcore/CommonJS.cpp @@ -47,9 +47,8 @@ bytes padded(bytes _b, unsigned _l) bytes paddedRight(bytes _b, unsigned _l) { - while (_b.size() < _l) - _b.insert(_b.end(), 0); - return asBytes(asString(_b).substr(_b.size() - std::max(_l, _l))); + _b.resize(_l); + return _b; } bytes unpadded(bytes _b) @@ -59,24 +58,21 @@ bytes unpadded(bytes _b) return _b; } -std::string unpadRight(std::string _b) +bytes unpadLeft(bytes _b) { + int i = 0; + if (_b.size() == 0) + return _b; while (true) { - auto p = _b.find_last_of("0"); - if (p == _b.size() - 1 && p != std::string::npos) - _b = _b.substr(0, _b.size() - 1); + if (_b.at(i) == byte(0)) + i++; else - return _b; + break; } -} - -std::string unpadLeft(std::string _b) -{ - auto p = _b.find_first_not_of('0'); - if (p == std::string::npos) - return "0"; - return _b.substr(p, _b.length() - 1); + if (i != 0) + _b.erase(_b.begin(), _b.begin() + i); + return _b; } std::string prettyU256(u256 _n) diff --git a/libdevcore/CommonJS.h b/libdevcore/CommonJS.h index 6b75d70dc..59e6c1d34 100644 --- a/libdevcore/CommonJS.h +++ b/libdevcore/CommonJS.h @@ -52,12 +52,10 @@ bytes jsToBytes(std::string const& _s); bytes padded(bytes _b, unsigned _l); /// Add '0' on the queue of @a _b until @a _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. bytes unpadded(bytes _s); -/// Remove all '0' on the head of @a _s. Returns 0 if @a _s contains only '0'. -std::string unpadLeft(std::string _s); +/// Remove all 0 byte on the head of @a _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. std::string prettyU256(u256 _n); /// Convert h256 into user-readable string (by directly using std::string constructor). diff --git a/mix/ClientModel.cpp b/mix/ClientModel.cpp index 393d08657..e71280821 100644 --- a/mix/ClientModel.cpp +++ b/mix/ClientModel.cpp @@ -170,6 +170,12 @@ void ClientModel::executeSequence(std::vector const& _seque if (!f) 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); for (int p = 0; p < t.parameterValues.size(); p++) c.push(t.parameterValues.at(p)->encodeValue()); diff --git a/mix/ContractCallDataEncoder.cpp b/mix/ContractCallDataEncoder.cpp index 19e40936d..bdec95ca4 100644 --- a/mix/ContractCallDataEncoder.cpp +++ b/mix/ContractCallDataEncoder.cpp @@ -49,11 +49,9 @@ void ContractCallDataEncoder::push(bytes const& _b) m_encodedData.insert(m_encodedData.end(), _b.begin(), _b.end()); } -QList ContractCallDataEncoder::decode(QList const& _returnParameters, bytes const& _value) +QList ContractCallDataEncoder::decode(QList const& _returnParameters, bytes _value) { QList r; - std::string returnValue = toJS(_value); - returnValue = returnValue.substr(2, returnValue.length() - 1); for (int k = 0; k <_returnParameters.length(); k++) { QVariableDeclaration* dec = (QVariableDeclaration*)_returnParameters.at(k); @@ -68,9 +66,14 @@ QList ContractCallDataEncoder::decode(QListtype().contains("hash") || dec->type().contains("address")) 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); - 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(); } return r; diff --git a/mix/ContractCallDataEncoder.h b/mix/ContractCallDataEncoder.h index d64745052..718beb8e0 100644 --- a/mix/ContractCallDataEncoder.h +++ b/mix/ContractCallDataEncoder.h @@ -44,7 +44,7 @@ public: /// Encode hash of the function to call. void encode(QFunctionDefinition const* _function); /// Decode variable in order to be sent to QML view. - QList decode(QList const& _dec, bytes const& _value); + QList decode(QList const& _dec, bytes _value); /// Get all encoded data encoded by encode function. bytes encodedData(); /// Push the given @a _b to the current param context. diff --git a/mix/QBigInt.h b/mix/QBigInt.h index 0f5c88c43..c6ec72305 100644 --- a/mix/QBigInt.h +++ b/mix/QBigInt.h @@ -36,7 +36,7 @@ namespace dev namespace mix { -using BigIntVariant = boost::variant; +using BigIntVariant = boost::variant; struct add: public boost::static_visitor { @@ -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::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(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() {} /// @returns the current used big integer. diff --git a/mix/QVariableDeclaration.h b/mix/QVariableDeclaration.h index 0d091b035..2670a4dc3 100644 --- a/mix/QVariableDeclaration.h +++ b/mix/QVariableDeclaration.h @@ -40,7 +40,7 @@ public: QVariableDeclaration() {} 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())) {} - Q_INVOKABLE QString type() const { return m_type; } + QString type() const { return m_type; } private: QString m_type; diff --git a/mix/QVariableDefinition.cpp b/mix/QVariableDefinition.cpp index ffa2c04aa..5049283da 100644 --- a/mix/QVariableDefinition.cpp +++ b/mix/QVariableDefinition.cpp @@ -59,25 +59,30 @@ QVariableDefinition* QVariableDefinitionList::val(int _idx) /* * 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::bigint i(value().toStdString()); if (i < 0) i = i + dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + 1; - std::ostringstream s; - s << std::hex << "0x" << i; - return padded(jsToBytes(s.str()), 32); + bytes ret(32); + toBigEndian(i, ret); + 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::bigint("0x" + rawParam); + dev::bigint bigint = dev::fromBigEndian(_rawValue); if (((bigint >> 32) & 1) == 1) bigint = bigint - dev::bigint("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") - 1; - std::ostringstream s; - s << std::dec << bigint; - setValue(QString::fromStdString(s.str())); + setValue(bigint); } /* @@ -85,14 +90,15 @@ void QIntType::decodeValue(std::string const& _rawValue) */ 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 unPadded = unpadLeft(rawParam); - setValue(QString::fromStdString(unPadded)); + std::string _ret = asString(unpadLeft(_rawValue)); + setValue(QString::fromStdString(_ret)); } /* @@ -103,7 +109,7 @@ dev::bytes QRealType::encodeValue() return bytes(); } -void QRealType::decodeValue(std::string const& _rawValue) +void QRealType::decodeValue(dev::bytes const& _rawValue) { Q_UNUSED(_rawValue); } @@ -113,24 +119,14 @@ void QRealType::decodeValue(std::string const& _rawValue) */ dev::bytes QStringType::encodeValue() { - qDebug() << QString::fromStdString(toJS(paddedRight(asBytes(value().toStdString()), 32))); - return paddedRight(asBytes(value().toStdString()), 32); + QByteArray b = value().toUtf8(); + 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); - 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(temp); - } - setValue(QString::fromStdString(res)); + setValue(QString::fromUtf8((char*)_rawValue.data())); } /* @@ -141,9 +137,10 @@ dev::bytes QBoolType::encodeValue() 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); - std::string unpadded = unpadLeft(rawParam); - setValue(QString::fromStdString(unpadded)); + byte ret = _rawValue.at(_rawValue.size() - 1); + bool boolRet = (ret == byte(1)); + m_boolValue = boolRet; + m_value = m_boolValue ? "1" : "0"; } diff --git a/mix/QVariableDefinition.h b/mix/QVariableDefinition.h index a41a4a7ad..ae9bf9459 100644 --- a/mix/QVariableDefinition.h +++ b/mix/QVariableDefinition.h @@ -52,10 +52,12 @@ public: /// Encode the current value in order to be used as function parameter. virtual bytes encodeValue() = 0; /// 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; + +private: QVariableDeclaration* m_dec; }; @@ -85,9 +87,14 @@ public: QIntType() {} QIntType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} 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. - 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 @@ -98,7 +105,7 @@ public: QRealType() {} QRealType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} dev::bytes encodeValue() override; - void decodeValue(std::string const& _rawValue) override; + void decodeValue(dev::bytes const& _rawValue) override; }; class QStringType: public QVariableDefinition @@ -109,7 +116,7 @@ public: QStringType() {} QStringType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} dev::bytes encodeValue() override; - void decodeValue(std::string const& _rawValue) override; + void decodeValue(dev::bytes const& _rawValue) override; }; class QHashType: public QVariableDefinition @@ -120,7 +127,7 @@ public: QHashType() {} QHashType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} dev::bytes encodeValue() override; - void decodeValue(std::string const& _rawValue) override; + void decodeValue(dev::bytes const& _rawValue) override; }; class QBoolType: public QVariableDefinition @@ -131,9 +138,12 @@ public: QBoolType() {} QBoolType(QVariableDeclaration* _def, QString _value): QVariableDefinition(_def, _value) {} 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. - bool toBool() { return value() != "0"; } + bool toBool() { return m_boolValue; } + +private: + bool m_boolValue; }; } diff --git a/mix/qml/QBoolTypeView.qml b/mix/qml/QBoolTypeView.qml index 4b17f5cc2..a52601bdb 100644 --- a/mix/qml/QBoolTypeView.qml +++ b/mix/qml/QBoolTypeView.qml @@ -5,22 +5,28 @@ Item { id: editRoot property string text - property bool defaultValue + property string defaultValue + Rectangle { anchors.fill: parent ComboBox { + property bool inited: false Component.onCompleted: { - text = (defaultValue ? "1" : "0"); - currentIndex = parseInt(text); + if (text === "") + currentIndex = parseInt(defaultValue); + else + currentIndex = parseInt(text); + inited = true } id: boolCombo anchors.fill: parent onCurrentIndexChanged: { - text = comboModel.get(currentIndex).value; + if (inited) + text = comboModel.get(currentIndex).value; } model: ListModel { diff --git a/mix/qml/QHashTypeView.qml b/mix/qml/QHashTypeView.qml index 7c77ae1c2..e36514fab 100644 --- a/mix/qml/QHashTypeView.qml +++ b/mix/qml/QHashTypeView.qml @@ -10,6 +10,7 @@ Item id: textinput text: text anchors.fill: parent + wrapMode: Text.WrapAnywhere MouseArea { id: mouseArea anchors.fill: parent diff --git a/mix/qml/QStringTypeView.qml b/mix/qml/QStringTypeView.qml index f794a3b2d..a78fc1d26 100644 --- a/mix/qml/QStringTypeView.qml +++ b/mix/qml/QStringTypeView.qml @@ -10,6 +10,7 @@ Item id: textinput text: text anchors.fill: parent + wrapMode: Text.WrapAnywhere MouseArea { id: mouseArea anchors.fill: parent diff --git a/mix/qml/TransactionDialog.qml b/mix/qml/TransactionDialog.qml index 96b2e0e6b..f81de10a5 100644 --- a/mix/qml/TransactionDialog.qml +++ b/mix/qml/TransactionDialog.qml @@ -356,19 +356,19 @@ Window { } } - Component { id: boolViewComp QBoolTypeView { id: boolView - text: styleData.value - defaultValue: true + defaultValue: "1" Component.onCompleted: { - //default value - loaderEditor.updateValue(styleData.row, styleData.role, "1"); + loaderEditor.updateValue(styleData.row, styleData.role, + (paramsModel.get(styleData.row).value === "" ? defaultValue : + paramsModel.get(styleData.row).value)); + text = (paramsModel.get(styleData.row).value === "" ? defaultValue : paramsModel.get(styleData.row).value); } } }