aboutsummaryrefslogtreecommitdiff
path: root/include/Database.hpp
blob: 6bc7bc52ff609969e3adcaf98c329f584e5f060a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once

#include "types.hpp"
#include "Key.hpp"
#include "DataView.hpp"
#include "DatabaseStorage.hpp"
#include "Hash.hpp"
#include "utils.hpp"
#include "StagedObject.hpp"
#include "Signature.hpp"
#include "Permission.hpp"
#include "DatabaseNode.hpp"
#include <opendht/dhtrunner.h>
#include <vector>
#include <ntp/NtpClient.hpp>
#include <boost/filesystem/path.hpp>
#include <stdexcept>

namespace odhtdb
{
    class User;
    class LocalUser;
    class Group;
    
    class CommitCreateException : public std::runtime_error
    {
    public:
        CommitCreateException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class CommitAddException : public std::runtime_error
    {
    public:
        CommitAddException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class DatabaseCreateException : public std::runtime_error
    {
    public:
        DatabaseCreateException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class DatabaseAddException : public std::runtime_error
    {
    public:
        DatabaseAddException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    enum class DatabaseOperation : u8
    {
        ADD_DATA,
        ADD_USER
    };
    
    struct DatabaseCreateRequest
    {
        DISABLE_COPY(DatabaseCreateRequest)
        
        u64 timestamp; // In microseconds
        Group *creatorGroup;
        std::string name;
        
        DatabaseCreateRequest(u64 _timestamp, Group *_creatorGroup, std::string &&_name) : 
            timestamp(_timestamp),
            creatorGroup(_creatorGroup),
            name(std::move(_name))
        {
            
        }
        
        DatabaseCreateRequest(DatabaseCreateRequest &&other)
        {
            timestamp = other.timestamp;
            creatorGroup = other.creatorGroup;
            name = std::move(other.name);
            
            other.timestamp = 0;
            other.creatorGroup = nullptr;
        }
    };

    struct DatabaseAddRequest
    {
        DISABLE_COPY(DatabaseAddRequest)
        
        u64 timestamp; // In microseconds
        const User *creatorUser;
        Decryption decryptedData;
        
        DatabaseAddRequest(u64 _timestamp, const User *_creatorUser, Decryption &&_decryptedData) : 
            timestamp(_timestamp),
            creatorUser(_creatorUser),
            decryptedData(std::move(_decryptedData))
        {
            
        }
        
        DatabaseAddRequest(DatabaseAddRequest &&other)
        {
            timestamp = other.timestamp;
            creatorUser = other.creatorUser;
            decryptedData = std::move(other.decryptedData);
            
            other.timestamp = 0;
            other.creatorUser = nullptr;
        }
    };
    
    class DatabaseCreateResponse
    {
    public:
        DatabaseCreateResponse(LocalUser *nodeAdminUser, const std::shared_ptr<char*> &key, const std::shared_ptr<Hash> &hash);
        
        const LocalUser* getNodeAdminUser() const;
        const std::shared_ptr<char*> getNodeEncryptionKey() const;
        const std::shared_ptr<Hash> getRequestHash() const;
    private:
        LocalUser *nodeAdminUser;
        std::shared_ptr<char*> key;
        std::shared_ptr<Hash> hash;
    };

    class Database
    {
    public:
        Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir);
        ~Database();

        void seed(const std::shared_ptr<Hash> hash, const std::shared_ptr<char*> encryptionKey);
        // Throws DatabaseCreateException on failure.
        std::unique_ptr<DatabaseCreateResponse> create(const std::string &ownerName, const std::string &nodeName);
        // Throws DatabaseAddException on failure
        void addData(const DatabaseNode &nodeInfo, LocalUser *userToPerformActionWith, DataView dataToAdd);
        // Throws PermissionDeniedException if user @userToPerformActionWith is not allowed to add user @userToAdd to group @groupToAddUserTo
        void addUserToGroup(const DatabaseNode &nodeInfo, LocalUser *userToPerformActionWith, const std::string &userToAddName, const Signature::PublicKey &userToAddPublicKey, Group *groupToAddUserTo);
        void commit();
    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;
        DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
        void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<char*> encryptionKey);
        bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
        bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<char*> encryptionKey);
    private:
        dht::DhtRunner node;
        std::vector<std::unique_ptr<StagedObject>> stagedCreateObjects;
        std::vector<std::unique_ptr<StagedObject>> stagedAddObjects;
        DatabaseStorage databaseStorage;
    };
}