Skip to content

Commit

Permalink
Merge pull request #393 from baidang201/createrawtx_support_utxo_min_…
Browse files Browse the repository at this point in the history
…confirm

Createrawtx support utxo min confirm
  • Loading branch information
codrush authored May 14, 2020
2 parents d08fd20 + 712986d commit aa449f9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/metaverse/blockchain/block_chain_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class BCB_API block_chain_impl

uint32_t get_median_time_past(uint64_t height) const;
bool is_utxo_spendable(const chain::transaction& tx, uint32_t index,
uint64_t tx_height, uint64_t latest_height) const;
uint64_t tx_height, uint64_t latest_height, uint64_t confirmations = transaction_maturity) const;

static bool is_valid_symbol(const std::string& symbol, uint32_t tx_version);
static bool is_valid_did_symbol(const std::string& symbol, bool check_sensitive = false);
Expand Down
8 changes: 7 additions & 1 deletion include/metaverse/explorer/extensions/base_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ class BCX_API base_transfer_common

virtual bool include_input_script() const { return false; }

virtual uint64_t utxo_min_confirm() const { return transaction_maturity;}

protected:
bc::blockchain::block_chain_impl& blockchain_;
tx_type tx_; // target transaction
Expand Down Expand Up @@ -469,13 +471,15 @@ class BCX_API base_transaction_constructor : public base_transfer_common
std::vector<std::string>&& from_vec, receiver_record::list&& receiver_list,
std::string&& symbol, std::string&& change,
std::string&& message, uint64_t fee, uint32_t locktime = 0,
bool include_input_script = false)
bool include_input_script = false,
uint64_t utxo_min_confirm = transaction_maturity)
: base_transfer_common(blockchain, std::move(receiver_list), fee,
std::move(symbol), "", std::move(change), locktime)
, type_{type}
, message_{std::move(message)}
, from_vec_{std::move(from_vec)}
, include_input_script_(include_input_script)
, utxo_min_confirm_(utxo_min_confirm)
{}

virtual ~base_transaction_constructor()
Expand All @@ -492,12 +496,14 @@ class BCX_API base_transaction_constructor : public base_transfer_common
void send_tx() override {}

bool include_input_script() const override { return include_input_script_; }
uint64_t utxo_min_confirm() const override { return utxo_min_confirm_;}

protected:
utxo_attach_type type_{utxo_attach_type::invalid};
std::string message_;
std::vector<std::string> from_vec_; // from address vector
bool include_input_script_; // set input's script for offline sign
uint64_t utxo_min_confirm_;
};

class BCX_API sending_etp : public base_transfer_helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class createrawtx: public command_extension
"fee,f",
value<uint64_t>(&option_.fee)->default_value(10000),
"Transaction fee. defaults to 10000 ETP bits"
)
(
"utxominimumconfirmations,c",
value<uint64_t>(&option_.utxo_min_confirm)->default_value(transaction_maturity),
"Create transaction with the utxo minimum confirmations. defaults to 3"
);

return options;
Expand Down Expand Up @@ -144,6 +149,7 @@ class createrawtx: public command_extension
uint64_t fee;
uint32_t locktime;
bool include_input_script;
uint64_t utxo_min_confirm;

} option_;

Expand Down
11 changes: 9 additions & 2 deletions src/lib/blockchain/block_chain_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3335,14 +3335,21 @@ uint32_t block_chain_impl::get_median_time_past(uint64_t height) const
return times.empty() ? 0 : times[times.size() / 2];
}

bool block_chain_impl::is_utxo_spendable(const chain::transaction& tx, uint32_t index, uint64_t tx_height, uint64_t latest_height) const
bool block_chain_impl::is_utxo_spendable(const chain::transaction& tx, uint32_t index, uint64_t tx_height, uint64_t latest_height, uint64_t confirmations) const
{
BITCOIN_ASSERT(index < tx.outputs.size());
if (index >= tx.outputs.size()){
return false;
}

if (transaction_maturity > calc_number_of_blocks(tx_height, latest_height)){
if (confirmations > 0 && 0 == tx_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;
}
if (confirmations > 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 <<
Expand Down
2 changes: 1 addition & 1 deletion src/lib/explorer/extensions/base_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ bool base_transfer_common::get_spendable_output(
}
}

return blockchain_.is_utxo_spendable(tx_temp, row.output.index, row.output_height, height);
return blockchain_.is_utxo_spendable(tx_temp, row.output.index, row.output_height, height, utxo_min_confirm());
}

// only consider etp and asset and cert.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/explorer/extensions/commands/createrawtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ console_result createrawtx::invoke(Json::Value& jv_output,
std::move(option_.symbol), std::move(change_address),
std::move(option_.message),
option_.fee, option_.locktime,
option_.include_input_script);
option_.include_input_script,
option_.utxo_min_confirm);
break;
}

Expand Down

0 comments on commit aa449f9

Please sign in to comment.