From f3b07a4cd1390101144d748663ee0c76959cbfb3 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Wed, 4 Mar 2015 22:30:17 -0500 Subject: [PATCH] Refactored tests and introducing python --- .gitignore | 7 +++++-- .travis.yml | 4 ++-- CMakeLists.txt | 2 +- Makefile | 2 ++ pyethash/.gitignore | 2 ++ pyethash/__init__.py | 0 python-src/core.c | 34 ++++++++++++++++++++++++++++++++ setup.cfg | 8 ++++++++ setup.py | 16 +++++++++++++++ test/{ => c}/CMakeLists.txt | 2 +- test/{ => c}/test.cpp | 0 test/c/test.sh | 19 ++++++++++++++++++ test/python/.gitignore | 1 + test/python/requirements.txt | 2 ++ test/python/test.sh | 20 +++++++++++++++++++ test/python/test_pyethash.py | 9 +++++++++ test/test.sh | 38 +++++++++++++++++------------------- 17 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 Makefile create mode 100644 pyethash/.gitignore create mode 100644 pyethash/__init__.py create mode 100644 python-src/core.c create mode 100644 setup.cfg create mode 100755 setup.py rename test/{ => c}/CMakeLists.txt (95%) rename test/{ => c}/test.cpp (100%) create mode 100755 test/c/test.sh create mode 100644 test/python/.gitignore create mode 100644 test/python/requirements.txt create mode 100755 test/python/test.sh create mode 100644 test/python/test_pyethash.py diff --git a/.gitignore b/.gitignore index 55a2692e..c1ff7bad 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,8 @@ .DS_Store */**/*un~ .vagrant/ -cpp-build/ -go-build/ +*.pyc +build/ +pyethash.egg-info/ +*.so +*~ diff --git a/.travis.yml b/.travis.yml index 10b8870b..d68197d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ before_install: - # Make sure we use the latest cmake - sudo apt-get update -qq - - sudo apt-get install -qq wget cmake gcc bash libboost-test-dev nodejs + - sudo apt-get install -qq wget cmake gcc bash libboost-test-dev nodejs python-pip + # Make sure we use the latest cmake - (cd /tmp && wget http://www.cmake.org/files/v3.1/cmake-3.1.3.tar.gz && tar zxf cmake-3.1.3.tar.gz) - (cd /tmp/cmake-3.1.3 && cmake . && make && sudo make install) > /dev/null 2>&1 diff --git a/CMakeLists.txt b/CMakeLists.txt index a9db4698..cf7e5625 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,4 +11,4 @@ endif() add_subdirectory(libethash) add_subdirectory(libethash-cl EXCLUDE_FROM_ALL) add_subdirectory(benchmark EXCLUDE_FROM_ALL) -add_subdirectory(test EXCLUDE_FROM_ALL) +add_subdirectory(test/c EXCLUDE_FROM_ALL) diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..e1cda496 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +clean: + rm -rf *.so pyethash.egg-info/ build/ test/python/python-virtual-env/ test/c/build/ pythethash/core.so diff --git a/pyethash/.gitignore b/pyethash/.gitignore new file mode 100644 index 00000000..e1374b86 --- /dev/null +++ b/pyethash/.gitignore @@ -0,0 +1,2 @@ +pyethash.egg-info/ +*.so diff --git a/pyethash/__init__.py b/pyethash/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python-src/core.c b/python-src/core.c new file mode 100644 index 00000000..2992231b --- /dev/null +++ b/python-src/core.c @@ -0,0 +1,34 @@ +#include +#include "../libethash/ethash.h" + +static PyObject* +get_params(PyObject* self, PyObject* args) +{ + unsigned long block_number; + ethash_params p; + + if (!PyArg_ParseTuple(args, "k", &block_number)) + Py_RETURN_NONE; + + ethash_params_init(&p, block_number); + + printf("\nBlock number: %lu\n", block_number); + printf("DAG Size: %lu\n", p.full_size); + printf("Cache Size: %lu\n", p.cache_size); + + return Py_BuildValue("{sisi}", + "DAG Size", p.full_size, + "Cache Size", p.cache_size); +} + +static PyMethodDef CoreMethods[] = +{ + {"get_params", get_params, METH_VARARGS, "Gets the parameters for a given block"}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC +initcore(void) +{ + (void) Py_InitModule("core", CoreMethods); +} diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..12bf26e3 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,8 @@ +[nosetests] +verbosity=1 +detailed-errors=1 +with-coverage=1 +cover-package=nose +debug=nose.loader +pdb=1 +pdb-failures=1 diff --git a/setup.py b/setup.py new file mode 100755 index 00000000..090570ed --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +from distutils.core import setup, Extension + +pyethash_core = Extension('pyethash.core', + sources = [ + 'python-src/core.c', + 'libethash/util.c', + 'libethash/internal.c', + 'libethash/sha3.c' + ]) + +setup (name = 'pyethash', + version = '1.0', + description = 'Python wrappers for ethash, the ethereum proof of work hashing function', + ext_modules = [pyethash_core], + ) diff --git a/test/CMakeLists.txt b/test/c/CMakeLists.txt similarity index 95% rename from test/CMakeLists.txt rename to test/c/CMakeLists.txt index 431af6a9..2bac841e 100644 --- a/test/CMakeLists.txt +++ b/test/c/CMakeLists.txt @@ -4,7 +4,7 @@ ENDIF() IF( Boost_FOUND ) include_directories( ${Boost_INCLUDE_DIR} ) - include_directories(..) + include_directories(../..) link_directories ( ${Boost_LIBRARY_DIRS} ) file(GLOB HEADERS "*.h") diff --git a/test/test.cpp b/test/c/test.cpp similarity index 100% rename from test/test.cpp rename to test/c/test.cpp diff --git a/test/c/test.sh b/test/c/test.sh new file mode 100755 index 00000000..6d02d30f --- /dev/null +++ b/test/c/test.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Strict mode +set -e + +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" +done +TEST_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +rm -rf $TEST_DIR/build +mkdir -p $TEST_DIR/build +cd $TEST_DIR/build ; +cmake ../../.. > /dev/null +make Test +./test/c/Test diff --git a/test/python/.gitignore b/test/python/.gitignore new file mode 100644 index 00000000..c304fd61 --- /dev/null +++ b/test/python/.gitignore @@ -0,0 +1 @@ +python-virtual-env/ diff --git a/test/python/requirements.txt b/test/python/requirements.txt new file mode 100644 index 00000000..1f38dc3c --- /dev/null +++ b/test/python/requirements.txt @@ -0,0 +1,2 @@ +pyethereum==0.7.522 +nose==1.3.4 diff --git a/test/python/test.sh b/test/python/test.sh new file mode 100755 index 00000000..3d2b53c8 --- /dev/null +++ b/test/python/test.sh @@ -0,0 +1,20 @@ +#!/bin/bash -x + +# Strict mode +set -e + +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" +done +TEST_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +# rm -rf $TEST_DIR/python-virtual-env +# virtualenv --system-site-packages $TEST_DIR/python-virtual-env > /dev/null +source $TEST_DIR/python-virtual-env/bin/activate +# pip install -r $TEST_DIR/requirements.txt > /dev/null +pip install -e $TEST_DIR/../.. +cd $TEST_DIR +nosetests --with-doctest -v diff --git a/test/python/test_pyethash.py b/test/python/test_pyethash.py new file mode 100644 index 00000000..cb1f182f --- /dev/null +++ b/test/python/test_pyethash.py @@ -0,0 +1,9 @@ +import pyethash.core + +def test_get_params(): + block_num = 123456 + out = pyethash.core.get_params(block_num) + assert out != None + +if __name__ == '__main__': + test_me() diff --git a/test/test.sh b/test/test.sh index 2d37a897..4bb0ea37 100755 --- a/test/test.sh +++ b/test/test.sh @@ -1,34 +1,32 @@ -#!/bin/bash -x +#!/bin/bash -SOURCE="${BASH_SOURCE[0]}" +# Strict mode +set -e +SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCE="$(readlink "$SOURCE")" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" done - TEST_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" -echo "################# Testing CPP ##################" - -( rm -rf $TEST_DIR/../cpp-build ; - mkdir -p $TEST_DIR/../cpp-build ; - cd $TEST_DIR/../cpp-build ; - cmake .. ; - make Test ; - test/Test ) +echo -e "\n################# Testing JS ##################" +# TODO: Use mocha and real testing tools instead of rolling our own +cd $TEST_DIR/../libethash-js +node test.js -echo "################# Testing JS ##################" +echo -e "\n################# Testing Python ##################" +$TEST_DIR/python/test.sh -( cd $TEST_DIR/../libethash-js ; - ./test.js ) +echo -e "\n################# Testing C ##################" +$TEST_DIR/c/test.sh #echo "################# Testing Go ##################" -#( rm -rf $TEST_DIR/../go-build ; -# export GOPATH=$TEST_DIR/../go-build ; -# export PATH=$PATH:$GOPATH/bin ; -# cd $TEST_DIR/go ; -# go get -dv ; -# go get -v github.com/ethereum/ethash ; +#( rm -rf $TEST_DIR/../go-build +# export GOPATH=$TEST_DIR/../go-build +# export PATH=$PATH:$GOPATH/bin +# cd $TEST_DIR/go +# go get -dv +# go get -v github.com/ethereum/ethash # go test )