Browse Source

Cleaned up code.

pool
Jianping Wu 6 years ago
parent
commit
1efdd6bd2e
  1. 7
      cnutil/.gitignore
  2. 44
      cnutil/CMakeLists.txt
  3. 28
      cnutil/cnutil.go
  4. 55
      cnutil/cnutil_test.go
  5. 27
      cnutil/src/cnutil.cpp
  6. 13
      cnutil/src/cnutil.h
  7. 5
      hashing/.gitignore
  8. 12
      hashing/CMakeLists.txt
  9. 23
      hashing/hashing.go
  10. 82
      hashing/hashing_test.go
  11. 10
      hashing/src/hashing.c
  12. 2
      hashing/src/hashing.h
  13. 2
      rpc/rpc.go
  14. 4
      stratum/api.go
  15. 5
      stratum/blocks.go
  16. 2
      stratum/handlers.go
  17. 12
      stratum/miner.go
  18. 6
      stratum/stratum.go
  19. 6
      util/util.go

7
cnutil/.gitignore vendored

@ -1,7 +0,0 @@
Makefile
CmakeCache.txt
cmake_install.cmake
CMakeFiles
*.a
*.so
*.dylib

44
cnutil/CMakeLists.txt

@ -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}
)

28
cnutil/cnutil.go

@ -1,28 +0,0 @@
package cnutil
// #cgo CFLAGS: -std=c11 -D_GNU_SOURCE
// #cgo LDFLAGS: -L${SRCDIR} -lcnutil -Wl,-rpath ${SRCDIR} -lstdc++
// #include <stdlib.h>
// #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)
}

55
cnutil/cnutil_test.go

@ -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")
}
}

27
cnutil/src/cnutil.cpp

@ -1,27 +0,0 @@
#include <stdint.h>
#include <string>
#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);
}

13
cnutil/src/cnutil.h

@ -1,13 +0,0 @@
#include <stdint.h>
#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

5
hashing/.gitignore vendored

@ -1,5 +0,0 @@
Makefile
CmakeCache.txt
cmake_install.cmake
CMakeFiles
libhashing.a

12
hashing/CMakeLists.txt

@ -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)

23
hashing/hashing.go

@ -1,23 +0,0 @@
package hashing
// #cgo CFLAGS: -std=c11 -D_GNU_SOURCE
// #cgo LDFLAGS: -L${SRCDIR} -lhashing -Wl,-rpath ${SRCDIR} -lstdc++
// #include <stdlib.h>
// #include <stdint.h>
// #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)
}

82
hashing/hashing_test.go

@ -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)
}
}

10
hashing/src/hashing.c

@ -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);
}

2
hashing/src/hashing.h

@ -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);

2
rpc/rpc.go

@ -11,7 +11,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sammy007/monero-stratum/pool" "../pool"
) )
type RPCClient struct { type RPCClient struct {

4
stratum/api.go

@ -6,8 +6,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sammy007/monero-stratum/rpc" "../rpc"
"github.com/sammy007/monero-stratum/util" "../util"
) )
func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) { func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) {

5
stratum/blocks.go

@ -6,8 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"log" "log"
"math/big" "math/big"
"github.com/sammy007/monero-stratum/cnutil"
) )
type BlockTemplate struct { type BlockTemplate struct {
@ -27,7 +25,8 @@ func (b *BlockTemplate) nextBlob(extraNonce uint32, instanceId []byte) string {
copy(blobBuff, b.buffer) copy(blobBuff, b.buffer)
copy(blobBuff[b.reservedOffset+4:b.reservedOffset+7], instanceId) copy(blobBuff[b.reservedOffset+4:b.reservedOffset+7], instanceId)
copy(blobBuff[b.reservedOffset:], extraBuff.Bytes()) copy(blobBuff[b.reservedOffset:], extraBuff.Bytes())
blob := cnutil.ConvertBlob(blobBuff) //blob := cnutil.ConvertBlob(blobBuff)
blob := nil //TODO: fix this!
return hex.EncodeToString(blob) return hex.EncodeToString(blob)
} }

2
stratum/handlers.go

@ -6,7 +6,7 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"github.com/sammy007/monero-stratum/util" "../util"
) )
var noncePattern *regexp.Regexp var noncePattern *regexp.Regexp

12
stratum/miner.go

@ -10,9 +10,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sammy007/monero-stratum/cnutil" "../util"
"github.com/sammy007/monero-stratum/hashing"
"github.com/sammy007/monero-stratum/util"
) )
type Job struct { type Job struct {
@ -151,7 +149,9 @@ func (m *Miner) processShare(s *StratumServer, cs *Session, job *Job, t *BlockTe
if s.config.BypassShareValidation { if s.config.BypassShareValidation {
hashBytes, _ = hex.DecodeString(result) hashBytes, _ = hex.DecodeString(result)
} else { } else {
convertedBlob = cnutil.ConvertBlob(shareBuff) //convertedBlob = cnutil.ConvertBlob(shareBuff)
// TODO: fix this.
convertedBlob = nill
hashBytes = hashing.Hash(convertedBlob, false) 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) log.Printf("Block rejected at height %d: %v", t.height, err)
} else { } else {
if len(convertedBlob) == 0 { if len(convertedBlob) == 0 {
convertedBlob = cnutil.ConvertBlob(shareBuff) //convertedBlob = cnutil.ConvertBlob(shareBuff)
// TODO: fix this
convertedBlob = nil
} }
blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob)) blockFastHash := hex.EncodeToString(hashing.FastHash(convertedBlob))
now := util.MakeTimestamp() now := util.MakeTimestamp()

6
stratum/stratum.go

@ -13,9 +13,9 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sammy007/monero-stratum/pool" "../pool"
"github.com/sammy007/monero-stratum/rpc" "../rpc"
"github.com/sammy007/monero-stratum/util" "../util"
) )
type StratumServer struct { type StratumServer struct {

6
util/util.go

@ -5,8 +5,6 @@ import (
"math/big" "math/big"
"time" "time"
"unicode/utf8" "unicode/utf8"
"github.com/sammy007/monero-stratum/cnutil"
) )
var Diff1 = StringToBig("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") var Diff1 = StringToBig("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
@ -51,7 +49,9 @@ func ValidateAddress(addy string, poolAddy string) bool {
if prefix != poolPrefix { if prefix != poolPrefix {
return false return false
} }
return cnutil.ValidateAddress(addy) //return cnutil.ValidateAddress(addy)
// TODO: fix this
return true
} }
func reverse(src []byte) []byte { func reverse(src []byte) []byte {

Loading…
Cancel
Save