static bool process_block(const struct bp_block *block, int64_t fpos) { struct blkinfo *bi = bi_new(); bu256_copy(&bi->hash, &block->sha256); bp_block_copy_hdr(&bi->hdr, block); bi->n_file = 0; bi->n_pos = fpos; struct blkdb_reorg reorg; if (!blkdb_add(&db, bi, &reorg)) { fprintf(plog, "brd: blkdb add fail\n"); goto err_out; } /* FIXME: support reorg */ assert(reorg.conn == 1); assert(reorg.disconn == 0); /* if best chain, mark TX's as spent */ if (bu256_equal(&db.best_chain->hash, &bi->hdr.sha256)) { if (!spend_block(&uset, block, bi->height)) { char hexstr[BU256_STRSZ]; bu256_hex(hexstr, &bi->hdr.sha256); fprintf(plog, "brd: block spend fail %u %s\n", bi->height, hexstr); /* FIXME: bad record is now in blkdb */ goto err_out; } } return true; err_out: bi_free(bi); return false; }
static void read_test_msg(struct blkdb *db, struct bp_utxo_set *uset, const struct p2p_message *msg, int64_t fpos) { assert(strncmp(msg->hdr.command, "block", sizeof(msg->hdr.command)) == 0); struct bp_block block; bp_block_init(&block); struct const_buffer buf = { msg->data, msg->hdr.data_len }; assert(deser_bp_block(&block, &buf) == true); bp_block_calc_sha256(&block); assert(bp_block_valid(&block) == true); struct blkinfo *bi = bi_new(); bu256_copy(&bi->hash, &block.sha256); bp_block_copy_hdr(&bi->hdr, &block); bi->n_file = 0; bi->n_pos = fpos + P2P_HDR_SZ; assert(blkdb_add(db, bi) == true); /* if best chain, mark TX's as spent */ if (bu256_equal(&db->hashBestChain, &bi->hdr.sha256)) { if (!spend_block(uset, &block, bi->height)) { char hexstr[BU256_STRSZ]; bu256_hex(hexstr, &bi->hdr.sha256); fprintf(stderr, "chain-verf: block fail %u %s\n", bi->height, hexstr); assert(!"spend_block"); } } bp_block_free(&block); }