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)
{
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)

6
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).

6
mix/ClientModel.cpp

@ -170,6 +170,12 @@ void ClientModel::executeSequence(std::vector<TransactionSettings> 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());

13
mix/ContractCallDataEncoder.cpp

@ -49,11 +49,9 @@ void ContractCallDataEncoder::push(bytes const& _b)
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;
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<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QVariableDecla
def = new QStringType(dec, QString());
else if (dec->type().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;

2
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<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.
bytes encodedData();
/// Push the given @a _b to the current param context.

4
mix/QBigInt.h

@ -36,7 +36,7 @@ namespace dev
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>
{
@ -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.

2
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;

65
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<dev::bigint>(_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<char>(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";
}

28
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;
};
}

14
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
{

1
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

1
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

10
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);
}
}
}

Loading…
Cancel
Save