diff options
| author | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-14 15:03:21 -0300 |
|---|---|---|
| committer | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-14 15:20:54 -0300 |
| commit | c49b92a9a79a398b51200e69f7dde8a467b9c925 (patch) | |
| tree | 25c86213839210a08ff69983e7219e924d5dbb85 /src/selftest.cpp | |
| parent | 9b281d890168c7c912857530d787fbc82203d848 (diff) | |
wallet: add experimental SQLite record store
Diffstat (limited to 'src/selftest.cpp')
| -rw-r--r-- | src/selftest.cpp | 108 |
1 files changed, 107 insertions, 1 deletions
diff --git a/src/selftest.cpp b/src/selftest.cpp index 31c168f..f9c4a8e 100644 --- a/src/selftest.cpp +++ b/src/selftest.cpp | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "sockcount.h" | 13 | #include "sockcount.h" |
| 14 | #include "tor.h" | 14 | #include "tor.h" |
| 15 | #include "walletcmd.h" | 15 | #include "walletcmd.h" |
| 16 | #include "wallet_sqlite.h" | ||
| 16 | 17 | ||
| 17 | extern int RunPoolStratumSelfTest(); | 18 | extern int RunPoolStratumSelfTest(); |
| 18 | 19 | ||
| @@ -62,6 +63,11 @@ static std::string HexStrLocal(const std::vector<unsigned char>& v) | |||
| 62 | return out; | 63 | return out; |
| 63 | } | 64 | } |
| 64 | 65 | ||
| 66 | static vector<unsigned char> DataStreamBytes(const CDataStream& ss) | ||
| 67 | { | ||
| 68 | return vector<unsigned char>(ss.begin(), ss.end()); | ||
| 69 | } | ||
| 70 | |||
| 65 | static bool MakeTempDir(std::string& out) | 71 | static bool MakeTempDir(std::string& out) |
| 66 | { | 72 | { |
| 67 | #ifdef _WIN32 | 73 | #ifdef _WIN32 |
| @@ -1030,6 +1036,104 @@ static int RunDbEnvReopenSelfTest() | |||
| 1030 | return nFail == 0 ? 0 : 1; | 1036 | return nFail == 0 ? 0 : 1; |
| 1031 | } | 1037 | } |
| 1032 | 1038 | ||
| 1039 | static int RunWalletSQLiteSelfTest() | ||
| 1040 | { | ||
| 1041 | fflush(stdout); | ||
| 1042 | printf("wallet-sqlite self-test\n"); | ||
| 1043 | |||
| 1044 | std::string tmp; | ||
| 1045 | if (!MakeTempDir(tmp)) | ||
| 1046 | { | ||
| 1047 | printf(" FAIL could not create a temporary data directory\n"); | ||
| 1048 | return 1; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | int nFail = 0; | ||
| 1052 | string strPath = tmp + "/wallet.sqlite"; | ||
| 1053 | string strError; | ||
| 1054 | |||
| 1055 | try | ||
| 1056 | { | ||
| 1057 | CDataStream ssKeySetting(SER_DISK); | ||
| 1058 | ssKeySetting << make_pair(string("setting"), string("sqlite-roundtrip")); | ||
| 1059 | CDataStream ssValueSetting(SER_DISK); | ||
| 1060 | ssValueSetting << (int64)424242; | ||
| 1061 | |||
| 1062 | vector<unsigned char> vchPubKey; | ||
| 1063 | vchPubKey.push_back(0x04); | ||
| 1064 | for (int i = 0; i < 64; i++) | ||
| 1065 | vchPubKey.push_back((unsigned char)i); | ||
| 1066 | CDataStream ssKeyDefault(SER_DISK); | ||
| 1067 | ssKeyDefault << string("defaultkey"); | ||
| 1068 | CDataStream ssValueDefault(SER_DISK); | ||
| 1069 | ssValueDefault << vchPubKey; | ||
| 1070 | |||
| 1071 | { | ||
| 1072 | CWalletDBSQLite db; | ||
| 1073 | nFail += Check(db.Open(strPath, strError), | ||
| 1074 | "SQLite wallet opens and creates schema") ? 0 : 1; | ||
| 1075 | nFail += Check(db.BeginTransaction(strError), | ||
| 1076 | "SQLite wallet begins a write transaction") ? 0 : 1; | ||
| 1077 | nFail += Check(db.WriteRecord(DataStreamBytes(ssKeySetting), | ||
| 1078 | DataStreamBytes(ssValueSetting), | ||
| 1079 | strError), | ||
| 1080 | "SQLite wallet writes a serialized setting record") ? 0 : 1; | ||
| 1081 | nFail += Check(db.WriteRecord(DataStreamBytes(ssKeyDefault), | ||
| 1082 | DataStreamBytes(ssValueDefault), | ||
| 1083 | strError), | ||
| 1084 | "SQLite wallet writes a serialized defaultkey record") ? 0 : 1; | ||
| 1085 | nFail += Check(db.CommitTransaction(strError), | ||
| 1086 | "SQLite wallet commits a write transaction") ? 0 : 1; | ||
| 1087 | int nCount = 0; | ||
| 1088 | nFail += Check(db.CountRecords(nCount, strError) && nCount == 2, | ||
| 1089 | "SQLite wallet counts raw records") ? 0 : 1; | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | { | ||
| 1093 | CWalletDBSQLite db; | ||
| 1094 | nFail += Check(db.Open(strPath, strError), | ||
| 1095 | "SQLite wallet reopens") ? 0 : 1; | ||
| 1096 | vector<unsigned char> vchRead; | ||
| 1097 | nFail += Check(db.ReadRecord(DataStreamBytes(ssKeySetting), | ||
| 1098 | vchRead, | ||
| 1099 | strError), | ||
| 1100 | "SQLite wallet reads a serialized setting record") ? 0 : 1; | ||
| 1101 | int64 nReadValue = 0; | ||
| 1102 | CDataStream ssRead(vchRead, SER_DISK); | ||
| 1103 | ssRead >> nReadValue; | ||
| 1104 | nFail += Check(!ssRead.fail() && nReadValue == 424242, | ||
| 1105 | "SQLite wallet preserves serialized values byte-for-byte") ? 0 : 1; | ||
| 1106 | |||
| 1107 | vchRead.clear(); | ||
| 1108 | nFail += Check(db.ReadRecord(DataStreamBytes(ssKeyDefault), | ||
| 1109 | vchRead, | ||
| 1110 | strError), | ||
| 1111 | "SQLite wallet reads a serialized defaultkey record") ? 0 : 1; | ||
| 1112 | vector<unsigned char> vchReadPubKey; | ||
| 1113 | CDataStream ssReadDefault(vchRead, SER_DISK); | ||
| 1114 | ssReadDefault >> vchReadPubKey; | ||
| 1115 | nFail += Check(!ssReadDefault.fail() && vchReadPubKey == vchPubKey, | ||
| 1116 | "SQLite wallet preserves public key blobs") ? 0 : 1; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | catch (const std::exception& e) | ||
| 1120 | { | ||
| 1121 | printf(" FAIL exception: %s\n", e.what()); | ||
| 1122 | nFail++; | ||
| 1123 | } | ||
| 1124 | catch (...) | ||
| 1125 | { | ||
| 1126 | printf(" FAIL unknown exception\n"); | ||
| 1127 | nFail++; | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | RemoveTree(tmp); | ||
| 1131 | printf("%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", | ||
| 1132 | nFail, nFail == 1 ? "" : "s"); | ||
| 1133 | fflush(stdout); | ||
| 1134 | return nFail == 0 ? 0 : 1; | ||
| 1135 | } | ||
| 1136 | |||
| 1033 | static void ClearWalletRuntimeForTest() | 1137 | static void ClearWalletRuntimeForTest() |
| 1034 | { | 1138 | { |
| 1035 | CRITICAL_BLOCK(cs_mapKeys) | 1139 | CRITICAL_BLOCK(cs_mapKeys) |
| @@ -2289,6 +2393,8 @@ int RunSelfTest(const std::string& name) | |||
| 2289 | return RunWalletStorageSanitySelfTest(); | 2393 | return RunWalletStorageSanitySelfTest(); |
| 2290 | if (name == "db-env-reopen") | 2394 | if (name == "db-env-reopen") |
| 2291 | return RunDbEnvReopenSelfTest(); | 2395 | return RunDbEnvReopenSelfTest(); |
| 2396 | if (name == "wallet-sqlite") | ||
| 2397 | return RunWalletSQLiteSelfTest(); | ||
| 2292 | if (name == "wallet-crypto") | 2398 | if (name == "wallet-crypto") |
| 2293 | return RunWalletCryptoSelfTest(); | 2399 | return RunWalletCryptoSelfTest(); |
| 2294 | if (name == "wallet-encrypt") | 2400 | if (name == "wallet-encrypt") |
| @@ -2309,6 +2415,6 @@ int RunSelfTest(const std::string& name) | |||
| 2309 | return RunManagedTorSelfTest(); | 2415 | return RunManagedTorSelfTest(); |
| 2310 | 2416 | ||
| 2311 | printf("Unknown self-test '%s'\n", name.c_str()); | 2417 | printf("Unknown self-test '%s'\n", name.c_str()); |
| 2312 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-crypto, wallet-encrypt, wallet-portability, net-message, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); | 2418 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-sqlite, wallet-crypto, wallet-encrypt, wallet-portability, net-message, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); |
| 2313 | return 1; | 2419 | return 1; |
| 2314 | } | 2420 | } |