Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ BITCOIN_CORE_H = \
compat.h \
core.h \
mastercore.h \
mastercore_tx.h \
mastercore_convert.h \
mastercore_dex.h \
mastercore_sp.h \
mastercore_errors.h \
mastercore_format.h \
mastercore_sp.h \
mastercore_tx.h \
crypter.h \
db.h \
hash.h \
Expand Down Expand Up @@ -131,10 +133,12 @@ libbitcoin_common_a_SOURCES = \
chainparams.cpp \
core.cpp \
mastercore.cpp \
mastercore_tx.cpp \
mastercore_rpc.cpp \
mastercore_convert.cpp \
mastercore_dex.cpp \
mastercore_format.cpp \
mastercore_rpc.cpp \
mastercore_sp.cpp \
mastercore_tx.cpp \
hash.cpp \
key.cpp \
netbase.cpp \
Expand Down
86 changes: 4 additions & 82 deletions src/mastercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ using namespace mastercore;
#include "mastercore_dex.h"
#include "mastercore_tx.h"
#include "mastercore_sp.h"
#include "mastercore_convert.h"
#include "mastercore_errors.h"
#include "mastercore_format.h"

// part of 'breakout' feature
static const int nBlockTop = 0;
Expand Down Expand Up @@ -287,91 +289,11 @@ void swapByteOrder64(uint64_t& ull)
(ull << 56);
}

uint64_t rounduint64(double d)
{
return (uint64_t)(abs(0.5 + d));
}

bool isNonMainNet()
{
return (TestNet() || RegTest());
}

// mostly taken from Bitcoin's FormatMoney()
string FormatDivisibleMP(int64_t n, bool fSign)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
int64_t n_abs = (n > 0 ? n : -n);
int64_t quotient = n_abs/COIN;
int64_t remainder = n_abs%COIN;
string str = strprintf("%d.%08d", quotient, remainder);

if (!fSign) return str;

if (n < 0)
str.insert((unsigned int)0, 1, '-');
else
str.insert((unsigned int)0, 1, '+');
return str;
}

int64_t mastercore::strToInt64(std::string strAmount, bool divisible)
{
int64_t Amount = 0;

//check for a negative (minus sign) and invalidate if present
size_t negSignPos = strAmount.find("-");
if (negSignPos!=std::string::npos) return 0;

//convert the string into a usable int64
if (divisible)
{
//check for existance of decimal point
size_t pos = strAmount.find(".");
if (pos==std::string::npos)
{ //no decimal point but divisible so pad 8 zeros on right
strAmount+="00000000";
}
else
{
size_t posSecond = strAmount.find(".", pos+1); //check for existence of second decimal point, if so invalidate amount
if (posSecond!=std::string::npos) return 0;
if ((strAmount.size()-pos)<9)
{ //there are decimals either exact or not enough, pad as needed
string strRightOfDecimal = strAmount.substr(pos+1);
unsigned int zerosToPad = 8-strRightOfDecimal.size();
if (zerosToPad>0) //do we need to pad?
{
for(unsigned int it = 0; it != zerosToPad; it++)
{
strAmount+="0";
}
}
}
else
{ //there are too many decimals, truncate after 8
strAmount = strAmount.substr(0,pos+9);
}
}
strAmount.erase(std::remove(strAmount.begin(), strAmount.end(), '.'), strAmount.end());
try { Amount = boost::lexical_cast<int64_t>(strAmount); } catch(const boost::bad_lexical_cast &e) { }
}
else
{
size_t pos = strAmount.find(".");
string newStrAmount = strAmount.substr(0,pos);
try { Amount = boost::lexical_cast<int64_t>(newStrAmount); } catch(const boost::bad_lexical_cast &e) { }
}
return Amount;
}

std::string mastercore::FormatIndivisibleMP(int64_t n)
{
string str = strprintf("%ld", n);
return str;
}

string const CMPSPInfo::watermarkKey("watermark");

CCriticalSection cs_tally;
Expand Down Expand Up @@ -800,7 +722,7 @@ const double years = seconds_passed/seconds_in_one_year;
const double part_available = 1 - pow(0.5, years);
const double available_reward=all_reward * part_available;

devmsc = rounduint64(available_reward);
devmsc = RoundToUInt64(available_reward);
exodus_delta = devmsc - exodus_prev;

if (msc_debug_exo) fprintf(mp_fp, "devmsc=%lu, exodus_prev=%lu, exodus_delta=%ld\n", devmsc, exodus_prev, exodus_delta);
Expand Down Expand Up @@ -3758,7 +3680,7 @@ int rc = PKT_ERROR_STO -1000;
owns, address.c_str(), percentage, piece, should_receive, will_really_receive, sent_so_far);

