20 #include <boost/assign/list_of.hpp>
21 #include <boost/algorithm/string.hpp>
22 #include <boost/algorithm/string/find.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/format.hpp>
25 #include <boost/filesystem.hpp>
27 #include "json/json_spirit_utils.h"
28 #include "json/json_spirit_value.h"
30 #include "leveldb/db.h"
31 #include "leveldb/write_batch.h"
34 using namespace boost;
52 std::string nextIdStr;
53 unsigned int res = propertyID;
55 Object spInfo = info.
toJSON();
59 string spValue = write_string(Value(spInfo),
false);
63 leveldb::WriteBatch commitBatch;
65 leveldb::ReadOptions readOpts;
66 readOpts.fill_cache =
false;
67 leveldb::WriteOptions writeOptions;
68 writeOptions.sync =
true;
71 if (
false == pDb->Get(readOpts, spKey, &spPrevValue).IsNotFound()) {
72 commitBatch.Put(spPrevKey, spPrevValue);
74 commitBatch.Put(spKey, spValue);
75 pDb->Write(writeOptions, &commitBatch);
77 file_log(
"Updated LevelDB with SP data successfully\n");
83 std::string nextIdStr;
90 res = next_test_spid++;
96 Object spInfo = info.
toJSON();
100 string spValue = write_string(Value(spInfo),
false);
105 string existingEntry;
106 leveldb::ReadOptions readOpts;
107 readOpts.fill_cache =
true;
108 if (
false == pDb->Get(readOpts, spKey, &existingEntry).IsNotFound() &&
false == boost::equals(spValue, existingEntry)) {
109 file_log(
"%s WRITING SP %d TO LEVELDB WHEN A DIFFERENT SP ALREADY EXISTS FOR THAT ID!!!\n", __FUNCTION__, res);
110 }
else if (
false == pDb->Get(readOpts, txIndexKey, &existingEntry).IsNotFound() &&
false == boost::equals(txValue, existingEntry)) {
111 file_log(
"%s WRITING INDEX TXID %s : SP %d IS OVERWRITING A DIFFERENT VALUE!!!\n", __FUNCTION__, info.
txid.
ToString().c_str(), res);
115 leveldb::WriteOptions writeOptions;
116 writeOptions.sync =
true;
118 leveldb::WriteBatch commitBatch;
119 commitBatch.Put(spKey, spValue);
120 commitBatch.Put(txIndexKey, txValue);
122 pDb->Write(writeOptions, &commitBatch);
138 leveldb::ReadOptions readOpts;
139 readOpts.fill_cache =
true;
143 if (
false == pDb->Get(readOpts, spKey, &spInfoStr).ok()) {
149 if (
false == read_string(spInfoStr, spInfoVal) || spInfoVal.type() != obj_type ) {
154 Object &spInfo = spInfoVal.get_obj();
166 leveldb::ReadOptions readOpts;
167 readOpts.fill_cache =
true;
170 leveldb::Iterator *iter = pDb->NewIterator(readOpts);
172 bool res = (iter->Valid() && iter->key().compare(spKey) == 0);
181 unsigned int res = 0;
182 leveldb::ReadOptions readOpts;
183 readOpts.fill_cache =
true;
187 if (pDb->Get(readOpts, txIndexKey, &spidStr).ok()) {
188 res = boost::lexical_cast<
unsigned int>(spidStr);
197 int remainingSPs = 0;
198 leveldb::WriteBatch commitBatch;
200 leveldb::ReadOptions readOpts;
201 readOpts.fill_cache =
false;
202 leveldb::Iterator *iter = pDb->NewIterator(readOpts);
203 for (iter->Seek(
"sp-"); iter->Valid() && iter->key().starts_with(
"sp-"); iter->Next()) {
206 if (read_string(iter->value().ToString(), spInfoVal) && spInfoVal.type() == obj_type ) {
214 commitBatch.Delete(iter->key());
215 commitBatch.Delete(txIndexKey);
217 std::vector<std::string> vstr;
218 std::string key = iter->key().ToString();
219 boost::split(vstr, key, boost::is_any_of(
"-"), token_compress_on);
220 unsigned int propertyID = boost::lexical_cast<
unsigned int>(vstr[1]);
225 if (
false == pDb->Get(readOpts, spPrevKey, &spPrevValue).IsNotFound()) {
227 commitBatch.Put(iter->key(), spPrevValue);
228 commitBatch.Delete(spPrevKey);
247 leveldb::WriteOptions writeOptions;
248 writeOptions.sync =
true;
250 pDb->Write(writeOptions, &commitBatch);
261 CrowdMap::iterator my_it =
my_crowds.find(address);
263 if (my_it !=
my_crowds.end())
return &(my_it->second);
282 return "Property Name Not Found";
291 if (foundPropertyId == propertyId)
return true;
300 FILE *fp = fopen(pathInfo.string().c_str(),
"a");
304 file_log(
"\nPROBLEM writing %s, errno= %d\n",
INFO_FILENAME, errno);
309 fprintf(fp,
"\nCrowdsale ended: %s\n", bExpired ?
"Expired" :
"Was closed");
311 crowd.
print(address, fp);
324 uint64_t numProps,
unsigned char issuerPerc,
const std::map<std::string, std::vector<uint64_t> > txFundraiserData,
325 const uint64_t amountPremined )
329 double totalCreated = 0;
330 double issuerPercentage = (double) (issuerPerc * 0.01);
332 std::map<std::string, std::vector<uint64_t> >::const_iterator it;
335 for(it = txFundraiserData.begin(); it != txFundraiserData.end(); it++) {
338 uint64_t currentSecs = it->second.at(1);
339 double amtTransfer = it->second.at(0);
342 uint64_t bonusSeconds = fundraiserSecs - currentSecs;
345 double weeks = bonusSeconds / (double) 604800;
348 double ebPercentage = weeks * bonusPerc;
349 double bonusPercentage = ( ebPercentage / 100 ) + 1;
352 double createdTokens;
357 createdTokens = (amtTransfer/1e8) * (
double) numProps * bonusPercentage ;
362 totalCreated += createdTokens;
367 createdTokens = (uint64_t) ( (amtTransfer/1e8) * (double) numProps * bonusPercentage);
369 totalCreated += createdTokens;
374 double totalPremined = totalCreated * issuerPercentage;
378 if( 2 == propType ) {
379 missedTokens = totalPremined - amountPremined;
381 missedTokens = (uint64_t) (totalPremined - amountPremined);
402 std::map<std::string, std::vector<uint64_t> > database = crowd->
getDatabase();
403 std::map<std::string, std::vector<uint64_t> >::const_iterator it;
404 for(it = database.begin(); it != database.end(); it++)
406 string tmpTxid = it->first;
407 string compTxid = txid.
GetHex().c_str();
408 if (tmpTxid == compTxid)
410 int64_t tmpUserTokens = it->second.at(2);
411 int64_t tmpIssuerTokens = it->second.at(3);
412 *propertyId = foundPropertyId;
413 *userTokens = tmpUserTokens;
414 *issuerTokens = tmpIssuerTokens;
421 int64_t tmpPropertyId;
425 for (tmpPropertyId = 1; tmpPropertyId<nextSPID; tmpPropertyId++)
432 std::map<std::string, std::vector<uint64_t> > database = sp.
historicalData;
433 std::map<std::string, std::vector<uint64_t> >::const_iterator it;
434 for(it = database.begin(); it != database.end(); it++)
436 string tmpTxid = it->first;
437 string compTxid = txid.
GetHex().c_str();
438 if (tmpTxid == compTxid)
440 int64_t tmpUserTokens = it->second.at(2);
441 int64_t tmpIssuerTokens = it->second.at(3);
442 *propertyId = tmpPropertyId;
443 *userTokens = tmpUserTokens;
444 *issuerTokens = tmpIssuerTokens;
458 std::map<std::string, std::vector<uint64_t> > database = sp.
historicalData;
459 std::map<std::string, std::vector<uint64_t> >::const_iterator it;
460 for(it = database.begin(); it != database.end(); it++)
462 string tmpTxid = it->first;
463 string compTxid = txid.
GetHex().c_str();
464 if (tmpTxid == compTxid)
466 int64_t tmpUserTokens = it->second.at(2);
467 int64_t tmpIssuerTokens = it->second.at(3);
468 *propertyId = tmpPropertyId;
469 *userTokens = tmpUserTokens;
470 *issuerTokens = tmpIssuerTokens;
484 CrowdMap::iterator it =
my_crowds.find(address);
489 file_log(
"%s() FOUND MAXED OUT CROWDSALE from address= '%s', erasing...\n", __FUNCTION__, address.c_str());
517 unsigned int how_many_erased = 0;
518 CrowdMap::iterator my_it =
my_crowds.begin();
529 file_log(
"%s() FOUND EXPIRED CROWDSALE from address= '%s', erasing...\n", __FUNCTION__, (my_it->first).c_str());
574 return how_many_erased;
585 return (
char *)
"*** property type error ***";
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
int popBlock(uint256 const &block_hash)
#define FORMAT_BOOST_TXINDEXKEY
bool update_tally_map(string who, unsigned int which_currency, int64_t amount, TallyType ttype)
#define MSC_PROPERTY_TYPE_INDIVISIBLE
#define TEST_ECO_PROPERTY_1
bool hasSP(unsigned int spid)
std::map< std::string, std::vector< uint64_t > > getDatabase() const
void print(const string &address, FILE *fp=stdout) const
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
#define OMNI_PROPERTY_MSC
CChain chainActive
The currently-connected chain of blocks.
bool isPropertyDivisible(unsigned int propertyId)
std::map< std::string, std::vector< uint64_t > > historicalData
int calculateFractional(unsigned short int propType, unsigned char bonusPerc, uint64_t fundraiserSecs, uint64_t numProps, unsigned char issuerPerc, const std::map< std::string, std::vector< uint64_t > > txFundraiserData, const uint64_t amountPremined)
#define OMNI_PROPERTY_TMSC
bool isCrowdsalePurchase(uint256 txid, string address, int64_t *propertyId=NULL, int64_t *userTokens=NULL, int64_t *issuerTokens=NULL)
uint64_t getIssuerCreated() const
void dumpCrowdsaleInfo(const string &address, CMPCrowd &crowd, bool bExpired=false)
bool isCrowdsaleActive(unsigned int propertyId)
void fromJSON(Object const &json)
bool getSP(unsigned int spid, Entry &info)
#define MSC_PROPERTY_TYPE_DIVISIBLE
std::string GetHex() const
void eraseMaxedCrowdsale(const string &address, uint64_t blockTime, int block)
string getPropertyName(unsigned int propertyId)
unsigned int putSP(unsigned char ecosystem, Entry const &info)
char * c_strPropertyType(int i)
The block chain is a tree shaped structure starting with the genesis block at the root...
std::string ToString() const
unsigned int findSPByTX(uint256 const &txid)
unsigned int peekNextSPID(unsigned char ecosystem)
CMPCrowd * getCrowd(const string &address)
unsigned int eraseExpiredCrowdsale(CBlockIndex const *pBlockIndex)
int64_t GetBlockTime() const
uint64_t getDeadline() const
unsigned int updateSP(unsigned int propertyID, Entry const &info)
#define FORMAT_BOOST_SPKEY
unsigned int getPropertyId() const
uint256 GetBlockHash() const