Browse Source

Remove natspec.

cl-refactor
Paweł Bylica 9 years ago
parent
commit
e2a4a57793
  1. 30
      libnatspec/CMakeLists.txt
  2. 57
      libnatspec/NatspecExpressionEvaluator.cpp
  3. 53
      libnatspec/NatspecExpressionEvaluator.h
  4. 5
      libnatspec/natspec.qrc
  5. 3
      libnatspec/natspecjs/.gitignore
  6. 8
      libnatspec/natspecjs/.travis.yml
  7. 47
      libnatspec/natspecjs/README.md
  8. 3570
      libnatspec/natspecjs/dist/natspec.js
  9. 1
      libnatspec/natspecjs/dist/natspec.js.map
  10. 2
      libnatspec/natspecjs/dist/natspec.min.js
  11. 13
      libnatspec/natspecjs/example/example.html
  12. 186
      libnatspec/natspecjs/natspec.js
  13. 24
      libnatspec/natspecjs/package.json
  14. 149
      libnatspec/natspecjs/test/test.js
  15. 32
      pullSubtree.sh

30
libnatspec/CMakeLists.txt

@ -1,30 +0,0 @@
cmake_policy(SET CMP0015 NEW)
# let cmake autolink dependencies on windows
cmake_policy(SET CMP0020 NEW)
# this policy was introduced in cmake 3.0
# remove if, once 3.0 will be used on unix
if (${CMAKE_MAJOR_VERSION} GREATER 2)
cmake_policy(SET CMP0043 OLD)
endif()
set(CMAKE_AUTOMOC OFF)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(. SRC_LIST)
include_directories(BEFORE ..)
include_directories(${Boost_INCLUDE_DIRS})
set(EXECUTABLE natspec)
file(GLOB HEADERS "*.h")
qt5_add_resources(NATSPECQRC natspec.qrc)
add_library(${EXECUTABLE} ${RESOURCE_ADDED} ${SRC_LIST} ${HEADERS} ${NATSPECQRC})
target_link_libraries(${EXECUTABLE} Qt5::Core)
target_link_libraries(${EXECUTABLE} Qt5::Qml)
target_link_libraries(${EXECUTABLE} devcore)
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} )

57
libnatspec/NatspecExpressionEvaluator.cpp

@ -1,57 +0,0 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file NatspecExpressionEvaluator.cpp
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#include <libdevcore/Log.h>
#include <libdevcore/Exceptions.h>
#include "NatspecExpressionEvaluator.h"
using namespace std;
using namespace dev;
static QString contentsOfQResource(string const& _res)
{
QFile file(QString::fromStdString(_res));
if (!file.open(QFile::ReadOnly))
BOOST_THROW_EXCEPTION(FileError());
QTextStream in(&file);
return in.readAll();
}
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("var natspec = require('natspec')");
}
QString NatspecExpressionEvaluator::evalExpression(QString const& _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();
}

53
libnatspec/NatspecExpressionEvaluator.h

@ -1,53 +0,0 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file NatspecExpressionEvaluator.h
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#pragma once
#pragma warning(push)
#pragma clang diagnostic ignored "-Winconsistent-missing-override"
#include <QtCore/QtCore>
#include <QtQml/QJSEngine>
#pragma warning(pop)
/**
* Should be used to evaluate natspec expression.
* @see test/natspec.cpp for natspec expression examples
*/
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.
/// javascript valid objects
NatspecExpressionEvaluator(QString const& _abi = "[]", QString const& _transaction = "{}", QString const& _method = "");
/// Should be called to evaluate natspec expression
/// @params expression - natspec expression
/// @returns evaluated natspec expression if it was valid, otherwise original expression
QString evalExpression(QString const& _expression);
private:
QJSEngine m_engine;
QString m_abi;
QString m_transaction;
QString m_method;
};

5
libnatspec/natspec.qrc

