From f416f33d7cf879f543be9c804c20b0c9cb76de4d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 30 Apr 2018 13:52:36 +0200 Subject: Include fraction in timestamp. Timestamp is now in microseconds --- src/Database.cpp | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index f2d0fe1..aa852f6 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -15,6 +15,7 @@ #include #include #include +#include using namespace dht; using namespace std; @@ -23,6 +24,7 @@ using namespace chrono_literals; static int databaseCount = 0; // TODO: Verify time_t is always signed static time_t timeOffset = 0; // Updated by comparing local time with ntp server +static odhtdb::u64 timeOffsetFraction = 0; static thread *ntpThread = nullptr; static bool timestampSynced = false; static InfoHash CREATE_DATA_HASH = InfoHash::get("__odhtdb__.create_data"); @@ -118,7 +120,7 @@ namespace odhtdb ntpThread = new thread([]() { - ntp::NtpClient ntpClient("pool.ntp.org"); + ntp::NtpClient ntpClient("pool.ntp.org", 10); while(databaseCount > 0) { int fetchRetryCounter = 0; @@ -127,7 +129,10 @@ namespace odhtdb try { ntp::NtpTimestamp ntpTimestamp = ntpClient.getTimestamp(); - timeOffset = time(nullptr) - ntpTimestamp.seconds; + struct timeval currentLocalTime; + gettimeofday(¤tLocalTime, NULL); + timeOffset = currentLocalTime.tv_sec - ntpTimestamp.seconds; + timeOffsetFraction = currentLocalTime.tv_usec - ntpTimestamp.fractions; timestampSynced = true; break; } @@ -297,9 +302,8 @@ namespace odhtdb // Header sibs::SafeSerializer serializer; serializer.add(DATABASE_CREATE_PACKET_STRUCTURE_VERSION); // Packet structure version - // TODO: Append fractions to get real microseconds time - u64 timestampMicroseconds = ((u64)getSyncedTimestampUtc().seconds) * 1000000ull; - serializer.add(timestampMicroseconds); + u64 timestampCombined = getSyncedTimestampUtc().getCombined(); + serializer.add(timestampCombined); serializer.add((u8*)nodeAdminUser->getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES); serializer.add(adminGroupId.data, adminGroupId.size()); @@ -318,7 +322,7 @@ namespace odhtdb DataView requestData = combine(serializer, encryptedBody); shared_ptr hashRequestKey = make_shared(requestData.data, requestData.size); databaseStorage.setNodeDecryptionKey(*hashRequestKey, DataView(encryptedBody.getKey().data, encryptedBody.getKey().size)); - databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampMicroseconds, (const u8*)requestData.data, requestData.size, serializer.getBuffer().size()); + databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampCombined, (const u8*)requestData.data, requestData.size, serializer.getBuffer().size()); stagedCreateObjects.emplace_back(make_unique(requestData, hashRequestKey)); @@ -350,9 +354,8 @@ namespace odhtdb sibs::SafeSerializer serializer; serializer.add(DATABASE_ADD_PACKET_STRUCTURE_VERSION); - // TODO: Append fractions to get real microseconds time - u64 timestampMicroseconds = ((u64)getSyncedTimestampUtc().seconds) * 1000000ull; - serializer.add(timestampMicroseconds); + u64 timestampCombined = getSyncedTimestampUtc().getCombined(); + serializer.add(timestampCombined); serializer.add(DatabaseOperation::ADD_DATA); DataView encryptionKey(nodeInfo.getNodeEncryptionKey()->data, ENCRYPTION_KEY_BYTE_SIZE); @@ -362,7 +365,7 @@ namespace odhtdb DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData); Hash requestDataHash(stagedAddObject.data, stagedAddObject.size); DataView encryptedDataView((char*)requestData.data + serializer.getBuffer().size(), requestData.size - serializer.getBuffer().size()); - databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith, timestampMicroseconds, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); + databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith, timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); delete[] (char*)requestData.data; stagedAddObjects.emplace_back(make_unique(stagedAddObject, nodeInfo.getRequestHash())); @@ -399,9 +402,8 @@ namespace odhtdb sibs::SafeSerializer serializer; serializer.add(DATABASE_ADD_PACKET_STRUCTURE_VERSION); - // TODO: Append fractions to get real microseconds time - u64 timestampMicroseconds = ((u64)getSyncedTimestampUtc().seconds) * 1000000ull; - serializer.add(timestampMicroseconds); + u64 timestampCombined = getSyncedTimestampUtc().getCombined(); + serializer.add(timestampCombined); serializer.add(DatabaseOperation::ADD_USER); assert(userToAddName.size() <= 255); @@ -423,7 +425,7 @@ namespace odhtdb DataView encryptedDataView(nullptr, 0); auto userToAdd = RemoteUser::create(userToAddPublicKey, userToAddName, groupToAddUserTo); databaseStorage.addUser(*nodeInfo.getRequestHash(), userToAdd); - databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith, timestampMicroseconds, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); + databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith, timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); stagedAddObjects.emplace_back(make_unique(stagedAddObject, nodeInfo.getRequestHash())); } @@ -497,9 +499,13 @@ namespace odhtdb { this_thread::sleep_for(10ms); } + + struct timeval currentLocalTime; + gettimeofday(¤tLocalTime, NULL); + ntp::NtpTimestamp timestamp; - timestamp.seconds = time(nullptr) - timeOffset; - timestamp.fractions = 0; // TODO: Set this + timestamp.seconds = currentLocalTime.tv_sec - timeOffset; + timestamp.fractions = currentLocalTime.tv_usec - timeOffsetFraction; return timestamp; } @@ -517,9 +523,7 @@ namespace odhtdb } u64 creationDate = deserializer.extract(); - // TODO: Append fractions to get real microseconds time - u64 timestampMicroseconds = ((u64)getSyncedTimestampUtc().seconds) * 1000000ull; - if(creationDate > timestampMicroseconds) + if(creationDate > getSyncedTimestampUtc().getCombined()) throw sibs::DeserializeException("Packet is from the future"); char creatorPublicKeyRaw[PUBLIC_KEY_NUM_BYTES]; @@ -560,9 +564,7 @@ namespace odhtdb } u64 creationDate = deserializerUnsigned.extract(); - // TODO: Append fractions to get real microseconds time - u64 timestampMicroseconds = ((u64)getSyncedTimestampUtc().seconds) * 1000000ull; - if(creationDate > timestampMicroseconds) + if(creationDate > getSyncedTimestampUtc().getCombined()) throw sibs::DeserializeException("Packet is from the future"); DatabaseOperation operation = deserializerUnsigned.extract(); -- cgit v1.2.3