Browse Source

Merge branch 'develop' of https://github.com/ethereum/cpp-ethereum into mix_test

cl-refactor
arkpar 10 years ago
parent
commit
5552d212d6
  1. 100
      abi/main.cpp
  2. 3
      libdevcore/vector_ref.h
  3. 6
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  4. 1
      mix/CodeModel.cpp
  5. 44
      mix/ContractCallDataEncoder.cpp
  6. 1
      mix/ContractCallDataEncoder.h
  7. 7
      mix/qml/TransactionDialog.qml

100
abi/main.cpp

@ -264,12 +264,12 @@ struct ABIType
{
bytes ret = _b;
while (ret.size() < _length)
if (_f == Format::Binary)
if (base == Base::Bytes || (base == Base::Unknown && _f == Format::Binary))
ret.push_back(0);
else
ret.insert(ret.begin(), 0);
while (ret.size() > _length)
if (_f == Format::Binary)
if (base == Base::Bytes || (base == Base::Unknown && _f == Format::Binary))
ret.pop_back();
else
ret.erase(ret.begin());
@ -303,7 +303,7 @@ tuple<bytes, ABIType, Format> fromUser(std::string const& _arg, Tristate _prefix
type.noteDecimalInput();
return make_tuple(toCompactBigEndian(bigint(val.substr(1))), type, Format::Decimal);
}
if (val.substr(0, 1) == "@")
if (val.substr(0, 1) == "'")
{
type.noteBinaryInput();
return make_tuple(asBytes(val.substr(1)), type, Format::Binary);
@ -335,6 +335,7 @@ tuple<bytes, ABIType, Format> fromUser(std::string const& _arg, Tristate _prefix
throw InvalidUserString();
}
struct ExpectedAdditionalParameter: public Exception {};
struct ExpectedOpen: public Exception {};
struct ExpectedClose: public Exception {};
@ -394,10 +395,10 @@ struct ABIMethod
ss << "constant ";
if (!outs.empty())
{
ss << "returns (";
ss << "returns(";
f = 0;
for (ABIType const& i: outs)
ss << (f ? ", " : "") << i.canon() << " " << i.name;
ss << (f++ ? ", " : "") << i.canon() << " " << i.name;
ss << ")";
}
return ss.str();
@ -417,11 +418,15 @@ struct ABIMethod
for (ABIType const& a: ins)
{
if (pi >= _params.size())
throw ExpectedAdditionalParameter();
auto put = [&]() {
if (a.isBytes())
ret += h256(u256(_params[pi].first.size())).asBytes();
suffix += a.unrender(_params[pi].first, _params[pi].second);
pi++;
if (pi >= _params.size())
throw ExpectedAdditionalParameter();
};
function<void(vector<int>, unsigned)> putDim = [&](vector<int> addr, unsigned q) {
if (addr.size() == a.dims.size())
@ -430,13 +435,14 @@ struct ABIMethod
{
if (_params[pi].second != Format::Open)
throw ExpectedOpen();
++pi;
int l = a.dims[addr.size()];
if (l == -1)
{
// read ahead in params and discover the arity.
unsigned depth = 0;
l = 0;
for (unsigned pi2 = pi + 1; depth || _params[pi2].second != Format::Close;)
for (unsigned pi2 = pi; depth || _params[pi2].second != Format::Close;)
{
if (_params[pi2].second == Format::Open)
++depth;
@ -454,6 +460,7 @@ struct ABIMethod
putDim(addr, q);
if (_params[pi].second != Format::Close)
throw ExpectedClose();
++pi;
}
};
putDim(vector<int>(), 1);
@ -463,8 +470,6 @@ struct ABIMethod
string decode(bytes const& _data, int _index, EncodingPrefs _ep)
{
stringstream out;
if (_index == -1)
out << "[";
unsigned di = 0;
vector<unsigned> catDims;
for (ABIType const& a: outs)
@ -481,7 +486,6 @@ struct ABIMethod
put();
else
{
out << "[";
int l = a.dims[addr.size()];
if (l == -1)
{
@ -492,7 +496,6 @@ struct ABIMethod
q *= l;
for (addr.push_back(0); addr.back() < l; ++addr.back())
putDim(addr, q);
out << "]";
}
};
putDim(vector<int>(), 1);
@ -500,12 +503,20 @@ struct ABIMethod
unsigned d = 0;
for (ABIType const& a: outs)
{
if (_index == -1 && out.tellp() > 0)
out << ", ";
auto put = [&]() {
unsigned l = 32;
if (a.isBytes())
l = (catDims[d++] + 31 / 32) * 32;
out << a.render(bytesConstRef(&_data).cropped(di, l).toBytes(), _ep) << ", ";
di += l;
{
out << a.render(bytesConstRef(&_data).cropped(di, catDims[d]).toBytes(), _ep);
di += ((catDims[d] + 31) / 32) * 32;
d++;
}
else
{
out << a.render(bytesConstRef(&_data).cropped(di, 32).toBytes(), _ep);
di += 32;
}
};
function<void(vector<int>)> putDim = [&](vector<int> addr) {
if (addr.size() == a.dims.size())
@ -527,22 +538,22 @@ struct ABIMethod
}
};
putDim(vector<int>());
if (_index == -1)
out << ", ";
}
(void)_data;
if (_index == -1)
out << "]";
return out.str();
}
};
string canonSig(string const& _name, vector<ABIType> const& _args)
{
string methodArgs;
for (auto const& arg: _args)
methodArgs += (methodArgs.empty() ? "" : ",") + arg.canon();
return _name + "(" + methodArgs + ")";
try {
string methodArgs;
for (auto const& arg: _args)
methodArgs += (methodArgs.empty() ? "" : ",") + arg.canon();
return _name + "(" + methodArgs + ")";
}
catch (...) {
return string();
}
}
struct UnknownMethod: public Exception {};
@ -632,6 +643,7 @@ int main(int argc, char** argv)
int outputIndex = -1;
vector<pair<bytes, Format>> params;
vector<ABIType> args;
string incoming;
for (int i = 1; i < argc; ++i)
{
@ -656,8 +668,6 @@ int main(int argc, char** argv)
typePrefix = Tristate::True;
else if (arg == "-T" || arg == "--no-typing")
typePrefix = Tristate::False;
else if (arg == "-v" || arg == "--verbose")
verbose = true;
else if (arg == "-x" || arg == "--hex")
prefs.e = Encoding::Hex;
else if (arg == "-d" || arg == "--decimal" || arg == "--dec")
@ -665,21 +675,23 @@ int main(int argc, char** argv)
else if (arg == "-b" || arg == "--binary" || arg == "--bin")
prefs.e = Encoding::Binary;
else if (arg == "-v" || arg == "--verbose")
version();
verbose = true;
else if (arg == "-V" || arg == "--version")
version();
else if (method.empty())
method = arg;
else
else if (mode == Mode::Encode)
{
auto u = fromUser(arg, formatPrefix, typePrefix);
args.push_back(get<1>(u));
params.push_back(make_pair(get<0>(u), get<2>(u)));
}
else if (mode == Mode::Decode)
incoming += arg;
}
string abiData;
if (abiFile.empty())
if (!abiFile.empty())
abiData = contentsString(abiFile);
if (mode == Mode::Encode)
@ -695,13 +707,30 @@ int main(int argc, char** argv)
try {
m = abi.method(method, args);
}
catch(...)
catch (...)
{
cerr << "Unknown method in ABI." << endl;
exit(-1);
}
}
userOutput(cout, m.encode(params), encoding);
try {
userOutput(cout, m.encode(params), encoding);
}
catch (ExpectedAdditionalParameter const&)
{
cerr << "Expected additional parameter in input." << endl;
exit(-1);
}
catch (ExpectedOpen const&)
{
cerr << "Expected open-bracket '[' in input." << endl;
exit(-1);
}
catch (ExpectedClose const&)
{
cerr << "Expected close-bracket ']' in input." << endl;
exit(-1);
}
}
else if (mode == Mode::Decode)
{
@ -725,9 +754,14 @@ int main(int argc, char** argv)
exit(-1);
}
string encoded;
for (int i = cin.get(); i != -1; i = cin.get())
encoded.push_back((char)i);
cout << m.decode(fromHex(encoded), outputIndex, prefs);
if (incoming == "--" || incoming.empty())
for (int i = cin.get(); i != -1; i = cin.get())
encoded.push_back((char)i);
else
{
encoded = contentsString(incoming);
}
cout << m.decode(fromHex(boost::trim_copy(encoded)), outputIndex, prefs) << endl;
}
}

