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 inline constexpr std::array TxDBPragma
76 {
77  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
78  "PRAGMA max_page_count=2147483646;",
79 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
80  "PRAGMA mmap_size=17179869184;"
81 #endif
82 };
83 
85  {"BEGIN TRANSACTION;",
86 
87  "CREATE TABLE IF NOT EXISTS Transactions ( \
88  TransID CHARACTER(64) PRIMARY KEY, \
89  TransType CHARACTER(24), \
90  FromAcct CHARACTER(35), \
91  FromSeq BIGINT UNSIGNED, \
92  LedgerSeq BIGINT UNSIGNED, \
93  Status CHARACTER(1), \
94  RawTxn BLOB, \
95  TxnMeta BLOB \
96  );",
97  "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \
98  Transactions(LedgerSeq);",
99 
100  "CREATE TABLE IF NOT EXISTS AccountTransactions ( \
101  TransID CHARACTER(64), \
102  Account CHARACTER(64), \
103  LedgerSeq BIGINT UNSIGNED, \
104  TxnSeq INTEGER \
105  );",
106  "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \
107  AccountTransactions(TransID);",
108  "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \
109  AccountTransactions(Account, LedgerSeq, TxnSeq, TransID);",
110  "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \
111  AccountTransactions(LedgerSeq, Account, TransID);",
112 
113  "END TRANSACTION;"}};
114 
116 
117 // The Ledger Meta database maps ledger hashes to shard indexes
118 inline constexpr auto LgrMetaDBName{"ledger_meta.db"};
119 
120 inline constexpr std::array LgrMetaDBPragma
121 {
122  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
123  "PRAGMA max_page_count=2147483646;",
124 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
125  "PRAGMA mmap_size=17179869184;"
126 #endif
127 };
128 
130  {"BEGIN TRANSACTION;",
131 
132  "CREATE TABLE IF NOT EXISTS LedgerMeta ( \
133  LedgerHash CHARACTER(64) PRIMARY KEY, \
134  ShardIndex INTEGER \
135  );",
136 
137  "END TRANSACTION;"}};
138 
140 
141 // Transaction Meta database maps transaction IDs to shard indexes
142 inline constexpr auto TxMetaDBName{"transaction_meta.db"};
143 
144 inline constexpr std::array TxMetaDBPragma
145 {
146  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
147  "PRAGMA max_page_count=2147483646;",
148 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
149  "PRAGMA mmap_size=17179869184;"
150 #endif
151 };
152 
154  {"BEGIN TRANSACTION;",
155 
156  "CREATE TABLE IF NOT EXISTS TransactionMeta ( \
157  TransID CHARACTER(64) PRIMARY KEY, \
158  ShardIndex INTEGER \
159  );",
160 
161  "END TRANSACTION;"}};
162 
164 
165 // Temporary database used with an incomplete shard that is being acquired
166 inline constexpr auto AcquireShardDBName{"acquire.db"};
167 
169  {"PRAGMA journal_size_limit=1582080;"}};
170 
172  {"CREATE TABLE IF NOT EXISTS Shard ( \
173  ShardIndex INTEGER PRIMARY KEY, \
174  LastLedgerHash CHARACTER(64), \
175  StoredLedgerSeqs BLOB \
176  );"}};
177 
179 
180 // Pragma for Ledger and Transaction databases with final shards
181 // These override the CommonDBPragma values defined above.
183  {"PRAGMA synchronous=OFF;", "PRAGMA journal_mode=OFF;"}};
184 
186 
187 inline constexpr auto WalletDBName{"wallet.db"};
188 
190  {"BEGIN TRANSACTION;",
191 
192  // A node's identity must be persisted, including
193  // for clustering purposes. This table holds one
194  // entry: the server's unique identity, but the
195  // value can be overriden by specifying a node
196  // identity in the config file using a [node_seed]
197  // entry.
198  "CREATE TABLE IF NOT EXISTS NodeIdentity ( \
199  PublicKey CHARACTER(53), \
200  PrivateKey CHARACTER(52) \
201  );",
202 
203  // Peer reservations
204  "CREATE TABLE IF NOT EXISTS PeerReservations ( \
205  PublicKey CHARACTER(53) UNIQUE NOT NULL, \
206  Description CHARACTER(64) NOT NULL \
207  );",
208 
209  // Validator Manifests
210  "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \
211  RawData BLOB NOT NULL \
212  );",
213 
214  "CREATE TABLE IF NOT EXISTS PublisherManifests ( \
215  RawData BLOB NOT NULL \
216  );",
217 
218  "END TRANSACTION;"}};
219 
221 
222 static constexpr auto stateDBName{"state.db"};
223 
224 // These override the CommonDBPragma values defined above.
226  {"PRAGMA synchronous=FULL;", "PRAGMA journal_mode=DELETE;"}};
227 
229  {"BEGIN TRANSACTION;",
230 
231  "CREATE TABLE IF NOT EXISTS State ( \
232  ShardIndex INTEGER PRIMARY KEY, \
233  URL TEXT \
234  );",
235 
236  "END TRANSACTION;"}};
237 
239  {"BEGIN TRANSACTION;",
240 
241  "CREATE TABLE IF NOT EXISTS download ( \
242  Path TEXT, \
243  Data BLOB, \
244  Size BIGINT UNSIGNED, \
245  Part BIGINT UNSIGNED PRIMARY KEY \
246  );",
247 
248  "END TRANSACTION;"}};
249 
250 } // namespace ripple
251 
252 #endif
ripple::AcquireShardDBPragma
constexpr std::array< char const *, 1 > AcquireShardDBPragma
Definition: DBInit.h:168
ripple::AcquireShardDBName
constexpr auto AcquireShardDBName
Definition: DBInit.h:166
ripple::WalletDBName
constexpr auto WalletDBName
Definition: DBInit.h:187
ripple::LgrMetaDBInit
constexpr std::array< char const *, 3 > LgrMetaDBInit
Definition: DBInit.h:129
ripple::AcquireShardDBInit
constexpr std::array< char const *, 1 > AcquireShardDBInit
Definition: DBInit.h:171
ripple::ShardArchiveHandlerDBInit
static constexpr std::array< char const *, 3 > ShardArchiveHandlerDBInit
Definition: DBInit.h:228
ripple::LgrDBInit
constexpr std::array< char const *, 5 > LgrDBInit
Definition: DBInit.h:48
ripple::TxMetaDBPragma
constexpr std::array TxMetaDBPragma
Definition: DBInit.h:145
ripple::LgrMetaDBName
constexpr auto LgrMetaDBName
Definition: DBInit.h:118
ripple::DownloaderDBPragma
static constexpr std::array< char const *, 2 > DownloaderDBPragma
Definition: DBInit.h:225
ripple::TxDBName
constexpr auto TxDBName
Definition: DBInit.h:73
ripple::TxMetaDBName
constexpr auto TxMetaDBName
Definition: DBInit.h:142
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:153
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::FinalShardDBPragma
constexpr std::array< char const *, 2 > FinalShardDBPragma
Definition: DBInit.h:182
ripple::stateDBName
static constexpr auto stateDBName
Definition: DBInit.h:222
ripple::LgrMetaDBPragma
constexpr std::array LgrMetaDBPragma
Definition: DBInit.h:121
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TxDBPragma
constexpr std::array TxDBPragma
Definition: DBInit.h:76
ripple::DatabaseBodyDBInit
static constexpr std::array< char const *, 3 > DatabaseBodyDBInit
Definition: DBInit.h:238
ripple::CommonDBPragmaSync
constexpr char const * CommonDBPragmaSync
Definition: DBInit.h:33
ripple::TxDBInit
constexpr std::array< char const *, 8 > TxDBInit
Definition: DBInit.h:84
ripple::WalletDBInit
constexpr std::array< char const *, 6 > WalletDBInit
Definition: DBInit.h:189
ripple::LgrDBName
constexpr auto LgrDBName
Definition: DBInit.h:43