rippled
DBInit.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_APP_DATA_DBINIT_H_INCLUDED
21 #define RIPPLE_APP_DATA_DBINIT_H_INCLUDED
22 
23 #include <array>
24 #include <cstdint>
25 
26 namespace ripple {
27 
29 
30 // These pragmas are built at startup and applied to all database
31 // connections, unless otherwise noted.
32 inline constexpr char const* CommonDBPragmaJournal{"PRAGMA journal_mode=%s;"};
33 inline constexpr char const* CommonDBPragmaSync{"PRAGMA synchronous=%s;"};
34 inline constexpr char const* CommonDBPragmaTemp{"PRAGMA temp_store=%s;"};
35 // A warning will be logged if any lower-safety sqlite tuning settings
36 // are used and at least this much ledger history is configured. This
37 // includes full history nodes. This is because such a large amount of
38 // data will be more difficult to recover if a rare failure occurs,
39 // which are more likely with some of the other available tuning settings.
40 inline constexpr std::uint32_t SQLITE_TUNING_CUTOFF = 10'000'000;
41 
42 // Ledger database holds ledgers and ledger confirmations
43 inline constexpr auto LgrDBName{"ledger.db"};
44 
46  {"PRAGMA journal_size_limit=1582080;"}};
47 
49  {"BEGIN TRANSACTION;",
50 
51  "CREATE TABLE IF NOT EXISTS Ledgers ( \
52  LedgerHash CHARACTER(64) PRIMARY KEY, \
53  LedgerSeq BIGINT UNSIGNED, \
54  PrevHash CHARACTER(64), \
55  TotalCoins BIGINT UNSIGNED, \
56  ClosingTime BIGINT UNSIGNED, \
57  PrevClosingTime BIGINT UNSIGNED, \
58  CloseTimeRes BIGINT UNSIGNED, \
59  CloseFlags BIGINT UNSIGNED, \
60  AccountSetHash CHARACTER(64), \
61  TransSetHash CHARACTER(64) \
62  );",
63  "CREATE INDEX IF NOT EXISTS SeqLedger ON Ledgers(LedgerSeq);",
64 
65  // Old table and indexes no longer needed
66  "DROP TABLE IF EXISTS Validations;",
67 
68  "END TRANSACTION;"}};
69 
71 
72 // Transaction database holds transactions and public keys
73 inline constexpr auto TxDBName{"transaction.db"};
74 
75 // In C++17 omitting the explicit template parameters caused
76 // a crash
78 {
79  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
80  "PRAGMA max_page_count=2147483646;",
81 
82 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
83  "PRAGMA mmap_size=17179869184;"
84 #else
85 
86  // Provide an explicit `no-op` SQL statement
87  // in order to keep the size of the array
88  // constant regardless of the preprocessor
89  // condition evaluation
90  "PRAGMA sqlite_noop_statement;"
91 #endif
92 };
93 
95  {"BEGIN TRANSACTION;",
96 
97  "CREATE TABLE IF NOT EXISTS Transactions ( \
98  TransID CHARACTER(64) PRIMARY KEY, \
99  TransType CHARACTER(24), \
100  FromAcct CHARACTER(35), \
101  FromSeq BIGINT UNSIGNED, \
102  LedgerSeq BIGINT UNSIGNED, \
103  Status CHARACTER(1), \
104  RawTxn BLOB, \
105  TxnMeta BLOB \
106  );",
107  "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \
108  Transactions(LedgerSeq);",
109 
110  "CREATE TABLE IF NOT EXISTS AccountTransactions ( \
111  TransID CHARACTER(64), \
112  Account CHARACTER(64), \
113  LedgerSeq BIGINT UNSIGNED, \
114  TxnSeq INTEGER \
115  );",
116  "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \
117  AccountTransactions(TransID);",
118  "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \
119  AccountTransactions(Account, LedgerSeq, TxnSeq, TransID);",
120  "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \
121  AccountTransactions(LedgerSeq, Account, TransID);",
122 
123  "END TRANSACTION;"}};
124 
126 
127 // The Ledger Meta database maps ledger hashes to shard indexes
128 inline constexpr auto LgrMetaDBName{"ledger_meta.db"};
129 
130 // In C++17 omitting the explicit template parameters caused
131 // a crash
133 {
134  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
135  "PRAGMA max_page_count=2147483646;",
136 
137 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
138  "PRAGMA mmap_size=17179869184;"
139 #else
140 
141  // Provide an explicit `no-op` SQL statement
142  // in order to keep the size of the array
143  // constant regardless of the preprocessor
144  // condition evaluation
145  "PRAGMA sqlite_noop_statement;"
146 #endif
147 };
148 
150  {"BEGIN TRANSACTION;",
151 
152  "CREATE TABLE IF NOT EXISTS LedgerMeta ( \
153  LedgerHash CHARACTER(64) PRIMARY KEY, \
154  ShardIndex INTEGER \
155  );",
156 
157  "END TRANSACTION;"}};
158 
160 
161 // Transaction Meta database maps transaction IDs to shard indexes
162 inline constexpr auto TxMetaDBName{"transaction_meta.db"};
163 
164 // In C++17 omitting the explicit template parameters caused
165 // a crash
167 {
168  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
169  "PRAGMA max_page_count=2147483646;",
170 
171 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
172  "PRAGMA mmap_size=17179869184;"
173 #else
174 
175  // Provide an explicit `no-op` SQL statement
176  // in order to keep the size of the array
177  // constant regardless of the preprocessor
178  // condition evaluation
179  "PRAGMA sqlite_noop_statement;"
180 #endif
181 };
182 
184  {"BEGIN TRANSACTION;",
185 
186  "CREATE TABLE IF NOT EXISTS TransactionMeta ( \
187  TransID CHARACTER(64) PRIMARY KEY, \
188  ShardIndex INTEGER \
189  );",
190 
191  "END TRANSACTION;"}};
192 
194 
195 // Temporary database used with an incomplete shard that is being acquired
196 inline constexpr auto AcquireShardDBName{"acquire.db"};
197 
199  {"PRAGMA journal_size_limit=1582080;"}};
200 
202  {"CREATE TABLE IF NOT EXISTS Shard ( \
203  ShardIndex INTEGER PRIMARY KEY, \
204  LastLedgerHash CHARACTER(64), \
205  StoredLedgerSeqs BLOB \
206  );"}};
207 
209 
210 // Pragma for Ledger and Transaction databases with final shards
211 // These override the CommonDBPragma values defined above.
213  {"PRAGMA synchronous=OFF;", "PRAGMA journal_mode=OFF;"}};
214 
216 
217 inline constexpr auto WalletDBName{"wallet.db"};
218 
220  {"BEGIN TRANSACTION;",
221 
222  // A node's identity must be persisted, including
223  // for clustering purposes. This table holds one
224  // entry: the server's unique identity, but the
225  // value can be overriden by specifying a node
226  // identity in the config file using a [node_seed]
227  // entry.
228  "CREATE TABLE IF NOT EXISTS NodeIdentity ( \
229  PublicKey CHARACTER(53), \
230  PrivateKey CHARACTER(52) \
231  );",
232 
233  // Peer reservations
234  "CREATE TABLE IF NOT EXISTS PeerReservations ( \
235  PublicKey CHARACTER(53) UNIQUE NOT NULL, \
236  Description CHARACTER(64) NOT NULL \
237  );",
238 
239  // Validator Manifests
240  "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \
241  RawData BLOB NOT NULL \
242  );",
243 
244  "CREATE TABLE IF NOT EXISTS PublisherManifests ( \
245  RawData BLOB NOT NULL \
246  );",
247 
248  "END TRANSACTION;"}};
249 
251 
252 static constexpr auto stateDBName{"state.db"};
253 
254 // These override the CommonDBPragma values defined above.
256  {"PRAGMA synchronous=FULL;", "PRAGMA journal_mode=DELETE;"}};
257 
259  {"BEGIN TRANSACTION;",
260 
261  "CREATE TABLE IF NOT EXISTS State ( \
262  ShardIndex INTEGER PRIMARY KEY, \
263  URL TEXT \
264  );",
265 
266  "END TRANSACTION;"}};
267 
269  {"BEGIN TRANSACTION;",
270 
271  "CREATE TABLE IF NOT EXISTS download ( \
272  Path TEXT, \
273  Data BLOB, \
274  Size BIGINT UNSIGNED, \
275  Part BIGINT UNSIGNED PRIMARY KEY \
276  );",
277 
278  "END TRANSACTION;"}};
279 
280 } // namespace ripple
281 
282 #endif
ripple::AcquireShardDBPragma
constexpr std::array< char const *, 1 > AcquireShardDBPragma
Definition: DBInit.h:198
ripple::AcquireShardDBName
constexpr auto AcquireShardDBName
Definition: DBInit.h:196
ripple::WalletDBName
constexpr auto WalletDBName
Definition: DBInit.h:217
ripple::TxDBPragma
constexpr std::array< char const *, 4 > TxDBPragma
Definition: DBInit.h:78
ripple::LgrMetaDBInit
constexpr std::array< char const *, 3 > LgrMetaDBInit
Definition: DBInit.h:149
ripple::AcquireShardDBInit
constexpr std::array< char const *, 1 > AcquireShardDBInit
Definition: DBInit.h:201
ripple::ShardArchiveHandlerDBInit
static constexpr std::array< char const *, 3 > ShardArchiveHandlerDBInit
Definition: DBInit.h:258
ripple::LgrDBInit
constexpr std::array< char const *, 5 > LgrDBInit
Definition: DBInit.h:48
ripple::LgrMetaDBName
constexpr auto LgrMetaDBName
Definition: DBInit.h:128
ripple::DownloaderDBPragma
static constexpr std::array< char const *, 2 > DownloaderDBPragma
Definition: DBInit.h:255
ripple::TxDBName
constexpr auto TxDBName
Definition: DBInit.h:73
ripple::TxMetaDBName
constexpr auto TxMetaDBName
Definition: DBInit.h:162
ripple::LgrDBPragma
constexpr std::array< char const *, 1 > LgrDBPragma
Definition: DBInit.h:45
array
ripple::TxMetaDBInit
constexpr std::array< char const *, 3 > TxMetaDBInit
Definition: DBInit.h:183
ripple::LgrMetaDBPragma
constexpr std::array< char const *, 4 > LgrMetaDBPragma
Definition: DBInit.h:133
cstdint
ripple::SQLITE_TUNING_CUTOFF
constexpr std::uint32_t SQLITE_TUNING_CUTOFF
Definition: DBInit.h:40
std::uint32_t
ripple::CommonDBPragmaTemp
constexpr char const * CommonDBPragmaTemp
Definition: DBInit.h:34
ripple::CommonDBPragmaJournal
constexpr char const * CommonDBPragmaJournal
Definition: DBInit.h:32
ripple::TxMetaDBPragma
constexpr std::array< char const *, 4 > TxMetaDBPragma
Definition: DBInit.h:167
ripple::FinalShardDBPragma
constexpr std::array< char const *, 2 > FinalShardDBPragma
Definition: DBInit.h:212
ripple::stateDBName
static constexpr auto stateDBName
Definition: DBInit.h:252
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::DatabaseBodyDBInit
static constexpr std::array< char const *, 3 > DatabaseBodyDBInit
Definition: DBInit.h:268
ripple::CommonDBPragmaSync
constexpr char const * CommonDBPragmaSync
Definition: DBInit.h:33
ripple::TxDBInit
constexpr std::array< char const *, 8 > TxDBInit
Definition: DBInit.h:94
ripple::WalletDBInit
constexpr std::array< char const *, 6 > WalletDBInit
Definition: DBInit.h:219
ripple::LgrDBName
constexpr auto LgrDBName
Definition: DBInit.h:43