// record the detailed info as needed
if (fhandle) fprintf(fhandle, "%s = %s\n", address.c_str(), bDivisible ? FormatDivisibleMP(will_really_receive).c_str() : FormatIndivisibleMP(will_really_receive).c_str());
if (fhandle) fprintf(fhandle, "%s = %s\n", address.c_str(), FormatTokenAmount(will_really_receive, bDivisible).c_str());

if (!update_tally_map(sender, currency, - will_really_receive, MONEY))
{
Expand Down
9 changes: 3 additions & 6 deletions src/mastercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef _MASTERCOIN
#define _MASTERCOIN 1

#include "mastercore_format.h"

#include "netbase.h"
#include "protocol.h"

Expand Down Expand Up @@ -130,7 +132,6 @@ enum FILETYPES {
#define MASTERCOIN_CURRENCY_TMSC 2

// forward declarations
string FormatDivisibleMP(int64_t n, bool fSign = false);
uint256 send_MP(const string &FromAddress, const string &ToAddress, const string &RedeemAddress, unsigned int CurrencyID, uint64_t Amount);
int64_t feeCheck(const string &address);

Expand Down Expand Up @@ -237,7 +238,7 @@ typedef struct
if (bDivisible)
{
printf("%22s [SO_RESERVE= %22s , ACCEPT_RESERVE= %22s ] %22s\n",
FormatDivisibleMP(money, true).c_str(), FormatDivisibleMP(so_r, true).c_str(), FormatDivisibleMP(a_r, true).c_str(), FormatDivisibleMP(pending, true).c_str());
FormatDivisibleAmount(money, true).c_str(), FormatDivisibleAmount(so_r, true).c_str(), FormatDivisibleAmount(a_r, true).c_str(), FormatDivisibleAmount(pending, true).c_str());
}
else
{
Expand Down Expand Up @@ -362,8 +363,6 @@ int mastercore_handler_block_end(int nBlockNow, CBlockIndex const * pBlockIndex,
int mastercore_handler_tx(const CTransaction &tx, int nBlock, unsigned int idx, CBlockIndex const *pBlockIndex );
int mastercore_save_state( CBlockIndex const *pBlockIndex );

uint64_t rounduint64(double d);

bool isBigEndian(void);

void swapByteOrder16(unsigned short& us);
Expand All @@ -386,15 +385,13 @@ string getPropertyName(unsigned int propertyId);
bool isCrowdsaleActive(unsigned int propertyId);
bool isCrowdsalePurchase(uint256 txid, string address, int64_t *propertyId = NULL, int64_t *userTokens = NULL, int64_t *issuerTokens = NULL);
bool isMPinBlockRange(int starting_block, int ending_block, bool bDeleteFound);
std::string FormatIndivisibleMP(int64_t n);

int ClassB_send(const string &senderAddress, const string &receiverAddress, const string &redemptionAddress, const vector<unsigned char> &data, uint256 & txid, int64_t additional = 0);

uint256 send_INTERNAL_1packet(const string &FromAddress, const string &ToAddress, const string &RedeemAddress, unsigned int CurrencyID, uint64_t Amount, unsigned int CurrencyID_2, uint64_t Amount_2,
unsigned int TransactionType, int64_t additional, int *error_code = NULL);

bool isTestEcosystemProperty(unsigned int property);
int64_t strToInt64(std::string strAmount, bool divisible);

CMPTally *getTally(const string & address);

Expand Down
65 changes: 65 additions & 0 deletions src/mastercore_convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "mastercore_convert.h"

#include <boost/lexical_cast.hpp>

#include <algorithm>
#include <string>
#include <stdint.h>

uint64_t RoundToUInt64(long double d) {
return static_cast<uint64_t>(labs(d + 0.5l));
}

int64_t StrToInt64(const std::string& str, bool divisible)
{
// copy original, so it remains unchanged
std::string strAmount (str);
int64_t nAmount = 0;

// check for a negative (minus sign) and invalidate if present
size_t negSignPos = strAmount.find("-");
if (negSignPos != std::string::npos) return 0;

// convert the string into a usable int64
if (divisible) {
// check for existance of decimal point
size_t pos = strAmount.find(".");
if (pos == std::string::npos) {
// no decimal point but divisible so pad 8 zeros on right
strAmount += "00000000";
} else {
// check for existence of second decimal point, if so invalidate amount
size_t posSecond = strAmount.find(".", pos + 1);
if (posSecond != std::string::npos) return 0;

if ((strAmount.size() - pos) < 9) {
// there are decimals either exact or not enough, pad as needed
std::string strRightOfDecimal = strAmount.substr(pos + 1);
unsigned int zerosToPad = 8 - strRightOfDecimal.size();

// do we need to pad?
if (zerosToPad > 0)
{
for (unsigned int it = 0; it != zerosToPad; it++) {
strAmount += "0";
}
}
} else {
// there are too many decimals, truncate after 8
strAmount = strAmount.substr(0, pos + 9);
}
}
strAmount.erase(std::remove(strAmount.begin(), strAmount.end(), '.'), strAmount.end());
try {
nAmount = boost::lexical_cast<int64_t>(strAmount);
} catch (const boost::bad_lexical_cast &e) {}
} else {
size_t pos = strAmount.find(".");
std::string newStrAmount = strAmount.substr(0, pos);
try {
nAmount = boost::lexical_cast<int64_t>(newStrAmount);
} catch (const boost::bad_lexical_cast &e) {}
}

return nAmount;
}
20 changes: 20 additions & 0 deletions src/mastercore_convert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _MASTERCOIN_CONVERT
#define _MASTERCOIN_CONVERT

#include <string>
#include <stdint.h>

// Converts numbers into 64 bit wide integer and rounds
// up.
uint64_t RoundToUInt64(long double d);

// Converts a string into an integer. Divisible as well as
// indivisible amounts are accepted. Invalid inputs result
// in 0. Any "-" invalidates the string.
// 1 indivisible unit equals 0.00000001 divisible units.
// Divisible amounts are truncated after the 8 digit, right
// behind the first and only decimal point ".".
// Indivisible amounts are truncated after a decimal point.
int64_t StrToInt64(const std::string& str, bool divisible);

#endif // _MASTERCOIN_CONVERT
8 changes: 5 additions & 3 deletions src/mastercore_dex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ using namespace mastercore;

#include "mastercore_dex.h"
#include "mastercore_tx.h"
#include "mastercore_convert.h"
#include "mastercore_format.h"

extern int msc_debug_dex, msc_debug_metadex, msc_debug_metadex2;

Expand Down Expand Up @@ -274,7 +276,7 @@ std::string CMPMetaDEx::ToString() const
{
return strprintf("%34s in %d/%03u, txid: %s, trade #%u %s for #%u %s; unit_price = %lu.%010lu, inverse= %lu.%010lu",
addr.c_str(), block, idx, txid.ToString().substr(0,10).c_str(),
currency, FormatDivisibleMP(amount_original), desired_currency, FormatDivisibleMP(desired_amount_original),
currency, FormatDivisibleAmount(amount_original), desired_currency, FormatDivisibleAmount(desired_amount_original),
price_int, price_frac, inverse_int, inverse_frac);
}

Expand Down Expand Up @@ -339,7 +341,7 @@ int rc = DEX_ERROR_SELLOFFER;
// AND we must also re-adjust the BTC desired in this case...
BTC = amount_desired * balanceReallyAvailable;
BTC /= (double)nValue;
amount_desired = rounduint64(BTC);
amount_desired = RoundToUInt64(BTC);

nValue = balanceReallyAvailable;

Expand Down Expand Up @@ -561,7 +563,7 @@ p_accept = DEx_getAccept(seller, curr, buyer);
double perc_X = (double)BTC_paid/BTC_desired_original;
double Purchased = offer_amount_original * perc_X;

uint64_t units_purchased = rounduint64(Purchased);
uint64_t units_purchased = RoundToUInt64(Purchased);

const uint64_t nActualAmount = p_accept->getAcceptAmountRemaining(); // actual amount desired, in the Accept

Expand Down
55 changes: 55 additions & 0 deletions src/mastercore_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "mastercore_format.h"

// TODO: find a way to use strprintf/tfm::format directly
// It currently relies on util.h and tinyformat.h, but has heavy dependencies
#include "util.h"

#include <string>
#include <stdint.h>

std::string FormatTokenAmount(int64_t n)
{
// Default token amounts are formatted as indivisible
// token which aligns well with an integer input
return FormatIndivisibleAmount(n);
}

std::string FormatTokenAmount(int64_t n, bool divisible)
{
if (divisible)
return FormatDivisibleAmount(n);

return FormatIndivisibleAmount(n);
}

std::string FormatIndivisibleAmount(int64_t n)
{
return tfm::format("%d", n);
}

std::string FormatDivisibleAmount(int64_t n)
{
// Prepend sign only for negative numbers
bool is_negative = (n < 0);
return FormatDivisibleAmount(n, is_negative);
}

std::string FormatDivisibleAmount(int64_t n, bool sign)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
int64_t n_abs = (n > 0 ? n : -n);
int64_t quotient = n_abs / COIN;
int64_t remainder = n_abs % COIN;
std::string str = tfm::format("%d.%08d", quotient, remainder);

if (!sign)
return str;

if (n < 0)
str.insert((unsigned int) 0, 1, '-');
else
str.insert((unsigned int) 0, 1, '+');

return str;
}
Loading