danicoin/src/CryptoNoteCore/CryptoNoteTools.h

127 lines
3.4 KiB
C
Raw Normal View History

2015-07-30 15:22:07 +00:00
// Copyright (c) 2012-2015, 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/>.
#pragma once
#include <limits>
#include "Common/MemoryInputStream.h"
#include "Common/StringTools.h"
#include "Common/VectorOutputStream.h"
#include "Serialization/BinaryOutputStreamSerializer.h"
#include "Serialization/BinaryInputStreamSerializer.h"
#include "CryptoNoteSerialization.h"
namespace CryptoNote {
void getBinaryArrayHash(const BinaryArray& binaryArray, Crypto::Hash& hash);
Crypto::Hash getBinaryArrayHash(const BinaryArray& binaryArray);
template<class T>
bool toBinaryArray(const T& object, BinaryArray& binaryArray) {
try {
::Common::VectorOutputStream stream(binaryArray);
BinaryOutputStreamSerializer serializer(stream);
serialize(const_cast<T&>(object), serializer);
} catch (std::exception&) {
return false;
}
return true;
}
template<>
bool toBinaryArray(const BinaryArray& object, BinaryArray& binaryArray);
template<class T>
BinaryArray toBinaryArray(const T& object) {
BinaryArray ba;
toBinaryArray(object, ba);
return ba;
}
template<class T>
bool fromBinaryArray(T& object, const BinaryArray& binaryArray) {
bool result = false;
try {
Common::MemoryInputStream stream(binaryArray.data(), binaryArray.size());
BinaryInputStreamSerializer serializer(stream);
serialize(object, serializer);
result = stream.endOfStream(); // check that all data was consumed
} catch (std::exception&) {
}
return result;
}
template<class T>
bool getObjectBinarySize(const T& object, size_t& size) {
BinaryArray ba;
if (!toBinaryArray(object, ba)) {
size = (std::numeric_limits<size_t>::max)();
return false;
}
size = ba.size();
return true;
}
template<class T>
size_t getObjectBinarySize(const T& object) {
size_t size;
getObjectBinarySize(object, size);
return size;
}
template<class T>
bool getObjectHash(const T& object, Crypto::Hash& hash) {
BinaryArray ba;
if (!toBinaryArray(object, ba)) {
hash = NULL_HASH;
return false;
}
hash = getBinaryArrayHash(ba);
return true;
}
template<class T>
bool getObjectHash(const T& object, Crypto::Hash& hash, size_t& size) {
BinaryArray ba;
if (!toBinaryArray(object, ba)) {
hash = NULL_HASH;
size = (std::numeric_limits<size_t>::max)();
return false;
}
size = ba.size();
hash = getBinaryArrayHash(ba);
return true;
}
template<class T>
Crypto::Hash getObjectHash(const T& object) {
Crypto::Hash hash;
getObjectHash(object, hash);
return hash;
}
uint64_t getInputAmount(const Transaction& transaction);
2015-08-27 18:55:14 +00:00
std::vector<uint64_t> getInputsAmounts(const Transaction& transaction);
2015-07-30 15:22:07 +00:00
uint64_t getOutputAmount(const Transaction& transaction);
void decomposeAmount(uint64_t amount, uint64_t dustThreshold, std::vector<uint64_t>& decomposedAmounts);
}