void block_database::store(const block_type& block) { const uint32_t height = index_.count(); const auto number_txs = block.transactions.size(); const uint32_t number_txs32 = static_cast<uint32_t>(number_txs); // Write block data. const auto write = [&](uint8_t* data) { satoshi_save(block.header, data); auto serial = make_serializer(data + 80); serial.write_4_bytes(height); serial.write_4_bytes(number_txs32); for (const auto& tx: block.transactions) { const auto tx_hash = hash_transaction(tx); serial.write_hash(tx_hash); } }; const auto key = hash_block_header(block.header); const auto value_size = 80 + 4 + 4 + number_txs * hash_size; const auto position = map_.store(key, write, value_size); // Write height -> position mapping. write_position(position); }
hash_digest hash_transaction_impl(const message::transaction& tx, uint32_t* hash_type_code) { data_chunk serialized_tx(satoshi_raw_size(tx)); satoshi_save(tx, serialized_tx.begin()); if (hash_type_code != nullptr) extend_data(serialized_tx, uncast_type(*hash_type_code)); return generate_sha256_hash(serialized_tx); }
hash_digest hash_transaction_impl(const transaction_type& tx, uint32_t* hash_type_code) { data_chunk serialized_tx(satoshi_raw_size(tx)); satoshi_save(tx, serialized_tx.begin()); if (hash_type_code != nullptr) extend_data(serialized_tx, to_little_endian(*hash_type_code)); return bitcoin_hash(serialized_tx); }
data_chunk create_raw_message(const Message& packet) { data_chunk payload(satoshi_raw_size(packet)); satoshi_save(packet, payload.begin()); // Make the header packet and serialise it message::header head; head.magic = magic_value; head.command = satoshi_command(packet); head.payload_length = payload.size(); head.checksum = generate_sha256_checksum(payload); data_chunk raw_header(satoshi_raw_size(head)); satoshi_save(head, raw_header.begin()); // Construct completed packet with header + payload data_chunk whole_message = raw_header; extend_data(whole_message, payload); // Probably not the right place for this // Networking output in an exporter log_info(log_domain::network) << "s: " << head.command << " (" << payload.size() << " bytes)"; return whole_message; }
data_chunk create_raw_message(const Message& packet) { data_chunk payload(satoshi_raw_size(packet)); satoshi_save(packet, payload.begin()); // Make the header packet and serialise it header_type head; head.magic = magic_value(); head.command = satoshi_command(packet); head.payload_length = static_cast<uint32_t>(payload.size()); head.checksum = bitcoin_checksum(payload); data_chunk raw_header(satoshi_raw_size(head)); satoshi_save(head, raw_header.begin()); // Construct completed packet with header + payload data_chunk whole_message = raw_header; extend_data(whole_message, payload); // Probably not the right place for this // Networking output in an exporter log_debug(LOG_NETWORK) << "s: " << head.command << " (" << payload.size() << " bytes)"; return whole_message; }
bool leveldb_common::save_transaction(leveldb_transaction_batch& batch, uint32_t block_height, uint32_t tx_index, const hash_digest& tx_hash, const transaction_type& block_tx) { if (duplicate_exists(tx_hash, block_height, tx_index)) return true; data_chunk tx_data(8 + satoshi_raw_size(block_tx)); // Serialize tx. auto serial = make_serializer(tx_data.begin()); serial.write_4_bytes(block_height); serial.write_4_bytes(tx_index); // Actual tx data. auto end_iter = satoshi_save(block_tx, serial.iterator()); BITCOIN_ASSERT( std::distance(tx_data.begin(), end_iter) == 8 + satoshi_raw_size(block_tx)); // Save tx to leveldb batch.tx.Put(slice(tx_hash), slice(tx_data)); // Add inputs to spends database. // Coinbase inputs do not spend anything. if (!is_coinbase(block_tx)) for (uint32_t input_index = 0; input_index < block_tx.inputs.size(); ++input_index) { const transaction_input_type& input = block_tx.inputs[input_index]; const input_point inpoint{tx_hash, input_index}; if (!mark_spent_outputs(batch.spend, input.previous_output, inpoint)) return false; if (!add_debit(batch.debit, input, {tx_hash, input_index}, block_height)) return false; } // Save address -> output mappings. for (uint32_t output_index = 0; output_index < block_tx.outputs.size(); ++output_index) { const transaction_output_type& output = block_tx.outputs[output_index]; if (!add_credit(batch.credit, output, {tx_hash, output_index}, block_height)) return false; } return true; }
bool leveldb_common::save_block( uint32_t height, const block_type& serial_block) { leveldb_transaction_batch batch; // Write block header + tx hashes data_chunk raw_block_data( 80 + 4 + serial_block.transactions.size() * hash_digest_size); // Downcast to base header type so serializer selects that. auto header_end = satoshi_save( serial_block.header, raw_block_data.begin()); BITCOIN_ASSERT(std::distance(raw_block_data.begin(), header_end) == 80); auto serial_hashes = make_serializer(header_end); // Write the number of transactions... serial_hashes.write_4_bytes(serial_block.transactions.size()); // ... And now the tx themselves. for (uint32_t tx_index = 0; tx_index < serial_block.transactions.size(); ++tx_index) { const transaction_type& block_tx = serial_block.transactions[tx_index]; const hash_digest& tx_hash = hash_transaction(block_tx); if (!save_transaction(batch, height, tx_index, tx_hash, block_tx)) { log_fatal(LOG_BLOCKCHAIN) << "Could not save transaction"; return false; } serial_hashes.write_hash(tx_hash); } BITCOIN_ASSERT(serial_hashes.iterator() == raw_block_data.begin() + 80 + 4 + serial_block.transactions.size() * hash_digest_size); data_chunk raw_height = uncast_type(height); hash_digest block_hash = hash_block_header(serial_block.header); // Write block header batch.block.Put(slice(raw_height), slice(raw_block_data)); batch.block_hash.Put(slice_block_hash(block_hash), slice(raw_height)); // Execute batches. db_.write(batch); // Sync stealth database. db_stealth_->sync(height); return true; }
void block_database::store(const block_type& block) { const size_t height = index_.size(); // Write block data. const hash_digest key = hash_block_header(block.header); const size_t number_txs = block.transactions.size(); const size_t value_size = 80 + 4 + 4 + number_txs * hash_size; auto write = [&](uint8_t* data) { satoshi_save(block.header, data); auto serial = make_serializer(data + 80); serial.write_4_bytes(height); serial.write_4_bytes(number_txs); for (const transaction_type& tx: block.transactions) { const hash_digest tx_hash = hash_transaction(tx); serial.write_hash(tx_hash); } }; const position_type position = map_.store(key, value_size, write); // Write height -> position mapping. write_position(position); }
bool leveldb_common::save_transaction(leveldb_transaction_batch& batch, uint32_t block_height, uint32_t tx_index, const hash_digest& tx_hash, const transaction_type& block_tx) { if (is_special_duplicate(block_height, tx_index)) return true; data_chunk tx_data(8 + satoshi_raw_size(block_tx)); // Serialize tx. auto serial = make_serializer(tx_data.begin()); serial.write_4_bytes(block_height); serial.write_4_bytes(tx_index); // Actual tx data. auto end_iter = satoshi_save(block_tx, serial.iterator()); BITCOIN_ASSERT( tx_data.begin() + 8 + satoshi_raw_size(block_tx) == end_iter); // Save tx to leveldb batch.tx.Put(slice(tx_hash), slice(tx_data)); // Add inputs to spends database. // Coinbase inputs do not spend anything. if (!is_coinbase(block_tx)) for (uint32_t input_index = 0; input_index < block_tx.inputs.size(); ++input_index) { const transaction_input_type& input = block_tx.inputs[input_index]; const input_point inpoint{tx_hash, input_index}; if (!mark_spent_outputs(batch.spend, input.previous_output, inpoint)) return false; if (!add_debit(batch.debit, input, {tx_hash, input_index}, block_height)) return false; } // A stack of size 1. Keep the stealth_data from // one iteration to the next. data_chunk stealth_data_store; auto unload_stealth_store = [&]() { return std::move(stealth_data_store); }; // Save address -> output mappings. for (uint32_t output_index = 0; output_index < block_tx.outputs.size(); ++output_index) { const transaction_output_type& output = block_tx.outputs[output_index]; // If a stealth output then skip processing. if (process_stealth_output_info(output, stealth_data_store)) continue; data_chunk stealth_data = unload_stealth_store(); // Try to extract an address. payment_address address; if (!extract(address, output.script)) continue; // Process this output. if (!stealth_data.empty()) add_stealth_info(stealth_data, address, tx_hash, *db_stealth_); if (!add_credit(batch.credit, address, output.value, {tx_hash, output_index}, block_height)) return false; } return true; }
hash_digest hash_block_header(const block_header_type& header) { data_chunk raw_block_header(80); satoshi_save(header, raw_block_header.begin()); return bitcoin_hash(raw_block_header); }
hash_digest hash_block_header(const block_header_type& header) { data_chunk raw_block_header(80); satoshi_save(header, raw_block_header.begin()); return generate_sha256_hash(raw_block_header); }