Skip to content

Commit

Permalink
Fixed some bugs, changed signature for UTXO function, removed some cruft
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv Bansal committed Mar 26, 2016
1 parent f99e539 commit 93aa1bb
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 65 deletions.
8 changes: 4 additions & 4 deletions calculations.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ static void mul_and_add(u64 *over, u64 *base, u64 l, u64 r)
*base = (b << 32) | c;
}

s64 calculate_bdc(const struct utxo *u, u32 timestamp)
s64 calculate_bdc(const struct utxo *u, struct block *current_block, struct block *last_utxo_block)
{
u32 age;
u32 interval = (last_utxo_block ? (current_block->bh.timestamp - last_utxo_block->bh.timestamp) : 0);
u32 utxo_age = (current_block->bh.timestamp - u->timestamp);
u64 total_over = 0;
u64 total_base = 0;
age = ((timestamp > u->timestamp) ? (timestamp - u->timestamp) : 0);
mul_and_add(&total_over, &total_base, u->unspent, age);
mul_and_add(&total_over, &total_base, u->unspent, (utxo_age <= interval) ? utxo_age : interval);
/* we have satoshi-seconds, convert to satoshi days by dividing by */
/* 86400 */
if (total_over >= 86400/2)
Expand Down
2 changes: 1 addition & 1 deletion calculations.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ s64 calculate_fees(const struct utxo_map *utxo_map,
const struct bitcoin_transaction *t,
bool is_coinbase);

s64 calculate_bdc(const struct utxo *u, u32 timestamp);
s64 calculate_bdc(const struct utxo *u, struct block *current_block, struct block *last_utxo_block);

s64 calculate_bdd(const struct utxo_map *utxo_map,
const struct bitcoin_transaction *t,
Expand Down
36 changes: 20 additions & 16 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,35 @@ static char *blockfmt = NULL, *txfmt = NULL, *inputfmt = NULL, *outputfmt = NULL

static void print_block(const struct utxo_map *utxo_map, struct block *b)
{
print_format(blockfmt, utxo_map, b, NULL, 0, NULL, NULL, NULL);
if (blockfmt) {
print_format(blockfmt, utxo_map, b, NULL, 0, NULL, NULL, NULL, NULL);
}
}
static void print_transaction(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum)
{
print_format(txfmt, utxo_map, b, t, txnum, NULL, NULL, NULL);
if (txfmt) {
print_format(txfmt, utxo_map, b, t, txnum, NULL, NULL, NULL, NULL);
}
}
static void print_input(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum, struct bitcoin_transaction_input *i)
{
print_format(inputfmt, utxo_map, b, t, txnum, i, NULL, NULL);
if (inputfmt) {
print_format(inputfmt, utxo_map, b, t, txnum, i, NULL, NULL, NULL);
}
}
static void print_output(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum, struct bitcoin_transaction_output *o)
{
print_format(outputfmt, utxo_map, b, t, txnum, NULL, o, NULL);
if (outputfmt) {
print_format(outputfmt, utxo_map, b, t, txnum, NULL, o, NULL, NULL);
}
}
static void print_utxo(const struct utxo_map *utxo_map, struct block *b, struct utxo *u)
static void print_utxo(const struct utxo_map *utxo_map, struct block *current_block, struct block *last_utxo_block, struct utxo *u)
{
print_format(utxofmt, utxo_map, b, NULL, 0, NULL, NULL, u);
if (utxofmt) {
print_format(utxofmt, utxo_map, current_block, NULL, 0, NULL, NULL, u, last_utxo_block);
}
}

/* static block_function blockfn = &print_block; */
/* static transaction_function txfn = &print_transaction; */
/* static input_function inputfn = &print_input; */
/* static output_function outputfn = &print_output; */
/* static utxo_function utxofn = &print_utxo; */

int main(int argc, char *argv[])
{
char *blockdir = NULL, *cachedir = NULL;
Expand Down Expand Up @@ -106,12 +110,13 @@ int main(int argc, char *argv[])
" %oX: output in hex\n"
"Valid utxo format:\n"
" %uh: utxo transaction hash\n"
" %ut: utxo timestamp\n"
" %us: utxo timestamp\n"
" %uN: utxo height\n"
" %uc: utxo output count\n"
" %uu: utxo unspent output count\n"
" %us: utxo spent output count\n"
" %ud: utxo spent output count\n"
" %uU: utxo unspent amount\n"
" %uS: utxo spent amount\n"
" %uD: utxo spent amount\n"
" %uC: utxo bitcoin days created\n",
"Display help message");
opt_register_arg("--block", opt_set_charp, NULL, &blockfmt,
Expand Down Expand Up @@ -178,7 +183,6 @@ int main(int argc, char *argv[])
needs_utxo, utxo_period,
use_mmap,
progress_marks, quiet,
/* blockfn, txfn, inputfn, outputfn, utxofn); */
print_block, print_transaction, print_input, print_output, print_utxo);
return 0;
}
14 changes: 9 additions & 5 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ void print_format(const char *format,
size_t txnum,
struct bitcoin_transaction_input *i,
struct bitcoin_transaction_output *o,
struct utxo *u)
struct utxo *u,
struct block *last_utxo_block)
{
const char *c;

Expand Down Expand Up @@ -286,27 +287,30 @@ void print_format(const char *format,
case 'h':
print_hash(u->tx);
break;
case 't':
case 's':
printf("%u", u->timestamp);
break;
case 'N':
printf("%u", u->height);
break;
case 'c':
printf("%u", u->num_outputs);
break;
case 'u':
printf("%u", u->unspent_outputs);
break;
case 's':
case 'd':
printf("%u", u->num_outputs - u->unspent_outputs);
break;
case 'U':
printf("%"PRIu64, u->unspent);
break;
case 'S':
case 'D':
printf("%"PRIu64, u->spent);
break;
case 'C':
printf("%"PRIi64,
calculate_bdc(u, b->bh.timestamp));
calculate_bdc(u, b, last_utxo_block));
break;
default:
goto bad_fmt;
Expand Down
4 changes: 3 additions & 1 deletion format.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @i: current transaction input
* @o: current transaction output
* @u: current UTXO
* @last_utxo_block: last block for which UTXOs were iterated over
*
* In typical usage, most arguments will be `NULL` as shown in the
* following examples:
Expand Down Expand Up @@ -58,6 +59,7 @@ void print_format(const char *format,
size_t txnum,
struct bitcoin_transaction_input *i,
struct bitcoin_transaction_output *o,
struct utxo *u);
struct utxo *u,
struct block *last_utxo_block);

