From 1efdd6bd2ebfc51837fb17435dcc4770fcac1f87 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Tue, 26 Feb 2019 14:12:04 -0800 Subject: [PATCH] Cleaned up code. --- cnutil/.gitignore | 7 ---- cnutil/CMakeLists.txt | 44 ---------------------- cnutil/cnutil.go | 28 -------------- cnutil/cnutil_test.go | 55 --------------------------- cnutil/src/cnutil.cpp | 27 -------------- cnutil/src/cnutil.h | 13 ------- hashing/.gitignore | 5 --- hashing/CMakeLists.txt | 12 ------ hashing/hashing.go | 23 ------------ hashing/hashing_test.go | 82 ----------------------------------------- hashing/src/hashing.c | 10 ----- hashing/src/hashing.h | 2 - rpc/rpc.go | 2 +- stratum/api.go | 4 +- stratum/blocks.go | 5 +-- stratum/handlers.go | 2 +- stratum/miner.go | 12 +++--- stratum/stratum.go | 6 +-- util/util.go | 6 +-- 19 files changed, 19 insertions(+), 326 deletions(-) delete mode 100644 cnutil/.gitignore delete mode 100644 cnutil/CMakeLists.txt delete mode 100644 cnutil/cnutil.go delete mode 100644 cnutil/cnutil_test.go delete mode 100644 cnutil/src/cnutil.cpp delete mode 100644 cnutil/src/cnutil.h delete mode 100644 hashing/.gitignore delete mode 100644 hashing/CMakeLists.txt delete mode 100644 hashing/hashing.go delete mode 100644 hashing/hashing_test.go delete mode 100644 hashing/src/hashing.c delete mode 100644 hashing/src/hashing.h diff --git a/cnutil/.gitignore b/cnutil/.gitignore deleted file mode 100644 index e9cfaaf..0000000 --- a/cnutil/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -Makefile -CmakeCache.txt -cmake_install.cmake -CMakeFiles -*.a -*.so -*.dylib diff --git a/cnutil/CMakeLists.txt b/cnutil/CMakeLists.txt deleted file mode 100644 index 2c82c2a..0000000 --- a/cnutil/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -set(CXXLIB "cnutil") - -find_package(Boost COMPONENTS thread system program_options date_time filesystem REQUIRED) - -# Flags -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE") - -include_directories(${Boost_INCLUDE_DIRS}) -include_directories("${MONERO_DIR}/contrib/epee/include") -include_directories("${MONERO_DIR}/src") -include_directories("${MONERO_DIR}/external/easylogging++") - -link_directories( - ${MONERO_DIR}/src/cryptonote_core - ${MONERO_DIR}/src/cryptonote_basic - ${MONERO_DIR}/src/crypto - ${MONERO_DIR}/src/common - ${MONERO_DIR}/src/ringct - ${MONERO_DIR}/src/blockchain_db - ${MONERO_DIR}/external/easylogging++ - ${MONERO_DIR}/contrib/epee/src -) - -# Build library -add_library(${CXXLIB} SHARED src/cnutil.cpp) - -target_link_libraries(${CXXLIB} - cryptonote_core - cryptonote_basic - cncrypto - common - ringct - blockchain_db - easylogging - epee -) - -target_link_libraries(${CXXLIB} - ${Boost_THREAD_LIBRARY} - ${Boost_SYSTEM_LIBRARY} - ${Boost_PROGRAM_OPTIONS_LIBRARY} - ${Boost_DATE_TIME_LIBRARY} - ${Boost_FILESYSTEM_LIBRARY} -) diff --git a/cnutil/cnutil.go b/cnutil/cnutil.go deleted file mode 100644 index 48e2cec..0000000 --- a/cnutil/cnutil.go +++ /dev/null @@ -1,28 +0,0 @@ -package cnutil - -// #cgo CFLAGS: -std=c11 -D_GNU_SOURCE -// #cgo LDFLAGS: -L${SRCDIR} -lcnutil -Wl,-rpath ${SRCDIR} -lstdc++ -// #include -// #include "src/cnutil.h" -import "C" -import "unsafe" - -func ConvertBlob(blob []byte) []byte { - output := make([]byte, 76) - out := (*C.char)(unsafe.Pointer(&output[0])) - - input := (*C.char)(unsafe.Pointer(&blob[0])) - - size := (C.uint32_t)(len(blob)) - C.convert_blob(input, size, out) - return output -} - -func ValidateAddress(addr string) bool { - input := C.CString(addr) - defer C.free(unsafe.Pointer(input)) - - size := (C.uint32_t)(len(addr)) - result := C.validate_address(input, size) - return (bool)(result) -} diff --git a/cnutil/cnutil_test.go b/cnutil/cnutil_test.go deleted file mode 100644 index ff1ef0e..0000000 --- a/cnutil/cnutil_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package cnutil - -import ( - "encoding/hex" - "testing" -) - -func TestConvertBlob(t *testing.T) { - hashBytes, _ := hex.DecodeString("0100a5d1fca9057dff46d140d453a672437ba0ec7d6a74bc5fa0391f8a918e41fd7ba2cf6fc1af000000000183811401ffc7801405889ec5dc2402e0fe0db63a8e532a7b988e0c32a764e5e8d64d7efac9bc9d24ce32b0984ab93980b09dc2df0102ccd38432501a9182ccc5b44cb47abdddbc9a6321cd581f5f07a7a9195795d5c68080dd9da41702f6e944ee4c6e1eeaed1fa6a3c2480a410e959c6e823b96dad54d31fe223cc0fd80c0a8ca9a3a0286d0e3411670e4c2abe8492c695c66d8262660ee88a0b14a2b03c9180fb6f0d480c0caf384a30202aec5c9b7efe841dd821476e0e06217be13a4c85a83efcf9576314d60130e02e72b0150526f7a381cec33e5827c1848dd80e6eac4b262304ea06b3a43303a4631df28020800000000018ba82000") - expectedResult, _ := hex.DecodeString("0100a5d1fca9057dff46d140d453a672437ba0ec7d6a74bc5fa0391f8a918e41fd7ba2cf6fc1af00000000e81cb2bf0d2c5054a49bda094c39cb263a9565b9b81cf4c4f848292040419f4a01") - output := ConvertBlob(hashBytes) - - if len(output) != 76 { - t.Error("Invalid result length") - } - ok := true - for i := range output { - if expectedResult[i] != output[i] { - ok = false - break - } - } - if !ok { - t.Error("Invalid result") - } -} - -func TestDecodeAddress(t *testing.T) { - addy := "45pyCXYn2UBVUmCFjgKr7LF8hCTeGwucWJ2xni7qrbj6GgAZBFY6tANarozZx9DaQqHyuR1AL8HJbRmqwLhUaDpKJW4hqS1" - if !ValidateAddress(addy) { - t.Error("Valid address") - } - - addy = "46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em" - if !ValidateAddress(addy) { - t.Error("Valid address") - } - - if ValidateAddress("OMG") { - t.Error("Invalid address") - } -} - -func BenchmarkConvertBlob(b *testing.B) { - for i := 0; i < b.N; i++ { - hashBytes, _ := hex.DecodeString("0100a5d1fca9057dff46d140d453a672437ba0ec7d6a74bc5fa0391f8a918e41fd7ba2cf6fc1af000000000183811401ffc7801405889ec5dc2402e0fe0db63a8e532a7b988e0c32a764e5e8d64d7efac9bc9d24ce32b0984ab93980b09dc2df0102ccd38432501a9182ccc5b44cb47abdddbc9a6321cd581f5f07a7a9195795d5c68080dd9da41702f6e944ee4c6e1eeaed1fa6a3c2480a410e959c6e823b96dad54d31fe223cc0fd80c0a8ca9a3a0286d0e3411670e4c2abe8492c695c66d8262660ee88a0b14a2b03c9180fb6f0d480c0caf384a30202aec5c9b7efe841dd821476e0e06217be13a4c85a83efcf9576314d60130e02e72b0150526f7a381cec33e5827c1848dd80e6eac4b262304ea06b3a43303a4631df28020800000000018ba82000") - ConvertBlob(hashBytes) - } -} - -func BenchmarkDecodeAddress(b *testing.B) { - for i := 0; i < b.N; i++ { - ValidateAddress("45pyCXYn2UBVUmCFjgKr7LF8hCTeGwucWJ2xni7qrbj6GgAZBFY6tANarozZx9DaQqHyuR1AL8HJbRmqwLhUaDpKJW4hqS1") - } -} diff --git a/cnutil/src/cnutil.cpp b/cnutil/src/cnutil.cpp deleted file mode 100644 index 462d2db..0000000 --- a/cnutil/src/cnutil.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "cryptonote_basic/cryptonote_format_utils.h" -#include "common/base58.h" - -using namespace cryptonote; - -extern "C" uint32_t convert_blob(const char *blob, size_t len, char *out) { - std::string input = std::string(blob, len); - std::string output = ""; - - block b = AUTO_VAL_INIT(b); - if (!parse_and_validate_block_from_blob(input, b)) { - return 0; - } - - output = get_block_hashing_blob(b); - output.copy(out, output.length(), 0); - return output.length(); -} - -extern "C" bool validate_address(const char *addr, size_t len) { - std::string input = std::string(addr, len); - std::string output = ""; - uint64_t prefix; - return tools::base58::decode_addr(addr, prefix, output); -} diff --git a/cnutil/src/cnutil.h b/cnutil/src/cnutil.h deleted file mode 100644 index b8cafbf..0000000 --- a/cnutil/src/cnutil.h +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "stdbool.h" - -#ifdef __cplusplus -extern "C" { -#endif - -uint32_t convert_blob(const char *blob, uint32_t len, char *out); -bool validate_address(const char *addr, uint32_t len); - -#ifdef __cplusplus -} -#endif diff --git a/hashing/.gitignore b/hashing/.gitignore deleted file mode 100644 index d9d63b4..0000000 --- a/hashing/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -Makefile -CmakeCache.txt -cmake_install.cmake -CMakeFiles -libhashing.a diff --git a/hashing/CMakeLists.txt b/hashing/CMakeLists.txt deleted file mode 100644 index 5a7ebb1..0000000 --- a/hashing/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -set(LIB "hashing") - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE") - -include_directories("${MONERO_DIR}/contrib/epee/include") -include_directories("${MONERO_DIR}/src") - -link_directories(${MONERO_DIR}/src/crypto) - -add_library(${LIB} SHARED src/hashing.c) - -target_link_libraries(${LIB} cncrypto) diff --git a/hashing/hashing.go b/hashing/hashing.go deleted file mode 100644 index 412eed5..0000000 --- a/hashing/hashing.go +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index 39aff74..0000000 --- a/hashing/hashing_test.go +++ /dev/null @@ -1,82 +0,0 @@ -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 deleted file mode 100644 index cec2072..0000000 --- a/hashing/src/hashing.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "crypto/hash-ops.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 deleted file mode 100644 index 0e699c1..0000000 --- a/hashing/src/hashing.h +++ /dev/null @@ -1,2 +0,0 @@ -void cryptonight_hash(const char* input, char* output, uint32_t len); -void cryptonight_fast_hash(const char* input, char* output, uint32_t len); diff --git a/rpc/rpc.go b/rpc/rpc.go index 338fdf0..572ba10 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -11,7 +11,7 @@ import ( "sync/atomic" "time" - "github.com/sammy007/monero-stratum/pool" + "../pool" ) type RPCClient struct { diff --git a/stratum/api.go b/stratum/api.go index 08806c8..ad67456 100644 --- a/stratum/api.go +++ b/stratum/api.go @@ -6,8 +6,8 @@ import ( "sync/atomic" "time" - "github.com/sammy007/monero-stratum/rpc" - "github.com/sammy007/monero-stratum/util" + "../rpc" + "../util" ) func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) { diff --git a/stratum/blocks.go b/stratum/blocks.go index 9222b34..a109ee0 100644 --- a/stratum/blocks.go +++ b/stratum/blocks.go @@ -6,8 +6,6 @@ import ( "encoding/hex" "log" "math/big" - - "github.com/sammy007/monero-stratum/cnutil" ) type BlockTemplate struct { @@ -27,7 +25,8 @@ func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string { copy(blobBuff, b.buffer) copy(blobBuff[b.reservedOffset+4:b.reservedOffset+7], instanceId) copy(blobBuff[b.reservedOffset:], extraBuff.Bytes()) - blob := cnutil.ConvertBlob(blobBuff) + //blob := cnutil.ConvertBlob(blobBuff) + blob := nil //TODO: fix this! return hex.EncodeToString(blob) } diff --git a/stratum/handlers.go b/stratum/handlers.go index ebf4880..7632fc5 100644 --- a/stratum/handlers.go +++ b/stratum/handlers.go @@ -6,7 +6,7 @@ import ( "strings" "sync/atomic" - "github.com/sammy007/monero-stratum/util" + "../util" ) var noncePattern *regexp.Regexp diff --git a/stratum/miner.go b/stratum/miner.go index 45bc5bc..5818b06 100644 --- a/stratum/miner.go +++ b/stratum/miner.go @@ -10,9 +10,7 @@ import ( "sync/atomic" "time" - "github.com/sammy007/monero-stratum/cnutil" - "github.com/sammy007/monero-stratum/hashing" - "github.com/sammy007/monero-stratum/util" + "../util" ) type Job struct { @@ -151,7 +149,9 @@ func (m *Miner) processShare(s *StratumServer, cs *Session, job *Job, t *BlockTe if s.config.BypassShareValidation { hashBytes, _ = hex.DecodeString(result) } else { - convertedBlob = cnutil.ConvertBlob(shareBuff) + //convertedBlob = cnutil.ConvertBlob(shareBuff) + // TODO: fix this. + convertedBlob = nill hashBytes = hashing.Hash(convertedBlob, false) } @@ -177,7 +177,9 @@ func (m *Miner) processShare(s *StratumServer, cs *Session, job *Job, t *BlockTe log.Printf("Block rejected at height %d: %v", t.height, err) } else { if len(convertedBlob) == 0 { - convertedBlob = cnutil.ConvertBlob(shareBuff) + //convertedBlob = cnutil.ConvertBlob(shareBuff) + // TODO: fix this + convertedBlob = nil } blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob)) now := util.MakeTimestamp() diff --git a/stratum/stratum.go b/stratum/stratum.go index 7cef82a..b491a80 100644 --- a/stratum/stratum.go +++ b/stratum/stratum.go @@ -13,9 +13,9 @@ import ( "sync/atomic" "time" - "github.com/sammy007/monero-stratum/pool" - "github.com/sammy007/monero-stratum/rpc" - "github.com/sammy007/monero-stratum/util" + "../pool" + "../rpc" + "../util" ) type StratumServer struct { diff --git a/util/util.go b/util/util.go index b4bfdc5..c682ca8 100644 --- a/util/util.go +++ b/util/util.go @@ -5,8 +5,6 @@ import ( "math/big" "time" "unicode/utf8" - - "github.com/sammy007/monero-stratum/cnutil" ) var Diff1 = StringToBig("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") @@ -51,7 +49,9 @@ func ValidateAddress(addy string, poolAddy string) bool { if prefix != poolPrefix { return false } - return cnutil.ValidateAddress(addy) + //return cnutil.ValidateAddress(addy) + // TODO: fix this + return true } func reverse(src []byte) []byte {