Skip to content

Commit

Permalink
Merge pull request #382 from mvs-org/lijun4727-patch-2
Browse files Browse the repository at this point in the history
Transaction adds block confirmation
  • Loading branch information
codrush authored Mar 2, 2020
2 parents b3e3717 + 8123513 commit 42bf8c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/metaverse/bitcoin/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ BC_CONSTEXPR uint8_t byte_bits = 8;

// Consensus constants.
extern uint32_t coinbase_maturity;
BC_CONSTEXPR uint64_t transaction_maturity = 3;
BC_CONSTEXPR uint32_t max_input_sequence = max_uint32;

BC_CONSTEXPR uint32_t total_reward = 100000000;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/blockchain/block_chain_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3330,6 +3330,14 @@ bool block_chain_impl::is_utxo_spendable(const chain::transaction& tx, uint32_t
return false;
}

if (transaction_maturity > calc_number_of_blocks(tx_height, latest_height)){
log::debug(LOG_BLOCKCHAIN) << "transaction is not mature" <<
" transaction hash =" << encode_hash(tx.hash()) <<
" tx_height=" << tx_height <<
" latest_height=" << latest_height;
return false;
}

const auto output = tx.outputs[index];

if (chain::operation::is_pay_key_hash_with_lock_height_pattern(output.script.operations)) {
Expand Down
20 changes: 11 additions & 9 deletions src/lib/consensus/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ bool miner::get_input_etp(const chain::transaction& tx, const std::vector<transa
{
total_inputs = 0;
blockchain::block_chain_impl& block_chain = node_.chain_impl();
uint64_t last_height = 0;
block_chain.get_last_height(last_height);
for (auto& input : tx.inputs) {
chain::transaction prev_tx;
uint64_t prev_height = 0;
uint64_t input_value = 0;
if (block_chain.get_transaction(prev_tx, prev_height, input.previous_output.hash)) {

if (block_chain.calc_number_of_blocks(prev_height, last_height) < transaction_maturity) {
// maturity of each input requires 12 blocks.
return false;
}

input_value = prev_tx.outputs[input.previous_output.index].value;
previous_out_map[input.previous_output] =
std::make_pair(prev_height, prev_tx.outputs[input.previous_output.index]);
Expand All @@ -137,9 +145,7 @@ bool miner::get_input_etp(const chain::transaction& tx, const std::vector<transa
std::make_pair(max_uint64, (*it)->outputs[input.previous_output.index]);
}
else {
#ifdef MVS_DEBUG
log::debug(LOG_HEADER) << "previous transaction not ready: " << encode_hash(hash);
#endif
log::warning(LOG_HEADER) << encode_hash(tx.hash())<< " previous transaction not ready: " << encode_hash(hash);
return false;
}
}
Expand Down Expand Up @@ -191,9 +197,7 @@ bool miner::get_transaction(

// check fees
if (fee < min_tx_fee || !blockchain::validate_transaction::check_special_fees(setting_.use_testnet_rules, tx, fee)) {
#ifdef MVS_DEBUG
log::debug(LOG_HEADER) << "check fees failed, delete_tx " << encode_hash(hash);
#endif
log::warning(LOG_HEADER) << "check fees failed, pool delete_tx " << encode_hash(hash);
i = transactions.erase(i);
// delete it from pool if not enough fee
node_.pool().delete_tx(hash);
Expand All @@ -205,9 +209,7 @@ bool miner::get_transaction(
// check double spending
for (const auto& input : tx.inputs) {
if (node_.chain_impl().get_spends_output(input.previous_output)) {
#ifdef MVS_DEBUG
log::debug(LOG_HEADER) << "check double spending failed, delete_tx " << encode_hash(hash);
#endif
log::warning(LOG_HEADER) << "check double spending failed, pool delete_tx " << encode_hash(hash);
i = transactions.erase(i);
node_.pool().delete_tx(hash);
transaction_is_ok = false;
Expand Down

0 comments on commit 42bf8c6

Please sign in to comment.