danicoin/src/CryptoNoteCore/CryptoNoteBasicImpl.cpp

94 lines
3.5 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-03-03 22:07:58 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-07-30 15:22:07 +00:00
#include "CryptoNoteBasicImpl.h"
#include "CryptoNoteFormatUtils.h"
#include "CryptoNoteTools.h"
#include "CryptoNoteSerialization.h"
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "Common/Base58.h"
2014-03-03 22:07:58 +00:00
#include "crypto/hash.h"
2015-05-27 12:08:46 +00:00
#include "Common/int-util.h"
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
using namespace Crypto;
using namespace Common;
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-03-03 22:07:58 +00:00
/************************************************************************/
2015-07-30 15:22:07 +00:00
/* CryptoNote helper functions */
2014-03-03 22:07:58 +00:00
/************************************************************************/
//-----------------------------------------------------------------------------------------------
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) {
2015-07-30 15:22:07 +00:00
BinaryArray ba;
bool r = toBinaryArray(adr, ba);
2014-08-13 10:51:37 +00:00
assert(r);
2015-07-30 15:22:07 +00:00
return Tools::Base58::encode_addr(prefix, Common::asString(ba));
2014-03-03 22:07:58 +00:00
}
//-----------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
bool is_coinbase(const Transaction& tx) {
2015-07-30 15:22:07 +00:00
if(tx.inputs.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
2015-07-30 15:22:07 +00:00
if(tx.inputs[0].type() != typeid(BaseInput)) {
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) {
2015-07-30 15:22:07 +00:00
std::string data;
2014-06-20 15:56:33 +00:00
2015-05-27 12:08:46 +00:00
return
2015-07-30 15:22:07 +00:00
Tools::Base58::decode_addr(str, prefix, data) &&
fromBinaryArray(adr, asBinaryArray(data)) &&
// ::serialization::parse_binary(data, adr) &&
check_key(adr.spendPublicKey) &&
check_key(adr.viewPublicKey);
2014-06-20 15:56:33 +00:00
}
//-----------------------------------------------------------------------
2015-05-27 12:08:46 +00:00
bool operator ==(const CryptoNote::Transaction& a, const CryptoNote::Transaction& b) {
2015-07-30 15:22:07 +00:00
return getObjectHash(a) == getObjectHash(b);
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
//-----------------------------------------------------------------------
2015-05-27 12:08:46 +00:00
bool operator ==(const CryptoNote::Block& a, const CryptoNote::Block& b) {
return CryptoNote::get_block_hash(a) == CryptoNote::get_block_hash(b);
2014-03-03 22:07:58 +00:00
}
}
//--------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool parse_hash256(const std::string& str_hash, Crypto::Hash& hash) {
return Common::podFromHex(str_hash, hash);
2014-03-03 22:07:58 +00:00
}