Exemple #1
0
void testFactory(vector<char> data, seconds duration, seconds maxTimeToTest, int id)
{
	Puzzle p;
	HardwareSpeedTester hst(1000, maxTimeToTest);
	TimeCapsuleFactory<char> tcf(hst);
	//char rawdata[] = "Hey, I hope it will finish in time.. tho probably not :D ...";
	auto capsule = tcf.createTimeCapsule(p, data, duration);
	capsule.save("capsule_" + to_string(id) + ".dat");

	Capsule<char> capsule2;
	capsule2.load("capsule_" + to_string(id) + ".dat");

	auto crdata = capsule.getCryptedData();
	auto crdata2 = capsule2.getCryptedData();

	assert(capsule.getBase() == capsule2.getBase());
	assert(capsule.getN() == capsule2.getN());
	assert(capsule.getIV() == capsule2.getIV());
	assert(capsule.getCryptedKey() == capsule2.getCryptedKey());
	assert(capsule.getNumberOfOperations() == capsule2.getNumberOfOperations());
	Logger::log("Original crypted data size: " + to_string(crdata.size()));
	Logger::log("Copypasted crypted data size: " + to_string(crdata2.size()));

	string datastr(crdata.begin(), crdata.end());
	string datastr2(crdata2.begin(), crdata2.end());
	Logger::log("Original   crypted data: " + datastr);
	Logger::log("Copypasted crypted data: " + datastr2);

	//cout << capsule.getCryptedData().size() << ", " << capsule2.getCryptedData().size() << endl;
	//assert(capsule.getCryptedData().size() == capsule2.getCryptedData().size());
	//for (size_t i = 0; i < capsule.getCryptedData().size(); ++i)
	//	assert(capsule.getCryptedData()[i] == capsule2.getCryptedData()[i]);

	Puzzle p2(capsule2.getBase());

	auto start = chrono::high_resolution_clock::now();
	auto key = p2.solve(capsule2.getCryptedKey(), capsule2.getNumberOfOperations(), capsule2.getN());
	auto end = chrono::high_resolution_clock::now();

	Logger::log("Time specified to decode: " + to_string(duration.count()) + " seconds");
	Logger::log("Time taken to decode: " + to_string(chrono::duration_cast<seconds>(end - start).count()) + " seconds");
	
	Encryptor<char> cr;
	auto databack = cr.decrypt(capsule2.getCryptedData(), key, capsule2.getIV());
	string tmpdata(data.begin(), data.end());
	string tmpdataback(databack.begin(), databack.end());
	Logger::log("Original  data: " + tmpdata);
	Logger::log("Data decrypted: " + tmpdataback);

	/*char dummychar;
	if (crdata.size() != crdata2.size())
	{
		Logger::log("PARA VAN!");
		cin >> dummychar;
	}*/
}
Exemple #2
0
	Target GetNextTargetRequired(const BlockHeader& headerLast, const Block& block) override {
		Target r;
		if (headerLast.Height > 181200) {
			r = base::GetNextTargetRequired(headerLast, block);
			if (headerLast.Height < 220000)
				r = std::min(r, TERRACOIN_FIVE_THOUSAND_LIMIT);
			return r;
		}

		if (headerLast.Height <= 101631)
			return base::GetNextTargetRequired(headerLast, block);

		if (block.get_Timestamp()-headerLast.Timestamp > ChainParams.BlockSpan*10 && headerLast.Height <= 175000)
			r = Target(BigInteger(headerLast.get_DifficultyTarget()) * (headerLast.Height>101631 && headerLast.Height<103791 ? 10 : 2));
		else {
			const seconds PER_BLOCK_SPAN = duration_cast<seconds>(ChainParams.BlockSpan);
			vector<seconds> durations(2160);
			BlockHeader b = headerLast;
			for (vector<seconds>::reverse_iterator it=durations.rbegin(), e=durations.rend(); it!=e; ++it) {
				BlockHeader prev = b.GetPrevHeader();
				seconds dur = duration_cast<seconds>(exchange(b, prev).get_Timestamp()-prev.get_Timestamp());
				if (headerLast.Height > 110322) {
					dur = std::min(PER_BLOCK_SPAN*3/2, dur);
					if (dur.count() >= 0)
						dur = std::max(PER_BLOCK_SPAN/2, dur);
				}
				*it = dur.count()<0 && headerLast.Height>104290 ? PER_BLOCK_SPAN : dur;
			}
			float acc = (float)PER_BLOCK_SPAN.count();
			for (int i=0; i<durations.size(); ++i)
				acc = headerLast.Height>110322 ? 0.06f*durations[i].count() + (1-0.06f)*acc				// float/double conversions are not well defined. Another way to calculate diverges with reference TerraCoin implementation
									: 0.09f*durations[i].count() + (1-0.09f)*acc;
			seconds actualSpan = clamp(seconds((int)acc), PER_BLOCK_SPAN/2, PER_BLOCK_SPAN * (headerLast.Height>110322 ? 2 : 4));
			r = Target(BigInteger(headerLast.get_DifficultyTarget()) * actualSpan.count() / PER_BLOCK_SPAN.count());
		}
		return headerLast.Height>104290 ? std::min(r, TERRACOIN_FIVE_THOUSAND_LIMIT) : r;
	}
log_entry::log_entry(logger& l, logger::LogLevel level):
    mLogger(l),
    mLevel(level)
{
    if(mLevel < 0) return;
    using namespace std::chrono;
    const milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
    const seconds s = duration_cast<seconds>(ms);
    const std::time_t t = s.count();
    const std::size_t fractional_seconds = ms.count() % 1000;

    mLogger.lock();
    const char timeFormat[] = "%Y-%m-%d %H:%M:%S";
    const size_t buffSize = 100;
    char buff[buffSize];
    if(0 != std::strftime(buff, buffSize, timeFormat, std::localtime(&t)))
    {
        mLogger.get() << buff;
    }
    else
    {
        size_t dBuffSize = buffSize;
        std::unique_ptr<char[]> dbuff;
        do
        {
            dBuffSize *= 2;
            dbuff.reset(new char[dBuffSize]);
        }
        while(0 == std::strftime(dbuff.get(), dBuffSize, timeFormat, std::localtime(&t)));
        mLogger.get() << dbuff.get();
    }
    mLogger.get() << '.' << fractional_seconds << " ";

    if(level >= 0 && level <= logger::logDEBUG)
    {
        const char* levels[] = {"ERROR","WARNING","INFO","DEBUG"};
        mLogger.get() << levels[level];
    }
    mLogger.get() << ": ";
}
Exemple #4
0
 quantity(seconds d) : q_(d.count()) {}  // note:  only User1::seconds needed here
Exemple #5
0
 void Settings::set_default_queue_redelivery_interval( const seconds value )
 {
     restbed::Settings::set_property( "redelivery-interval", ::to_string( value.count( ) ) );
 }
Exemple #6
0
seconds abs(const seconds& t) {
	return seconds(abs(t.count()));
}