#endif /* BITCOIN_ITERATE_DUMP_H */
40 changes: 8 additions & 32 deletions iterate.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void iterate(char *blockdir, char *cachedir,
void *tal_ctx = tal(NULL, char);
size_t i, block_count = 0;
bool needs_fee;
struct block *b, *best = NULL, *genesis = NULL, *start = NULL;
struct block *b, *best = NULL, *genesis = NULL, *start = NULL, *last_utxo_block = NULL;
struct block_map block_map;
char *blockcache = NULL;
struct utxo_map utxo_map;
Expand Down Expand Up @@ -129,9 +129,13 @@ void iterate(char *blockdir, char *cachedir,
set_iteration_end(block_end, &best, genesis);
set_iteration_start(block_start, &start, genesis);

if (!quiet)
if (!quiet) {
fprintf(stderr, "bitcoin-iterate: Iterating between block heights %u and %u (of %zu total blocks)\n",
start->height, best->height, block_count);
if (utxofn) {
fprintf(stderr, "bitcoin-iterate: Iterating over UTXOs every %u blocks\n", utxo_period);
}
}

utxo_map_init(&utxo_map);

Expand Down Expand Up @@ -163,9 +167,6 @@ void iterate(char *blockdir, char *cachedir,
start = NULL;
}

/* if (!start && blockfmt) */
/* print_format(blockfmt, &utxo_map, b, NULL, 0, NULL, NULL, NULL); */

if (!start && blockfn)
blockfn(&utxo_map, b);

Expand Down Expand Up @@ -194,32 +195,15 @@ void iterate(char *blockdir, char *cachedir,
read_bitcoin_transaction(&space, &tx[i],
block_file(block_fnames, b->filenum, use_mmap), &off);

/* if (!start && txfmt) */
/* print_format(txfmt, &utxo_map, b, &tx[i], i, */
/* NULL, NULL, NULL); */
if (!start && txfn)
txfn(&utxo_map, b, &tx[i], i);

/* if (!start && inputfmt) { */
/* for (j = 0; j < tx[i].input_count; j++) { */
/* print_format(inputfmt, &utxo_map, b, */
/* &tx[i], i, &tx[i].input[j], */
/* NULL, NULL); */
/* } */
/* } */
if (!start && inputfn) {
for (j = 0; j < tx[i].input_count; j++) {
inputfn(&utxo_map, b, &tx[i], i, &tx[i].input[j]);
}
}

/* if (!start && outputfmt) { */
/* for (j = 0; j < tx[i].output_count; j++) { */
/* print_format(outputfmt, &utxo_map, b, */
/* &tx[i], i, NULL, */
/* &tx[i].output[j], NULL); */
/* } */
/* } */
if (!start && outputfn) {
for (j = 0; j < tx[i].output_count; j++) {
outputfn(&utxo_map, b, &tx[i], i, &tx[i].output[j]);
Expand All @@ -240,23 +224,15 @@ void iterate(char *blockdir, char *cachedir,
add_utxo(tal_ctx, &utxo_map, b, &tx[i], i, txoff);
}
}
/* if (utxofmt && ((b->height % utxo_period) == 0)) { */
/* struct utxo_map_iter it; */
/* struct utxo *utxo; */
/* for (utxo = utxo_map_first(&utxo_map, &it); */
/* utxo; */
/* utxo = utxo_map_next(&utxo_map, &it)) { */
/* print_format(utxofmt, &utxo_map, b, NULL, 0, NULL, NULL, utxo); */
/* } */
/* } */
if (utxofn && ((b->height % utxo_period) == 0)) {
struct utxo_map_iter it;
struct utxo *utxo;
for (utxo = utxo_map_first(&utxo_map, &it);
utxo;
utxo = utxo_map_next(&utxo_map, &it)) {
utxofn(&utxo_map, b, utxo);
utxofn(&utxo_map, b, last_utxo_block, utxo);
}
last_utxo_block = b;
}

}
Expand Down
2 changes: 1 addition & 1 deletion iterate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef void (*block_function)(const struct utxo_map *utxo_map, struct block *b)
typedef void (*transaction_function)(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum);
typedef void (*input_function)(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum, struct bitcoin_transaction_input *i);
typedef void (*output_function)(const struct utxo_map *utxo_map, struct block *b, struct bitcoin_transaction *t, size_t txnum, struct bitcoin_transaction_output *o);
typedef void (*utxo_function)(const struct utxo_map *utxo_map, struct block *b, struct utxo *u);
typedef void (*utxo_function)(const struct utxo_map *utxo_map, struct block *current_block, struct block *last_utxo_block, struct utxo *u);

void iterate(char *blockdir, char *cachedir,
bool use_testnet,
Expand Down
11 changes: 6 additions & 5 deletions utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ void add_utxo(const tal_t *tal_ctx,
struct utxo *utxo;
unsigned int i;
unsigned int spend_count = 0;
u64 initial_spent = 0;
u64 initial_spent = 0, initial_unspent = 0;

for (i = 0; i < t->output_count; i++) {
if (!is_unspendable(&t->output[i])) {
spend_count++;
initial_unspent += t->output[i].amount;
} else {
initial_spent += t->output[i].amount;
}
Expand All @@ -81,11 +82,10 @@ void add_utxo(const tal_t *tal_ctx,
utxo->unspent_outputs = spend_count;
utxo->height = b->height;
utxo->timestamp = b->bh.timestamp;
utxo->unspent = 0;
utxo->spent = initial_spent;
utxo->spent = initial_spent;
utxo->unspent = initial_unspent;
for (i = 0; i < utxo->num_outputs; i++) {
utxo->amount[i] = t->output[i].amount;
utxo->unspent += t->output[i].amount;
}
guess_output_types(t, output_types(utxo));

Expand All @@ -101,7 +101,8 @@ void release_utxo(struct utxo_map *utxo_map,
if (!utxo)
errx(1, "Unknown utxo for "SHA_FMT, SHA_VALS(i->hash));

utxo->spent += utxo->amount[i->index];
utxo->spent += utxo->amount[i->index];
utxo->unspent -= utxo->amount[i->index];

if (--utxo->unspent_outputs == 0) {
utxo_map_del(utxo_map, utxo);
Expand Down

0 comments on commit 93aa1bb

Please sign in to comment.