コード例 #1
0
ファイル: brd.c プロジェクト: gitter-badger/picocoin
static void read_blocks(void)
{
	int fd = blocks_fd;

	struct p2p_message msg = {};
	bool read_ok = true;
	int64_t fpos = 0;
	while (fread_message(fd, &msg, &read_ok)) {
		if (memcmp(msg.hdr.netmagic, chain->netmagic, 4)) {
			fprintf(plog, "blocks file: invalid network magic\n");
			exit(1);
		}

		if (!read_block_msg(&msg, fpos))
			exit(1);

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

	if (!read_ok) {
		fprintf(plog, "blocks file: read failed\n");
		exit(1);
	}

	free(msg.data);
}
コード例 #2
0
ファイル: chain-verf.c プロジェクト: digiwhite/picocoin
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);
}
コード例 #3
0
ファイル: peerman.c プロジェクト: aido/picocoin
struct peer_manager *peerman_read(void *peers_file)
{
	char *filename = peers_file;
	if (!filename)
		return NULL;

	struct peer_manager *peers;

	peers = peerman_new();
	if (!peers)
		return NULL;

	int fd = file_seq_open(filename);
	if (fd < 0) {
		log_error("peerman: %s: %s",
			filename,
			strerror(errno));
		goto err_out;
	}

	struct p2p_message msg = {};
	bool read_ok = true;

	while (fread_message(fd, &msg, &read_ok)) {
		if (!peerman_read_rec(peers, &msg)) {
			log_error("peerman: read record failed");
			goto err_out_fd;
		}
	}

	if (!read_ok) {
		log_error("peerman: read I/O failed");
		goto err_out_fd;
	}

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

	return peers;

err_out_fd:
	free(msg.data);
	close(fd);
err_out:
	peerman_free(peers);
	return NULL;
}
コード例 #4
0
ファイル: peerman.c プロジェクト: notespace/picocoin
struct peer_manager *peerman_read(void)
{
	char *filename = setting("peers");
	if (!filename)
		return NULL;

	struct peer_manager *peers;

	peers = peerman_new();
	if (!peers)
		return NULL;

	int fd = file_seq_open(filename);
	if (fd < 0) {
		perror(filename);
		goto err_out;
	}

	struct p2p_message msg = {};
	bool read_ok = true;

	while (fread_message(fd, &msg, &read_ok)) {
		if (!peerman_read_rec(peers, &msg)) {
			fprintf(stderr, "peerman: read record failed\n");
			goto err_out;
		}
	}

	if (!read_ok) {
		fprintf(stderr, "peerman: read I/O failed\n");
		goto err_out;
	}

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

	return peers;

err_out:
	peerman_free(peers);
	return NULL;
}
コード例 #5
0
ファイル: script-parse.c プロジェクト: libbitc/libbitc
static void runtest(const char *ser_fn_base)
{
	char *ser_fn = test_filename(ser_fn_base);
	int fd = file_seq_open(ser_fn);
	if (fd < 0) {
		perror(ser_fn);
		exit(1);
	}

	struct p2p_message msg = {};
	bool read_ok = false;
	bool rc = fread_message(fd, &msg, &read_ok);
	assert(rc);
	assert(read_ok);
	assert(!strncmp(msg.hdr.command, "block", 12));

	close(fd);

	struct bitc_block block;
	bitc_block_init(&block);

	struct const_buffer buf = { msg.data, msg.hdr.data_len };

	rc = deser_bitc_block(&block, &buf);
	assert(rc);

	unsigned int n_tx, n_out;
	for (n_tx = 0; n_tx < block.vtx->len; n_tx++) {
		struct bitc_tx *tx = parr_idx(block.vtx, n_tx);

		for (n_out = 0; n_out < tx->vout->len; n_out++) {
			struct bitc_txout *txout;

			txout = parr_idx(tx->vout, n_out);
			test_txout(txout);
		}
	}

	bitc_block_free(&block);
	free(msg.data);
	free(ser_fn);
}
コード例 #6
0
ファイル: blkdb.c プロジェクト: hsk81/picocoin
bool blkdb_read(struct blkdb *db, const char *idx_fn)
{
	bool rc = true;
	int fd = file_seq_open(idx_fn);
	if (fd < 0)
		return false;

	struct p2p_message msg;
	memset(&msg, 0, sizeof(msg));
	bool read_ok = true;

	while (fread_message(fd, &msg, &read_ok)) {
		rc = blkdb_read_rec(db, &msg);
		if (!rc)
			break;
	}

	close(fd);

	free(msg.data);

	return read_ok && rc;
}
コード例 #7
0
ファイル: block.c プロジェクト: aido/picocoin
static void runtest(const char *json_fn_base, const char *ser_fn_base)
{
	char *fn = test_filename(json_fn_base);
	json_t *meta = read_json(fn);
	assert(json_is_object(meta));

	char *ser_fn = test_filename(ser_fn_base);
	int fd = file_seq_open(ser_fn);
	if (fd < 0) {
		perror(ser_fn);
		exit(1);
	}

	struct p2p_message msg = {};
	bool read_ok = false;
	bool rc = fread_message(fd, &msg, &read_ok);
	assert(rc);
	assert(read_ok);
	assert(!strncmp(msg.hdr.command, "block", 12));

	close(fd);

	const char *hashstr = json_string_value(json_object_get(meta, "hash"));
	assert(hashstr != NULL);

	unsigned int size = json_integer_value(json_object_get(meta, "size"));
	assert((24 + msg.hdr.data_len) == size);

	struct bp_block block;
	bp_block_init(&block);

	struct const_buffer buf = { msg.data, msg.hdr.data_len };

	rc = deser_bp_block(&block, &buf);
	assert(rc);

	cstring *gs = cstr_new_sz(100000);
	ser_bp_block(gs, &block);

	if (gs->len != msg.hdr.data_len) {
		fprintf(stderr, "gs->len %ld, msg.hdr.data_len %u\n",
			(long)gs->len, msg.hdr.data_len);
		assert(gs->len == msg.hdr.data_len);
	}
	assert(memcmp(gs->str, msg.data, msg.hdr.data_len) == 0);

	bp_block_calc_sha256(&block);

	char hexstr[BU256_STRSZ];
	bu256_hex(hexstr, &block.sha256);

	if (strcmp(hexstr, hashstr)) {
		fprintf(stderr, "block: wanted hash %s,\n       got    hash %s\n",
			hashstr, hexstr);
		assert(!strcmp(hexstr, hashstr));
	}

	rc = bp_block_valid(&block);
	assert(rc);

	bp_block_free(&block);
	cstr_free(gs, true);
	free(msg.data);
	free(fn);
	free(ser_fn);
	json_decref(meta);
}