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 // Temporary database used with an incomplete shard that is being acquired
118 inline constexpr auto AcquireShardDBName{"acquire.db"};
119 
121  {"PRAGMA journal_size_limit=1582080;"}};
122 
124  {"CREATE TABLE IF NOT EXISTS Shard ( \
125  ShardIndex INTEGER PRIMARY KEY, \
126  LastLedgerHash CHARACTER(64), \
127  StoredLedgerSeqs BLOB \
128  );"}};
129 
131 
132 // Pragma for Ledger and Transaction databases with complete shards
133 // These override the CommonDBPragma values defined above.
135  {"PRAGMA synchronous=OFF;", "PRAGMA journal_mode=OFF;"}};
136 
138 
139 inline constexpr auto WalletDBName{"wallet.db"};
140 
142  {"BEGIN TRANSACTION;",
143 
144  // A node's identity must be persisted, including
145  // for clustering purposes. This table holds one
146  // entry: the server's unique identity, but the
147  // value can be overriden by specifying a node
148  // identity in the config file using a [node_seed]
149  // entry.
150  "CREATE TABLE IF NOT EXISTS NodeIdentity ( \
151  PublicKey CHARACTER(53), \
152  PrivateKey CHARACTER(52) \
153  );",
154 
155  // Peer reservations
156  "CREATE TABLE IF NOT EXISTS PeerReservations ( \
157  PublicKey CHARACTER(53) UNIQUE NOT NULL, \
158  Description CHARACTER(64) NOT NULL \
159  );",
160 
161  // Validator Manifests
162  "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \
163  RawData BLOB NOT NULL \
164  );",
165 
166  "CREATE TABLE IF NOT EXISTS PublisherManifests ( \
167  RawData BLOB NOT NULL \
168  );",
169 
170  "END TRANSACTION;"}};
171 
173 
174 static constexpr auto stateDBName{"state.db"};
175 
176 // These override the CommonDBPragma values defined above.
178  {"PRAGMA synchronous=FULL;", "PRAGMA journal_mode=DELETE;"}};
179 
181  {"BEGIN TRANSACTION;",
182 
183  "CREATE TABLE IF NOT EXISTS State ( \
184  ShardIndex INTEGER PRIMARY KEY, \
185  URL TEXT \
186  );",
187 
188  "END TRANSACTION;"}};
189 
191  {"BEGIN TRANSACTION;",
192 
193  "CREATE TABLE IF NOT EXISTS download ( \
194  Path TEXT, \
195  Data BLOB, \
196  Size BIGINT UNSIGNED, \
197  Part BIGINT UNSIGNED PRIMARY KEY \
198  );",
199 
200  "END TRANSACTION;"}};
201 
202 } // namespace ripple
203 
204 #endif
ripple::AcquireShardDBPragma
constexpr std::array< char const *, 1 > AcquireShardDBPragma
Definition: DBInit.h:120
ripple::AcquireShardDBName
constexpr auto AcquireShardDBName
Definition: DBInit.h:118
ripple::WalletDBName
constexpr auto WalletDBName
Definition: DBInit.h:139
ripple::AcquireShardDBInit
constexpr std::array< char const *, 1 > AcquireShardDBInit
Definition: DBInit.h:123
ripple::ShardArchiveHandlerDBInit
static constexpr std::array< char const *, 3 > ShardArchiveHandlerDBInit
Definition: DBInit.h:180
ripple::LgrDBInit
constexpr std::array< char const *, 5 > LgrDBInit
Definition: DBInit.h:48
ripple::DownloaderDBPragma
static constexpr std::array< char const *, 2 > DownloaderDBPragma
Definition: DBInit.h:177
ripple::TxDBName
constexpr auto TxDBName
Definition: DBInit.h:73
ripple::LgrDBPragma
constexpr std::array< char const *, 1 > LgrDBPragma
Definition: DBInit.h:45
array
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::stateDBName
static constexpr auto stateDBName
Definition: DBInit.h:174
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:190
ripple::CommonDBPragmaSync
constexpr char const * CommonDBPragmaSync
Definition: DBInit.h:33
ripple::CompleteShardDBPragma
constexpr std::array< char const *, 2 > CompleteShardDBPragma
Definition: DBInit.h:134
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:141
ripple::LgrDBName
constexpr auto LgrDBName
Definition: DBInit.h:43