add StartMiner to niceapi, make sure it works on linux

This commit is contained in:
cryptonote-social 2020-08-11 12:02:35 -07:00
parent 2be8cb1efb
commit 0373e03d52
6 changed files with 117 additions and 30 deletions

View File

@ -26,4 +26,15 @@ func PoolLogin(
return resp.Code, C.CString(resp.Message)
}
//export StartMiner
func StartMiner(threads int, excludeHrStart, excludeHrEnd int) (code int, message *C.char) {
args := &csminer.StartMinerArgs{
Threads: threads,
ExcludeHourStart: excludeHrStart,
ExcludeHourEnd: excludeHrEnd,
}
resp := csminer.StartMiner(args)
return resp.Code, C.CString(resp.Message)
}
func main() {}

View File

@ -1,4 +1,3 @@
go build -o capi.a -buildmode=c-archive capi.go
gcc -c test.c -o test.o
gcc test.o capi.a -lpthread -o test
gcc -L../../rxlib/ test.o capi.a ../../rxlib/rxlib.cpp.o -lpthread -lrandomx -lstdc++ -lm -o test

View File

@ -52,3 +52,43 @@ pool_login_response pool_login(const pool_login_args *args) {
}
return response;
}
typedef struct start_miner_args {
// threads specifies the initial # of threads to mine with. Must be >=1
int threads;
// begin/end hours (24 time) of the time during the day where mining should be paused. Set both
// to 0 if there is no excluded range.
int exclude_hour_start;
int exclude_hour_end;
} start_miner_args;
typedef struct start_miner_response {
// code == 1: miner started successfully.
//
// code == 2: miner started successfully but hugepages could not be enabled, so mining may be
// slow. You can suggest to the user that a machine restart might help resolve this.
//
// code > 2: miner failed to start due to bad config, see details in message. For example, an
// invalid number of threads or invalid hour range may have been specified.
//
// code < 0: non-recoverable error, message will provide details. program should exit after
// showing message.
int code;
const char* message; // must be freed by the caller
} start_miner_response;
// call only after successful pool_login. This should only be called once!
start_miner_response start_miner(const start_miner_args *args) {
struct StartMiner_return r =
StartMiner((GoInt)args->threads, (GoInt)args->exclude_hour_start, (GoInt)args->exclude_hour_end);
start_miner_response response;
response.code = (int)r.r0;
if (strlen(r.r1) > 0) {
response.message = r.r1;
} else {
response.message = NULL;
}
return response;
}

View File

@ -4,41 +4,62 @@
#include "niceapi.h"
int main(int argc, char* argv[]) {
pool_login_args args;
args.username = "cryptonote-social";
// Pool login...
pool_login_args pl_args;
pl_args.username = "cryptonote-social";
if (argc > 1) {
printf("using arg for username: %s\n", argv[1]);
args.username = argv[1];
pl_args.username = argv[1];
}
args.rigid = NULL;
pl_args.rigid = NULL;
args.wallet = NULL;
pl_args.wallet = NULL;
if (argc > 2) {
printf("using arg for wallet: %s\n", argv[2]);
args.wallet = argv[2];
pl_args.wallet = argv[2];
}
args.agent = "Super Power Ultimate Miner (S.P.U.M.) v0.6.9";
pl_args.agent = "Super Power Ultimate Miner (S.P.U.M.) v0.6.9";
args.config = NULL;
pl_args.config = NULL;
pool_login_response r;
r = pool_login(&args);
if (r.code < 0) {
printf("Oh no, login failed: %s\n", r.message);
free((void*)r.message);
pool_login_response pl_resp = pool_login(&pl_args);
if (pl_resp.code < 0) {
printf("Oh no, login failed: %s\n", pl_resp.message);
free((void*)pl_resp.message);
return 1;
}
if (r.code > 1) {
printf("Pool server didn't like login info: %s\n", r.message);
free((void*)r.message);
if (pl_resp.code > 1) {
printf("Pool server didn't like login info: %s\n", pl_resp.message);
free((void*)pl_resp.message);
return 2;
}
printf("Successful login.\n");
if (r.message) {
printf(" Pool returned warning: %s\n", r.message);
free((void*)r.message);
if (pl_resp.message) {
printf(" Pool returned warning: %s\n", pl_resp.message);
free((void*)pl_resp.message);
}
// Starting the miner....
start_miner_args sm_args;
sm_args.threads = 1;
sm_args.exclude_hour_start = 0;
sm_args.exclude_hour_end = 0;
start_miner_response sm_resp = start_miner(&sm_args);
if (sm_resp.code > 2) {
printf("Bad config options specified: %s", sm_resp.message);
free((void*)sm_resp.message);
return 3;
}
if (sm_resp.code < 0) {
printf("Unrecoverable error: %s\n", sm_resp.message);
free((void*)sm_resp.message);
return 4;
}
if (sm_resp.code == 2) {
printf("Huge Pages could not be enabled -- mining may be slow. Consider restarting your machine and trying again.\n");
}
printf("Miner started.\n");
return 0;
}

View File

@ -145,6 +145,13 @@ type StartMinerResponse struct {
// successful. You should only call this method once.
func StartMiner(args *StartMinerArgs) *StartMinerResponse {
r := &StartMinerResponse{}
hr1 := args.ExcludeHourStart
hr2 := args.ExcludeHourEnd
if hr1 > 24 || hr1 < 0 || hr2 > 24 || hr1 < 0 {
r.Code = 3
r.Message = "exclude_hour_start and exclude_hour_end must each be between 0 and 24"
return r
}
// Make sure connection was established
clMutex.Lock()
alive := clientAlive
@ -164,15 +171,19 @@ func StartMiner(args *StartMinerArgs) *StartMinerResponse {
r.Message = "Invalid seed hash from pool server"
return r
}
if !rx.InitRX(newSeed, args.Threads, runtime.GOMAXPROCS(0)) {
// TODO: Handle case where it initialized but not w/ hugepages
code := rx.InitRX(newSeed, args.Threads, runtime.GOMAXPROCS(0))
if code < 0 {
crylog.Error("Failed to initialize RandomX")
r.Code = -3
r.Message = "Failed to initialize RandomX"
return r
}
if code == 2 {
r.Code = 2
} else {
r.Code = 1
}
go MiningLoop()
r.Code = 1
return r
}

View File

@ -13,22 +13,27 @@ package rx
import "C"
import (
// "cryptonote.social/crylog"
"github.com/cryptonote-social/csminer/crylog"
// "encoding/hex"
"unsafe"
)
// Call this every time the seed hash provided by the daemon changes before performing any hashing.
func InitRX(seedHash []byte, threads int, initThreads int) bool {
// return values:
// 1: success
// 2: success, but no huge pages.
// -1: unexpected failure
func InitRX(seedHash []byte, threads int, initThreads int) int {
if len(seedHash) == 0 {
return false
crylog.Error("Bad seed hash:", seedHash)
return -1
}
b := C.init_rxlib(
i := C.init_rxlib(
(*C.char)(unsafe.Pointer(&seedHash[0])),
(C.uint32_t)(len(seedHash)),
(C.int)(threads),
(C.int)(initThreads))
return bool(b)
return int(i)
}
func HashUntil(blob []byte, difficulty uint64, thread int, hash []byte, nonce []byte, stopper *uint32) int64 {