PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ripple/app/main/DBInit.cpp

https://gitlab.com/vectorci/rippled
C++ | 329 lines | 139 code | 32 blank | 158 comment | 1 complexity | 8d7676dae3d81f970f0376103a84bb68 MD5 | raw file
  1. //------------------------------------------------------------------------------
  2. /*
  3. This file is part of rippled: https://github.com/ripple/rippled
  4. Copyright (c) 2012, 2013 Ripple Labs Inc.
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. //==============================================================================
  17. #include <BeastConfig.h>
  18. #include <ripple/app/main/DBInit.h>
  19. #include <type_traits>
  20. namespace ripple {
  21. // Transaction database holds transactions and public keys
  22. const char* TxnDBInit[] =
  23. {
  24. "PRAGMA synchronous=NORMAL;",
  25. "PRAGMA journal_mode=WAL;",
  26. "PRAGMA journal_size_limit=1582080;",
  27. #if (ULONG_MAX > UINT_MAX) && !defined (NO_SQLITE_MMAP)
  28. "PRAGMA mmap_size=17179869184;",
  29. #endif
  30. "BEGIN TRANSACTION;",
  31. "CREATE TABLE IF NOT EXISTS Transactions ( \
  32. TransID CHARACTER(64) PRIMARY KEY, \
  33. TransType CHARACTER(24), \
  34. FromAcct CHARACTER(35), \
  35. FromSeq BIGINT UNSIGNED, \
  36. LedgerSeq BIGINT UNSIGNED, \
  37. Status CHARACTER(1), \
  38. RawTxn BLOB, \
  39. TxnMeta BLOB \
  40. );",
  41. "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \
  42. Transactions(LedgerSeq);",
  43. "CREATE TABLE IF NOT EXISTS AccountTransactions ( \
  44. TransID CHARACTER(64), \
  45. Account CHARACTER(64), \
  46. LedgerSeq BIGINT UNSIGNED, \
  47. TxnSeq INTEGER \
  48. );",
  49. "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \
  50. AccountTransactions(TransID);",
  51. "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \
  52. AccountTransactions(Account, LedgerSeq, TxnSeq, TransID);",
  53. "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \
  54. AccountTransactions(LedgerSeq, Account, TransID);",
  55. "END TRANSACTION;"
  56. };
  57. int TxnDBCount = std::extent<decltype(TxnDBInit)>::value;
  58. // Ledger database holds ledgers and ledger confirmations
  59. const char* LedgerDBInit[] =
  60. {
  61. "PRAGMA synchronous=NORMAL;",
  62. "PRAGMA journal_mode=WAL;",
  63. "PRAGMA journal_size_limit=1582080;",
  64. "BEGIN TRANSACTION;",
  65. "CREATE TABLE IF NOT EXISTS Ledgers ( \
  66. LedgerHash CHARACTER(64) PRIMARY KEY, \
  67. LedgerSeq BIGINT UNSIGNED, \
  68. PrevHash CHARACTER(64), \
  69. TotalCoins BIGINT UNSIGNED, \
  70. ClosingTime BIGINT UNSIGNED, \
  71. PrevClosingTime BIGINT UNSIGNED, \
  72. CloseTimeRes BIGINT UNSIGNED, \
  73. CloseFlags BIGINT UNSIGNED, \
  74. AccountSetHash CHARACTER(64), \
  75. TransSetHash CHARACTER(64) \
  76. );",
  77. "CREATE INDEX IF NOT EXISTS SeqLedger ON Ledgers(LedgerSeq);",
  78. // InitialSeq field is the current ledger seq when the row
  79. // is inserted. Only relevant during online delete
  80. "CREATE TABLE IF NOT EXISTS Validations ( \
  81. LedgerSeq BIGINT UNSIGNED, \
  82. InitialSeq BIGINT UNSIGNED, \
  83. LedgerHash CHARACTER(64), \
  84. NodePubKey CHARACTER(56), \
  85. SignTime BIGINT UNSIGNED, \
  86. RawData BLOB \
  87. );",
  88. "CREATE INDEX IF NOT EXISTS ValidationsByHash ON \
  89. Validations(LedgerHash);",
  90. "CREATE INDEX IF NOT EXISTS ValidationsBySeq ON \
  91. Validations(LedgerSeq);",
  92. "CREATE INDEX IF NOT EXISTS ValidationsByInitialSeq ON \
  93. Validations(InitialSeq, LedgerSeq);",
  94. "CREATE INDEX IF NOT EXISTS ValidationsByTime ON \
  95. Validations(SignTime);",
  96. "END TRANSACTION;"
  97. };
  98. int LedgerDBCount = std::extent<decltype(LedgerDBInit)>::value;
  99. // NodeIdentity database holds local accounts and trusted nodes
  100. // VFALCO NOTE but its a table not a database, so...?
  101. //
  102. const char* WalletDBInit[] =
  103. {
  104. // Node identity must be persisted for CAS routing and responsibilities.
  105. "BEGIN TRANSACTION;",
  106. "CREATE TABLE IF NOT EXISTS NodeIdentity ( \
  107. PublicKey CHARACTER(53), \
  108. PrivateKey CHARACTER(52) \
  109. );",
  110. // Miscellaneous persistent information
  111. // Integer: 1 : Used to simplify SQL.
  112. // ScoreUpdated: when scores was last updated.
  113. // FetchUpdated: when last fetch succeeded.
  114. "CREATE TABLE IF NOT EXISTS Misc ( \
  115. Magic INTEGER UNIQUE NOT NULL, \
  116. ScoreUpdated DATETIME, \
  117. FetchUpdated DATETIME \
  118. );",
  119. // Scoring and other information for domains.
  120. //
  121. // Domain:
  122. // Domain source for https.
  123. // PublicKey:
  124. // Set if ever succeeded.
  125. // XXX Use NULL in place of ""
  126. // Source:
  127. // 'M' = Manually added. : 1500
  128. // 'V' = validators.txt : 1000
  129. // 'W' = Web browsing. : 200
  130. // 'R' = Referral : 0
  131. // Next:
  132. // Time of next fetch attempt.
  133. // Scan:
  134. // Time of last fetch attempt.
  135. // Fetch:
  136. // Time of last successful fetch.
  137. // Sha256:
  138. // Checksum of last fetch.
  139. // Comment:
  140. // User supplied comment.
  141. // Table of Domains user has asked to trust.
  142. "CREATE TABLE IF NOT EXISTS SeedDomains ( \
  143. Domain TEXT PRIMARY KEY NOT NULL, \
  144. PublicKey CHARACTER(53), \
  145. Source CHARACTER(1) NOT NULL, \
  146. Next DATETIME, \
  147. Scan DATETIME, \
  148. Fetch DATETIME, \
  149. Sha256 CHARACTER[64], \
  150. Comment TEXT \
  151. );",
  152. // Allow us to easily find the next SeedDomain to fetch.
  153. "CREATE INDEX IF NOT EXISTS SeedDomainNext ON SeedDomains (Next);",
  154. // Table of PublicKeys user has asked to trust.
  155. // Fetches are made to the CAS. This gets the ripple.txt so even validators
  156. // without a web server can publish a ripple.txt.
  157. // Source:
  158. // 'M' = Manually added. : 1500
  159. // 'V' = validators.txt : 1000
  160. // 'W' = Web browsing. : 200
  161. // 'R' = Referral : 0
  162. // Next:
  163. // Time of next fetch attempt.
  164. // Scan:
  165. // Time of last fetch attempt.
  166. // Fetch:
  167. // Time of last successful fetch.
  168. // Sha256:
  169. // Checksum of last fetch.
  170. // Comment:
  171. // User supplied comment.
  172. "CREATE TABLE IF NOT EXISTS SeedNodes ( \
  173. PublicKey CHARACTER(53) PRIMARY KEY NOT NULL, \
  174. Source CHARACTER(1) NOT NULL, \
  175. Next DATETIME, \
  176. Scan DATETIME, \
  177. Fetch DATETIME, \
  178. Sha256 CHARACTER[64], \
  179. Comment TEXT \
  180. );",
  181. // Allow us to easily find the next SeedNode to fetch.
  182. "CREATE INDEX IF NOT EXISTS SeedNodeNext ON SeedNodes (Next);",
  183. // Nodes we trust to not grossly collude against us. Derived from
  184. // SeedDomains, SeedNodes, and ValidatorReferrals.
  185. //
  186. // Score:
  187. // Computed trust score. Higher is better.
  188. // Seen:
  189. // Last validation received.
  190. "CREATE TABLE IF NOT EXISTS TrustedNodes ( \
  191. PublicKey CHARACTER(53) PRIMARY KEY NOT NULL, \
  192. Score INTEGER DEFAULT 0 NOT NULL, \
  193. Seen DATETIME, \
  194. Comment TEXT \
  195. );",
  196. // List of referrals.
  197. // - There may be multiple sources for a Validator. The last source is used.
  198. // Validator:
  199. // Public key of referrer.
  200. // Entry:
  201. // Entry index in [validators] table.
  202. // Referral:
  203. // This is the form provided by the ripple.txt:
  204. // - Public key for CAS based referral.
  205. // - Domain for domain based referral.
  206. // XXX Do garbage collection when validators have no references.
  207. "CREATE TABLE IF NOT EXISTS ValidatorReferrals (\
  208. Validator CHARACTER(53) NOT NULL, \
  209. Entry INTEGER NOT NULL, \
  210. Referral TEXT NOT NULL, \
  211. PRIMARY KEY (Validator,Entry) \
  212. );",
  213. // Validator Manifests
  214. R"(
  215. CREATE TABLE IF NOT EXISTS ValidatorManifests (
  216. RawData BLOB NOT NULL
  217. );
  218. )",
  219. // List of referrals from ripple.txt files.
  220. // Validator:
  221. // Public key of referree.
  222. // Entry:
  223. // Entry index in [validators] table.
  224. // IP:
  225. // IP of referred.
  226. // Port:
  227. // -1 = Default
  228. // XXX Do garbage collection when ips have no references.
  229. "CREATE TABLE IF NOT EXISTS IpReferrals ( \
  230. Validator CHARACTER(53) NOT NULL, \
  231. Entry INTEGER NOT NULL, \
  232. IP TEXT NOT NULL, \
  233. Port INTEGER NOT NULL DEFAULT -1, \
  234. PRIMARY KEY (Validator,Entry) \
  235. );",
  236. "END TRANSACTION;"
  237. };
  238. int WalletDBCount = std::extent<decltype(WalletDBInit)>::value;
  239. // Hash node database holds nodes indexed by hash
  240. // VFALCO TODO Remove this since it looks unused
  241. /*
  242. int HashNodeDBCount = std::extent<decltype(HashNodeDBInit)>::value;
  243. */
  244. // Net node database holds nodes seen on the network
  245. // XXX Not really used needs replacement.
  246. /*
  247. const char* NetNodeDBInit[] =
  248. {
  249. "CREATE TABLE KnownNodes ( \
  250. Hanko CHARACTER(35) PRIMARY KEY, \
  251. LastSeen TEXT, \
  252. HaveContactInfo CHARACTER(1), \
  253. ContactObject BLOB \
  254. );"
  255. };
  256. int NetNodeDBCount = std::extent<decltype(NetNodeDBInit)>::value;
  257. */
  258. // This appears to be unused
  259. /*
  260. const char* PathFindDBInit[] =
  261. {
  262. "PRAGMA synchronous = OFF; ",
  263. "DROP TABLE TrustLines; ",
  264. "CREATE TABLE TrustLines { "
  265. "To CHARACTER(40), " // Hex of account trusted
  266. "By CHARACTER(40), " // Hex of account trusting
  267. "Currency CHARACTER(80), " // Hex currency, hex issuer
  268. "Use INTEGER, " // Use count
  269. "Seq BIGINT UNSIGNED " // Sequence when use count was updated
  270. "}; ",
  271. "CREATE INDEX TLBy ON TrustLines(By, Currency, Use);",
  272. "CREATE INDEX TLTo ON TrustLines(To, Currency, Use);",
  273. "DROP TABLE Exchanges;",
  274. "CREATE TABLE Exchanges { "
  275. "From CHARACTER(80), "
  276. "To CHARACTER(80), "
  277. "Currency CHARACTER(80), "
  278. "Use INTEGER, "
  279. "Seq BIGINT UNSIGNED "
  280. "}; ",
  281. "CREATE INDEX ExBy ON Exchanges(By, Currency, Use);",
  282. "CREATE INDEX ExTo ON Exchanges(To, Currency, Use);",
  283. };
  284. int PathFindDBCount = std::extent<decltype(PathFindDBInit)>::value;
  285. */
  286. } // ripple