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 
25 namespace ripple {
26 
28 
29 // These pragmas are built at startup and applied to all database
30 // connections, unless otherwise noted.
31 inline constexpr char const* CommonDBPragmaJournal{"PRAGMA journal_mode=%s;"};
32 inline constexpr char const* CommonDBPragmaSync{"PRAGMA synchronous=%s;"};
33 inline constexpr char const* CommonDBPragmaTemp{"PRAGMA temp_store=%s;"};
34 // A warning will be logged if any lower-safety sqlite tuning settings
35 // are used and at least this much ledger history is configured. This
36 // includes full history nodes. This is because such a large amount of
37 // data will be more difficult to recover if a rare failure occurs,
38 // which are more likely with some of the other available tuning settings.
39 inline constexpr std::uint32_t SQLITE_TUNING_CUTOFF = 10'000'000;
40 
41 // Ledger database holds ledgers and ledger confirmations
42 inline constexpr auto LgrDBName{"ledger.db"};
43 
45  {"PRAGMA journal_size_limit=1582080;"}};
46 
48  {"BEGIN TRANSACTION;",
49 
50  "CREATE TABLE IF NOT EXISTS Ledgers ( \
51  LedgerHash CHARACTER(64) PRIMARY KEY, \
52  LedgerSeq BIGINT UNSIGNED, \
53  PrevHash CHARACTER(64), \
54  TotalCoins BIGINT UNSIGNED, \
55  ClosingTime BIGINT UNSIGNED, \
56  PrevClosingTime BIGINT UNSIGNED, \
57  CloseTimeRes BIGINT UNSIGNED, \
58  CloseFlags BIGINT UNSIGNED, \
59  AccountSetHash CHARACTER(64), \
60  TransSetHash CHARACTER(64) \
61  );",
62  "CREATE INDEX IF NOT EXISTS SeqLedger ON Ledgers(LedgerSeq);",
63 
64  // Old table and indexes no longer needed
65  "DROP TABLE IF EXISTS Validations;",
66 
67  "END TRANSACTION;"}};
68 
70 
71 // Transaction database holds transactions and public keys
72 inline constexpr auto TxDBName{"transaction.db"};
73 
74 inline constexpr std::array TxDBPragma
75 {
76  "PRAGMA page_size=4096;", "PRAGMA journal_size_limit=1582080;",
77  "PRAGMA max_page_count=2147483646;",
78 #if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP)
79  "PRAGMA mmap_size=17179869184;"
80 #endif
81 };
82 
84  {"BEGIN TRANSACTION;",
85 
86  "CREATE TABLE IF NOT EXISTS Transactions ( \
87  TransID CHARACTER(64) PRIMARY KEY, \
88  TransType CHARACTER(24), \
89  FromAcct CHARACTER(35), \
90  FromSeq BIGINT UNSIGNED, \
91  LedgerSeq BIGINT UNSIGNED, \
92  Status CHARACTER(1), \
93  RawTxn BLOB, \
94  TxnMeta BLOB \
95  );",
96  "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \
97  Transactions(LedgerSeq);",
98 
99  "CREATE TABLE IF NOT EXISTS AccountTransactions ( \
100  TransID CHARACTER(64), \
101  Account CHARACTER(64), \
102  LedgerSeq BIGINT UNSIGNED, \
103  TxnSeq INTEGER \
104  );",
105  "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \
106  AccountTransactions(TransID);",
107  "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \
108  AccountTransactions(Account, LedgerSeq, TxnSeq, TransID);",
109  "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \
110  AccountTransactions(LedgerSeq, Account, TransID);",
111 
112  "END TRANSACTION;"}};
113 
115 
116 // Temporary database used with an incomplete shard that is being acquired
117 inline constexpr auto AcquireShardDBName{"acquire.db"};
118 
120  {"PRAGMA journal_size_limit=1582080;"}};
121 
123  {"CREATE TABLE IF NOT EXISTS Shard ( \
124  ShardIndex INTEGER PRIMARY KEY, \
125  LastLedgerHash CHARACTER(64), \
126  StoredLedgerSeqs BLOB \
127  );"}};
128 
130 
131 // Pragma for Ledger and Transaction databases with complete shards
132 // These override the CommonDBPragma values defined above.
134  {"PRAGMA synchronous=OFF;", "PRAGMA journal_mode=OFF;"}};
135 
137 
138 inline constexpr auto WalletDBName{"wallet.db"};
139 
141  {"BEGIN TRANSACTION;",
142 
143  // A node's identity must be persisted, including
144  // for clustering purposes. This table holds one
145  // entry: the server's unique identity, but the
146  // value can be overriden by specifying a node
147  // identity in the config file using a [node_seed]
148  // entry.
149  "CREATE TABLE IF NOT EXISTS NodeIdentity ( \
150  PublicKey CHARACTER(53), \
151  PrivateKey CHARACTER(52) \
152  );",
153 
154  // Peer reservations
155  "CREATE TABLE IF NOT EXISTS PeerReservations ( \
156  PublicKey CHARACTER(53) UNIQUE NOT NULL, \
157  Description CHARACTER(64) NOT NULL \
158  );",
159 
160  // Validator Manifests
161  "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \
162  RawData BLOB NOT NULL \
163  );",
164 
165  "CREATE TABLE IF NOT EXISTS PublisherManifests ( \
166  RawData BLOB NOT NULL \
167  );",
168 
169  "END TRANSACTION;"}};
170 
172 
173 static constexpr auto stateDBName{"state.db"};
174 
175 // These override the CommonDBPragma values defined above.
177  {"PRAGMA synchronous=FULL;", "PRAGMA journal_mode=DELETE;"}};
178 
180  {"BEGIN TRANSACTION;",
181 
182  "CREATE TABLE IF NOT EXISTS State ( \
183  ShardIndex INTEGER PRIMARY KEY, \
184  URL TEXT \
185  );",
186 
187  "END TRANSACTION;"}};
188 
190  {"BEGIN TRANSACTION;",
191 
192  "CREATE TABLE IF NOT EXISTS download ( \
193  Path TEXT, \
194  Data BLOB, \
195  Size BIGINT UNSIGNED, \
196  Part BIGINT UNSIGNED PRIMARY KEY \
197  );",
198 
199  "END TRANSACTION;"}};
200 
201 } // namespace ripple
202 
203 #endif
ripple::AcquireShardDBPragma
constexpr std::array< char const *, 1 > AcquireShardDBPragma
Definition: DBInit.h:119
ripple::AcquireShardDBName
constexpr auto AcquireShardDBName
Definition: DBInit.h:117
ripple::WalletDBName
constexpr auto WalletDBName
Definition: DBInit.h:138
ripple::AcquireShardDBInit
constexpr std::array< char const *, 1 > AcquireShardDBInit
Definition: DBInit.h:122
ripple::ShardArchiveHandlerDBInit
static constexpr std::array< char const *, 3 > ShardArchiveHandlerDBInit
Definition: DBInit.h:179
ripple::LgrDBInit
constexpr std::array< char const *, 5 > LgrDBInit
Definition: DBInit.h:47
ripple::DownloaderDBPragma
static constexpr std::array< char const *, 2 > DownloaderDBPragma
Definition: DBInit.h:176
ripple::TxDBName
constexpr auto TxDBName
Definition: DBInit.h:72
ripple::LgrDBPragma
constexpr std::array< char const *, 1 > LgrDBPragma
Definition: DBInit.h:44
array
ripple::SQLITE_TUNING_CUTOFF
constexpr std::uint32_t SQLITE_TUNING_CUTOFF
Definition: DBInit.h:39
std::uint32_t
ripple::CommonDBPragmaTemp
constexpr char const * CommonDBPragmaTemp
Definition: DBInit.h:33
ripple::CommonDBPragmaJournal
constexpr char const * CommonDBPragmaJournal
Definition: DBInit.h:31
ripple::stateDBName
static constexpr auto stateDBName
Definition: DBInit.h:173
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:75
ripple::DatabaseBodyDBInit
static constexpr std::array< char const *, 3 > DatabaseBodyDBInit
Definition: DBInit.h:189
ripple::CommonDBPragmaSync
constexpr char const * CommonDBPragmaSync
Definition: DBInit.h:32
ripple::CompleteShardDBPragma
constexpr std::array< char const *, 2 > CompleteShardDBPragma
Definition: DBInit.h:133
ripple::TxDBInit
constexpr std::array< char const *, 8 > TxDBInit
Definition: DBInit.h:83
ripple::WalletDBInit
constexpr std::array< char const *, 6 > WalletDBInit
Definition: DBInit.h:140
ripple::LgrDBName
constexpr auto LgrDBName
Definition: DBInit.h:42