2014-08-13 10:38:35 +00:00
|
|
|
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
|
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
using namespace epee;
|
|
|
|
|
|
|
|
#include "cryptonote_basic_impl.h"
|
|
|
|
#include "string_tools.h"
|
|
|
|
#include "serialization/binary_utils.h"
|
|
|
|
#include "serialization/vector.h"
|
|
|
|
#include "cryptonote_format_utils.h"
|
|
|
|
#include "cryptonote_config.h"
|
|
|
|
#include "misc_language.h"
|
|
|
|
#include "common/base58.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "common/int-util.h"
|
|
|
|
|
|
|
|
namespace cryptonote {
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* Cryptonote helper functions */
|
|
|
|
/************************************************************************/
|
|
|
|
//-----------------------------------------------------------------------------------------------
|
2014-08-13 10:51:37 +00:00
|
|
|
uint64_t getPenalizedAmount(uint64_t amount, size_t medianSize, size_t currentBlockSize) {
|
|
|
|
static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t is too small");
|
|
|
|
assert(currentBlockSize <= 2 * medianSize);
|
|
|
|
assert(medianSize <= std::numeric_limits<uint32_t>::max());
|
|
|
|
assert(currentBlockSize <= std::numeric_limits<uint32_t>::max());
|
|
|
|
|
|
|
|
if (amount == 0) {
|
|
|
|
return 0;
|
2014-04-02 16:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
if (currentBlockSize <= medianSize) {
|
|
|
|
return amount;
|
2014-04-02 16:00:17 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
uint64_t productHi;
|
|
|
|
uint64_t productLo = mul128(amount, currentBlockSize * (UINT64_C(2) * medianSize - currentBlockSize), &productHi);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
uint64_t penalizedAmountHi;
|
|
|
|
uint64_t penalizedAmountLo;
|
|
|
|
div128_32(productHi, productLo, static_cast<uint32_t>(medianSize), &penalizedAmountHi, &penalizedAmountLo);
|
|
|
|
div128_32(penalizedAmountHi, penalizedAmountLo, static_cast<uint32_t>(medianSize), &penalizedAmountHi, &penalizedAmountLo);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
assert(0 == penalizedAmountHi);
|
|
|
|
assert(penalizedAmountLo < amount);
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
return penalizedAmountLo;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2014-08-13 10:51:37 +00:00
|
|
|
std::string getAccountAddressAsStr(uint64_t prefix, const AccountPublicAddress& adr) {
|
|
|
|
blobdata blob;
|
|
|
|
bool r = t_serializable_object_to_blob(adr, blob);
|
|
|
|
assert(r);
|
|
|
|
return tools::base58::encode_addr(prefix, blob);
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2014-08-13 10:51:37 +00:00
|
|
|
bool is_coinbase(const Transaction& tx) {
|
|
|
|
if(tx.vin.size() != 1) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
if(tx.vin[0].type() != typeid(TransactionInputGenerate)) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return false;
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2014-08-13 10:51:37 +00:00
|
|
|
bool parseAccountAddressString(uint64_t& prefix, AccountPublicAddress& adr, const std::string& str) {
|
|
|
|
blobdata data;
|
|
|
|
if (!tools::base58::decode_addr(str, prefix, data)) {
|
|
|
|
LOG_PRINT_L1("Invalid address format");
|
|
|
|
return false;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
if (!::serialization::parse_binary(data, adr)) {
|
|
|
|
LOG_PRINT_L1("Account public address keys can't be parsed");
|
2014-06-20 15:56:33 +00:00
|
|
|
return false;
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|
2014-06-20 15:56:33 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
if (!crypto::check_key(adr.m_spendPublicKey) || !crypto::check_key(adr.m_viewPublicKey)) {
|
|
|
|
LOG_PRINT_L1("Failed to validate address keys");
|
2014-06-20 15:56:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2014-08-13 10:51:37 +00:00
|
|
|
bool operator ==(const cryptonote::Transaction& a, const cryptonote::Transaction& b) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return cryptonote::get_transaction_hash(a) == cryptonote::get_transaction_hash(b);
|
|
|
|
}
|
2014-08-13 10:51:37 +00:00
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
bool operator ==(const cryptonote::Block& a, const cryptonote::Block& b) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return cryptonote::get_block_hash(a) == cryptonote::get_block_hash(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
bool parse_hash256(const std::string str_hash, crypto::hash& hash)
|
|
|
|
{
|
|
|
|
std::string buf;
|
|
|
|
bool res = epee::string_tools::parse_hexstr_to_binbuff(str_hash, buf);
|
|
|
|
if (!res || buf.size() != sizeof(crypto::hash))
|
|
|
|
{
|
|
|
|
std::cout << "invalid hash format: <" << str_hash << '>' << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.copy(reinterpret_cast<char *>(&hash), sizeof(crypto::hash));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|