aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-04-30 13:52:36 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-04-30 13:52:41 +0200
commit670e3eed2703dcee1dee0508e45d8454cae78544 (patch)
treebf4ddf2e1023a0425f677d399a391ad0652a2306
parent54de8e3b0b353b8c62b566dabb7b082a1cdd371e (diff)
Include fraction in timestamp. Timestamp is now in microseconds
-rw-r--r--include/odhtdb/Database.hpp2
-rw-r--r--src/Database.cpp46
2 files changed, 25 insertions, 23 deletions
diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp
index f222215..18ae5a1 100644
--- a/include/odhtdb/Database.hpp
+++ b/include/odhtdb/Database.hpp
@@ -171,12 +171,12 @@ namespace odhtdb
void setOnAddUserCallback(std::function<void(const DatabaseAddUserRequest&)> callbackFunc);
DatabaseStorage& getStorage();
+ ntp::NtpTimestamp getSyncedTimestampUtc() const;
private:
// Throws CommitCreateException on failure
void commitStagedCreateObject(const std::unique_ptr<StagedObject> &stagedObject);
// Throws CommitAddException on failure
void commitStagedAddObject(const std::unique_ptr<StagedObject> &stagedObject);
- ntp::NtpTimestamp getSyncedTimestampUtc() const;
void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey);
void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey);
bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey);
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>();