Skip to content

Commit

Permalink
Merge pull request #226 from VanGrx/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
VanGrx authored May 24, 2020
2 parents 468b0ab + 1a63ee6 commit 5ecd3a6
Show file tree
Hide file tree
Showing 33 changed files with 1,179 additions and 871 deletions.
34 changes: 27 additions & 7 deletions src/blockchain_db/blockchain_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti
tx_hash = *tx_hash_ptr;
}

for (const txin_v& tx_input : tx.vin)
if(tx_input.type() == typeid(txin_to_script)){
const cryptonote::txin_to_script &txin = boost::get<cryptonote::txin_to_script>(tx_input);
if (!safex::validate_safex_command(*this, txin)) {
throw SAFEX_TX_CONFLICT(tx_hash);
}
}

for (const txin_v& tx_input : tx.vin)
{

Expand All @@ -149,7 +157,7 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti
{
auto k_image_opt = boost::apply_visitor(key_image_visitor(), tx_input);
if (!k_image_opt)
DB_ERROR("Output does not have proper key image");
throw DB_ERROR("Output does not have proper key image");
const crypto::key_image &k_image = *k_image_opt;
add_spent_key(k_image);
}
Expand All @@ -164,9 +172,11 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti
//mark key image as spent
auto k_image_opt = boost::apply_visitor(key_image_visitor(), tx_input);
if (!k_image_opt)
DB_ERROR("Output does not have proper key image");
throw DB_ERROR("Output does not have proper key image");
const crypto::key_image &k_image = *k_image_opt;
add_spent_key(k_image);

if(is_safex_key_image_verification_needed(txin.command_type))
add_spent_key(k_image);


}
Expand All @@ -181,14 +191,22 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti
{
if ((tx_input.type() == typeid(txin_to_key))
|| (tx_input.type() == typeid(txin_token_to_key))
|| (tx_input.type() == typeid(txin_token_migration))
|| (tx_input.type() == typeid(txin_to_script))
)
|| (tx_input.type() == typeid(txin_token_migration)))
{
auto k_image_opt = boost::apply_visitor(key_image_visitor(), tx_input);
if (!k_image_opt) continue;
const crypto::key_image &k_image = *k_image_opt;
remove_spent_key(k_image);
} else if (tx_input.type() == typeid(txin_to_script)){
auto input = boost::get<txin_to_script>(tx_input);

if(safex::is_safex_key_image_verification_needed(input.command_type))
{
auto k_image_opt = boost::apply_visitor(key_image_visitor(), tx_input);
if (!k_image_opt) continue;
const crypto::key_image &k_image = *k_image_opt;
remove_spent_key(k_image);
}
}
}
return;
Expand Down Expand Up @@ -321,7 +339,9 @@ void BlockchainDB::remove_transaction(const crypto::hash& tx_hash)
auto input = boost::get<txin_to_script>(tx_input);
if(input.command_type == safex::command_t::token_unstake)
remove_unstake_token(tx_hash, tx);
remove_spent_key(boost::get<txin_to_script>(tx_input).k_image);

if(safex::is_safex_key_image_verification_needed(input.command_type))
remove_spent_key(boost::get<txin_to_script>(tx_input).k_image);
}

}
Expand Down
29 changes: 27 additions & 2 deletions src/blockchain_db/blockchain_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,21 @@ namespace cryptonote
{}
};

/**
* @brief thrown when a transaction cannot be added due to conflict with another tx in the block
*/
class SAFEX_TX_CONFLICT : public DB_EXCEPTION
{
public:
SAFEX_TX_CONFLICT() : DB_EXCEPTION("The safex transaction verification failed!")
{}

SAFEX_TX_CONFLICT(const crypto::hash& _tx_hash) : DB_EXCEPTION("The safex transaction verification failed!"), tx_hash(_tx_hash)
{}

crypto::hash tx_hash;
};

