From b76622494e0564b28d2b70adcf159074cfb41a85 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 9 Mar 2015 15:22:07 +0100 Subject: [PATCH] fixed natspec evaluation --- libnatspec/NatspecExpressionEvaluator.cpp | 20 +++++++++----------- libnatspec/NatspecExpressionEvaluator.h | 9 +++++---- test/natspec.cpp | 19 +++++++++++++++---- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/libnatspec/NatspecExpressionEvaluator.cpp b/libnatspec/NatspecExpressionEvaluator.cpp index fd8369145..ab957295e 100644 --- a/libnatspec/NatspecExpressionEvaluator.cpp +++ b/libnatspec/NatspecExpressionEvaluator.cpp @@ -35,25 +35,23 @@ static QString contentsOfQResource(string const& _res) return in.readAll(); } -NatspecExpressionEvaluator::NatspecExpressionEvaluator(QString const& _abi, QString const& _method, QString const& _params) +NatspecExpressionEvaluator::NatspecExpressionEvaluator(QString const& _abi, QString const& _transaction, QString const& _method) +: m_abi(_abi), m_transaction(_transaction), m_method(_method) { Q_INIT_RESOURCE(natspec); QJSValue result = m_engine.evaluate(contentsOfQResource(":/natspec/natspec.js")); if (result.isError()) BOOST_THROW_EXCEPTION(FileError()); - - m_engine.evaluate("globals.abi = " + _abi); - m_engine.evaluate("globals.method = " + _method); - m_engine.evaluate("globals.params = " + _params); + + m_engine.evaluate("var natspec = require('natspec')"); } QString NatspecExpressionEvaluator::evalExpression(QString const& _expression) { - QJSValue result = m_engine.evaluate("evaluateExpression(\"" + _expression + "\")"); - if (result.isError()) - { - cerr << "Could not evaluate expression: \"" << _expression.toStdString() << "\"" << endl; - return _expression; - } + QString call = ""; + if (!m_abi.isEmpty() && !m_transaction.isEmpty() && !m_method.isEmpty()) + call = ", {abi:" + m_abi + ", transaction:" + m_transaction + ", method: '" + m_method + "' }"; + + QJSValue result = m_engine.evaluate("natspec.evaluateExpressionSafe(\"" + _expression + "\"" + call + ")"); return result.toString(); } diff --git a/libnatspec/NatspecExpressionEvaluator.h b/libnatspec/NatspecExpressionEvaluator.h index 2ea224027..4aca090d6 100644 --- a/libnatspec/NatspecExpressionEvaluator.h +++ b/libnatspec/NatspecExpressionEvaluator.h @@ -34,12 +34,10 @@ class NatspecExpressionEvaluator public: /// Construct natspec expression evaluator /// @params abi - contract's abi in json format, passed as string + /// @params transaction - json object containing transaction data /// @params method - name of the contract's method for which we evaluate the natspec. - /// If we want to use raw string, it should be passed with quotation marks eg. "\"helloWorld\"" - /// If we pass string "helloWorld", the value of the object with name "helloWorld" will be used - /// @params params - array of method input params, passed as string, objects in array should be /// javascript valid objects - NatspecExpressionEvaluator(QString const& _abi = "[]", QString const& _method = "", QString const& _params = "[]"); + NatspecExpressionEvaluator(QString const& _abi = "[]", QString const& _transaction = "{}", QString const& _method = ""); /// Should be called to evaluate natspec expression /// @params expression - natspec expression @@ -48,4 +46,7 @@ public: private: QJSEngine m_engine; + QString m_abi; + QString m_transaction; + QString m_method; }; diff --git a/test/natspec.cpp b/test/natspec.cpp index 8ba660418..cdcedca46 100644 --- a/test/natspec.cpp +++ b/test/natspec.cpp @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(natspec_eval_function_exists) // given NatspecExpressionEvaluator e; // when - string result = e.evalExpression("`typeof evaluateExpression`").toStdString(); + string result = e.evalExpression("`typeof natspec.evaluateExpression`").toStdString(); // then BOOST_CHECK_EQUAL(result, "function"); } @@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params) // given char const* abi = R"([ { - "name": "f", + "name": "multiply", "constant": false, "type": "function", "inputs": [ @@ -94,7 +94,18 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params) ] } ])"; - NatspecExpressionEvaluator e(abi, "'f'", "[4]"); + + char const* transaction = R"({ + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000004" + }], + "id": 6 + })"; + + NatspecExpressionEvaluator e(abi, transaction , "multiply"); // when string result = e.evalExpression("Will multiply `a` by 7 and return `a * 7`.").toStdString(); // then @@ -108,7 +119,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_error) // when string result = e.evalExpression("`test(`").toStdString(); // then - BOOST_CHECK_EQUAL(result, "`test(`"); + BOOST_CHECK_EQUAL(result, "Natspec evaluation failed, wrong input params"); } BOOST_AUTO_TEST_SUITE_END()