Example #1
0
static void runtest(bool use_testnet, const char *blocks_fn)
{
	const struct chain_info *chain =
		&chain_metadata[use_testnet ? CHAIN_TESTNET3 : CHAIN_BITCOIN];

	struct blkdb blkdb;
	bu256_t blk0;

	hex_bu256(&blk0, chain->genesis_hash);
	assert(blkdb_init(&blkdb, chain->netmagic, &blk0) == true);

	struct bp_utxo_set uset;
	bp_utxo_set_init(&uset);

	fprintf(stderr, "chain-verf: validating %s chainfile %s\n",
		use_testnet ? "testnet3" : "mainnet",
		blocks_fn);

	int fd = open(blocks_fn, O_RDONLY);
	if (fd < 0) {
		perror(blocks_fn);
		assert(fd >= 0);
	}

#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L
	posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif

	struct p2p_message msg = {};
	bool read_ok = true;
	int64_t fpos = 0;
	unsigned int records = 0;
	while (fread_message(fd, &msg, &read_ok)) {
		assert(memcmp(msg.hdr.netmagic, chain->netmagic, 4) == 0);

		read_test_msg(&blkdb, &uset, &msg, fpos);

		fpos += P2P_HDR_SZ;
		fpos += msg.hdr.data_len;
		records++;
	}

	assert(read_ok == true);

	close(fd);
	free(msg.data);

	blkdb_free(&blkdb);
	bp_utxo_set_free(&uset);

	fprintf(stderr, "chain-verf: %u records validated\n", records);
}
Example #2
0
static void shutdown_daemon(struct net_child_info *nci)
{
	LOG_BEGIN;
	bool rc = peerman_write(nci->peers);
	fprintf(plog, "net: %s %u/%zu peers\n",
		rc ? "wrote" : "failed to write",
		bp_hashtab_size(nci->peers->map_addr),
		clist_length(nci->peers->addrlist));

	if (plog != stdout && plog != stderr) {
		fclose(plog);
		plog = NULL;
	}

	if (setting("free")) {
		shutdown_nci(nci);
		bp_hashtab_unref(orphans);
		bp_hashtab_unref(settings);
		blkdb_free(&db);
		bp_utxo_set_free(&uset);
	}
	LOG_END;
}