diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-05-01 14:57:29 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-05-01 14:57:32 +0200 |
commit | b0cc417bc9289fdd5c98028136e37c6a6ee62944 (patch) | |
tree | bb1646b4436d56bc10e103af0c03e157670f32fd | |
parent | 8da5ba7f057978b224868028679cbda9e11089ab (diff) |
Update ntp dependency with bug fix, make error messages better
-rw-r--r-- | include/odhtdb/DatabaseStorage.hpp | 4 | ||||
-rw-r--r-- | project.conf | 4 | ||||
-rw-r--r-- | src/Database.cpp | 26 | ||||
-rw-r--r-- | src/DatabaseStorage.cpp | 8 | ||||
-rw-r--r-- | tests/main.cpp | 14 |
5 files changed, 50 insertions, 6 deletions
diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index 9cfe12d..9f1a992 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -165,6 +165,10 @@ namespace odhtdb // Safe to call multiple times. std::vector<NodeLocalUser> getLocalNodeUsers(const Signature::KeyPair &keyPair); + // Returns data in node by its id. Doesn't include 'create' requests. + // Returns nullptr if not data with @dataHash exists. + DatabaseStorageObject* getDataById(const Hash &dataHash); + // Returns true and node decryption key if node exists and we have the decryption key, // otherwise return false and OwnedMemory with data set to nullptr std::pair<bool, std::shared_ptr<OwnedMemory>> getNodeDecryptionKey(const Hash &nodeHash); diff --git a/project.conf b/project.conf index 1d687b0..c878112 100644 --- a/project.conf +++ b/project.conf @@ -11,8 +11,8 @@ expose_include_dirs = ["include"] [dependencies] opendht = "1.7.0" libsodium = "1.0.16" -ntpclient = "0.2.1" -sibs-serializer = "0.2.0" +ntpclient = "0.3.0" +sibs-serializer = "0.2.1" boost-filesystem = "1.66.0" boost-uuid = "1.66.0" argon2 = "2017.12.27" diff --git a/src/Database.cpp b/src/Database.cpp index 8cca298..85e56d3 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -521,8 +521,16 @@ namespace odhtdb } u64 creationDate = deserializer.extract<u64>(); - if(creationDate > getSyncedTimestampUtc().getCombined()) - throw sibs::DeserializeException("Packet is from the future"); + auto currentTimestamp = getSyncedTimestampUtc(); + if(creationDate > currentTimestamp.getCombined()) + { + auto creationDateTimestamp = ntp::NtpTimestamp::fromCombined(creationDate); + string errMsg = "Packet is from the future. Packet creation time: "; + errMsg += to_string((double)creationDateTimestamp.seconds + creationDateTimestamp.getFractionAsSeconds()); + errMsg += ", current time: "; + errMsg += to_string((double)currentTimestamp.seconds + currentTimestamp.getFractionAsSeconds()); + throw sibs::DeserializeException(errMsg); + } char creatorPublicKeyRaw[PUBLIC_KEY_NUM_BYTES]; deserializer.extract((u8*)creatorPublicKeyRaw, PUBLIC_KEY_NUM_BYTES); @@ -562,8 +570,16 @@ namespace odhtdb } u64 creationDate = deserializerUnsigned.extract<u64>(); - if(creationDate > getSyncedTimestampUtc().getCombined()) - throw sibs::DeserializeException("Packet is from the future"); + auto currentTimestamp = getSyncedTimestampUtc(); + if(creationDate > currentTimestamp.getCombined()) + { + auto creationDateTimestamp = ntp::NtpTimestamp::fromCombined(creationDate); + string errMsg = "Packet is from the future. Packet creation time: "; + errMsg += to_string((double)creationDateTimestamp.seconds + creationDateTimestamp.getFractionAsSeconds()); + errMsg += ", current time: "; + errMsg += to_string((double)currentTimestamp.seconds + currentTimestamp.getFractionAsSeconds()); + throw sibs::DeserializeException(errMsg); + } DatabaseOperation operation = deserializerUnsigned.extract<DatabaseOperation>(); #if 0 @@ -691,6 +707,8 @@ namespace odhtdb Log::debug("Got add data"); try { + if(databaseStorage.getDataById(requestDataHash)) + throw DatabaseStorageAlreadyExists("Add data request hash is equal to hash already in storage (duplicate data?)"); deserializeAddRequest(value, requestDataHash, nodeHash, encryptionKey); //Log::debug("Got add object, timestamp: %zu", addObject.timestamp); } diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 233f7c6..9c64a86 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -822,6 +822,14 @@ namespace odhtdb return localUsers; } + DatabaseStorageObject* DatabaseStorage::getDataById(const Hash &dataHash) + { + auto storageIt = storedDataHash.find(dataHash); + if(storageIt != storedDataHash.end()) + return storageIt->second; + return nullptr; + } + std::pair<bool, std::shared_ptr<OwnedMemory>> DatabaseStorage::getNodeDecryptionKey(const Hash &nodeHash) { auto nodeDecryptionKeyIt = nodeDecryptionKeyMap.find(nodeHash); diff --git a/tests/main.cpp b/tests/main.cpp index 7e8f489..104c9b8 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -129,6 +129,18 @@ void testCachedIdentity() printf("Certificate data size: %d, serialized data: %s\n", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str()); } +void testTimestamp(const Database &database) +{ + auto timestamp1 = database.getSyncedTimestampUtc(); + this_thread::sleep_for(chrono::milliseconds(100)); + auto timestamp2 = database.getSyncedTimestampUtc(); + + if(timestamp2.getCombined() > timestamp1.getCombined()) + Log::debug("Second timestamp is more than first one, as expected"); + else + fail("Second timestamp is not more than first one for some reason"); +} + int main() { Log::debug("Starting tests..."); @@ -143,6 +155,8 @@ int main() // TODO: Setup local bootstrap node for tests Database database("bootstrap.ring.cx", 4222, "storage"); + testTimestamp(database); + int createNodeCounter = 0; int addDataCounter = 0; int addUserCounter = 0; |