2020-06-20 01:21:08 +03:00
|
|
|
// Copyright 2020 cryptonote.social. All rights reserved. Use of this source code is governed by
|
|
|
|
// the license found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package rx provides Go access to various randomx library methods.
|
|
|
|
package rx
|
|
|
|
|
2021-03-14 20:50:29 +03:00
|
|
|
// #cgo CFLAGS: -std=c11 -D_GNU_SOURCE -m64 -O3 -I${SRCDIR}/../../RandomX/rxlib/
|
|
|
|
// #cgo LDFLAGS: -L${SRCDIR}/../../RandomX/rxlib/ -Wl,-rpath,$ORIGIN ${SRCDIR}/../../RandomX/rxlib/rxlib.cpp.o -lrandomx -lstdc++ -lm
|
2020-06-20 01:21:08 +03:00
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
2020-07-27 21:26:10 +03:00
|
|
|
#include "rxlib.h"
|
2020-06-20 01:21:08 +03:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2020-08-11 22:02:35 +03:00
|
|
|
"github.com/cryptonote-social/csminer/crylog"
|
2020-06-20 01:21:08 +03:00
|
|
|
// "encoding/hex"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Call this every time the seed hash provided by the daemon changes before performing any hashing.
|
2020-08-12 22:01:50 +03:00
|
|
|
// Only call when all existing threads are stopped. Returns false if an unrecoverable error
|
|
|
|
// occurred.
|
|
|
|
func SeedRX(seedHash []byte, initThreads int) bool {
|
2020-06-20 01:21:08 +03:00
|
|
|
if len(seedHash) == 0 {
|
2020-08-11 22:02:35 +03:00
|
|
|
crylog.Error("Bad seed hash:", seedHash)
|
2020-08-12 22:01:50 +03:00
|
|
|
return false
|
2020-06-20 01:21:08 +03:00
|
|
|
}
|
2020-08-12 22:01:50 +03:00
|
|
|
b := C.seed_rxlib(
|
2020-06-20 01:21:08 +03:00
|
|
|
(*C.char)(unsafe.Pointer(&seedHash[0])),
|
|
|
|
(C.uint32_t)(len(seedHash)),
|
|
|
|
(C.int)(initThreads))
|
2020-08-12 22:01:50 +03:00
|
|
|
return bool(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call this once.
|
|
|
|
// return values:
|
|
|
|
// 1: success
|
|
|
|
// 2: success, but no huge pages.
|
|
|
|
// -1: unexpected failure
|
|
|
|
func InitRX(threads int) int {
|
|
|
|
i := C.init_rxlib((C.int)(threads))
|
2020-08-11 22:02:35 +03:00
|
|
|
return int(i)
|
2020-06-20 01:21:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func HashUntil(blob []byte, difficulty uint64, thread int, hash []byte, nonce []byte, stopper *uint32) int64 {
|
|
|
|
res := C.rx_hash_until(
|
|
|
|
(*C.char)(unsafe.Pointer(&blob[0])),
|
|
|
|
(C.uint32_t)(len(blob)),
|
|
|
|
(C.uint64_t)(difficulty),
|
|
|
|
(C.int)(thread),
|
|
|
|
(*C.char)(unsafe.Pointer(&hash[0])),
|
|
|
|
(*C.char)(unsafe.Pointer(&nonce[0])),
|
|
|
|
(*C.uint32_t)(unsafe.Pointer(stopper)))
|
|
|
|
return int64(res)
|
|
|
|
}
|
2020-08-04 19:44:17 +03:00
|
|
|
|
|
|
|
// only call when all existing threads are stopped
|
|
|
|
func AddThread() int {
|
|
|
|
res := C.rx_add_thread()
|
|
|
|
return int(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// only call when all existing threads are stopped
|
|
|
|
func RemoveThread() int {
|
|
|
|
res := C.rx_remove_thread()
|
|
|
|
return int(res)
|
|
|
|
}
|