Skip to content

Commit

Permalink
Add blktemplate_t.has_cbvalue boolean to differentiate between cbvalu…
Browse files Browse the repository at this point in the history
…e 0 being unknown or zero
  • Loading branch information
luke-jr committed Feb 17, 2018
1 parent c1392f1 commit c7388aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
6 changes: 4 additions & 2 deletions blkmaker.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const s
return 0;
}

if (!tmpl->cbvalue) {
if (!tmpl->has_cbvalue) {
// TODO: Figure it out from the existing cbtxn
return 0;
}
Expand Down Expand Up @@ -798,7 +798,9 @@ bool blkmk_get_mdata(blktemplate_t * const tmpl, void * const buf, const size_t
free(*out_cbtxn);
return false;
}
memcpy(*out_branches, tmpl->_mrklbranch, branches_bytesz);
if (branches_bytesz) {
memcpy(*out_branches, tmpl->_mrklbranch, branches_bytesz);
}

return true;
}
Expand Down
11 changes: 9 additions & 2 deletions blkmaker_jansson.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,14 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t
return "Unrecognized block version, and not allowed to reduce or force it";
}

GETNUM_O2(cbvalue, coinbasevalue, uint64_t);
if ((v = json_object_get(json, "coinbasevalue")) && json_is_number(v)) {
const double tmpd = json_number_value(v);
const uint64_t tmp = tmpd;
if (tmpd == tmp) {
tmpl->has_cbvalue = true;
tmpl->cbvalue = tmp;
}
}

GETSTR(workid, workid);

Expand Down Expand Up @@ -464,7 +471,7 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t
tmpl->cbtxn = calloc(1, sizeof(*tmpl->cbtxn));
if ((s = parse_txn(tmpl->cbtxn, v, 0)))
return s;
} else if (!tmpl->cbvalue) {
} else if (!tmpl->has_cbvalue) {
return "Missing either coinbasetxn or coinbasevalue";
}

Expand Down
2 changes: 2 additions & 0 deletions blktemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ typedef struct {
libblkmaker_hash_t *_witnessmrklroot;
int64_t weightlimit;
int64_t txns_weight;

bool has_cbvalue;
} blktemplate_t;

extern void blktxn_init(struct blktxn_t *);
Expand Down
12 changes: 12 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ static void blktmpl_jansson_simple() {
assert(tmpl->prevblk[i] == 0x77777777);
}
assert(!tmpl->prevblk[7]);
assert(tmpl->has_cbvalue);
assert(tmpl->cbvalue == 512);

// Check clear values
Expand Down Expand Up @@ -249,6 +250,13 @@ static void blktmpl_jansson_simple() {
blktmpl_free(tmpl);
tmpl = blktmpl_create();

assert(!blktmpl_add_jansson_str(tmpl, "{\"version\":2,\"height\":3,\"bits\":\"1d00ffff\",\"curtime\":777,\"previousblockhash\":\"0000000077777777777777777777777777777777777777777777777777777777\",\"coinbasevalue\":0}", simple_time_rcvd));
assert(tmpl->has_cbvalue);
assert(tmpl->cbvalue == 0);

blktmpl_free(tmpl);
tmpl = blktmpl_create();

assert(blktmpl_add_jansson_str(tmpl, "{\"height\":3,\"bits\":\"1d00ffff\",\"curtime\":777,\"previousblockhash\":\"0000000077777777777777777777777777777777777777777777777777777777\",\"coinbasevalue\":512}", simple_time_rcvd));
blktmpl_free(tmpl);
tmpl = blktmpl_create();
Expand Down Expand Up @@ -287,6 +295,7 @@ static void blktmpl_jansson_bip22_required() {
assert(tmpl->prevblk[i] == 0xa7777777);
}
assert(!tmpl->prevblk[7]);
assert(tmpl->has_cbvalue);
assert(tmpl->cbvalue == 640);
assert(tmpl->sigoplimit == 100);
assert(tmpl->sizelimit == 1000);
Expand Down Expand Up @@ -475,6 +484,7 @@ static void test_blktmpl_jansson_floaty() {
assert(tmpl->prevblk[i] == 0x77777777);
}
assert(!tmpl->prevblk[7]);
assert(tmpl->has_cbvalue);
assert(tmpl->cbvalue == 512);

assert(tmpl->txncount == 2);
Expand Down Expand Up @@ -534,6 +544,7 @@ static void test_blktmpl_jansson_floaty() {
assert(tmpl->prevblk[i] == 0x77777777);
}
assert(!tmpl->prevblk[7]);
assert(!tmpl->has_cbvalue);
assert(!tmpl->cbvalue);

assert(tmpl->expires == 33);
Expand Down Expand Up @@ -1122,6 +1133,7 @@ static void test_blkmk_init_generation() {
assert(!blkmk_init_generation(tmpl, NULL, 0));
tmpl->height = 4;
assert(blkmk_init_generation(tmpl, NULL, 0) == 640);
tmpl->has_cbvalue = false;
tmpl->cbvalue = 0;
newcb = true;
// Unknown cbvalue needs to either fail, or figure it out from an existing cbtxn (which we don't support yet)
Expand Down

0 comments on commit c7388aa

Please sign in to comment.