aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-04-30 13:52:36 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commitf416f33d7cf879f543be9c804c20b0c9cb76de4d (patch)
tree5375a3a23661499463b09e19e4e018b13210562a /src
parentd46f2063e04ee49c631e8966029affd8dd1fa5e4 (diff)
Include fraction in timestamp. Timestamp is now in microseconds
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp46
1 files changed, 24 insertions, 22 deletions
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 <sibs/SafeSerializer.hpp>
#include <sibs/SafeDeserializer.hpp>
#include <cassert>
+#include <sys/time.h>
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(&currentLocalTime, 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<Hash> hashRequestKey = make_shared<Hash>(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<StagedObject>(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<StagedObject>(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<StagedObject>(stagedAddObject, nodeInfo.getRequestHash()));
}
@@ -497,9 +499,13 @@ namespace odhtdb
{
this_thread::sleep_for(10ms);
}
+
+ struct timeval currentLocalTime;
+ gettimeofday(&currentLocalTime, 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<u64>();
- // 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<u64>();
- // 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<DatabaseOperation>();