-
Notifications
You must be signed in to change notification settings - Fork 9
/
coind_submit.cpp
147 lines (106 loc) · 3.32 KB
/
coind_submit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "stratum.h"
bool coind_submitwork(YAAMP_COIND *coind, const char *block)
{
int paramlen = strlen(block);
char *params = (char *)malloc(paramlen+1024);
if(!params) {
debuglog("%s: OOM!\n", __func__);
return false;
}
sprintf(params, "[\"%s\"]", block);
json_value *json = rpc_call(&coind->rpc, "getwork", params);
if(!json) {
debuglog("%s: retry\n", __func__);
usleep(500*YAAMP_MS);
json = rpc_call(&coind->rpc, "getwork", params);
}
free(params);
if(!json) {
debuglog("%s: error, no answer\n", __func__);
return false;
}
json_value *json_res = json_get_object(json, "result");
bool b = json_res && json_res->type == json_boolean && json_res->u.boolean;
json_value_free(json_res);
return b;
}
bool coind_submitblock(YAAMP_COIND *coind, const char *block)
{
int paramlen = strlen(block);
char *params = (char *)malloc(paramlen+1024);
if(!params) return false;
sprintf(params, "[\"%s\"]", block);
json_value *json = rpc_call(&coind->rpc, "submitblock", params);
free(params);
if(!json) return false;
json_value *json_error = json_get_object(json, "error");
if(json_error && json_error->type != json_null)
{
const char *p = json_get_string(json_error, "message");
if(p) stratumlog("ERROR %s %s\n", coind->name, p);
// job_reset();
json_value_free(json);
return false;
}
json_value *json_result = json_get_object(json, "result");
bool b = json_result && json_result->type == json_null;
json_value_free(json);
return b;
}
bool coind_submitblocktemplate(YAAMP_COIND *coind, const char *block)
{
int paramlen = strlen(block);
char *params = (char *)malloc(paramlen+1024);
if(!params) return false;
sprintf(params, "[{\"mode\": \"submit\", \"data\": \"%s\"}]", block);
json_value *json = rpc_call(&coind->rpc, "getblocktemplate", params);
free(params);
if(!json) return false;
json_value *json_error = json_get_object(json, "error");
if(json_error && json_error->type != json_null)
{
const char *p = json_get_string(json_error, "message");
if(p) stratumlog("ERROR %s %s\n", coind->name, p);
// job_reset();
json_value_free(json);
return false;
}
json_value *json_result = json_get_object(json, "result");
bool b = json_result && json_result->type == json_null;
json_value_free(json);
return b;
}
bool coind_submit(YAAMP_COIND *coind, const char *block)
{
bool b;
if(coind->usegetwork) // DCR
b = coind_submitwork(coind, block);
else if(coind->hassubmitblock)
b = coind_submitblock(coind, block);
else
b = coind_submitblocktemplate(coind, block);
return b;
}
bool coind_submitgetauxblock(YAAMP_COIND *coind, const char *hash, const char *block)
{
int paramlen = strlen(block);
char *params = (char *)malloc(paramlen+1024);
if(!params) return false;
sprintf(params, "[\"%s\",\"%s\"]", hash, block);
json_value *json = rpc_call(&coind->rpc, "getauxblock", params);
free(params);
if(!json) return false;
json_value *json_error = json_get_object(json, "error");
if(json_error && json_error->type != json_null)
{
const char *p = json_get_string(json_error, "message");
if(p) stratumlog("ERROR %s %s\n", coind->name, p);
// job_reset();
json_value_free(json);
return false;
}
json_value *json_result = json_get_object(json, "result");
bool b = json_result && json_result->type == json_boolean && json_result->u.boolean;
json_value_free(json);
return b;
}