From 3142940719e01288bcb2ec6381806bcd2ef5bacc Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Tue, 26 Feb 2019 15:11:58 -0800 Subject: [PATCH] Added C files. --- CMakeLists.txt | 11 +----- build/env.sh | 8 ++-- hashing/.gitignore | 5 +++ hashing/CMakeLists.txt | 6 +++ hashing/hashing.go | 23 ++++++++++++ hashing/hashing_test.go | 82 +++++++++++++++++++++++++++++++++++++++++ hashing/src/hashing.c | 13 +++++++ hashing/src/hashing.h | 3 ++ main.go | 4 +- stratum/miner.go | 1 + 10 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 hashing/.gitignore create mode 100644 hashing/CMakeLists.txt create mode 100644 hashing/hashing.go create mode 100644 hashing/hashing_test.go create mode 100644 hashing/src/hashing.c create mode 100644 hashing/src/hashing.h diff --git a/CMakeLists.txt b/CMakeLists.txt index da34151..8380076 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,11 @@ cmake_minimum_required (VERSION 2.8.11) project(pool) -if (DEFINED ENV{MONERO_DIR}) - get_filename_component(MONERO_DIR $ENV{MONERO_DIR} ABSOLUTE) - message("Using Monero source from env ${MONERO_DIR}") -else() - get_filename_component(MONERO_DIR "${CMAKE_SOURCE_DIR}/../monero" ABSOLUTE) - message("Monero surce directory is not defined, using default ${MONERO_DIR}") -endif() - add_subdirectory(hashing) -add_subdirectory(cnutil) add_custom_command( OUTPUT build/bin COMMAND build/env.sh go get -v ./... ) -add_custom_target(build ALL DEPENDS hashing cnutil ${CMAKE_CURRENT_BINARY_DIR}/build/bin) +add_custom_target(build ALL DEPENDS hashing ${CMAKE_CURRENT_BINARY_DIR}/build/bin) diff --git a/build/env.sh b/build/env.sh index 903c0fe..7e7e557 100755 --- a/build/env.sh +++ b/build/env.sh @@ -10,8 +10,8 @@ fi # Create fake Go workspace if it doesn't exist yet. workspace="$PWD/build/_workspace" root="$PWD" -ethdir="$workspace/src/github.com/sammy007" -if [ ! -L "$ethdir/monero-stratum" ]; then +ethdir="$workspace/src/github.com/kevacoin-project" +if [ ! -L "$ethdir/keva-stratum" ]; then mkdir -p "$ethdir" cd "$ethdir" ln -s ../../../../../. monero-stratum @@ -25,8 +25,8 @@ GOBIN="$PWD/build/bin" export GOPATH GOBIN # Run the command inside the workspace. -cd "$ethdir/monero-stratum" -PWD="$ethdir/monero-stratum" +cd "$ethdir/keva-stratum" +PWD="$ethdir/keva-stratum" # Launch the arguments with the configured environment. exec "$@" diff --git a/hashing/.gitignore b/hashing/.gitignore new file mode 100644 index 0000000..d9d63b4 --- /dev/null +++ b/hashing/.gitignore @@ -0,0 +1,5 @@ +Makefile +CmakeCache.txt +cmake_install.cmake +CMakeFiles +libhashing.a diff --git a/hashing/CMakeLists.txt b/hashing/CMakeLists.txt new file mode 100644 index 0000000..c776b80 --- /dev/null +++ b/hashing/CMakeLists.txt @@ -0,0 +1,6 @@ +set(LIB "hashing") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE") + +add_library(${LIB} src/hashing.c) + diff --git a/hashing/hashing.go b/hashing/hashing.go new file mode 100644 index 0000000..412eed5 --- /dev/null +++ b/hashing/hashing.go @@ -0,0 +1,23 @@ +package hashing + +// #cgo CFLAGS: -std=c11 -D_GNU_SOURCE +// #cgo LDFLAGS: -L${SRCDIR} -lhashing -Wl,-rpath ${SRCDIR} -lstdc++ +// #include +// #include +// #include "src/hashing.h" +import "C" +import "unsafe" + +func Hash(blob []byte, fast bool) []byte { + output := make([]byte, 32) + if fast { + C.cryptonight_fast_hash((*C.char)(unsafe.Pointer(&blob[0])), (*C.char)(unsafe.Pointer(&output[0])), (C.uint32_t)(len(blob))) + } else { + C.cryptonight_hash((*C.char)(unsafe.Pointer(&blob[0])), (*C.char)(unsafe.Pointer(&output[0])), (C.uint32_t)(len(blob))) + } + return output +} + +func FastHash(blob []byte) []byte { + return Hash(append([]byte{byte(len(blob))}, blob...), true) +} diff --git a/hashing/hashing_test.go b/hashing/hashing_test.go new file mode 100644 index 0000000..39aff74 --- /dev/null +++ b/hashing/hashing_test.go @@ -0,0 +1,82 @@ +package hashing + +import "testing" +import "log" +import "encoding/hex" + +func TestHash(t *testing.T) { + blob, _ := hex.DecodeString("01009091e4aa05ff5fe4801727ed0c1b8b339e1a0054d75568fec6ba9c4346e88b10d59edbf6858b2b00008a63b2865b65b84d28bb31feb057b16a21e2eda4bf6cc6377e3310af04debe4a01") + hashBytes := Hash(blob, false) + hash := hex.EncodeToString(hashBytes) + log.Println(hash) + + expectedHash := "a70a96f64a266f0f59e4f67c4a92f24fe8237c1349f377fd2720c9e1f2970400" + + if hash != expectedHash { + t.Error("Invalid hash") + } +} + +func TestHash_fast(t *testing.T) { + blob, _ := hex.DecodeString("01009091e4aa05ff5fe4801727ed0c1b8b339e1a0054d75568fec6ba9c4346e88b10d59edbf6858b2b00008a63b2865b65b84d28bb31feb057b16a21e2eda4bf6cc6377e3310af04debe4a01") + hashBytes := Hash(blob, true) + hash := hex.EncodeToString(hashBytes) + log.Println(hash) + + expectedHash := "7591f4d8ff9d86ea44873e89a5fb6f380f4410be6206030010567ac9d0d4b0e1" + + if hash != expectedHash { + t.Error("Invalid fast hash") + } +} + +func TestFastHash(t *testing.T) { + blob, _ := hex.DecodeString("01009091e4aa05ff5fe4801727ed0c1b8b339e1a0054d75568fec6ba9c4346e88b10d59edbf6858b2b00008a63b2865b65b84d28bb31feb057b16a21e2eda4bf6cc6377e3310af04debe4a01") + hashBytes := FastHash(blob) + hash := hex.EncodeToString(hashBytes) + log.Println(hash) + + expectedHash := "8706c697d9fc8a48b14ea93a31c6f0750c48683e585ec1a534e9c57c97193fa6" + + if hash != expectedHash { + t.Error("Invalid fast hash") + } +} + +func BenchmarkHash(b *testing.B) { + blob, _ := hex.DecodeString("0100b69bb3aa050a3106491f858f8646d3a8d13fd9924403bf07af95e6e7cc9e4ad105d76da27241565555866b1baa9db8f027cf57cd45d6835c11287b210d9ddb407deda565f8112e19e501") + b.ResetTimer() + + for i := 0; i < b.N; i++ { + Hash(blob, false) + } +} + +func BenchmarkHashParallel(b *testing.B) { + blob, _ := hex.DecodeString("0100b69bb3aa050a3106491f858f8646d3a8d13fd9924403bf07af95e6e7cc9e4ad105d76da27241565555866b1baa9db8f027cf57cd45d6835c11287b210d9ddb407deda565f8112e19e501") + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + Hash(blob, false) + } + }) +} + +func BenchmarkHash_fast(b *testing.B) { + blob, _ := hex.DecodeString("0100b69bb3aa050a3106491f858f8646d3a8d13fd9924403bf07af95e6e7cc9e4ad105d76da27241565555866b1baa9db8f027cf57cd45d6835c11287b210d9ddb407deda565f8112e19e501") + b.ResetTimer() + + for i := 0; i < b.N; i++ { + Hash(blob, true) + } +} + +func BenchmarkFastHash(b *testing.B) { + blob, _ := hex.DecodeString("0100b69bb3aa050a3106491f858f8646d3a8d13fd9924403bf07af95e6e7cc9e4ad105d76da27241565555866b1baa9db8f027cf57cd45d6835c11287b210d9ddb407deda565f8112e19e501") + b.ResetTimer() + + for i := 0; i < b.N; i++ { + FastHash(blob) + } +} diff --git a/hashing/src/hashing.c b/hashing/src/hashing.c new file mode 100644 index 0000000..e9be005 --- /dev/null +++ b/hashing/src/hashing.c @@ -0,0 +1,13 @@ +#include +#include +#include "hashing.h" + +void cryptonight_hash(const char* input, char* output, uint32_t len) { + //const int variant = input[0] >= 7 ? input[0] - 6 : 0; + //cn_slow_hash(input, len, output, variant, 0); +} + +void cryptonight_fast_hash(const char* input, char* output, uint32_t len) { + //cn_fast_hash(input, len, output); +} + diff --git a/hashing/src/hashing.h b/hashing/src/hashing.h new file mode 100644 index 0000000..7786dba --- /dev/null +++ b/hashing/src/hashing.h @@ -0,0 +1,3 @@ + +void cryptonight_hash(const char* input, char* output, uint32_t len); +void cryptonight_fast_hash(const char* input, char* output, uint32_t len); \ No newline at end of file diff --git a/main.go b/main.go index 71abda5..d552f99 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,8 @@ import ( "runtime" "time" - "github.com/sammy007/monero-stratum/pool" - "github.com/sammy007/monero-stratum/stratum" + "./pool" + "./stratum" "github.com/goji/httpauth" "github.com/gorilla/mux" diff --git a/stratum/miner.go b/stratum/miner.go index 5818b06..c857924 100644 --- a/stratum/miner.go +++ b/stratum/miner.go @@ -10,6 +10,7 @@ import ( "sync/atomic" "time" + "../hashing" "../util" )