@ -1,5 +0,0 @@
<RCC>
<qresource prefix="/natspec">
<file alias="natspec.js">natspecjs/dist/natspec.min.js</file>
</qresource>
</RCC>

3
libnatspec/natspecjs/.gitignore

@ -1,3 +0,0 @@
# VIM stuff
*.swp
node_modules/

8
libnatspec/natspecjs/.travis.yml

@ -1,8 +0,0 @@
language: node_js
node_js:
- "0.11"
- "0.10"
before_script:
- npm install
after_script:
- npm run-script test-coveralls

47
libnatspec/natspecjs/README.md

@ -1,47 +0,0 @@
# natspec.js
Javascript Library used to evaluate natspec expressions
[![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url]
[travis-image]: https://travis-ci.org/ethereum/natspec.js.svg
[travis-url]: https://travis-ci.org/ethereum/natspec.js
[coveralls-image]: https://coveralls.io/repos/ethereum/natspec.js/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/r/ethereum/natspec.js?branch=master
## Usage
It exposes global object `natspec` with method `evaluateExpression`.
```javascript
var natspec = require('natspec');
var natspecExpression = "Will multiply `a` by 7 and return `a * 7`.";
var call = {
method: 'multiply',
abi: abi,
transaction: transaction
};
var evaluatedExpression = natspec.evaluateExpression(natspecExpression, call);
console.log(evaluatedExpression); // "Will multiply 4 by 7 and return 28."
```
More examples are available [here](https://github.com/ethereum/natspec.js/blob/master/test/test.js).
## Building
```bash
npm run-script build
```
## Testing (mocha)
```bash
npm test
```
## Wiki
* [Ethereum Natural Specification Format](https://github.com/ethereum/wiki/wiki/Ethereum-Natural-Specification-Format)
* [Natspec Example](https://github.com/ethereum/wiki/wiki/Natspec-Example)

3570
libnatspec/natspecjs/dist/natspec.js

File diff suppressed because it is too large

1
libnatspec/natspecjs/dist/natspec.js.map

File diff suppressed because one or more lines are too long

2
libnatspec/natspecjs/dist/natspec.min.js

File diff suppressed because one or more lines are too long

13
libnatspec/natspecjs/example/example.html

@ -1,13 +0,0 @@
<!doctype>
<html>
<head>
<script type="text/javascript" src="../dist/natspec.js"></script>
<script type="text/javascript">
var natspec = require('natspec');
</script>
</head>
<body>
</body>
</html>

186
libnatspec/natspecjs/natspec.js

@ -1,186 +0,0 @@
/*
This file is part of natspec.js.
natspec.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
natspec.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with natspec.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file natspec.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
var abi = require('./node_modules/ethereum.js/lib/abi.js');
/**
* This object should be used to evaluate natspec expression
* It has one method evaluateExpression which shoul be used
*/
var natspec = (function () {
/**
* Helper method
* Should be called to copy values from object to global context
*
* @method copyToContext
* @param {Object} object from which we want to copy properties
* @param {Object} object to which we copy
*/
var copyToContext = function (obj, context) {
Object.keys(obj).forEach(function (key) {
context[key] = obj[key];
});
}
/**
* Should be used to generate codes, which will be evaluated
*
* @method generateCode
* @param {Object} object from which code will be generated
* @return {String} javascript code which is used to initalized variables
*/
var generateCode = function (obj) {
return Object.keys(obj).reduce(function (acc, key) {
return acc + "var " + key + " = context['" + key + "'];\n";
}, "");
};
/**
* Helper method
* Should be called to get method with given name from the abi
*
* @method getMethodWithName
* @param {Array} contract's abi
* @param {String} name of the method that we are looking for
* @return {Object} abi for method with name
*/
var getMethodWithName = function(abi, name) {
return abi.filter(function (method) {
return method.name === name;
})[0];
};
/**
* Should be used to get all contract method input variables
*
* @method getMethodInputParams
* @param {Object} abi for certain method
* @param {Object} transaction object
* @return {Object} object with all contract's method input variables
*/
var getMethodInputParams = function (method, transaction) {
// do it with output formatter (cause we have to decode)
var params = abi.formatOutput(method.inputs, '0x' + transaction.params[0].data.slice(10));
return method.inputs.reduce(function (acc, current, index) {
acc[current.name] = params[index];
return acc;
}, {});
};
/**
* Should be called when we want to evaluate natspec expression
* Replaces all natspec 'subexpressions' with evaluated value
*
* @method mapExpressionToEvaluate
* @param {String} expression to evaluate
* @param {Function} callback which is called to evaluate te expression
* @return {String} evaluated expression
*/
var mapExpressionsToEvaluate = function (expression, cb) {
var evaluatedExpression = "";
// match everything in `` quotes
var pattern = /\`(?:\\.|[^`\\])*\`/gim
var match;
var lastIndex = 0;
try {
while ((match = pattern.exec(expression)) !== null) {
var startIndex = pattern.lastIndex - match[0].length;
var toEval = match[0].slice(1, match[0].length - 1);
evaluatedExpression += expression.slice(lastIndex, startIndex);
var evaluatedPart = cb(toEval);
evaluatedExpression += evaluatedPart;
lastIndex = pattern.lastIndex;
}
evaluatedExpression += expression.slice(lastIndex);
}
catch (err) {
throw new Error("Natspec evaluation failed, wrong input params");
}
return evaluatedExpression;
};
/**
* Should be called to evaluate single expression
* Is internally using javascript's 'eval' method
*
* @method evaluateExpression
* @param {String} expression which should be evaluated
* @param {Object} [call] object containing contract abi, transaction, called method
* @return {String} evaluated expression
* @throws exception if method is not found or we are trying to evaluate input params that does not exists
*/
var evaluateExpression = function (expression, call) {
//var self = this;
var context = {};
if (!!call) {
try {
var method = getMethodWithName(call.abi, call.method);
var params = getMethodInputParams(method, call.transaction);
copyToContext(params, context);
}
catch (err) {
throw new Error("Natspec evaluation failed, method does not exist");
}
}
var code = generateCode(context);
var evaluatedExpression = mapExpressionsToEvaluate(expression, function (toEval) {
var fn = new Function("context", code + "return " + toEval + ";");
return fn(context).toString();
});
return evaluatedExpression;
};
/**
* Safe version of evaluateExpression
* Instead of throwing an exception it returns it as a string
*
* @method evaluateExpressionSafe
* @param {String} expression which should be evaluated
* @param {Object} [call] object containing contract abi, transaction, called method
* @return {String} evaluated expression
*/
var evaluateExpressionSafe = function (expression, call) {
try {
return evaluateExpression(expression, call);
}
catch (err) {
return err.message;
}
};
return {
evaluateExpression: evaluateExpression,
evaluateExpressionSafe: evaluateExpressionSafe
};
})();
module.exports = natspec;

24
libnatspec/natspecjs/package.json

@ -1,24 +0,0 @@
{
"name": "natspec.js",
"version": "0.0.1",
"description": "Javascript Library used to evaluate natspec expressions",
"main": "natspec.js",
"scripts": {
"build": "cd dist && browserify -r ../natspec.js:natspec -i crypto -o natspec.js && uglifyjs natspec.js --source-map natspec.js.map -c -m -o natspec.min.js",
"test": "mocha",
"test-coveralls": "istanbul cover _mocha -- -R spec && cat coverage/lcov.info | coveralls --verbose"
},
"author": "",
"dependencies": {
"ethereum.js": "ethereum/ethereum.js#master"
},
"devDependencies": {
"browserify": "^9.0.3",
"chai": "^2.1.0",
"coveralls": "^2.11.2",
"istanbul": "^0.3.6",
"mocha": "^2.1.0",
"uglify-js": "^2.4.16"
},
"license": "LGPL-3.0"
}

149
libnatspec/natspecjs/test/test.js

@ -1,149 +0,0 @@
var chai = require('chai');
var natspec = require('../natspec.js');
var assert = chai.assert;
describe('natspec', function () {
it('should evaluate simple expression', function () {
// given
var expression = "`x = 1` + `y = 2` will be equal `x + y`";
// when
var result = natspec.evaluateExpression(expression);
var result2 = natspec.evaluateExpressionSafe(expression);
// then
assert.equal(result, "1 + 2 will be equal 3");
assert.equal(result2, "1 + 2 will be equal 3");
});
it('should evalute expression using input params', function () {
//given
var expression = "Will multiply `a` by 7 and return `a * 7`.";
var method = 'multiply';
var abi = [{
"name": "multiply",
"constant": false,
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}],
"outputs": [{
"name": "d",
"type": "uint256"
}]
}];
var transaction = {
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
"data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
}],
"id": 6
};
var call = {
method: method,
abi: abi,
transaction: transaction
};
// when
var result = natspec.evaluateExpression(expression, call);
var result2 = natspec.evaluateExpressionSafe(expression, call);
// then
assert.equal(result, "Will multiply 122 by 7 and return 854.");
assert.equal(result2, "Will multiply 122 by 7 and return 854.");
});
it('should evalute expression using input params', function () {
//given
var expression = "Will multiply `a` by 7 and return `a * 7`.";
var method = 'multiply';
var abi = [{
"name": "multiply",
"constant": false,
"type": "function",
"inputs": [{
"name": "b",
"type": "uint256"
}],
"outputs": [{
"name": "d",
"type": "uint256"
}]
}];
var transaction = {
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
"data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
}],
"id": 6
};
var call = {
method: method,
abi: abi,
transaction: transaction
};
// when
var exceptionThrow = function () { natspec.evaluateExpression(expression, call);}
var result = natspec.evaluateExpressionSafe(expression, call);
// then
assert.equal(result, "Natspec evaluation failed, wrong input params");
assert.throws(exceptionThrow, "Natspec evaluation failed, wrong input params");
});
it('should evalute expression using input params', function () {
//given
var expression = "Will multiply `a` by 7 and return `a * 7`.";
var method = 'multiply2';
var abi = [{
"name": "multiply",
"constant": false,
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}],
"outputs": [{
"name": "d",
"type": "uint256"
}]
}];
var transaction = {
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
"data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
}],
"id": 6
};
var call = {
method: method,
abi: abi,
transaction: transaction
};
// when
var exceptionThrow = function () { natspec.evaluateExpression(expression, call);}
var result = natspec.evaluateExpressionSafe(expression, call);
// then
assert.equal(result, "Natspec evaluation failed, method does not exist");
assert.throws(exceptionThrow, "Natspec evaluation failed, method does not exist");
});
});

32
pullSubtree.sh

@ -1,32 +0,0 @@
# usage
# ./pullsubtree [repository branch] [repository2 branch2]
#
# example
# ./pullSubtree evmjit master
# ./pullSubtree ethereumjs develop
# ./pullSubtree evmjit master ethereumjs master
evmjit_repo="https://github.com/ethereum/evmjit"
evmjit_location="evmjit"
ethereumjs_repo="https://github.com/ethereum/ethereum.js"
ethereumjs_location="libjsqrc/ethereumjs"
natspecjs_repo="https://github.com/ethereum/natspec.js"
natspecjs_location="libnatspec/natspecjs"
while [ "$1" != "" ]; do
case $1 in
evmjit | ethereumjs | natspecjs )
REPO="${1}_repo"
REPO=${!REPO}
LOCATION="${1}_location"
LOCATION=${!LOCATION}
shift
BRANCH=$1
git subtree pull --prefix=${LOCATION} ${REPO} ${BRANCH} --squash
;;
esac
shift
done
Loading…
Cancel
Save