3
libdevcore/vector_ref.h

@ -39,7 +39,8 @@ public:
size_t size() const { return m_count; }
bool empty() const { return !m_count; }
vector_ref<_T> next() const { return vector_ref<_T>(m_data + m_count, m_count); }
vector_ref<_T> cropped(size_t _begin, size_t _count = ~size_t(0)) const { if (m_data && _begin + std::max(size_t(0), _count) <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
vector_ref<_T> cropped(size_t _begin, size_t _count) const { if (m_data && _begin + _count <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
vector_ref<_T> cropped(size_t _begin) const { if (m_data && _begin <= m_count) return vector_ref<_T>(m_data + _begin, m_count - _begin); else return vector_ref<_T>(); }
void retarget(_T* _d, size_t _s) { m_data = _d; m_count = _s; }
void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); }
void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const { memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); }

6
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -79,7 +79,7 @@ static Json::Value toJson(dev::eth::Transaction const& _t)
Json::Value res;
res["hash"] = toJS(_t.sha3());
res["input"] = toJS(_t.data());
res["to"] = toJS(_t.receiveAddress());
res["to"] = _t.isCreation() ? Json::Value() : toJS(_t.receiveAddress());
res["from"] = toJS(_t.safeSender());
res["gas"] = toJS(_t.gas());
res["gasPrice"] = toJS(_t.gasPrice());
@ -115,7 +115,7 @@ static Json::Value toJson(dev::eth::BlockInfo const& _bi, UncleHashes const& _us
static Json::Value toJson(dev::eth::TransactionSkeleton const& _t)
{
Json::Value res;
res["to"] = toJS(_t.to);
res["to"] = _t.creation ? Json::Value() : toJS(_t.to);
res["from"] = toJS(_t.from);
res["gas"] = toJS(_t.gas);
res["gasPrice"] = toJS(_t.gasPrice);
@ -418,7 +418,7 @@ static TransactionSkeleton toTransaction(Json::Value const& _json)
if (!_json["from"].empty())
ret.from = jsToAddress(_json["from"].asString());
if (!_json["to"].empty())
if (!_json["to"].empty() && _json["to"].asString() != "0x")
ret.to = jsToAddress(_json["to"].asString());
else
ret.creation = true;

1
mix/CodeModel.cpp

@ -342,6 +342,7 @@ SolidityType CodeModel::nodeType(dev::solidity::Type const* _type)
r.type = SolidityType::Type::Bytes;
r.size = static_cast<unsigned>(b->getNumBytes());
}
break;
case Type::Category::Contract:
r.type = SolidityType::Type::Address;
break;

44
mix/ContractCallDataEncoder.cpp

@ -80,28 +80,32 @@ unsigned ContractCallDataEncoder::encodeSingleItem(QVariant const& _data, Solidi
if ((src.startsWith("\"") && src.endsWith("\"")) || (src.startsWith("\'") && src.endsWith("\'")))
src = src.remove(src.length() - 1, 1).remove(0, 1);
QRegExp rx("[a-z]+");
if (src.startsWith("0x"))
{
result = fromHex(src.toStdString().substr(2));
if (_type.type != SolidityType::Type::Bytes)
result = padded(result, alignSize);
}
else if (rx.indexIn(src.toLower(), 0) != -1)
{
QByteArray bytesAr = src.toLocal8Bit();
result = bytes(bytesAr.begin(), bytesAr.end());
}
else
{
bigint i(src.toStdString());
result = bytes(alignSize);
toBigEndian((u256)i, result);
try
{
bigint i(src.toStdString());
result = bytes(alignSize);
toBigEndian((u256)i, result);
}
catch (std::exception const& ex)
{
// manage input as a string.
QByteArray bytesAr = src.toLocal8Bit();
result = bytes(bytesAr.begin(), bytesAr.end());
result = paddedRight(result, alignSize);
}
}
unsigned dataSize = _type.dynamicSize ? result.size() : alignSize;
_dest.insert(_dest.end(), result.begin(), result.end());
if (_dest.size() % alignSize != 0)
if ((_dest.size() - 4) % alignSize != 0)
_dest.resize((_dest.size() & ~(alignSize - 1)) + alignSize);
return dataSize;
}
@ -158,7 +162,11 @@ dev::bytes ContractCallDataEncoder::decodeBytes(dev::bytes const& _rawValue)
QString ContractCallDataEncoder::toString(dev::bytes const& _b)
{
return QString::fromStdString(dev::toJS(_b));
QString str;
if (asString(_b, str))
return "\"" + str + "\" " + QString::fromStdString(dev::toJS(_b));
else
return QString::fromStdString(dev::toJS(_b));
}
@ -196,3 +204,17 @@ QStringList ContractCallDataEncoder::decode(QList<QVariableDeclaration*> const&
}
return r;
}
bool ContractCallDataEncoder::asString(dev::bytes const& _b, QString& _str)
{
dev::bytes bunPad = unpadded(_b);
for (unsigned i = 0; i < bunPad.size(); i++)
{
if (bunPad.at(i) < 9 || bunPad.at(i) > 127)
return false;
else
_str += QString::fromStdString(dev::toJS(bunPad.at(i))).replace("0x", "");
}
return true;
}

1
mix/ContractCallDataEncoder.h

@ -66,6 +66,7 @@ private:
dev::bytes encodeBytes(QString const& _str);
dev::bytes decodeBytes(dev::bytes const& _rawValue);
QString toString(dev::bytes const& _b);
bool asString(dev::bytes const& _b, QString& _str);
private:
bytes m_encodedData;

7
mix/qml/TransactionDialog.qml

@ -81,6 +81,7 @@ Window {
loadParameter(params[p]);
}
}
initTypeLoader();
modalTransactionDialog.setX((Screen.width - width) / 2);
modalTransactionDialog.setY((Screen.height - height) / 2);
@ -126,11 +127,15 @@ Window {
}
}
}
initTypeLoader();
}
function initTypeLoader()
{
typeLoader.value = {}
typeLoader.members = []
typeLoader.value = paramValues;
typeLoader.members = paramsModel;
}
function close()

Loading…
Cancel
Save