/***********************************
* End of Exception Definitions
***********************************/
Expand Down Expand Up @@ -1438,10 +1453,11 @@ namespace cryptonote
*
* @param output_type type of output(e.g. staked token output
* @param output_index index of the output for selected type
* @param output_id reference to var where output_id will be returned
*
* @return list of public keys that can use this output
* @return true if it can find output id, false if it is not found
*/
virtual uint64_t get_output_id(const tx_out_type output_type, const uint64_t output_index) const = 0;
virtual bool get_output_id(const tx_out_type output_type, const uint64_t output_index, uint64_t& output_id) const = 0;
/**
* @brief gets an output's tx hash and index
*
Expand Down Expand Up @@ -1754,6 +1770,15 @@ namespace cryptonote
virtual bool get_interval_interest_map(const uint64_t start_height, const uint64_t end_height, safex::map_interval_interest &map) const = 0;


/**
* Returns accumulated interest for given output
*
*
* @param txin script of unstake command
* @param unlock_height height of the Blockchain when the command is called
* @return Total SFX network fee for given output
*/
virtual uint64_t calculate_staked_token_interest_for_output(const txin_to_script &txin, const uint64_t unlock_height) const = 0;

/**
* Get safex account public key
Expand Down
67 changes: 54 additions & 13 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,10 +1586,6 @@ void BlockchainLMDB::process_command_input(const cryptonote::txin_to_script &txi
else if (txin.command_type == safex::command_t::donate_network_fee)
{
//network_fee_sum is updated at place of output processing
}
else if (txin.command_type == safex::command_t::distribute_network_fee)
{

}
else if (txin.command_type == safex::command_t::create_account)
{
Expand Down Expand Up @@ -3120,7 +3116,10 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t& amount, const uint6
if (!(output_type >= cryptonote::tx_out_type::out_advanced && output_type < cryptonote::tx_out_type::out_invalid))
throw0(DB_ERROR("Unknown advanced output type"));

uint64_t output_id = get_output_id(output_type, output_index);
uint64_t output_id;

if( !get_output_id(output_type, output_index, output_id) )
throw0(DB_ERROR("Output ID not found!"));

check_open();

Expand Down Expand Up @@ -3152,7 +3151,7 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t& amount, const uint6
return output;
}

uint64_t BlockchainLMDB::get_output_id(const tx_out_type output_type, const uint64_t output_index) const
bool BlockchainLMDB::get_output_id(const tx_out_type output_type, const uint64_t output_index, uint64_t& output_id) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);

Expand All @@ -3166,7 +3165,6 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t& amount, const uint6
RCURSOR(output_advanced_type);
cur_output_advanced_type = m_cur_output_advanced_type;

uint64_t output_id = 0;

const uint64_t out_type = static_cast<uint64_t>(output_type);
MDB_val_set(key, out_type);
Expand All @@ -3179,13 +3177,13 @@ output_data_t BlockchainLMDB::get_output_key(const uint64_t& amount, const uint6
output_id = okadv->output_id;
}
else if (result == MDB_NOTFOUND)
throw0(DB_ERROR(lmdb_error("Attemting to get output id from output with index " + std::to_string(output_index) + " but not found: ", result).c_str()));
return false;
else
throw0(DB_ERROR(lmdb_error("DB error attempting to advanced output blob: ", result).c_str()));


TXN_POSTFIX_RDONLY();
return output_id;
return true;
}

tx_out_index BlockchainLMDB::get_output_tx_and_index_from_global(const uint64_t& output_id) const
Expand Down Expand Up @@ -4142,8 +4140,9 @@ void BlockchainLMDB::get_amount_output_key(const uint64_t &amount, const std::ve
std::vector<uint64_t> output_ids;

for(const auto output_index: output_indexes){
uint64_t output_id = get_output_id(output_type, output_index);
output_ids.push_back(output_id);
uint64_t output_id;
if(get_output_id(output_type, output_index, output_id))
output_ids.push_back(output_id);
}

TXN_PREFIX_RDONLY();
Expand Down Expand Up @@ -4860,6 +4859,44 @@ bool BlockchainLMDB::is_valid_transaction_output_type(const txout_target_v &txou
return true;
};

uint64_t BlockchainLMDB::calculate_staked_token_interest_for_output(const txin_to_script &txin, const uint64_t unlock_height) const
{

if (txin.command_type != safex::command_t::token_unstake) {
MERROR("Invalid command for interest calculation");
return 0;
}

output_advanced_data_t output_data = get_output_advanced_data(tx_out_type::out_staked_token, txin.key_offsets[0]);

if (output_data.height == 0) {
MERROR("Invalid output lock height");
return 0;
}

uint64_t starting_interval = safex::calculate_interval_for_height(output_data.height, m_nettype) + 1;
uint64_t end_interval = safex::calculate_interval_for_height(unlock_height, m_nettype) - 1;

if (starting_interval > end_interval) {
MERROR("Calculating interest for invalid intervals");
return 0;
}

safex::map_interval_interest interest_map;
if (!get_interval_interest_map(starting_interval, end_interval, interest_map)) {
MERROR("Could not get interval map");
return 0;
}

uint64_t interest = 0;
for (uint64_t i=starting_interval;i<=end_interval;++i) {
interest += interest_map[i]*(txin.token_amount/SAFEX_TOKEN);
}

return interest;
}


void BlockchainLMDB::add_safex_account(const safex::account_username &username, const blobdata &blob) {
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
Expand Down Expand Up @@ -5976,7 +6013,9 @@ bool BlockchainLMDB::is_valid_transaction_output_type(const txout_target_v &txou


tx_out_type out_type = edited ? tx_out_type::out_safex_offer_update : tx_out_type::out_safex_offer;
uint64_t output_id = get_output_id(out_type, output_index);
uint64_t output_id;
if( !get_output_id(out_type, output_index, output_id))
throw0(DB_ERROR("Output ID not found!"));

MDB_cursor *cur_output_advanced;
RCURSOR(output_advanced);
Expand Down Expand Up @@ -6033,7 +6072,9 @@ bool BlockchainLMDB::is_valid_transaction_output_type(const txout_target_v &txou
throw0(DB_ERROR(lmdb_error("DB error attempting to get advanced output data: ", get_result).c_str()));


uint64_t output_id_creation = get_output_id(tx_out_type::out_safex_offer, output_index_creation);
uint64_t output_id_creation;
if(!get_output_id(tx_out_type::out_safex_offer, output_index_creation, output_id_creation))
throw0(DB_ERROR("Output ID of offer creation not found!"));
//Get offer keys
MDB_val_set(k_creation, output_id_creation);
MDB_val_set(v_blob, blob);
Expand Down
3 changes: 2 additions & 1 deletion src/blockchain_db/lmdb/db_lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class BlockchainLMDB : public BlockchainDB
bool allow_partial = false) const;

virtual output_advanced_data_t get_output_advanced_data(const tx_out_type output_type, const uint64_t output_index) const;
virtual uint64_t get_output_id(const tx_out_type output_type, const uint64_t output_index) const;
virtual bool get_output_id(const tx_out_type output_type, const uint64_t output_index, uint64_t& output_id) const;

virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& output_id) const;
virtual void get_output_tx_and_index_from_global(const std::vector<uint64_t> &global_indices,
Expand Down Expand Up @@ -320,6 +320,7 @@ class BlockchainLMDB : public BlockchainDB
virtual uint64_t get_network_fee_sum_for_interval(const uint64_t interval) const override;
virtual std::vector<uint64_t> get_token_stake_expiry_outputs(const uint64_t block_height) const override;
virtual bool get_interval_interest_map(const uint64_t start_interval, const uint64_t end_interval, safex::map_interval_interest &map) const override;
virtual uint64_t calculate_staked_token_interest_for_output(const txin_to_script &txin, const uint64_t unlock_height) const override;
virtual bool get_account_key(const safex::account_username &username, crypto::public_key &pkey) const;
virtual bool get_account_data(const safex::account_username &username, std::vector<uint8_t> &data) const;
virtual bool get_offer(const crypto::hash offer_id, safex::safex_offer &offer) const;
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoints/checkpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace cryptonote
if (nettype == TESTNET)
{
ADD_CHECKPOINT(1, "753e1454ebc44ffb18d7b390f7393a66ce7f3a8aaaf1e4e857af9df4e80d5784");
ADD_CHECKPOINT(100000,"35f7c0edccf0721fc2022d8bc43ae4ad086649f7778a6f564a1e7c9238f53cd9");
ADD_CHECKPOINT(100000,"e255e1515acb9cccca8ebe9ce57718726c27c6e647878e6a18879dd42bb28434");

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ namespace cryptonote
if (vin.type() == typeid(txin_to_script))
{
const txin_to_script& in = boost::get<txin_to_script>(vin);
if (in.command_type == safex::command_t::distribute_network_fee) {
if (in.command_type == safex::command_t::token_unstake) {
network_fee += in.amount;
}
}
Expand Down
57 changes: 33 additions & 24 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,39 @@
#define HF_VERSION_STOP_COUNTERFEIT_TOKENS 6

//Safex related constants
#define SAFEX_COMMAND_PROTOCOL_VERSION 1
#define SAFEX_MINIMUM_TOKEN_STAKE_AMOUNT 10000 * SAFEX_TOKEN
#define SAFEX_DEFAULT_TOKEN_STAKE_EXPIRY_PERIOD 500000
#define SAFEX_DEFAULT_INTERVAL_PERIOD 1000 //blocks
#define SAFEX_DEFAULT_MINUMUM_TOKEN_STAKE_PERIOD SAFEX_DEFAULT_INTERVAL_PERIOD*10 //blocks
#define SAFEX_DEFAULT_NETWORK_FEE_PERCENTAGE ((uint64_t)5)
#define SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_FEE ((uint64_t)100*SAFEX_TOKEN)

#define SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD_FAKECHAIN ((uint64_t)1)
#define SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD_TESTNET ((uint64_t)150)
#define SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD ((uint64_t)150)// TBD

#define SAFEX_ACCOUNT_USERNAME_MAX_SIZE 32
#define SAFEX_ACCOUNT_DATA_MAX_SIZE 2048

#define SAFEX_OFFER_NAME_MAX_SIZE 80
#define SAFEX_OFFER_DATA_MAX_SIZE 2048


#define SAFEX_PRICE_PEG_NAME_MAX_SIZE 60
#define SAFEX_PRICE_PEG_CURRENCY_MAX_SIZE 8
#define SAFEX_PRICE_PEG_DATA_MAX_SIZE 2048

#define SAFEX_FEEDBACK_DATA_MAX_SIZE 2048
constexpr uint64_t SAFEX_COMMAND_PROTOCOL_VERSION = 1;

//Safex token stake constants
constexpr uint64_t SAFEX_MINIMUM_TOKEN_STAKE_AMOUNT = 10000 * SAFEX_TOKEN;
constexpr uint64_t SAFEX_DEFAULT_TOKEN_STAKE_EXPIRY_PERIOD = 500000;
constexpr uint64_t SAFEX_DEFAULT_INTERVAL_PERIOD = 1000; //blocks
constexpr uint64_t SAFEX_DEFAULT_MINUMUM_TOKEN_STAKE_PERIOD = SAFEX_DEFAULT_INTERVAL_PERIOD*10; //blocks

//Safex network fee constants
constexpr uint64_t SAFEX_DEFAULT_NETWORK_FEE_PERCENTAGE = 5;

//Safex create account token lock constants
constexpr uint64_t SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_FEE = 100*SAFEX_TOKEN;
constexpr uint64_t SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD_FAKECHAIN = 1;
constexpr uint64_t SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD_TESTNET = 10;
constexpr uint64_t SAFEX_CREATE_ACCOUNT_TOKEN_LOCK_PERIOD = 150;// TBD

//Safex account constants
constexpr uint64_t SAFEX_ACCOUNT_USERNAME_MAX_SIZE = 32;
constexpr uint64_t SAFEX_ACCOUNT_DATA_MAX_SIZE = 2048;

//Safex offer constants
constexpr uint64_t SAFEX_OFFER_NAME_MAX_SIZE = 80;
constexpr uint64_t SAFEX_OFFER_DATA_MAX_SIZE = 2048;
constexpr uint64_t SAFEX_OFFER_MINIMUM_PRICE = SAFEX_CASH_COIN/10000; // 0.0001 SFX

//Safex price peg constants
constexpr uint64_t SAFEX_PRICE_PEG_NAME_MAX_SIZE = 60;
constexpr uint64_t SAFEX_PRICE_PEG_CURRENCY_MAX_SIZE = 8;
constexpr uint64_t SAFEX_PRICE_PEG_DATA_MAX_SIZE = 2048;

//Safex feedback constants
constexpr uint64_t SAFEX_FEEDBACK_DATA_MAX_SIZE = 2048;

#define DEFAULT_MIX 6 //default wallet mix for transactions

Expand Down
Loading

0 comments on commit 5ecd3a6

Please sign in to comment.