File: blktemplate.c

package info (click to toggle)
libblkmaker 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 176 kB
  • ctags: 165
  • sloc: ansic: 1,306; makefile: 67; sh: 7
file content (129 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download
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
/*
 * Copyright 2012-2016 Luke Dashjr
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the standard MIT license.  See COPYING for more details.
 */

#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include <blktemplate.h>

static const char *capnames[] = {
	"coinbasetxn",
	"coinbasevalue",
	"workid",
	
	"longpoll",
	"proposal",
	"serverlist",
	                                     NULL, NULL,
	NULL, NULL, NULL, NULL,  NULL, NULL, NULL, NULL,
	
	"coinbase/append",
	"coinbase",
	"generate",
	"time/increment",
	"time/decrement",
	"transactions/add",
	"prevblock",
	"version/force",
	"version/reduce",
	
	"submit/hash",
	"submit/coinbase",
	"submit/truncate",
	"share/coinbase",
	"share/merkle",
	"share/truncate",
};

const char *blktmpl_capabilityname(gbt_capabilities_t caps) {
	for (unsigned int i = 0; i < GBT_CAPABILITY_COUNT; ++i)
		if (caps & (1 << i))
			return capnames[i];
	return NULL;
}

gbt_capabilities_t blktmpl_getcapability(const char *n) {
	for (unsigned int i = 0; i < GBT_CAPABILITY_COUNT; ++i)
		if (capnames[i] && !strcasecmp(n, capnames[i]))
			return 1 << i;
	if (!strcasecmp(n, "transactions"))
		return BMM_TXNADD;  // Odd one as it's overloaded w/"transactions/add" per spec
	return 0;
}

blktemplate_t *blktmpl_create() {
	blktemplate_t *tmpl;
	tmpl = calloc(1, sizeof(*tmpl));
	if (!tmpl)
		return NULL;
	
	tmpl->sigoplimit = USHRT_MAX;
	tmpl->sizelimit = ULONG_MAX;
	
	tmpl->maxtime = 0xffffffff;
	tmpl->maxtimeoff = 0x7fff;
	tmpl->mintimeoff = -0x7fff;
	tmpl->maxnonce = 0xffffffff;
	tmpl->expires = 0x7fff;
	
	return tmpl;
}

uint32_t blktmpl_addcaps(const blktemplate_t *tmpl) {
	// TODO: make this a lot more flexible for merging
	// For now, it's a simple "filled" vs "not filled"
	if (tmpl->version)
		return 0;
	return GBT_CBTXN | GBT_WORKID | BMM_TIMEINC | BMM_CBAPPEND | BMM_VERFORCE | BMM_VERDROP | BMAb_COINBASE | BMAb_TRUNCATE;
}

const struct blktmpl_longpoll_req *blktmpl_get_longpoll(blktemplate_t *tmpl) {
	if (!tmpl->lp.id)
		return NULL;
	return &tmpl->lp;
}

bool blktmpl_get_submitold(blktemplate_t *tmpl) {
	return tmpl->submitold;
}

void _blktxn_free(struct blktxn_t *bt) {
	free(bt->data);
	free(bt->hash);
	free(bt->hash_);
	free(bt->depends);
}
#define blktxn_free  _blktxn_free

static
void blkaux_clean(struct blkaux_t * const aux) {
	free(aux->auxname);
	free(aux->data);
}

void blktmpl_free(blktemplate_t *tmpl) {
	for (unsigned long i = 0; i < tmpl->txncount; ++i)
		blktxn_free(&tmpl->txns[i]);
	free(tmpl->txns);
	if (tmpl->cbtxn)
	{
		blktxn_free(tmpl->cbtxn);
		free(tmpl->cbtxn);
	}
	free(tmpl->_mrklbranch);
	for (unsigned i = 0; i < tmpl->aux_count; ++i)
		blkaux_clean(&tmpl->auxs[i]);
	free(tmpl->auxs);
	free(tmpl->workid);
	free(tmpl->target);
	free(tmpl->lp.id);
	free(tmpl->lp.uri);
	free(tmpl);
}