Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 372b252294 | |||
| 6c56e41e82 | |||
| 79b0c4a176 | |||
| 3db537d759 | |||
| 63be053b1d | |||
| d0fb2dc105 | |||
| bea3c4447c | |||
| 89a480a85a | |||
| 47e358dc18 | |||
| 47c9293849 | |||
| 0f5582f100 | |||
| 3a78a6baf9 | |||
| 0f2cf711db | |||
| 55c202516d | |||
| 4e1a0576e1 | |||
| 6d70b41844 | |||
| 4fa30abb4b | |||
| 68a86c38b5 | |||
| 3099371864 | |||
| 42653434e0 | |||
| 25475d1057 | |||
| ce27e8e5cf | |||
| b17a004b83 | |||
| ccfada5ca9 | |||
| 59ee532bf6 | |||
| 03073bd597 | |||
| 674bdc7192 | |||
| 76579e3059 | |||
| 426e23d8be | |||
| 269498453e | |||
| 32330b420e | |||
| 2ba0ecf428 | |||
| 2b5471283e | |||
| dbde798221 | |||
| 68f5515588 | |||
| 891ad5ad25 | |||
| c02994c836 | |||
| 569ca99e66 | |||
| f13e512712 | |||
| b28525057a | |||
| b9d631e968 | |||
| d5473d7cae | |||
| cd51ba41d8 | |||
| aef95bdf78 | |||
| f633b9e330 | |||
| e7c5c6596a | |||
| 16b35f6b2b | |||
| 7faf13dc31 | |||
| db65324b7a | |||
| c98bdbe335 | |||
| 6f1227b022 | |||
| 12205cdc37 | |||
| 2fc0e8155a | |||
| eeda728564 | |||
| 0df054bbcb | |||
| fbd931a392 | |||
| a792f90489 | |||
| 64939a9793 | |||
| b506a48192 | |||
| dee0d9ef62 |
@@ -0,0 +1,49 @@
|
||||
# Triangles code style.
|
||||
# Conservative: do not reflow long lines, do not reorganize includes.
|
||||
# This config is enforced *only on changed lines* via `git clang-format` in CI,
|
||||
# so it shapes new/edited code without touching legacy files until they're touched.
|
||||
|
||||
BasedOnStyle: LLVM
|
||||
Language: Cpp
|
||||
Standard: c++17
|
||||
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ContinuationIndentWidth: 4
|
||||
AccessModifierOffset: -4
|
||||
|
||||
ColumnLimit: 0 # Don't reflow long lines — too disruptive for legacy code.
|
||||
ReflowComments: false
|
||||
|
||||
BreakBeforeBraces: Attach
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
|
||||
PointerAlignment: Left
|
||||
DerivePointerAlignment: false
|
||||
SpaceAfterCStyleCast: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
|
||||
NamespaceIndentation: None
|
||||
FixNamespaceComments: true
|
||||
|
||||
# Includes: don't shuffle — header order in this codebase is load-bearing
|
||||
# (e.g. main.cpp's mix of project + system headers carries platform meaning).
|
||||
SortIncludes: false
|
||||
IncludeBlocks: Preserve
|
||||
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MaxEmptyLinesToKeep: 2
|
||||
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignTrailingComments: true
|
||||
|
||||
# Don't auto-add braces to single-statement bodies — too invasive.
|
||||
InsertBraces: false
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
# Triangles clang-tidy config.
|
||||
#
|
||||
# Goal: catch real bugs in new/edited code without drowning in noise from
|
||||
# legacy patterns. Enforced *diff-only* in CI (changed lines on PRs).
|
||||
#
|
||||
# Conservative starter set. Graduate checks to WarningsAsErrors only after
|
||||
# the codebase is clean for that check.
|
||||
|
||||
Checks: >
|
||||
-*,
|
||||
bugprone-*,
|
||||
performance-*,
|
||||
readability-misleading-indentation,
|
||||
readability-redundant-control-flow,
|
||||
readability-redundant-smartptr-get,
|
||||
readability-redundant-string-cstr,
|
||||
readability-redundant-string-init,
|
||||
readability-string-compare,
|
||||
modernize-use-nullptr,
|
||||
modernize-use-override,
|
||||
modernize-deprecated-headers,
|
||||
cppcoreguidelines-init-variables,
|
||||
cppcoreguidelines-pro-type-member-init,
|
||||
-bugprone-easily-swappable-parameters,
|
||||
-bugprone-implicit-widening-of-multiplication-result,
|
||||
-bugprone-narrowing-conversions,
|
||||
-bugprone-branch-clone,
|
||||
-bugprone-signed-char-misuse,
|
||||
-bugprone-reserved-identifier,
|
||||
-bugprone-unchecked-optional-access,
|
||||
-performance-no-int-to-ptr,
|
||||
-performance-avoid-endl
|
||||
|
||||
# Warn-only initially. Once a check is clean repo-wide we can promote it here.
|
||||
WarningsAsErrors: ''
|
||||
|
||||
# Run on project sources; skip vendored/generated code.
|
||||
HeaderFilterRegex: '^.*src/(?!json/nlohmann_json|leveldb|lz4|tor/tor-src).*\.h$'
|
||||
|
||||
FormatStyle: file
|
||||
|
||||
CheckOptions:
|
||||
- key: readability-identifier-naming.IgnoreMainLikeFunctions
|
||||
value: '1'
|
||||
- key: cppcoreguidelines-init-variables.IncludeStyle
|
||||
value: 'google'
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(C:/msys64/msys2_shell.cmd -mingw64 -defterm -no-start -here -c \"ls /mingw64/lib/libboost_system* 2>/dev/null\")",
|
||||
"Bash(git tag:*)"
|
||||
],
|
||||
"additionalDirectories": [
|
||||
"C:\\msys64\\mingw64\\bin"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# Revisions listed here are skipped by `git blame` when --ignore-revs-file
|
||||
# is configured. GitHub honors this file automatically.
|
||||
#
|
||||
# Add the SHA of any large mechanical reformat / rename / mass-style commit
|
||||
# below, with a one-line comment.
|
||||
#
|
||||
# Example:
|
||||
# abc1234567890abcdef # repo-wide clang-format (no behavior change)
|
||||
#
|
||||
# To enable locally:
|
||||
# git config blame.ignoreRevsFile .git-blame-ignore-revs
|
||||
+100
-10
@@ -13,14 +13,20 @@ jobs:
|
||||
runs-on: ubuntu-22.04
|
||||
continue-on-error: true
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential cmake ninja-build \
|
||||
libboost-all-dev libssl-dev libdb++-dev libleveldb-dev \
|
||||
libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
librocksdb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
@@ -37,13 +43,70 @@ jobs:
|
||||
- name: Run unit tests
|
||||
run: cd build && ctest --output-on-failure || true
|
||||
|
||||
test-linux-sanitizers:
|
||||
# ASan + UBSan build of the daemon + unit tests. Allowed to fail until
|
||||
# findings are triaged — see .github/workflows/lint.yml comment block.
|
||||
# Once the test suite is clean under sanitizers, drop continue-on-error.
|
||||
runs-on: ubuntu-22.04
|
||||
continue-on-error: true
|
||||
env:
|
||||
# ASan: leak detection off by default (BDB and OpenSSL produce noise on shutdown).
|
||||
# Re-enable once we've quieted the legitimate suspects.
|
||||
ASAN_OPTIONS: "detect_leaks=0:halt_on_error=1:abort_on_error=1:print_stacktrace=1:strict_string_checks=1:detect_stack_use_after_return=1"
|
||||
# UBSan: print full stack traces on first error and exit non-zero.
|
||||
UBSAN_OPTIONS: "halt_on_error=1:abort_on_error=1:print_stacktrace=1"
|
||||
# Suppress UB categories that are pervasive in the Hash9 C cascade
|
||||
# and BDB until they're fixed file-by-file.
|
||||
SAN_FLAGS: "-fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=undefined -fno-sanitize=alignment,signed-integer-overflow,vptr"
|
||||
steps:
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential cmake ninja-build \
|
||||
libboost-all-dev libssl-dev libdb++-dev libleveldb-dev \
|
||||
librocksdb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
|
||||
- name: Configure with sanitizers
|
||||
run: |
|
||||
cmake -B build-san -G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Debug \
|
||||
-DCMAKE_C_FLAGS="$SAN_FLAGS" \
|
||||
-DCMAKE_CXX_FLAGS="$SAN_FLAGS" \
|
||||
-DCMAKE_EXE_LINKER_FLAGS="$SAN_FLAGS" \
|
||||
-DCMAKE_SHARED_LINKER_FLAGS="$SAN_FLAGS" \
|
||||
-DBUILD_QT=OFF \
|
||||
-DBUILD_DAEMON=ON \
|
||||
-DBUILD_TESTS=ON \
|
||||
-DUSE_UPNP=OFF
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build-san -j$(nproc)
|
||||
|
||||
- name: Run unit tests under sanitizers
|
||||
run: cd build-san && ctest --output-on-failure
|
||||
|
||||
build-windows-qt:
|
||||
runs-on: windows-latest
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
shell: bash
|
||||
|
||||
- uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
@@ -61,6 +124,7 @@ jobs:
|
||||
mingw-w64-x86_64-libevent
|
||||
mingw-w64-x86_64-miniupnpc
|
||||
mingw-w64-x86_64-zlib
|
||||
mingw-w64-x86_64-rocksdb
|
||||
|
||||
- name: Set VERSION
|
||||
run: |
|
||||
@@ -193,7 +257,14 @@ jobs:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
shell: bash
|
||||
|
||||
- uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
@@ -209,6 +280,7 @@ jobs:
|
||||
mingw-w64-x86_64-libevent
|
||||
mingw-w64-x86_64-miniupnpc
|
||||
mingw-w64-x86_64-zlib
|
||||
mingw-w64-x86_64-rocksdb
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
@@ -255,7 +327,13 @@ jobs:
|
||||
build-linux-qt:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
|
||||
- name: Set VERSION
|
||||
run: |
|
||||
@@ -274,7 +352,7 @@ jobs:
|
||||
sudo apt-get install -y build-essential cmake ninja-build \
|
||||
qtbase5-dev qttools5-dev-tools \
|
||||
libboost-all-dev libssl-dev libdb++-dev \
|
||||
libleveldb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
libleveldb-dev librocksdb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
@@ -372,7 +450,13 @@ jobs:
|
||||
build-linux-daemon:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
|
||||
- name: Set VERSION
|
||||
run: |
|
||||
@@ -390,7 +474,7 @@ jobs:
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential cmake ninja-build \
|
||||
libboost-all-dev libssl-dev libdb++-dev libleveldb-dev \
|
||||
libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
librocksdb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
@@ -502,7 +586,13 @@ jobs:
|
||||
build-macos:
|
||||
runs-on: macos-15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout (with history for submodule)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive
|
||||
|
||||
- name: Set VERSION
|
||||
run: |
|
||||
@@ -517,7 +607,7 @@ jobs:
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
brew install cmake ninja qt@5 openssl@3 boost berkeley-db@5 leveldb libevent miniupnpc
|
||||
brew install cmake ninja qt@5 openssl@3 boost berkeley-db@5 leveldb rocksdb libevent miniupnpc
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
name: Lint
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [master]
|
||||
workflow_dispatch:
|
||||
|
||||
# Diff-only enforcement: clang-format and clang-tidy run only on lines changed
|
||||
# in the PR. Existing files keep their current style until they're edited.
|
||||
# See .clang-format and .clang-tidy for the rule sets.
|
||||
|
||||
jobs:
|
||||
clang-format-diff:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
# Need merge-base with target branch to compute the diff.
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install clang-format
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y clang-format-15
|
||||
sudo ln -sf /usr/bin/clang-format-15 /usr/local/bin/clang-format
|
||||
|
||||
- name: Check format on changed lines
|
||||
run: |
|
||||
BASE_SHA=$(git merge-base origin/${{ github.base_ref }} HEAD)
|
||||
echo "Comparing against merge-base: $BASE_SHA"
|
||||
|
||||
# git-clang-format prints a diff if any changed line violates style.
|
||||
# --diff exits non-zero when reformatting would change something.
|
||||
OUTPUT=$(git clang-format --diff "$BASE_SHA" -- '*.cpp' '*.h' '*.hpp' '*.cc' || true)
|
||||
|
||||
if [ -z "$OUTPUT" ] || [ "$OUTPUT" = "no modified files to format" ] || [ "$OUTPUT" = "clang-format did not modify any files" ]; then
|
||||
echo "clang-format: clean"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "::error::clang-format wants to change the following on lines you touched."
|
||||
echo "Run \`git clang-format $BASE_SHA\` locally and commit the result."
|
||||
echo "$OUTPUT"
|
||||
exit 1
|
||||
|
||||
clang-tidy-diff:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install dependencies + clang-tidy
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential cmake ninja-build clang-tidy-15 \
|
||||
libboost-all-dev libssl-dev libdb++-dev libleveldb-dev \
|
||||
librocksdb-dev libevent-dev libminiupnpc-dev zlib1g-dev
|
||||
sudo ln -sf /usr/bin/clang-tidy-15 /usr/local/bin/clang-tidy
|
||||
|
||||
- name: Configure (export compile_commands.json)
|
||||
run: |
|
||||
cmake -B build -G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Debug \
|
||||
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
|
||||
-DBUILD_QT=OFF \
|
||||
-DBUILD_DAEMON=ON \
|
||||
-DBUILD_TESTS=ON \
|
||||
-DUSE_UPNP=OFF
|
||||
|
||||
- name: Generate build artifacts that headers depend on
|
||||
# build.h, qt UI headers, etc. — clang-tidy needs them to parse sources.
|
||||
run: cmake --build build --target generate_build_info
|
||||
|
||||
- name: Run clang-tidy on changed lines
|
||||
run: |
|
||||
BASE_SHA=$(git merge-base origin/${{ github.base_ref }} HEAD)
|
||||
echo "Comparing against merge-base: $BASE_SHA"
|
||||
|
||||
# clang-tidy-diff.py ships with clang-tidy; runs tidy only on changed lines.
|
||||
DIFF_SCRIPT=$(dpkg -L clang-tidy-15 | grep clang-tidy-diff.py | head -1)
|
||||
if [ -z "$DIFF_SCRIPT" ]; then
|
||||
DIFF_SCRIPT=/usr/share/clang/clang-tidy-diff.py
|
||||
fi
|
||||
echo "Using: $DIFF_SCRIPT"
|
||||
|
||||
# -p1 strips the leading "a/"/"b/" from git diff paths.
|
||||
# -path=build points clang-tidy at compile_commands.json.
|
||||
# -iregex restricts to project sources (not vendored).
|
||||
git diff -U0 "$BASE_SHA" -- 'src/*.cpp' 'src/*.h' \
|
||||
':(exclude)src/json/nlohmann_json.hpp' \
|
||||
':(exclude)src/leveldb/*' \
|
||||
':(exclude)src/lz4/*' \
|
||||
':(exclude)src/tor/tor-src/*' \
|
||||
| python3 "$DIFF_SCRIPT" -p1 -path build \
|
||||
-iregex '.*\.(cpp|cc|h|hpp)$' \
|
||||
-j$(nproc) || EXIT=$?
|
||||
|
||||
# Warn-only initially. Flip this to `exit ${EXIT:-0}` once we're clean.
|
||||
exit 0
|
||||
+15
@@ -1,3 +1,6 @@
|
||||
# Per-user Claude Code settings (machine-specific paths/permissions)
|
||||
.claude/
|
||||
|
||||
# Build artifacts
|
||||
*.o
|
||||
*.exe
|
||||
@@ -7,8 +10,12 @@
|
||||
*.a
|
||||
/dist/
|
||||
build/
|
||||
build2/
|
||||
build_*/
|
||||
release/
|
||||
debug/
|
||||
build_err*.txt
|
||||
*build_err.txt
|
||||
/Makefile
|
||||
Makefile.Debug
|
||||
Makefile.Release
|
||||
@@ -24,6 +31,7 @@ ui_*.h
|
||||
qrc_*.cpp
|
||||
*.pro.user
|
||||
*.pro.user.*
|
||||
*.qm
|
||||
|
||||
# Blockchain data
|
||||
*.dat
|
||||
@@ -63,3 +71,10 @@ triangles.conf
|
||||
*.o
|
||||
src/trianglesd
|
||||
src/obj/
|
||||
build-bench/
|
||||
build-cmake/
|
||||
build-cmake-test/
|
||||
build-latest/
|
||||
build-rocks-probe/
|
||||
build-rocksdb/
|
||||
bench-results.csv
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
[submodule "src/tor/tor-src"]
|
||||
path = src/tor/tor-src
|
||||
url = https://gitlab.torproject.org/tpo/core/tor.git
|
||||
[submodule "src/secp256k1"]
|
||||
path = src/secp256k1
|
||||
url = https://github.com/bitcoin-core/secp256k1
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
# Triangles Codebase Cleanup Notes
|
||||
|
||||
## Overview
|
||||
Systematic code quality improvements for the Triangles cryptocurrency codebase (v5.3.4+).
|
||||
|
||||
**Goal:** Improve maintainability without changing behavior or breaking consensus.
|
||||
|
||||
## Inventory
|
||||
|
||||
### TODOs/FIXMEs Found (38 total)
|
||||
|
||||
#### High Priority (Affects Safety/Correctness)
|
||||
- `rpcmining.cpp:263` - **Thread safety issue** in mapNewBlock (static variable, no mutex)
|
||||
- `walletmodel.cpp:249` - **Potential collision** in balance calculation
|
||||
- `smessage.cpp:863, 2219, 2373` - **File size limit** (files must be split if >2GB)
|
||||
|
||||
#### Medium Priority (Encapsulation/Security)
|
||||
- `protocol.h:50, 100, 132` - Public members should be private (3 locations)
|
||||
- `wallet.h:378` - nOrderPos calculation should move elsewhere
|
||||
- `wallet.cpp:733, 1732` - Change output handling needs improvement
|
||||
- `rpcwallet.cpp:1474, 1513, 1569` - SecureString operator= missing (forced .c_str())
|
||||
|
||||
#### Low Priority (Nice-to-Have)
|
||||
- `util.cpp:1322` - Disabled feature needs verification
|
||||
- `tor/tor_embedded.cpp:209` - Tor 0.4.9+ shutdown API upgrade
|
||||
- `init.cpp:442` - Remaining sanity checks (see Bitcoin issue #4081)
|
||||
- `rpcmining.cpp:232` - DRM comment (unclear what it means)
|
||||
- `smessage.cpp:*` - Various improvements (hash inclusion, thread safety, defaults)
|
||||
- `qt/*` - UI improvements (decrypt not supported, message filtering, OSX startup)
|
||||
|
||||
#### External/Third-Party (Don't Touch)
|
||||
- `leveldb/*` - LevelDB library TODOs (upstream issues)
|
||||
|
||||
## Code Quality Issues
|
||||
|
||||
### Using namespace std (37 files)
|
||||
All in .cpp files - **this is fine for .cpp**, problematic only in headers.
|
||||
No headers have this issue, so **no action needed**.
|
||||
|
||||
### Printf/Cout Usage (56 files)
|
||||
Most cryptocurrency code uses printf for early init/error handling before logging is available.
|
||||
**Review needed:** Check if these are legitimate early-init cases or should use LogPrintf.
|
||||
|
||||
## Cleanup Plan (Safest → Riskiest)
|
||||
|
||||
### Phase 1: Documentation & Comments ✅ SAFE
|
||||
1. Document all TODOs with context (why deferred, what's needed)
|
||||
2. Add function-level comments for complex logic
|
||||
3. Improve inline comments for clarity
|
||||
|
||||
### Phase 2: Low-Risk Code Quality 🟨 MEDIUM RISK
|
||||
4. Fix compiler warnings (-Wall -Wextra)
|
||||
5. Add const correctness where missing
|
||||
6. Remove commented-out dead code
|
||||
7. Standardize code formatting (if inconsistent)
|
||||
|
||||
### Phase 3: Functional Improvements 🟥 HIGH RISK (Skip for now)
|
||||
8. Fix thread safety issue in rpcmining.cpp (requires testing)
|
||||
9. Improve protocol.h encapsulation (may affect other code)
|
||||
10. Address >2GB file handling in smessage.cpp
|
||||
|
||||
## Decisions
|
||||
|
||||
### What NOT to Change
|
||||
- **Consensus code** - main.cpp (validation), kernel.cpp (PoS), miner.cpp (staking)
|
||||
- **Serialization** - Any READWRITE, serialize/deserialize code
|
||||
- **Protocol constants** - Network message types, version numbers
|
||||
- **Third-party code** - leveldb/, tor/, sph_types.h, xxhash/, lz4/
|
||||
|
||||
### What's Safe to Change
|
||||
- Comments and documentation
|
||||
- Variable names (in non-consensus code)
|
||||
- Code organization (splitting large functions)
|
||||
- Logging statements
|
||||
- UI code (qt/)
|
||||
- RPC interface (as long as API contract preserved)
|
||||
|
||||
## Initial Cleanup (2026-03-22)
|
||||
|
||||
### Actions Taken
|
||||
1. Created this documentation file
|
||||
2. Created cleanup/desloppify branch
|
||||
3. Inventoried all TODOs/FIXMEs
|
||||
|
||||
### Next Steps
|
||||
1. Add documentation comments to TODO items
|
||||
2. Review printf/cout usage patterns
|
||||
3. Check for compiler warnings
|
||||
4. Consider low-risk improvements
|
||||
|
||||
## Notes
|
||||
- This is a Bitcoin-derived codebase, so many patterns follow Bitcoin Core conventions
|
||||
- Recent v5.3.x work already modernized to C++17 and removed Boost - good foundation
|
||||
- Code is generally well-structured; main improvements are documentation and minor cleanup
|
||||
@@ -1,76 +0,0 @@
|
||||
# Triangles Cleanup Strategy - Safe Improvements
|
||||
|
||||
**Branch:** `cleanup/safe-improvements`
|
||||
**Goal:** Improve code quality without touching consensus-critical code
|
||||
|
||||
## ✅ SAFE TO FIX
|
||||
|
||||
### 1. Compiler Warnings (Non-Consensus)
|
||||
- **C++11 literal-suffix warnings** - Add spaces between literals and suffixes
|
||||
- **Unused variables/functions** - Remove dead code (verify not consensus-critical first)
|
||||
- **Deprecated-copy warnings** - Fix CScript assignment operator if safe
|
||||
|
||||
### 2. Code Style Improvements
|
||||
- Remove `using namespace std` from headers (keep in .cpp files)
|
||||
- Standardize logging patterns
|
||||
- Improve code comments (remove unclear/misleading ones)
|
||||
- Add context to TODOs/FIXMEs
|
||||
|
||||
### 3. Documentation
|
||||
- Add inline comments for thread safety concerns
|
||||
- Document collision vulnerabilities
|
||||
- Improve function/class documentation
|
||||
|
||||
## ❌ DO NOT TOUCH
|
||||
|
||||
### Consensus-Critical Code
|
||||
- **OpenSSL SHA256/RIPEMD160 usage** - Deprecated warnings OK, do not change
|
||||
- **BN_is_prime_ex** - Crypto library deprecation, leave as-is
|
||||
- **Hash algorithms** - Third-party libraries with warnings, consensus-critical
|
||||
- **Block validation logic** - Any code affecting block/transaction validation
|
||||
- **Merkle tree construction** - Core consensus
|
||||
- **Proof-of-Work/Proof-of-Stake** - Staking/mining algorithms
|
||||
|
||||
### How to Identify Consensus Code
|
||||
- Files in `src/` related to: `main.cpp`, `main.h`, block validation, transaction validation
|
||||
- Anything in hash algorithm libraries
|
||||
- Cryptographic primitives
|
||||
- Network protocol message formats (version, serialization)
|
||||
|
||||
## Incremental Testing Strategy
|
||||
|
||||
1. **One warning category at a time**
|
||||
2. **Compile after each change**
|
||||
3. **Test basic functionality:**
|
||||
- `trianglesd getinfo`
|
||||
- `trianglesd getblockchaininfo`
|
||||
- Verify block sync works
|
||||
4. **Commit incrementally** with clear messages
|
||||
|
||||
## Warning Categories (From Build Output)
|
||||
|
||||
```
|
||||
1. C++11 literal-suffix: ~20 instances (util.h, net.h, alert.cpp)
|
||||
2. OpenSSL deprecation: SHA256, RIPEMD160 (DO NOT FIX)
|
||||
3. BN_is_prime_ex: crypto library (DO NOT FIX)
|
||||
4. Deprecated-copy: CScript assignment (REVIEW CAREFULLY)
|
||||
5. Unused variables/functions: Various (SAFE IF NOT CONSENSUS)
|
||||
```
|
||||
|
||||
## Branch History
|
||||
|
||||
- Previous work: `cleanup/desloppify` (documentation improvements, merged to master)
|
||||
- This branch: Focus on safe compiler warnings and code quality
|
||||
|
||||
## Verification Checklist
|
||||
|
||||
Before pushing each commit:
|
||||
- [ ] Code compiles successfully
|
||||
- [ ] No new warnings introduced
|
||||
- [ ] trianglesd runs without errors
|
||||
- [ ] getinfo/getblockchaininfo work
|
||||
- [ ] No consensus-critical code touched
|
||||
|
||||
---
|
||||
|
||||
**Principle:** When in doubt, don't touch it. A clean codebase is worthless if the blockchain forks.
|
||||
+86
-3
@@ -6,17 +6,37 @@ if(POLICY CMP0167)
|
||||
endif()
|
||||
|
||||
project(Triangles
|
||||
VERSION 5.8.6
|
||||
VERSION 5.9.5
|
||||
DESCRIPTION "Cryptographic Triangles Wallet"
|
||||
LANGUAGES C CXX
|
||||
)
|
||||
|
||||
# ── C++ Standard ──
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
# C++20 required: RocksDB headers in MSYS2/Homebrew (8.x+) use `using enum`
|
||||
# and defaulted operator== on user-defined types, both C++20-only.
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
# ── Build acceleration ──
|
||||
# ccache: auto-detect and use if available
|
||||
find_program(CCACHE_PROGRAM ccache)
|
||||
if(CCACHE_PROGRAM)
|
||||
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
|
||||
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
|
||||
message(STATUS "ccache found: ${CCACHE_PROGRAM}")
|
||||
else()
|
||||
message(STATUS "ccache not found — install it for faster rebuilds")
|
||||
endif()
|
||||
|
||||
# Unity (jumbo) build: batch source files to reduce header parsing overhead
|
||||
option(ENABLE_UNITY_BUILD "Enable CMake unity (jumbo) builds" OFF)
|
||||
if(ENABLE_UNITY_BUILD)
|
||||
set(CMAKE_UNITY_BUILD ON)
|
||||
set(CMAKE_UNITY_BUILD_BATCH_SIZE 8)
|
||||
endif()
|
||||
|
||||
# ── Output directories ──
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
||||
@@ -53,7 +73,7 @@ include(AddCompilerFlags)
|
||||
# ── Find required dependencies ──
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(Boost 1.71 REQUIRED COMPONENTS
|
||||
filesystem program_options thread chrono
|
||||
program_options thread chrono
|
||||
)
|
||||
if(BUILD_TESTS)
|
||||
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
|
||||
@@ -77,6 +97,66 @@ if(USE_ZMQ)
|
||||
pkg_check_modules(ZMQ REQUIRED IMPORTED_TARGET libzmq)
|
||||
endif()
|
||||
|
||||
# RocksDB is now a hard dependency: backs both the chain database and the
|
||||
# secure-messaging store (smessage). Probe in order:
|
||||
# 1. CMake config package (MSYS2, Homebrew, vcpkg, recent Linux)
|
||||
# 2. pkg-config (some Linux distros, no .cmake files)
|
||||
# 3. Manual find_path/find_library (Ubuntu 22.04's librocksdb-dev ships
|
||||
# neither a CMake config nor a .pc file)
|
||||
# In all paths, a target named RocksDB::rocksdb is exposed for consumers.
|
||||
find_package(RocksDB CONFIG QUIET)
|
||||
if(NOT RocksDB_FOUND)
|
||||
find_package(PkgConfig QUIET)
|
||||
if(PkgConfig_FOUND)
|
||||
pkg_check_modules(RocksDB IMPORTED_TARGET QUIET rocksdb)
|
||||
endif()
|
||||
endif()
|
||||
if(NOT TARGET RocksDB::rocksdb AND NOT TARGET PkgConfig::RocksDB)
|
||||
find_path(ROCKSDB_INCLUDE_DIR
|
||||
NAMES rocksdb/db.h
|
||||
PATHS /usr/include /usr/local/include
|
||||
)
|
||||
find_library(ROCKSDB_LIBRARY
|
||||
NAMES rocksdb
|
||||
PATHS /usr/lib /usr/lib/x86_64-linux-gnu /usr/local/lib
|
||||
)
|
||||
if(NOT ROCKSDB_INCLUDE_DIR OR NOT ROCKSDB_LIBRARY)
|
||||
message(FATAL_ERROR
|
||||
"RocksDB not found. Install librocksdb-dev (Ubuntu/Debian), "
|
||||
"rocksdb (Homebrew), or mingw-w64-x86_64-rocksdb (MSYS2).")
|
||||
endif()
|
||||
add_library(RocksDB::rocksdb UNKNOWN IMPORTED)
|
||||
set_target_properties(RocksDB::rocksdb PROPERTIES
|
||||
IMPORTED_LOCATION "${ROCKSDB_LIBRARY}"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${ROCKSDB_INCLUDE_DIR}"
|
||||
)
|
||||
message(STATUS "Found RocksDB (manual probe): ${ROCKSDB_LIBRARY}")
|
||||
endif()
|
||||
|
||||
# libsecp256k1 — vendored as a git submodule under src/secp256k1. Provides
|
||||
# ECDSA signing/verification, pubkey recovery (via the recovery module), and
|
||||
# ECDH for secure messaging. Configure the submodule's build for our needs:
|
||||
# only ECDH + recovery, none of the test/benchmark/extra-module bloat, and
|
||||
# don't install (we link statically against the in-tree target).
|
||||
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/src/secp256k1/CMakeLists.txt")
|
||||
message(FATAL_ERROR
|
||||
"src/secp256k1 is empty. Run: git submodule update --init --recursive")
|
||||
endif()
|
||||
set(SECP256K1_DISABLE_SHARED ON CACHE INTERNAL "")
|
||||
set(SECP256K1_INSTALL OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_BUILD_BENCHMARK OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_BUILD_TESTS OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_BUILD_EXHAUSTIVE_TESTS OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_BUILD_CTIME_TESTS OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_BUILD_EXAMPLES OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_ECDH ON CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_RECOVERY ON CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_EXTRAKEYS OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_SCHNORRSIG OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_MUSIG OFF CACHE INTERNAL "")
|
||||
set(SECP256K1_ENABLE_MODULE_ELLSWIFT OFF CACHE INTERNAL "")
|
||||
add_subdirectory(src/secp256k1 EXCLUDE_FROM_ALL)
|
||||
|
||||
if(BUILD_QT)
|
||||
find_package(Qt5 5.9 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
find_package(Qt5 COMPONENTS LinguistTools QUIET)
|
||||
@@ -113,4 +193,7 @@ message(STATUS " D-Bus: ${USE_DBUS}")
|
||||
message(STATUS " ZMQ: ${USE_ZMQ}")
|
||||
message(STATUS " Embedded Tor: ${USE_TOR_EMBEDDED}")
|
||||
message(STATUS " Static linking: ${ENABLE_STATIC}")
|
||||
message(STATUS " ccache: ${CCACHE_PROGRAM}")
|
||||
message(STATUS " Unity build: ${ENABLE_UNITY_BUILD}")
|
||||
message(STATUS " Precompiled header: ON")
|
||||
message(STATUS "")
|
||||
|
||||
@@ -1,220 +0,0 @@
|
||||
# Embedded Tor Integration Guide for Triangles
|
||||
|
||||
This guide explains how to compile Tor as a static library (`libtor.a`) and link
|
||||
it directly into the Triangles wallet binary so that every node automatically
|
||||
runs a Tor hidden service without needing an external Tor installation.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
```
|
||||
trianglesd / triangles-qt
|
||||
├── tor_embedded.cpp ← calls tor_run_main() in a background thread
|
||||
├── tor_process.cpp ← fallback: launches external tor binary (already works)
|
||||
├── onion_v3.cpp ← V3 onion address generation / SOCKS5 proxy logic
|
||||
└── libtor.a ← aggregate static Tor library (built from official source)
|
||||
```
|
||||
|
||||
When compiled with `ENABLE_TOR_EMBEDDED`, the wallet calls `tor_run_main()` from
|
||||
`tor_api.h` on a dedicated thread. This gives the wallet a SOCKS5 proxy on
|
||||
`127.0.0.1:19099` and a V3 hidden service on port 24112 (the P2P port).
|
||||
|
||||
When compiled **without** the flag, `tor_embedded.cpp` falls back to the external
|
||||
`tor_process.cpp` which searches for and launches a system `tor` binary.
|
||||
|
||||
## Step 1: Add Tor as a Git Submodule
|
||||
|
||||
```bash
|
||||
cd /path/to/triangles
|
||||
git submodule add https://gitlab.torproject.org/tpo/core/tor.git src/tor/tor-src
|
||||
cd src/tor/tor-src
|
||||
git checkout release-0.4.9 # latest stable branch as of 2026
|
||||
```
|
||||
|
||||
This puts the full Tor source at `src/tor/tor-src/`.
|
||||
Current imported checkout in this repo: `release-0.4.9` at commit `1442ca4`.
|
||||
There is also a helper build script at `src/tor/build-libtor.sh`.
|
||||
|
||||
## Step 2: Build libtor.a
|
||||
|
||||
Tor uses autotools. Build it as a static library:
|
||||
|
||||
```bash
|
||||
cd src/tor/tor-src
|
||||
|
||||
# Install Tor build dependencies
|
||||
sudo apt install autoconf automake libtool pkg-config \
|
||||
libssl-dev libevent-dev zlib1g-dev
|
||||
|
||||
# Generate configure script
|
||||
./autogen.sh
|
||||
|
||||
# Configure for static library build (disable unneeded modules)
|
||||
./configure \
|
||||
--enable-static-tor \
|
||||
--disable-module-relay \
|
||||
--disable-module-dirauth \
|
||||
--disable-asciidoc \
|
||||
--disable-manpage \
|
||||
--disable-html-manual \
|
||||
--disable-unittests \
|
||||
--disable-tool-name-check \
|
||||
--with-openssl-dir=/usr \
|
||||
--with-libevent-dir=/usr \
|
||||
--with-zlib-dir=/usr \
|
||||
--prefix=/usr/local
|
||||
|
||||
make -j$(nproc)
|
||||
```
|
||||
|
||||
Or from the repo root:
|
||||
```bash
|
||||
./src/tor/build-libtor.sh
|
||||
```
|
||||
|
||||
After building, the static libraries are in `src/tor/tor-src/`:
|
||||
- `libtor.a`
|
||||
- `src/lib/libtor-*.a` (multiple component libs)
|
||||
|
||||
The header `src/feature/api/tor_api.h` provides the public C API:
|
||||
```c
|
||||
tor_main_configuration_t *tor_main_configuration_new(void);
|
||||
int tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
|
||||
int argc, char *argv[]);
|
||||
int tor_run_main(const tor_main_configuration_t *);
|
||||
void tor_main_configuration_free(tor_main_configuration_t *);
|
||||
```
|
||||
|
||||
## Step 3: Build Triangles with Embedded Tor
|
||||
|
||||
### Linux (makefile.unix)
|
||||
|
||||
```bash
|
||||
cd src
|
||||
|
||||
# Point to Tor's built libraries and headers
|
||||
make -f makefile.unix \
|
||||
USE_TOR_EMBEDDED=1
|
||||
```
|
||||
|
||||
You may need to adjust the `-l` flags in the makefile depending on the exact
|
||||
library names Tor produces. Check `src/tor/tor-src/` after building:
|
||||
|
||||
```bash
|
||||
find tor/tor-src -name '*.a' | sort
|
||||
```
|
||||
|
||||
On the imported `release-0.4.9` checkout in this repo, the simplest working
|
||||
link path is the aggregate `libtor.a` plus the normal dependency libraries.
|
||||
|
||||
### Windows (triangles-qt.pro)
|
||||
|
||||
Add to `triangles-qt.pro`:
|
||||
```qmake
|
||||
qmake "USE_TOR_EMBEDDED=1" \
|
||||
"TOR_SOURCE_ROOT=src/tor/tor-src"
|
||||
```
|
||||
|
||||
Both build systems now default to:
|
||||
- source root: `src/tor/tor-src`
|
||||
- include path: `src/tor/tor-src/src/feature/api`
|
||||
- library path: `src/tor/tor-src`
|
||||
- embedded Tor library: `-ltor`
|
||||
|
||||
On Windows, the imported Tor `0.4.9.5` build also needed:
|
||||
- `-llzma`
|
||||
- `-lzstd`
|
||||
- `-liphlpapi`
|
||||
- `-lshlwapi` (already linked by Triangles)
|
||||
|
||||
## Step 4: Wire into init.cpp
|
||||
|
||||
The global hooks `StartEmbeddedTor()` and `StopEmbeddedTor()` need to be called
|
||||
from `init.cpp`. Add these calls:
|
||||
|
||||
### In AppInit2() (after network init, before starting node):
|
||||
```cpp
|
||||
#include "tor/tor_embedded.h"
|
||||
|
||||
// Near the end of AppInit2, after network initialization:
|
||||
if (!StartEmbeddedTor()) {
|
||||
printf("WARNING: Embedded Tor failed to start. .onion connectivity unavailable.\n");
|
||||
// Non-fatal: wallet works without Tor, just no .onion
|
||||
}
|
||||
```
|
||||
|
||||
### In Shutdown():
|
||||
```cpp
|
||||
StopEmbeddedTor();
|
||||
```
|
||||
|
||||
## Step 5: Configure SOCKS Proxy for Outbound Connections
|
||||
|
||||
After Tor starts, the wallet needs to route `.onion` connections through the
|
||||
SOCKS5 proxy. In `net.cpp`, after Tor is initialized:
|
||||
|
||||
```cpp
|
||||
// If embedded Tor is running, use its SOCKS proxy for .onion addresses
|
||||
CTorEmbedded* tor = CTorEmbedded::GetInstance();
|
||||
if (tor->IsRunning()) {
|
||||
// Set proxy for .onion connections
|
||||
proxyType addrProxy(CService("127.0.0.1", tor->GetSocksPort()), 5);
|
||||
SetNameProxy(addrProxy);
|
||||
}
|
||||
```
|
||||
|
||||
## Runtime Flags
|
||||
|
||||
The embedded Tor respects these command-line flags:
|
||||
|
||||
| Flag | Default | Description |
|
||||
|------|---------|-------------|
|
||||
| `-notor` | false | Disable Tor entirely |
|
||||
| `-torsocks=PORT` | 19099 | SOCKS5 proxy port |
|
||||
| `-torhsport=PORT` | 24112 | Hidden service virtual port |
|
||||
|
||||
## File Layout After Integration
|
||||
|
||||
```
|
||||
src/tor/
|
||||
├── tor-src/ ← git submodule (official Tor repo)
|
||||
│ └── src/
|
||||
│ ├── lib/libtor-*.a
|
||||
│ └── feature/api/tor_api.h
|
||||
│ └── libtor.a
|
||||
├── tor_embedded.h ← CTorEmbedded class header
|
||||
├── tor_embedded.cpp ← implementation (calls tor_run_main)
|
||||
├── tor_process.h ← external Tor process manager (fallback)
|
||||
├── tor_process.cpp
|
||||
├── onion_v3.h ← V3 onion address utilities
|
||||
├── onion_v3.cpp
|
||||
├── anonymize.h ← data dir helpers
|
||||
├── anonymize.cpp
|
||||
└── LICENSE
|
||||
```
|
||||
|
||||
## Reference: How VERGE (XVG) Does It
|
||||
|
||||
VERGE uses the same pattern. Their implementation is at:
|
||||
- `src/torcontroller.cpp` (~100 lines)
|
||||
- They use `tor_main()` (older API, pre-0.4.5)
|
||||
- Git submodule at `src/tor/` pointing to `release-0.4.8` branch
|
||||
- Build Tor as part of their `depends/` system
|
||||
|
||||
Key difference: modern Tor (0.4.5+) uses `tor_run_main()` with a configuration
|
||||
object instead of raw `tor_main(int argc, char** argv)`.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**Tor fails to bootstrap**: Check firewall rules. Tor needs outbound TCP to the
|
||||
Tor network (ports 80, 443, 9001, 9030).
|
||||
|
||||
**Link errors with libtor**: Prefer the aggregate `libtor.a` from the top level
|
||||
of the Tor build tree. On the imported Windows/MSYS2 build in this repo, the
|
||||
minimal verified link set was:
|
||||
```
|
||||
-ltor -levent -lssl -lcrypto -lz -llzma -lzstd -lws2_32 -liphlpapi -lshlwapi
|
||||
```
|
||||
|
||||
**OpenSSL version mismatch**: Both Tor and Triangles must link against the same
|
||||
OpenSSL version (3.x). If Tor was built against a different OpenSSL, rebuild it
|
||||
with the same `--with-openssl-dir`.
|
||||
@@ -1,210 +0,0 @@
|
||||
# Triangles Modernization Roadmap
|
||||
|
||||
**Goal:** Make TRI faster to sync, safer for wallets, and more useful as a currency — without breaking consensus.
|
||||
|
||||
**Invariant:** Any change that modifies block validation, stake modifier computation, transaction format, or signature verification MUST preserve exact consensus with existing v5.x nodes. When in doubt, test against a synced v5.8.1 node.
|
||||
|
||||
---
|
||||
|
||||
## Priority 1: Faster Syncing (High Impact, Low Risk)
|
||||
|
||||
### 1.1 Update Checkpoints (Easy, Immediate)
|
||||
**Problem:** Last hardcoded checkpoint is at block 2,186,940. `IsInitialBlockDownload()` returns false past this point, causing orphan limit to drop from 4000 to 750 — exactly what caused the fork deadlock.
|
||||
|
||||
**Fix:** Add checkpoints every ~50,000 blocks up to current height (~2,207,000+).
|
||||
```cpp
|
||||
// src/checkpoints.cpp - add recent checkpoints
|
||||
{2190000, uint256("...")},
|
||||
{2195000, uint256("...")},
|
||||
{2200000, uint256("...")},
|
||||
{2205000, uint256("...")},
|
||||
{2210000, uint256("...")},
|
||||
```
|
||||
**Risk:** None — checkpoints are only used for IBD detection and quick rejection of clearly wrong chains.
|
||||
|
||||
### 1.2 Increase Post-Checkpoint Orphan Limit (Easy)
|
||||
**Problem:** 750 orphans after IBD is too low for a low-peer network. During the fork incident, 750 orphans filled up and the node deadlocked.
|
||||
|
||||
**Fix:**
|
||||
```cpp
|
||||
// src/main.h
|
||||
static const unsigned int MAX_ORPHAN_BLOCKS = 2000; // was 750
|
||||
```
|
||||
**Risk:** Slightly more memory usage during forks. Worth it for resilience.
|
||||
|
||||
### 1.3 Parallel Block Download (Medium Effort)
|
||||
**Problem:** Current implementation downloads blocks sequentially from one peer at a time during IBD.
|
||||
|
||||
**Fix:** Increase batch sizes and allow concurrent block downloads from multiple peers:
|
||||
```cpp
|
||||
// src/main.cpp
|
||||
// During IBD, request blocks from multiple peers simultaneously
|
||||
unsigned int nGetDataBatchSize = IsInitialBlockDownload() ? 8000 : 1000; // was 4000
|
||||
```
|
||||
**Risk:** Low — larger batch sizes are already proven in Bitcoin forks.
|
||||
|
||||
### 1.4 Header-First Sync (Medium Effort)
|
||||
**Problem:** Node downloads full blocks before validating headers. A bad peer can waste bandwidth.
|
||||
|
||||
**Fix:** Download and validate all headers first (compact ~80 bytes each), then download full blocks only for the best chain.
|
||||
- Separate `getheaders`/`headers` message handling
|
||||
- Download blocks only for the best header chain
|
||||
- Reduces wasted bandwidth during forks by 95%+
|
||||
|
||||
### 1.5 Bootstrap Over HTTPS with Resume (Easy)
|
||||
**Problem:** Built-in bootstrap (`-bootstrap`) uses raw TCP and can't resume interrupted downloads.
|
||||
|
||||
**Fix:** The existing `bootstrap.cpp` already supports downloading. Add:
|
||||
- Resume support (Range headers)
|
||||
- SHA256 verification of downloaded archive
|
||||
- Better progress reporting
|
||||
- Fallback mirrors
|
||||
|
||||
---
|
||||
|
||||
## Priority 2: Wallet Safety (Critical)
|
||||
|
||||
### 2.1 Automatic Wallet Backup Before Dangerous Operations (Easy)
|
||||
**Problem:** Corrupt wallet = lost funds. No automatic backup before risky operations.
|
||||
|
||||
**Fix:** In `walletdb.cpp`, before any rewrite:
|
||||
```cpp
|
||||
// Before wallet.dat rewrite, copy to wallet.dat.bak
|
||||
if (boost::filesystem::exists(pathWallet)) {
|
||||
boost::filesystem::copy_file(pathWallet, pathWallet + ".bak",
|
||||
boost::filesystem::copy_option::overwrite_if_exists);
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2 Detect and Report BDB Corruption (Easy)
|
||||
**Problem:** BDB corruption silently corrupts wallet. User doesn't know until it's too late.
|
||||
|
||||
**Fix:** Add wallet integrity check on load:
|
||||
```cpp
|
||||
// In CWallet::LoadWallet()
|
||||
// After opening, verify BDB environment is healthy
|
||||
// If DB_RUNRECOVERY, auto-salvage and warn user
|
||||
```
|
||||
|
||||
### 2.3 Wallet.dat Versioning (Medium Effort)
|
||||
**Problem:** Single wallet.dat file. If it corrupts during write, funds are lost.
|
||||
|
||||
**Fix:** Implement copy-on-write wallet saves:
|
||||
- Write new wallet data to `wallet.dat.new`
|
||||
- Atomically rename `wallet.dat` → `wallet.dat.old`, `wallet.dat.new` → `wallet.dat`
|
||||
- Keep last 3 wallet revisions
|
||||
- On load, try wallet.dat first, fall back to wallet.dat.old if corrupt
|
||||
|
||||
### 2.4 Seed Phrase / HD Wallet (High Effort, High Impact)
|
||||
**Problem:** Losing wallet.dat = losing everything. No recovery mechanism.
|
||||
|
||||
**Fix:** Implement BIP39/BIP44 HD wallet as optional upgrade:
|
||||
- Generate 12/24-word seed phrase on new wallet creation
|
||||
- Derive all keys from seed deterministically
|
||||
- Import seed on any device to recover wallet
|
||||
- Keep backward compatibility with existing non-HD wallets
|
||||
|
||||
---
|
||||
|
||||
## Priority 3: Network Resilience (Medium Impact)
|
||||
|
||||
### 3.1 Better Peer Management (Medium Effort)
|
||||
**Problem:** Low peer counts (2-6) lead to fork divergence. No prioritization of reliable peers.
|
||||
|
||||
**Fix:**
|
||||
- Peer reliability scoring (track which peers provide valid blocks)
|
||||
- Prefer peers that are ahead and on the same chain
|
||||
- Automatic disconnection of stale/forked peers
|
||||
- Increase default `maxconnections` from 64 to 128
|
||||
|
||||
### 3.2 Compact Block Relay (High Effort)
|
||||
**Problem:** Full blocks are sent even when the receiver likely already has most transactions.
|
||||
|
||||
**Fix:** Implement BIP 152 compact blocks:
|
||||
- Send block header + short transaction IDs
|
||||
- Receiver fills in from mempool, only requests missing transactions
|
||||
- Reduces bandwidth by ~90% during normal operation
|
||||
|
||||
### 3.3 DNS Seed Infrastructure (Easy)
|
||||
**Problem:** `dnsseed=0` when Tor-only means no automatic peer discovery.
|
||||
|
||||
**Fix:** Run a DNS seed server that resolves to known reliable onion addresses:
|
||||
```
|
||||
seed.cryptographic-triangles.org → returns onion addresses of healthy nodes
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Priority 4: User Experience (Medium Impact)
|
||||
|
||||
### 4.1 Progress Reporting for IBD (Easy)
|
||||
**Problem:** Users see "downloading blocks..." with no useful progress indicator.
|
||||
|
||||
**Fix:**
|
||||
- Report `headers` vs `blocks` progress separately
|
||||
- Show estimated time remaining based on download speed
|
||||
- Log progress every 1000 blocks (currently every 5000)
|
||||
- Qt wallet: update progress bar more frequently
|
||||
|
||||
### 4.2 Staking Dashboard Improvements (Easy)
|
||||
**Problem:** Qt wallet shows staking info but not clearly.
|
||||
|
||||
**Fix:**
|
||||
- Show expected time to stake more prominently
|
||||
- Display staking weight as percentage of network
|
||||
- Notify when stake is found (system notification)
|
||||
- Show "staking" indicator in system tray
|
||||
|
||||
### 4.3 Transaction Fee Estimation (Medium Effort)
|
||||
**Problem:** No fee estimation. Users guess.
|
||||
|
||||
**Fix:** Track recent block inclusion rates by fee level, provide fee recommendations.
|
||||
|
||||
---
|
||||
|
||||
## Priority 5: Code Modernization (Low Urgency, Good Hygiene)
|
||||
|
||||
### 5.1 C++17/20 Features
|
||||
- Replace raw pointers with smart pointers where safe
|
||||
- Use `std::optional`, `std::string_view`, `std::filesystem`
|
||||
- Replace boost::filesystem with std::filesystem (C++17)
|
||||
|
||||
### 5.2 Build System
|
||||
- CMake is already in place (good)
|
||||
- Add sanitizers (ASAN, UBSAN) to CI
|
||||
- Static analysis with clang-tidy
|
||||
|
||||
### 5.3 Testing
|
||||
- Current test coverage is thin
|
||||
- Add unit tests for:
|
||||
- Checkpoint validation
|
||||
- Stake modifier computation
|
||||
- Bootstrap download/resume
|
||||
- Wallet BDB recovery
|
||||
- Orphan block handling
|
||||
|
||||
---
|
||||
|
||||
## What NOT to Change
|
||||
|
||||
These are consensus-critical and must remain identical:
|
||||
- Block validation rules
|
||||
- Stake modifier computation (`ComputeNextStakeModifier`)
|
||||
- Transaction signature verification
|
||||
- Block reward schedule
|
||||
- PoW/PoS target computation
|
||||
- Chain trust / difficulty adjustment
|
||||
- Message serialization format
|
||||
- Protocol version handshaking
|
||||
|
||||
Any change to these requires a coordinated network upgrade (hard fork).
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
1. **This week:** Update checkpoints (1.1), increase orphan limit (1.2), wallet backup before save (2.1)
|
||||
2. **Next week:** Better progress reporting (4.1), increase batch size (1.3)
|
||||
3. **Month 1:** Wallet versioning (2.3), bootstrap resume (1.5)
|
||||
4. **Month 2:** Header-first sync (1.4), peer reliability (3.1)
|
||||
5. **Month 3+:** HD wallet (2.4), compact blocks (3.2)
|
||||
@@ -1,300 +0,0 @@
|
||||
# OpenClaw Bootstrap Snapshot Guide
|
||||
|
||||
## Purpose
|
||||
|
||||
This document tells OpenClaw exactly how to update the existing Triangles bootstrap server so new wallets download a ready-to-use snapshot instead of downloading `blk0001.dat` and rebuilding the index locally.
|
||||
|
||||
This guide matches the current wallet code in:
|
||||
|
||||
- `src/bootstrap.cpp`
|
||||
- `src/bootstrap.h`
|
||||
- `src/checkpoints.cpp`
|
||||
- `src/version.h`
|
||||
|
||||
## What The Wallet Actually Does
|
||||
|
||||
When a fresh wallet bootstraps, it:
|
||||
|
||||
1. Downloads `http://bootstrap.cryptographic-triangles.org/bootstrap.tar.gz`
|
||||
2. Extracts it into the data directory
|
||||
3. Requires `blk0001.dat` to exist after extraction
|
||||
4. Looks for `txleveldb/` and `snapshot.manifest`
|
||||
5. Keeps `txleveldb/` only if `snapshot.manifest` passes verification
|
||||
6. Deletes `txleveldb/` if verification fails, then rebuilds from `blk0001.dat`
|
||||
7. Always deletes `database/` from the extracted snapshot
|
||||
|
||||
The verification rules are strict:
|
||||
|
||||
- `format` must be `1`
|
||||
- `network` must be `main` on mainnet
|
||||
- `dbversion` must be `70509`
|
||||
- `height` and `hash` must exactly match a hardcoded checkpoint
|
||||
|
||||
If any of those checks fail, the wallet throws away the shipped `txleveldb/`.
|
||||
|
||||
## Current Hardcoded Mainnet Checkpoint
|
||||
|
||||
As of the current codebase, the latest hardcoded mainnet checkpoint is:
|
||||
|
||||
- Height: `2186940`
|
||||
- Hash: `bd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0`
|
||||
|
||||
OpenClaw must not generate a manifest with an arbitrary tip hash. The manifest only survives if it matches a hardcoded checkpoint from `src/checkpoints.cpp`.
|
||||
|
||||
## Important Limitation
|
||||
|
||||
If the live chain tip is past the latest hardcoded checkpoint, OpenClaw has two valid options:
|
||||
|
||||
1. Publish a snapshot taken exactly at the latest hardcoded checkpoint
|
||||
2. Publish `blk0001.dat` only, without `txleveldb/`, and let clients rebuild locally
|
||||
|
||||
OpenClaw must not publish a `snapshot.manifest` for a height/hash that is not compiled into the wallet.
|
||||
|
||||
## Files OpenClaw Should Publish
|
||||
|
||||
The preferred `bootstrap.tar.gz` should contain:
|
||||
|
||||
- `blk0001.dat`
|
||||
- `txleveldb/`
|
||||
- `snapshot.manifest`
|
||||
- optionally `peers.dat`
|
||||
|
||||
It must not contain:
|
||||
|
||||
- `wallet.dat`
|
||||
- `database/`
|
||||
- `.lock`
|
||||
- pid files
|
||||
- logs
|
||||
- Tor state
|
||||
|
||||
Legacy fallback files should still exist on the web root:
|
||||
|
||||
- `blk0001.dat`
|
||||
- `filelist.txt`
|
||||
|
||||
## Requirements For The Source Node
|
||||
|
||||
Before building a snapshot, the source node should be:
|
||||
|
||||
- fully synced
|
||||
- cleanly shut down before copying files
|
||||
- built from the same code/version expected by clients
|
||||
- using the same LevelDB schema as the client (`DATABASE_VERSION=70509`)
|
||||
|
||||
Recommended node config for the source snapshot node:
|
||||
|
||||
```ini
|
||||
txindex=1
|
||||
addressindex=1
|
||||
daemon=1
|
||||
server=1
|
||||
```
|
||||
|
||||
`addressindex=1` is recommended so clients that enable address index can benefit from faster indexed wallet rescans and address RPCs immediately.
|
||||
|
||||
## OpenClaw Workflow
|
||||
|
||||
### Step 1: Decide Whether A Prebuilt Index Is Allowed
|
||||
|
||||
OpenClaw must first decide whether it can ship `txleveldb/`.
|
||||
|
||||
Rules:
|
||||
|
||||
- If the snapshot node is exactly at checkpoint `2186940`, shipping `txleveldb/` is allowed
|
||||
- If the snapshot node is above `2186940` and the code has not been updated with a newer checkpoint, do not ship `txleveldb/`
|
||||
- In that case, publish a blocks-only bootstrap instead
|
||||
|
||||
### Step 2: Stop The Source Node Cleanly
|
||||
|
||||
Never copy a live LevelDB directory.
|
||||
|
||||
```bash
|
||||
trianglesd stop
|
||||
sleep 10
|
||||
pgrep -af trianglesd || true
|
||||
```
|
||||
|
||||
OpenClaw should confirm the daemon is fully stopped before copying `txleveldb/`.
|
||||
|
||||
### Step 3: Create A Staging Directory
|
||||
|
||||
```bash
|
||||
rm -rf /tmp/triangles-bootstrap-stage
|
||||
mkdir -p /tmp/triangles-bootstrap-stage
|
||||
```
|
||||
|
||||
### Step 4: Copy Snapshot Files
|
||||
|
||||
For a verified snapshot:
|
||||
|
||||
```bash
|
||||
cp ~/.triangles/blk0001.dat /tmp/triangles-bootstrap-stage/
|
||||
cp -a ~/.triangles/txleveldb /tmp/triangles-bootstrap-stage/
|
||||
test -f ~/.triangles/peers.dat && cp ~/.triangles/peers.dat /tmp/triangles-bootstrap-stage/
|
||||
```
|
||||
|
||||
Do not copy:
|
||||
|
||||
```bash
|
||||
rm -rf /tmp/triangles-bootstrap-stage/database
|
||||
rm -f /tmp/triangles-bootstrap-stage/wallet.dat
|
||||
rm -f /tmp/triangles-bootstrap-stage/.lock
|
||||
rm -f /tmp/triangles-bootstrap-stage/*.pid
|
||||
rm -f /tmp/triangles-bootstrap-stage/debug.log
|
||||
```
|
||||
|
||||
### Step 5: Write `snapshot.manifest`
|
||||
|
||||
If OpenClaw is publishing a verified prebuilt index, write:
|
||||
|
||||
```bash
|
||||
cat > /tmp/triangles-bootstrap-stage/snapshot.manifest << 'EOF'
|
||||
format=1
|
||||
network=main
|
||||
height=2186940
|
||||
hash=bd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0
|
||||
dbversion=70509
|
||||
EOF
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- `hash` must not include `0x`
|
||||
- `network` must be `main`
|
||||
- `dbversion` must be `70509`
|
||||
- If OpenClaw is publishing blocks-only bootstrap, it should omit `snapshot.manifest` entirely
|
||||
|
||||
### Step 6: Build The Tarball
|
||||
|
||||
```bash
|
||||
cd /tmp/triangles-bootstrap-stage
|
||||
tar czf /tmp/bootstrap.tar.gz .
|
||||
```
|
||||
|
||||
### Step 7: Publish To The Existing Bootstrap Server
|
||||
|
||||
This guide assumes the existing nginx root is:
|
||||
|
||||
- `/var/www/triangles-bootstrap`
|
||||
|
||||
Publish the preferred tarball and the legacy fallback files:
|
||||
|
||||
```bash
|
||||
sudo mkdir -p /var/www/triangles-bootstrap
|
||||
sudo mv /tmp/bootstrap.tar.gz /var/www/triangles-bootstrap/bootstrap.tar.gz
|
||||
sudo cp ~/.triangles/blk0001.dat /var/www/triangles-bootstrap/blk0001.dat
|
||||
printf "blk0001.dat\n" | sudo tee /var/www/triangles-bootstrap/filelist.txt > /dev/null
|
||||
sudo chown -R www-data:www-data /var/www/triangles-bootstrap
|
||||
```
|
||||
|
||||
If OpenClaw is publishing a blocks-only bootstrap, the commands are the same except the tarball should contain only `blk0001.dat` and optional `peers.dat`.
|
||||
|
||||
## Validation Checklist
|
||||
|
||||
Before marking the update complete, OpenClaw should verify:
|
||||
|
||||
### Tarball contents
|
||||
|
||||
```bash
|
||||
tar tzf /var/www/triangles-bootstrap/bootstrap.tar.gz | sort
|
||||
```
|
||||
|
||||
Expected for verified snapshot:
|
||||
|
||||
- `./blk0001.dat`
|
||||
- `./txleveldb/...`
|
||||
- `./snapshot.manifest`
|
||||
|
||||
Expected not to exist:
|
||||
|
||||
- `wallet.dat`
|
||||
- `database/`
|
||||
|
||||
### HTTP responses
|
||||
|
||||
```bash
|
||||
curl -I http://localhost/bootstrap.tar.gz
|
||||
curl -I http://localhost/blk0001.dat
|
||||
curl http://localhost/filelist.txt
|
||||
```
|
||||
|
||||
Expected:
|
||||
|
||||
- HTTP `200`
|
||||
- `filelist.txt` contains `blk0001.dat`
|
||||
|
||||
### Manifest sanity
|
||||
|
||||
```bash
|
||||
tar xOf /var/www/triangles-bootstrap/bootstrap.tar.gz ./snapshot.manifest
|
||||
```
|
||||
|
||||
Expected:
|
||||
|
||||
- `format=1`
|
||||
- `network=main`
|
||||
- `height=2186940`
|
||||
- `hash=bd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0`
|
||||
- `dbversion=70509`
|
||||
|
||||
## Fresh-Client Test
|
||||
|
||||
OpenClaw should test the artifact on a clean machine or clean data directory:
|
||||
|
||||
```bash
|
||||
mv ~/.triangles ~/.triangles.backup.$(date +%s)
|
||||
mkdir -p ~/.triangles
|
||||
trianglesd -bootstrap
|
||||
```
|
||||
|
||||
Then inspect startup logs.
|
||||
|
||||
Successful verified snapshot behavior should include:
|
||||
|
||||
- snapshot downloaded
|
||||
- `snapshot.manifest found`
|
||||
- `manifest verified - keeping pre-built index`
|
||||
- no message about removing extracted `txleveldb/`
|
||||
|
||||
Failure behavior will include:
|
||||
|
||||
- manifest parse or verification failure
|
||||
- `removing extracted txleveldb/`
|
||||
- slow rebuild from `blk0001.dat`
|
||||
|
||||
## Safe Publish Procedure
|
||||
|
||||
OpenClaw should use this order:
|
||||
|
||||
1. Build snapshot in `/tmp`
|
||||
2. Validate tarball contents
|
||||
3. Replace `/var/www/triangles-bootstrap/bootstrap.tar.gz`
|
||||
4. Replace `/var/www/triangles-bootstrap/blk0001.dat`
|
||||
5. Replace `/var/www/triangles-bootstrap/filelist.txt`
|
||||
6. Confirm HTTP `200`
|
||||
|
||||
This avoids serving a half-written tarball.
|
||||
|
||||
## Example Bot Prompt
|
||||
|
||||
Use this exact tasking for OpenClaw:
|
||||
|
||||
```text
|
||||
Update the existing Triangles bootstrap server on bootstrap.cryptographic-triangles.org.
|
||||
|
||||
Rules:
|
||||
- Build the snapshot from a cleanly stopped source node
|
||||
- If the source node is exactly at hardcoded checkpoint 2186940 / bd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0, publish a verified snapshot containing blk0001.dat, txleveldb/, and snapshot.manifest
|
||||
- If the source node is above the latest hardcoded checkpoint, publish a blocks-only bootstrap and do not ship txleveldb/
|
||||
- Do not ship wallet.dat, database/, .lock, pid files, logs, or Tor state
|
||||
- Publish bootstrap.tar.gz, blk0001.dat, and filelist.txt to /var/www/triangles-bootstrap
|
||||
- Verify curl HTTP 200 for bootstrap.tar.gz and blk0001.dat
|
||||
- Report the tarball contents and whether the snapshot is verified or blocks-only
|
||||
```
|
||||
|
||||
## Recommended Next Improvement
|
||||
|
||||
This workflow will stay constrained until the next checkpoint is updated in `src/checkpoints.cpp`.
|
||||
|
||||
If you want OpenClaw to keep shipping prebuilt `txleveldb/` snapshots as the chain advances, the software needs periodic checkpoint updates. Without that, the verified snapshot path will stop at the latest compiled checkpoint and clients will fall back to rebuilds.
|
||||
@@ -1,123 +0,0 @@
|
||||
# TODO/FIXME Documentation
|
||||
|
||||
Detailed context for each TODO/FIXME in the codebase.
|
||||
|
||||
## Critical (Needs Attention)
|
||||
|
||||
### src/rpcmining.cpp:263 - Thread Safety Issue
|
||||
```cpp
|
||||
static mapNewBlock_t mapNewBlock; // FIXME: thread safety
|
||||
```
|
||||
**Issue:** Static variable accessed by multiple RPC threads without mutex protection.
|
||||
**Impact:** Potential race condition in getwork RPC (used for mining).
|
||||
**Status:** Low priority - PoW mining ended at block 9000, this code path rarely used.
|
||||
**Fix:** Add std::mutex and lock_guard if getwork usage increases.
|
||||
|
||||
### src/qt/walletmodel.cpp:249 - Collision Risk
|
||||
```cpp
|
||||
if((total + nFeeRequired) > nBalance) // FIXME: could cause collisions in the future
|
||||
```
|
||||
**Issue:** Balance check may have edge case causing transaction collisions.
|
||||
**Context:** In createTransaction fee calculation loop.
|
||||
**Status:** Needs investigation - unclear what "collisions" means here.
|
||||
**Fix:** Review Bitcoin Core's current implementation of this logic.
|
||||
|
||||
### src/smessage.cpp - File Size Limits
|
||||
```cpp
|
||||
// Lines 863, 2219, 2373: "TODO files must be split if > 2GB"
|
||||
```
|
||||
**Issue:** Secure message storage files not split when exceeding 2GB.
|
||||
**Impact:** May fail on 32-bit systems or with large message volumes.
|
||||
**Status:** Low priority - unlikely to reach 2GB in practice.
|
||||
**Fix:** Implement file rotation when approaching 2GB limit.
|
||||
|
||||
## Medium Priority (Encapsulation/API)
|
||||
|
||||
### src/protocol.h - Make Members Private
|
||||
```cpp
|
||||
// Lines 50, 100, 132: "TODO: make private (improves encapsulation)"
|
||||
```
|
||||
**Issue:** CAddress, CInv, CMessageHeader have public data members.
|
||||
**Impact:** Poor encapsulation, harder to maintain invariants.
|
||||
**Status:** Deferred - would require extensive refactoring.
|
||||
**Fix:** Add getter/setter methods, make members private, update all call sites.
|
||||
|
||||
### src/wallet.h:378 - nOrderPos Calculation
|
||||
```cpp
|
||||
nOrderPos = -1; // TODO: calculate elsewhere
|
||||
```
|
||||
**Issue:** Transaction ordering position calculated in constructor.
|
||||
**Impact:** Minor - works but not ideal separation of concerns.
|
||||
**Status:** Deferred - no functional issue.
|
||||
**Fix:** Move calculation to WalletDB when transaction is added.
|
||||
|
||||
### src/rpcwallet.cpp / src/qt/askpassphrasedialog.cpp - SecureString Conversion
|
||||
**Issue:** Password-handling paths were converting through `.c_str()` because `SecureString`
|
||||
did not have a convenient conversion helper from `std::string`.
|
||||
**Impact:** Unnecessary C-string shims in sensitive code paths.
|
||||
**Status:** Resolved.
|
||||
**Fix:** Added `MakeSecureString(const std::string&)` in `src/allocators.h` and updated
|
||||
the wallet RPC and passphrase dialog call sites to use it directly.
|
||||
|
||||
## Low Priority (Nice-to-Have)
|
||||
|
||||
### src/util.cpp:1322 - Disabled Feature
|
||||
```cpp
|
||||
// TODO: This is currently disabled because it needs to be verified to work
|
||||
```
|
||||
**Context:** File descriptor management code.
|
||||
**Status:** Intentionally disabled pending verification.
|
||||
**Fix:** Test thoroughly, then enable if needed.
|
||||
|
||||
### src/tor/tor_embedded.cpp:209 - Tor Shutdown API
|
||||
```cpp
|
||||
// TODO: Tor 0.4.9+ may add tor_api_shutdown(), use it when available
|
||||
```
|
||||
**Context:** Embedded Tor cleanup.
|
||||
**Status:** Waiting for upstream Tor API.
|
||||
**Fix:** Check Tor 0.4.9+ releases for new API, integrate when stable.
|
||||
|
||||
### src/init.cpp:442 - Sanity Checks
|
||||
```cpp
|
||||
// TODO: remaining sanity checks, see #4081
|
||||
```
|
||||
**Context:** Bitcoin Core issue #4081 - additional startup sanity checks.
|
||||
**Status:** Deferred - core checks already in place.
|
||||
**Fix:** Review Bitcoin Core's current sanity check implementation.
|
||||
|
||||
### src/rpcmining.cpp:232 - DRM Comment
|
||||
```cpp
|
||||
CDataStream(coinbase, SER_NETWORK, PROTOCOL_VERSION) >> pblock->vtx[0]; // FIXME - DRM!
|
||||
```
|
||||
**Issue:** Unclear what "DRM" means here - likely "Data Race Maybe"?
|
||||
**Status:** Needs clarification from original author.
|
||||
**Fix:** Investigate if there's an actual issue, otherwise remove comment.
|
||||
|
||||
## Deferred (External/Low Impact)
|
||||
|
||||
### LevelDB TODOs (src/leveldb/*)
|
||||
**Status:** Upstream LevelDB issues - don't modify embedded library.
|
||||
**Action:** None - track upstream LevelDB project.
|
||||
|
||||
### Qt TODOs (src/qt/*)
|
||||
**Status:** UI improvements, not critical.
|
||||
**Action:** Track as nice-to-have enhancements.
|
||||
|
||||
### Secure Message TODOs (src/smessage.cpp)
|
||||
Multiple minor improvements suggested:
|
||||
- Include hash in certain operations
|
||||
- Improve thread shutdown
|
||||
- Set default recv/recvAnon behavior
|
||||
- Update outbox after PoW completes
|
||||
|
||||
**Status:** Non-critical enhancements.
|
||||
**Action:** Consider for future encrypted messaging upgrades.
|
||||
|
||||
## Summary
|
||||
|
||||
**Critical:** 3 items (thread safety, balance collision, file limits)
|
||||
**Medium:** 6 items (encapsulation, SecureString)
|
||||
**Low:** 5 items (disabled features, upstream APIs)
|
||||
**Deferred:** ~24 items (external libs, minor enhancements)
|
||||
|
||||
**Recommendation:** Focus on documenting critical items in code comments, defer fixes until specific issues arise.
|
||||
@@ -0,0 +1,281 @@
|
||||
# Triangles (TRI) RPC Command Reference
|
||||
|
||||
This document describes every RPC command available in the Triangles daemon (`trianglesd`) and Qt wallet. Connect via JSON-RPC on port **19112** (default). All commands can also be run from the Qt wallet's debug console.
|
||||
|
||||
Triangles is a Tor-only PoS cryptocurrency. PoW ended at block 9000; from block 9001 onward the chain is pure Proof-of-Stake with 33% annual interest (coin-age based). Block time is 2 minutes. Max supply is 2,222,222 TRI.
|
||||
|
||||
---
|
||||
|
||||
## Server Control
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `help` | `[command]` | List all commands, or get detailed help for a specific command. |
|
||||
| `stop` | | Shut down the daemon. |
|
||||
|
||||
---
|
||||
|
||||
## Blockchain
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getbestblockhash` | | Returns the hash of the tip of the best chain. |
|
||||
| `getblockcount` | | Returns the current block height. |
|
||||
| `getblockhash` | `<index>` | Returns the block hash at the given height. |
|
||||
| `getblock` | `<hash> [txinfo]` | Returns block details for the given hash. Set `txinfo=true` for full transaction data. |
|
||||
| `getblockbynumber` | `<number> [txinfo]` | Same as `getblock` but accepts a height instead of a hash. |
|
||||
| `getblockheader` | `<hash> [verbose=true]` | Returns block header data. If verbose is false, returns hex-encoded header. |
|
||||
| `getblockchaininfo` | | Returns chain state info: chain name, block height, best hash, difficulty, etc. |
|
||||
| `getdifficulty` | | Returns current PoW and PoS difficulty values. |
|
||||
| `gettxoutsetinfo` | | Returns statistics about the UTXO set (total txouts, size, etc.). |
|
||||
| `getrawmempool` | | Returns all transaction IDs currently in the mempool. |
|
||||
| `getcheckpoint` | | Returns info about the current synchronized checkpoint. |
|
||||
| `getchaintips` | | Returns info about all known chain tips (forks). |
|
||||
| `invalidateblock` | `<hash>` | Permanently marks a block as invalid and rewinds the chain past it. |
|
||||
| `reconsiderblock` | `<hash>` | Removes the invalid mark from a previously invalidated block. |
|
||||
| `recalculatesupply` | | Recalculates money supply by summing all UTXOs. Updates the stored value at the chain tip and persists to disk. Returns old/new supply and difference. |
|
||||
| `settxfee` | `<amount>` | Sets the transaction fee per kB. Amount is rounded to nearest 0.01. |
|
||||
| `estimatefee` | `<nblocks>` | Estimates the fee per kB needed for confirmation within `nblocks` blocks. |
|
||||
|
||||
---
|
||||
|
||||
## Address Index
|
||||
|
||||
These commands query the address index. The daemon must be running with `-addressindex=1`.
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getaddressbalance` | `{"addresses":["addr",...]}` | Returns confirmed balance for the given address(es). |
|
||||
| `getaddressutxos` | `{"addresses":["addr",...]}` | Returns all unspent outputs for the given address(es). |
|
||||
| `getaddresstxids` | `{"addresses":["addr",...], "start":n, "end":n}` | Returns transaction IDs for the given address(es), optionally filtered by block range. |
|
||||
|
||||
---
|
||||
|
||||
## Mining & Staking
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getmininginfo` | | Returns mining-related info: height, difficulty, network hashrate, etc. |
|
||||
| `getstakinginfo` | | Returns staking-related info: whether staking is active, weight, expected time to stake, etc. |
|
||||
| `getsubsidy` | `[nTarget]` | Returns the PoW subsidy value for the given target height (historical reference only since PoW ended at block 9000). |
|
||||
|
||||
---
|
||||
|
||||
## Network
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getconnectioncount` | | Returns the number of peer connections. |
|
||||
| `getpeerinfo` | | Returns detailed info about each connected peer (address, version, ping time, etc.). |
|
||||
| `getnetworkinfo` | | Returns P2P network state: version, protocol, peer mix, connections, relay fee, etc. |
|
||||
| `getseedlist` | | Returns the list of configured seed nodes. |
|
||||
| `addnode` | `<node> <add\|remove\|onetry>` | Add or remove a node from the manual peer list, or try connecting once. For Tor nodes use the `.onion` address. |
|
||||
| `disconnectnode` | `<node>` | Immediately disconnects from the specified peer. |
|
||||
| `sendalert` | `<message> <privatekey> <minver> <maxver> <priority> <id> [cancelupto]` | Broadcasts a network alert (requires the alert master private key). |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — General
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getinfo` | | Returns general info: version, balance, stake, block height, connections, etc. |
|
||||
| `getwalletinfo` | | Returns wallet-specific info: balance, unconfirmed, immature, txcount, keypoolsize, etc. |
|
||||
| `getbalance` | `[account] [minconf=1]` | Returns total available balance (optionally for a specific account). |
|
||||
| `checkwallet` | | Checks wallet database for consistency errors. |
|
||||
| `repairwallet` | | Attempts to repair the wallet database. |
|
||||
| `resendtx` | | Re-broadcasts all unconfirmed wallet transactions. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Addresses & Accounts
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `getnewaddress` | `[account]` | Generates a new receiving address (optionally assigned to an account). |
|
||||
| `getnewpubkey` | `[account]` | Returns a new public key for the wallet. |
|
||||
| `getaccountaddress` | `<account>` | Returns the current receiving address for the given account. |
|
||||
| `setaccount` | `<address> <account>` | Assigns an address to the given account label. |
|
||||
| `getaccount` | `<address>` | Returns the account label for the given address. |
|
||||
| `getaddressesbyaccount` | `<account>` | Returns all addresses assigned to the given account. |
|
||||
| `listaddressgroupings` | | Returns addresses grouped by common ownership (based on transaction history). |
|
||||
| `validateaddress` | `<address>` | Validates a Triangles address and returns info (ismine, account, pubkey, etc.). |
|
||||
| `validatepubkey` | `<pubkey>` | Validates a Triangles public key. |
|
||||
| `listaccounts` | `[minconf=1]` | Returns all account names and their balances. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Sending
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `sendtoaddress` | `<address> <amount> [comment] [comment-to]` | Sends TRI to an address. Returns the transaction ID. |
|
||||
| `sendfrom` | `<fromaccount> <address> <amount> [minconf=1] [comment] [comment-to]` | Sends TRI from a specific account. |
|
||||
| `sendmany` | `<fromaccount> {"addr":amount,...} [minconf=1] [comment]` | Sends TRI to multiple addresses in a single transaction. |
|
||||
| `move` | `<fromaccount> <toaccount> <amount> [minconf=1] [comment]` | Moves funds between accounts (internal bookkeeping only, no on-chain tx). |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Transaction History
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `listtransactions` | `[account] [count=10] [from=0]` | Returns the most recent transactions (optionally filtered by account). |
|
||||
| `listsinceblock` | `[blockhash] [target-confirmations]` | Returns all transactions since the given block. |
|
||||
| `gettransaction` | `<txid>` | Returns detailed info about a wallet transaction. |
|
||||
| `getreceivedbyaddress` | `<address> [minconf=1]` | Returns total amount received by an address. |
|
||||
| `getreceivedbyaccount` | `<account> [minconf=1]` | Returns total amount received by an account. |
|
||||
| `listreceivedbyaddress` | `[minconf=1] [includeempty=false]` | Returns amounts received for each address. |
|
||||
| `listreceivedbyaccount` | `[minconf=1] [includeempty=false]` | Returns amounts received for each account. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Staking Control
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `reservebalance` | `[reserve] [amount]` | Show or set a reserve balance that will not be used for staking. `reserve` is true/false, `amount` is the TRI to reserve. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Security
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `encryptwallet` | `<passphrase>` | Encrypts the wallet with the given passphrase. **This shuts down the daemon.** The wallet must be re-started and unlocked afterward. |
|
||||
| `walletpassphrase` | `<passphrase> <timeout> [stakingonly]` | Unlocks the wallet for `timeout` seconds. Set `stakingonly=true` to allow staking but prevent sending. |
|
||||
| `walletpassphrasechange` | `<oldpassphrase> <newpassphrase>` | Changes the wallet encryption passphrase. |
|
||||
| `walletlock` | | Immediately locks the wallet (removes decryption key from memory). |
|
||||
| `keypoolrefill` | `[new-size]` | Tops up the pre-generated key pool. |
|
||||
| `makekeypair` | `[prefix]` | Generates a new public/private keypair (not added to wallet). |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Backup & Import
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `backupwallet` | `<destination>` | Copies `wallet.dat` to the given file path. |
|
||||
| `dumpwallet` | `<filename>` | Exports all wallet private keys to a plaintext file. |
|
||||
| `dumpprivkey` | `<address>` | Returns the private key (WIF format) for the given address. |
|
||||
| `importwallet` | `<filename>` | Imports keys from a wallet dump file. |
|
||||
| `importprivkey` | `<privkey> [label]` | Imports a single private key (WIF format) with optional label. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Multisig
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `addmultisigaddress` | `<nrequired> ["key",...] [account]` | Creates an M-of-N multisig address. `nrequired` is the number of signatures needed. |
|
||||
| `addredeemscript` | `<redeemScript> [account]` | Adds a P2SH redeem script to the wallet. |
|
||||
|
||||
---
|
||||
|
||||
## Wallet — Message Signing
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `signmessage` | `<address> <message>` | Signs a message with the private key of the given address. |
|
||||
| `verifymessage` | `<address> <signature> <message>` | Verifies a signed message. Returns true/false. |
|
||||
|
||||
---
|
||||
|
||||
## Raw Transactions
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `listunspent` | `[minconf=1] [maxconf=9999999] ["addr",...]` | Returns unspent transaction outputs, optionally filtered by address and confirmation count. |
|
||||
| `createrawtransaction` | `[{"txid":"id","vout":n},...] {"addr":amount,...}` | Creates an unsigned raw transaction from the given inputs and outputs. |
|
||||
| `decoderawtransaction` | `<hex>` | Decodes a raw transaction hex string into a JSON object. |
|
||||
| `decodescript` | `<hex>` | Decodes a hex-encoded script into human-readable form. |
|
||||
| `signrawtransaction` | `<hex> [prevtxs] [privkeys] [sighashtype="ALL"]` | Signs a raw transaction. Can provide previous tx outputs and private keys for offline signing. |
|
||||
| `sendrawtransaction` | `<hex>` | Broadcasts a signed raw transaction to the network. Returns the txid. |
|
||||
| `getrawtransaction` | `<txid> [verbose=0]` | Returns raw transaction data. Set verbose=1 for decoded JSON output. |
|
||||
|
||||
---
|
||||
|
||||
## Secure Messaging (SMSG)
|
||||
|
||||
Triangles has a built-in encrypted peer-to-peer messaging system. Messages are stored in a DHT-like bucket system and relayed through the network.
|
||||
|
||||
| Command | Parameters | Description |
|
||||
|---------|-----------|-------------|
|
||||
| `smsgenable` | | Enables the secure messaging system. |
|
||||
| `smsgdisable` | | Disables the secure messaging system. |
|
||||
| `smsgoptions` | `[list\|set <optname> <value>]` | View or change secure messaging options. |
|
||||
| `smsglocalkeys` | `[whitelist\|all\|wallet\|recv +/- <addr>\|anon +/- <addr>]` | Manage which local keys participate in secure messaging. |
|
||||
| `smsgaddkey` | `<address> <pubkey>` | Adds someone's public key so you can send them encrypted messages. |
|
||||
| `smsggetpubkey` | `<address>` | Retrieves the public key for an address (needed to send messages to it). |
|
||||
| `smsgsend` | `<fromAddr> <toAddr> <message>` | Sends an encrypted message from one of your addresses to a recipient. |
|
||||
| `smsgsendanon` | `<toAddr> <message>` | Sends an anonymous encrypted message (no sender address attached). |
|
||||
| `smsginbox` | `[all\|unread\|clear]` | View received secure messages. Default shows unread. |
|
||||
| `smsgoutbox` | `[all\|clear]` | View sent secure messages. |
|
||||
| `smsgscanchain` | | Scans the blockchain for secure message public keys. |
|
||||
| `smsgscanbuckets` | | Scans stored message buckets for messages addressed to your keys. |
|
||||
| `smsgbuckets` | `[stats\|dump]` | View secure message bucket statistics or dump contents. |
|
||||
| `smsgbroadcast` | `<fromAddr> <message>` | Broadcasts a message to all SMSG participants (not encrypted to a single recipient). |
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference — Common Tasks
|
||||
|
||||
**Check node status:**
|
||||
```
|
||||
getinfo
|
||||
getblockcount
|
||||
getconnectioncount
|
||||
getstakinginfo
|
||||
```
|
||||
|
||||
**Check balance and transactions:**
|
||||
```
|
||||
getbalance
|
||||
listtransactions
|
||||
```
|
||||
|
||||
**Send coins:**
|
||||
```
|
||||
walletpassphrase "yourpassphrase" 60
|
||||
sendtoaddress "TRIaddress" 100
|
||||
walletlock
|
||||
```
|
||||
|
||||
**Unlock for staking only:**
|
||||
```
|
||||
walletpassphrase "yourpassphrase" 999999999 true
|
||||
```
|
||||
|
||||
**Add a peer manually (Tor .onion):**
|
||||
```
|
||||
addnode "abcdef1234567890.onion" "add"
|
||||
```
|
||||
|
||||
**Export/import a private key:**
|
||||
```
|
||||
dumpprivkey "TRIaddress"
|
||||
importprivkey "5KPrivKeyHere" "mylabel"
|
||||
```
|
||||
|
||||
**Fix incorrect money supply display:**
|
||||
```
|
||||
recalculatesupply
|
||||
```
|
||||
|
||||
**Full reindex (rebuild block index from raw data):**
|
||||
```
|
||||
trianglesd -reindex
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Connection Info
|
||||
|
||||
| Setting | Value |
|
||||
|---------|-------|
|
||||
| Default RPC port | 19112 |
|
||||
| Default P2P port | 24112 |
|
||||
| Config file (Windows) | `%APPDATA%\triangles\triangles.conf` |
|
||||
| Config file (Linux) | `~/.triangles/triangles.conf` |
|
||||
| Protocol version | 70205 |
|
||||
| Network | Tor-only |
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
# TRI v6 Development Task Queue
|
||||
|
||||
*Autonomous development pipeline — Krystie cycles through these continuously.*
|
||||
|
||||
## Legend
|
||||
- **P0** = Critical (chain broken / users blocked)
|
||||
- **P1** = Important (v6 milestone)
|
||||
- **P2** = Nice-to-have (polish / optimization)
|
||||
- **Status**: TODO | IN-PROGRESS | DONE | BLOCKED
|
||||
|
||||
---
|
||||
|
||||
## P0 — Immediate (Unblock Chain & Users)
|
||||
|
||||
### T001: Fix DNS2 RPC thread crash
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: ThreadRPCServer exits on bad auth attempts from external IPs. Need to not kill the RPC thread on individual auth failures.
|
||||
- **Files**: `src/rpc.cpp` or `src/bitcoinrpc.cpp`
|
||||
- **Acceptance**: RPC stays up even with bad auth attempts; curl JSON-RPC works reliably
|
||||
- **Model**: Claude Code or MiniMax M2.7
|
||||
|
||||
### T002: Fix DNS2 wallet 0 confirmed balance
|
||||
- **Status**: TODO
|
||||
- **Depends**: T001 (need reliable RPC)
|
||||
- **Description**: Wallet restored from April 20 backup. Shows 11.24 TRI unconfirmed. Need to verify rescan completes and coins mature (520 confirmations) for staking.
|
||||
- **Files**: wallet.dat, `src/wallet.cpp`
|
||||
- **Acceptance**: Wallet shows confirmed balance after rescan + confirmations
|
||||
- **Model**: Krystie (manual investigation, not subagent)
|
||||
|
||||
### T003: Fix seeds.txt parsing (only returns 1 address)
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: HTTPS fetch of seeds.cryptographic-triangles.org/seeds.txt only returns 1 address. Possible comment parsing bug in net.cpp seed fetch logic.
|
||||
- **Files**: `src/net.cpp`, `/var/www/seeds/seeds.txt`
|
||||
- **Acceptance**: All 7 onion addresses returned on fetch
|
||||
- **Model**: ZAI GLM-5.1
|
||||
|
||||
### T004: Fix Sami's PC wallet block 570 stall
|
||||
- **Status**: IN-PROGRESS
|
||||
- **Depends**: Windows binary build (DONE — built on sami-pc)
|
||||
- **Description**: Windows Qt wallet stuck at block 570. GUI bootstrap fix committed (d0fb2dc). New binary built at E:\repos\triangles_v5\build-mingw\bin\triangles-qt.exe. Needs testing.
|
||||
- **Acceptance**: Windows wallet syncs past block 570 with bootstrap
|
||||
- **Model**: Krystie (manual deployment)
|
||||
|
||||
---
|
||||
|
||||
## P1 — v6 Core Milestones
|
||||
|
||||
### T010: Complete RocksDB runtime testing
|
||||
- **Status**: TODO
|
||||
- **Depends**: T001
|
||||
- **Description**: RocksDB backend compiles clean but never tested with actual blockchain data. Need to: start daemon with `-rocksdb`, let it index chain, verify block lookups work, compare performance vs LevelDB.
|
||||
- **Files**: `src/txdb.h`, `src/txdb.cpp`, `src/utxosnapshot.cpp`
|
||||
- **Acceptance**: Daemon runs with `-rocksdb` flag, processes blocks, RPC queries return correct data
|
||||
- **Model**: MiniMax M2.7
|
||||
|
||||
### T011: Wire UTXO snapshot P2P distribution (SnapshotNet)
|
||||
- **Status**: TODO
|
||||
- **Depends**: T010
|
||||
- **Description**: `snapshotnet.cpp` exists but is placeholder. Need to implement: peer advertisement of snapshot availability, chunk transfer protocol, hash verification, integration with bootstrap flow.
|
||||
- **Files**: `src/snapshotnet.cpp`, `src/net.cpp`, `src/utxosnapshot.cpp`
|
||||
- **Acceptance**: New node can get UTXO snapshot from peers via P2P (not just HTTPS)
|
||||
- **Model**: Claude Code + MiniMax M2.7 (architecture + implementation)
|
||||
|
||||
### T012: Implement automated checkpoint generation (DESIGN DONE)
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: Checkpoints exist through block 2,207,000 but are manually maintained. Need automated checkpoint generation: every N blocks, compute checkpoint hash, push to code or external manifest.
|
||||
- **Files**: `src/checkpoints.cpp`, `src/checkpoints.h`
|
||||
- **Acceptance**: New checkpoints generated automatically, committed or published
|
||||
- **Model**: Claude Code
|
||||
|
||||
### T013: GPG signing for bootstrap artifacts
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: GPG key created (6913E13610F698183429CE20C2DC60618C85A159). Need to: sign every bootstrap/snapshot artifact on generation, verify signature on download, publish public key.
|
||||
- **Files**: `/usr/local/bin/auto-update.sh`, `src/bootstrap.cpp`
|
||||
- **Acceptance**: `gpg --verify` works on downloaded artifacts
|
||||
- **Model**: ZAI GLM-5.1
|
||||
|
||||
### T014: Contabo seed Docker image hardening
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: Seeds are running but image is fragile. Need: proper Dockerfile with version pinning, health checks, auto-restart, log shipping, and persistent volumes.
|
||||
- **Files**: `/tmp/Dockerfile` on Contabo, `/tri/seed-{1..4}/`
|
||||
- **Acceptance**: Seeds survive host reboot, auto-restart on crash, health check endpoint
|
||||
- **Model**: ZAI GLM-5.1
|
||||
|
||||
### T015: Network health dashboard
|
||||
- **Status**: TODO
|
||||
- **Depends**: T001, T003
|
||||
- **Description**: Operator-facing dashboard showing: block height per node, peer count, staking weight, chain sync status, seed health. Could be a simple web page served from DNS2.
|
||||
- **Files**: New — `src/rpcblockchain.cpp` (health endpoint), frontend
|
||||
- **Acceptance**: Live page showing all 7 nodes' status updated every 30s
|
||||
- **Model**: MiniMax M2.7 (design) + Claude Code (implementation)
|
||||
|
||||
### T016: Hetzner ARM64 persistent setup
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: Hetzner node is running but manually configured. Need: systemd service, auto-start on boot, bootstrap automation, monitoring.
|
||||
- **Files**: systemd unit file on Hetzner
|
||||
- **Acceptance**: Node survives reboot, auto-syncs, reports health
|
||||
- **Model**: Krystie (manual, it's infra not code)
|
||||
|
||||
---
|
||||
|
||||
## P2 — Polish & Optimization
|
||||
|
||||
### T020: Remove unused Gemini/Google references from codebase
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: Clean up any dead code, unused imports, stale comments referencing old architectures.
|
||||
- **Model**: ZAI GLM-5.1
|
||||
|
||||
### T021: Comprehensive test suite
|
||||
- **Status**: TODO
|
||||
- **Depends**: T010
|
||||
- **Description**: Expand test coverage for: UTXO snapshot load/dump, RocksDB backend, bootstrap download, seed fetch, checkpoint verification.
|
||||
- **Files**: `src/test/`
|
||||
- **Acceptance**: `test_triangles` passes with < 5 pre-existing failures
|
||||
- **Model**: ZAI GLM-5.1 + MiniMax M2.7
|
||||
|
||||
### T022: CI/CD pipeline for releases
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: GitHub Actions workflow: on tag push, build Linux x86_64 + ARM64 + Windows, create release with all binaries + checksums.
|
||||
- **Files**: `.github/workflows/build-all.yml`
|
||||
- **Acceptance**: Tag push produces release with 3 platform binaries
|
||||
- **Model**: ZAI GLM-5.1
|
||||
|
||||
### T023: TRIdock + tri-wallet-web consolidation
|
||||
- **Status**: TODO
|
||||
- **Depends**: none
|
||||
- **Description**: TRIdock and tri-wallet-web appear to be near-duplicates. Evaluate and either consolidate or clearly separate concerns.
|
||||
- **Model**: MiniMax M2.7 (analysis)
|
||||
|
||||
---
|
||||
|
||||
## Completed
|
||||
|
||||
### ✅ Windows GUI bootstrap fix (d0fb2dc)
|
||||
- Removed `#ifndef QT_GUI` guard so auto-bootstrap runs in GUI wallet
|
||||
- Added `uiInterface.InitMessage()` for progress display
|
||||
|
||||
### ✅ Windows native build on sami-pc
|
||||
- Built `triangles-qt.exe` (26MB) and `trianglesd.exe` via MSYS2/MinGW64
|
||||
- All dependencies found natively
|
||||
|
||||
### ✅ RocksDB integration complete (ac9c6fb)
|
||||
- CActiveTxDB wrapper, dual-backend support, compiles clean
|
||||
|
||||
### ✅ All nodes updated to v5.9.7.0
|
||||
- DNS2, DNS3, Hetzner, Contabo seeds all running latest
|
||||
|
||||
### ✅ Bootstrap infrastructure live
|
||||
- HTTPS at bootstrap.cryptographic-triangles.org
|
||||
- Tor hidden service serving nginx on port 8085
|
||||
- Seeds.txt with 7 onion nodes
|
||||
@@ -9,6 +9,20 @@ if(NOT TARGET leveldb_lib)
|
||||
add_subdirectory("${LEVELDB_SOURCE_DIR}" "${LEVELDB_BINARY_DIR}")
|
||||
endif()
|
||||
|
||||
# Pin bundled LevelDB to C++17. It only needs C++11 (declared via its own
|
||||
# target_compile_features) but inherits CMAKE_CXX_STANDARD=20 from the
|
||||
# top-level project, where some of its atomic-enum syntax
|
||||
# (std::memory_order::memory_order_relaxed) becomes a hard error.
|
||||
foreach(_leveldb_target leveldb_lib leveldb_memenv)
|
||||
if(TARGET ${_leveldb_target})
|
||||
set_target_properties(${_leveldb_target} PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
CXX_EXTENSIONS OFF
|
||||
)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(NOT TARGET build_leveldb)
|
||||
add_custom_target(build_leveldb DEPENDS leveldb_lib leveldb_memenv)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
# Chain DB benchmark harness
|
||||
|
||||
Measures `FastImportBlockFile()` speed under each chain-DB backend
|
||||
(LevelDB vs RocksDB) using a user-supplied `blk0001.dat` block stream.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- A `trianglesd` binary (RocksDB is now a hard build dep, both backends are
|
||||
always available):
|
||||
```
|
||||
cmake -B build -G Ninja \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DBUILD_QT=OFF \
|
||||
-DBUILD_DAEMON=ON
|
||||
cmake --build build
|
||||
```
|
||||
- An `blk0001.dat` file (old-style block stream). If you have a synced
|
||||
node, copy `~/.triangles/blk0001.dat` (Linux) or `%APPDATA%\triangles\blk0001.dat` (Windows).
|
||||
- Free disk space: ~3× the size of `blk0001.dat` per backend run
|
||||
(raw blocks + chain DB index + working space).
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
contrib/bench/bench-chaindb.sh \
|
||||
--binary=$(pwd)/build/bin/trianglesd \
|
||||
--bootstrap=/path/to/blk0001.dat
|
||||
```
|
||||
|
||||
Runs each backend in turn, appends a CSV row to `./bench-results.csv`,
|
||||
and prints a summary to stdout. Default `--dbcache=2048` (MB).
|
||||
|
||||
### Options
|
||||
|
||||
| Flag | Default | Notes |
|
||||
| --- | --- | --- |
|
||||
| `--binary=PATH` | (required) | Path to `trianglesd` |
|
||||
| `--bootstrap=PATH` | (required) | Path to `blk0001.dat` |
|
||||
| `--backends=LIST` | `leveldb,rocksdb` | Comma-separated subset |
|
||||
| `--workdir=DIR` | `/tmp/triangles-bench-XXXXXX` | Per-backend datadirs go here |
|
||||
| `--dbcache=MB` | `2048` | Chain DB cache size |
|
||||
| `--results-csv=FILE` | `./bench-results.csv` | Appended to |
|
||||
| `--keep-datadirs` | off | Preserve datadirs after run for inspection |
|
||||
| `--rpc-port=BASE` | `19112` | Each backend uses `BASE+offset` |
|
||||
|
||||
## What it measures
|
||||
|
||||
| Column | Source |
|
||||
| --- | --- |
|
||||
| `wall_ms` | The daemon's own log line: `FastImportBlockFile: indexed N blocks in Mms` |
|
||||
| `peak_rss_kb` | `ps -o rss=` sampled once per second |
|
||||
| `datadir_bytes` | `du -sb` of the working datadir (includes `blk0001.dat`) |
|
||||
| `blocks_indexed` | Parsed from the same log line |
|
||||
|
||||
## What it does not measure
|
||||
|
||||
- Network IBD (peer fetch, header sync) — this is pure DB ingest.
|
||||
- UTXO snapshot load — `LoadSnapshot` is currently rocksdb-guarded
|
||||
(see `src/utxosnapshot.cpp`); will be unblocked when LevelDB is retired.
|
||||
- Reorg cost — separate test, not yet implemented.
|
||||
- Disk I/O bytes (read/written) — could be added with `iostat` integration.
|
||||
|
||||
## Interpreting results
|
||||
|
||||
A meaningful comparison requires both rows to have run on the same machine
|
||||
with the same `blk0001.dat`. The `host` column makes mixing runs across
|
||||
machines visible in the CSV.
|
||||
|
||||
Backend-relevant size comparisons should subtract `bootstrap_size_bytes`
|
||||
from `datadir_bytes` to isolate the chain DB tree.
|
||||
|
||||
## One-liners
|
||||
|
||||
```bash
|
||||
# LevelDB only
|
||||
./bench-chaindb.sh --binary=... --bootstrap=... --backends=leveldb
|
||||
|
||||
# Compare 2GB vs 4GB cache on RocksDB
|
||||
./bench-chaindb.sh --binary=... --bootstrap=... --backends=rocksdb --dbcache=2048
|
||||
./bench-chaindb.sh --binary=... --bootstrap=... --backends=rocksdb --dbcache=4096
|
||||
|
||||
# Keep the datadirs for poking around afterwards
|
||||
./bench-chaindb.sh --binary=... --bootstrap=... --keep-datadirs
|
||||
```
|
||||
@@ -0,0 +1,217 @@
|
||||
#!/usr/bin/env bash
|
||||
# Benchmark FastImportBlockFile() speed across chain-DB backends.
|
||||
#
|
||||
# Reads a user-supplied blk0001.dat (old-style block stream) and times the
|
||||
# full block-index rebuild under each backend. Output: a CSV row per backend
|
||||
# with wall time, peak RSS, and resulting datadir size on disk.
|
||||
#
|
||||
# Usage:
|
||||
# ./bench-chaindb.sh \
|
||||
# --binary=/path/to/trianglesd \
|
||||
# --bootstrap=/path/to/blk0001.dat \
|
||||
# [--backends=leveldb,rocksdb] default: both
|
||||
# [--workdir=/tmp/triangles-bench] parent dir for per-backend datadirs
|
||||
# [--dbcache=2048] in MB
|
||||
# [--results-csv=./bench-results.csv]
|
||||
# [--keep-datadirs] preserve datadirs after run
|
||||
# [--rpc-port=BASE] default 19112; each run uses BASE+offset
|
||||
#
|
||||
# Notes:
|
||||
# - RocksDB is a hard build dep, so any current trianglesd has both backends.
|
||||
# - This script does not assume Tor is configured. It launches with -nolisten
|
||||
# and -connect=0 to keep the run network-isolated.
|
||||
# - Wall time comes from the daemon's own perf log line:
|
||||
# "FastImportBlockFile: indexed N blocks in Mms"
|
||||
# - Peak RSS is sampled via `ps -o rss=` once a second.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# ── Defaults ────────────────────────────────────────────────────────────────
|
||||
BINARY=""
|
||||
BOOTSTRAP=""
|
||||
BACKENDS="leveldb,rocksdb"
|
||||
WORKDIR=""
|
||||
DBCACHE=2048
|
||||
RESULTS_CSV="./bench-results.csv"
|
||||
KEEP=0
|
||||
RPC_BASE=19112
|
||||
|
||||
# ── Arg parsing ─────────────────────────────────────────────────────────────
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--binary=*) BINARY="${arg#*=}" ;;
|
||||
--bootstrap=*) BOOTSTRAP="${arg#*=}" ;;
|
||||
--backends=*) BACKENDS="${arg#*=}" ;;
|
||||
--workdir=*) WORKDIR="${arg#*=}" ;;
|
||||
--dbcache=*) DBCACHE="${arg#*=}" ;;
|
||||
--results-csv=*) RESULTS_CSV="${arg#*=}" ;;
|
||||
--keep-datadirs) KEEP=1 ;;
|
||||
--rpc-port=*) RPC_BASE="${arg#*=}" ;;
|
||||
-h|--help)
|
||||
sed -n '2,28p' "$0" | sed 's/^# \?//'
|
||||
exit 0 ;;
|
||||
*)
|
||||
echo "Unknown argument: $arg" >&2
|
||||
exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -n "$BINARY" ] || { echo "--binary is required" >&2; exit 2; }
|
||||
[ -n "$BOOTSTRAP" ] || { echo "--bootstrap is required" >&2; exit 2; }
|
||||
[ -x "$BINARY" ] || { echo "Binary not executable: $BINARY" >&2; exit 2; }
|
||||
[ -f "$BOOTSTRAP" ] || { echo "Bootstrap file not found: $BOOTSTRAP" >&2; exit 2; }
|
||||
|
||||
if [ -z "$WORKDIR" ]; then
|
||||
WORKDIR="$(mktemp -d -t triangles-bench-XXXXXX)"
|
||||
fi
|
||||
mkdir -p "$WORKDIR"
|
||||
echo "Workdir: $WORKDIR"
|
||||
|
||||
# ── CSV header (only if file is new) ───────────────────────────────────────
|
||||
if [ ! -f "$RESULTS_CSV" ]; then
|
||||
echo "timestamp,backend,bootstrap_size_bytes,dbcache_mb,blocks_indexed,wall_ms,peak_rss_kb,datadir_bytes,binary,host" > "$RESULTS_CSV"
|
||||
fi
|
||||
|
||||
bootstrap_size="$(stat -c%s "$BOOTSTRAP" 2>/dev/null || stat -f%z "$BOOTSTRAP")"
|
||||
host="$(hostname)"
|
||||
ts_run="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
|
||||
# ── Per-backend run ─────────────────────────────────────────────────────────
|
||||
run_backend() {
|
||||
local backend="$1"
|
||||
local idx="$2"
|
||||
local datadir="$WORKDIR/$backend"
|
||||
local rpc_port=$((RPC_BASE + idx))
|
||||
local rss_log="$WORKDIR/$backend.rss.log"
|
||||
|
||||
echo
|
||||
echo "════════════════════════════════════════════════════════════════════"
|
||||
echo " Backend: $backend (datadir: $datadir, rpcport: $rpc_port)"
|
||||
echo "════════════════════════════════════════════════════════════════════"
|
||||
|
||||
# Fresh datadir, copy bootstrap into place. FastImportBlockFile() picks
|
||||
# this up automatically when the block index is empty.
|
||||
rm -rf "$datadir"
|
||||
mkdir -p "$datadir"
|
||||
cp "$BOOTSTRAP" "$datadir/blk0001.dat"
|
||||
|
||||
# Minimal config — disable network so we measure only the import path.
|
||||
cat > "$datadir/triangles.conf" <<EOF
|
||||
chaindb=$backend
|
||||
dbcache=$DBCACHE
|
||||
nolisten=1
|
||||
connect=0
|
||||
rpcuser=bench
|
||||
rpcpassword=bench
|
||||
rpcport=$rpc_port
|
||||
debug=1
|
||||
printtoconsole=0
|
||||
EOF
|
||||
|
||||
# Launch in background. -daemon would daemonize but we want to track the
|
||||
# process tree; run in foreground and background it ourselves so we keep
|
||||
# the PID for RSS sampling and clean shutdown.
|
||||
local pid
|
||||
"$BINARY" -datadir="$datadir" -conf="triangles.conf" >"$datadir/stdout.log" 2>&1 &
|
||||
pid=$!
|
||||
echo "Launched $backend (pid $pid)"
|
||||
|
||||
# RSS sampler: log peak every second to a file.
|
||||
(
|
||||
while kill -0 "$pid" 2>/dev/null; do
|
||||
ps -o rss= -p "$pid" 2>/dev/null | tr -d ' ' >> "$rss_log" || true
|
||||
sleep 1
|
||||
done
|
||||
) &
|
||||
local sampler_pid=$!
|
||||
|
||||
# Watch for "FastImportBlockFile: indexed N blocks in Mms" in the daemon's
|
||||
# debug.log, which is the deterministic completion signal.
|
||||
local debug_log="$datadir/debug.log"
|
||||
local wait_start
|
||||
wait_start="$(date +%s)"
|
||||
local timeout_s=86400 # 24 hours hard cap
|
||||
local indexed_line=""
|
||||
while :; do
|
||||
if [ -f "$debug_log" ]; then
|
||||
indexed_line="$(grep -E "FastImportBlockFile: indexed [0-9]+ blocks in [0-9]+ms" "$debug_log" | tail -1 || true)"
|
||||
if [ -n "$indexed_line" ]; then
|
||||
break
|
||||
fi
|
||||
fi
|
||||
if ! kill -0 "$pid" 2>/dev/null; then
|
||||
echo "Daemon exited before completion line appeared. Check $datadir/stdout.log" >&2
|
||||
kill "$sampler_pid" 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
local elapsed=$(( $(date +%s) - wait_start ))
|
||||
if [ "$elapsed" -gt "$timeout_s" ]; then
|
||||
echo "Timeout after ${timeout_s}s without completion line" >&2
|
||||
kill "$pid" 2>/dev/null || true
|
||||
kill "$sampler_pid" 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
sleep 5
|
||||
done
|
||||
|
||||
echo "Completion: $indexed_line"
|
||||
|
||||
# Parse blocks_indexed and wall_ms from the line.
|
||||
local blocks_indexed wall_ms
|
||||
blocks_indexed="$(echo "$indexed_line" | sed -E 's/.*indexed ([0-9]+) blocks.*/\1/')"
|
||||
wall_ms="$(echo "$indexed_line" | sed -E 's/.*in ([0-9]+)ms.*/\1/')"
|
||||
|
||||
# Stop daemon cleanly via RPC, fall back to SIGTERM.
|
||||
"$BINARY" -datadir="$datadir" -conf="triangles.conf" stop >/dev/null 2>&1 || \
|
||||
kill -TERM "$pid" 2>/dev/null || true
|
||||
|
||||
# Wait up to 60s for clean exit.
|
||||
local stop_wait=0
|
||||
while kill -0 "$pid" 2>/dev/null && [ "$stop_wait" -lt 60 ]; do
|
||||
sleep 1
|
||||
stop_wait=$((stop_wait + 1))
|
||||
done
|
||||
kill -KILL "$pid" 2>/dev/null || true
|
||||
wait "$sampler_pid" 2>/dev/null || true
|
||||
|
||||
# Peak RSS: max of the sampler's recorded values.
|
||||
local peak_rss_kb=0
|
||||
if [ -f "$rss_log" ] && [ -s "$rss_log" ]; then
|
||||
peak_rss_kb="$(sort -nr "$rss_log" | head -1)"
|
||||
fi
|
||||
|
||||
# Datadir size — separate the chain DB from blk0001.dat (which is ~constant
|
||||
# across backends). We report the total datadir size; the consumer can
|
||||
# subtract bootstrap_size_bytes if they want chain-DB-only.
|
||||
local datadir_bytes
|
||||
datadir_bytes="$(du -sb "$datadir" 2>/dev/null | awk '{print $1}' || du -sk "$datadir" | awk '{print $1*1024}')"
|
||||
|
||||
# Append CSV row.
|
||||
echo "$ts_run,$backend,$bootstrap_size,$DBCACHE,$blocks_indexed,$wall_ms,$peak_rss_kb,$datadir_bytes,$BINARY,$host" >> "$RESULTS_CSV"
|
||||
|
||||
# Stdout summary.
|
||||
printf " blocks indexed: %s\n" "$blocks_indexed"
|
||||
printf " wall time: %s ms (%.1f min)\n" "$wall_ms" "$(awk "BEGIN{print $wall_ms/60000}")"
|
||||
printf " peak RSS: %s KB (%.1f GB)\n" "$peak_rss_kb" "$(awk "BEGIN{print $peak_rss_kb/1024/1024}")"
|
||||
printf " datadir size: %s bytes (%.1f GB)\n" "$datadir_bytes" "$(awk "BEGIN{print $datadir_bytes/1024/1024/1024}")"
|
||||
|
||||
# Cleanup unless --keep-datadirs.
|
||||
if [ "$KEEP" -eq 0 ]; then
|
||||
rm -rf "$datadir"
|
||||
fi
|
||||
}
|
||||
|
||||
# ── Main loop ──────────────────────────────────────────────────────────────
|
||||
idx=0
|
||||
IFS=',' read -r -a backends_arr <<< "$BACKENDS"
|
||||
for backend in "${backends_arr[@]}"; do
|
||||
case "$backend" in
|
||||
leveldb|rocksdb) ;;
|
||||
*) echo "Unknown backend: $backend" >&2; exit 2 ;;
|
||||
esac
|
||||
run_backend "$backend" "$idx"
|
||||
idx=$((idx + 1))
|
||||
done
|
||||
|
||||
echo
|
||||
echo "Done. Results appended to $RESULTS_CSV"
|
||||
Executable
+210
@@ -0,0 +1,210 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Fresh-datadir IBD smoke test for TRI.
|
||||
# Goal: detect the classic "starts from zero but stalls early / loops around 570"
|
||||
# failure mode, and verify that sync keeps making forward progress.
|
||||
#
|
||||
# Example:
|
||||
# bash scripts/ibd-smoke-test.sh \
|
||||
# --bin ./build/src/trianglesd \
|
||||
# --bootstrap-url http://100.104.4.5:8085/triangles-bootstrap.tar.gz \
|
||||
# --addnode 74.208.167.19 --addnode 194.233.88.206
|
||||
|
||||
BIN="${BIN:-./build/src/trianglesd}"
|
||||
TIMEOUT_SECONDS="${TIMEOUT_SECONDS:-1800}" # 30 minutes target window
|
||||
POLL_SECONDS="${POLL_SECONDS:-15}"
|
||||
STALL_WINDOW_SECONDS="${STALL_WINDOW_SECONDS:-180}"
|
||||
BOOTSTRAP_URL="${BOOTSTRAP_URL:-}"
|
||||
WORKDIR="${WORKDIR:-}"
|
||||
RPC_PORT="${RPC_PORT:-19192}"
|
||||
P2P_PORT="${P2P_PORT:-24193}"
|
||||
MIN_EXPECTED_HEIGHT="${MIN_EXPECTED_HEIGHT:-5000}"
|
||||
ALLOW_IBD="${ALLOW_IBD:-0}"
|
||||
WHITELIST="${WHITELIST:-127.0.0.1}"
|
||||
ADDNODES=()
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $0 [options]
|
||||
|
||||
Options:
|
||||
--bin PATH trianglesd binary (default: $BIN)
|
||||
--bootstrap-url URL optional bootstrap tar.gz URL to preload
|
||||
--workdir PATH use an explicit temp workdir
|
||||
--rpc-port N RPC port for test node (default: $RPC_PORT)
|
||||
--p2p-port N P2P port for test node (default: $P2P_PORT)
|
||||
--timeout N total test timeout seconds (default: $TIMEOUT_SECONDS)
|
||||
--poll N poll interval seconds (default: $POLL_SECONDS)
|
||||
--stall-window N no-progress failure window seconds (default: $STALL_WINDOW_SECONDS)
|
||||
--min-height N minimum expected height/progress floor (default: $MIN_EXPECTED_HEIGHT)
|
||||
--allow-ibd allow test to pass while still in IBD if progress is strong
|
||||
--addnode HOST trusted peer to add (repeatable)
|
||||
-h, --help show this help
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--bin) BIN="$2"; shift 2 ;;
|
||||
--bootstrap-url) BOOTSTRAP_URL="$2"; shift 2 ;;
|
||||
--workdir) WORKDIR="$2"; shift 2 ;;
|
||||
--rpc-port) RPC_PORT="$2"; shift 2 ;;
|
||||
--p2p-port) P2P_PORT="$2"; shift 2 ;;
|
||||
--timeout) TIMEOUT_SECONDS="$2"; shift 2 ;;
|
||||
--poll) POLL_SECONDS="$2"; shift 2 ;;
|
||||
--stall-window) STALL_WINDOW_SECONDS="$2"; shift 2 ;;
|
||||
--min-height) MIN_EXPECTED_HEIGHT="$2"; shift 2 ;;
|
||||
--allow-ibd) ALLOW_IBD=1; shift ;;
|
||||
--addnode) ADDNODES+=("$2"); shift 2 ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) echo "unknown arg: $1" >&2; usage; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ ! -x "$BIN" ]]; then
|
||||
echo "ERROR: trianglesd binary not executable: $BIN" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [[ -z "$WORKDIR" ]]; then
|
||||
WORKDIR="$(mktemp -d /tmp/tri-ibd-smoke-XXXXXX)"
|
||||
fi
|
||||
DATADIR="$WORKDIR/datadir"
|
||||
mkdir -p "$DATADIR"
|
||||
|
||||
RPCUSER="tri_test"
|
||||
RPCPASSWORD="tri_test_$(date +%s)_$RANDOM"
|
||||
CONF="$DATADIR/triangles.conf"
|
||||
cat > "$CONF" <<EOF
|
||||
server=1
|
||||
daemon=1
|
||||
staking=0
|
||||
listen=1
|
||||
discover=0
|
||||
upnp=0
|
||||
tor=0
|
||||
irc=0
|
||||
dnsseed=1
|
||||
checkpoints=1
|
||||
rpcuser=$RPCUSER
|
||||
rpcpassword=$RPCPASSWORD
|
||||
rpcport=$RPC_PORT
|
||||
port=$P2P_PORT
|
||||
maxconnections=32
|
||||
whitelist=$WHITELIST
|
||||
logtimestamps=1
|
||||
EOF
|
||||
|
||||
for host in "${ADDNODES[@]}"; do
|
||||
echo "addnode=$host" >> "$CONF"
|
||||
done
|
||||
|
||||
cleanup() {
|
||||
"$BIN" -datadir="$DATADIR" -conf="$CONF" stop >/dev/null 2>&1 || true
|
||||
sleep 2 || true
|
||||
pkill -f "$DATADIR" >/dev/null 2>&1 || true
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
if [[ -n "$BOOTSTRAP_URL" ]]; then
|
||||
echo "[ibd-test] downloading bootstrap: $BOOTSTRAP_URL"
|
||||
curl -L --fail --max-time 1800 "$BOOTSTRAP_URL" -o "$WORKDIR/bootstrap.tar.gz"
|
||||
tar xzf "$WORKDIR/bootstrap.tar.gz" -C "$DATADIR"
|
||||
rm -f "$DATADIR/database/log."* "$DATADIR/txleveldb/LOCK" "$DATADIR/smsgDB/LOCK" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
echo "[ibd-test] starting node from datadir: $DATADIR"
|
||||
"$BIN" -daemon -datadir="$DATADIR" -conf="$CONF" >/dev/null
|
||||
sleep 6
|
||||
|
||||
rpc() {
|
||||
local method="$1"
|
||||
local params="${2:-[]}"
|
||||
curl -sS --fail --user "$RPCUSER:$RPCPASSWORD" \
|
||||
--data-binary "{\"jsonrpc\":\"1.0\",\"id\":\"ibd\",\"method\":\"$method\",\"params\":$params}" \
|
||||
-H 'content-type: text/plain;' "http://127.0.0.1:$RPC_PORT/"
|
||||
}
|
||||
|
||||
extract_json() {
|
||||
python3 -c 'import json,sys; obj=json.load(sys.stdin); print(obj["result"])'
|
||||
}
|
||||
|
||||
extract_field() {
|
||||
local field="$1"
|
||||
python3 -c 'import json,sys; obj=json.load(sys.stdin); val=obj["result"].get(sys.argv[1]); print(val if val is not None else "")' "$field"
|
||||
}
|
||||
|
||||
start_ts=$(date +%s)
|
||||
last_progress_ts=$start_ts
|
||||
last_height=-1
|
||||
samples=0
|
||||
same_570_loops=0
|
||||
best_height=0
|
||||
|
||||
while true; do
|
||||
now=$(date +%s)
|
||||
elapsed=$((now - start_ts))
|
||||
if (( elapsed > TIMEOUT_SECONDS )); then
|
||||
echo "FAIL: timeout after ${elapsed}s"
|
||||
break
|
||||
fi
|
||||
|
||||
if info_json="$(rpc getblockchaininfo 2>/dev/null)"; then
|
||||
height=$(printf '%s' "$info_json" | extract_field blocks)
|
||||
ibd=$(printf '%s' "$info_json" | extract_field initialblockdownload)
|
||||
headers=$(printf '%s' "$info_json" | extract_field headers)
|
||||
else
|
||||
height=""
|
||||
ibd=""
|
||||
headers=""
|
||||
fi
|
||||
|
||||
peers=0
|
||||
if peer_json="$(rpc getconnectioncount 2>/dev/null)"; then
|
||||
peers=$(printf '%s' "$peer_json" | extract_json)
|
||||
fi
|
||||
|
||||
if [[ -n "$height" && "$height" != "$last_height" ]]; then
|
||||
last_progress_ts=$now
|
||||
last_height="$height"
|
||||
if (( height > best_height )); then
|
||||
best_height=$height
|
||||
fi
|
||||
fi
|
||||
|
||||
log_file="$DATADIR/debug.log"
|
||||
if [[ -f "$log_file" ]]; then
|
||||
loop_hits=$(tail -n 400 "$log_file" | grep -c 'start=571' || true)
|
||||
if (( loop_hits >= 3 )); then
|
||||
same_570_loops=$loop_hits
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "[ibd-test] t=${elapsed}s height=${height:-?} headers=${headers:-?} ibd=${ibd:-?} peers=$peers best=$best_height"
|
||||
|
||||
if [[ -n "$height" ]] && (( best_height >= MIN_EXPECTED_HEIGHT )) && [[ "$ibd" == "False" || "$ibd" == "false" ]]; then
|
||||
echo "PASS: left IBD and reached height $best_height"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$ALLOW_IBD" == "1" && -n "$height" ]] && (( best_height >= MIN_EXPECTED_HEIGHT )); then
|
||||
echo "PASS: strong sync progress observed (height $best_height) even though IBD remains true"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if (( now - last_progress_ts > STALL_WINDOW_SECONDS )); then
|
||||
echo "FAIL: no block-height progress for $((now - last_progress_ts))s"
|
||||
if (( same_570_loops > 0 )); then
|
||||
echo "HINT: detected repeated start=571 loop pattern ($same_570_loops hits in recent log tail)"
|
||||
fi
|
||||
echo "--- debug tail ---"
|
||||
tail -n 120 "$log_file" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
((samples++)) || true
|
||||
sleep "$POLL_SECONDS"
|
||||
done
|
||||
|
||||
exit 1
|
||||
+49
-3
@@ -23,6 +23,8 @@ add_library(hash9_crypto STATIC
|
||||
)
|
||||
target_include_directories(hash9_crypto PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
set_target_properties(hash9_crypto PROPERTIES LINKER_LANGUAGE C)
|
||||
# Hash9 C files have colliding static symbols (IV512, DECL_STATE, etc.) — skip unity
|
||||
set_target_properties(hash9_crypto PROPERTIES UNITY_BUILD OFF)
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 2. JSON library (header-only nlohmann/json via json_compat.h shim)
|
||||
@@ -36,13 +38,13 @@ target_include_directories(json_compat INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/js
|
||||
# EXCLUDES init.cpp, wallet.cpp (QT_GUI-conditional), noui.cpp (target-specific)
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
set(CORE_SOURCES
|
||||
alert.cpp
|
||||
addrman.cpp
|
||||
bootstrap.cpp
|
||||
checkpoints.cpp
|
||||
crypter.cpp
|
||||
crypto_ecdh.cpp
|
||||
crypto_ecdsa.cpp
|
||||
db.cpp
|
||||
irc.cpp
|
||||
key.cpp
|
||||
keystore.cpp
|
||||
main.cpp
|
||||
@@ -71,7 +73,11 @@ set(CORE_SOURCES
|
||||
rpcrawtransaction.cpp
|
||||
rpcsmessage.cpp
|
||||
zmqpublishnotifier.cpp
|
||||
txdb-base.cpp
|
||||
txdb-factory.cpp
|
||||
txdb-leveldb.cpp
|
||||
utxosnapshot.cpp
|
||||
snapshotnet.cpp
|
||||
lz4/lz4.c
|
||||
tor/onion_v3.cpp
|
||||
tor/tor_process.cpp
|
||||
@@ -93,6 +99,10 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM")
|
||||
list(APPEND CORE_SOURCES scrypt-arm.S)
|
||||
endif()
|
||||
|
||||
# RocksDB chain database backend (always built; see top-level CMakeLists.txt
|
||||
# for the rationale — RocksDB also backs the smessage store).
|
||||
list(APPEND CORE_SOURCES txdb-rocksdb.cpp)
|
||||
|
||||
add_library(triangles_common OBJECT ${CORE_SOURCES})
|
||||
|
||||
target_include_directories(triangles_common PUBLIC
|
||||
@@ -110,7 +120,6 @@ target_link_libraries(triangles_common PUBLIC
|
||||
leveldb_bundled
|
||||
OpenSSL::SSL
|
||||
OpenSSL::Crypto
|
||||
Boost::filesystem
|
||||
Boost::program_options
|
||||
Boost::thread
|
||||
Boost::chrono
|
||||
@@ -140,6 +149,17 @@ if(USE_ZMQ)
|
||||
target_link_libraries(triangles_common PUBLIC PkgConfig::ZMQ)
|
||||
endif()
|
||||
|
||||
# libsecp256k1 (mandatory) — ECDH / ECDSA replacement for OpenSSL EC.
|
||||
# Provided by add_subdirectory(src/secp256k1) in the top-level CMakeLists.
|
||||
target_link_libraries(triangles_common PUBLIC secp256k1)
|
||||
|
||||
# RocksDB (mandatory)
|
||||
if(TARGET RocksDB::rocksdb)
|
||||
target_link_libraries(triangles_common PUBLIC RocksDB::rocksdb)
|
||||
elseif(TARGET PkgConfig::RocksDB)
|
||||
target_link_libraries(triangles_common PUBLIC PkgConfig::RocksDB)
|
||||
endif()
|
||||
|
||||
# Optional: Embedded Tor
|
||||
if(USE_TOR_EMBEDDED)
|
||||
if(TOR_SOURCE_ROOT STREQUAL "")
|
||||
@@ -184,6 +204,31 @@ endif()
|
||||
|
||||
add_dependencies(triangles_common generate_build_info build_leveldb)
|
||||
|
||||
# ── Precompiled header (heavy STL + Boost + OpenSSL includes, C++ only) ──
|
||||
target_precompile_headers(triangles_common PRIVATE
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<string$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<vector$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<map$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<deque$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<algorithm$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<sstream$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<stdexcept$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<cstdint$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<cstring$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<memory$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<functional$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<filesystem$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<fstream$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<thread$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<mutex$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<condition_variable$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<boost/algorithm/string.hpp$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<openssl/sha.h$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<openssl/crypto.h$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<openssl/rand.h$<ANGLE-R>>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:<openssl/evp.h$<ANGLE-R>>"
|
||||
)
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 4. Headless daemon (trianglesd)
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
@@ -195,6 +240,7 @@ if(BUILD_DAEMON)
|
||||
)
|
||||
# No QT_GUI define — daemon gets the #if !defined(QT_GUI) code paths
|
||||
target_link_libraries(trianglesd PRIVATE triangles_common)
|
||||
target_precompile_headers(trianglesd REUSE_FROM triangles_common)
|
||||
|
||||
if(WIN32)
|
||||
set_target_properties(trianglesd PROPERTIES SUFFIX ".exe")
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "addrman.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int CAddrInfo::GetTriedBucket(const std::vector<unsigned char> &nKey) const
|
||||
|
||||
-276
@@ -1,276 +0,0 @@
|
||||
//
|
||||
// Alert system
|
||||
//
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <map>
|
||||
|
||||
#include "alert.h"
|
||||
#include "key.h"
|
||||
#include "net.h"
|
||||
#include "sync.h"
|
||||
#include "ui_interface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
map<uint256, CAlert> mapAlerts;
|
||||
CCriticalSection cs_mapAlerts;
|
||||
|
||||
// Alert keys disabled for decentralization - v5 hard fork
|
||||
static const char* pszMainKey = "";
|
||||
|
||||
// TestNet alerts pubKey
|
||||
static const char* pszTestKey = "";
|
||||
|
||||
void CUnsignedAlert::SetNull()
|
||||
{
|
||||
nVersion = 1;
|
||||
nRelayUntil = 0;
|
||||
nExpiration = 0;
|
||||
nID = 0;
|
||||
nCancel = 0;
|
||||
setCancel.clear();
|
||||
nMinVer = 0;
|
||||
nMaxVer = 0;
|
||||
setSubVer.clear();
|
||||
nPriority = 0;
|
||||
|
||||
strComment.clear();
|
||||
strStatusBar.clear();
|
||||
strReserved.clear();
|
||||
}
|
||||
|
||||
std::string CUnsignedAlert::ToString() const
|
||||
{
|
||||
std::string strSetCancel;
|
||||
for (int n : setCancel)
|
||||
strSetCancel += strprintf("%d ", n);
|
||||
std::string strSetSubVer;
|
||||
for (std::string str : setSubVer)
|
||||
strSetSubVer += "\"" + str + "\" ";
|
||||
return strprintf(
|
||||
"CAlert(\n"
|
||||
" nVersion = %d\n"
|
||||
" nRelayUntil = %" PRId64 "\n"
|
||||
" nExpiration = %" PRId64 "\n"
|
||||
" nID = %d\n"
|
||||
" nCancel = %d\n"
|
||||
" setCancel = %s\n"
|
||||
" nMinVer = %d\n"
|
||||
" nMaxVer = %d\n"
|
||||
" setSubVer = %s\n"
|
||||
" nPriority = %d\n"
|
||||
" strComment = \"%s\"\n"
|
||||
" strStatusBar = \"%s\"\n"
|
||||
")\n",
|
||||
nVersion,
|
||||
nRelayUntil,
|
||||
nExpiration,
|
||||
nID,
|
||||
nCancel,
|
||||
strSetCancel.c_str(),
|
||||
nMinVer,
|
||||
nMaxVer,
|
||||
strSetSubVer.c_str(),
|
||||
nPriority,
|
||||
strComment.c_str(),
|
||||
strStatusBar.c_str());
|
||||
}
|
||||
|
||||
void CUnsignedAlert::print() const
|
||||
{
|
||||
printf("%s", ToString().c_str());
|
||||
}
|
||||
|
||||
void CAlert::SetNull()
|
||||
{
|
||||
CUnsignedAlert::SetNull();
|
||||
vchMsg.clear();
|
||||
vchSig.clear();
|
||||
}
|
||||
|
||||
bool CAlert::IsNull() const
|
||||
{
|
||||
return (nExpiration == 0);
|
||||
}
|
||||
|
||||
uint256 CAlert::GetHash() const
|
||||
{
|
||||
return Hash(this->vchMsg.begin(), this->vchMsg.end());
|
||||
}
|
||||
|
||||
bool CAlert::IsInEffect() const
|
||||
{
|
||||
return (GetAdjustedTime() < nExpiration);
|
||||
}
|
||||
|
||||
bool CAlert::Cancels(const CAlert& alert) const
|
||||
{
|
||||
if (!IsInEffect())
|
||||
return false; // this was a no-op before 31403
|
||||
return (alert.nID <= nCancel || setCancel.count(alert.nID));
|
||||
}
|
||||
|
||||
bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
|
||||
{
|
||||
// TODO: rework for client-version-embedded-in-strSubVer ?
|
||||
return (IsInEffect() &&
|
||||
nMinVer <= nVersion && nVersion <= nMaxVer &&
|
||||
(setSubVer.empty() || setSubVer.count(strSubVerIn)));
|
||||
}
|
||||
|
||||
bool CAlert::AppliesToMe() const
|
||||
{
|
||||
return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
|
||||
}
|
||||
|
||||
bool CAlert::RelayTo(CNode* pnode) const
|
||||
{
|
||||
if (!IsInEffect())
|
||||
return false;
|
||||
// returns true if wasn't already contained in the set
|
||||
if (pnode->setKnown.insert(GetHash()).second)
|
||||
{
|
||||
if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
|
||||
AppliesToMe() ||
|
||||
GetAdjustedTime() < nRelayUntil)
|
||||
{
|
||||
pnode->PushMessage("alert", *this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CAlert::CheckSignature() const
|
||||
{
|
||||
// Alert key system disabled for decentralization - v5 hard fork
|
||||
const char* pszKey = fTestNet ? pszTestKey : pszMainKey;
|
||||
if (pszKey[0] == '\0')
|
||||
return false; // No alerts accepted without a valid key
|
||||
|
||||
CKey key;
|
||||
if (!key.SetPubKey(ParseHex(pszKey)))
|
||||
return error("CAlert::CheckSignature() : SetPubKey failed");
|
||||
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
|
||||
return error("CAlert::CheckSignature() : verify signature failed");
|
||||
|
||||
// Now unserialize the data
|
||||
CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
|
||||
sMsg >> *(CUnsignedAlert*)this;
|
||||
return true;
|
||||
}
|
||||
|
||||
CAlert CAlert::getAlertByHash(const uint256 &hash)
|
||||
{
|
||||
CAlert retval;
|
||||
{
|
||||
LOCK(cs_mapAlerts);
|
||||
map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
|
||||
if(mi != mapAlerts.end())
|
||||
retval = mi->second;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool CAlert::ProcessAlert(bool fThread)
|
||||
{
|
||||
if (!CheckSignature())
|
||||
return false;
|
||||
if (!IsInEffect())
|
||||
return false;
|
||||
|
||||
// alert.nID=max is reserved for if the alert key is
|
||||
// compromised. It must have a pre-defined message,
|
||||
// must never expire, must apply to all versions,
|
||||
// and must cancel all previous
|
||||
// alerts or it will be ignored (so an attacker can't
|
||||
// send an "everything is OK, don't panic" version that
|
||||
// cannot be overridden):
|
||||
int maxInt = std::numeric_limits<int>::max();
|
||||
if (nID == maxInt)
|
||||
{
|
||||
if (!(
|
||||
nExpiration == maxInt &&
|
||||
nCancel == (maxInt-1) &&
|
||||
nMinVer == 0 &&
|
||||
nMaxVer == maxInt &&
|
||||
setSubVer.empty() &&
|
||||
nPriority == maxInt &&
|
||||
strStatusBar == "URGENT: Alert key compromised, upgrade required"
|
||||
))
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
LOCK(cs_mapAlerts);
|
||||
// Cancel previous alerts
|
||||
for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
|
||||
{
|
||||
const CAlert& alert = (*mi).second;
|
||||
if (Cancels(alert))
|
||||
{
|
||||
printf("cancelling alert %d\n", alert.nID);
|
||||
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
|
||||
mapAlerts.erase(mi++);
|
||||
}
|
||||
else if (!alert.IsInEffect())
|
||||
{
|
||||
printf("expiring alert %d\n", alert.nID);
|
||||
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
|
||||
mapAlerts.erase(mi++);
|
||||
}
|
||||
else
|
||||
mi++;
|
||||
}
|
||||
|
||||
// Check if this alert has been cancelled
|
||||
for (auto& item : mapAlerts)
|
||||
{
|
||||
const CAlert& alert = item.second;
|
||||
if (alert.Cancels(*this))
|
||||
{
|
||||
printf("alert already cancelled by %d\n", alert.nID);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Add to mapAlerts
|
||||
mapAlerts.insert(make_pair(GetHash(), *this));
|
||||
// Notify UI and -alertnotify if it applies to me
|
||||
if(AppliesToMe())
|
||||
{
|
||||
uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
|
||||
std::string strCmd = GetArg("-alertnotify", "");
|
||||
if (!strCmd.empty())
|
||||
{
|
||||
// Alert text should be plain ascii coming from a trusted source, but to
|
||||
// be safe we first strip anything not in safeChars, then add single quotes around
|
||||
// the whole string before passing it to the shell:
|
||||
std::string singleQuote("'");
|
||||
// safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything
|
||||
// even possibly remotely dangerous like & or >
|
||||
std::string safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@");
|
||||
std::string safeStatus;
|
||||
for (std::string::size_type i = 0; i < strStatusBar.size(); i++)
|
||||
{
|
||||
if (safeChars.find(strStatusBar[i]) != std::string::npos)
|
||||
safeStatus.push_back(strStatusBar[i]);
|
||||
}
|
||||
safeStatus = singleQuote+safeStatus+singleQuote;
|
||||
boost::replace_all(strCmd, "%s", safeStatus);
|
||||
|
||||
if (fThread)
|
||||
boost::thread t(runCommand, strCmd); // thread runs free
|
||||
else
|
||||
runCommand(strCmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
|
||||
return true;
|
||||
}
|
||||
|
||||
-104
@@ -1,104 +0,0 @@
|
||||
// Copyright (c) 2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef _TRIANGLESALERT_H_
|
||||
#define _TRIANGLESALERT_H_ 1
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "uint256.h"
|
||||
#include "util.h"
|
||||
|
||||
class CNode;
|
||||
|
||||
/** Alerts are for notifying old versions if they become too obsolete and
|
||||
* need to upgrade. The message is displayed in the status bar.
|
||||
* Alert messages are broadcast as a vector of signed data. Unserializing may
|
||||
* not read the entire buffer if the alert is for a newer version, but older
|
||||
* versions can still relay the original data.
|
||||
*/
|
||||
class CUnsignedAlert
|
||||
{
|
||||
public:
|
||||
int nVersion;
|
||||
int64_t nRelayUntil; // when newer nodes stop relaying to newer nodes
|
||||
int64_t nExpiration;
|
||||
int nID;
|
||||
int nCancel;
|
||||
std::set<int> setCancel;
|
||||
int nMinVer; // lowest version inclusive
|
||||
int nMaxVer; // highest version inclusive
|
||||
std::set<std::string> setSubVer; // empty matches all
|
||||
int nPriority;
|
||||
|
||||
// Actions
|
||||
std::string strComment;
|
||||
std::string strStatusBar;
|
||||
std::string strReserved;
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(this->nVersion);
|
||||
nVersion = this->nVersion;
|
||||
READWRITE(nRelayUntil);
|
||||
READWRITE(nExpiration);
|
||||
READWRITE(nID);
|
||||
READWRITE(nCancel);
|
||||
READWRITE(setCancel);
|
||||
READWRITE(nMinVer);
|
||||
READWRITE(nMaxVer);
|
||||
READWRITE(setSubVer);
|
||||
READWRITE(nPriority);
|
||||
|
||||
READWRITE(strComment);
|
||||
READWRITE(strStatusBar);
|
||||
READWRITE(strReserved);
|
||||
)
|
||||
|
||||
void SetNull();
|
||||
|
||||
std::string ToString() const;
|
||||
void print() const;
|
||||
};
|
||||
|
||||
/** An alert is a combination of a serialized CUnsignedAlert and a signature. */
|
||||
class CAlert : public CUnsignedAlert
|
||||
{
|
||||
public:
|
||||
std::vector<unsigned char> vchMsg;
|
||||
std::vector<unsigned char> vchSig;
|
||||
|
||||
CAlert()
|
||||
{
|
||||
SetNull();
|
||||
}
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(vchMsg);
|
||||
READWRITE(vchSig);
|
||||
)
|
||||
|
||||
void SetNull();
|
||||
bool IsNull() const;
|
||||
uint256 GetHash() const;
|
||||
bool IsInEffect() const;
|
||||
bool Cancels(const CAlert& alert) const;
|
||||
bool AppliesTo(int nVersion, std::string strSubVerIn) const;
|
||||
bool AppliesToMe() const;
|
||||
bool RelayTo(CNode* pnode) const;
|
||||
bool CheckSignature() const;
|
||||
bool ProcessAlert(bool fThread = true);
|
||||
|
||||
/*
|
||||
* Get copy of (active) alert object by hash. Returns a null alert if it is not found.
|
||||
*/
|
||||
static CAlert getAlertByHash(const uint256 &hash);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+26
-24
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
// For all pages in affected range, increase lock count
|
||||
void LockRange(void *p, size_t size)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if(!size) return;
|
||||
const size_t base_addr = reinterpret_cast<size_t>(p);
|
||||
const size_t start_page = base_addr & page_mask;
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
// For all pages in affected range, decrease lock count
|
||||
void UnlockRange(void *p, size_t size)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if(!size) return;
|
||||
const size_t base_addr = reinterpret_cast<size_t>(p);
|
||||
const size_t start_page = base_addr & page_mask;
|
||||
@@ -101,13 +101,13 @@ public:
|
||||
// Get number of locked pages for diagnostics
|
||||
int GetLockedPageCount()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
return histogram.size();
|
||||
}
|
||||
|
||||
private:
|
||||
Locker locker;
|
||||
boost::mutex mutex;
|
||||
std::mutex mutex;
|
||||
size_t page_size, page_mask;
|
||||
// map of page base address to lock count
|
||||
typedef std::map<size_t,int> Histogram;
|
||||
@@ -182,15 +182,17 @@ private:
|
||||
template<typename T>
|
||||
struct secure_allocator : public std::allocator<T>
|
||||
{
|
||||
// MSVC8 default copy constructor is broken
|
||||
// C++20 removed pointer/reference/etc. member typedefs from std::allocator
|
||||
// and removed the 2-arg allocate(n, hint). Define what we still need
|
||||
// directly instead of pulling from base.
|
||||
typedef std::allocator<T> base;
|
||||
typedef typename base::size_type size_type;
|
||||
typedef typename base::difference_type difference_type;
|
||||
typedef typename base::pointer pointer;
|
||||
typedef typename base::const_pointer const_pointer;
|
||||
typedef typename base::reference reference;
|
||||
typedef typename base::const_reference const_reference;
|
||||
typedef typename base::value_type value_type;
|
||||
typedef T value_type;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
secure_allocator() throw() {}
|
||||
secure_allocator(const secure_allocator& a) throw() : base(a) {}
|
||||
template <typename U>
|
||||
@@ -199,10 +201,9 @@ struct secure_allocator : public std::allocator<T>
|
||||
template<typename _Other> struct rebind
|
||||
{ typedef secure_allocator<_Other> other; };
|
||||
|
||||
T* allocate(std::size_t n, const void *hint = 0)
|
||||
T* allocate(std::size_t n)
|
||||
{
|
||||
T *p;
|
||||
p = std::allocator<T>::allocate(n, hint);
|
||||
T* p = std::allocator<T>::allocate(n);
|
||||
if (p != NULL)
|
||||
LockedPageManager::instance.LockRange(p, sizeof(T) * n);
|
||||
return p;
|
||||
@@ -226,15 +227,16 @@ struct secure_allocator : public std::allocator<T>
|
||||
template<typename T>
|
||||
struct zero_after_free_allocator : public std::allocator<T>
|
||||
{
|
||||
// MSVC8 default copy constructor is broken
|
||||
// C++20 removed pointer/reference/etc. member typedefs from std::allocator.
|
||||
// Define what we still need directly instead of pulling from base.
|
||||
typedef std::allocator<T> base;
|
||||
typedef typename base::size_type size_type;
|
||||
typedef typename base::difference_type difference_type;
|
||||
typedef typename base::pointer pointer;
|
||||
typedef typename base::const_pointer const_pointer;
|
||||
typedef typename base::reference reference;
|
||||
typedef typename base::const_reference const_reference;
|
||||
typedef typename base::value_type value_type;
|
||||
typedef T value_type;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
zero_after_free_allocator() throw() {}
|
||||
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
|
||||
template <typename U>
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <openssl/bn.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
|
||||
+52
-12
@@ -2,9 +2,11 @@
|
||||
// Distributed under the MIT/X11 software license
|
||||
|
||||
#include "bootstrap.h"
|
||||
#include "utxosnapshot.h"
|
||||
#include "txdb.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include <zlib.h>
|
||||
@@ -36,7 +38,7 @@
|
||||
extern bool fTestNet;
|
||||
namespace Checkpoints { bool IsKnownCheckpoint(int nHeight, const uint256& hash); }
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Bootstrap {
|
||||
|
||||
@@ -207,12 +209,13 @@ bool DownloadFile(const std::string& host, const std::string& urlPath,
|
||||
const fs::path& destPath,
|
||||
ProgressCallback progressFn,
|
||||
std::string& strError,
|
||||
bool noProxy)
|
||||
bool noProxy,
|
||||
int portOverride)
|
||||
{
|
||||
try {
|
||||
std::string currentHost = host;
|
||||
std::string currentPath = urlPath;
|
||||
int currentPort = PORT;
|
||||
int currentPort = (portOverride > 0) ? portOverride : PORT;
|
||||
bool useSSL = false;
|
||||
std::string headerData;
|
||||
int redirectCount = 0;
|
||||
@@ -674,7 +677,9 @@ bool DownloadBootstrap(const std::string& host,
|
||||
fs::path tmpTarGz = dataDir / "bootstrap.tar.gz.tmp";
|
||||
std::string tarUrl = std::string(BASE_PATH) + "triangles-bootstrap.tar.gz";
|
||||
|
||||
printf("DownloadBootstrap(): attempting tar.gz download from %s%s\n", host.c_str(), tarUrl.c_str());
|
||||
bool tarDownloaded = DownloadFile(host, tarUrl, tmpTarGz, progressFn, strError, noProxy);
|
||||
printf("DownloadBootstrap(): tarDownloaded=%d result=%s\n", tarDownloaded, strError.c_str());
|
||||
|
||||
if (tarDownloaded) {
|
||||
bool extractOk = ExtractTarGz(tmpTarGz, dataDir, strError);
|
||||
@@ -714,16 +719,16 @@ bool DownloadBootstrap(const std::string& host,
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the archive included a trusted pre-built index (txleveldb/)
|
||||
// with a valid snapshot.manifest. If verified, keep it to skip the
|
||||
// Check if the archive included a trusted pre-built index for the active
|
||||
// backend with a valid snapshot.manifest. If verified, keep it to skip the
|
||||
// multi-hour FastImportBlockFile() rebuild.
|
||||
fs::path txleveldb = dataDir / "txleveldb";
|
||||
fs::path chainDbPath = GetChainDataDir();
|
||||
fs::path database = dataDir / "database";
|
||||
fs::path manifestPath = dataDir / "snapshot.manifest";
|
||||
|
||||
bool keepIndex = false;
|
||||
|
||||
if (fs::exists(manifestPath) && fs::exists(txleveldb)) {
|
||||
if (fs::exists(manifestPath) && fs::exists(chainDbPath)) {
|
||||
SnapshotManifest manifest;
|
||||
std::string manifestError;
|
||||
|
||||
@@ -750,9 +755,10 @@ bool DownloadBootstrap(const std::string& host,
|
||||
if (!keepIndex) {
|
||||
// No valid manifest or verification failed - delete the index.
|
||||
// FastImportBlockFile() will rebuild from blk0001.dat on next startup.
|
||||
printf("Bootstrap: removing extracted txleveldb/ (will rebuild index from blk0001.dat)\n");
|
||||
if (fs::exists(txleveldb))
|
||||
fs::remove_all(txleveldb);
|
||||
printf("Bootstrap: removing extracted %s/ (will rebuild index from blk0001.dat)\n",
|
||||
GetChainDataDir().filename().string().c_str());
|
||||
if (fs::exists(chainDbPath))
|
||||
fs::remove_all(chainDbPath);
|
||||
}
|
||||
|
||||
// Always remove BDB database/ dir (wallet environment from another machine)
|
||||
@@ -766,4 +772,38 @@ bool DownloadBootstrap(const std::string& host,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DownloadUtxoSnapshot(const std::string& host,
|
||||
const fs::path& dataDir,
|
||||
ProgressCallback progressFn,
|
||||
std::string& strError)
|
||||
{
|
||||
const bool noProxy = true;
|
||||
const char* snapshotFilename = "utxo-snapshot.bin";
|
||||
|
||||
// Download utxo-snapshot.bin to a temp file
|
||||
fs::path tmpPath = dataDir / "utxo-snapshot.bin.tmp";
|
||||
std::string urlPath = std::string(BASE_PATH) + snapshotFilename;
|
||||
|
||||
printf("Bootstrap: downloading UTXO snapshot from %s%s...\n", host.c_str(), urlPath.c_str());
|
||||
|
||||
if (!DownloadFile(host, urlPath, tmpPath, progressFn, strError, noProxy)) {
|
||||
fs::remove(tmpPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Bootstrap: UTXO snapshot downloaded, loading into database...\n");
|
||||
|
||||
// Load the snapshot into a fresh active chain DB
|
||||
if (!UtxoSnapshot::LoadSnapshot(tmpPath, dataDir, strError)) {
|
||||
fs::remove(tmpPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clean up the temp file
|
||||
fs::remove(tmpPath);
|
||||
|
||||
printf("Bootstrap: UTXO snapshot loaded successfully.\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Bootstrap
|
||||
|
||||
+16
-6
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
namespace Bootstrap {
|
||||
|
||||
@@ -20,16 +20,18 @@ namespace Bootstrap {
|
||||
typedef std::function<void(int64_t, int64_t)> ProgressCallback;
|
||||
|
||||
// Check if data dir already has blockchain data
|
||||
bool NeedsBootstrap(const boost::filesystem::path& dataDir);
|
||||
bool NeedsBootstrap(const std::filesystem::path& dataDir);
|
||||
|
||||
// Download a single file via HTTP GET, write to destPath.
|
||||
// If noProxy is true, bypass Tor SOCKS proxy and connect directly
|
||||
// (used for clearnet bootstrap downloads).
|
||||
// If portOverride is set (>0), uses that port instead of the default PORT.
|
||||
bool DownloadFile(const std::string& host, const std::string& urlPath,
|
||||
const boost::filesystem::path& destPath,
|
||||
const std::filesystem::path& destPath,
|
||||
ProgressCallback progressFn,
|
||||
std::string& strError,
|
||||
bool noProxy = false);
|
||||
bool noProxy = false,
|
||||
int portOverride = -1);
|
||||
|
||||
// Fetch the file manifest (list of relative paths to download)
|
||||
bool FetchFileList(const std::string& host,
|
||||
@@ -40,7 +42,7 @@ namespace Bootstrap {
|
||||
// Download bootstrap.tar.gz and extract to dataDir.
|
||||
// Falls back to filelist.txt + individual file download if tar.gz unavailable.
|
||||
bool DownloadBootstrap(const std::string& host,
|
||||
const boost::filesystem::path& dataDir,
|
||||
const std::filesystem::path& dataDir,
|
||||
ProgressCallback progressFn,
|
||||
std::string& strError);
|
||||
|
||||
@@ -54,7 +56,7 @@ namespace Bootstrap {
|
||||
};
|
||||
|
||||
// Parse a snapshot.manifest file into a SnapshotManifest struct.
|
||||
bool ParseManifest(const boost::filesystem::path& manifestPath,
|
||||
bool ParseManifest(const std::filesystem::path& manifestPath,
|
||||
SnapshotManifest& manifest,
|
||||
std::string& strError);
|
||||
|
||||
@@ -62,6 +64,14 @@ namespace Bootstrap {
|
||||
bool VerifyManifest(const SnapshotManifest& manifest,
|
||||
std::string& strError);
|
||||
|
||||
// Download a UTXO snapshot and load it into a fresh txleveldb.
|
||||
// This is much faster than downloading the full bootstrap archive.
|
||||
// Returns true if snapshot was downloaded and loaded successfully.
|
||||
bool DownloadUtxoSnapshot(const std::string& host,
|
||||
const std::filesystem::path& dataDir,
|
||||
ProgressCallback progressFn,
|
||||
std::string& strError);
|
||||
|
||||
} // namespace Bootstrap
|
||||
|
||||
#endif // TRIANGLES_BOOTSTRAP_H
|
||||
|
||||
+38
-6
@@ -32,14 +32,29 @@ namespace Checkpoints
|
||||
{ 9002, uint256("0xa1e20fb1d44688b763690cf74d6aefe859e4cc32981f9e3f2b2ae9702bbcf249")},
|
||||
{ 10881, uint256("0x4b6554c45e1e6764a6f3c309c47baf53c9edd81f624e52b072518cd15da237e6")},
|
||||
{ 17650, uint256("0x224940e1f986a202209b8e762728d1452ab45870c308abf84905674acf326a47")},
|
||||
{2000000, uint256("0xb0b02d4bb5ffa31f6f22fd042082ca0a085257af34dc2d4b31c3c8567b5574d5")},
|
||||
{2186940, uint256("0xbd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0")},
|
||||
{2190000, uint256("0x682baf783581468ba18f9967254a7f3944e8b8c4cc7101e7d99b68f4f9dd5271")},
|
||||
{2200000, uint256("0x0a8d0442f031f1258120f713f34e45f4f9a625fb753558e27b89b32ad5a9a740")},
|
||||
{2205000, uint256("0xf7aa893ec012181e321783d6a5487addf6997377908faa3e760c9054a4217d29")},
|
||||
{2206000, uint256("0x780ae878f8b10b6cbd51ceb2c0799c90d3551fa916be62974375071b9e36581c")},
|
||||
{2203594, uint256("0x5e016ae5d1f163c6679292b717a3db467a39d24b0a315182f4783caa79c722d8")},
|
||||
{2207000, uint256("0x8836d67b0f08036c4a7c26ff0a29d4461a52b6d8f552165ad9c1abec2f3cadfd")},
|
||||
{2208000, uint256("0xe4a19e8a29fa7aae47f7563377af3e896fee18ed069a64e325b8c2c6c820a1be")},
|
||||
{2209000, uint256("0x04c78a6fc863bed918a9364c58c64489943b2e85d84ddb1ac2fba584f390d5dc")},
|
||||
};
|
||||
|
||||
// Published UTXO snapshot file SHA256, keyed by snapshot height.
|
||||
// Each entry binds height -> SHA256 of the canonical snapshot file produced by
|
||||
// UtxoSnapshot::DumpSnapshot at that height. Used by SnapshotNet to verify
|
||||
// P2P-delivered snapshots without trusting any peer.
|
||||
//
|
||||
// Maintainers: after producing a snapshot, sha256 the file and add an entry
|
||||
// here. The corresponding (height, blockhash) must already exist in
|
||||
// mapCheckpoints / mapCheckpointsTestnet.
|
||||
static std::map<int, uint256> mapSnapshotHashes = {
|
||||
{2203594, uint256("0x49b35dd01659975c4a31954f37174c6e2e8878dd0723ab306ccecd991c80f79a")},
|
||||
};
|
||||
|
||||
static std::map<int, uint256> mapSnapshotHashesTestnet = {
|
||||
};
|
||||
|
||||
static MapCheckpoints mapCheckpointsTestnet = {
|
||||
@@ -55,6 +70,7 @@ namespace Checkpoints
|
||||
{ 9002, uint256("0xa1e20fb1d44688b763690cf74d6aefe859e4cc32981f9e3f2b2ae9702bbcf249")},
|
||||
{ 10881, uint256("0x4b6554c45e1e6764a6f3c309c47baf53c9edd81f624e52b072518cd15da237e6")},
|
||||
{ 17650, uint256("0x224940e1f986a202209b8e762728d1452ab45870c308abf84905674acf326a47")},
|
||||
{2000000, uint256("0xb0b02d4bb5ffa31f6f22fd042082ca0a085257af34dc2d4b31c3c8567b5574d5")},
|
||||
{2186940, uint256("0xbd952e8d4a612e336d840ad924a7e09395e36bcd9d929b302e47e60b5c3098c0")},
|
||||
{2190000, uint256("0x682baf783581468ba18f9967254a7f3944e8b8c4cc7101e7d99b68f4f9dd5271")},
|
||||
{2200000, uint256("0x0a8d0442f031f1258120f713f34e45f4f9a625fb753558e27b89b32ad5a9a740")},
|
||||
@@ -89,6 +105,22 @@ namespace Checkpoints
|
||||
return checkpoints.rbegin()->first;
|
||||
}
|
||||
|
||||
int GetBestSnapshotHeight()
|
||||
{
|
||||
std::map<int, uint256>& snaps = (fTestNet ? mapSnapshotHashesTestnet : mapSnapshotHashes);
|
||||
if (snaps.empty()) return 0;
|
||||
return snaps.rbegin()->first;
|
||||
}
|
||||
|
||||
bool GetSnapshotHash(int nHeight, uint256& fileHashOut)
|
||||
{
|
||||
std::map<int, uint256>& snaps = (fTestNet ? mapSnapshotHashesTestnet : mapSnapshotHashes);
|
||||
auto it = snaps.find(nHeight);
|
||||
if (it == snaps.end()) return false;
|
||||
fileHashOut = it->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
|
||||
{
|
||||
MapCheckpoints& checkpoints = (fTestNet ? mapCheckpointsTestnet : mapCheckpoints);
|
||||
@@ -167,7 +199,7 @@ namespace Checkpoints
|
||||
|
||||
bool WriteSyncCheckpoint(const uint256& hashCheckpoint)
|
||||
{
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
txdb.TxnBegin();
|
||||
if (!txdb.WriteSyncCheckpoint(hashCheckpoint))
|
||||
{
|
||||
@@ -193,7 +225,7 @@ namespace Checkpoints
|
||||
return false;
|
||||
}
|
||||
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashPendingCheckpoint];
|
||||
if (!pindexCheckpoint->IsInMainChain())
|
||||
{
|
||||
@@ -264,7 +296,7 @@ namespace Checkpoints
|
||||
{
|
||||
// checkpoint block accepted but not yet in main chain
|
||||
printf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str());
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(mapBlockIndex[hash]))
|
||||
return error("ResetSyncCheckpoint: ReadFromDisk failed for hardened checkpoint %s", hash.ToString().c_str());
|
||||
@@ -408,7 +440,7 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
|
||||
if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
|
||||
return false;
|
||||
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
|
||||
if (!pindexCheckpoint->IsInMainChain())
|
||||
{
|
||||
|
||||
@@ -45,6 +45,11 @@ namespace Checkpoints
|
||||
// Return conservative estimate of total number of blocks, 0 if unknown
|
||||
int GetTotalBlocksEstimate();
|
||||
|
||||
// Return the highest checkpoint height that has a published UTXO snapshot
|
||||
// hash, along with the snapshot's file SHA256. Returns 0 height if none.
|
||||
int GetBestSnapshotHeight();
|
||||
bool GetSnapshotHash(int nHeight, uint256& fileHashOut);
|
||||
|
||||
// Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
|
||||
CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
|
||||
|
||||
|
||||
+10
-10
@@ -9,17 +9,17 @@
|
||||
#include <deque>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
template<typename T>
|
||||
class CCheckQueue
|
||||
{
|
||||
private:
|
||||
boost::mutex mutex;
|
||||
boost::condition_variable condWorker;
|
||||
boost::condition_variable condMaster;
|
||||
std::mutex mutex;
|
||||
std::condition_variable condWorker;
|
||||
std::condition_variable condMaster;
|
||||
|
||||
std::deque<T> queue;
|
||||
unsigned int nIdle;
|
||||
@@ -32,7 +32,7 @@ private:
|
||||
|
||||
bool Loop(bool fMaster)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (!fMaster)
|
||||
nTotal++;
|
||||
nIdle++;
|
||||
@@ -102,7 +102,7 @@ public:
|
||||
|
||||
void StartBatch()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
fAllOk = true;
|
||||
nTodo = 0;
|
||||
}
|
||||
@@ -112,7 +112,7 @@ public:
|
||||
if (vChecks.empty())
|
||||
return;
|
||||
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
for (typename std::vector<T>::iterator it = vChecks.begin(); it != vChecks.end(); ++it)
|
||||
{
|
||||
queue.push_back(T());
|
||||
@@ -132,7 +132,7 @@ public:
|
||||
|
||||
void Quit()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
fQuit = true;
|
||||
condWorker.notify_all();
|
||||
condMaster.notify_all();
|
||||
|
||||
+2
-2
@@ -7,8 +7,8 @@
|
||||
|
||||
// These need to be macros, as version.cpp's and triangles-qt.rc's voodoo requires it
|
||||
#define CLIENT_VERSION_MAJOR 5
|
||||
#define CLIENT_VERSION_MINOR 8
|
||||
#define CLIENT_VERSION_REVISION 6
|
||||
#define CLIENT_VERSION_MINOR 9
|
||||
#define CLIENT_VERSION_REVISION 7
|
||||
#define CLIENT_VERSION_BUILD 0
|
||||
|
||||
// Converts the parameter X to a string after macro replacement on X has been performed.
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "key.h"
|
||||
#include "serialize.h"
|
||||
|
||||
#include <openssl/crypto.h> /* for OPENSSL_cleanse */
|
||||
|
||||
const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
|
||||
const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "crypto_ecdh.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
#include <secp256k1.h>
|
||||
#include <secp256k1_ecdh.h>
|
||||
|
||||
namespace {
|
||||
|
||||
// One process-wide context is sufficient for ECDH — no signing or verification
|
||||
// flags needed. Created lazily on first use; libsecp256k1 contexts are
|
||||
// thread-safe for read-only operations like ECDH.
|
||||
secp256k1_context* GetECDHContext()
|
||||
{
|
||||
static std::once_flag once;
|
||||
static secp256k1_context* ctx = nullptr;
|
||||
std::call_once(once, []() {
|
||||
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||
});
|
||||
return ctx;
|
||||
}
|
||||
|
||||
// Hash function callback that returns the raw X coordinate of the shared
|
||||
// point. Mirrors OpenSSL's ECDH_compute_key behaviour when the KDF is NULL.
|
||||
int hash_xonly(unsigned char* output,
|
||||
const unsigned char* x32,
|
||||
const unsigned char* /*y32*/,
|
||||
void* /*data*/)
|
||||
{
|
||||
std::memcpy(output, x32, 32);
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ECDH_xonly_secp256k1(unsigned char out32[32],
|
||||
const unsigned char privkey32[32],
|
||||
const unsigned char* pubkey,
|
||||
std::size_t pubkey_len)
|
||||
{
|
||||
if (pubkey_len != 33 && pubkey_len != 65) return false;
|
||||
|
||||
secp256k1_context* ctx = GetECDHContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_pubkey pk;
|
||||
if (!secp256k1_ec_pubkey_parse(ctx, &pk, pubkey, pubkey_len))
|
||||
return false;
|
||||
|
||||
return secp256k1_ecdh(ctx, out32, &pk, privkey32, hash_xonly, nullptr) == 1;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
#ifndef TRIANGLES_CRYPTO_ECDH_H
|
||||
#define TRIANGLES_CRYPTO_ECDH_H
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
/**
|
||||
* Compute the shared secret X coordinate via secp256k1 ECDH.
|
||||
*
|
||||
* Output matches OpenSSL's ECDH_compute_key(buf, 32, peer_pub, our_priv, NULL)
|
||||
* — i.e. the raw X coordinate of the shared point, with no KDF applied. This
|
||||
* preserves bit-for-bit compatibility with smessage's existing key derivation
|
||||
* (which feeds the X coordinate into SHA-512 itself), so historical encrypted
|
||||
* messages remain decryptable after the migration off OpenSSL EC.
|
||||
*
|
||||
* @param out32 32-byte buffer for the shared X coordinate.
|
||||
* @param privkey32 32-byte secret scalar (big-endian).
|
||||
* @param pubkey Peer public key, serialized as either 33 bytes (compressed)
|
||||
* or 65 bytes (uncompressed).
|
||||
* @param pubkey_len 33 or 65; any other length fails immediately.
|
||||
* @return true on success, false if the public key is malformed or the
|
||||
* private key is invalid (zero / >= curve order).
|
||||
*/
|
||||
bool ECDH_xonly_secp256k1(unsigned char out32[32],
|
||||
const unsigned char privkey32[32],
|
||||
const unsigned char* pubkey,
|
||||
std::size_t pubkey_len);
|
||||
|
||||
#endif // TRIANGLES_CRYPTO_ECDH_H
|
||||
@@ -0,0 +1,389 @@
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Copyright (c) 2015 Pieter Wuille (lax DER parser, MIT licence)
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "crypto_ecdsa.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
#include <secp256k1.h>
|
||||
#include <secp256k1_recovery.h>
|
||||
|
||||
namespace {
|
||||
|
||||
// Combined VERIFY + SIGN context. libsecp256k1 contexts are thread-safe for
|
||||
// signing and verification once created. In libsecp256k1 >= 0.2 these flags
|
||||
// are accepted but increasingly no-ops; passing both keeps us compatible with
|
||||
// older versions still in distro packages.
|
||||
secp256k1_context* GetEcdsaContext()
|
||||
{
|
||||
static std::once_flag once;
|
||||
static secp256k1_context* ctx = nullptr;
|
||||
std::call_once(once, []() {
|
||||
ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
|
||||
});
|
||||
return ctx;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Lax DER parser, vendored from Bitcoin Core (contrib/lax_der_parsing.c).
|
||||
//
|
||||
// libsecp256k1's strict parser rejects DER encodings that OpenSSL has
|
||||
// historically accepted: non-minimal length bytes, extra leading zeros on R/S,
|
||||
// negative integers, etc. Many such signatures already exist on chain. This
|
||||
// parser tolerates them, normalises (R, S) into a 64-byte compact buffer, and
|
||||
// hands that to libsecp256k1's compact-signature parser. Anything that still
|
||||
// fails to fit (e.g. R or S exceeding 32 bytes after stripping leading zeros)
|
||||
// is treated as zero so the verify call returns a clean failure rather than
|
||||
// crashing.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx,
|
||||
secp256k1_ecdsa_signature* sig,
|
||||
const unsigned char* input,
|
||||
std::size_t inputlen)
|
||||
{
|
||||
std::size_t rpos, rlen, spos, slen;
|
||||
std::size_t pos = 0;
|
||||
std::size_t lenbyte;
|
||||
unsigned char tmpsig[64] = {0};
|
||||
int overflow = 0;
|
||||
|
||||
// Initialise sig with a parseable but invalid signature so the caller
|
||||
// always gets a defined value back even on early-exit paths.
|
||||
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
|
||||
|
||||
// SEQUENCE tag.
|
||||
if (pos == inputlen || input[pos] != 0x30) return 0;
|
||||
pos++;
|
||||
|
||||
// SEQUENCE length (skipped — we trust the inner element lengths).
|
||||
if (pos == inputlen) return 0;
|
||||
lenbyte = input[pos++];
|
||||
if (lenbyte & 0x80) {
|
||||
lenbyte -= 0x80;
|
||||
if (lenbyte > inputlen - pos) return 0;
|
||||
pos += lenbyte;
|
||||
}
|
||||
|
||||
// R: INTEGER tag.
|
||||
if (pos == inputlen || input[pos] != 0x02) return 0;
|
||||
pos++;
|
||||
|
||||
// R: length.
|
||||
if (pos == inputlen) return 0;
|
||||
lenbyte = input[pos++];
|
||||
if (lenbyte & 0x80) {
|
||||
lenbyte -= 0x80;
|
||||
if (lenbyte > inputlen - pos) return 0;
|
||||
while (lenbyte > 0 && input[pos] == 0) { pos++; lenbyte--; }
|
||||
if (lenbyte >= sizeof(std::size_t)) return 0;
|
||||
rlen = 0;
|
||||
while (lenbyte > 0) { rlen = (rlen << 8) + input[pos]; pos++; lenbyte--; }
|
||||
} else {
|
||||
rlen = lenbyte;
|
||||
}
|
||||
if (rlen > inputlen - pos) return 0;
|
||||
rpos = pos;
|
||||
pos += rlen;
|
||||
|
||||
// S: INTEGER tag.
|
||||
if (pos == inputlen || input[pos] != 0x02) return 0;
|
||||
pos++;
|
||||
|
||||
// S: length.
|
||||
if (pos == inputlen) return 0;
|
||||
lenbyte = input[pos++];
|
||||
if (lenbyte & 0x80) {
|
||||
lenbyte -= 0x80;
|
||||
if (lenbyte > inputlen - pos) return 0;
|
||||
while (lenbyte > 0 && input[pos] == 0) { pos++; lenbyte--; }
|
||||
if (lenbyte >= sizeof(std::size_t)) return 0;
|
||||
slen = 0;
|
||||
while (lenbyte > 0) { slen = (slen << 8) + input[pos]; pos++; lenbyte--; }
|
||||
} else {
|
||||
slen = lenbyte;
|
||||
}
|
||||
if (slen > inputlen - pos) return 0;
|
||||
spos = pos;
|
||||
|
||||
// Strip leading zeros from R and place right-aligned in tmpsig[0..32).
|
||||
while (rlen > 0 && input[rpos] == 0) { rlen--; rpos++; }
|
||||
if (rlen > 32) {
|
||||
overflow = 1;
|
||||
} else {
|
||||
std::memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
|
||||
}
|
||||
|
||||
// Strip leading zeros from S and place right-aligned in tmpsig[32..64).
|
||||
while (slen > 0 && input[spos] == 0) { slen--; spos++; }
|
||||
if (slen > 32) {
|
||||
overflow = 1;
|
||||
} else {
|
||||
std::memcpy(tmpsig + 64 - slen, input + spos, slen);
|
||||
}
|
||||
|
||||
if (!overflow) {
|
||||
overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
|
||||
}
|
||||
if (overflow) {
|
||||
std::memset(tmpsig, 0, 64);
|
||||
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ECDSA_verify_secp256k1(const unsigned char hash32[32],
|
||||
const unsigned char* sig, std::size_t sig_len,
|
||||
const unsigned char* pubkey, std::size_t pubkey_len)
|
||||
{
|
||||
if (sig_len == 0) return false;
|
||||
if (pubkey_len != 33 && pubkey_len != 65) return false;
|
||||
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_pubkey pk;
|
||||
if (!secp256k1_ec_pubkey_parse(ctx, &pk, pubkey, pubkey_len))
|
||||
return false;
|
||||
|
||||
secp256k1_ecdsa_signature parsed_sig;
|
||||
if (!ecdsa_signature_parse_der_lax(ctx, &parsed_sig, sig, sig_len))
|
||||
return false;
|
||||
|
||||
return secp256k1_ecdsa_verify(ctx, &parsed_sig, hash32, &pk) == 1;
|
||||
}
|
||||
|
||||
bool ECDSA_sign_secp256k1(unsigned char* out, std::size_t* out_len,
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char privkey32[32])
|
||||
{
|
||||
if (!out || !out_len) return false;
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_ecdsa_signature sig;
|
||||
if (!secp256k1_ecdsa_sign(ctx, &sig, hash32, privkey32, nullptr, nullptr))
|
||||
return false;
|
||||
|
||||
return secp256k1_ecdsa_signature_serialize_der(ctx, out, out_len, &sig) == 1;
|
||||
}
|
||||
|
||||
bool ECDSA_sign_compact_secp256k1(unsigned char out65[65],
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed)
|
||||
{
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_ecdsa_recoverable_signature recsig;
|
||||
if (!secp256k1_ecdsa_sign_recoverable(ctx, &recsig, hash32, privkey32, nullptr, nullptr))
|
||||
return false;
|
||||
|
||||
int recid = -1;
|
||||
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, &out65[1], &recid, &recsig))
|
||||
return false;
|
||||
if (recid < 0 || recid > 3) return false;
|
||||
|
||||
out65[0] = static_cast<unsigned char>(27 + recid + (fCompressed ? 4 : 0));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ECDSA_recover_compact_secp256k1(unsigned char* pubkey_out,
|
||||
std::size_t* pubkey_len_out,
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char sig65[65])
|
||||
{
|
||||
if (!pubkey_out || !pubkey_len_out) return false;
|
||||
|
||||
int header = sig65[0];
|
||||
if (header < 27 || header >= 35) return false;
|
||||
bool fCompressed = (header >= 31);
|
||||
int recid = (header - 27) & 0x3;
|
||||
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_ecdsa_recoverable_signature recsig;
|
||||
if (!secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, &recsig, &sig65[1], recid))
|
||||
return false;
|
||||
|
||||
secp256k1_pubkey pk;
|
||||
if (!secp256k1_ecdsa_recover(ctx, &pk, &recsig, hash32))
|
||||
return false;
|
||||
|
||||
std::size_t out_len = fCompressed ? 33 : 65;
|
||||
if (!secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &out_len, &pk,
|
||||
fCompressed ? SECP256K1_EC_COMPRESSED
|
||||
: SECP256K1_EC_UNCOMPRESSED))
|
||||
return false;
|
||||
|
||||
*pubkey_len_out = out_len;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ECDSA_seckey_verify_secp256k1(const unsigned char privkey32[32])
|
||||
{
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
return secp256k1_ec_seckey_verify(ctx, privkey32) == 1;
|
||||
}
|
||||
|
||||
bool ECDSA_pubkey_verify_secp256k1(const unsigned char* pubkey, std::size_t pubkey_len)
|
||||
{
|
||||
if (pubkey_len != 33 && pubkey_len != 65) return false;
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
secp256k1_pubkey pk;
|
||||
return secp256k1_ec_pubkey_parse(ctx, &pk, pubkey, pubkey_len) == 1;
|
||||
}
|
||||
|
||||
bool ECDSA_pubkey_from_privkey_secp256k1(unsigned char* out, std::size_t* out_len_out,
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed)
|
||||
{
|
||||
if (!out || !out_len_out) return false;
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_pubkey pk;
|
||||
if (!secp256k1_ec_pubkey_create(ctx, &pk, privkey32))
|
||||
return false;
|
||||
|
||||
std::size_t len = fCompressed ? 33 : 65;
|
||||
if (!secp256k1_ec_pubkey_serialize(ctx, out, &len, &pk,
|
||||
fCompressed ? SECP256K1_EC_COMPRESSED
|
||||
: SECP256K1_EC_UNCOMPRESSED))
|
||||
return false;
|
||||
*out_len_out = len;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// SEC1 / RFC-5915 DER codec for secp256k1 ECPrivateKey
|
||||
//
|
||||
// Vendored from Bitcoin Core (src/key.cpp), MIT-licensed. The decoder is lax
|
||||
// about details (matches OpenSSL's d2i_ECPrivateKey lenience); the encoder
|
||||
// writes the exact byte layout that OpenSSL's i2d_ECPrivateKey produces for
|
||||
// this curve so wallet.dat records remain interchangeable across versions.
|
||||
//
|
||||
// Compressed pubkey: 214 bytes
|
||||
// Uncompressed pubkey: 279 bytes
|
||||
//
|
||||
// The static templates below carry every byte except the 32-byte private
|
||||
// scalar and the public key bytes, which are spliced into the precomputed
|
||||
// offsets at encode time.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
namespace {
|
||||
|
||||
const unsigned char der_template_compressed[214] = {
|
||||
0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20,
|
||||
/* private key (32 bytes) at offset 8 */
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
|
||||
0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,
|
||||
0x04,0x01,0x07,0x04,0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,
|
||||
0x62,0x95,0xCE,0x87,0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,
|
||||
0x81,0x5B,0x16,0xF8,0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,
|
||||
0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,
|
||||
0x00,
|
||||
/* compressed pubkey (33 bytes) at offset 181 */
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
};
|
||||
|
||||
const unsigned char der_template_uncompressed[279] = {
|
||||
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20,
|
||||
/* private key (32 bytes) at offset 9 */
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
|
||||
0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,
|
||||
0x04,0x01,0x07,0x04,0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,
|
||||
0x62,0x95,0xCE,0x87,0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,
|
||||
0x81,0x5B,0x16,0xF8,0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,
|
||||
0xFB,0xFC,0x0E,0x11,0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,
|
||||
0xD0,0x8F,0xFB,0x10,0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,
|
||||
0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,
|
||||
0x00,
|
||||
/* uncompressed pubkey (65 bytes) at offset 214 */
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ECDSA_privkey_export_der_secp256k1(unsigned char* out, std::size_t* out_len_out,
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed)
|
||||
{
|
||||
if (!out || !out_len_out) return false;
|
||||
|
||||
secp256k1_context* ctx = GetEcdsaContext();
|
||||
if (!ctx) return false;
|
||||
|
||||
secp256k1_pubkey pk;
|
||||
if (!secp256k1_ec_pubkey_create(ctx, &pk, privkey32))
|
||||
return false;
|
||||
|
||||
if (fCompressed) {
|
||||
std::memcpy(out, der_template_compressed, sizeof(der_template_compressed));
|
||||
std::memcpy(out + 8, privkey32, 32);
|
||||
std::size_t pub_len = 33;
|
||||
if (!secp256k1_ec_pubkey_serialize(ctx, out + 181, &pub_len, &pk, SECP256K1_EC_COMPRESSED))
|
||||
return false;
|
||||
*out_len_out = sizeof(der_template_compressed);
|
||||
} else {
|
||||
std::memcpy(out, der_template_uncompressed, sizeof(der_template_uncompressed));
|
||||
std::memcpy(out + 9, privkey32, 32);
|
||||
std::size_t pub_len = 65;
|
||||
if (!secp256k1_ec_pubkey_serialize(ctx, out + 214, &pub_len, &pk, SECP256K1_EC_UNCOMPRESSED))
|
||||
return false;
|
||||
*out_len_out = sizeof(der_template_uncompressed);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ECDSA_privkey_import_der_secp256k1(unsigned char privkey32_out[32],
|
||||
const unsigned char* der, std::size_t der_len)
|
||||
{
|
||||
// Lax SEC1/RFC-5915 ECPrivateKey parser. We only need to find the OCTET
|
||||
// STRING containing the private key scalar; everything else (curve params,
|
||||
// optional public key) is informational. Mirrors Bitcoin Core's
|
||||
// ec_privkey_import_der.
|
||||
const unsigned char* end = der + der_len;
|
||||
if (end < der + 1 || *(der++) != 0x30) return false;
|
||||
|
||||
// Outer SEQUENCE length — variable length encoding.
|
||||
if (der >= end) return false;
|
||||
int lenb = *(der++);
|
||||
if (lenb < 0x80) {
|
||||
// short form, ignore
|
||||
} else {
|
||||
int n = lenb & 0x7F;
|
||||
if (n == 0 || n > 2) return false;
|
||||
if (der + n > end) return false;
|
||||
der += n;
|
||||
}
|
||||
|
||||
// Version INTEGER (1).
|
||||
if (der + 3 > end || der[0] != 0x02 || der[1] != 0x01 || der[2] != 0x01) return false;
|
||||
der += 3;
|
||||
|
||||
// privateKey OCTET STRING (length 32).
|
||||
if (der + 2 > end || der[0] != 0x04 || der[1] != 0x20) return false;
|
||||
der += 2;
|
||||
if (der + 32 > end) return false;
|
||||
std::memcpy(privkey32_out, der, 32);
|
||||
|
||||
// Validate the result against the curve order; reject zero / >= n.
|
||||
return ECDSA_seckey_verify_secp256k1(privkey32_out);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
#ifndef TRIANGLES_CRYPTO_ECDSA_H
|
||||
#define TRIANGLES_CRYPTO_ECDSA_H
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
/**
|
||||
* Verify a DER-encoded secp256k1 ECDSA signature using libsecp256k1.
|
||||
*
|
||||
* Drop-in replacement for OpenSSL's
|
||||
* ECDSA_verify(0, hash, 32, sig, sig_len, pkey)
|
||||
* with one important caveat baked in: the DER input is parsed *laxly*
|
||||
* (Bitcoin Core's `lax_der_parsing` algorithm), so historical non-canonical
|
||||
* encodings already on chain — extra padding, leading zeros, length-byte
|
||||
* quirks that OpenSSL's permissive ASN.1 reader once accepted — continue
|
||||
* to verify. Strict-DER-only parsing here would silently fork the chain.
|
||||
*
|
||||
* High-S signatures are accepted (libsecp256k1's verify behaviour by default).
|
||||
* No malleability check is applied; that is policy and lives elsewhere.
|
||||
*
|
||||
* @param hash32 32-byte message hash to verify against.
|
||||
* @param sig DER-encoded signature bytes.
|
||||
* @param sig_len Length of `sig`.
|
||||
* @param pubkey Serialized public key (33 bytes compressed or 65 uncompressed).
|
||||
* @param pubkey_len 33 or 65; any other length fails immediately.
|
||||
* @return true iff the signature is valid for (hash32, pubkey).
|
||||
*/
|
||||
bool ECDSA_verify_secp256k1(const unsigned char hash32[32],
|
||||
const unsigned char* sig, std::size_t sig_len,
|
||||
const unsigned char* pubkey, std::size_t pubkey_len);
|
||||
|
||||
/**
|
||||
* Sign `hash32` with `privkey32` and write a DER-encoded signature to `out`.
|
||||
*
|
||||
* libsecp256k1 uses RFC 6979 deterministic nonces, so signature bytes will
|
||||
* differ from OpenSSL's random-nonce output for the same key+hash, but any
|
||||
* resulting signature is equally valid. Low-S is enforced automatically.
|
||||
*
|
||||
* @param out Output buffer; must be at least `*out_len` bytes.
|
||||
* libsecp256k1 produces at most 72 bytes of DER.
|
||||
* @param out_len In: capacity of `out`. Out: bytes actually written.
|
||||
* @param hash32 32-byte message hash to sign.
|
||||
* @param privkey32 32-byte secret scalar.
|
||||
* @return true on success.
|
||||
*/
|
||||
bool ECDSA_sign_secp256k1(unsigned char* out, std::size_t* out_len,
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char privkey32[32]);
|
||||
|
||||
/**
|
||||
* Produce a 65-byte recoverable compact signature.
|
||||
*
|
||||
* Output layout matches the existing wire format:
|
||||
* out[0] = 27 + recid + (fCompressed ? 4 : 0)
|
||||
* out[1..33) = R (big-endian, 32 bytes)
|
||||
* out[33..65) = S (big-endian, 32 bytes)
|
||||
*
|
||||
* @param out65 65-byte output buffer.
|
||||
* @param hash32 32-byte message hash to sign.
|
||||
* @param privkey32 32-byte secret scalar.
|
||||
* @param fCompressed Whether the matching public key is compressed; affects
|
||||
* the recid offset in the header byte.
|
||||
* @return true on success.
|
||||
*/
|
||||
bool ECDSA_sign_compact_secp256k1(unsigned char out65[65],
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed);
|
||||
|
||||
/**
|
||||
* Recover the signing public key from a 65-byte compact signature (as produced
|
||||
* by ECDSA_sign_compact_secp256k1) and a message hash.
|
||||
*
|
||||
* The header byte's "compressed" flag determines whether the recovered key is
|
||||
* serialized as 33 bytes (compressed) or 65 bytes (uncompressed).
|
||||
*
|
||||
* @param pubkey_out Output buffer; needs at least 65 bytes capacity.
|
||||
* @param pubkey_len_out Receives the actual serialized length (33 or 65).
|
||||
* @param hash32 32-byte message hash that was signed.
|
||||
* @param sig65 65-byte compact signature.
|
||||
* @return true if recovery succeeded.
|
||||
*/
|
||||
bool ECDSA_recover_compact_secp256k1(unsigned char* pubkey_out,
|
||||
std::size_t* pubkey_len_out,
|
||||
const unsigned char hash32[32],
|
||||
const unsigned char sig65[65]);
|
||||
|
||||
/** Return true iff `privkey32` is a valid secp256k1 secret (in (0, n)). */
|
||||
bool ECDSA_seckey_verify_secp256k1(const unsigned char privkey32[32]);
|
||||
|
||||
/** Return true iff `pubkey/pubkey_len` parses as a valid secp256k1 point. */
|
||||
bool ECDSA_pubkey_verify_secp256k1(const unsigned char* pubkey, std::size_t pubkey_len);
|
||||
|
||||
/**
|
||||
* Derive the public key for `privkey32` and serialize it.
|
||||
* @param out Output buffer; must be at least 65 bytes.
|
||||
* @param out_len_out Receives the actual length (33 or 65).
|
||||
* @param privkey32 32-byte secret scalar.
|
||||
* @param fCompressed Whether to serialize compressed (33B) or uncompressed (65B).
|
||||
* @return true on success.
|
||||
*/
|
||||
bool ECDSA_pubkey_from_privkey_secp256k1(unsigned char* out, std::size_t* out_len_out,
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed);
|
||||
|
||||
/**
|
||||
* SEC1/RFC-5915 DER ECPrivateKey encoder/decoder for the secp256k1 curve.
|
||||
* Output bytes match the layout produced by OpenSSL's i2d_ECPrivateKey on this
|
||||
* curve (compressed = 214 bytes, uncompressed = 279 bytes), so wallet.dat
|
||||
* records written by previous OpenSSL-EC builds remain readable, and records
|
||||
* we write remain readable by older OpenSSL-based builds.
|
||||
*/
|
||||
bool ECDSA_privkey_export_der_secp256k1(unsigned char* out, std::size_t* out_len_out,
|
||||
const unsigned char privkey32[32],
|
||||
bool fCompressed);
|
||||
bool ECDSA_privkey_import_der_secp256k1(unsigned char privkey32_out[32],
|
||||
const unsigned char* der, std::size_t der_len);
|
||||
|
||||
#endif // TRIANGLES_CRYPTO_ECDSA_H
|
||||
+3
-4
@@ -8,16 +8,15 @@
|
||||
#include "util.h"
|
||||
#include "main.h"
|
||||
#include "ui_interface.h"
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#ifndef WIN32
|
||||
#include "sys/stat.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
unsigned int nWalletDBUpdated;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -28,7 +29,7 @@ extern unsigned int nWalletDBUpdated;
|
||||
|
||||
void ThreadFlushWalletDB(void* parg);
|
||||
bool BackupWallet(const CWallet& wallet, const std::string& strDest);
|
||||
bool AutoBackupWallet(const boost::filesystem::path& walletPath);
|
||||
bool AutoBackupWallet(const std::filesystem::path& walletPath);
|
||||
|
||||
|
||||
class CDBEnv
|
||||
@@ -37,7 +38,7 @@ private:
|
||||
bool fDetachDB;
|
||||
bool fDbEnvInit;
|
||||
bool fMockDb;
|
||||
boost::filesystem::path pathEnv;
|
||||
std::filesystem::path pathEnv;
|
||||
std::string strPath;
|
||||
|
||||
void EnvShutdown();
|
||||
@@ -71,7 +72,7 @@ public:
|
||||
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
|
||||
bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
|
||||
|
||||
bool Open(boost::filesystem::path pathEnv_);
|
||||
bool Open(std::filesystem::path pathEnv_);
|
||||
void Close();
|
||||
void Flush(bool fShutdown);
|
||||
void CheckpointLSN(std::string strFile);
|
||||
@@ -317,7 +318,7 @@ public:
|
||||
class CAddrDB
|
||||
{
|
||||
private:
|
||||
boost::filesystem::path pathAddr;
|
||||
std::filesystem::path pathAddr;
|
||||
public:
|
||||
CAddrDB();
|
||||
bool Write(const CAddrMan& addr);
|
||||
|
||||
+169
-40
@@ -14,6 +14,8 @@
|
||||
#include "smessage.h"
|
||||
#include "openssl_compat.h"
|
||||
#include "bootstrap.h"
|
||||
#include "utxosnapshot.h"
|
||||
#include "snapshotnet.h"
|
||||
#include "tor/tor_embedded.h"
|
||||
#include "tor/onion_v3.h"
|
||||
#include "tor/tor_process.h"
|
||||
@@ -22,10 +24,10 @@
|
||||
#endif
|
||||
#include "notificationqueue.h"
|
||||
#include "addressindex.h"
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
// boost/filesystem/convenience.hpp removed in modern Boost; functionality is in filesystem.hpp
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <boost/interprocess/sync/file_lock.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <openssl/crypto.h>
|
||||
@@ -34,10 +36,20 @@
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
// Windows.h (transitively included) defines these as macros, clobbering Checkpoints:: enum values.
|
||||
#ifdef STRICT
|
||||
#undef STRICT
|
||||
#endif
|
||||
#ifdef ADVISORY
|
||||
#undef ADVISORY
|
||||
#endif
|
||||
#ifdef PERMISSIVE
|
||||
#undef PERMISSIVE
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
CWallet* pwalletMain;
|
||||
CClientUIInterface uiInterface;
|
||||
@@ -52,7 +64,7 @@ enum Checkpoints::CPMode CheckpointsMode;
|
||||
|
||||
static CCriticalSection cs_DeferredStartup;
|
||||
static bool fDeferredStartupRunning = false;
|
||||
static boost::thread_group* pScriptCheckThreads = NULL;
|
||||
static std::vector<std::thread>* pScriptCheckThreads = nullptr;
|
||||
|
||||
static void ThreadScriptCheck()
|
||||
{
|
||||
@@ -107,6 +119,31 @@ bool ShutdownRequested()
|
||||
return fRequestShutdown;
|
||||
}
|
||||
|
||||
// P2P UTXO snapshot fetcher. Started from AppInit2 step 11.6 when the chain
|
||||
// is empty and snapshot mode is enabled. Saves utxo-snapshot.bin on success
|
||||
// and requests shutdown so a fresh boot can load it via Step 6c.
|
||||
static void ThreadSnapshotFetch(void* parg)
|
||||
{
|
||||
RenameThread("Triangles-snapfetch");
|
||||
// Give peers ~30s to connect and complete version handshake.
|
||||
for (int i = 0; i < 30 && !fRequestShutdown; ++i)
|
||||
MilliSleep(1000);
|
||||
if (fRequestShutdown) return;
|
||||
|
||||
int snapTimeoutSec = (int)GetArg("-snapshottimeout", 600);
|
||||
printf("SnapshotNet: starting P2P snapshot fetch (timeout=%ds)...\n", snapTimeoutSec);
|
||||
|
||||
std::string err;
|
||||
if (SnapshotNet::TryFetchSnapshot(GetDataDir(), snapTimeoutSec, err)) {
|
||||
printf("SnapshotNet: snapshot saved. Shutting down — restart the daemon to load it.\n");
|
||||
uiInterface.InitMessage(_("UTXO snapshot saved. Restart the node to load it."));
|
||||
StartShutdown();
|
||||
} else {
|
||||
printf("SnapshotNet: P2P snapshot fetch failed: %s\n", err.c_str());
|
||||
printf("SnapshotNet: falling back to genesis sync. Use -bootstrap for legacy HTTP fallback.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadDeferredStartup(void* parg)
|
||||
{
|
||||
// Make this thread recognisable as the deferred startup worker.
|
||||
@@ -196,9 +233,10 @@ void Shutdown(void* parg)
|
||||
pScriptCheckQueue->Quit();
|
||||
if (pScriptCheckThreads)
|
||||
{
|
||||
pScriptCheckThreads->join_all();
|
||||
for (std::thread& t : *pScriptCheckThreads)
|
||||
if (t.joinable()) t.join();
|
||||
delete pScriptCheckThreads;
|
||||
pScriptCheckThreads = NULL;
|
||||
pScriptCheckThreads = nullptr;
|
||||
}
|
||||
delete pScriptCheckQueue;
|
||||
pScriptCheckQueue = NULL;
|
||||
@@ -223,7 +261,7 @@ void Shutdown(void* parg)
|
||||
pNotificationQueue = NULL;
|
||||
}
|
||||
|
||||
// CTxDB().Close();
|
||||
// MakeChainDB()->Close();
|
||||
bitdb.Flush(false);
|
||||
bitdb.Flush(true);
|
||||
fs::remove(GetPidFile());
|
||||
@@ -380,7 +418,7 @@ std::string HelpMessage()
|
||||
//" -proxy=<ip:port> " + _("Connect through socks proxy") + "\n" +
|
||||
//" -socks=<n> " + _("Select the version of socks proxy to use (4-5, default: 5)") + "\n" +
|
||||
" -tor=<ip:port> " + _("Use proxy to reach tor hidden services (default: same as -proxy)") + "\n"
|
||||
" -notor " + _("Disable Tor (WARNING: wallet will not start - Tor is required)") + "\n" +
|
||||
" -notor " + _("Disable Tor - run in clearnet-only mode (no .onion connectivity)") + "\n" +
|
||||
" -torsocks=<port> " + _("Set embedded or managed Tor SOCKS proxy port (default: 19099)") + "\n" +
|
||||
" -torhiddenservice " + _("Enable the managed Tor hidden service (default: 1)") + "\n" +
|
||||
" -torhsport=<port> " + _("Set embedded or managed Tor hidden service port (default: wallet listen port)") + "\n" +
|
||||
@@ -442,7 +480,6 @@ std::string HelpMessage()
|
||||
" -walletnotify=<cmd> " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)") + "\n" +
|
||||
" -confchange " + _("Require a confirmations for change (default: 0)") + "\n" +
|
||||
" -enforcecanonical " + _("Enforce transaction scripts to use canonical PUSH operators (default: 1)") + "\n" +
|
||||
" -alertnotify=<cmd> " + _("Execute command when a relevant alert is received (%s in cmd is replaced by message)") + "\n" +
|
||||
" -upgradewallet " + _("Upgrade wallet to latest format") + "\n" +
|
||||
" -keypool=<n> " + _("Set key pool size to <n> (default: 100)") + "\n" +
|
||||
" -rescan " + _("Rescan the block chain for missing wallet transactions") + "\n" +
|
||||
@@ -664,15 +701,15 @@ bool AppInit2()
|
||||
|
||||
int nScriptCheckThreads = GetArg("-par", 0);
|
||||
if (nScriptCheckThreads <= 0)
|
||||
nScriptCheckThreads = boost::thread::hardware_concurrency();
|
||||
nScriptCheckThreads = std::thread::hardware_concurrency();
|
||||
if (nScriptCheckThreads > 16)
|
||||
nScriptCheckThreads = 16;
|
||||
if (nScriptCheckThreads > 1)
|
||||
{
|
||||
pScriptCheckQueue = new CCheckQueue<CScriptCheck>(128);
|
||||
pScriptCheckThreads = new boost::thread_group();
|
||||
pScriptCheckQueue = new CCheckQueue<CScriptCheck>(32);
|
||||
pScriptCheckThreads = new std::vector<std::thread>();
|
||||
for (int i = 0; i < nScriptCheckThreads - 1; ++i)
|
||||
pScriptCheckThreads->create_thread(&ThreadScriptCheck);
|
||||
pScriptCheckThreads->emplace_back(&ThreadScriptCheck);
|
||||
printf("Script verification threads: %d workers + main thread\n", nScriptCheckThreads - 1);
|
||||
}
|
||||
|
||||
@@ -887,17 +924,27 @@ bool AppInit2()
|
||||
// ********************************************************* Step 6b: bootstrap download (daemon)
|
||||
// Automatic: if data dir has no blockchain, bootstrap without asking.
|
||||
// Can also be forced with -bootstrap flag, or disabled with -nobootstrap.
|
||||
#ifndef QT_GUI
|
||||
//
|
||||
// v5.9.5: P2P UTXO snapshot fetch is the default for fresh installs (Step 11.6).
|
||||
// The legacy clearnet HTTP bootstrap only runs when the user explicitly requests
|
||||
// it via -bootstrap, or when -snapshot=0 disables the P2P fetcher.
|
||||
// Bootstrap auto-download works for both GUI and daemon.
|
||||
// GUI users get the same automatic bootstrap on fresh installs.
|
||||
{
|
||||
bool wantsBootstrap = GetBoolArg("-bootstrap", false);
|
||||
bool noBootstrap = GetBoolArg("-nobootstrap", false);
|
||||
bool snapshotMode = GetBoolArg("-snapshot", true);
|
||||
fs::path dataPath = GetDataDir();
|
||||
bool needsBootstrap = Bootstrap::NeedsBootstrap(dataPath);
|
||||
|
||||
if (needsBootstrap && !noBootstrap) {
|
||||
if (needsBootstrap && !noBootstrap && !snapshotMode) {
|
||||
printf("Bootstrap: no blockchain data found — downloading automatically.\n");
|
||||
printf("Bootstrap: (use -nobootstrap to skip)\n");
|
||||
uiInterface.InitMessage(_("Downloading blockchain data..."));
|
||||
wantsBootstrap = true;
|
||||
} else if (needsBootstrap && snapshotMode && !wantsBootstrap) {
|
||||
printf("Bootstrap: no blockchain data found — will fetch UTXO snapshot via P2P after network start.\n");
|
||||
printf("Bootstrap: (use -bootstrap for legacy clearnet HTTP bootstrap, or -snapshot=0 to disable P2P fetcher)\n");
|
||||
}
|
||||
|
||||
if (wantsBootstrap)
|
||||
@@ -907,32 +954,87 @@ bool AppInit2()
|
||||
std::string host = Bootstrap::DEFAULT_HOST;
|
||||
std::string strError;
|
||||
|
||||
uiInterface.InitMessage(_("Downloading blockchain snapshot..."));
|
||||
printf("Bootstrap: contacting %s...\n", host.c_str());
|
||||
|
||||
auto progressFn = [](int64_t bytesDownloaded, int64_t totalBytes) {
|
||||
int64_t lastGuiUpdate = 0;
|
||||
auto progressFn = [&lastGuiUpdate](int64_t bytesDownloaded, int64_t totalBytes) {
|
||||
if (totalBytes > 0) {
|
||||
printf("\rBootstrap: %lld / %lld MB (%lld%%)",
|
||||
(long long)(bytesDownloaded / (1024*1024)),
|
||||
(long long)(totalBytes / (1024*1024)),
|
||||
(long long)((bytesDownloaded * 100) / totalBytes));
|
||||
fflush(stdout);
|
||||
// Update GUI status bar every ~1 MB
|
||||
int64_t now = GetTimeMillis();
|
||||
if (now - lastGuiUpdate > 1000) {
|
||||
lastGuiUpdate = now;
|
||||
std::string msg = strprintf("Downloading blockchain: %lld / %lld MB (%lld%%)",
|
||||
(long long)(bytesDownloaded / (1024*1024)),
|
||||
(long long)(totalBytes / (1024*1024)),
|
||||
(long long)((bytesDownloaded * 100) / totalBytes));
|
||||
uiInterface.InitMessage(msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool success = Bootstrap::DownloadBootstrap(host, dataPath, progressFn, strError);
|
||||
// Try UTXO snapshot first (fast: ~2-10 MB download). Only attempted
|
||||
// if the configured backend's chain DB doesn't already exist.
|
||||
bool success = false;
|
||||
bool triedUtxoSnapshot = false;
|
||||
if (needsBootstrap && !fs::exists(GetChainDataDir())) {
|
||||
uiInterface.InitMessage(_("Downloading UTXO snapshot..."));
|
||||
printf("Bootstrap: trying UTXO snapshot from %s (fast path)...\n", host.c_str());
|
||||
|
||||
if (!success) {
|
||||
printf("\nBootstrap: failed: %s\n", strError.c_str());
|
||||
printf("Bootstrap: skipping, will sync from network.\n");
|
||||
} else {
|
||||
printf("\nBootstrap: done.\n");
|
||||
std::string utxoError;
|
||||
if (Bootstrap::DownloadUtxoSnapshot(host, dataPath, progressFn, utxoError)) {
|
||||
printf("\nBootstrap: UTXO snapshot loaded — will sync remaining blocks from network.\n");
|
||||
success = true;
|
||||
} else {
|
||||
printf("\nBootstrap: UTXO snapshot unavailable: %s\n", utxoError.c_str());
|
||||
printf("Bootstrap: falling back to full bootstrap download...\n");
|
||||
}
|
||||
triedUtxoSnapshot = true;
|
||||
}
|
||||
|
||||
// Fall back to full bootstrap.tar.gz if UTXO snapshot failed
|
||||
if (!success) {
|
||||
uiInterface.InitMessage(_("Downloading blockchain snapshot..."));
|
||||
printf("Bootstrap: contacting %s...\n", host.c_str());
|
||||
|
||||
success = Bootstrap::DownloadBootstrap(host, dataPath, progressFn, strError);
|
||||
|
||||
if (!success) {
|
||||
printf("\nBootstrap: failed: %s\n", strError.c_str());
|
||||
printf("Bootstrap: skipping, will sync from network.\n");
|
||||
} else {
|
||||
printf("\nBootstrap: done.\n");
|
||||
}
|
||||
}
|
||||
|
||||
StartupPerfLog("bootstrap_download", GetTimeMillis() - nBootstrapStart,
|
||||
strprintf("host=%s success=%d", host.c_str(), success));
|
||||
strprintf("host=%s success=%d utxo_snapshot=%d", host.c_str(), success, triedUtxoSnapshot));
|
||||
}
|
||||
} // end bootstrap scope
|
||||
#endif
|
||||
|
||||
// ********************************************************* Step 6c: manual UTXO snapshot loading
|
||||
// If utxo-snapshot.bin exists in data dir and the chain DB hasn't been
|
||||
// initialized for the configured backend, load it.
|
||||
{
|
||||
fs::path dataPath = GetDataDir();
|
||||
fs::path snapshotFile = dataPath / "utxo-snapshot.bin";
|
||||
fs::path chainDbDir = GetChainDataDir();
|
||||
|
||||
if (fs::exists(snapshotFile) && !fs::exists(chainDbDir)) {
|
||||
printf("Found utxo-snapshot.bin — loading UTXO snapshot...\n");
|
||||
uiInterface.InitMessage(_("Loading UTXO snapshot..."));
|
||||
|
||||
std::string strError;
|
||||
if (UtxoSnapshot::LoadSnapshot(snapshotFile, dataPath, strError)) {
|
||||
printf("UTXO snapshot loaded successfully.\n");
|
||||
} else {
|
||||
printf("UTXO snapshot load failed: %s\n", strError.c_str());
|
||||
printf("Will proceed with normal sync.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ********************************************************* Step 7: load blockchain
|
||||
|
||||
@@ -946,22 +1048,22 @@ bool AppInit2()
|
||||
|
||||
if (GetBoolArg("-loadblockindextest"))
|
||||
{
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
txdb.LoadBlockIndex();
|
||||
PrintBlockTree();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle -reindex: delete the LevelDB block index so it gets rebuilt
|
||||
// from the raw blk*.dat files via FastImportBlockFile().
|
||||
// This recalculates money supply, tx index, and UTXO set from scratch.
|
||||
// Handle -reindex: delete the chain DB so it gets rebuilt from the raw
|
||||
// blk*.dat files via FastImportBlockFile(). This recalculates money
|
||||
// supply, tx index, and UTXO set from scratch. Backend-agnostic via
|
||||
// WipeChainDataDir(), which resolves the directory per the configured
|
||||
// -chaindb backend.
|
||||
if (GetBoolArg("-reindex", false))
|
||||
{
|
||||
printf("Reindex requested: removing block index database...\n");
|
||||
uiInterface.InitMessage(_("Removing block index for reindex..."));
|
||||
fs::path txleveldbPath = GetDataDir() / "txleveldb";
|
||||
if (fs::exists(txleveldbPath))
|
||||
fs::remove_all(txleveldbPath);
|
||||
printf("Reindex requested: removing chain database...\n");
|
||||
uiInterface.InitMessage(_("Removing chain database for reindex..."));
|
||||
WipeChainDataDir();
|
||||
}
|
||||
|
||||
uiInterface.InitMessage(_("Loading block index..."));
|
||||
@@ -973,7 +1075,7 @@ bool AppInit2()
|
||||
// If the block index is empty but blk0001.dat exists (bootstrap download),
|
||||
// fast-import: build the index directly from the block file without re-writing
|
||||
// data. Batches LevelDB commits every 200K blocks for speed.
|
||||
if (nBestHeight == 0 && boost::filesystem::exists(GetDataDir() / "blk0001.dat")
|
||||
if (nBestHeight == 0 && std::filesystem::exists(GetDataDir() / "blk0001.dat")
|
||||
&& mapBlockIndex.size() <= 1)
|
||||
{
|
||||
uiInterface.InitMessage(_("Importing bootstrap blocks..."));
|
||||
@@ -1146,7 +1248,7 @@ bool AppInit2()
|
||||
bool fScannedWithIndex = false;
|
||||
if (fAddressIndex && !GetBoolArg("-rescan"))
|
||||
{
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
int nAddressIndexStartHeight = 0;
|
||||
uint256 hashAddressIndexBestChain = 0;
|
||||
if (txdb.ReadAddressIndexStartHeight(nAddressIndexStartHeight) &&
|
||||
@@ -1244,6 +1346,15 @@ bool AppInit2()
|
||||
#ifdef USE_UPNP
|
||||
fUseUPnP = false;
|
||||
#endif
|
||||
} else if (GetBoolArg("-notor", false)) {
|
||||
// -notor: user explicitly disabled Tor. Allow the daemon to start
|
||||
// in clearnet-only mode (useful for diagnostics, benchmarking, and
|
||||
// recovery). .onion connectivity will not be available.
|
||||
printf("NOTICE: Tor disabled via -notor. Running in clearnet-only mode.\n");
|
||||
printf(" .onion connections will NOT be available.\n");
|
||||
SetReachable(NET_IPV4, true);
|
||||
SetReachable(NET_IPV6, true);
|
||||
SetReachable(NET_TOR, false);
|
||||
} else {
|
||||
std::string torError = CTorEmbedded::GetInstance()->GetStartupError();
|
||||
if (torError.empty())
|
||||
@@ -1388,6 +1499,24 @@ bool AppInit2()
|
||||
if (fServer)
|
||||
NewThread(ThreadRPCServer, NULL);
|
||||
|
||||
// ********************************************************* Step 11.6: P2P UTXO snapshot fetch
|
||||
// If the chain is empty and snapshot mode is enabled (default), spawn a
|
||||
// background thread that waits for snapshot-capable peers, downloads the
|
||||
// canonical snapshot via P2P, and saves it to utxo-snapshot.bin. On
|
||||
// success, requests a clean shutdown so the user can restart and have
|
||||
// Step 6c load the snapshot in a fresh boot.
|
||||
{
|
||||
bool snapshotMode = GetBoolArg("-snapshot", true);
|
||||
bool needsSnapshot = (nBestHeight <= 0);
|
||||
bool haveSnapshotFile = fs::exists(GetDataDir() / "utxo-snapshot.bin");
|
||||
|
||||
if (snapshotMode && needsSnapshot && !haveSnapshotFile &&
|
||||
Checkpoints::GetBestSnapshotHeight() > 0)
|
||||
{
|
||||
NewThread(ThreadSnapshotFetch, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
LOCK(cs_DeferredStartup);
|
||||
fDeferredStartupRunning = true;
|
||||
|
||||
-405
@@ -1,405 +0,0 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "irc.h"
|
||||
#include "net.h"
|
||||
#include "strlcpy.h"
|
||||
#include "base58.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int nGotIRCAddresses = 0;
|
||||
|
||||
void ThreadIRCSeed2(void* parg);
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct ircaddr
|
||||
{
|
||||
struct in_addr ip;
|
||||
short port;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
string EncodeAddress(const CService& addr)
|
||||
{
|
||||
struct ircaddr tmp;
|
||||
if (addr.GetInAddr(&tmp.ip))
|
||||
{
|
||||
tmp.port = htons(addr.GetPort());
|
||||
|
||||
vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
|
||||
return string("u") + EncodeBase58Check(vch);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool DecodeAddress(string str, CService& addr)
|
||||
{
|
||||
vector<unsigned char> vch;
|
||||
if (!DecodeBase58Check(str.substr(1), vch))
|
||||
return false;
|
||||
|
||||
struct ircaddr tmp;
|
||||
if (vch.size() != sizeof(tmp))
|
||||
return false;
|
||||
memcpy(&tmp, &vch[0], sizeof(tmp));
|
||||
|
||||
addr = CService(tmp.ip, ntohs(tmp.port));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static bool Send(SOCKET hSocket, const char* pszSend)
|
||||
{
|
||||
if (strstr(pszSend, "PONG") != pszSend)
|
||||
printf("IRC SENDING: %s\n", pszSend);
|
||||
const char* psz = pszSend;
|
||||
const char* pszEnd = psz + strlen(psz);
|
||||
while (psz < pszEnd)
|
||||
{
|
||||
int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
|
||||
if (ret < 0)
|
||||
return false;
|
||||
psz += ret;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RecvLineIRC(SOCKET hSocket, string& strLine)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
bool fRet = RecvLine(hSocket, strLine);
|
||||
if (fRet)
|
||||
{
|
||||
if (fShutdown)
|
||||
return false;
|
||||
vector<string> vWords;
|
||||
ParseString(strLine, ' ', vWords);
|
||||
if (vWords.size() >= 1 && vWords[0] == "PING")
|
||||
{
|
||||
strLine[1] = 'O';
|
||||
strLine += '\r';
|
||||
Send(hSocket, strLine.c_str());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return fRet;
|
||||
}
|
||||
}
|
||||
|
||||
int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
string strLine;
|
||||
strLine.reserve(10000);
|
||||
if (!RecvLineIRC(hSocket, strLine))
|
||||
return 0;
|
||||
printf("IRC %s\n", strLine.c_str());
|
||||
if (psz1 && strLine.find(psz1) != string::npos)
|
||||
return 1;
|
||||
if (psz2 && strLine.find(psz2) != string::npos)
|
||||
return 2;
|
||||
if (psz3 && strLine.find(psz3) != string::npos)
|
||||
return 3;
|
||||
if (psz4 && strLine.find(psz4) != string::npos)
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
bool Wait(int nSeconds)
|
||||
{
|
||||
if (fShutdown)
|
||||
return false;
|
||||
printf("IRC waiting %d seconds to reconnect\n", nSeconds);
|
||||
for (int i = 0; i < nSeconds; i++)
|
||||
{
|
||||
if (fShutdown)
|
||||
return false;
|
||||
MilliSleep(1000);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
|
||||
{
|
||||
strRet.clear();
|
||||
while (true)
|
||||
{
|
||||
string strLine;
|
||||
if (!RecvLineIRC(hSocket, strLine))
|
||||
return false;
|
||||
|
||||
vector<string> vWords;
|
||||
ParseString(strLine, ' ', vWords);
|
||||
if (vWords.size() < 2)
|
||||
continue;
|
||||
|
||||
if (vWords[1] == psz1)
|
||||
{
|
||||
printf("IRC %s\n", strLine.c_str());
|
||||
strRet = strLine;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
|
||||
{
|
||||
Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
|
||||
|
||||
string strLine;
|
||||
if (!RecvCodeLine(hSocket, "302", strLine))
|
||||
return false;
|
||||
|
||||
vector<string> vWords;
|
||||
ParseString(strLine, ' ', vWords);
|
||||
if (vWords.size() < 4)
|
||||
return false;
|
||||
|
||||
string str = vWords[3];
|
||||
if (str.rfind("@") == string::npos)
|
||||
return false;
|
||||
string strHost = str.substr(str.rfind("@")+1);
|
||||
|
||||
// Hybrid IRC used by lfnet always returns IP when you userhost yourself,
|
||||
// but in case another IRC is ever used this should work.
|
||||
printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
|
||||
CNetAddr addr(strHost, true);
|
||||
if (!addr.IsValid())
|
||||
return false;
|
||||
ipRet = addr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ThreadIRCSeed(void* parg)
|
||||
{
|
||||
// Make this thread recognisable as the IRC seeding thread
|
||||
RenameThread("Triangles-ircseed");
|
||||
|
||||
try
|
||||
{
|
||||
ThreadIRCSeed2(parg);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
PrintExceptionContinue(&e, "ThreadIRCSeed()");
|
||||
} catch (...) {
|
||||
PrintExceptionContinue(NULL, "ThreadIRCSeed()");
|
||||
}
|
||||
printf("ThreadIRCSeed exited\n");
|
||||
}
|
||||
|
||||
void ThreadIRCSeed2(void* parg)
|
||||
{
|
||||
// Don't connect to IRC if we won't use IPv4 connections.
|
||||
if (IsLimited(NET_IPV4))
|
||||
return;
|
||||
|
||||
// ... or if we won't make outbound connections and won't accept inbound ones.
|
||||
if (mapArgs.count("-connect") && fNoListen)
|
||||
return;
|
||||
|
||||
// ... or if IRC is not enabled.
|
||||
if (!GetBoolArg("-irc", false))
|
||||
return;
|
||||
|
||||
printf("ThreadIRCSeed started\n");
|
||||
int nErrorWait = 10;
|
||||
int nRetryWait = 10;
|
||||
int nNameRetry = 0;
|
||||
|
||||
while (!fShutdown)
|
||||
{
|
||||
CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
|
||||
|
||||
CService addrIRC("irc.lfnet.org", 6667, true);
|
||||
if (addrIRC.IsValid())
|
||||
addrConnect = addrIRC;
|
||||
|
||||
SOCKET hSocket;
|
||||
if (!ConnectSocket(addrConnect, hSocket))
|
||||
{
|
||||
printf("IRC connect failed\n");
|
||||
nErrorWait = nErrorWait * 11 / 10;
|
||||
if (Wait(nErrorWait += 60))
|
||||
continue;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
|
||||
{
|
||||
closesocket(hSocket);
|
||||
hSocket = INVALID_SOCKET;
|
||||
nErrorWait = nErrorWait * 11 / 10;
|
||||
if (Wait(nErrorWait += 60))
|
||||
continue;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
|
||||
CService addrLocal;
|
||||
string strMyName;
|
||||
// Don't use our IP as our nick if we're not listening
|
||||
// or if it keeps failing because the nick is already in use.
|
||||
if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
|
||||
strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
|
||||
if (strMyName == "")
|
||||
strMyName = strprintf("x%" PRIu64 "", GetRand(1000000000));
|
||||
|
||||
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
|
||||
Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
|
||||
|
||||
int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
|
||||
if (nRet != 1)
|
||||
{
|
||||
closesocket(hSocket);
|
||||
hSocket = INVALID_SOCKET;
|
||||
if (nRet == 2)
|
||||
{
|
||||
printf("IRC name already in use\n");
|
||||
nNameRetry++;
|
||||
Wait(10);
|
||||
continue;
|
||||
}
|
||||
nErrorWait = nErrorWait * 11 / 10;
|
||||
if (Wait(nErrorWait += 60))
|
||||
continue;
|
||||
else
|
||||
return;
|
||||
}
|
||||
nNameRetry = 0;
|
||||
MilliSleep(500);
|
||||
|
||||
// Get our external IP from the IRC server and re-nick before joining the channel
|
||||
CNetAddr addrFromIRC;
|
||||
if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
|
||||
{
|
||||
printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
|
||||
// Don't use our IP as our nick if we're not listening
|
||||
if (!fNoListen && addrFromIRC.IsRoutable())
|
||||
{
|
||||
// IRC lets you to re-nick
|
||||
AddLocal(addrFromIRC, LOCAL_IRC);
|
||||
strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
|
||||
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (fTestNet) {
|
||||
Send(hSocket, "JOIN #TrianglesTEST\r");
|
||||
Send(hSocket, "WHO #TrianglesTEST\r");
|
||||
} else {
|
||||
// randomly join
|
||||
// int channel_number = GetRandInt(5);
|
||||
|
||||
// Channel number is always 0 for initial release
|
||||
int channel_number = 0;
|
||||
Send(hSocket, strprintf("JOIN #Triangles%02d\r", channel_number).c_str());
|
||||
Send(hSocket, strprintf("WHO #Triangles%02d\r", channel_number).c_str());
|
||||
}
|
||||
|
||||
int64_t nStart = GetTime();
|
||||
string strLine;
|
||||
strLine.reserve(10000);
|
||||
while (!fShutdown && RecvLineIRC(hSocket, strLine))
|
||||
{
|
||||
if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
|
||||
continue;
|
||||
|
||||
vector<string> vWords;
|
||||
ParseString(strLine, ' ', vWords);
|
||||
if (vWords.size() < 2)
|
||||
continue;
|
||||
|
||||
char pszName[10000];
|
||||
pszName[0] = '\0';
|
||||
|
||||
if (vWords[1] == "352" && vWords.size() >= 8)
|
||||
{
|
||||
// index 7 is limited to 16 characters
|
||||
// could get full length name at index 10, but would be different from join messages
|
||||
strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
|
||||
printf("IRC got who\n");
|
||||
}
|
||||
|
||||
if (vWords[1] == "JOIN" && vWords[0].size() > 1)
|
||||
{
|
||||
// :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
|
||||
strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
|
||||
if (strchr(pszName, '!'))
|
||||
*strchr(pszName, '!') = '\0';
|
||||
printf("IRC got join\n");
|
||||
}
|
||||
|
||||
if (pszName[0] == 'u')
|
||||
{
|
||||
CAddress addr;
|
||||
if (DecodeAddress(pszName, addr))
|
||||
{
|
||||
addr.nTime = GetAdjustedTime();
|
||||
if (addrman.Add(addr, addrConnect, 51 * 60))
|
||||
printf("IRC got new address: %s\n", addr.ToString().c_str());
|
||||
nGotIRCAddresses++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("IRC decode failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
closesocket(hSocket);
|
||||
hSocket = INVALID_SOCKET;
|
||||
|
||||
if (GetTime() - nStart > 20 * 60)
|
||||
{
|
||||
nErrorWait /= 3;
|
||||
nRetryWait /= 3;
|
||||
}
|
||||
|
||||
nRetryWait = nRetryWait * 11 / 10;
|
||||
if (!Wait(nRetryWait += 60))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef TEST
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
WSADATA wsadata;
|
||||
if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
|
||||
{
|
||||
printf("Error at WSAStartup()\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ThreadIRCSeed(NULL);
|
||||
|
||||
WSACleanup();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
#ifndef TRIANGLES_IRC_H
|
||||
#define TRIANGLES_IRC_H
|
||||
|
||||
void ThreadIRCSeed(void* parg);
|
||||
|
||||
extern int nGotIRCAddresses;
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -386,7 +386,7 @@ bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hash
|
||||
const CTxIn& txin = tx.vin[0];
|
||||
|
||||
// First try finding the previous transaction in database
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
CTransaction txPrev;
|
||||
CTxIndex txindex;
|
||||
if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
|
||||
|
||||
+272
-397
@@ -1,213 +1,33 @@
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
|
||||
#include <openssl/ecdsa.h>
|
||||
#include <openssl/obj_mac.h>
|
||||
#include <openssl/crypto.h> // OPENSSL_cleanse for secure wipe of secret bytes
|
||||
#include <openssl/rand.h> // RAND_bytes for new-key entropy
|
||||
|
||||
#include "crypto_ecdsa.h"
|
||||
#include "key.h"
|
||||
|
||||
// Generate a private key from just the secret parameter
|
||||
int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Order-of-generator constants (still used by CheckSignatureElement, the only
|
||||
// caller into the BigEndian comparison helper below). Kept here so the file
|
||||
// remains self-contained.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
namespace {
|
||||
|
||||
int CompareBigEndian(const unsigned char* c1, std::size_t c1len,
|
||||
const unsigned char* c2, std::size_t c2len)
|
||||
{
|
||||
int ok = 0;
|
||||
BN_CTX *ctx = NULL;
|
||||
EC_POINT *pub_key = NULL;
|
||||
|
||||
if (!eckey) return 0;
|
||||
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
|
||||
if ((ctx = BN_CTX_new()) == NULL)
|
||||
goto err;
|
||||
|
||||
pub_key = EC_POINT_new(group);
|
||||
|
||||
if (pub_key == NULL)
|
||||
goto err;
|
||||
|
||||
if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
|
||||
goto err;
|
||||
|
||||
EC_KEY_set_private_key(eckey,priv_key);
|
||||
EC_KEY_set_public_key(eckey,pub_key);
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
|
||||
if (pub_key)
|
||||
EC_POINT_free(pub_key);
|
||||
if (ctx != NULL)
|
||||
BN_CTX_free(ctx);
|
||||
|
||||
return(ok);
|
||||
}
|
||||
|
||||
// Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
|
||||
// recid selects which key is recovered
|
||||
// if check is non-zero, additional checks are performed
|
||||
int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
|
||||
{
|
||||
if (!eckey) return 0;
|
||||
|
||||
int ret = 0;
|
||||
BN_CTX *ctx = NULL;
|
||||
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *e = NULL;
|
||||
BIGNUM *order = NULL;
|
||||
BIGNUM *sor = NULL;
|
||||
BIGNUM *eor = NULL;
|
||||
BIGNUM *field = NULL;
|
||||
EC_POINT *R = NULL;
|
||||
EC_POINT *O = NULL;
|
||||
EC_POINT *Q = NULL;
|
||||
BIGNUM *rr = NULL;
|
||||
BIGNUM *zero = NULL;
|
||||
int n = 0;
|
||||
int i = recid / 2;
|
||||
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
|
||||
BN_CTX_start(ctx);
|
||||
const BIGNUM *sig_r, *sig_s;
|
||||
ECDSA_SIG_get0(ecsig, &sig_r, &sig_s);
|
||||
order = BN_CTX_get(ctx);
|
||||
if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
|
||||
x = BN_CTX_get(ctx);
|
||||
if (!BN_copy(x, order)) { ret=-1; goto err; }
|
||||
if (!BN_mul_word(x, i)) { ret=-1; goto err; }
|
||||
if (!BN_add(x, x, sig_r)) { ret=-1; goto err; }
|
||||
field = BN_CTX_get(ctx);
|
||||
if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
|
||||
if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
|
||||
if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
|
||||
if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
|
||||
if (check)
|
||||
{
|
||||
if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
|
||||
if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
|
||||
if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
|
||||
}
|
||||
if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
|
||||
n = EC_GROUP_get_degree(group);
|
||||
e = BN_CTX_get(ctx);
|
||||
if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
|
||||
if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
|
||||
zero = BN_CTX_get(ctx);
|
||||
BN_zero(zero);
|
||||
if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
|
||||
rr = BN_CTX_get(ctx);
|
||||
if (!BN_mod_inverse(rr, sig_r, order, ctx)) { ret=-1; goto err; }
|
||||
sor = BN_CTX_get(ctx);
|
||||
if (!BN_mod_mul(sor, sig_s, rr, order, ctx)) { ret=-1; goto err; }
|
||||
eor = BN_CTX_get(ctx);
|
||||
if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
|
||||
if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
|
||||
if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
if (ctx) {
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
}
|
||||
if (R != NULL) EC_POINT_free(R);
|
||||
if (O != NULL) EC_POINT_free(O);
|
||||
if (Q != NULL) EC_POINT_free(Q);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CKey::SetCompressedPubKey()
|
||||
{
|
||||
EC_KEY_set_conv_form(pkey, POINT_CONVERSION_COMPRESSED);
|
||||
fCompressedPubKey = true;
|
||||
}
|
||||
|
||||
void CKey::SetUnCompressedPubKey()
|
||||
{
|
||||
EC_KEY_set_conv_form(pkey, POINT_CONVERSION_UNCOMPRESSED);
|
||||
fCompressedPubKey = false;
|
||||
}
|
||||
|
||||
EC_KEY* CKey::GetECKey()
|
||||
{
|
||||
return pkey;
|
||||
}
|
||||
|
||||
void CKey::Reset()
|
||||
{
|
||||
fCompressedPubKey = false;
|
||||
if (pkey != NULL)
|
||||
EC_KEY_free(pkey);
|
||||
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
|
||||
if (pkey == NULL)
|
||||
throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
|
||||
fSet = false;
|
||||
}
|
||||
|
||||
CKey::CKey()
|
||||
{
|
||||
pkey = NULL;
|
||||
Reset();
|
||||
}
|
||||
|
||||
CKey::CKey(const CKey& b)
|
||||
{
|
||||
pkey = EC_KEY_dup(b.pkey);
|
||||
if (pkey == NULL)
|
||||
throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed");
|
||||
fSet = b.fSet;
|
||||
}
|
||||
|
||||
CKey& CKey::operator=(const CKey& b)
|
||||
{
|
||||
if (!EC_KEY_copy(pkey, b.pkey))
|
||||
throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed");
|
||||
fSet = b.fSet;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
CKey::~CKey()
|
||||
{
|
||||
EC_KEY_free(pkey);
|
||||
}
|
||||
|
||||
bool CKey::IsNull() const
|
||||
{
|
||||
return !fSet;
|
||||
}
|
||||
|
||||
bool CKey::IsCompressed() const
|
||||
{
|
||||
return fCompressedPubKey;
|
||||
}
|
||||
|
||||
int CompareBigEndian(const unsigned char *c1, size_t c1len, const unsigned char *c2, size_t c2len) {
|
||||
while (c1len > c2len) {
|
||||
if (*c1)
|
||||
return 1;
|
||||
c1++;
|
||||
c1len--;
|
||||
}
|
||||
while (c2len > c1len) {
|
||||
if (*c2)
|
||||
return -1;
|
||||
c2++;
|
||||
c2len--;
|
||||
}
|
||||
while (c1len > c2len) { if (*c1) return 1; c1++; c1len--; }
|
||||
while (c2len > c1len) { if (*c2) return -1; c2++; c2len--; }
|
||||
while (c1len > 0) {
|
||||
if (*c1 > *c2)
|
||||
return 1;
|
||||
if (*c2 > *c1)
|
||||
return -1;
|
||||
c1++;
|
||||
c2++;
|
||||
c1len--;
|
||||
if (*c1 > *c2) return 1;
|
||||
if (*c2 > *c1) return -1;
|
||||
c1++; c2++; c1len--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -228,277 +48,332 @@ const unsigned char vchMaxModHalfOrder[32] = {
|
||||
0xDF,0xE9,0x2F,0x46,0x68,0x1B,0x20,0xA0
|
||||
};
|
||||
|
||||
const unsigned char vchZero[0] = {};
|
||||
const unsigned char vchZero[1] = { 0 };
|
||||
|
||||
bool CKey::CheckSignatureElement(const unsigned char *vch, int len, bool half) {
|
||||
return CompareBigEndian(vch, len, vchZero, 0) > 0 &&
|
||||
CompareBigEndian(vch, len, half ? vchMaxModHalfOrder : vchMaxModOrder, 32) <= 0;
|
||||
} // namespace
|
||||
|
||||
bool CKey::CheckSignatureElement(const unsigned char* vchIn, int len, bool half)
|
||||
{
|
||||
return CompareBigEndian(vchIn, len, vchZero, 0) > 0 &&
|
||||
CompareBigEndian(vchIn, len, half ? vchMaxModHalfOrder : vchMaxModOrder, 32) <= 0;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Lifecycle
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
void CKey::Reset()
|
||||
{
|
||||
OPENSSL_cleanse(vch, sizeof(vch));
|
||||
vchPubKey.clear();
|
||||
fSet = false;
|
||||
fHavePrivKey = false;
|
||||
fCompressedPubKey = false;
|
||||
}
|
||||
|
||||
CKey::CKey()
|
||||
{
|
||||
std::memset(vch, 0, sizeof(vch));
|
||||
vchPubKey.clear();
|
||||
fSet = false;
|
||||
fHavePrivKey = false;
|
||||
fCompressedPubKey = false;
|
||||
}
|
||||
|
||||
CKey::CKey(const CKey& b)
|
||||
{
|
||||
*this = b;
|
||||
}
|
||||
|
||||
CKey& CKey::operator=(const CKey& b)
|
||||
{
|
||||
if (this == &b) return *this;
|
||||
std::memcpy(vch, b.vch, sizeof(vch));
|
||||
vchPubKey = b.vchPubKey;
|
||||
fSet = b.fSet;
|
||||
fHavePrivKey = b.fHavePrivKey;
|
||||
fCompressedPubKey = b.fCompressedPubKey;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CKey::~CKey()
|
||||
{
|
||||
OPENSSL_cleanse(vch, sizeof(vch));
|
||||
}
|
||||
|
||||
bool CKey::IsNull() const { return !fSet; }
|
||||
bool CKey::IsCompressed() const { return fCompressedPubKey; }
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Compression toggle
|
||||
//
|
||||
// In the new model the pubkey is always cached at the current compression. If
|
||||
// we hold the private key we can re-derive trivially; if we only hold a public
|
||||
// key, callers don't toggle compression in practice in this codebase, so we
|
||||
// just flip the flag and rely on the next SetPubKey/SetSecret to refresh the
|
||||
// cache.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
void CKey::SetCompressedPubKey()
|
||||
{
|
||||
if (fCompressedPubKey) return;
|
||||
fCompressedPubKey = true;
|
||||
if (fSet && fHavePrivKey) {
|
||||
std::size_t len = 33;
|
||||
vchPubKey.resize(len);
|
||||
if (!ECDSA_pubkey_from_privkey_secp256k1(&vchPubKey[0], &len, vch, /*fCompressed=*/true)) {
|
||||
Reset();
|
||||
return;
|
||||
}
|
||||
vchPubKey.resize(len);
|
||||
}
|
||||
}
|
||||
|
||||
void CKey::SetUnCompressedPubKey()
|
||||
{
|
||||
if (!fCompressedPubKey && fSet) return;
|
||||
fCompressedPubKey = false;
|
||||
if (fSet && fHavePrivKey) {
|
||||
std::size_t len = 65;
|
||||
vchPubKey.resize(len);
|
||||
if (!ECDSA_pubkey_from_privkey_secp256k1(&vchPubKey[0], &len, vch, /*fCompressed=*/false)) {
|
||||
Reset();
|
||||
return;
|
||||
}
|
||||
vchPubKey.resize(len);
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Key generation / load / store
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
void CKey::MakeNewKey(bool fCompressed)
|
||||
{
|
||||
if (!EC_KEY_generate_key(pkey))
|
||||
throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
|
||||
if (fCompressed)
|
||||
SetCompressedPubKey();
|
||||
fSet = true;
|
||||
// Sample 32 bytes of entropy and reject any that fall outside (0, n).
|
||||
// Probability of needing a retry is ~2^-128.
|
||||
do {
|
||||
if (RAND_bytes(vch, sizeof(vch)) != 1)
|
||||
throw key_error("CKey::MakeNewKey() : RAND_bytes failed");
|
||||
} while (!ECDSA_seckey_verify_secp256k1(vch));
|
||||
|
||||
fSet = true;
|
||||
fHavePrivKey = true;
|
||||
fCompressedPubKey = fCompressed;
|
||||
|
||||
std::size_t len = fCompressed ? 33 : 65;
|
||||
vchPubKey.resize(len);
|
||||
if (!ECDSA_pubkey_from_privkey_secp256k1(&vchPubKey[0], &len, vch, fCompressed)) {
|
||||
Reset();
|
||||
throw key_error("CKey::MakeNewKey() : failed to derive public key");
|
||||
}
|
||||
vchPubKey.resize(len);
|
||||
}
|
||||
|
||||
bool CKey::SetPrivKey(const CPrivKey& vchPrivKey)
|
||||
{
|
||||
const unsigned char* pbegin = &vchPrivKey[0];
|
||||
if (d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
|
||||
{
|
||||
// In testing, d2i_ECPrivateKey can return true
|
||||
// but fill in pkey with a key that fails
|
||||
// EC_KEY_check_key, so:
|
||||
if (EC_KEY_check_key(pkey))
|
||||
{
|
||||
fSet = true;
|
||||
return true;
|
||||
}
|
||||
unsigned char raw[32];
|
||||
if (!ECDSA_privkey_import_der_secp256k1(raw, &vchPrivKey[0], vchPrivKey.size())) {
|
||||
OPENSSL_cleanse(raw, sizeof(raw));
|
||||
Reset();
|
||||
return false;
|
||||
}
|
||||
// If vchPrivKey data is bad d2i_ECPrivateKey() can
|
||||
// leave pkey in a state where calling EC_KEY_free()
|
||||
// crashes. To avoid that, set pkey to NULL and
|
||||
// leak the memory (a leak is better than a crash)
|
||||
pkey = NULL;
|
||||
Reset();
|
||||
return false;
|
||||
|
||||
// Carry the compressed flag out of the DER blob. The two valid sizes
|
||||
// produced by ECDSA_privkey_export_der_secp256k1 are 214 (compressed) and
|
||||
// 279 (uncompressed); foreign DER blobs are best-effort but those two
|
||||
// cover every record this codebase has ever written.
|
||||
bool fCompressed = (vchPrivKey.size() == 214);
|
||||
|
||||
CSecret secret(raw, raw + 32);
|
||||
OPENSSL_cleanse(raw, sizeof(raw));
|
||||
return SetSecret(secret, fCompressed);
|
||||
}
|
||||
|
||||
bool CKey::SetSecret(const CSecret& vchSecret, bool fCompressed)
|
||||
{
|
||||
EC_KEY_free(pkey);
|
||||
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
|
||||
if (pkey == NULL)
|
||||
throw key_error("CKey::SetSecret() : EC_KEY_new_by_curve_name failed");
|
||||
if (vchSecret.size() != 32)
|
||||
throw key_error("CKey::SetSecret() : secret must be 32 bytes");
|
||||
BIGNUM *bn = BN_bin2bn(&vchSecret[0],32,BN_new());
|
||||
if (bn == NULL)
|
||||
throw key_error("CKey::SetSecret() : BN_bin2bn failed");
|
||||
if (!EC_KEY_regenerate_key(pkey,bn))
|
||||
{
|
||||
BN_clear_free(bn);
|
||||
throw key_error("CKey::SetSecret() : EC_KEY_regenerate_key failed");
|
||||
if (!ECDSA_seckey_verify_secp256k1(&vchSecret[0]))
|
||||
throw key_error("CKey::SetSecret() : secret is not a valid scalar");
|
||||
|
||||
std::memcpy(vch, &vchSecret[0], 32);
|
||||
fSet = true;
|
||||
fHavePrivKey = true;
|
||||
// Preserve sticky-compression behaviour from the OpenSSL implementation:
|
||||
// if either the explicit argument or the previously-set flag is true,
|
||||
// the result is compressed.
|
||||
bool fComp = fCompressed || fCompressedPubKey;
|
||||
fCompressedPubKey = fComp;
|
||||
|
||||
std::size_t len = fComp ? 33 : 65;
|
||||
vchPubKey.resize(len);
|
||||
if (!ECDSA_pubkey_from_privkey_secp256k1(&vchPubKey[0], &len, vch, fComp)) {
|
||||
Reset();
|
||||
return false;
|
||||
}
|
||||
BN_clear_free(bn);
|
||||
fSet = true;
|
||||
if (fCompressed || fCompressedPubKey)
|
||||
SetCompressedPubKey();
|
||||
vchPubKey.resize(len);
|
||||
return true;
|
||||
}
|
||||
|
||||
CSecret CKey::GetSecret(bool &fCompressed) const
|
||||
CSecret CKey::GetSecret(bool& fCompressed) const
|
||||
{
|
||||
CSecret vchRet;
|
||||
vchRet.resize(32);
|
||||
const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
|
||||
int nBytes = BN_num_bytes(bn);
|
||||
if (bn == NULL)
|
||||
throw key_error("CKey::GetSecret() : EC_KEY_get0_private_key failed");
|
||||
int n=BN_bn2bin(bn,&vchRet[32 - nBytes]);
|
||||
if (n != nBytes)
|
||||
throw key_error("CKey::GetSecret(): BN_bn2bin failed");
|
||||
if (!fSet || !fHavePrivKey)
|
||||
throw key_error("CKey::GetSecret() : key is not set or has no private component");
|
||||
CSecret out(vch, vch + 32);
|
||||
fCompressed = fCompressedPubKey;
|
||||
return vchRet;
|
||||
return out;
|
||||
}
|
||||
|
||||
CPrivKey CKey::GetPrivKey() const
|
||||
{
|
||||
int nSize = i2d_ECPrivateKey(pkey, NULL);
|
||||
if (!nSize)
|
||||
throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed");
|
||||
CPrivKey vchPrivKey(nSize, 0);
|
||||
unsigned char* pbegin = &vchPrivKey[0];
|
||||
if (i2d_ECPrivateKey(pkey, &pbegin) != nSize)
|
||||
throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size");
|
||||
return vchPrivKey;
|
||||
if (!fSet || !fHavePrivKey)
|
||||
throw key_error("CKey::GetPrivKey() : key is not set or has no private component");
|
||||
|
||||
// Max possible output: 279 bytes (uncompressed).
|
||||
CPrivKey out(279, 0);
|
||||
std::size_t out_len = out.size();
|
||||
if (!ECDSA_privkey_export_der_secp256k1(&out[0], &out_len, vch, fCompressedPubKey))
|
||||
throw key_error("CKey::GetPrivKey() : DER export failed");
|
||||
out.resize(out_len);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool CKey::SetPubKey(const CPubKey& vchPubKey)
|
||||
bool CKey::SetPubKey(const CPubKey& cpub)
|
||||
{
|
||||
const unsigned char* pbegin = &vchPubKey.vchPubKey[0];
|
||||
if (o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.vchPubKey.size()))
|
||||
{
|
||||
fSet = true;
|
||||
if (vchPubKey.vchPubKey.size() == 33)
|
||||
SetCompressedPubKey();
|
||||
return true;
|
||||
const std::vector<unsigned char>& vchPub = cpub.vchPubKey;
|
||||
if (vchPub.size() != 33 && vchPub.size() != 65) {
|
||||
Reset();
|
||||
return false;
|
||||
}
|
||||
pkey = NULL;
|
||||
Reset();
|
||||
return false;
|
||||
if (!ECDSA_pubkey_verify_secp256k1(&vchPub[0], vchPub.size())) {
|
||||
Reset();
|
||||
return false;
|
||||
}
|
||||
vchPubKey = vchPub;
|
||||
fSet = true;
|
||||
fHavePrivKey = false;
|
||||
fCompressedPubKey = (vchPub.size() == 33);
|
||||
return true;
|
||||
}
|
||||
|
||||
CPubKey CKey::GetPubKey() const
|
||||
{
|
||||
int nSize = i2o_ECPublicKey(pkey, NULL);
|
||||
if (!nSize)
|
||||
throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
|
||||
std::vector<unsigned char> vchPubKey(nSize, 0);
|
||||
unsigned char* pbegin = &vchPubKey[0];
|
||||
if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
|
||||
throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
|
||||
return CPubKey(vchPubKey);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Sign / verify / recover (all delegate to crypto_ecdsa wrappers)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
bool CKey::Sign(uint256 hash, std::vector<unsigned char>& vchSig)
|
||||
{
|
||||
vchSig.clear();
|
||||
ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
|
||||
if (sig == NULL)
|
||||
if (!fSet || !fHavePrivKey) return false;
|
||||
|
||||
// libsecp256k1's max DER output is 72 bytes; allocate that and shrink.
|
||||
vchSig.resize(72);
|
||||
std::size_t sig_len = vchSig.size();
|
||||
if (!ECDSA_sign_secp256k1(&vchSig[0], &sig_len,
|
||||
reinterpret_cast<const unsigned char*>(&hash),
|
||||
vch))
|
||||
{
|
||||
vchSig.clear();
|
||||
return false;
|
||||
BN_CTX *ctx = BN_CTX_new();
|
||||
BN_CTX_start(ctx);
|
||||
const EC_GROUP *group = EC_KEY_get0_group(pkey);
|
||||
BIGNUM *order = BN_CTX_get(ctx);
|
||||
BIGNUM *halforder = BN_CTX_get(ctx);
|
||||
EC_GROUP_get_order(group, order, ctx);
|
||||
BN_rshift1(halforder, order);
|
||||
const BIGNUM *sig_r, *sig_s;
|
||||
ECDSA_SIG_get0(sig, &sig_r, &sig_s);
|
||||
if (BN_cmp(sig_s, halforder) > 0) {
|
||||
// enforce low S values, by negating the value (modulo the order) if above order/2.
|
||||
BIGNUM *new_s = BN_new();
|
||||
BN_sub(new_s, order, sig_s);
|
||||
BIGNUM *dup_r = BN_dup(sig_r);
|
||||
ECDSA_SIG_set0(sig, dup_r, new_s);
|
||||
}
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
unsigned int nSize = ECDSA_size(pkey);
|
||||
vchSig.resize(nSize); // Make sure it is big enough
|
||||
unsigned char *pos = &vchSig[0];
|
||||
nSize = i2d_ECDSA_SIG(sig, &pos);
|
||||
ECDSA_SIG_free(sig);
|
||||
vchSig.resize(nSize); // Shrink to fit actual size
|
||||
vchSig.resize(sig_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
// create a compact signature (65 bytes), which allows reconstructing the used public key
|
||||
// The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
|
||||
// The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
|
||||
// 0x1D = second key with even y, 0x1E = second key with odd y
|
||||
// Compact signature (65 bytes): one header byte (encoding recid + compression)
|
||||
// followed by 32-byte r and 32-byte s.
|
||||
bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
|
||||
{
|
||||
bool fOk = false;
|
||||
ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
|
||||
if (sig==NULL)
|
||||
return false;
|
||||
vchSig.clear();
|
||||
vchSig.resize(65,0);
|
||||
const BIGNUM *sig_r, *sig_s;
|
||||
ECDSA_SIG_get0(sig, &sig_r, &sig_s);
|
||||
int nBitsR = BN_num_bits(sig_r);
|
||||
int nBitsS = BN_num_bits(sig_s);
|
||||
if (nBitsR <= 256 && nBitsS <= 256)
|
||||
if (!fSet || !fHavePrivKey) return false;
|
||||
|
||||
vchSig.resize(65, 0);
|
||||
if (!ECDSA_sign_compact_secp256k1(&vchSig[0],
|
||||
reinterpret_cast<const unsigned char*>(&hash),
|
||||
vch,
|
||||
fCompressedPubKey))
|
||||
{
|
||||
int nRecId = -1;
|
||||
for (int i=0; i<4; i++)
|
||||
{
|
||||
CKey keyRec;
|
||||
keyRec.fSet = true;
|
||||
if (fCompressedPubKey)
|
||||
keyRec.SetCompressedPubKey();
|
||||
if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1)
|
||||
if (keyRec.GetPubKey() == this->GetPubKey())
|
||||
{
|
||||
nRecId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nRecId == -1)
|
||||
{
|
||||
ECDSA_SIG_free(sig);
|
||||
throw key_error("CKey::SignCompact() : unable to construct recoverable key");
|
||||
}
|
||||
|
||||
vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0);
|
||||
BN_bn2bin(sig_r,&vchSig[33-(nBitsR+7)/8]);
|
||||
BN_bn2bin(sig_s,&vchSig[65-(nBitsS+7)/8]);
|
||||
fOk = true;
|
||||
vchSig.clear();
|
||||
return false;
|
||||
}
|
||||
ECDSA_SIG_free(sig);
|
||||
return fOk;
|
||||
return true;
|
||||
}
|
||||
|
||||
// reconstruct public key from a compact signature
|
||||
// This is only slightly more CPU intensive than just verifying it.
|
||||
// If this function succeeds, the recovered public key is guaranteed to be valid
|
||||
// (the signature is a valid signature of the given data for that key)
|
||||
bool CKey::SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig)
|
||||
{
|
||||
if (vchSig.size() != 65)
|
||||
return false;
|
||||
if (vchSig.size() != 65) return false;
|
||||
int nV = vchSig[0];
|
||||
if (nV<27 || nV>=35)
|
||||
return false;
|
||||
ECDSA_SIG *sig = ECDSA_SIG_new();
|
||||
BIGNUM *sig_r = BN_bin2bn(&vchSig[1],32,NULL);
|
||||
BIGNUM *sig_s = BN_bin2bn(&vchSig[33],32,NULL);
|
||||
ECDSA_SIG_set0(sig, sig_r, sig_s);
|
||||
if (nV < 27 || nV >= 35) return false;
|
||||
|
||||
EC_KEY_free(pkey);
|
||||
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
|
||||
if (nV >= 31)
|
||||
{
|
||||
SetCompressedPubKey();
|
||||
nV -= 4;
|
||||
}
|
||||
if (ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), nV - 27, 0) == 1)
|
||||
{
|
||||
fSet = true;
|
||||
ECDSA_SIG_free(sig);
|
||||
return true;
|
||||
}
|
||||
ECDSA_SIG_free(sig);
|
||||
return false;
|
||||
unsigned char pubkey[65];
|
||||
std::size_t pubkey_len = 0;
|
||||
if (!ECDSA_recover_compact_secp256k1(pubkey, &pubkey_len,
|
||||
reinterpret_cast<const unsigned char*>(&hash),
|
||||
&vchSig[0]))
|
||||
return false;
|
||||
|
||||
std::vector<unsigned char> vchPub(pubkey, pubkey + pubkey_len);
|
||||
return SetPubKey(CPubKey(vchPub));
|
||||
}
|
||||
|
||||
bool CKey::Verify(uint256 hash, const std::vector<unsigned char>& vchSig)
|
||||
{
|
||||
// -1 = error, 0 = bad sig, 1 = good
|
||||
if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
|
||||
return false;
|
||||
if (vchSig.empty() || !fSet) return false;
|
||||
|
||||
return true;
|
||||
return ECDSA_verify_secp256k1(
|
||||
reinterpret_cast<const unsigned char*>(&hash),
|
||||
&vchSig[0], vchSig.size(),
|
||||
&vchPubKey[0], vchPubKey.size());
|
||||
}
|
||||
|
||||
bool CKey::VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig)
|
||||
{
|
||||
CKey key;
|
||||
if (!key.SetCompactSignature(hash, vchSig))
|
||||
return false;
|
||||
if (GetPubKey() != key.GetPubKey())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
if (!key.SetCompactSignature(hash, vchSig)) return false;
|
||||
return GetPubKey() == key.GetPubKey();
|
||||
}
|
||||
|
||||
bool CKey::IsValid()
|
||||
{
|
||||
if (!fSet)
|
||||
return false;
|
||||
if (!fSet) return false;
|
||||
|
||||
if (!EC_KEY_check_key(pkey))
|
||||
return false;
|
||||
if (fHavePrivKey) {
|
||||
if (!ECDSA_seckey_verify_secp256k1(vch)) return false;
|
||||
|
||||
bool fCompr;
|
||||
CSecret secret = GetSecret(fCompr);
|
||||
CKey key2;
|
||||
key2.SetSecret(secret, fCompr);
|
||||
return GetPubKey() == key2.GetPubKey();
|
||||
// Re-derive the pubkey and check it matches the cache. This is the
|
||||
// libsecp256k1 equivalent of OpenSSL's "consistency between priv and
|
||||
// pub" check the original implementation performed.
|
||||
unsigned char rederived[65];
|
||||
std::size_t rederived_len = 0;
|
||||
if (!ECDSA_pubkey_from_privkey_secp256k1(rederived, &rederived_len, vch, fCompressedPubKey))
|
||||
return false;
|
||||
if (rederived_len != vchPubKey.size()) return false;
|
||||
return std::memcmp(rederived, &vchPubKey[0], rederived_len) == 0;
|
||||
}
|
||||
|
||||
return ECDSA_pubkey_verify_secp256k1(&vchPubKey[0], vchPubKey.size());
|
||||
}
|
||||
|
||||
bool ECC_InitSanityCheck() {
|
||||
EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
|
||||
if(pkey == NULL)
|
||||
return false;
|
||||
EC_KEY_free(pkey);
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Startup smoke test for the cryptography backend.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
// TODO Is there more EC functionality that could be missing?
|
||||
bool ECC_InitSanityCheck()
|
||||
{
|
||||
// Verify that libsecp256k1 can validate a trivially-known good secret
|
||||
// (the scalar 1) and reject zero. If either of these fails, the linked
|
||||
// library is broken and we should refuse to start.
|
||||
static const unsigned char one[32] = {
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
|
||||
};
|
||||
static const unsigned char zero[32] = {0};
|
||||
if (!ECDSA_seckey_verify_secp256k1(one)) return false;
|
||||
if ( ECDSA_seckey_verify_secp256k1(zero)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
#include "uint256.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <openssl/ec.h> // for EC_KEY definition
|
||||
|
||||
// secp160k1
|
||||
// const unsigned int PRIVATE_KEY_SIZE = 192;
|
||||
// const unsigned int PUBLIC_KEY_SIZE = 41;
|
||||
@@ -105,20 +103,22 @@ typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
|
||||
// CSecret is a serialization of just the secret parameter (32 bytes)
|
||||
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
|
||||
|
||||
/** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
|
||||
/** An encapsulated secp256k1 elliptic-curve key (public and/or private). */
|
||||
class CKey
|
||||
{
|
||||
protected:
|
||||
EC_KEY* pkey;
|
||||
// 32-byte private scalar. Valid iff fSet && fHavePrivKey.
|
||||
unsigned char vch[32];
|
||||
// Cached serialized public key (33 or 65 bytes). Valid iff fSet.
|
||||
std::vector<unsigned char> vchPubKey;
|
||||
bool fSet;
|
||||
bool fCompressedPubKey;
|
||||
bool fHavePrivKey;
|
||||
|
||||
public:
|
||||
void SetCompressedPubKey();
|
||||
void SetUnCompressedPubKey();
|
||||
|
||||
EC_KEY* GetECKey();
|
||||
|
||||
|
||||
void Reset();
|
||||
|
||||
CKey();
|
||||
|
||||
+2
-2
@@ -6,8 +6,8 @@
|
||||
#define TRIANGLES_KEYSTORE_H
|
||||
|
||||
#include "crypter.h"
|
||||
#include "util_signal.h"
|
||||
#include "sync.h"
|
||||
#include <boost/signals2/signal.hpp>
|
||||
|
||||
class CScript;
|
||||
|
||||
@@ -177,7 +177,7 @@ public:
|
||||
/* Wallet status (encrypted, locked) changed.
|
||||
* Note: Called without locks held.
|
||||
*/
|
||||
boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
|
||||
CSignal<void(CCryptoKeyStore*)> NotifyStatusChanged;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+839
-254
File diff suppressed because it is too large
Load Diff
+138
-17
@@ -12,6 +12,7 @@
|
||||
#include "scrypt.h"
|
||||
#include "hashblock.h"
|
||||
#include "checkqueue.h"
|
||||
#include "sigcache.h"
|
||||
|
||||
#include <list>
|
||||
|
||||
@@ -37,8 +38,8 @@ static const unsigned int MAX_BLOCK_SIZE = 1000000;
|
||||
static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2;
|
||||
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
|
||||
static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
|
||||
static const unsigned int MAX_ORPHAN_BLOCKS = 2000;
|
||||
static const unsigned int MAX_ORPHAN_BLOCKS_IBD = 4000;
|
||||
static const unsigned int MAX_ORPHAN_BLOCKS = 750;
|
||||
static const unsigned int MAX_ORPHAN_BLOCKS_IBD = 1500;
|
||||
static const unsigned int MAX_REORG_DEPTH = 100; // reject reorgs deeper than this (finality)
|
||||
static const unsigned int MAX_INV_SZ = 50000;
|
||||
static const int64_t MIN_TX_FEE = (1 * CENT) / 100;
|
||||
@@ -109,7 +110,7 @@ extern bool fEnforceCanonical;
|
||||
static const uint64_t nMinDiskSpace = 52428800;
|
||||
|
||||
class CReserveKey;
|
||||
class CTxDB;
|
||||
class CTxDBBase;
|
||||
class CTxIndex;
|
||||
|
||||
void RegisterWallet(CWallet* pwalletIn);
|
||||
@@ -720,10 +721,10 @@ public:
|
||||
}
|
||||
|
||||
|
||||
bool ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet);
|
||||
bool ReadFromDisk(CTxDB& txdb, COutPoint prevout);
|
||||
bool ReadFromDisk(CTxDBBase& txdb, COutPoint prevout, CTxIndex& txindexRet);
|
||||
bool ReadFromDisk(CTxDBBase& txdb, COutPoint prevout);
|
||||
bool ReadFromDisk(COutPoint prevout);
|
||||
bool DisconnectInputs(CTxDB& txdb);
|
||||
bool DisconnectInputs(CTxDBBase& txdb);
|
||||
|
||||
/** Fetch UTXO entries for all inputs from the UTXO database or mempool.
|
||||
|
||||
@@ -735,7 +736,7 @@ public:
|
||||
@param[out] fInvalid returns true if transaction is invalid
|
||||
@return Returns true if all inputs are found
|
||||
*/
|
||||
bool FetchInputs(CTxDB& txdb, const MapPrevTx& mapPendingUtxos,
|
||||
bool FetchInputs(CTxDBBase& txdb, const MapPrevTx& mapPendingUtxos,
|
||||
bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid);
|
||||
|
||||
/** Validate inputs against UTXO entries and verify signatures.
|
||||
@@ -746,13 +747,13 @@ public:
|
||||
@param[in] fMiner true if called from CreateNewBlock
|
||||
@return Returns true if all checks succeed
|
||||
*/
|
||||
bool ConnectInputs(CTxDB& txdb, const MapPrevTx& inputs,
|
||||
bool ConnectInputs(CTxDBBase& txdb, const MapPrevTx& inputs,
|
||||
const CBlockIndex* pindexBlock, bool fBlock, bool fMiner,
|
||||
std::vector<CScriptCheck>* pvChecks = NULL);
|
||||
bool ClientConnectInputs();
|
||||
bool CheckTransaction() const;
|
||||
bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
|
||||
bool GetCoinAge(CTxDB& txdb, uint64_t& nCoinAge) const; // triangles: get transaction coin age
|
||||
bool AcceptToMemoryPool(CTxDBBase& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
|
||||
bool GetCoinAge(CTxDBBase& txdb, uint64_t& nCoinAge) const; // triangles: get transaction coin age
|
||||
|
||||
protected:
|
||||
const CTxOut& GetOutputFor(const CTxIn& input, const MapPrevTx& inputs) const;
|
||||
@@ -814,7 +815,7 @@ public:
|
||||
int GetDepthInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
|
||||
bool IsInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChainINTERNAL(pindexRet) > 0; }
|
||||
int GetBlocksToMaturity() const;
|
||||
bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true);
|
||||
bool AcceptToMemoryPool(CTxDBBase& txdb, bool fCheckInputs=true);
|
||||
bool AcceptToMemoryPool();
|
||||
};
|
||||
|
||||
@@ -1145,10 +1146,10 @@ public:
|
||||
}
|
||||
|
||||
|
||||
bool DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex);
|
||||
bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck=false);
|
||||
bool DisconnectBlock(CTxDBBase& txdb, CBlockIndex* pindex);
|
||||
bool ConnectBlock(CTxDBBase& txdb, CBlockIndex* pindex, bool fJustCheck=false);
|
||||
bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
|
||||
bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
|
||||
bool SetBestChain(CTxDBBase& txdb, CBlockIndex* pindexNew);
|
||||
bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProofOfStake);
|
||||
bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true) const;
|
||||
bool AcceptBlock();
|
||||
@@ -1157,7 +1158,7 @@ public:
|
||||
bool CheckBlockSignature() const;
|
||||
|
||||
private:
|
||||
bool SetBestChainInner(CTxDB& txdb, CBlockIndex *pindexNew);
|
||||
bool SetBestChainInner(CTxDBBase& txdb, CBlockIndex *pindexNew);
|
||||
};
|
||||
|
||||
|
||||
@@ -1661,7 +1662,7 @@ public:
|
||||
std::map<uint256, CTransaction> mapTx;
|
||||
std::map<COutPoint, CInPoint> mapNextTx;
|
||||
|
||||
bool accept(CTxDB& txdb, CTransaction &tx,
|
||||
bool accept(CTxDBBase& txdb, CTransaction &tx,
|
||||
bool fCheckInputs, bool* pfMissingInputs);
|
||||
bool addUnchecked(const uint256& hash, CTransaction &tx);
|
||||
bool remove(const CTransaction &tx, bool fRecursive = false);
|
||||
@@ -1687,6 +1688,121 @@ public:
|
||||
};
|
||||
|
||||
extern CTxMemPool mempool;
|
||||
extern CScriptVerifyCache scriptVerifyCache;
|
||||
|
||||
/**
|
||||
* Compact block relay for Tor-only networks.
|
||||
*
|
||||
* Instead of sending a full block, send the header + short transaction IDs.
|
||||
* The receiver reconstructs the block from its mempool. For PoS blocks with
|
||||
* 0-2 transactions (the common case), the coinstake is always prefilled, so
|
||||
* the compact block IS the complete block — no extra round-trip needed.
|
||||
*/
|
||||
|
||||
/** Short transaction ID: first 6 bytes of SipHash(txid) */
|
||||
static inline uint64_t GetShortTxId(const uint256& txhash, uint64_t nonce)
|
||||
{
|
||||
// Simple short ID: XOR txhash prefix with nonce
|
||||
uint64_t id = 0;
|
||||
memcpy(&id, txhash.begin(), 6); // first 6 bytes
|
||||
id ^= nonce;
|
||||
return id & 0xFFFFFFFFFFFFULL; // mask to 48 bits
|
||||
}
|
||||
|
||||
class CCompactBlock
|
||||
{
|
||||
public:
|
||||
// Block header fields
|
||||
int nVersion;
|
||||
uint256 hashPrevBlock;
|
||||
uint256 hashMerkleRoot;
|
||||
unsigned int nTime;
|
||||
unsigned int nBits;
|
||||
unsigned int nNonce;
|
||||
std::vector<unsigned char> vchBlockSig;
|
||||
|
||||
// Compact block data
|
||||
uint64_t nShortIdNonce; // nonce for short ID calculation
|
||||
std::vector<uint64_t> vShortTxIds; // short IDs for non-prefilled txs
|
||||
std::vector<std::pair<uint16_t, CTransaction>> vPrefilledTxn; // index + full tx
|
||||
|
||||
CCompactBlock() : nVersion(0), nTime(0), nBits(0), nNonce(0), nShortIdNonce(0) {}
|
||||
|
||||
// Construct from a full block: prefill coinbase + coinstake, short-ID the rest
|
||||
CCompactBlock(const CBlock& block)
|
||||
{
|
||||
nVersion = block.nVersion;
|
||||
hashPrevBlock = block.hashPrevBlock;
|
||||
hashMerkleRoot = block.hashMerkleRoot;
|
||||
nTime = block.nTime;
|
||||
nBits = block.nBits;
|
||||
nNonce = block.nNonce;
|
||||
vchBlockSig = block.vchBlockSig;
|
||||
nShortIdNonce = GetRand(std::numeric_limits<uint64_t>::max());
|
||||
|
||||
for (uint16_t i = 0; i < block.vtx.size(); i++)
|
||||
{
|
||||
if (i <= 1) {
|
||||
// Always prefill coinbase (idx 0) and coinstake (idx 1)
|
||||
vPrefilledTxn.push_back(std::make_pair(i, block.vtx[i]));
|
||||
} else {
|
||||
vShortTxIds.push_back(GetShortTxId(block.vtx[i].GetHash(), nShortIdNonce));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(nVersion);
|
||||
READWRITE(hashPrevBlock);
|
||||
READWRITE(hashMerkleRoot);
|
||||
READWRITE(nTime);
|
||||
READWRITE(nBits);
|
||||
READWRITE(nNonce);
|
||||
READWRITE(vchBlockSig);
|
||||
READWRITE(nShortIdNonce);
|
||||
READWRITE(vShortTxIds);
|
||||
READWRITE(vPrefilledTxn);
|
||||
)
|
||||
|
||||
uint256 GetBlockHash() const
|
||||
{
|
||||
CBlock hdr;
|
||||
hdr.nVersion = nVersion;
|
||||
hdr.hashPrevBlock = hashPrevBlock;
|
||||
hdr.hashMerkleRoot = hashMerkleRoot;
|
||||
hdr.nTime = nTime;
|
||||
hdr.nBits = nBits;
|
||||
hdr.nNonce = nNonce;
|
||||
return hdr.GetHash();
|
||||
}
|
||||
};
|
||||
|
||||
class CBlockTxnRequest
|
||||
{
|
||||
public:
|
||||
uint256 blockhash;
|
||||
std::vector<uint16_t> vIndex; // indices of missing transactions
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(blockhash);
|
||||
READWRITE(vIndex);
|
||||
)
|
||||
};
|
||||
|
||||
class CBlockTxnResponse
|
||||
{
|
||||
public:
|
||||
uint256 blockhash;
|
||||
std::vector<CTransaction> vTxn;
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(blockhash);
|
||||
READWRITE(vTxn);
|
||||
)
|
||||
};
|
||||
|
||||
/**
|
||||
* Closure representing one script check for parallel verification.
|
||||
@@ -1711,7 +1827,12 @@ public:
|
||||
|
||||
bool operator()()
|
||||
{
|
||||
return ptxTo && VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nHashType);
|
||||
if (!ptxTo)
|
||||
return false;
|
||||
if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nHashType))
|
||||
return false;
|
||||
scriptVerifyCache.Set(ptxTo->GetHash(), nIn);
|
||||
return true;
|
||||
}
|
||||
|
||||
void swap(CScriptCheck& other)
|
||||
|
||||
+4
-3
@@ -136,7 +136,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake, int64_t* pFees)
|
||||
int64_t nFees = 0;
|
||||
{
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
|
||||
// Priority order to process transactions
|
||||
list<COrphan> vOrphan; // list memory doesn't move
|
||||
@@ -387,6 +387,7 @@ void StakeMiner(CWallet *pwallet)
|
||||
RenameThread("Triangles-miner");
|
||||
|
||||
bool fTryToSync = true;
|
||||
bool fForceStaking = GetBoolArg("-forcestaking", false);
|
||||
|
||||
while (true)
|
||||
{
|
||||
@@ -401,7 +402,7 @@ void StakeMiner(CWallet *pwallet)
|
||||
return;
|
||||
}
|
||||
|
||||
while (vNodes.empty() || IsInitialBlockDownload())
|
||||
while (!fForceStaking && (vNodes.empty() || IsInitialBlockDownload()))
|
||||
{
|
||||
nLastCoinStakeSearchInterval = 0;
|
||||
fTryToSync = true;
|
||||
@@ -410,7 +411,7 @@ void StakeMiner(CWallet *pwallet)
|
||||
return;
|
||||
}
|
||||
|
||||
if (fTryToSync)
|
||||
if (fTryToSync && !fForceStaking)
|
||||
{
|
||||
fTryToSync = false;
|
||||
if (vNodes.size() < 2 || nBestHeight < GetNumBlocksOfPeers())
|
||||
|
||||
+90
-37
@@ -3,7 +3,6 @@
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "irc.h"
|
||||
#include "db.h"
|
||||
#include "net.h"
|
||||
#include "main.h"
|
||||
@@ -37,7 +36,7 @@ extern "C" {
|
||||
// int tor_main(int argc, char *argv[]);
|
||||
}
|
||||
|
||||
static const int MAX_OUTBOUND_CONNECTIONS = 16;
|
||||
static const int MAX_OUTBOUND_CONNECTIONS = 8; // reduced from 16 for Tor-only small networks
|
||||
|
||||
void ThreadMessageHandler2(void* parg);
|
||||
void ThreadSocketHandler2(void* parg);
|
||||
@@ -690,6 +689,9 @@ void CNode::copyStats(CNodeStats &stats)
|
||||
X(fInbound);
|
||||
X(nStartingHeight);
|
||||
X(nMisbehavior);
|
||||
X(nPingUsecTime);
|
||||
X(nBlocksDelivered);
|
||||
X(nAvgBlockLatencyUs);
|
||||
}
|
||||
#undef X
|
||||
|
||||
@@ -902,13 +904,9 @@ void ThreadSocketHandler2(void* parg)
|
||||
TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
|
||||
if (lockRecv)
|
||||
{
|
||||
TRY_LOCK(pnode->cs_mapRequests, lockReq);
|
||||
if (lockReq)
|
||||
{
|
||||
TRY_LOCK(pnode->cs_inventory, lockInv);
|
||||
if (lockInv)
|
||||
fDelete = true;
|
||||
}
|
||||
TRY_LOCK(pnode->cs_inventory, lockInv);
|
||||
if (lockInv)
|
||||
fDelete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1029,10 +1027,6 @@ void ThreadSocketHandler2(void* parg)
|
||||
if (nErr != WSAEWOULDBLOCK)
|
||||
printf("socket error accept failed: %d\n", nErr);
|
||||
}
|
||||
else if (nInbound >= GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS)
|
||||
{
|
||||
closesocket(hSocket);
|
||||
}
|
||||
else if (CNode::IsBanned(addr))
|
||||
{
|
||||
printf("connection from %s dropped (banned)\n", addr.ToString().c_str());
|
||||
@@ -1040,12 +1034,36 @@ void ThreadSocketHandler2(void* parg)
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("accepted connection %s\n", addr.ToString().c_str());
|
||||
CNode* pnode = new CNode(hSocket, addr, "", true);
|
||||
pnode->AddRef();
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
vNodes.push_back(pnode);
|
||||
int nMaxInbound = GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS;
|
||||
bool fAccept = (nInbound < nMaxInbound);
|
||||
|
||||
// Reserve 2 extra inbound slots for known seed nodes
|
||||
if (!fAccept) {
|
||||
bool fIsSeed = false;
|
||||
static const char *(*strOnionSeedCheck)[1] = fTestNet ? strTestNetOnionSeed : strMainNetOnionSeed;
|
||||
std::string incomingAddr = addr.ToStringIP();
|
||||
for (unsigned int si = 0; strOnionSeedCheck[si][0] != NULL; si++) {
|
||||
if (incomingAddr.find(strOnionSeedCheck[si][0]) != std::string::npos) {
|
||||
fIsSeed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fIsSeed && nInbound < nMaxInbound + 2) {
|
||||
fAccept = true;
|
||||
printf("accepted seed node %s (reserved slot)\n", addr.ToString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (fAccept) {
|
||||
printf("accepted connection %s\n", addr.ToString().c_str());
|
||||
CNode* pnode = new CNode(hSocket, addr, "", true);
|
||||
pnode->AddRef();
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
vNodes.push_back(pnode);
|
||||
}
|
||||
} else {
|
||||
closesocket(hSocket);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1137,14 +1155,14 @@ void ThreadSocketHandler2(void* parg)
|
||||
printf("socket no message in first 60 seconds, %d %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0);
|
||||
pnode->fDisconnect = true;
|
||||
}
|
||||
else if (GetTime() - pnode->nLastSend > 90*60 && GetTime() - pnode->nLastSendEmpty > 90*60)
|
||||
else if (GetTime() - pnode->nLastSend > 10*60 && GetTime() - pnode->nLastSendEmpty > 10*60)
|
||||
{
|
||||
printf("socket not sending\n");
|
||||
printf("socket not sending (10min timeout)\n");
|
||||
pnode->fDisconnect = true;
|
||||
}
|
||||
else if (GetTime() - pnode->nLastRecv > 90*60)
|
||||
else if (GetTime() - pnode->nLastRecv > 10*60)
|
||||
{
|
||||
printf("socket inactivity timeout\n");
|
||||
printf("socket inactivity timeout (10min)\n");
|
||||
pnode->fDisconnect = true;
|
||||
}
|
||||
}
|
||||
@@ -1432,16 +1450,12 @@ void ThreadOnionSeed(void* parg)
|
||||
printf("ThreadOnionSeed: initial seeding complete\n");
|
||||
|
||||
// Periodic re-seeding for isolated or under-connected nodes.
|
||||
// Check every 2 minutes, re-seed when < 2 outbound peers.
|
||||
// First re-seed after 5 min cooldown, then 15 min for subsequent.
|
||||
// EMERGENCY MODE: When 0 outbound peers, check every 15 seconds
|
||||
// NORMAL MODE: Check every 2 minutes, re-seed when < 2 outbound peers
|
||||
int64_t nLastReseed = GetTime();
|
||||
bool bFirstReseed = true;
|
||||
while (!fShutdown) {
|
||||
for (int i = 0; i < 120 && !fShutdown; i++) // sleep 2 minutes
|
||||
MilliSleep(1000);
|
||||
|
||||
if (fShutdown) break;
|
||||
|
||||
// Count outbound peers to determine check interval
|
||||
int nOutbound = 0;
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
@@ -1450,12 +1464,44 @@ void ThreadOnionSeed(void* parg)
|
||||
nOutbound++;
|
||||
}
|
||||
|
||||
int64_t nCooldown = bFirstReseed ? 5 * 60 : 15 * 60;
|
||||
// Emergency mode: 0 peers = check every 15 seconds
|
||||
// Low mode: 1 peer = check every 30 seconds
|
||||
// Normal: 2+ peers = check every 2 minutes
|
||||
int nSleepSeconds = (nOutbound == 0) ? 15 : (nOutbound < 2) ? 30 : 120;
|
||||
for (int i = 0; i < nSleepSeconds && !fShutdown; i++)
|
||||
MilliSleep(1000);
|
||||
|
||||
if (fShutdown) break;
|
||||
|
||||
// Recount after sleep
|
||||
nOutbound = 0;
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (CNode* pnode : vNodes)
|
||||
if (!pnode->fInbound)
|
||||
nOutbound++;
|
||||
}
|
||||
|
||||
// Emergency (0 peers): no cooldown, reseed immediately
|
||||
// Low (1 peer): 60 second cooldown
|
||||
// Normal (<2): 5 min first, 15 min subsequent
|
||||
int64_t nCooldown;
|
||||
if (nOutbound == 0)
|
||||
nCooldown = 0; // immediate
|
||||
else if (nOutbound < 2)
|
||||
nCooldown = bFirstReseed ? 60 : 5 * 60;
|
||||
else
|
||||
nCooldown = bFirstReseed ? 5 * 60 : 15 * 60;
|
||||
|
||||
if (nOutbound < 2 && GetTime() - nLastReseed > nCooldown) {
|
||||
printf("ThreadOnionSeed: low outbound peers (%d), re-seeding...\n", nOutbound);
|
||||
if (nOutbound == 0)
|
||||
printf("ThreadOnionSeed: EMERGENCY - 0 outbound peers, re-seeding immediately!\n");
|
||||
else
|
||||
printf("ThreadOnionSeed: low outbound peers (%d), re-seeding...\n", nOutbound);
|
||||
|
||||
ThreadHTTPSeedFetch2(NULL);
|
||||
|
||||
// Also re-queue hardcoded seeds for direct connection
|
||||
// Re-queue hardcoded seeds for direct connection
|
||||
for (unsigned int seed_idx = 0; strOnionSeed[seed_idx][0] != NULL; seed_idx++) {
|
||||
std::string oneShotAddr = std::string(strOnionSeed[seed_idx][0])
|
||||
+ ":" + std::to_string(GetDefaultPort());
|
||||
@@ -2341,8 +2387,11 @@ void StartNode(void* parg)
|
||||
RenameThread("Triangles-start");
|
||||
|
||||
if (semOutbound == NULL) {
|
||||
// initialize semaphore
|
||||
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));
|
||||
// initialize semaphore — use -maxoutbound if specified, else default
|
||||
int nMaxOutbound = (int)GetArg("-maxoutbound", MAX_OUTBOUND_CONNECTIONS);
|
||||
nMaxOutbound = min(nMaxOutbound, (int)GetArg("-maxconnections", 125));
|
||||
nMaxOutbound = max(nMaxOutbound, 1); // at least 1 outbound
|
||||
printf("Max outbound connections: %d\n", nMaxOutbound);
|
||||
semOutbound = new CSemaphore(nMaxOutbound);
|
||||
}
|
||||
|
||||
@@ -2412,9 +2461,13 @@ bool StopNode()
|
||||
fShutdown = true;
|
||||
nTransactionsUpdated++;
|
||||
int64_t nStart = GetTime();
|
||||
if (semOutbound)
|
||||
for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
|
||||
if (semOutbound) {
|
||||
int nMaxOutbound = (int)GetArg("-maxoutbound", MAX_OUTBOUND_CONNECTIONS);
|
||||
nMaxOutbound = min(nMaxOutbound, (int)GetArg("-maxconnections", 125));
|
||||
nMaxOutbound = max(nMaxOutbound, 1);
|
||||
for (int i=0; i<nMaxOutbound; i++)
|
||||
semOutbound->post();
|
||||
}
|
||||
do
|
||||
{
|
||||
int nThreadsRunning = 0;
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "protocol.h"
|
||||
#include "addrman.h"
|
||||
|
||||
class CRequestTracker;
|
||||
class CNode;
|
||||
class CBlockIndex;
|
||||
bool IsInitialBlockDownload();
|
||||
@@ -26,7 +25,7 @@ extern int nBestHeight;
|
||||
|
||||
|
||||
|
||||
inline unsigned int ReceiveFloodSize() { return 100 * 1024 * 1024; } // 100 MB
|
||||
inline unsigned int ReceiveFloodSize() { return 50 * 1024 * 1024; } // 50 MB (reduced for Tor-only network)
|
||||
inline unsigned int SendBufferSize() { return 32 * 1024 * 1024; } // 32 MB
|
||||
|
||||
void AddOneShot(std::string strDest);
|
||||
@@ -76,25 +75,6 @@ enum
|
||||
MSG_BLOCK,
|
||||
};
|
||||
|
||||
class CRequestTracker
|
||||
{
|
||||
public:
|
||||
void (*fn)(void*, CDataStream&);
|
||||
void* param1;
|
||||
|
||||
explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
|
||||
{
|
||||
fn = fnIn;
|
||||
param1 = param1In;
|
||||
}
|
||||
|
||||
bool IsNull()
|
||||
{
|
||||
return fn == NULL;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** Thread types */
|
||||
enum threadId
|
||||
{
|
||||
@@ -146,6 +126,9 @@ public:
|
||||
bool fInbound;
|
||||
int nStartingHeight;
|
||||
int nMisbehavior;
|
||||
int64_t nPingUsecTime;
|
||||
int nBlocksDelivered;
|
||||
int64_t nAvgBlockLatencyUs;
|
||||
};
|
||||
|
||||
|
||||
@@ -253,6 +236,7 @@ public:
|
||||
bool fSuccessfullyConnected;
|
||||
bool fDisconnect;
|
||||
bool fPreferHeaders; // peer requested block announcements via headers (sendheaders)
|
||||
bool fSendCmpct; // peer supports compact block relay (sendcmpct)
|
||||
CSemaphoreGrant grantOutbound;
|
||||
int nRefCount;
|
||||
protected:
|
||||
@@ -264,8 +248,6 @@ protected:
|
||||
int nMisbehavior;
|
||||
|
||||
public:
|
||||
std::map<uint256, CRequestTracker> mapRequests;
|
||||
CCriticalSection cs_mapRequests;
|
||||
uint256 hashContinue;
|
||||
CBlockIndex* pindexLastGetBlocksBegin;
|
||||
uint256 hashLastGetBlocksEnd;
|
||||
@@ -273,10 +255,18 @@ public:
|
||||
uint256 hashLastGetHeadersEnd;
|
||||
int nStartingHeight;
|
||||
int64_t nLastTipCheck; // last time we asked this peer for chain tip
|
||||
int64_t nLastIbdHeaderRequest; // last time we sent IBD-mode getheaders to this peer (heartbeat throttle)
|
||||
int64_t nAvgBlockLatencyUs; // rolling average block delivery latency (microseconds)
|
||||
int nBlocksDelivered; // count of blocks delivered by this peer
|
||||
int nBestKnownHeight; // highest block height known to this peer (updated from inv/block msgs)
|
||||
int nIncompatibleGetblocks; // count of getblocks with no common blocks (fork detection)
|
||||
|
||||
// BIP 31 ping/pong latency tracking
|
||||
uint64_t nPingNonceSent; // nonce of last ping sent (0 = no outstanding ping)
|
||||
int64_t nPingUsecStart; // microsecond timestamp when last ping was sent
|
||||
int64_t nPingUsecTime; // last measured round-trip time (microseconds), 0 = unknown
|
||||
int nPingRetryCount; // consecutive pings without pong response
|
||||
|
||||
// flood relay
|
||||
std::vector<CAddress> vAddrToSend;
|
||||
mruset<CAddress> setAddrKnown;
|
||||
@@ -314,6 +304,7 @@ public:
|
||||
fSuccessfullyConnected = false;
|
||||
fDisconnect = false;
|
||||
fPreferHeaders = false;
|
||||
fSendCmpct = false;
|
||||
nRefCount = 0;
|
||||
nSendSize = 0;
|
||||
nSendOffset = 0;
|
||||
@@ -324,9 +315,15 @@ public:
|
||||
hashLastGetHeadersEnd = 0;
|
||||
nStartingHeight = -1;
|
||||
nLastTipCheck = 0;
|
||||
nLastIbdHeaderRequest = 0;
|
||||
nAvgBlockLatencyUs = 0;
|
||||
nBlocksDelivered = 0;
|
||||
nBestKnownHeight = -1;
|
||||
nIncompatibleGetblocks = 0;
|
||||
nPingNonceSent = 0;
|
||||
nPingUsecStart = 0;
|
||||
nPingUsecTime = 0;
|
||||
nPingRetryCount = 0;
|
||||
fGetAddr = false;
|
||||
nMisbehavior = 0;
|
||||
hashCheckpointKnown = 0;
|
||||
@@ -671,52 +668,6 @@ public:
|
||||
}
|
||||
|
||||
|
||||
void PushRequest(const char* pszCommand,
|
||||
void (*fn)(void*, CDataStream&), void* param1)
|
||||
{
|
||||
uint256 hashReply;
|
||||
RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
|
||||
|
||||
{
|
||||
LOCK(cs_mapRequests);
|
||||
mapRequests[hashReply] = CRequestTracker(fn, param1);
|
||||
}
|
||||
|
||||
PushMessage(pszCommand, hashReply);
|
||||
}
|
||||
|
||||
template<typename T1>
|
||||
void PushRequest(const char* pszCommand, const T1& a1,
|
||||
void (*fn)(void*, CDataStream&), void* param1)
|
||||
{
|
||||
uint256 hashReply;
|
||||
RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
|
||||
|
||||
{
|
||||
LOCK(cs_mapRequests);
|
||||
mapRequests[hashReply] = CRequestTracker(fn, param1);
|
||||
}
|
||||
|
||||
PushMessage(pszCommand, hashReply, a1);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
|
||||
void (*fn)(void*, CDataStream&), void* param1)
|
||||
{
|
||||
uint256 hashReply;
|
||||
RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
|
||||
|
||||
{
|
||||
LOCK(cs_mapRequests);
|
||||
mapRequests[hashReply] = CRequestTracker(fn, param1);
|
||||
}
|
||||
|
||||
PushMessage(pszCommand, hashReply, a1, a2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
|
||||
void PushGetHeaders(CBlockIndex* pindexBegin, uint256 hashEnd);
|
||||
bool IsSubscribed(unsigned int nChannel);
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
#include <string>
|
||||
#include <deque>
|
||||
#include <vector>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
/**
|
||||
* Thread-safe notification queue for SSE (Server-Sent Events) clients.
|
||||
@@ -24,8 +25,8 @@
|
||||
class CNotificationQueue
|
||||
{
|
||||
private:
|
||||
mutable boost::mutex cs;
|
||||
boost::condition_variable cond;
|
||||
mutable std::mutex cs;
|
||||
std::condition_variable cond;
|
||||
|
||||
struct Event {
|
||||
uint64_t id;
|
||||
@@ -43,7 +44,7 @@ public:
|
||||
/** Push a new event. Wakes all waiting SSE clients. */
|
||||
void Push(const std::string& strData)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(cs);
|
||||
std::unique_lock<std::mutex> lock(cs);
|
||||
events.push_back(Event{nNextId++, strData});
|
||||
while (events.size() > MAX_QUEUED_EVENTS)
|
||||
events.pop_front();
|
||||
@@ -59,7 +60,7 @@ public:
|
||||
bool WaitForEvents(uint64_t& nLastId, std::vector<std::string>& vEvents, int nTimeoutMs, const volatile bool& fShutdown)
|
||||
{
|
||||
vEvents.clear();
|
||||
boost::mutex::scoped_lock lock(cs);
|
||||
std::unique_lock<std::mutex> lock(cs);
|
||||
|
||||
// Check for events already in the queue past our read position
|
||||
bool fHasNew = false;
|
||||
@@ -75,7 +76,7 @@ public:
|
||||
if (!fHasNew)
|
||||
{
|
||||
// Wait for new events or timeout
|
||||
cond.timed_wait(lock, boost::posix_time::milliseconds(nTimeoutMs));
|
||||
cond.wait_for(lock, std::chrono::milliseconds(nTimeoutMs));
|
||||
}
|
||||
|
||||
// Drain all events newer than nLastId
|
||||
@@ -97,7 +98,7 @@ public:
|
||||
/** Get the current latest event ID (for clients that want to skip history). */
|
||||
uint64_t GetLatestId() const
|
||||
{
|
||||
boost::mutex::scoped_lock lock(cs);
|
||||
std::unique_lock<std::mutex> lock(cs);
|
||||
return nNextId - 1;
|
||||
}
|
||||
};
|
||||
|
||||
+14
-7
@@ -1,16 +1,23 @@
|
||||
|
||||
#ifndef TRIANGLES_ONIONSEED_H
|
||||
#define TRIANGLES_ONIONSEED_H
|
||||
|
||||
// Hardcoded onion seed nodes for initial peer discovery.
|
||||
// Also fetched dynamically via https://seeds.cryptographic-triangles.org/seeds.txt
|
||||
static const char *strMainNetOnionSeed[][1] = {
|
||||
{"6ygpphp2qsucwvhwefv6h6ehvk6zjf7b7zdp4ggkzjjwe76cg6jwm7id.onion"},
|
||||
{"jbpfhe7zw3qm67wy3j2ayysp3mnrjobopthnko3b3sgahqtecblwqmid.onion"},
|
||||
{"uddaxjbo3lh2zskg7w6gwln4ty5cel7q4c5jbx7fdtv6zf2j47gdlyad.onion"},
|
||||
{"el5sirhhleecuctpeeprelzubpqmoqivvra3rzlwbjttinxa4fq3wnid.onion"},
|
||||
{"sj5dhybnlp3v4y5niyc5unrnd6s43lyx5ibup7rolyosjbi2u2hsbvyd.onion"},
|
||||
{"i3kr5meha7se4ns3wss3h7v46m6uksfzv4wrohdqxpj6n35wyo2bvlid.onion"},
|
||||
// DNS2 - primary bootstrap server (194.233.88.206)
|
||||
{"gxvrhv3qitnc6kobrhsrse46bmcfitnybapor3or3oczzuxn6hfzxyid.onion"},
|
||||
// DNS3 - canonical chain reference (74.208.167.19)
|
||||
{"i6tk7soznftvoibtskwlezviskiererhjndpsmrff4kaxw7jnd5izfqd.onion"},
|
||||
// Hetzner Helsinki - ARM64 staking node (46.62.249.20)
|
||||
{"nawqqoazk2hhaglygulpeg6kh7hsgnvi2fursdvpvkantu4ojj26taid.onion"},
|
||||
// Contabo seed 1 (173.212.201.200)
|
||||
{"vmepp7plxngv4qpyngbgtb6njwnmlwy4api64xnwkhaf6fm3qlqtpfad.onion"},
|
||||
// Contabo seed 2
|
||||
{"nsldmfujkiwsfha42ajp5zx7gz3ekwdk4nvowdpf56mayuxnzshuykqd.onion"},
|
||||
// Contabo seed 3
|
||||
{"on4noksywc7b6cdbbxsp535l7j4cugunvlyz3iyhf6sfcg2qzaoy3eqd.onion"},
|
||||
// Contabo seed 4
|
||||
{"3uyzltm5cy7xzunncp3d7ariw75erabdnj4l3cxwvsxb6h4orc7eiqad.onion"},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
||||
+2
-1
@@ -68,7 +68,8 @@ class CMessageHeader
|
||||
/** nServices flags */
|
||||
enum
|
||||
{
|
||||
NODE_NETWORK = (1 << 0),
|
||||
NODE_NETWORK = (1 << 0),
|
||||
NODE_SNAPSHOT = (1 << 1), // peer can serve UTXO snapshot chunks
|
||||
};
|
||||
|
||||
/** A CService with information about it as peer */
|
||||
|
||||
+5
-37
@@ -4,7 +4,6 @@
|
||||
#include "addresstablemodel.h"
|
||||
#include "transactiontablemodel.h"
|
||||
|
||||
#include "alert.h"
|
||||
#include "main.h"
|
||||
#include "ui_interface.h"
|
||||
|
||||
@@ -94,25 +93,6 @@ void ClientModel::updateNumConnections(int numConnections)
|
||||
emit numConnectionsChanged(numConnections);
|
||||
}
|
||||
|
||||
void ClientModel::updateAlert(const QString &hash, int status)
|
||||
{
|
||||
// Show error message notification for new alert
|
||||
if(status == CT_NEW)
|
||||
{
|
||||
uint256 hash_256;
|
||||
hash_256.SetHex(hash.toStdString());
|
||||
CAlert alert = CAlert::getAlertByHash(hash_256);
|
||||
if(!alert.IsNull())
|
||||
{
|
||||
emit error(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), false);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit a numBlocksChanged when the status message changes,
|
||||
// so that the view recomputes and updates the status bar.
|
||||
emit numBlocksChanged(getNumBlocks(), getNumBlocksOfPeers());
|
||||
}
|
||||
|
||||
double ClientModel::GetDifficulty() const
|
||||
{
|
||||
// Floating point number that is a multiple of the minimum difficulty,
|
||||
@@ -200,27 +180,15 @@ static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConn
|
||||
Q_ARG(int, newNumConnections));
|
||||
}
|
||||
|
||||
static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
|
||||
{
|
||||
if (fShutdown) return;
|
||||
OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
|
||||
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
|
||||
Q_ARG(QString, QString::fromStdString(hash.GetHex())),
|
||||
Q_ARG(int, status));
|
||||
}
|
||||
|
||||
void ClientModel::subscribeToCoreSignals()
|
||||
{
|
||||
// Connect signals to client
|
||||
uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
|
||||
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
|
||||
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
|
||||
m_core_signal_connections.add(uiInterface.NotifyBlocksChanged.connect(
|
||||
[this]() { NotifyBlocksChanged(this); }));
|
||||
m_core_signal_connections.add(uiInterface.NotifyNumConnectionsChanged.connect(
|
||||
[this](int n) { NotifyNumConnectionsChanged(this, n); }));
|
||||
}
|
||||
|
||||
void ClientModel::unsubscribeFromCoreSignals()
|
||||
{
|
||||
// Disconnect signals from client
|
||||
uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
|
||||
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
|
||||
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
|
||||
m_core_signal_connections.disconnect_all();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "../util_signal.h"
|
||||
|
||||
class OptionsModel;
|
||||
class AddressTableModel;
|
||||
class TransactionTableModel;
|
||||
@@ -57,6 +59,8 @@ private:
|
||||
|
||||
void subscribeToCoreSignals();
|
||||
void unsubscribeFromCoreSignals();
|
||||
|
||||
CSignalConnections m_core_signal_connections;
|
||||
signals:
|
||||
void numConnectionsChanged(int count);
|
||||
void numBlocksChanged(int count, int countOfPeers);
|
||||
@@ -67,7 +71,6 @@ signals:
|
||||
public slots:
|
||||
void updateTimer();
|
||||
void updateNumConnections(int numConnections);
|
||||
void updateAlert(const QString &hash, int status);
|
||||
};
|
||||
|
||||
#endif // CLIENTMODEL_H
|
||||
|
||||
@@ -868,7 +868,7 @@ QWidget#line {
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<a href="http://explorer.triangles.technology"> &#187; TRI block explorer</a></body></string>
|
||||
<a href="https://blocks.cryptographic-triangles.org"> &#187; TRI block explorer</a></body></string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
|
||||
+22
-22
@@ -20,8 +20,8 @@
|
||||
#include <QDesktopServices>
|
||||
#include <QThread>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32_WINNT
|
||||
@@ -240,10 +240,10 @@ bool isObscured(QWidget *w)
|
||||
|
||||
void openDebugLogfile()
|
||||
{
|
||||
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
|
||||
std::filesystem::path pathDebug = GetDataDir() / "debug.log";
|
||||
|
||||
/* Open debug.log with the associated application */
|
||||
if (boost::filesystem::exists(pathDebug))
|
||||
if (std::filesystem::exists(pathDebug))
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(QString::fromStdString(pathDebug.string())));
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ bool ToolTipToRichTextFilter::eventFilter(QObject *obj, QEvent *evt)
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
boost::filesystem::path static StartupShortcutPath()
|
||||
std::filesystem::path static StartupShortcutPath()
|
||||
{
|
||||
return GetSpecialFolderPath(CSIDL_STARTUP) / "triangles.lnk";
|
||||
}
|
||||
@@ -280,13 +280,13 @@ boost::filesystem::path static StartupShortcutPath()
|
||||
bool GetStartOnSystemStartup()
|
||||
{
|
||||
// check for triangles.lnk
|
||||
return boost::filesystem::exists(StartupShortcutPath());
|
||||
return std::filesystem::exists(StartupShortcutPath());
|
||||
}
|
||||
|
||||
bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
{
|
||||
// If the shortcut exists already, remove it for updating
|
||||
boost::filesystem::remove(StartupShortcutPath());
|
||||
std::filesystem::remove(StartupShortcutPath());
|
||||
|
||||
if (fAutoStart)
|
||||
{
|
||||
@@ -343,9 +343,9 @@ bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
// Follow the Desktop Application Autostart Spec:
|
||||
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
|
||||
|
||||
boost::filesystem::path static GetAutostartDir()
|
||||
std::filesystem::path static GetAutostartDir()
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
char* pszConfigHome = getenv("XDG_CONFIG_HOME");
|
||||
if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
|
||||
@@ -354,14 +354,14 @@ boost::filesystem::path static GetAutostartDir()
|
||||
return fs::path();
|
||||
}
|
||||
|
||||
boost::filesystem::path static GetAutostartFilePath()
|
||||
std::filesystem::path static GetAutostartFilePath()
|
||||
{
|
||||
return GetAutostartDir() / "triangles.desktop";
|
||||
}
|
||||
|
||||
bool GetStartOnSystemStartup()
|
||||
{
|
||||
boost::filesystem::ifstream optionFile(GetAutostartFilePath());
|
||||
std::ifstream optionFile(GetAutostartFilePath());
|
||||
if (!optionFile.good())
|
||||
return false;
|
||||
// Scan through file for "Hidden=true":
|
||||
@@ -381,7 +381,7 @@ bool GetStartOnSystemStartup()
|
||||
bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
{
|
||||
if (!fAutoStart)
|
||||
boost::filesystem::remove(GetAutostartFilePath());
|
||||
std::filesystem::remove(GetAutostartFilePath());
|
||||
else
|
||||
{
|
||||
char pszExePath[MAX_PATH+1];
|
||||
@@ -389,9 +389,9 @@ bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
|
||||
return false;
|
||||
|
||||
boost::filesystem::create_directories(GetAutostartDir());
|
||||
std::filesystem::create_directories(GetAutostartDir());
|
||||
|
||||
boost::filesystem::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
|
||||
std::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
|
||||
if (!optionFile.good())
|
||||
return false;
|
||||
// Write a triangles.desktop file to the autostart directory:
|
||||
@@ -407,15 +407,15 @@ bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
}
|
||||
#elif defined(Q_OS_MAC) || defined(MAC_OSX) || defined(__APPLE__)
|
||||
|
||||
boost::filesystem::path static GetLaunchAgentsDir()
|
||||
std::filesystem::path static GetLaunchAgentsDir()
|
||||
{
|
||||
const QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
|
||||
if (homeDir.isEmpty())
|
||||
return boost::filesystem::path();
|
||||
return boost::filesystem::path(homeDir.toStdString()) / "Library" / "LaunchAgents";
|
||||
return std::filesystem::path();
|
||||
return std::filesystem::path(homeDir.toStdString()) / "Library" / "LaunchAgents";
|
||||
}
|
||||
|
||||
boost::filesystem::path static GetAutostartFilePath()
|
||||
std::filesystem::path static GetAutostartFilePath()
|
||||
{
|
||||
return GetLaunchAgentsDir() / "org.triangles.triangles-qt.plist";
|
||||
}
|
||||
@@ -441,7 +441,7 @@ static std::string PlistEscape(const std::string& value)
|
||||
|
||||
bool GetStartOnSystemStartup()
|
||||
{
|
||||
boost::filesystem::ifstream optionFile(GetAutostartFilePath());
|
||||
std::ifstream optionFile(GetAutostartFilePath());
|
||||
if (!optionFile.good())
|
||||
return false;
|
||||
|
||||
@@ -459,16 +459,16 @@ bool GetStartOnSystemStartup()
|
||||
bool SetStartOnSystemStartup(bool fAutoStart)
|
||||
{
|
||||
if (!fAutoStart)
|
||||
return !boost::filesystem::exists(GetAutostartFilePath()) || boost::filesystem::remove(GetAutostartFilePath());
|
||||
return !std::filesystem::exists(GetAutostartFilePath()) || std::filesystem::remove(GetAutostartFilePath());
|
||||
|
||||
const QString exePath = QApplication::applicationFilePath();
|
||||
if (exePath.isEmpty())
|
||||
return false;
|
||||
|
||||
const QString workingDir = QFileInfo(exePath).absolutePath();
|
||||
boost::filesystem::create_directories(GetLaunchAgentsDir());
|
||||
std::filesystem::create_directories(GetLaunchAgentsDir());
|
||||
|
||||
boost::filesystem::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
|
||||
std::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
|
||||
if (!optionFile.good())
|
||||
return false;
|
||||
|
||||
|
||||
+10
-10
@@ -14,7 +14,7 @@
|
||||
#include <QCheckBox>
|
||||
#include <QApplication>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
#include <set>
|
||||
|
||||
@@ -152,28 +152,28 @@ void IntroDialog::on_defaultRadio_toggled(bool checked)
|
||||
void IntroDialog::updateFreeSpace()
|
||||
{
|
||||
QString path = getDataDirectory();
|
||||
boost::filesystem::path fsPath(path.toStdString());
|
||||
std::filesystem::path fsPath(path.toStdString());
|
||||
|
||||
// Walk up to find an existing parent
|
||||
try {
|
||||
while (!fsPath.empty() && !boost::filesystem::exists(fsPath))
|
||||
while (!fsPath.empty() && !std::filesystem::exists(fsPath))
|
||||
fsPath = fsPath.parent_path();
|
||||
|
||||
if (!fsPath.empty()) {
|
||||
boost::filesystem::space_info si = boost::filesystem::space(fsPath);
|
||||
std::filesystem::space_info si = std::filesystem::space(fsPath);
|
||||
double freeGB = (double)si.available / (1024.0 * 1024.0 * 1024.0);
|
||||
freeSpaceLabel->setText(tr("Free space: %1 GB").arg(QString::number(freeGB, 'f', 2)));
|
||||
} else {
|
||||
freeSpaceLabel->setText(tr("Cannot determine free space"));
|
||||
}
|
||||
} catch (const boost::filesystem::filesystem_error &) {
|
||||
} catch (const std::filesystem::filesystem_error &) {
|
||||
freeSpaceLabel->setText(tr("Cannot determine free space"));
|
||||
}
|
||||
}
|
||||
|
||||
bool IntroDialog::pickDataDirectory()
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
QSettings settings;
|
||||
// If -datadir was passed on the command line, skip the dialog entirely
|
||||
@@ -305,10 +305,10 @@ bool IntroDialog::pickDataDirectory()
|
||||
return true;
|
||||
}
|
||||
|
||||
static void copyDirectoryRecursive(const boost::filesystem::path& src,
|
||||
const boost::filesystem::path& dst)
|
||||
static void copyDirectoryRecursive(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dst)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
fs::create_directories(dst);
|
||||
for (fs::directory_iterator it(src), end; it != end; ++it) {
|
||||
fs::path dstChild = dst / it->path().filename();
|
||||
@@ -322,7 +322,7 @@ static void copyDirectoryRecursive(const boost::filesystem::path& src,
|
||||
|
||||
bool IntroDialog::migrateDataDirectory(const QString& oldPath, const QString& newPath)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path srcDir(oldPath.toStdString());
|
||||
fs::path dstDir(newPath.toStdString());
|
||||
|
||||
+11
-12
@@ -93,7 +93,7 @@ public:
|
||||
QDateTime received_datetime;
|
||||
|
||||
std::string sPrefix("im");
|
||||
leveldb::Iterator* it = dbSmsg.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbSmsg.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbSmsg.NextSmesg(it, sPrefix, chKey, smsgStored))
|
||||
{
|
||||
uint32_t nPayload = smsgStored.vchMessage.size() - SMSG_HDR_LEN;
|
||||
@@ -121,7 +121,7 @@ public:
|
||||
delete it;
|
||||
|
||||
sPrefix = "sm";
|
||||
it = dbSmsg.pdb->NewIterator(leveldb::ReadOptions());
|
||||
it = dbSmsg.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbSmsg.NextSmesg(it, sPrefix, chKey, smsgStored))
|
||||
{
|
||||
uint32_t nPayload = smsgStored.vchMessage.size() - SMSG_HDR_LEN;
|
||||
@@ -620,20 +620,19 @@ void MessageModel::subscribeToCoreSignals()
|
||||
{
|
||||
qRegisterMetaType<SecMsgStored>("SecMsgStored");
|
||||
|
||||
// Connect signals
|
||||
NotifySecMsgInboxChanged.connect(boost::bind(NotifySecMsgInbox, this, _1));
|
||||
NotifySecMsgOutboxChanged.connect(boost::bind(NotifySecMsgOutbox, this, _1));
|
||||
NotifySecMsgWalletUnlocked.connect(boost::bind(NotifySecMsgWallet, this));
|
||||
|
||||
m_core_signal_connections.add(NotifySecMsgInboxChanged.connect(
|
||||
[this](SecMsgStored& hdr) { NotifySecMsgInbox(this, hdr); }));
|
||||
m_core_signal_connections.add(NotifySecMsgOutboxChanged.connect(
|
||||
[this](SecMsgStored& hdr) { NotifySecMsgOutbox(this, hdr); }));
|
||||
m_core_signal_connections.add(NotifySecMsgWalletUnlocked.connect(
|
||||
[this]() { NotifySecMsgWallet(this); }));
|
||||
|
||||
connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
|
||||
}
|
||||
|
||||
void MessageModel::unsubscribeFromCoreSignals()
|
||||
{
|
||||
// Disconnect signals
|
||||
NotifySecMsgInboxChanged.disconnect(boost::bind(NotifySecMsgInbox, this, _1));
|
||||
NotifySecMsgOutboxChanged.disconnect(boost::bind(NotifySecMsgOutbox, this, _1));
|
||||
NotifySecMsgWalletUnlocked.disconnect(boost::bind(NotifySecMsgWallet, this));
|
||||
|
||||
m_core_signal_connections.disconnect_all();
|
||||
|
||||
disconnect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include "allocators.h" /* for SecureString */
|
||||
#include "../util_signal.h"
|
||||
#include "smessage.h"
|
||||
#include <map>
|
||||
#include <QSortFilterProxyModel>
|
||||
@@ -175,6 +176,8 @@ private:
|
||||
void subscribeToCoreSignals();
|
||||
void unsubscribeFromCoreSignals();
|
||||
|
||||
CSignalConnections m_core_signal_connections;
|
||||
|
||||
public slots:
|
||||
|
||||
/* Check for new messages */
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "init.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
@@ -374,7 +374,7 @@ void OptionsDialog::on_dataDirBrowseButton_clicked()
|
||||
|
||||
void OptionsDialog::updateDataDirFreeSpace()
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
QString path = dataDirPath->text();
|
||||
fs::path fsPath(path.toStdString());
|
||||
try {
|
||||
@@ -395,7 +395,7 @@ void OptionsDialog::updateDataDirFreeSpace()
|
||||
|
||||
quint64 OptionsDialog::calculateDirSize(const QString& path)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
quint64 totalSize = 0;
|
||||
try {
|
||||
for (fs::recursive_directory_iterator it(path.toStdString()), end; it != end; ++it) {
|
||||
@@ -411,7 +411,7 @@ bool OptionsDialog::handleDataDirChange()
|
||||
if (m_pendingDataDir.isEmpty() || m_pendingDataDir == m_currentDataDir)
|
||||
return false;
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
fs::path destPath(m_pendingDataDir.toStdString());
|
||||
|
||||
// Check destination is writable
|
||||
|
||||
@@ -244,7 +244,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
|
||||
strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
|
||||
strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
|
||||
|
||||
CTxDB txdb("r"); // To fetch source txouts
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder; // To fetch source txouts
|
||||
|
||||
strHTML += "<br><b>" + tr("Inputs") + ":</b>";
|
||||
strHTML += "<ul>";
|
||||
|
||||
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
|
||||
// ... then triangles.conf:
|
||||
if (!boost::filesystem::is_directory(GetDataDir(false)))
|
||||
if (!std::filesystem::is_directory(GetDataDir(false)))
|
||||
{
|
||||
// This message can not be translated, as translation is not initialized yet
|
||||
// (which not yet possible because lang=XX can be overridden in triangles.conf in the data directory)
|
||||
|
||||
+11
-8
@@ -545,18 +545,21 @@ static void NotifyTransactionChanged(WalletModel *walletmodel, CWallet *wallet,
|
||||
|
||||
void WalletModel::subscribeToCoreSignals()
|
||||
{
|
||||
// Connect signals to wallet
|
||||
wallet->NotifyStatusChanged.connect(boost::bind(&NotifyKeyStoreStatusChanged, this, _1));
|
||||
wallet->NotifyAddressBookChanged.connect(boost::bind(NotifyAddressBookChanged, this, _1, _2, _3, _4, _5));
|
||||
wallet->NotifyTransactionChanged.connect(boost::bind(NotifyTransactionChanged, this, _1, _2, _3));
|
||||
m_core_signal_connections.add(wallet->NotifyStatusChanged.connect(
|
||||
[this](CCryptoKeyStore* w) { NotifyKeyStoreStatusChanged(this, w); }));
|
||||
m_core_signal_connections.add(wallet->NotifyAddressBookChanged.connect(
|
||||
[this](CWallet* w, const CTxDestination& address, const std::string& label, bool isMine, ChangeType status) {
|
||||
NotifyAddressBookChanged(this, w, address, label, isMine, status);
|
||||
}));
|
||||
m_core_signal_connections.add(wallet->NotifyTransactionChanged.connect(
|
||||
[this](CWallet* w, const uint256& hash, ChangeType status) {
|
||||
NotifyTransactionChanged(this, w, hash, status);
|
||||
}));
|
||||
}
|
||||
|
||||
void WalletModel::unsubscribeFromCoreSignals()
|
||||
{
|
||||
// Disconnect signals from wallet
|
||||
wallet->NotifyStatusChanged.disconnect(boost::bind(&NotifyKeyStoreStatusChanged, this, _1));
|
||||
wallet->NotifyAddressBookChanged.disconnect(boost::bind(NotifyAddressBookChanged, this, _1, _2, _3, _4, _5));
|
||||
wallet->NotifyTransactionChanged.disconnect(boost::bind(NotifyTransactionChanged, this, _1, _2, _3));
|
||||
m_core_signal_connections.disconnect_all();
|
||||
}
|
||||
|
||||
// WalletModel::UnlockContext implementation
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <QMutex>
|
||||
|
||||
#include "allocators.h" /* for SecureString */
|
||||
#include "../util_signal.h"
|
||||
|
||||
class OptionsModel;
|
||||
class AddressTableModel;
|
||||
@@ -157,6 +158,8 @@ private:
|
||||
void unsubscribeFromCoreSignals();
|
||||
bool checkBalanceChanged();
|
||||
|
||||
CSignalConnections m_core_signal_connections;
|
||||
|
||||
|
||||
public slots:
|
||||
/* Wallet status might have changed */
|
||||
|
||||
+330
-32
@@ -9,6 +9,20 @@
|
||||
#include "addressindex.h"
|
||||
#include "txdb.h"
|
||||
#include "base58.h"
|
||||
#include "utxosnapshot.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
// Windows.h (transitively included) defines these as macros, clobbering Checkpoints:: enum values.
|
||||
#ifdef STRICT
|
||||
#undef STRICT
|
||||
#endif
|
||||
#ifdef ADVISORY
|
||||
#undef ADVISORY
|
||||
#endif
|
||||
#ifdef PERMISSIVE
|
||||
#undef PERMISSIVE
|
||||
#endif
|
||||
|
||||
using namespace json_spirit;
|
||||
using namespace std;
|
||||
@@ -363,49 +377,287 @@ Value gettxoutsetinfo(const Array& params, bool fHelp)
|
||||
return obj;
|
||||
}
|
||||
|
||||
Value recalculatesupply(const Array& params, bool fHelp)
|
||||
static void GetActiveChainVector(std::vector<CBlockIndex*>& chain)
|
||||
{
|
||||
if (fHelp || params.size() != 0)
|
||||
throw runtime_error(
|
||||
"recalculatesupply\n"
|
||||
"Recalculates the money supply by summing all unspent transaction outputs.\n"
|
||||
"Updates the stored money supply value at the chain tip and persists it to disk.\n"
|
||||
"Returns the old and new supply values for comparison.\n"
|
||||
"\nWARNING: This modifies blockchain index state. Only use if money supply is incorrect.");
|
||||
chain.clear();
|
||||
|
||||
if (!pindexBest)
|
||||
throw runtime_error("recalculatesupply: no best block");
|
||||
|
||||
for (CBlockIndex* pindex = pindexBest; pindex; pindex = pindex->pprev)
|
||||
chain.push_back(pindex);
|
||||
|
||||
std::reverse(chain.begin(), chain.end());
|
||||
}
|
||||
|
||||
static int64_t ComputeActiveChainSupplyFromBlocks(const std::vector<CBlockIndex*>& chain, int& nBlocksScanned, int& nTransactionsScanned)
|
||||
{
|
||||
nBlocksScanned = 0;
|
||||
nTransactionsScanned = 0;
|
||||
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
int64_t nSupply = 0;
|
||||
|
||||
for (std::vector<CBlockIndex*>::const_iterator pindexIt = chain.begin(); pindexIt != chain.end(); ++pindexIt)
|
||||
{
|
||||
CBlockIndex* pindex = *pindexIt;
|
||||
if (!pindex)
|
||||
throw runtime_error("recalculatesupply: null active-chain block index");
|
||||
|
||||
if (pindex->nHeight == 0)
|
||||
{
|
||||
nBlocksScanned++;
|
||||
continue;
|
||||
}
|
||||
|
||||
CBlock block;
|
||||
int64_t nBlockValueIn = 0;
|
||||
int64_t nBlockValueOut = 0;
|
||||
|
||||
if (!block.ReadFromDisk(pindex))
|
||||
throw runtime_error(strprintf("recalculatesupply: failed reading block at height %d", pindex->nHeight));
|
||||
|
||||
for (std::vector<CTransaction>::const_iterator txIt = block.vtx.begin(); txIt != block.vtx.end(); ++txIt)
|
||||
{
|
||||
const CTransaction& tx = *txIt;
|
||||
nTransactionsScanned++;
|
||||
nBlockValueOut += tx.GetValueOut();
|
||||
|
||||
if (!tx.IsCoinBase())
|
||||
{
|
||||
for (std::vector<CTxIn>::const_iterator txinIt = tx.vin.begin(); txinIt != tx.vin.end(); ++txinIt)
|
||||
{
|
||||
const CTxIn& txin = *txinIt;
|
||||
CTxIndex txindex;
|
||||
CTransaction txPrev;
|
||||
if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
|
||||
throw runtime_error(strprintf(
|
||||
"recalculatesupply: failed reading prevout %s:%u while processing height %d",
|
||||
txin.prevout.hash.ToString().c_str(), txin.prevout.n, pindex->nHeight));
|
||||
|
||||
if (txin.prevout.n >= txPrev.vout.size())
|
||||
throw runtime_error(strprintf(
|
||||
"recalculatesupply: prevout index %u out of range for tx %s at height %d",
|
||||
txin.prevout.n, txin.prevout.hash.ToString().c_str(), pindex->nHeight));
|
||||
|
||||
nBlockValueIn += txPrev.vout[txin.prevout.n].nValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nSupply += (nBlockValueOut - nBlockValueIn);
|
||||
nBlocksScanned++;
|
||||
}
|
||||
|
||||
return nSupply;
|
||||
}
|
||||
|
||||
Value recalculatesupply(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 1)
|
||||
throw runtime_error(
|
||||
"recalculatesupply [apply=false]\n"
|
||||
"Rebuilds money supply by walking the active chain from genesis and summing (valueOut - valueIn) per block.\n"
|
||||
"Also returns the current UTXO-set total for comparison.\n"
|
||||
"If apply=true, rewrites nMoneySupply for every block on the active chain and persists the repaired values.\n"
|
||||
"\nThis is intended for repairing corrupted money-supply tracking after chain/index incidents.");
|
||||
|
||||
bool fApply = false;
|
||||
if (params.size() == 1)
|
||||
fApply = params[0].get_bool();
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
if (!pindexBest)
|
||||
throw runtime_error("recalculatesupply: no best block");
|
||||
|
||||
auto txdbRead_holder = MakeChainDB("r"); CTxDBBase& txdbRead = *txdbRead_holder;
|
||||
int nUtxoCount = 0;
|
||||
CTxDB txdb;
|
||||
int64_t nCalculatedSupply = txdb.SumUtxoValues(nUtxoCount);
|
||||
int64_t nOldSupply = pindexBest->nMoneySupply;
|
||||
int64_t nDifference = nCalculatedSupply - nOldSupply;
|
||||
int64_t nUtxoSupply = txdbRead.SumUtxoValues(nUtxoCount);
|
||||
|
||||
// Sanity check: difference should be reasonable (not millions of TRI)
|
||||
// Max supply is 2,222,222 TRI, so any difference > 1M TRI is suspicious
|
||||
if (abs64(nDifference) > 1000000 * COIN)
|
||||
throw runtime_error(strprintf(
|
||||
"recalculatesupply: calculated supply differs by %s TRI - this is abnormal, refusing to update",
|
||||
FormatMoney(abs64(nDifference)).c_str()));
|
||||
std::vector<CBlockIndex*> activeChain;
|
||||
GetActiveChainVector(activeChain);
|
||||
|
||||
// Update the chain tip's money supply
|
||||
pindexBest->nMoneySupply = nCalculatedSupply;
|
||||
int nBlocksScanned = 0;
|
||||
int nTransactionsScanned = 0;
|
||||
int64_t nHistoricalSupply = ComputeActiveChainSupplyFromBlocks(activeChain, nBlocksScanned, nTransactionsScanned);
|
||||
|
||||
// Persist to LevelDB
|
||||
CTxDB txdbWrite;
|
||||
if (!txdbWrite.WriteBlockIndex(CDiskBlockIndex(pindexBest)))
|
||||
throw runtime_error("recalculatesupply: failed to write updated block index");
|
||||
int64_t nOldTipSupply = pindexBest->nMoneySupply;
|
||||
|
||||
if (fApply)
|
||||
{
|
||||
auto txdbWrite_holder = MakeChainDB(); CTxDBBase& txdbWrite = *txdbWrite_holder;
|
||||
int64_t nRunningSupply = 0;
|
||||
|
||||
for (std::vector<CBlockIndex*>::const_iterator pindexIt = activeChain.begin(); pindexIt != activeChain.end(); ++pindexIt)
|
||||
{
|
||||
CBlockIndex* pindex = *pindexIt;
|
||||
if (!pindex)
|
||||
throw runtime_error("recalculatesupply: null active-chain block index during apply");
|
||||
|
||||
if (pindex->nHeight == 0)
|
||||
{
|
||||
pindex->nMoneySupply = 0;
|
||||
if (!txdbWrite.WriteBlockIndex(CDiskBlockIndex(pindex)))
|
||||
throw runtime_error("recalculatesupply: failed to persist genesis block index during apply");
|
||||
continue;
|
||||
}
|
||||
|
||||
CBlock block;
|
||||
int64_t nBlockValueIn = 0;
|
||||
int64_t nBlockValueOut = 0;
|
||||
|
||||
if (!block.ReadFromDisk(pindex))
|
||||
throw runtime_error(strprintf("recalculatesupply: failed reading block at height %d during apply", pindex->nHeight));
|
||||
|
||||
for (std::vector<CTransaction>::const_iterator txIt = block.vtx.begin(); txIt != block.vtx.end(); ++txIt)
|
||||
{
|
||||
const CTransaction& tx = *txIt;
|
||||
nBlockValueOut += tx.GetValueOut();
|
||||
|
||||
if (!tx.IsCoinBase())
|
||||
{
|
||||
for (std::vector<CTxIn>::const_iterator txinIt = tx.vin.begin(); txinIt != tx.vin.end(); ++txinIt)
|
||||
{
|
||||
const CTxIn& txin = *txinIt;
|
||||
CTxIndex txindex;
|
||||
CTransaction txPrev;
|
||||
if (!txPrev.ReadFromDisk(txdbWrite, txin.prevout, txindex))
|
||||
throw runtime_error(strprintf(
|
||||
"recalculatesupply: failed reading prevout %s:%u during apply at height %d",
|
||||
txin.prevout.hash.ToString().c_str(), txin.prevout.n, pindex->nHeight));
|
||||
if (txin.prevout.n >= txPrev.vout.size())
|
||||
throw runtime_error(strprintf(
|
||||
"recalculatesupply: prevout index %u out of range during apply for tx %s at height %d",
|
||||
txin.prevout.n, txin.prevout.hash.ToString().c_str(), pindex->nHeight));
|
||||
nBlockValueIn += txPrev.vout[txin.prevout.n].nValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nRunningSupply += (nBlockValueOut - nBlockValueIn);
|
||||
pindex->nMoneySupply = nRunningSupply;
|
||||
|
||||
if (!txdbWrite.WriteBlockIndex(CDiskBlockIndex(pindex)))
|
||||
throw runtime_error(strprintf("recalculatesupply: failed to persist block index at height %d", pindex->nHeight));
|
||||
}
|
||||
}
|
||||
|
||||
Object result;
|
||||
result.push_back(Pair("height", (int)nBestHeight));
|
||||
result.push_back(Pair("old_supply", ValueFromAmount(nOldSupply)));
|
||||
result.push_back(Pair("new_supply", ValueFromAmount(nCalculatedSupply)));
|
||||
result.push_back(Pair("difference", ValueFromAmount(nDifference)));
|
||||
result.push_back(Pair("tip_bestblock", hashBestChain.GetHex()));
|
||||
result.push_back(Pair("old_tip_supply", ValueFromAmount(nOldTipSupply)));
|
||||
result.push_back(Pair("recalculated_chain_supply", ValueFromAmount(nHistoricalSupply)));
|
||||
result.push_back(Pair("utxo_supply", ValueFromAmount(nUtxoSupply)));
|
||||
result.push_back(Pair("tip_vs_recalculated", ValueFromAmount(nHistoricalSupply - nOldTipSupply)));
|
||||
result.push_back(Pair("utxo_vs_recalculated", ValueFromAmount(nHistoricalSupply - nUtxoSupply)));
|
||||
result.push_back(Pair("blocks_scanned", nBlocksScanned));
|
||||
result.push_back(Pair("transactions_scanned", nTransactionsScanned));
|
||||
result.push_back(Pair("utxo_count", nUtxoCount));
|
||||
result.push_back(Pair("applied", fApply));
|
||||
return result;
|
||||
}
|
||||
|
||||
// Walks every non-coinbase transaction input across [start_height, end_height]
|
||||
// and runs the existing VerifySignature path. Reports counts and the first 100
|
||||
// failures so the caller can spot regressions when the underlying ECDSA
|
||||
// implementation changes (e.g. OpenSSL EC -> libsecp256k1).
|
||||
Value auditsignatures(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 2)
|
||||
throw runtime_error(
|
||||
"auditsignatures [start_height] [end_height]\n"
|
||||
"Walk the active chain in [start_height, end_height] (inclusive) and run\n"
|
||||
"VerifySignature on every non-coinbase input. Returns counts plus up to 100\n"
|
||||
"failures.\n"
|
||||
"Defaults: start = max(1, tip-1000), end = tip.\n"
|
||||
"Pre-migration this should always report 0 failures; post-migration any non-zero\n"
|
||||
"result identifies a behavioural regression in the new ECDSA path.");
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
if (!pindexBest)
|
||||
throw runtime_error("auditsignatures: no best block");
|
||||
|
||||
int tip = nBestHeight;
|
||||
int start = (params.size() > 0) ? params[0].get_int() : std::max(1, tip - 1000);
|
||||
int end = (params.size() > 1) ? params[1].get_int() : tip;
|
||||
|
||||
if (start < 1) throw runtime_error("auditsignatures: start_height must be >= 1");
|
||||
if (end > tip) throw runtime_error("auditsignatures: end_height exceeds tip");
|
||||
if (start > end) throw runtime_error("auditsignatures: start_height > end_height");
|
||||
|
||||
// Build forward walk by descending from tip.
|
||||
CBlockIndex* pindex = pindexBest;
|
||||
while (pindex && pindex->nHeight > end)
|
||||
pindex = pindex->pprev;
|
||||
|
||||
std::vector<CBlockIndex*> walk;
|
||||
while (pindex && pindex->nHeight >= start) {
|
||||
walk.push_back(pindex);
|
||||
pindex = pindex->pprev;
|
||||
}
|
||||
std::reverse(walk.begin(), walk.end());
|
||||
|
||||
int nBlocksScanned = 0;
|
||||
int64_t nInputsChecked = 0;
|
||||
int64_t nInputsFailed = 0;
|
||||
Array failures;
|
||||
const size_t kMaxFailures = 100;
|
||||
|
||||
auto recordFailure = [&](int height, const uint256& txid, unsigned int vin, const char* reason) {
|
||||
++nInputsFailed;
|
||||
if (failures.size() >= kMaxFailures) return;
|
||||
Object f;
|
||||
f.push_back(Pair("height", height));
|
||||
f.push_back(Pair("txid", txid.GetHex()));
|
||||
f.push_back(Pair("vin", (int)vin));
|
||||
f.push_back(Pair("reason", reason));
|
||||
failures.push_back(f);
|
||||
};
|
||||
|
||||
for (CBlockIndex* pi : walk) {
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(pi, true)) {
|
||||
++nBlocksScanned;
|
||||
continue;
|
||||
}
|
||||
for (const CTransaction& tx : block.vtx) {
|
||||
if (tx.IsCoinBase()) continue;
|
||||
for (unsigned int i = 0; i < tx.vin.size(); ++i) {
|
||||
const COutPoint& prev = tx.vin[i].prevout;
|
||||
CTransaction txFrom;
|
||||
uint256 hashBlock;
|
||||
if (!GetTransaction(prev.hash, txFrom, hashBlock)) {
|
||||
recordFailure(pi->nHeight, tx.GetHash(), i, "prevout transaction not found");
|
||||
continue;
|
||||
}
|
||||
if (prev.n >= txFrom.vout.size()) {
|
||||
recordFailure(pi->nHeight, tx.GetHash(), i, "prevout index out of range");
|
||||
continue;
|
||||
}
|
||||
++nInputsChecked;
|
||||
if (!VerifySignature(txFrom, tx, i, 0))
|
||||
recordFailure(pi->nHeight, tx.GetHash(), i, "VerifySignature returned false");
|
||||
}
|
||||
}
|
||||
++nBlocksScanned;
|
||||
if (nBlocksScanned % 1000 == 0)
|
||||
printf("auditsignatures: scanned %d blocks, %lld inputs, %lld failures\n",
|
||||
nBlocksScanned, (long long)nInputsChecked, (long long)nInputsFailed);
|
||||
}
|
||||
|
||||
Object result;
|
||||
result.push_back(Pair("start_height", start));
|
||||
result.push_back(Pair("end_height", end));
|
||||
result.push_back(Pair("blocks_scanned", nBlocksScanned));
|
||||
result.push_back(Pair("inputs_checked", nInputsChecked));
|
||||
result.push_back(Pair("inputs_failed", nInputsFailed));
|
||||
result.push_back(Pair("failures", failures));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// triangles: get information of sync-checkpoint
|
||||
Value getcheckpoint(const Array& params, bool fHelp)
|
||||
{
|
||||
@@ -566,7 +818,7 @@ Value getaddressbalance(const Array& params, bool fHelp)
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + strAddr);
|
||||
|
||||
int64_t nBalance = 0;
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
txdb.ReadAddressBalance(nType, hashBytes, nBalance);
|
||||
nTotalBalance += nBalance;
|
||||
}
|
||||
@@ -601,7 +853,7 @@ Value getaddressutxos(const Array& params, bool fHelp)
|
||||
Array addrArray = find_value(addrObj, "addresses").get_array();
|
||||
|
||||
Array result;
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
|
||||
for (unsigned int i = 0; i < addrArray.size(); i++)
|
||||
{
|
||||
@@ -661,7 +913,7 @@ Value getaddresstxids(const Array& params, bool fHelp)
|
||||
nEndHeight = endVal.get_int();
|
||||
|
||||
Array result;
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
|
||||
// Use a set to deduplicate txids across multiple addresses
|
||||
std::set<uint256> setTxIds;
|
||||
@@ -767,7 +1019,7 @@ Value invalidateblock(const Array& params, bool fHelp)
|
||||
|
||||
if (pindex->IsInMainChain())
|
||||
{
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
if (!txdb.TxnBegin())
|
||||
throw runtime_error("Failed to begin transaction.");
|
||||
|
||||
@@ -836,7 +1088,7 @@ Value reconsiderblock(const Array& params, bool fHelp)
|
||||
if (!block.ReadFromDisk(pindex))
|
||||
throw runtime_error("Failed to read block from disk.");
|
||||
|
||||
CTxDB txdb;
|
||||
auto txdb_holder = MakeChainDB(); CTxDBBase& txdb = *txdb_holder;
|
||||
block.SetBestChain(txdb, pindex);
|
||||
printf("reconsiderblock: reconsidered block %s at height %d, new best height=%d\n",
|
||||
hash.ToString().c_str(), pindex->nHeight, nBestHeight);
|
||||
@@ -849,3 +1101,49 @@ Value reconsiderblock(const Array& params, bool fHelp)
|
||||
|
||||
return Value::null;
|
||||
}
|
||||
|
||||
Value dumputxoset(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 1 || params.size() > 2)
|
||||
throw runtime_error(
|
||||
"dumputxoset <filename> [nheaders]\n"
|
||||
"Dumps the current UTXO set and recent block headers to a binary snapshot file.\n"
|
||||
"The snapshot can be used by new nodes to skip initial block download.\n"
|
||||
"\nArguments:\n"
|
||||
"1. filename (string, required) Destination file path\n"
|
||||
"2. nheaders (int, optional, default=2000) Number of block headers to include\n"
|
||||
"\nResult:\n"
|
||||
"{\n"
|
||||
" \"filename\": \"...\",\n"
|
||||
" \"height\": n,\n"
|
||||
" \"blockhash\": \"...\",\n"
|
||||
" \"file_size\": n\n"
|
||||
"}");
|
||||
|
||||
string filename = params[0].get_str();
|
||||
unsigned int nHeaders = UTXO_SNAPSHOT_DEFAULT_HEADERS;
|
||||
if (params.size() > 1)
|
||||
nHeaders = params[1].get_int();
|
||||
|
||||
if (nHeaders < 100)
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "nheaders must be at least 100");
|
||||
|
||||
std::filesystem::path destPath(filename);
|
||||
std::string strError;
|
||||
|
||||
if (!UtxoSnapshot::DumpSnapshot(destPath, nHeaders, strError))
|
||||
throw runtime_error("dumputxoset failed: " + strError);
|
||||
|
||||
// Get file size
|
||||
int64_t nFileSize = 0;
|
||||
if (std::filesystem::exists(destPath))
|
||||
nFileSize = (int64_t)std::filesystem::file_size(destPath);
|
||||
|
||||
Object result;
|
||||
result.push_back(Pair("filename", filename));
|
||||
result.push_back(Pair("height", nBestHeight));
|
||||
result.push_back(Pair("blockhash", hashBestChain.GetHex()));
|
||||
result.push_back(Pair("file_size", nFileSize));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+85
-66
@@ -6,7 +6,6 @@
|
||||
#include "net.h"
|
||||
#include "addrman.h"
|
||||
#include "trianglesrpc.h"
|
||||
#include "alert.h"
|
||||
#include "wallet.h"
|
||||
#include "db.h"
|
||||
#include "walletdb.h"
|
||||
@@ -97,6 +96,9 @@ Value getpeerinfo(const Array& params, bool fHelp)
|
||||
obj.push_back(Pair("inbound", stats.fInbound));
|
||||
obj.push_back(Pair("startingheight", stats.nStartingHeight));
|
||||
obj.push_back(Pair("banscore", stats.nMisbehavior));
|
||||
obj.push_back(Pair("pingtime", stats.nPingUsecTime > 0 ? (double)stats.nPingUsecTime / 1000000.0 : -1.0));
|
||||
obj.push_back(Pair("blocksdelivered", stats.nBlocksDelivered));
|
||||
obj.push_back(Pair("avglatency", stats.nAvgBlockLatencyUs > 0 ? (double)stats.nAvgBlockLatencyUs / 1000.0 : -1.0));
|
||||
|
||||
ret.push_back(obj);
|
||||
}
|
||||
@@ -104,71 +106,6 @@ Value getpeerinfo(const Array& params, bool fHelp)
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern CCriticalSection cs_mapAlerts;
|
||||
extern map<uint256, CAlert> mapAlerts;
|
||||
|
||||
// triangles: send alert.
|
||||
// There is a known deadlock situation with ThreadMessageHandler
|
||||
// ThreadMessageHandler: holds cs_vSend and acquiring cs_main in SendMessages()
|
||||
// ThreadRPCServer: holds cs_main and acquiring cs_vSend in alert.RelayTo()/PushMessage()/BeginMessage()
|
||||
Value sendalert(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 6)
|
||||
throw runtime_error(
|
||||
"sendalert <message> <privatekey> <minver> <maxver> <priority> <id> [cancelupto]\n"
|
||||
"<message> is the alert text message\n"
|
||||
"<privatekey> is hex string of alert master private key\n"
|
||||
"<minver> is the minimum applicable internal client version\n"
|
||||
"<maxver> is the maximum applicable internal client version\n"
|
||||
"<priority> is integer priority number\n"
|
||||
"<id> is the alert id\n"
|
||||
"[cancelupto] cancels all alert id's up to this number\n"
|
||||
"Returns true or false.");
|
||||
|
||||
CAlert alert;
|
||||
CKey key;
|
||||
|
||||
alert.strStatusBar = params[0].get_str();
|
||||
alert.nMinVer = params[2].get_int();
|
||||
alert.nMaxVer = params[3].get_int();
|
||||
alert.nPriority = params[4].get_int();
|
||||
alert.nID = params[5].get_int();
|
||||
if (params.size() > 6)
|
||||
alert.nCancel = params[6].get_int();
|
||||
alert.nVersion = PROTOCOL_VERSION;
|
||||
alert.nRelayUntil = GetAdjustedTime() + 365*24*60*60;
|
||||
alert.nExpiration = GetAdjustedTime() + 365*24*60*60;
|
||||
|
||||
CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
|
||||
sMsg << (CUnsignedAlert)alert;
|
||||
alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end());
|
||||
|
||||
vector<unsigned char> vchPrivKey = ParseHex(params[1].get_str());
|
||||
key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
|
||||
if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig))
|
||||
throw runtime_error(
|
||||
"Unable to sign alert, check private key?\n");
|
||||
if(!alert.ProcessAlert())
|
||||
throw runtime_error(
|
||||
"Failed to process alert.\n");
|
||||
// Relay alert
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (CNode* pnode : vNodes)
|
||||
alert.RelayTo(pnode);
|
||||
}
|
||||
|
||||
Object result;
|
||||
result.push_back(Pair("strStatusBar", alert.strStatusBar));
|
||||
result.push_back(Pair("nVersion", alert.nVersion));
|
||||
result.push_back(Pair("nMinVer", alert.nMinVer));
|
||||
result.push_back(Pair("nMaxVer", alert.nMaxVer));
|
||||
result.push_back(Pair("nPriority", alert.nPriority));
|
||||
result.push_back(Pair("nID", alert.nID));
|
||||
if (alert.nCancel > 0)
|
||||
result.push_back(Pair("nCancel", alert.nCancel));
|
||||
return result;
|
||||
}
|
||||
|
||||
Value addnode(const Array& params, bool fHelp)
|
||||
{
|
||||
@@ -264,3 +201,85 @@ Value getseedlist(const Array& params, bool fHelp)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Value getnetworkstability(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() != 0)
|
||||
throw runtime_error(
|
||||
"getnetworkstability\n"
|
||||
"Returns detailed network stability metrics including peer quality,\n"
|
||||
"connection health, and isolation risk assessment.");
|
||||
|
||||
int nOutbound = 0, nInbound = 0, nTotal = 0;
|
||||
int64_t nBestPing = INT64_MAX, nWorstPing = 0, nTotalPing = 0;
|
||||
int nPingCount = 0;
|
||||
int nTotalBlocksDelivered = 0;
|
||||
int64_t nOldestConnection = 0;
|
||||
int64_t nNewestConnection = INT64_MAX;
|
||||
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
nTotal = vNodes.size();
|
||||
for (CNode* pnode : vNodes) {
|
||||
if (pnode->fInbound)
|
||||
nInbound++;
|
||||
else
|
||||
nOutbound++;
|
||||
|
||||
if (pnode->nPingUsecTime > 0) {
|
||||
nTotalPing += pnode->nPingUsecTime;
|
||||
nPingCount++;
|
||||
if (pnode->nPingUsecTime < nBestPing)
|
||||
nBestPing = pnode->nPingUsecTime;
|
||||
if (pnode->nPingUsecTime > nWorstPing)
|
||||
nWorstPing = pnode->nPingUsecTime;
|
||||
}
|
||||
|
||||
nTotalBlocksDelivered += pnode->nBlocksDelivered;
|
||||
|
||||
int64_t uptime = GetTime() - pnode->nTimeConnected;
|
||||
if (uptime > nOldestConnection)
|
||||
nOldestConnection = uptime;
|
||||
if (uptime < nNewestConnection)
|
||||
nNewestConnection = uptime;
|
||||
}
|
||||
}
|
||||
|
||||
// Determine isolation risk
|
||||
string strRisk;
|
||||
if (nOutbound == 0 && nInbound == 0)
|
||||
strRisk = "critical";
|
||||
else if (nOutbound == 0)
|
||||
strRisk = "high";
|
||||
else if (nOutbound == 1)
|
||||
strRisk = "elevated";
|
||||
else if (nOutbound < 3)
|
||||
strRisk = "moderate";
|
||||
else
|
||||
strRisk = "low";
|
||||
|
||||
Object obj;
|
||||
obj.push_back(Pair("connections_total", nTotal));
|
||||
obj.push_back(Pair("connections_outbound", nOutbound));
|
||||
obj.push_back(Pair("connections_inbound", nInbound));
|
||||
obj.push_back(Pair("isolation_risk", strRisk));
|
||||
obj.push_back(Pair("blocks_delivered_total", nTotalBlocksDelivered));
|
||||
obj.push_back(Pair("known_addresses", (int)addrman.size()));
|
||||
|
||||
Object pingObj;
|
||||
pingObj.push_back(Pair("best_ms", nPingCount > 0 ? (double)nBestPing / 1000.0 : -1.0));
|
||||
pingObj.push_back(Pair("worst_ms", nPingCount > 0 ? (double)nWorstPing / 1000.0 : -1.0));
|
||||
pingObj.push_back(Pair("avg_ms", nPingCount > 0 ? (double)nTotalPing / nPingCount / 1000.0 : -1.0));
|
||||
pingObj.push_back(Pair("peers_measured", nPingCount));
|
||||
obj.push_back(Pair("ping", pingObj));
|
||||
|
||||
Object uptimeObj;
|
||||
uptimeObj.push_back(Pair("newest_sec", nTotal > 0 ? (boost::int64_t)nNewestConnection : 0));
|
||||
uptimeObj.push_back(Pair("oldest_sec", nTotal > 0 ? (boost::int64_t)nOldestConnection : 0));
|
||||
obj.push_back(Pair("connection_uptime", uptimeObj));
|
||||
|
||||
obj.push_back(Pair("seconds_since_last_block", (boost::int64_t)(GetTime() - nTimeBestReceived)));
|
||||
obj.push_back(Pair("current_height", nBestHeight));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -371,7 +371,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
|
||||
CTransaction tempTx;
|
||||
MapPrevTx mapPrevTx;
|
||||
MapPrevTx mapEmpty;
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
bool fInvalid;
|
||||
|
||||
// FetchInputs aborts on failure, so we go one at a time.
|
||||
@@ -548,7 +548,7 @@ Value sendrawtransaction(const Array& params, bool fHelp)
|
||||
else
|
||||
{
|
||||
// push to local node
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
if (!tx.AcceptToMemoryPool(txdb))
|
||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX rejected");
|
||||
|
||||
|
||||
+14
-12
@@ -10,6 +10,8 @@
|
||||
#include "smessage.h"
|
||||
#include "init.h" // pwalletMain
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
using namespace json_spirit;
|
||||
using namespace std;
|
||||
|
||||
@@ -607,7 +609,7 @@ Value smsginbox(const Array& params, bool fHelp)
|
||||
{
|
||||
dbInbox.TxnBegin();
|
||||
|
||||
leveldb::Iterator* it = dbInbox.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbInbox.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbInbox.NextSmesgKey(it, sPrefix, chKey))
|
||||
{
|
||||
dbInbox.EraseSmesg(chKey);
|
||||
@@ -629,7 +631,7 @@ Value smsginbox(const Array& params, bool fHelp)
|
||||
|
||||
dbInbox.TxnBegin();
|
||||
|
||||
leveldb::Iterator* it = dbInbox.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbInbox.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbInbox.NextSmesg(it, sPrefix, chKey, smsgStored))
|
||||
{
|
||||
if (fCheckReadStatus
|
||||
@@ -717,7 +719,7 @@ Value smsgoutbox(const Array& params, bool fHelp)
|
||||
{
|
||||
dbOutbox.TxnBegin();
|
||||
|
||||
leveldb::Iterator* it = dbOutbox.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbOutbox.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbOutbox.NextSmesgKey(it, sPrefix, chKey))
|
||||
{
|
||||
dbOutbox.EraseSmesg(chKey);
|
||||
@@ -734,7 +736,7 @@ Value smsgoutbox(const Array& params, bool fHelp)
|
||||
{
|
||||
SecMsgStored smsgStored;
|
||||
MessageData msg;
|
||||
leveldb::Iterator* it = dbOutbox.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbOutbox.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
while (dbOutbox.NextSmesg(it, sPrefix, chKey, smsgStored))
|
||||
{
|
||||
uint32_t nPayload = smsgStored.vchMessage.size() - SMSG_HDR_LEN;
|
||||
@@ -820,10 +822,10 @@ Value smsgbuckets(const Array& params, bool fHelp)
|
||||
objM.push_back(Pair("hash", sHash));
|
||||
objM.push_back(Pair("last changed", getTimeString(it->second.timeChanged, cbuf, sizeof(cbuf))));
|
||||
|
||||
boost::filesystem::path fullPath = GetDataDir() / "smsgStore" / sFile;
|
||||
std::filesystem::path fullPath = GetDataDir() / "smsgStore" / sFile;
|
||||
|
||||
|
||||
if (!boost::filesystem::exists(fullPath))
|
||||
if (!std::filesystem::exists(fullPath))
|
||||
{
|
||||
// -- If there is a file for an empty bucket something is wrong.
|
||||
if (tokenSet.size() == 0)
|
||||
@@ -835,10 +837,10 @@ Value smsgbuckets(const Array& params, bool fHelp)
|
||||
try {
|
||||
|
||||
uint64_t nFBytes = 0;
|
||||
nFBytes = boost::filesystem::file_size(fullPath);
|
||||
nFBytes = std::filesystem::file_size(fullPath);
|
||||
nBytes += nFBytes;
|
||||
objM.push_back(Pair("file size", fsReadable(nFBytes)));
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
objM.push_back(Pair("file size, error", ex.what()));
|
||||
};
|
||||
@@ -871,9 +873,9 @@ Value smsgbuckets(const Array& params, bool fHelp)
|
||||
std::string sFile = std::to_string(it->first) + "_01.dat";
|
||||
|
||||
try {
|
||||
boost::filesystem::path fullPath = GetDataDir() / "smsgStore" / sFile;
|
||||
boost::filesystem::remove(fullPath);
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
std::filesystem::path fullPath = GetDataDir() / "smsgStore" / sFile;
|
||||
std::filesystem::remove(fullPath);
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
//objM.push_back(Pair("file size, error", ex.what()));
|
||||
printf("Error removing bucket file %s.\n", ex.what());
|
||||
@@ -930,7 +932,7 @@ Value smsgbroadcast(const Array& params, bool fHelp)
|
||||
|
||||
// Iterate all "pk" entries
|
||||
std::string sPrefix("pk");
|
||||
leveldb::Iterator* it = dbPub.pdb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::Iterator* it = dbPub.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
for (it->Seek(sPrefix); it->Valid(); it->Next())
|
||||
{
|
||||
std::string key = it->key().ToString();
|
||||
|
||||
@@ -1858,181 +1858,3 @@ Value makekeypair(const Array& params, bool fHelp)
|
||||
result.push_back(Pair("PublicKey", HexStr(key.GetPubKey().Raw())));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Value clearwallettransactions(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 0)
|
||||
throw runtime_error(
|
||||
"clearwallettransactions \n"
|
||||
"delete all transactions from wallet - reload with scanforalltxns\n"
|
||||
"Warning: Backup your wallet first!");
|
||||
|
||||
|
||||
|
||||
Object result;
|
||||
|
||||
uint32_t nTransactions = 0;
|
||||
|
||||
char cbuf[256];
|
||||
|
||||
{
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
|
||||
CWalletDB walletdb(pwalletMain->strWalletFile);
|
||||
walletdb.TxnBegin();
|
||||
Dbc* pcursor = walletdb.GetTxnCursor();
|
||||
if (!pcursor)
|
||||
throw runtime_error("Cannot get wallet DB cursor");
|
||||
|
||||
// RAII guard ensures cursor is closed even on exception
|
||||
struct CursorGuard {
|
||||
Dbc* cur;
|
||||
CursorGuard(Dbc* c) : cur(c) {}
|
||||
~CursorGuard() { if (cur) cur->close(); }
|
||||
} cursorGuard(pcursor);
|
||||
|
||||
Dbt datKey;
|
||||
Dbt datValue;
|
||||
|
||||
datKey.set_flags(DB_DBT_USERMEM);
|
||||
datValue.set_flags(DB_DBT_USERMEM);
|
||||
|
||||
std::vector<unsigned char> vchKey;
|
||||
std::vector<unsigned char> vchType;
|
||||
std::vector<unsigned char> vchKeyData;
|
||||
std::vector<unsigned char> vchValueData;
|
||||
|
||||
vchKeyData.resize(100);
|
||||
vchValueData.resize(100);
|
||||
|
||||
datKey.set_ulen(vchKeyData.size());
|
||||
datKey.set_data(&vchKeyData[0]);
|
||||
|
||||
datValue.set_ulen(vchValueData.size());
|
||||
datValue.set_data(&vchValueData[0]);
|
||||
|
||||
unsigned int fFlags = DB_NEXT; // same as using DB_FIRST for new cursor
|
||||
while (true)
|
||||
{
|
||||
int ret = pcursor->get(&datKey, &datValue, fFlags);
|
||||
|
||||
if (ret == ENOMEM
|
||||
|| ret == DB_BUFFER_SMALL)
|
||||
{
|
||||
if (datKey.get_size() > datKey.get_ulen())
|
||||
{
|
||||
vchKeyData.resize(datKey.get_size());
|
||||
datKey.set_ulen(vchKeyData.size());
|
||||
datKey.set_data(&vchKeyData[0]);
|
||||
};
|
||||
|
||||
if (datValue.get_size() > datValue.get_ulen())
|
||||
{
|
||||
vchValueData.resize(datValue.get_size());
|
||||
datValue.set_ulen(vchValueData.size());
|
||||
datValue.set_data(&vchValueData[0]);
|
||||
};
|
||||
// -- try once more, when DB_BUFFER_SMALL cursor is not expected to move
|
||||
ret = pcursor->get(&datKey, &datValue, fFlags);
|
||||
};
|
||||
|
||||
if (ret == DB_NOTFOUND)
|
||||
break;
|
||||
else
|
||||
if (datKey.get_data() == NULL || datValue.get_data() == NULL
|
||||
|| ret != 0)
|
||||
{
|
||||
const char* dbErr = db_strerror(ret);
|
||||
snprintf(cbuf, sizeof(cbuf), "wallet DB error %d, %s", ret, dbErr ? dbErr : "unknown");
|
||||
throw runtime_error(cbuf);
|
||||
};
|
||||
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue.SetType(SER_DISK);
|
||||
ssValue.clear();
|
||||
ssValue.write((char*)datKey.get_data(), datKey.get_size());
|
||||
|
||||
ssValue >> vchType;
|
||||
|
||||
|
||||
std::string strType(vchType.begin(), vchType.end());
|
||||
|
||||
//printf("strType %s\n", strType.c_str());
|
||||
|
||||
if (strType == "tx")
|
||||
{
|
||||
uint256 hash;
|
||||
ssValue >> hash;
|
||||
|
||||
if ((ret = pcursor->del(0)) != 0)
|
||||
{
|
||||
printf("Delete transaction failed %d, %s\n", ret, db_strerror(ret));
|
||||
continue;
|
||||
};
|
||||
|
||||
pwalletMain->mapWallet.erase(hash);
|
||||
try { pwalletMain->NotifyTransactionChanged(pwalletMain, hash, CT_DELETED); }
|
||||
catch (...) {
|
||||
printf("clearwallettransactions: NotifyTransactionChanged failed\n");
|
||||
}
|
||||
|
||||
nTransactions++;
|
||||
};
|
||||
};
|
||||
cursorGuard.cur = nullptr; // mark as handled
|
||||
pcursor->close();
|
||||
walletdb.TxnCommit();
|
||||
}
|
||||
|
||||
snprintf(cbuf, sizeof(cbuf), "Removed %u transactions.", nTransactions);
|
||||
result.push_back(Pair("complete", std::string(cbuf)));
|
||||
result.push_back(Pair("", "Reload with scanforstealthtxns or re-download blockchain."));
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Value scanforalltxns(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 1)
|
||||
throw runtime_error(
|
||||
"scanforalltxns [fromHeight]\n"
|
||||
"Scan blockchain for owned transactions.");
|
||||
|
||||
Object result;
|
||||
int32_t nFromHeight = 0;
|
||||
|
||||
CBlockIndex *pindex = pindexGenesisBlock;
|
||||
|
||||
|
||||
if (params.size() > 0)
|
||||
nFromHeight = params[0].get_int();
|
||||
|
||||
|
||||
if (nFromHeight > 0)
|
||||
{
|
||||
pindex = mapBlockIndex[hashBestChain];
|
||||
while (pindex->nHeight > nFromHeight
|
||||
&& pindex->pprev)
|
||||
pindex = pindex->pprev;
|
||||
};
|
||||
|
||||
if (pindex == NULL)
|
||||
throw runtime_error("Genesis Block is not set.");
|
||||
|
||||
{
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
|
||||
pwalletMain->MarkDirty();
|
||||
|
||||
pwalletMain->ScanForWalletTransactions(pindex, true);
|
||||
pwalletMain->ReacceptWalletTransactions();
|
||||
}
|
||||
|
||||
result.push_back(Pair("result", "Scan complete."));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Submodule
+1
Submodule src/secp256k1 added at 1a53f4961f
@@ -11,6 +11,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <cassert>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <stdint.h>
|
||||
#include <cstring>
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2024 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
#ifndef TRIANGLES_SCRIPT_VERIFY_CACHE_H
|
||||
#define TRIANGLES_SCRIPT_VERIFY_CACHE_H
|
||||
|
||||
#include "uint256.h"
|
||||
#include "sync.h"
|
||||
|
||||
#include <openssl/sha.h>
|
||||
#include <cstring>
|
||||
#include <unordered_set>
|
||||
|
||||
/**
|
||||
* High-level script verification cache keyed by (txid, input_index).
|
||||
* Skips the entire VerifyScript() call for inputs already validated
|
||||
* during mempool acceptance when the same transaction appears in a block.
|
||||
*
|
||||
* Complements the lower-level CSignatureCache in script.cpp which caches
|
||||
* individual ECDSA signature checks.
|
||||
*
|
||||
* ~256KB memory footprint at 32K entries.
|
||||
*/
|
||||
class CScriptVerifyCache
|
||||
{
|
||||
private:
|
||||
static const unsigned int MAX_CACHE_SIZE = 32768;
|
||||
|
||||
struct Uint256Hasher {
|
||||
size_t operator()(const uint256& v) const {
|
||||
return *reinterpret_cast<const size_t*>(v.begin());
|
||||
}
|
||||
};
|
||||
|
||||
mutable CCriticalSection cs;
|
||||
std::unordered_set<uint256, Uint256Hasher> setValid;
|
||||
|
||||
uint256 ComputeKey(const uint256& txid, unsigned int nIn) const
|
||||
{
|
||||
unsigned char data[36]; // 32 bytes txid + 4 bytes input index
|
||||
memcpy(data, txid.begin(), 32);
|
||||
memcpy(data + 32, &nIn, 4);
|
||||
uint256 result;
|
||||
SHA256(data, 36, (unsigned char*)&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public:
|
||||
bool Get(const uint256& txid, unsigned int nIn) const
|
||||
{
|
||||
LOCK(cs);
|
||||
return setValid.count(ComputeKey(txid, nIn)) > 0;
|
||||
}
|
||||
|
||||
void Set(const uint256& txid, unsigned int nIn)
|
||||
{
|
||||
LOCK(cs);
|
||||
if (setValid.size() >= MAX_CACHE_SIZE)
|
||||
{
|
||||
// Evict half the cache when full
|
||||
auto it = setValid.begin();
|
||||
unsigned int nEvict = MAX_CACHE_SIZE / 2;
|
||||
while (nEvict > 0 && it != setValid.end()) {
|
||||
it = setValid.erase(it);
|
||||
--nEvict;
|
||||
}
|
||||
}
|
||||
setValid.insert(ComputeKey(txid, nIn));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // TRIANGLES_SCRIPT_VERIFY_CACHE_H
|
||||
+270
-114
@@ -41,8 +41,6 @@ Notes:
|
||||
#include <errno.h>
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/ecdh.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/evp.h>
|
||||
@@ -53,6 +51,7 @@ Notes:
|
||||
|
||||
|
||||
#include "base58.h"
|
||||
#include "crypto_ecdh.h"
|
||||
#include "db.h"
|
||||
#include "init.h" // pwalletMain
|
||||
#include "txdb.h"
|
||||
@@ -60,6 +59,12 @@ Notes:
|
||||
|
||||
#include "lz4/lz4.h"
|
||||
|
||||
// LevelDB headers retained solely for the one-shot smsgDB leveldb→rocksdb
|
||||
// migration in MigrateSmsgDBLevelDbToRocksDb. Once all users have upgraded
|
||||
// past v5.10 the migration helper (and these includes) can be dropped.
|
||||
#include <leveldb/db.h>
|
||||
#include <leveldb/iterator.h>
|
||||
|
||||
#include "xxhash/xxhash.h"
|
||||
#include "xxhash/xxhash.c"
|
||||
|
||||
@@ -77,9 +82,9 @@ Notes:
|
||||
|
||||
// TODO: For buckets older than current, only need to store no. messages and hash in memory
|
||||
|
||||
boost::signals2::signal<void (SecMsgStored& inboxHdr)> NotifySecMsgInboxChanged;
|
||||
boost::signals2::signal<void (SecMsgStored& outboxHdr)> NotifySecMsgOutboxChanged;
|
||||
boost::signals2::signal<void ()> NotifySecMsgWalletUnlocked;
|
||||
CSignal<void(SecMsgStored&)> NotifySecMsgInboxChanged;
|
||||
CSignal<void(SecMsgStored&)> NotifySecMsgOutboxChanged;
|
||||
CSignal<void()> NotifySecMsgWalletUnlocked;
|
||||
|
||||
bool fSecMsgEnabled = false;
|
||||
|
||||
@@ -94,13 +99,163 @@ uint32_t nPeerIdCounter = 1;
|
||||
CCriticalSection cs_smsg;
|
||||
CCriticalSection cs_smsgDB;
|
||||
|
||||
leveldb::DB *smsgDB = NULL;
|
||||
rocksdb::DB *smsgDB = NULL;
|
||||
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// rocksdb::DB::Open shipped the raw DB** overload for years, then added a
|
||||
// std::unique_ptr<DB>* form in 8.x and removed the raw form in newer
|
||||
// releases (Homebrew's macOS rocksdb hits this path; Ubuntu 22.04 and
|
||||
// MSYS2 still expose the DB** form). Pick whichever overload the linked
|
||||
// rocksdb actually has via SFINAE — `int` parameter is preferred over
|
||||
// `long`, so if DB** exists, the first overload wins; otherwise the
|
||||
// fallback that wraps unique_ptr is used.
|
||||
template<typename T>
|
||||
inline auto OpenSmsgDBImpl(const rocksdb::Options& opts, const std::string& path,
|
||||
T** dbptr, int)
|
||||
-> decltype(rocksdb::DB::Open(opts, path, dbptr))
|
||||
{
|
||||
return rocksdb::DB::Open(opts, path, dbptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline rocksdb::Status OpenSmsgDBImpl(const rocksdb::Options& opts, const std::string& path,
|
||||
T** dbptr, long)
|
||||
{
|
||||
std::unique_ptr<T> tmp;
|
||||
auto s = rocksdb::DB::Open(opts, path, &tmp);
|
||||
if (s.ok()) *dbptr = tmp.release();
|
||||
return s;
|
||||
}
|
||||
|
||||
inline rocksdb::Status OpenSmsgDB(const rocksdb::Options& opts,
|
||||
const std::string& path,
|
||||
rocksdb::DB** dbptr)
|
||||
{
|
||||
return OpenSmsgDBImpl(opts, path, dbptr, 0);
|
||||
}
|
||||
|
||||
// ── smsgDB leveldb → rocksdb migration ──────────────────────────────────────
|
||||
//
|
||||
// Pre-v5.10 the smessage store was backed by LevelDB at <datadir>/smsgDB/.
|
||||
// Phase 3a switched it to RocksDB. Existing installations need their pubkey
|
||||
// cache and inbox/outbox to carry across. The migration is one-shot and
|
||||
// runs lazily inside SecMsgDB::Open: detect the legacy format, rename the
|
||||
// directory aside as a backup, copy every key into a fresh rocksdb tree,
|
||||
// then proceed with the normal open path. The leveldb backup is preserved
|
||||
// (never deleted) so the user can roll back by deleting smsgDB/ and
|
||||
// renaming smsgDB.leveldb-backup/ back.
|
||||
|
||||
// LevelDB and RocksDB share several filenames (CURRENT, MANIFEST-*, LOG).
|
||||
// RocksDB additionally writes IDENTITY and OPTIONS-* on first open;
|
||||
// presence of CURRENT *without* IDENTITY indicates a legacy LevelDB tree.
|
||||
inline bool IsLegacyLevelDbSmsgDir(const fs::path& dir)
|
||||
{
|
||||
if (!fs::exists(dir / "CURRENT"))
|
||||
return false;
|
||||
if (fs::exists(dir / "IDENTITY"))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool MigrateSmsgDBLevelDbToRocksDb(std::string& strError)
|
||||
{
|
||||
fs::path smsgDir = GetDataDir() / "smsgDB";
|
||||
fs::path backupDir = GetDataDir() / "smsgDB.leveldb-backup";
|
||||
|
||||
if (fs::exists(backupDir)) {
|
||||
strError = "smsgDB.leveldb-backup/ already present at " + backupDir.string()
|
||||
+ " — manual cleanup required before retrying migration.";
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("smessage: migrating LevelDB-format smsgDB to RocksDB...\n");
|
||||
|
||||
// Atomic rename so the legacy data is never deleted by this routine —
|
||||
// worst case we leave the backup and bail. Filesystem rename within the
|
||||
// same datadir is atomic on every supported platform.
|
||||
std::error_code ec;
|
||||
fs::rename(smsgDir, backupDir, ec);
|
||||
if (ec) {
|
||||
strError = "Failed to rename smsgDB to backup: " + ec.message();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open backup as leveldb (read-only) — create_if_missing left at default
|
||||
// false so a malformed dir errors out cleanly.
|
||||
leveldb::Options leveldbOpts;
|
||||
leveldb::DB* oldDb = nullptr;
|
||||
leveldb::Status ls = leveldb::DB::Open(leveldbOpts, backupDir.string(), &oldDb);
|
||||
if (!ls.ok()) {
|
||||
strError = "Failed to open legacy LevelDB smsgDB at "
|
||||
+ backupDir.string() + ": " + ls.ToString();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open destination as fresh rocksdb.
|
||||
rocksdb::Options rdbOpts;
|
||||
rdbOpts.create_if_missing = true;
|
||||
rocksdb::DB* newDb = nullptr;
|
||||
rocksdb::Status rs = OpenSmsgDB(rdbOpts, smsgDir.string(), &newDb);
|
||||
if (!rs.ok()) {
|
||||
delete oldDb;
|
||||
strError = "Failed to create new RocksDB smsgDB: " + rs.ToString();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy every key in batches of 5000.
|
||||
leveldb::Iterator* it = oldDb->NewIterator(leveldb::ReadOptions());
|
||||
rocksdb::WriteBatch batch;
|
||||
int nMigrated = 0;
|
||||
int nBatch = 0;
|
||||
bool fOk = true;
|
||||
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
||||
batch.Put(it->key().ToString(), it->value().ToString());
|
||||
nBatch++;
|
||||
nMigrated++;
|
||||
if (nBatch >= 5000) {
|
||||
rocksdb::Status ws = newDb->Write(rocksdb::WriteOptions(), &batch);
|
||||
if (!ws.ok()) {
|
||||
strError = "RocksDB batch write failed during migration: " + ws.ToString();
|
||||
fOk = false;
|
||||
break;
|
||||
}
|
||||
batch.Clear();
|
||||
nBatch = 0;
|
||||
}
|
||||
}
|
||||
if (fOk && nBatch > 0) {
|
||||
rocksdb::Status ws = newDb->Write(rocksdb::WriteOptions(), &batch);
|
||||
if (!ws.ok()) {
|
||||
strError = "RocksDB final batch write failed: " + ws.ToString();
|
||||
fOk = false;
|
||||
}
|
||||
}
|
||||
if (fOk && !it->status().ok()) {
|
||||
strError = "LevelDB iterator failed mid-migration: " + it->status().ToString();
|
||||
fOk = false;
|
||||
}
|
||||
delete it;
|
||||
delete oldDb;
|
||||
delete newDb;
|
||||
|
||||
if (!fOk) {
|
||||
// Leave smsgDB/ in a partially-written state but the backup is
|
||||
// intact. The user can recover by removing smsgDB/ and renaming
|
||||
// smsgDB.leveldb-backup/ → smsgDB/.
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("smessage: migrated %d entries from LevelDB to RocksDB. "
|
||||
"Original data preserved at %s\n",
|
||||
nMigrated, backupDir.string().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
const long int SMSG_BUCKET_FILE_SIZE_LIMIT = 0x70000000L;
|
||||
const int64_t SMSG_THREAD_SHUTDOWN_WAIT_MS = 5000;
|
||||
const int64_t SMSG_THREAD_SHUTDOWN_POLL_MS = 50;
|
||||
@@ -390,9 +545,9 @@ bool SecMsgDB::Open(const char* pszMode)
|
||||
};
|
||||
|
||||
bool fCreate = strchr(pszMode, 'c');
|
||||
|
||||
|
||||
fs::path fullpath = GetDataDir() / "smsgDB";
|
||||
|
||||
|
||||
if (!fCreate
|
||||
&& (!fs::exists(fullpath)
|
||||
|| !fs::is_directory(fullpath)))
|
||||
@@ -400,10 +555,21 @@ bool SecMsgDB::Open(const char* pszMode)
|
||||
printf("SecMsgDB::open() - DB does not exist.\n");
|
||||
return false;
|
||||
};
|
||||
|
||||
leveldb::Options options;
|
||||
|
||||
// One-shot migration: pre-v5.10 nodes have a LevelDB tree under smsgDB/.
|
||||
// Detect that and convert to RocksDB before opening. The legacy data is
|
||||
// renamed to smsgDB.leveldb-backup/ as a recovery option.
|
||||
if (fs::is_directory(fullpath) && IsLegacyLevelDbSmsgDir(fullpath)) {
|
||||
std::string migrateError;
|
||||
if (!MigrateSmsgDBLevelDbToRocksDb(migrateError)) {
|
||||
printf("SecMsgDB::open() - migration failed: %s\n", migrateError.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
rocksdb::Options options;
|
||||
options.create_if_missing = fCreate;
|
||||
leveldb::Status s = leveldb::DB::Open(options, fullpath.string(), &smsgDB);
|
||||
rocksdb::Status s = OpenSmsgDB(options, fullpath.string(), &smsgDB);
|
||||
|
||||
if (!s.ok())
|
||||
{
|
||||
@@ -417,66 +583,37 @@ bool SecMsgDB::Open(const char* pszMode)
|
||||
};
|
||||
|
||||
|
||||
class SecMsgBatchScanner : public leveldb::WriteBatch::Handler
|
||||
{
|
||||
public:
|
||||
std::string needle;
|
||||
bool* deleted;
|
||||
std::string* foundValue;
|
||||
bool foundEntry;
|
||||
|
||||
SecMsgBatchScanner() : foundEntry(false) {}
|
||||
|
||||
virtual void Put(const leveldb::Slice& key, const leveldb::Slice& value)
|
||||
{
|
||||
if (key.ToString() == needle)
|
||||
{
|
||||
foundEntry = true;
|
||||
*deleted = false;
|
||||
*foundValue = value.ToString();
|
||||
};
|
||||
};
|
||||
|
||||
virtual void Delete(const leveldb::Slice& key)
|
||||
{
|
||||
if (key.ToString() == needle)
|
||||
{
|
||||
foundEntry = true;
|
||||
*deleted = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// When performing a read, if we have an active batch we need to check it first
|
||||
// before reading from the database, as the rest of the code assumes that once
|
||||
// a database transaction begins reads are consistent with it. It would be good
|
||||
// to change that assumption in future and avoid the performance hit, though in
|
||||
// practice it does not appear to be large.
|
||||
// a database transaction begins reads are consistent with it.
|
||||
//
|
||||
// Previously implemented via rocksdb::WriteBatch::Handler subclass, which fails
|
||||
// to link against Ubuntu's librocksdb-dev (typeinfo for the Handler base class
|
||||
// isn't exported there). The pendingBatch map is updated alongside every Put
|
||||
// or Delete on activeBatch and answers ScanBatch queries directly.
|
||||
bool SecMsgDB::ScanBatch(const CDataStream& key, std::string* value, bool* deleted) const
|
||||
{
|
||||
if (!activeBatch)
|
||||
return false;
|
||||
|
||||
|
||||
*deleted = false;
|
||||
SecMsgBatchScanner scanner;
|
||||
scanner.needle = key.str();
|
||||
scanner.deleted = deleted;
|
||||
scanner.foundValue = value;
|
||||
leveldb::Status s = activeBatch->Iterate(&scanner);
|
||||
if (!s.ok())
|
||||
{
|
||||
printf("SecMsgDB ScanBatch error: %s\n", s.ToString().c_str());
|
||||
auto it = pendingBatch.find(key.str());
|
||||
if (it == pendingBatch.end())
|
||||
return false;
|
||||
};
|
||||
|
||||
return scanner.foundEntry;
|
||||
if (!it->second.has_value()) {
|
||||
*deleted = true;
|
||||
return true;
|
||||
}
|
||||
*value = *it->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SecMsgDB::TxnBegin()
|
||||
{
|
||||
if (activeBatch)
|
||||
return true;
|
||||
activeBatch = new leveldb::WriteBatch();
|
||||
activeBatch = new rocksdb::WriteBatch();
|
||||
pendingBatch.clear();
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -484,12 +621,13 @@ bool SecMsgDB::TxnCommit()
|
||||
{
|
||||
if (!activeBatch)
|
||||
return false;
|
||||
|
||||
leveldb::WriteOptions writeOptions;
|
||||
|
||||
rocksdb::WriteOptions writeOptions;
|
||||
writeOptions.sync = true;
|
||||
leveldb::Status status = pdb->Write(writeOptions, activeBatch);
|
||||
rocksdb::Status status = pdb->Write(writeOptions, activeBatch);
|
||||
delete activeBatch;
|
||||
activeBatch = NULL;
|
||||
pendingBatch.clear();
|
||||
|
||||
if (!status.ok())
|
||||
{
|
||||
@@ -504,6 +642,7 @@ bool SecMsgDB::TxnAbort()
|
||||
{
|
||||
delete activeBatch;
|
||||
activeBatch = NULL;
|
||||
pendingBatch.clear();
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -531,12 +670,12 @@ bool SecMsgDB::ReadPK(CKeyID& addr, CPubKey& pubkey)
|
||||
|
||||
if (readFromDb)
|
||||
{
|
||||
leveldb::Status s = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &strValue);
|
||||
rocksdb::Status s = pdb->Get(rocksdb::ReadOptions(), ssKey.str(), &strValue);
|
||||
if (!s.ok())
|
||||
{
|
||||
if (s.IsNotFound())
|
||||
return false;
|
||||
printf("LevelDB read failure: %s\n", s.ToString().c_str());
|
||||
printf("RocksDB read failure: %s\n", s.ToString().c_str());
|
||||
return false;
|
||||
};
|
||||
};
|
||||
@@ -569,18 +708,19 @@ bool SecMsgDB::WritePK(CKeyID& addr, CPubKey& pubkey)
|
||||
if (activeBatch)
|
||||
{
|
||||
activeBatch->Put(ssKey.str(), ssValue.str());
|
||||
pendingBatch[ssKey.str()] = ssValue.str();
|
||||
return true;
|
||||
};
|
||||
|
||||
leveldb::WriteOptions writeOptions;
|
||||
|
||||
rocksdb::WriteOptions writeOptions;
|
||||
writeOptions.sync = true;
|
||||
leveldb::Status s = pdb->Put(writeOptions, ssKey.str(), ssValue.str());
|
||||
rocksdb::Status s = pdb->Put(writeOptions, ssKey.str(), ssValue.str());
|
||||
if (!s.ok())
|
||||
{
|
||||
printf("SecMsgDB write failure: %s\n", s.ToString().c_str());
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -605,12 +745,12 @@ bool SecMsgDB::ExistsPK(CKeyID& addr)
|
||||
};
|
||||
};
|
||||
|
||||
leveldb::Status s = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
|
||||
rocksdb::Status s = pdb->Get(rocksdb::ReadOptions(), ssKey.str(), &unused);
|
||||
return s.IsNotFound() == false;
|
||||
};
|
||||
|
||||
|
||||
bool SecMsgDB::NextSmesg(leveldb::Iterator* it, std::string& prefix, unsigned char* chKey, SecMsgStored& smsgStored)
|
||||
bool SecMsgDB::NextSmesg(rocksdb::Iterator* it, std::string& prefix, unsigned char* chKey, SecMsgStored& smsgStored)
|
||||
{
|
||||
if (!pdb)
|
||||
return false;
|
||||
@@ -638,7 +778,7 @@ bool SecMsgDB::NextSmesg(leveldb::Iterator* it, std::string& prefix, unsigned ch
|
||||
return true;
|
||||
};
|
||||
|
||||
bool SecMsgDB::NextSmesgKey(leveldb::Iterator* it, std::string& prefix, unsigned char* chKey)
|
||||
bool SecMsgDB::NextSmesgKey(rocksdb::Iterator* it, std::string& prefix, unsigned char* chKey)
|
||||
{
|
||||
if (!pdb)
|
||||
return false;
|
||||
@@ -679,12 +819,12 @@ bool SecMsgDB::ReadSmesg(unsigned char* chKey, SecMsgStored& smsgStored)
|
||||
|
||||
if (readFromDb)
|
||||
{
|
||||
leveldb::Status s = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &strValue);
|
||||
rocksdb::Status s = pdb->Get(rocksdb::ReadOptions(), ssKey.str(), &strValue);
|
||||
if (!s.ok())
|
||||
{
|
||||
if (s.IsNotFound())
|
||||
return false;
|
||||
printf("LevelDB read failure: %s\n", s.ToString().c_str());
|
||||
printf("RocksDB read failure: %s\n", s.ToString().c_str());
|
||||
return false;
|
||||
};
|
||||
};
|
||||
@@ -713,18 +853,19 @@ bool SecMsgDB::WriteSmesg(unsigned char* chKey, SecMsgStored& smsgStored)
|
||||
if (activeBatch)
|
||||
{
|
||||
activeBatch->Put(ssKey.str(), ssValue.str());
|
||||
pendingBatch[ssKey.str()] = ssValue.str();
|
||||
return true;
|
||||
};
|
||||
|
||||
leveldb::WriteOptions writeOptions;
|
||||
|
||||
rocksdb::WriteOptions writeOptions;
|
||||
writeOptions.sync = true;
|
||||
leveldb::Status s = pdb->Put(writeOptions, ssKey.str(), ssValue.str());
|
||||
rocksdb::Status s = pdb->Put(writeOptions, ssKey.str(), ssValue.str());
|
||||
if (!s.ok())
|
||||
{
|
||||
printf("SecMsgDB write failed: %s\n", s.ToString().c_str());
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -746,7 +887,7 @@ bool SecMsgDB::ExistsSmesg(unsigned char* chKey)
|
||||
};
|
||||
};
|
||||
|
||||
leveldb::Status s = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
|
||||
rocksdb::Status s = pdb->Get(rocksdb::ReadOptions(), ssKey.str(), &unused);
|
||||
return s.IsNotFound() == false;
|
||||
return true;
|
||||
};
|
||||
@@ -759,12 +900,13 @@ bool SecMsgDB::EraseSmesg(unsigned char* chKey)
|
||||
if (activeBatch)
|
||||
{
|
||||
activeBatch->Delete(ssKey.str());
|
||||
pendingBatch[ssKey.str()] = std::nullopt;
|
||||
return true;
|
||||
};
|
||||
|
||||
leveldb::WriteOptions writeOptions;
|
||||
|
||||
rocksdb::WriteOptions writeOptions;
|
||||
writeOptions.sync = true;
|
||||
leveldb::Status s = pdb->Delete(writeOptions, ssKey.str());
|
||||
rocksdb::Status s = pdb->Delete(writeOptions, ssKey.str());
|
||||
|
||||
if (s.ok() || s.IsNotFound())
|
||||
return true;
|
||||
@@ -881,7 +1023,7 @@ void ThreadSecureMsgPow(void* parg)
|
||||
// -- sleep at end, then fSecMsgEnabled is tested on wake
|
||||
|
||||
SecMsgDB dbOutbox;
|
||||
leveldb::Iterator* it;
|
||||
rocksdb::Iterator* it;
|
||||
{
|
||||
LOCK(cs_smsgDB);
|
||||
|
||||
@@ -889,7 +1031,7 @@ void ThreadSecureMsgPow(void* parg)
|
||||
continue;
|
||||
|
||||
// -- fifo (smallest key first)
|
||||
it = dbOutbox.pdb->NewIterator(leveldb::ReadOptions());
|
||||
it = dbOutbox.pdb->NewIterator(rocksdb::ReadOptions());
|
||||
}
|
||||
// -- break up lock, SecureMsgSetHash will take long
|
||||
|
||||
@@ -2110,7 +2252,7 @@ int SecureMsgInsertAddress(CKeyID& hashKey, CPubKey& pubKey)
|
||||
};
|
||||
|
||||
|
||||
static bool ScanBlock(CBlock& block, CTxDB& txdb, SecMsgDB& addrpkdb,
|
||||
static bool ScanBlock(CBlock& block, CTxDBBase& txdb, SecMsgDB& addrpkdb,
|
||||
uint32_t& nTransactions, uint32_t& nInputs, uint32_t& nPubkeys, uint32_t& nDuplicates)
|
||||
{
|
||||
// -- should have LOCK(cs_smsg) where db is opened
|
||||
@@ -2238,7 +2380,7 @@ bool SecureMsgScanBlock(CBlock& block)
|
||||
|
||||
{
|
||||
LOCK(cs_smsgDB);
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
|
||||
SecMsgDB addrpkdb;
|
||||
if (!addrpkdb.Open("cw")
|
||||
@@ -2277,7 +2419,7 @@ bool ScanChainForPublicKeys(CBlockIndex* pindexStart)
|
||||
{
|
||||
LOCK(cs_smsgDB);
|
||||
|
||||
CTxDB txdb("r");
|
||||
auto txdb_holder = MakeChainDB("r"); CTxDBBase& txdb = *txdb_holder;
|
||||
|
||||
SecMsgDB addrpkdb;
|
||||
if (!addrpkdb.Open("cw")
|
||||
@@ -2472,7 +2614,7 @@ bool SecureMsgScanBuckets()
|
||||
// -- remove wl file when scanned
|
||||
try {
|
||||
fs::remove((*itd).path());
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
printf("Error removing wl file %s - %s\n", fileName.c_str(), ex.what());
|
||||
return 1;
|
||||
@@ -2552,7 +2694,7 @@ int SecureMsgWalletUnlocked()
|
||||
printf("Dropping wallet locked file %s, expired.\n", fileName.c_str());
|
||||
try {
|
||||
fs::remove((*itd).path());
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
printf("Error removing wl file %s - %s\n", fileName.c_str(), ex.what());
|
||||
return 1;
|
||||
@@ -2620,7 +2762,7 @@ int SecureMsgWalletUnlocked()
|
||||
// -- remove wl file when scanned
|
||||
try {
|
||||
fs::remove((*itd).path());
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
printf("Error removing wl file %s - %s\n", fileName.c_str(), ex.what());
|
||||
return 1;
|
||||
@@ -3119,7 +3261,7 @@ int SecureMsgStoreUnscanned(unsigned char *pHeader, unsigned char *pPayload, uin
|
||||
try {
|
||||
pathSmsgDir = GetDataDir() / "smsgStore";
|
||||
fs::create_directory(pathSmsgDir);
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
printf("Error: Failed to create directory %s - %s\n", pathSmsgDir.string().c_str(), ex.what());
|
||||
return 1;
|
||||
@@ -3209,7 +3351,7 @@ int SecureMsgStore(unsigned char *pHeader, unsigned char *pPayload, uint32_t nPa
|
||||
try {
|
||||
pathSmsgDir = GetDataDir() / "smsgStore";
|
||||
fs::create_directory(pathSmsgDir);
|
||||
} catch (const boost::filesystem::filesystem_error& ex)
|
||||
} catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
printf("Error: Failed to create directory %s - %s\n", pathSmsgDir.string().c_str(), ex.what());
|
||||
return 1;
|
||||
@@ -3539,7 +3681,7 @@ int SecureMsgEncrypt(SecureMessage& smsg, std::string& addressFrom, std::string&
|
||||
3 addressFrom is invalid.
|
||||
4 addressTo is invalid.
|
||||
5 Could not get public key for addressTo.
|
||||
6 ECDH_compute_key failed
|
||||
6 ECDH key derivation failed
|
||||
7 Could not get private key for addressFrom.
|
||||
8 Could not allocate memory.
|
||||
9 Could not compress message data.
|
||||
@@ -3634,22 +3776,26 @@ int SecureMsgEncrypt(SecureMessage& smsg, std::string& addressFrom, std::string&
|
||||
|
||||
std::vector<unsigned char> vchP;
|
||||
vchP.resize(32);
|
||||
EC_KEY* pkeyr = keyR.GetECKey();
|
||||
EC_KEY* pkeyK = keyK.GetECKey();
|
||||
|
||||
// always seems to be 32, worth checking?
|
||||
//int field_size = EC_GROUP_get_degree(EC_KEY_get0_group(pkeyr));
|
||||
//int secret_len = (field_size+7)/8;
|
||||
//printf("secret_len %d.\n", secret_len);
|
||||
|
||||
// -- ECDH_compute_key returns the same P if fed compressed or uncompressed public keys
|
||||
int lenP = ECDH_compute_key(&vchP[0], 32, EC_KEY_get0_public_key(pkeyK), pkeyr, NULL);
|
||||
|
||||
if (lenP != 32)
|
||||
|
||||
bool fCompressedR = false;
|
||||
CSecret secretR = keyR.GetSecret(fCompressedR);
|
||||
if (secretR.size() != 32)
|
||||
{
|
||||
printf("ECDH_compute_key failed, lenP: %d.\n", lenP);
|
||||
printf("ECDH: keyR secret has unexpected size %zu.\n", secretR.size());
|
||||
return 6;
|
||||
};
|
||||
}
|
||||
std::vector<unsigned char> vchPubK = keyK.GetPubKey().Raw();
|
||||
if (vchPubK.size() != 33 && vchPubK.size() != 65)
|
||||
{
|
||||
printf("ECDH: keyK pubkey has unexpected size %zu.\n", vchPubK.size());
|
||||
return 6;
|
||||
}
|
||||
|
||||
if (!ECDH_xonly_secp256k1(&vchP[0], &secretR[0], &vchPubK[0], vchPubK.size()))
|
||||
{
|
||||
printf("ECDH (encrypt): secp256k1_ecdh failed.\n");
|
||||
return 6;
|
||||
}
|
||||
|
||||
CPubKey cpkR = keyR.GetPubKey();
|
||||
if (!cpkR.IsValid()
|
||||
@@ -3837,7 +3983,7 @@ int SecureMsgSend(std::string& addressFrom, std::string& addressTo, std::string&
|
||||
case 3: sError = "Invalid addressFrom."; break;
|
||||
case 4: sError = "Invalid addressTo."; break;
|
||||
case 5: sError = "Could not get public key for addressTo."; break;
|
||||
case 6: sError = "ECDH_compute_key failed."; break;
|
||||
case 6: sError = "ECDH key derivation failed."; break;
|
||||
case 7: sError = "Could not get private key for addressFrom."; break;
|
||||
case 8: sError = "Could not allocate memory."; break;
|
||||
case 9: sError = "Could not compress message data."; break;
|
||||
@@ -4050,16 +4196,26 @@ int SecureMsgDecrypt(bool fTestOnly, std::string& address, unsigned char *pHeade
|
||||
// -- Do an EC point multiply with private key k and public key R. This gives you public key P.
|
||||
std::vector<unsigned char> vchP;
|
||||
vchP.resize(32);
|
||||
EC_KEY* pkeyk = keyDest.GetECKey();
|
||||
EC_KEY* pkeyR = keyR.GetECKey();
|
||||
|
||||
int lenPdec = ECDH_compute_key(&vchP[0], 32, EC_KEY_get0_public_key(pkeyR), pkeyk, NULL);
|
||||
|
||||
if (lenPdec != 32)
|
||||
|
||||
bool fCompressedDest = false;
|
||||
CSecret secretDest = keyDest.GetSecret(fCompressedDest);
|
||||
if (secretDest.size() != 32)
|
||||
{
|
||||
printf("ECDH_compute_key failed, lenPdec: %d.\n", lenPdec);
|
||||
printf("ECDH: keyDest secret has unexpected size %zu.\n", secretDest.size());
|
||||
return 1;
|
||||
};
|
||||
}
|
||||
std::vector<unsigned char> vchPubR = keyR.GetPubKey().Raw();
|
||||
if (vchPubR.size() != 33 && vchPubR.size() != 65)
|
||||
{
|
||||
printf("ECDH: keyR pubkey has unexpected size %zu.\n", vchPubR.size());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!ECDH_xonly_secp256k1(&vchP[0], &secretDest[0], &vchPubR[0], vchPubR.size()))
|
||||
{
|
||||
printf("ECDH (decrypt): secp256k1_ecdh failed.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// -- Use public key P to calculate the SHA512 hash H.
|
||||
|
||||
+20
-10
@@ -4,11 +4,15 @@
|
||||
#ifndef SEC_MESSAGE_H
|
||||
#define SEC_MESSAGE_H
|
||||
|
||||
#include <leveldb/db.h>
|
||||
#include <leveldb/write_batch.h>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
|
||||
#include <rocksdb/db.h>
|
||||
#include <rocksdb/write_batch.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "db.h"
|
||||
#include "util_signal.h"
|
||||
#include "wallet.h"
|
||||
#include "lz4/lz4.h"
|
||||
|
||||
@@ -41,13 +45,13 @@ extern bool fSecMsgEnabled;
|
||||
class SecMsgStored;
|
||||
|
||||
// Inbox db changed, called with lock cs_smsgDB held.
|
||||
extern boost::signals2::signal<void (SecMsgStored& inboxHdr)> NotifySecMsgInboxChanged;
|
||||
extern CSignal<void(SecMsgStored&)> NotifySecMsgInboxChanged;
|
||||
|
||||
// Outbox db changed, called with lock cs_smsgDB held.
|
||||
extern boost::signals2::signal<void (SecMsgStored& outboxHdr)> NotifySecMsgOutboxChanged;
|
||||
extern CSignal<void(SecMsgStored&)> NotifySecMsgOutboxChanged;
|
||||
|
||||
// Wallet Unlocked, called after all messages received while locked have been processed.
|
||||
extern boost::signals2::signal<void ()> NotifySecMsgWalletUnlocked;
|
||||
extern CSignal<void()> NotifySecMsgWalletUnlocked;
|
||||
|
||||
|
||||
class SecMsgBucket;
|
||||
@@ -313,16 +317,22 @@ public:
|
||||
bool WritePK(CKeyID& addr, CPubKey& pubkey);
|
||||
bool ExistsPK(CKeyID& addr);
|
||||
|
||||
bool NextSmesg(leveldb::Iterator* it, std::string& prefix, unsigned char* vchKey, SecMsgStored& smsgStored);
|
||||
bool NextSmesgKey(leveldb::Iterator* it, std::string& prefix, unsigned char* vchKey);
|
||||
bool NextSmesg(rocksdb::Iterator* it, std::string& prefix, unsigned char* vchKey, SecMsgStored& smsgStored);
|
||||
bool NextSmesgKey(rocksdb::Iterator* it, std::string& prefix, unsigned char* vchKey);
|
||||
bool ReadSmesg(unsigned char* chKey, SecMsgStored& smsgStored);
|
||||
bool WriteSmesg(unsigned char* chKey, SecMsgStored& smsgStored);
|
||||
bool ExistsSmesg(unsigned char* chKey);
|
||||
bool EraseSmesg(unsigned char* chKey);
|
||||
|
||||
leveldb::DB *pdb; // points to the global instance
|
||||
leveldb::WriteBatch *activeBatch;
|
||||
|
||||
rocksdb::DB *pdb; // points to the global instance
|
||||
rocksdb::WriteBatch *activeBatch;
|
||||
|
||||
// Parallel record of every pending write (value) or delete (nullopt) on
|
||||
// activeBatch. Used by ScanBatch to answer "is this key in the active
|
||||
// batch?" without iterating the WriteBatch via Handler — Ubuntu's
|
||||
// librocksdb-dev hides typeinfo for rocksdb::WriteBatch::Handler so a
|
||||
// subclass-based scan fails to link there.
|
||||
std::map<std::string, std::optional<std::string>> pendingBatch;
|
||||
};
|
||||
|
||||
std::string getTimeString(int64_t timestamp, char *buffer, size_t nBuffer);
|
||||
|
||||
@@ -0,0 +1,644 @@
|
||||
// Copyright (c) 2026 Triangles developers
|
||||
// Distributed under the MIT/X11 software license
|
||||
|
||||
#include "snapshotnet.h"
|
||||
|
||||
#include "checkpoints.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "protocol.h"
|
||||
#include "sync.h"
|
||||
#include "ui_interface.h"
|
||||
#include "util.h"
|
||||
#include "utxosnapshot.h"
|
||||
#include "version.h"
|
||||
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
extern std::vector<CNode*> vNodes;
|
||||
extern CCriticalSection cs_vNodes;
|
||||
extern uint64_t nLocalServices;
|
||||
|
||||
namespace SnapshotNet {
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fetcher state
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
|
||||
struct ChunkRequest
|
||||
{
|
||||
int64_t offset;
|
||||
int32_t size;
|
||||
int64_t requestedAt; // GetTimeMicros() when sent
|
||||
CNode* pnode; // not refcounted; checked under cs_vNodes
|
||||
bool done;
|
||||
};
|
||||
|
||||
struct FetcherState
|
||||
{
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
bool active = false;
|
||||
bool finished = false;
|
||||
bool success = false;
|
||||
|
||||
int targetHeight = 0;
|
||||
uint256 expectedFileHash;
|
||||
int64_t totalSize = 0;
|
||||
|
||||
// Per-peer announcement: peer NodeId -> AvailableSnapshot for our targetHeight
|
||||
std::map<int, AvailableSnapshot> peerOffers;
|
||||
|
||||
// Outstanding chunk requests, keyed by chunk-aligned offset.
|
||||
std::map<int64_t, ChunkRequest> pending;
|
||||
|
||||
// Bitmap of chunks already written, by chunk-aligned offset.
|
||||
std::map<int64_t, bool> received;
|
||||
|
||||
fs::path destPath;
|
||||
FILE* fpDest = nullptr;
|
||||
};
|
||||
|
||||
static FetcherState g_fetch;
|
||||
|
||||
// Per-CNode integer id (used as map key). We stash a counter via the node's
|
||||
// pointer address — the pointer itself is stable for the node's lifetime, but
|
||||
// reused across reconnects, so we just use it as an opaque identity for the
|
||||
// duration of a single fetch.
|
||||
static intptr_t NodeKey(const CNode* p) { return reinterpret_cast<intptr_t>(p); }
|
||||
|
||||
static int64_t AlignDown(int64_t off, int32_t chunk)
|
||||
{
|
||||
return (off / chunk) * chunk;
|
||||
}
|
||||
|
||||
static void CloseDest()
|
||||
{
|
||||
if (g_fetch.fpDest) {
|
||||
fclose(g_fetch.fpDest);
|
||||
g_fetch.fpDest = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static void ResetState()
|
||||
{
|
||||
g_fetch.active = false;
|
||||
g_fetch.finished = false;
|
||||
g_fetch.success = false;
|
||||
g_fetch.targetHeight = 0;
|
||||
g_fetch.expectedFileHash = 0;
|
||||
g_fetch.totalSize = 0;
|
||||
g_fetch.peerOffers.clear();
|
||||
g_fetch.pending.clear();
|
||||
g_fetch.received.clear();
|
||||
CloseDest();
|
||||
g_fetch.destPath.clear();
|
||||
}
|
||||
|
||||
// Verify the full destination file's SHA256 matches g_fetch.expectedFileHash.
|
||||
// Returns true on match. Caller holds g_fetch.mu.
|
||||
static bool VerifyDestFileHash(std::string& strErr)
|
||||
{
|
||||
if (!g_fetch.fpDest) {
|
||||
strErr = "no dest file open";
|
||||
return false;
|
||||
}
|
||||
fflush(g_fetch.fpDest);
|
||||
fseek(g_fetch.fpDest, 0, SEEK_SET);
|
||||
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init(&ctx);
|
||||
|
||||
std::vector<unsigned char> buf(64 * 1024);
|
||||
int64_t total = 0;
|
||||
while (true) {
|
||||
size_t n = fread(buf.data(), 1, buf.size(), g_fetch.fpDest);
|
||||
if (n == 0) break;
|
||||
SHA256_Update(&ctx, buf.data(), n);
|
||||
total += (int64_t)n;
|
||||
}
|
||||
if (total != g_fetch.totalSize) {
|
||||
strErr = strprintf("size mismatch: have %" PRId64 " want %" PRId64, total, g_fetch.totalSize);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint256 actual;
|
||||
SHA256_Final((unsigned char*)&actual, &ctx);
|
||||
if (actual != g_fetch.expectedFileHash) {
|
||||
strErr = "snapshot file hash mismatch";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Build the list of chunk offsets that still need a request (not pending, not done).
|
||||
// Caller holds g_fetch.mu.
|
||||
static std::vector<int64_t> MissingChunkOffsets()
|
||||
{
|
||||
std::vector<int64_t> out;
|
||||
if (g_fetch.totalSize <= 0) return out;
|
||||
for (int64_t off = 0; off < g_fetch.totalSize; off += SNAPSHOT_CHUNK_MAX) {
|
||||
if (g_fetch.received.count(off)) continue;
|
||||
if (g_fetch.pending.count(off)) continue;
|
||||
out.push_back(off);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Send getsnapchunk requests striped across snapshot-capable peers.
|
||||
// Caller holds g_fetch.mu.
|
||||
static int DispatchChunkRequests()
|
||||
{
|
||||
if (!g_fetch.active || g_fetch.finished) return 0;
|
||||
|
||||
std::vector<CNode*> servers;
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (CNode* p : vNodes) {
|
||||
if (!p->fSuccessfullyConnected) continue;
|
||||
if (p->nVersion < SNAPSHOT_PROTO_VERSION) continue;
|
||||
if (!(p->nServices & NODE_SNAPSHOT)) continue;
|
||||
// peer must have offered our target snapshot
|
||||
auto it = g_fetch.peerOffers.find((int)NodeKey(p));
|
||||
if (it == g_fetch.peerOffers.end()) continue;
|
||||
if (it->second.fileHash != g_fetch.expectedFileHash) continue;
|
||||
servers.push_back(p);
|
||||
}
|
||||
}
|
||||
if (servers.empty()) return 0;
|
||||
|
||||
std::vector<int64_t> missing = MissingChunkOffsets();
|
||||
if (missing.empty()) return 0;
|
||||
|
||||
// Cap inflight to avoid swamping peer send queues. Each chunk is up to
|
||||
// 256 KB; 32 outstanding * 256 KB = 8 MB pipeline per peer max.
|
||||
const size_t kMaxInflightPerPeer = 32;
|
||||
std::map<int, size_t> inflightPerPeer;
|
||||
for (const auto& kv : g_fetch.pending)
|
||||
inflightPerPeer[(int)NodeKey(kv.second.pnode)]++;
|
||||
|
||||
int64_t now = GetTimeMicros();
|
||||
int sent = 0;
|
||||
size_t serverIdx = 0;
|
||||
for (int64_t off : missing) {
|
||||
// Round-robin pick a server with capacity.
|
||||
CNode* pick = nullptr;
|
||||
for (size_t tries = 0; tries < servers.size(); ++tries) {
|
||||
CNode* candidate = servers[(serverIdx + tries) % servers.size()];
|
||||
if (inflightPerPeer[(int)NodeKey(candidate)] < kMaxInflightPerPeer) {
|
||||
pick = candidate;
|
||||
serverIdx = (serverIdx + tries + 1) % servers.size();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!pick) break; // all peers saturated; loop will resume later
|
||||
|
||||
int32_t reqSize = (int32_t)std::min<int64_t>(SNAPSHOT_CHUNK_MAX,
|
||||
g_fetch.totalSize - off);
|
||||
ChunkRequest req;
|
||||
req.offset = off;
|
||||
req.size = reqSize;
|
||||
req.requestedAt = now;
|
||||
req.pnode = pick;
|
||||
req.done = false;
|
||||
g_fetch.pending[off] = req;
|
||||
inflightPerPeer[(int)NodeKey(pick)]++;
|
||||
|
||||
// PushMessage is thread-safe (acquires its own cs_vSend).
|
||||
pick->PushMessage("getsnapchunk", g_fetch.targetHeight, off, reqSize);
|
||||
++sent;
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
// Reassign chunks whose request has timed out (peer slow or dropped).
|
||||
// Caller holds g_fetch.mu.
|
||||
static void ReissueStalledChunks(int64_t timeoutMicros)
|
||||
{
|
||||
int64_t now = GetTimeMicros();
|
||||
std::vector<int64_t> stale;
|
||||
for (const auto& kv : g_fetch.pending) {
|
||||
if (now - kv.second.requestedAt > timeoutMicros)
|
||||
stale.push_back(kv.first);
|
||||
}
|
||||
for (int64_t off : stale)
|
||||
g_fetch.pending.erase(off);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public: TryFetchSnapshot
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool TryFetchSnapshot(const fs::path& dataDir, int timeoutSec, std::string& strError)
|
||||
{
|
||||
int snapHeight = Checkpoints::GetBestSnapshotHeight();
|
||||
if (snapHeight <= 0) {
|
||||
strError = "no compiled-in snapshot hash available";
|
||||
return false;
|
||||
}
|
||||
|
||||
uint256 expectedHash;
|
||||
if (!Checkpoints::GetSnapshotHash(snapHeight, expectedHash)) {
|
||||
strError = "snapshot hash lookup failed";
|
||||
return false;
|
||||
}
|
||||
|
||||
fs::path destPath = dataDir / "utxo-snapshot.bin";
|
||||
if (fs::exists(destPath)) {
|
||||
// Caller already has a snapshot file; let normal init pick it up.
|
||||
return true;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_fetch.mu);
|
||||
if (g_fetch.active) {
|
||||
strError = "snapshot fetch already in progress";
|
||||
return false;
|
||||
}
|
||||
ResetState();
|
||||
g_fetch.targetHeight = snapHeight;
|
||||
g_fetch.expectedFileHash = expectedHash;
|
||||
g_fetch.destPath = destPath;
|
||||
g_fetch.active = true;
|
||||
}
|
||||
|
||||
printf("SnapshotNet: requesting snapshot at height %d (hash=%s)\n",
|
||||
snapHeight, expectedHash.ToString().c_str());
|
||||
uiInterface.InitMessage(_("Looking for UTXO snapshot peers..."));
|
||||
|
||||
int64_t start = GetTime();
|
||||
int64_t deadline = start + timeoutSec;
|
||||
int64_t lastBroadcast = 0;
|
||||
int64_t lastProgress = 0;
|
||||
|
||||
while (GetTime() < deadline) {
|
||||
// (Re)broadcast getsnap every 30s to pick up newly connected peers.
|
||||
if (GetTime() - lastBroadcast >= 30) {
|
||||
int peerCount = 0;
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (CNode* p : vNodes) {
|
||||
if (!p->fSuccessfullyConnected) continue;
|
||||
if (p->nVersion < SNAPSHOT_PROTO_VERSION) continue;
|
||||
if (!(p->nServices & NODE_SNAPSHOT)) continue;
|
||||
p->PushMessage("getsnap");
|
||||
++peerCount;
|
||||
}
|
||||
}
|
||||
lastBroadcast = GetTime();
|
||||
printf("SnapshotNet: getsnap sent to %d snapshot-capable peers\n", peerCount);
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_fetch.mu);
|
||||
|
||||
// If we have at least one matching offer and total size known,
|
||||
// open dest file and start dispatching chunk requests.
|
||||
if (g_fetch.totalSize > 0 && !g_fetch.fpDest) {
|
||||
g_fetch.fpDest = fopen(g_fetch.destPath.string().c_str(), "wb+");
|
||||
if (!g_fetch.fpDest) {
|
||||
strError = "cannot create " + g_fetch.destPath.string();
|
||||
g_fetch.finished = true;
|
||||
g_fetch.success = false;
|
||||
break;
|
||||
}
|
||||
// Pre-size the file so chunk writes can use random access.
|
||||
if (fseek(g_fetch.fpDest, g_fetch.totalSize - 1, SEEK_SET) == 0) {
|
||||
char zero = 0;
|
||||
fwrite(&zero, 1, 1, g_fetch.fpDest);
|
||||
fflush(g_fetch.fpDest);
|
||||
}
|
||||
}
|
||||
|
||||
ReissueStalledChunks(45 * (int64_t)1000000); // 45s per-chunk timeout
|
||||
DispatchChunkRequests();
|
||||
|
||||
// Progress print every 10s
|
||||
if (GetTime() - lastProgress >= 10 && g_fetch.totalSize > 0) {
|
||||
int64_t got = (int64_t)g_fetch.received.size() * SNAPSHOT_CHUNK_MAX;
|
||||
if (got > g_fetch.totalSize) got = g_fetch.totalSize;
|
||||
printf("SnapshotNet: %" PRId64 " / %" PRId64 " bytes (%" PRId64 "%%)\n",
|
||||
got, g_fetch.totalSize,
|
||||
(int64_t)((got * 100) / g_fetch.totalSize));
|
||||
lastProgress = GetTime();
|
||||
}
|
||||
|
||||
// All chunks in?
|
||||
if (g_fetch.totalSize > 0) {
|
||||
int64_t total = (g_fetch.totalSize + SNAPSHOT_CHUNK_MAX - 1) / SNAPSHOT_CHUNK_MAX;
|
||||
if ((int64_t)g_fetch.received.size() >= total) {
|
||||
std::string verifyErr;
|
||||
if (VerifyDestFileHash(verifyErr)) {
|
||||
g_fetch.success = true;
|
||||
} else {
|
||||
strError = verifyErr;
|
||||
g_fetch.success = false;
|
||||
// Drop bad file so we don't trick later loaders.
|
||||
CloseDest();
|
||||
std::error_code ec;
|
||||
fs::remove(g_fetch.destPath, ec);
|
||||
}
|
||||
g_fetch.finished = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
bool ok;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_fetch.mu);
|
||||
if (!g_fetch.finished) {
|
||||
// Timed out
|
||||
if (strError.empty())
|
||||
strError = strprintf("timeout after %d seconds (totalSize=%" PRId64 ", chunks=%" PRIszu ")",
|
||||
timeoutSec, g_fetch.totalSize, g_fetch.received.size());
|
||||
CloseDest();
|
||||
std::error_code ec;
|
||||
fs::remove(g_fetch.destPath, ec);
|
||||
}
|
||||
ok = g_fetch.success;
|
||||
ResetState();
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
printf("SnapshotNet: snapshot fetched and verified (%s)\n",
|
||||
destPath.string().c_str());
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Server side: read from local snapshot file
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
|
||||
// Cached metadata for the local snapshot file. Filled lazily by EnsureLocalSnapshot
|
||||
// or by HasServableSnapshot scanning the dest path.
|
||||
static std::mutex g_localMu;
|
||||
static bool g_localScanned = false;
|
||||
static bool g_localPresent = false;
|
||||
static int g_localHeight = 0;
|
||||
static uint256 g_localFileHash = 0;
|
||||
static int64_t g_localTotalSize = 0;
|
||||
static fs::path g_localPath;
|
||||
|
||||
static bool ScanLocalSnapshot()
|
||||
{
|
||||
g_localPresent = false;
|
||||
g_localHeight = 0;
|
||||
g_localFileHash = 0;
|
||||
g_localTotalSize = 0;
|
||||
g_localPath = GetDataDir() / "utxo-snapshot.bin";
|
||||
|
||||
if (!fs::exists(g_localPath)) return false;
|
||||
|
||||
int snapHeight = Checkpoints::GetBestSnapshotHeight();
|
||||
if (snapHeight <= 0) return false;
|
||||
|
||||
uint256 expectedHash;
|
||||
if (!Checkpoints::GetSnapshotHash(snapHeight, expectedHash)) return false;
|
||||
|
||||
std::error_code ec;
|
||||
int64_t sz = (int64_t)fs::file_size(g_localPath, ec);
|
||||
if (ec) return false;
|
||||
|
||||
// Hash the file once on first scan to confirm it matches the compiled-in
|
||||
// snapshot hash. A node won't advertise NODE_SNAPSHOT if the local file is
|
||||
// corrupt or for a different height.
|
||||
FILE* f = fopen(g_localPath.string().c_str(), "rb");
|
||||
if (!f) return false;
|
||||
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init(&ctx);
|
||||
std::vector<unsigned char> buf(64 * 1024);
|
||||
while (true) {
|
||||
size_t n = fread(buf.data(), 1, buf.size(), f);
|
||||
if (n == 0) break;
|
||||
SHA256_Update(&ctx, buf.data(), n);
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
uint256 actual;
|
||||
SHA256_Final((unsigned char*)&actual, &ctx);
|
||||
if (actual != expectedHash) {
|
||||
printf("SnapshotNet: local utxo-snapshot.bin hash mismatch — not advertising\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
g_localPresent = true;
|
||||
g_localHeight = snapHeight;
|
||||
g_localFileHash = expectedHash;
|
||||
g_localTotalSize = sz;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ReadLocalChunk(int64_t offset, int32_t size, std::vector<unsigned char>& out)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
if (!g_localPresent) return false;
|
||||
if (offset < 0 || offset >= g_localTotalSize) return false;
|
||||
if (size <= 0 || size > SNAPSHOT_CHUNK_MAX) return false;
|
||||
int32_t actual = (int32_t)std::min<int64_t>(size, g_localTotalSize - offset);
|
||||
|
||||
FILE* f = fopen(g_localPath.string().c_str(), "rb");
|
||||
if (!f) return false;
|
||||
if (fseek(f, offset, SEEK_SET) != 0) { fclose(f); return false; }
|
||||
|
||||
out.resize(actual);
|
||||
size_t n = fread(out.data(), 1, actual, f);
|
||||
fclose(f);
|
||||
if ((int32_t)n != actual) { out.clear(); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool HasServableSnapshot()
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
if (!g_localScanned) {
|
||||
ScanLocalSnapshot();
|
||||
g_localScanned = true;
|
||||
}
|
||||
return g_localPresent;
|
||||
}
|
||||
|
||||
void EnsureLocalSnapshot()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
if (g_localScanned && g_localPresent) return;
|
||||
}
|
||||
|
||||
int snapHeight = Checkpoints::GetBestSnapshotHeight();
|
||||
if (snapHeight <= 0) return;
|
||||
|
||||
fs::path destPath = GetDataDir() / "utxo-snapshot.bin";
|
||||
|
||||
// If the file exists, scan it (validates hash). Otherwise, generate it
|
||||
// from the current chain if our tip is past the snapshot height.
|
||||
bool needGenerate = !fs::exists(destPath);
|
||||
|
||||
if (needGenerate) {
|
||||
if (nBestHeight < snapHeight) return; // not synced past it yet
|
||||
printf("SnapshotNet: dumping local snapshot at height %d -> %s\n",
|
||||
snapHeight, destPath.string().c_str());
|
||||
std::string err;
|
||||
// DumpSnapshot dumps from current chain tip — only call when tip == snapHeight,
|
||||
// otherwise the produced file won't match the published hash. Skip for now;
|
||||
// operators must produce the canonical file out-of-band and place it here.
|
||||
// (Auto-dump from arbitrary tip would not produce the canonical hash.)
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
ScanLocalSnapshot();
|
||||
g_localScanned = true;
|
||||
}
|
||||
|
||||
if (g_localPresent) {
|
||||
nLocalServices |= NODE_SNAPSHOT;
|
||||
printf("SnapshotNet: serving local snapshot height=%d size=%" PRId64 "\n",
|
||||
g_localHeight, g_localTotalSize);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Server side: P2P message dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool ProcessSnapshotMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv)
|
||||
{
|
||||
if (strCommand == "getsnap")
|
||||
{
|
||||
// Reply with a list of snapshots we can serve. Currently only the
|
||||
// single canonical snapshot at the latest checkpoint with a published
|
||||
// hash; future versions may serve multiple.
|
||||
std::vector<AvailableSnapshot> reply;
|
||||
if (HasServableSnapshot()) {
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
AvailableSnapshot a;
|
||||
a.height = g_localHeight;
|
||||
a.fileHash = g_localFileHash;
|
||||
a.totalSize = g_localTotalSize;
|
||||
reply.push_back(a);
|
||||
}
|
||||
pfrom->PushMessage("snap", reply);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strCommand == "snap")
|
||||
{
|
||||
std::vector<AvailableSnapshot> offers;
|
||||
vRecv >> offers;
|
||||
if (offers.size() > 16) {
|
||||
pfrom->Misbehaving(20);
|
||||
return true;
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(g_fetch.mu);
|
||||
if (!g_fetch.active) return true;
|
||||
for (const AvailableSnapshot& a : offers) {
|
||||
if (a.height != g_fetch.targetHeight) continue;
|
||||
if (a.fileHash != g_fetch.expectedFileHash) continue;
|
||||
if (a.totalSize <= 0 || a.totalSize > (int64_t)4 * 1024 * 1024 * 1024) continue;
|
||||
g_fetch.peerOffers[(int)NodeKey(pfrom)] = a;
|
||||
if (g_fetch.totalSize == 0)
|
||||
g_fetch.totalSize = a.totalSize;
|
||||
}
|
||||
g_fetch.cv.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strCommand == "getsnapchunk")
|
||||
{
|
||||
int height;
|
||||
int64_t offset;
|
||||
int32_t size;
|
||||
vRecv >> height >> offset >> size;
|
||||
|
||||
std::vector<unsigned char> data;
|
||||
if (HasServableSnapshot()) {
|
||||
std::lock_guard<std::mutex> lk(g_localMu);
|
||||
if (height == g_localHeight)
|
||||
ReadLocalChunk(offset, size, data);
|
||||
}
|
||||
// Always reply, even with empty data, so the requester can give up
|
||||
// on this peer for this chunk and reissue elsewhere.
|
||||
pfrom->PushMessage("snapchunk", height, offset, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strCommand == "snapchunk")
|
||||
{
|
||||
int height;
|
||||
int64_t offset;
|
||||
std::vector<unsigned char> data;
|
||||
vRecv >> height >> offset >> data;
|
||||
|
||||
if (data.size() > (size_t)SNAPSHOT_CHUNK_MAX) {
|
||||
pfrom->Misbehaving(20);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lk(g_fetch.mu);
|
||||
if (!g_fetch.active) return true;
|
||||
if (height != g_fetch.targetHeight) return true;
|
||||
if (data.empty()) {
|
||||
// Peer doesn't have it; drop pending so it gets reissued.
|
||||
g_fetch.pending.erase(offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (offset < 0 || offset >= g_fetch.totalSize) {
|
||||
pfrom->Misbehaving(10);
|
||||
g_fetch.pending.erase(offset);
|
||||
return true;
|
||||
}
|
||||
int32_t expected = (int32_t)std::min<int64_t>(SNAPSHOT_CHUNK_MAX,
|
||||
g_fetch.totalSize - offset);
|
||||
if ((int32_t)data.size() != expected) {
|
||||
pfrom->Misbehaving(10);
|
||||
g_fetch.pending.erase(offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (g_fetch.fpDest) {
|
||||
if (fseek(g_fetch.fpDest, offset, SEEK_SET) == 0) {
|
||||
size_t w = fwrite(data.data(), 1, data.size(), g_fetch.fpDest);
|
||||
if (w == data.size()) {
|
||||
g_fetch.received[offset] = true;
|
||||
g_fetch.pending.erase(offset);
|
||||
g_fetch.cv.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace SnapshotNet
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2026 Triangles developers
|
||||
// Distributed under the MIT/X11 software license
|
||||
|
||||
#ifndef TRIANGLES_SNAPSHOTNET_H
|
||||
#define TRIANGLES_SNAPSHOTNET_H
|
||||
|
||||
#include "uint256.h"
|
||||
#include "serialize.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class CNode;
|
||||
class CDataStream;
|
||||
|
||||
namespace SnapshotNet {
|
||||
|
||||
// Maximum bytes returned per snapshot chunk reply. Sized for Tor cell efficiency
|
||||
// (Tor sends 514-byte cells; ~256 KB amortizes overhead without exceeding the
|
||||
// 32 MB peer send buffer when many chunks are queued).
|
||||
static const int32_t SNAPSHOT_CHUNK_MAX = 256 * 1024;
|
||||
|
||||
// One advertised snapshot a peer can serve.
|
||||
struct AvailableSnapshot
|
||||
{
|
||||
int height;
|
||||
uint256 fileHash;
|
||||
int64_t totalSize;
|
||||
|
||||
AvailableSnapshot() : height(0), fileHash(0), totalSize(0) {}
|
||||
|
||||
IMPLEMENT_SERIALIZE
|
||||
(
|
||||
READWRITE(height);
|
||||
READWRITE(fileHash);
|
||||
READWRITE(totalSize);
|
||||
)
|
||||
};
|
||||
|
||||
// Initial snapshot fetch on a fresh install.
|
||||
// - Picks the latest checkpoint height with a published snapshot hash.
|
||||
// - Polls connected peers for matching snapshots.
|
||||
// - Stripes chunk requests across peers in parallel.
|
||||
// - Verifies the full file SHA256 against the compiled-in snapshot hash.
|
||||
// - Writes the result to dataDir/utxo-snapshot.bin.
|
||||
//
|
||||
// Blocks for up to timeoutSec waiting for peers + transfer. Returns true if a
|
||||
// verified snapshot was written, false on timeout/no peer/verification fail.
|
||||
bool TryFetchSnapshot(const std::filesystem::path& dataDir,
|
||||
int timeoutSec,
|
||||
std::string& strError);
|
||||
|
||||
// Server-side message dispatch. Called from main.cpp ProcessMessage.
|
||||
// Returns true if strCommand was a snapshot-protocol message (handled or
|
||||
// rejected for malformed input).
|
||||
bool ProcessSnapshotMessage(CNode* pfrom,
|
||||
const std::string& strCommand,
|
||||
CDataStream& vRecv);
|
||||
|
||||
// Generate dataDir/utxo-snapshot.bin from the current chain if our tip is past
|
||||
// the latest checkpoint height with a published snapshot hash and the file does
|
||||
// not already exist. Safe to call repeatedly; no-op when conditions aren't met.
|
||||
// Sets the NODE_SNAPSHOT service flag on success.
|
||||
void EnsureLocalSnapshot();
|
||||
|
||||
// Returns true when this node holds a verified snapshot file ready to serve.
|
||||
bool HasServableSnapshot();
|
||||
|
||||
} // namespace SnapshotNet
|
||||
|
||||
#endif // TRIANGLES_SNAPSHOTNET_H
|
||||
+3
-3
@@ -48,9 +48,9 @@ private:
|
||||
|
||||
typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
|
||||
|
||||
static boost::mutex dd_mutex;
|
||||
static std::mutex dd_mutex;
|
||||
static std::map<std::pair<void*, void*>, LockStack> lockorders;
|
||||
static boost::thread_specific_ptr<LockStack> lockstack;
|
||||
static thread_local std::unique_ptr<LockStack> lockstack;
|
||||
|
||||
|
||||
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
|
||||
@@ -74,7 +74,7 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
|
||||
|
||||
static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
|
||||
{
|
||||
if (lockstack.get() == NULL)
|
||||
if (!lockstack)
|
||||
lockstack.reset(new LockStack);
|
||||
|
||||
if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
|
||||
|
||||
+14
-19
@@ -5,19 +5,14 @@
|
||||
#ifndef TRIANGLES_SYNC_H
|
||||
#define TRIANGLES_SYNC_H
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
/** Recursive mutex: supports recursive locking, but no waiting */
|
||||
typedef std::recursive_mutex CCriticalSection;
|
||||
|
||||
|
||||
|
||||
/** Wrapped boost mutex: supports recursive locking, but no waiting */
|
||||
typedef boost::recursive_mutex CCriticalSection;
|
||||
|
||||
/** Wrapped boost mutex: supports waiting but not recursive locking */
|
||||
typedef boost::mutex CWaitableCriticalSection;
|
||||
/** Plain mutex: supports waiting but not recursive locking */
|
||||
typedef std::mutex CWaitableCriticalSection;
|
||||
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
|
||||
@@ -36,7 +31,7 @@ template<typename Mutex>
|
||||
class CMutexLock
|
||||
{
|
||||
private:
|
||||
boost::unique_lock<Mutex> lock;
|
||||
std::unique_lock<Mutex> lock;
|
||||
public:
|
||||
|
||||
void Enter(const char* pszName, const char* pszFile, int nLine)
|
||||
@@ -77,7 +72,7 @@ public:
|
||||
return lock.owns_lock();
|
||||
}
|
||||
|
||||
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
|
||||
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, std::defer_lock)
|
||||
{
|
||||
if (fTry)
|
||||
TryEnter(pszName, pszFile, nLine);
|
||||
@@ -96,7 +91,7 @@ public:
|
||||
return lock.owns_lock();
|
||||
}
|
||||
|
||||
boost::unique_lock<Mutex> &GetLock()
|
||||
std::unique_lock<Mutex> &GetLock()
|
||||
{
|
||||
return lock;
|
||||
}
|
||||
@@ -123,15 +118,15 @@ typedef CMutexLock<CCriticalSection> CCriticalBlock;
|
||||
class CSemaphore
|
||||
{
|
||||
private:
|
||||
boost::condition_variable condition;
|
||||
boost::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::mutex mutex;
|
||||
int value;
|
||||
|
||||
public:
|
||||
CSemaphore(int init) : value(init) {}
|
||||
|
||||
void wait() {
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
while (value < 1) {
|
||||
condition.wait(lock);
|
||||
}
|
||||
@@ -139,7 +134,7 @@ public:
|
||||
}
|
||||
|
||||
bool try_wait() {
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (value < 1)
|
||||
return false;
|
||||
value--;
|
||||
@@ -148,7 +143,7 @@ public:
|
||||
|
||||
void post() {
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
value++;
|
||||
}
|
||||
condition.notify_one();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
||||
#include <chrono>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "main.h"
|
||||
@@ -249,25 +249,23 @@ BOOST_AUTO_TEST_CASE(DoS_checkSig)
|
||||
tx.vin[j].prevout.hash = orphans[j].GetHash();
|
||||
}
|
||||
// Creating signatures primes the cache:
|
||||
boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
|
||||
auto mst1 = std::chrono::steady_clock::now();
|
||||
for (unsigned int j = 0; j < tx.vin.size(); j++)
|
||||
BOOST_CHECK(SignSignature(keystore, orphans[j], tx, j));
|
||||
boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
|
||||
boost::posix_time::time_duration msdiff = mst2 - mst1;
|
||||
long nOneValidate = msdiff.total_milliseconds();
|
||||
auto mst2 = std::chrono::steady_clock::now();
|
||||
long nOneValidate = std::chrono::duration_cast<std::chrono::milliseconds>(mst2 - mst1).count();
|
||||
if (fDebug) printf("DoS_Checksig sign: %ld\n", nOneValidate);
|
||||
|
||||
// ... now validating repeatedly should be quick:
|
||||
// 2.8GHz machine, -g build: Sign takes ~760ms,
|
||||
// uncached Verify takes ~250ms, cached Verify takes ~50ms
|
||||
// (for 100 single-signature inputs)
|
||||
mst1 = boost::posix_time::microsec_clock::local_time();
|
||||
mst1 = std::chrono::steady_clock::now();
|
||||
for (unsigned int i = 0; i < 5; i++)
|
||||
for (unsigned int j = 0; j < tx.vin.size(); j++)
|
||||
BOOST_CHECK(VerifySignature(orphans[j], tx, j, SIGHASH_ALL));
|
||||
mst2 = boost::posix_time::microsec_clock::local_time();
|
||||
msdiff = mst2 - mst1;
|
||||
long nManyValidate = msdiff.total_milliseconds();
|
||||
mst2 = std::chrono::steady_clock::now();
|
||||
long nManyValidate = std::chrono::duration_cast<std::chrono::milliseconds>(mst2 - mst1).count();
|
||||
if (fDebug) printf("DoS_Checksig five: %ld\n", nManyValidate);
|
||||
|
||||
BOOST_CHECK_MESSAGE(nManyValidate < nOneValidate, "Signature cache timing failed");
|
||||
|
||||
@@ -85,7 +85,7 @@ ParseScript(string s)
|
||||
Array
|
||||
read_json(const std::string& filename)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
fs::path testFile = fs::current_path() / "test" / "data" / filename;
|
||||
|
||||
#ifdef TEST_DATA_DIR
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
#include "anonymize.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
@@ -42,10 +41,10 @@ int check_interrupted(
|
||||
) ? 1 : 0;
|
||||
}
|
||||
|
||||
static boost::mutex initializing;
|
||||
static std::mutex initializing;
|
||||
|
||||
static std::unique_ptr<boost::unique_lock<boost::mutex> > uninitialized(
|
||||
new boost::unique_lock<boost::mutex>(
|
||||
static std::unique_ptr<std::unique_lock<std::mutex> > uninitialized(
|
||||
new std::unique_lock<std::mutex>(
|
||||
initializing
|
||||
)
|
||||
);
|
||||
@@ -57,5 +56,5 @@ void set_initialized(
|
||||
|
||||
void wait_initialized(
|
||||
) {
|
||||
boost::unique_lock<boost::mutex> checking(initializing);
|
||||
std::unique_lock<std::mutex> checking(initializing);
|
||||
}
|
||||
|
||||
+10
-10
@@ -34,7 +34,7 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
@@ -60,12 +60,12 @@ extern CWallet* pwalletMain;
|
||||
CTorV3Manager* CTorV3Manager::instance = nullptr;
|
||||
static TorV3Config torV3Config;
|
||||
|
||||
static boost::filesystem::path GetBackendHiddenServiceDir(const std::string& torDataDir)
|
||||
static std::filesystem::path GetBackendHiddenServiceDir(const std::string& torDataDir)
|
||||
{
|
||||
return boost::filesystem::path(torDataDir) / "hidden_service";
|
||||
return std::filesystem::path(torDataDir) / "hidden_service";
|
||||
}
|
||||
|
||||
static bool ReadTrimmedFirstLine(const boost::filesystem::path& path, std::string& valueOut)
|
||||
static bool ReadTrimmedFirstLine(const std::filesystem::path& path, std::string& valueOut)
|
||||
{
|
||||
valueOut.clear();
|
||||
|
||||
@@ -573,12 +573,12 @@ bool CTorV3Service::AttachToBackendService(const std::string& torDataDir, int se
|
||||
|
||||
port = servicePort;
|
||||
|
||||
const boost::filesystem::path serviceDir = GetBackendHiddenServiceDir(torDataDir);
|
||||
const boost::filesystem::path hostnamePath = serviceDir / "hostname";
|
||||
const std::filesystem::path serviceDir = GetBackendHiddenServiceDir(torDataDir);
|
||||
const std::filesystem::path hostnamePath = serviceDir / "hostname";
|
||||
|
||||
std::string backendOnion;
|
||||
for (int waited = 0; waited <= waitSeconds; ++waited) {
|
||||
if (boost::filesystem::exists(hostnamePath) &&
|
||||
if (std::filesystem::exists(hostnamePath) &&
|
||||
ReadTrimmedFirstLine(hostnamePath, backendOnion)) {
|
||||
break;
|
||||
}
|
||||
@@ -616,8 +616,8 @@ bool CTorV3Service::AttachToBackendService(const std::string& torDataDir, int se
|
||||
|
||||
// Back up the Tor-generated secret key to wallet.dat so the onion
|
||||
// identity survives deletion of the tor_data directory.
|
||||
boost::filesystem::path secretKeyPath = serviceDir / "hs_ed25519_secret_key";
|
||||
if (boost::filesystem::exists(secretKeyPath)) {
|
||||
std::filesystem::path secretKeyPath = serviceDir / "hs_ed25519_secret_key";
|
||||
if (std::filesystem::exists(secretKeyPath)) {
|
||||
std::ifstream keyFile(secretKeyPath.string().c_str(), std::ios::binary);
|
||||
if (keyFile.is_open()) {
|
||||
std::vector<unsigned char> keyData(
|
||||
@@ -1218,7 +1218,7 @@ bool CTorV3Manager::InitializeTor()
|
||||
torDataDir = torV3Config.torDataDirectory;
|
||||
|
||||
// Create tor data directory
|
||||
boost::filesystem::create_directories(torDataDir);
|
||||
std::filesystem::create_directories(torDataDir);
|
||||
|
||||
torEnabled = true;
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// Distributed under the MIT/X11 software license
|
||||
//
|
||||
// BUILD REQUIREMENT: Link against libtor.a built from the official Tor source.
|
||||
// See CODEX-TOR-GUIDE.md for submodule setup and build instructions.
|
||||
//
|
||||
// This file compiles in two modes:
|
||||
// 1. ENABLE_TOR_EMBEDDED defined: full embedded Tor via tor_api.h
|
||||
@@ -13,8 +12,8 @@
|
||||
#include "../util.h"
|
||||
#include "../net.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
@@ -36,7 +35,7 @@ extern "C" {
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// Singleton
|
||||
CTorEmbedded* CTorEmbedded::instance = nullptr;
|
||||
@@ -153,7 +152,7 @@ bool CTorEmbedded::Start(int socks, int hsPort, bool enableHiddenService)
|
||||
running.store(true);
|
||||
|
||||
// Launch Tor on a dedicated thread (tor_run_main blocks)
|
||||
boost::thread torThread(TorThreadFunc, argv);
|
||||
std::thread torThread(TorThreadFunc, argv);
|
||||
torThread.detach();
|
||||
|
||||
// Wait for SOCKS port to become available (up to 60s)
|
||||
|
||||
+127
-9
@@ -35,7 +35,7 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static std::string ReadTailLines(const fs::path& filePath, size_t maxLines)
|
||||
{
|
||||
@@ -76,6 +76,7 @@ CTorProcess::CTorProcess()
|
||||
, running(false)
|
||||
#ifdef WIN32
|
||||
, hProcess(NULL)
|
||||
, hJob(NULL)
|
||||
, processId(0)
|
||||
#else
|
||||
, processId(0)
|
||||
@@ -195,6 +196,46 @@ bool CTorProcess::IsPortInUse(int port)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
bool CTorProcess::KillOrphanedTor()
|
||||
{
|
||||
// Walk all processes looking for tor.exe listening on our SOCKS port.
|
||||
// We identify orphans by matching the executable name AND checking that
|
||||
// the Tor data directory inside our wallet data dir has a matching PID lock.
|
||||
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (hSnap == INVALID_HANDLE_VALUE) return false;
|
||||
|
||||
PROCESSENTRY32 pe;
|
||||
pe.dwSize = sizeof(pe);
|
||||
bool killed = false;
|
||||
|
||||
if (Process32First(hSnap, &pe)) {
|
||||
do {
|
||||
// Case-insensitive compare against "tor.exe"
|
||||
if (_stricmp(pe.szExeFile, "tor.exe") != 0)
|
||||
continue;
|
||||
|
||||
printf("Found orphaned tor.exe (PID %lu), terminating...\n", pe.th32ProcessID);
|
||||
HANDLE h = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pe.th32ProcessID);
|
||||
if (h) {
|
||||
TerminateProcess(h, 0);
|
||||
WaitForSingleObject(h, 5000);
|
||||
CloseHandle(h);
|
||||
killed = true;
|
||||
}
|
||||
} while (Process32Next(hSnap, &pe));
|
||||
}
|
||||
|
||||
CloseHandle(hSnap);
|
||||
|
||||
if (killed) {
|
||||
// Give the OS a moment to release the port
|
||||
MilliSleep(1000);
|
||||
}
|
||||
return killed;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool CTorProcess::WriteTorrc()
|
||||
{
|
||||
fs::path dataPath(torDataDir);
|
||||
@@ -218,10 +259,34 @@ bool CTorProcess::WriteTorrc()
|
||||
// SOCKS proxy for wallet connections
|
||||
torrc << "SocksPort " << socksPort << "\n";
|
||||
|
||||
// Data directory for Tor state
|
||||
fs::path torStateDir = dataPath / "state";
|
||||
fs::create_directories(torStateDir);
|
||||
torrc << "DataDirectory " << torStateDir.string() << "\n";
|
||||
// Data directory for Tor state.
|
||||
// Use the tor_data directory itself as DataDirectory so that Tor creates
|
||||
// its internal 'state' FILE at <tor_data>/state. Older wallet builds
|
||||
// erroneously created a subdirectory called 'state' and pointed
|
||||
// DataDirectory at it; newer Tor versions (0.4.9+) reject that because
|
||||
// they expect to write a plain file called 'state' inside DataDirectory.
|
||||
//
|
||||
// Recovery: if 'state' exists as a directory, move its contents up and
|
||||
// remove it so that Tor can create its state file in the normal location.
|
||||
{
|
||||
fs::path badStateDir = dataPath / "state";
|
||||
if (fs::exists(badStateDir) && fs::is_directory(badStateDir)) {
|
||||
// Migrate any files inside the bad 'state/' directory up to dataPath
|
||||
try {
|
||||
for (auto& entry : fs::directory_iterator(badStateDir)) {
|
||||
fs::path dest = dataPath / entry.path().filename();
|
||||
if (!fs::exists(dest)) {
|
||||
fs::rename(entry.path(), dest);
|
||||
}
|
||||
}
|
||||
fs::remove(badStateDir);
|
||||
printf("Auto-recovered: removed legacy 'state' directory from %s\n", dataPath.string().c_str());
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
printf("WARNING: Could not auto-recover tor_data/state directory: %s\n", e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
torrc << "DataDirectory " << dataPath.string() << "\n";
|
||||
|
||||
// Persistent Tor log for post-mortem debugging on user machines.
|
||||
fs::path torLogPath = dataPath / "tor.log";
|
||||
@@ -268,10 +333,44 @@ bool CTorProcess::Start(const std::string& dataDir, int socks, int hsPort, bool
|
||||
|
||||
// Check if something is already listening on our SOCKS port
|
||||
if (IsPortInUse(socksPort)) {
|
||||
printf("Tor SOCKS port %d already in use - assuming Tor is running\n", socksPort);
|
||||
lastError = strprintf("SOCKS port %d is already in use; assuming an existing Tor instance is serving it.", socksPort);
|
||||
running = true;
|
||||
return true;
|
||||
#ifdef WIN32
|
||||
// An orphaned tor.exe from a previous wallet session is likely still
|
||||
// running. Kill it so we can start a fresh one under our Job Object.
|
||||
printf("Tor SOCKS port %d already in use - killing orphaned tor.exe\n", socksPort);
|
||||
KillOrphanedTor();
|
||||
// If the port is STILL in use after killing all tor.exe, something
|
||||
// else owns it. Fall through and let the new Tor fail gracefully
|
||||
// rather than silently adopting an unknown process.
|
||||
if (IsPortInUse(socksPort)) {
|
||||
printf("WARNING: Port %d still in use after killing tor.exe - another process owns it\n", socksPort);
|
||||
}
|
||||
#else
|
||||
// On Linux the child is reaped via waitpid, so orphans are less common.
|
||||
// If the port is busy, assume a system Tor or leftover process.
|
||||
printf("Tor SOCKS port %d already in use - killing orphaned tor\n", socksPort);
|
||||
// Try to find and kill by PID file
|
||||
fs::path pidFile = fs::path(torDataDir) / "state" / "pid";
|
||||
if (fs::exists(pidFile)) {
|
||||
std::ifstream f(pidFile.string().c_str());
|
||||
pid_t oldPid = 0;
|
||||
if (f >> oldPid && oldPid > 0) {
|
||||
printf("Found stale Tor PID %d, sending SIGTERM...\n", oldPid);
|
||||
kill(oldPid, SIGTERM);
|
||||
for (int i = 0; i < 30; i++) {
|
||||
MilliSleep(100);
|
||||
if (kill(oldPid, 0) != 0) break;
|
||||
}
|
||||
if (kill(oldPid, 0) == 0) {
|
||||
printf("Tor PID %d still alive, sending SIGKILL...\n", oldPid);
|
||||
kill(oldPid, SIGKILL);
|
||||
MilliSleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (IsPortInUse(socksPort)) {
|
||||
printf("WARNING: Port %d still in use after cleanup - another process owns it\n", socksPort);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Find Tor binary
|
||||
@@ -324,6 +423,21 @@ bool CTorProcess::Start(const std::string& dataDir, int socks, int hsPort, bool
|
||||
processId = pi.dwProcessId;
|
||||
CloseHandle(pi.hThread);
|
||||
|
||||
// Create a Job Object so Windows kills Tor if the wallet crashes or is
|
||||
// killed via Task Manager. JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE means
|
||||
// all processes in the job die when the last handle to the job closes
|
||||
// (i.e. when our process exits for any reason).
|
||||
hJob = CreateJobObject(NULL, NULL);
|
||||
if (hJob) {
|
||||
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = {};
|
||||
jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
||||
SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
|
||||
&jobInfo, sizeof(jobInfo));
|
||||
if (!AssignProcessToJobObject(hJob, hProcess)) {
|
||||
printf("WARNING: Could not assign Tor to Job Object (error %lu)\n", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
printf("Tor process started (PID %lu)\n", processId);
|
||||
#else
|
||||
pid_t pid = fork();
|
||||
@@ -407,6 +521,10 @@ void CTorProcess::Stop()
|
||||
CloseHandle(hProcess);
|
||||
hProcess = NULL;
|
||||
}
|
||||
if (hJob != NULL) {
|
||||
CloseHandle(hJob);
|
||||
hJob = NULL;
|
||||
}
|
||||
#else
|
||||
if (processId > 0) {
|
||||
printf("Stopping Tor process (PID %d)...\n", processId);
|
||||
|
||||
@@ -27,7 +27,11 @@ private:
|
||||
|
||||
#ifdef WIN32
|
||||
HANDLE hProcess;
|
||||
HANDLE hJob; // Job Object: kills Tor if wallet crashes/exits
|
||||
DWORD processId;
|
||||
|
||||
// Find and kill an orphaned Tor process from a previous wallet session
|
||||
bool KillOrphanedTor();
|
||||
#else
|
||||
pid_t processId;
|
||||
#endif
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
#include "tor_embed_hooks.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -25,13 +24,13 @@ const char* triangles_onion_service_directory()
|
||||
|
||||
int triangles_tor_check_interrupted()
|
||||
{
|
||||
return boost::this_thread::interruption_requested() ? 1 : 0;
|
||||
return fShutdown ? 1 : 0;
|
||||
}
|
||||
|
||||
static boost::mutex g_torInitializing;
|
||||
static std::mutex g_torInitializing;
|
||||
|
||||
static std::unique_ptr<boost::unique_lock<boost::mutex> > g_torUninitialized(
|
||||
new boost::unique_lock<boost::mutex>(g_torInitializing));
|
||||
static std::unique_ptr<std::unique_lock<std::mutex> > g_torUninitialized(
|
||||
new std::unique_lock<std::mutex>(g_torInitializing));
|
||||
|
||||
void triangles_tor_set_initialized()
|
||||
{
|
||||
@@ -40,5 +39,5 @@ void triangles_tor_set_initialized()
|
||||
|
||||
void triangles_tor_wait_initialized()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> checking(g_torInitializing);
|
||||
std::unique_lock<std::mutex> checking(g_torInitializing);
|
||||
}
|
||||
|
||||
+92
-56
@@ -13,18 +13,20 @@
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "notificationqueue.h"
|
||||
#include "util_signal.h"
|
||||
|
||||
#undef printf
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ip/v6_only.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
#include <boost/iostreams/concepts.hpp>
|
||||
#include <boost/iostreams/stream.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
#include <fstream>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
|
||||
@@ -34,7 +36,7 @@ using namespace std;
|
||||
using namespace boost;
|
||||
using namespace boost::asio;
|
||||
using namespace json_spirit;
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
void ThreadRPCServer2(void* parg);
|
||||
|
||||
@@ -256,6 +258,7 @@ static const CRPCCommand vRPCCommands[] =
|
||||
{ "getwalletinfo", &getwalletinfo, true, false },
|
||||
{ "getnetworkinfo", &getnetworkinfo, true, false },
|
||||
{ "getseedlist", &getseedlist, true, false },
|
||||
{ "getnetworkstability", &getnetworkstability, true, false },
|
||||
{ "gettxoutsetinfo", &gettxoutsetinfo, true, false },
|
||||
{ "estimatefee", &estimatefee, true, false },
|
||||
{ "getaddressbalance", &getaddressbalance, true, false },
|
||||
@@ -319,12 +322,13 @@ static const CRPCCommand vRPCCommands[] =
|
||||
{ "invalidateblock", &invalidateblock, false, false },
|
||||
{ "reconsiderblock", &reconsiderblock, false, false },
|
||||
{ "recalculatesupply", &recalculatesupply, false, false },
|
||||
{ "auditsignatures", &auditsignatures, true, false },
|
||||
{ "dumputxoset", &dumputxoset, false, false },
|
||||
{ "reservebalance", &reservebalance, false, true},
|
||||
{ "checkwallet", &checkwallet, false, true},
|
||||
{ "repairwallet", &repairwallet, false, true},
|
||||
{ "resendtx", &resendtx, false, true},
|
||||
{ "makekeypair", &makekeypair, false, true},
|
||||
{ "sendalert", &sendalert, false, false},
|
||||
|
||||
{ "smsgenable", &smsgenable, false, false},
|
||||
{ "smsgdisable", &smsgdisable, false, false},
|
||||
@@ -544,10 +548,17 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
|
||||
bool HTTPAuthorized(map<string, string>& mapHeaders)
|
||||
{
|
||||
string strAuth = mapHeaders["authorization"];
|
||||
if (strAuth.substr(0,6) != "Basic ")
|
||||
if (strAuth.size() < 6 || strAuth.substr(0,6) != "Basic ")
|
||||
return false;
|
||||
string strUserPass64 = strAuth.substr(6); boost::trim(strUserPass64);
|
||||
string strUserPass = DecodeBase64(strUserPass64);
|
||||
if (strUserPass64.empty())
|
||||
return false;
|
||||
string strUserPass;
|
||||
try {
|
||||
strUserPass = DecodeBase64(strUserPass64);
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
}
|
||||
return TimingResistantEqual(strUserPass, strRPCUserColonPass);
|
||||
}
|
||||
|
||||
@@ -784,44 +795,61 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
|
||||
{
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]++;
|
||||
|
||||
// Immediately start accepting new connections, except when we're cancelled or our socket is closed.
|
||||
if (error != asio::error::operation_aborted
|
||||
&& acceptor->is_open())
|
||||
RPCListen(acceptor, context, fUseSSL);
|
||||
try {
|
||||
// Immediately start accepting new connections, except when we're cancelled or our socket is closed.
|
||||
if (error != asio::error::operation_aborted
|
||||
&& acceptor->is_open())
|
||||
RPCListen(acceptor, context, fUseSSL);
|
||||
|
||||
AcceptedConnectionImpl<ip::tcp>* tcp_conn = dynamic_cast< AcceptedConnectionImpl<ip::tcp>* >(conn);
|
||||
AcceptedConnectionImpl<ip::tcp>* tcp_conn = dynamic_cast< AcceptedConnectionImpl<ip::tcp>* >(conn);
|
||||
|
||||
if (error)
|
||||
{
|
||||
if (error != asio::error::operation_aborted)
|
||||
printf("RPC accept error from %s: %s (%d)\n",
|
||||
tcp_conn ? tcp_conn->peer.address().to_string().c_str() : "unknown peer",
|
||||
error.message().c_str(),
|
||||
error.value());
|
||||
if (error)
|
||||
{
|
||||
if (error != asio::error::operation_aborted)
|
||||
printf("RPC accept error from %s: %s (%d)\n",
|
||||
tcp_conn ? tcp_conn->peer.address().to_string().c_str() : "unknown peer",
|
||||
error.message().c_str(),
|
||||
error.value());
|
||||
delete conn;
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
return;
|
||||
}
|
||||
|
||||
// Restrict callers by IP. It is important to
|
||||
// do this before starting client thread, to filter out
|
||||
// certain DoS and misbehaving clients.
|
||||
else if (tcp_conn
|
||||
&& !ClientAllowed(tcp_conn->peer.address()))
|
||||
{
|
||||
// Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
|
||||
try {
|
||||
if (!fUseSSL)
|
||||
conn->stream() << HTTPReply(HTTP_FORBIDDEN, "", false) << std::flush;
|
||||
} catch (const std::exception& e) {
|
||||
printf("RPC error sending 403 to %s: %s\n",
|
||||
tcp_conn->peer.address().to_string().c_str(), e.what());
|
||||
}
|
||||
delete conn;
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
return;
|
||||
}
|
||||
|
||||
// start HTTP client thread
|
||||
else if (!NewThread(ThreadRPCServer3, conn)) {
|
||||
printf("Failed to create RPC server client thread\n");
|
||||
delete conn;
|
||||
}
|
||||
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
} catch (std::exception& e) {
|
||||
PrintException(&e, "RPCAcceptHandler()");
|
||||
delete conn;
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
return;
|
||||
}
|
||||
|
||||
// Restrict callers by IP. It is important to
|
||||
// do this before starting client thread, to filter out
|
||||
// certain DoS and misbehaving clients.
|
||||
else if (tcp_conn
|
||||
&& !ClientAllowed(tcp_conn->peer.address()))
|
||||
{
|
||||
// Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
|
||||
if (!fUseSSL)
|
||||
conn->stream() << HTTPReply(HTTP_FORBIDDEN, "", false) << std::flush;
|
||||
} catch (...) {
|
||||
PrintException(NULL, "RPCAcceptHandler()");
|
||||
delete conn;
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
}
|
||||
|
||||
// start HTTP client thread
|
||||
else if (!NewThread(ThreadRPCServer3, conn)) {
|
||||
printf("Failed to create RPC server client thread\n");
|
||||
delete conn;
|
||||
}
|
||||
|
||||
vnThreadsRunning[THREAD_RPCLISTENER]--;
|
||||
}
|
||||
|
||||
void ThreadRPCServer2(void* parg)
|
||||
@@ -846,9 +874,7 @@ void ThreadRPCServer2(void* parg)
|
||||
"rpcpassword=%s\n"
|
||||
"(you do not need to remember this password)\n"
|
||||
"The username and password MUST NOT be the same.\n"
|
||||
"If the file does not exist, create it with owner-readable-only file permissions.\n"
|
||||
"It is also recommended to set alertnotify so you are notified of problems;\n"
|
||||
"for example: alertnotify=echo %%s | mail -s \"Triangles Alert\" admin@foo.com\n"),
|
||||
"If the file does not exist, create it with owner-readable-only file permissions.\n"),
|
||||
strWhatAmI.c_str(),
|
||||
GetConfigFile().string().c_str(),
|
||||
EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32).c_str()),
|
||||
@@ -887,7 +913,7 @@ void ThreadRPCServer2(void* parg)
|
||||
boost::system::error_code v6_only_error;
|
||||
boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(io_service));
|
||||
|
||||
boost::signals2::signal<void ()> StopRequests;
|
||||
CSignal<void()> StopRequests;
|
||||
|
||||
bool fListening = false;
|
||||
std::string strerr;
|
||||
@@ -903,10 +929,15 @@ void ThreadRPCServer2(void* parg)
|
||||
acceptor->listen(socket_base::max_listen_connections);
|
||||
|
||||
RPCListen(acceptor, context, fUseSSL);
|
||||
// Cancel outstanding listen-requests for this acceptor when shutting down
|
||||
StopRequests.connect(signals2::slot<void ()>(
|
||||
static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
|
||||
.track(acceptor));
|
||||
// Cancel outstanding listen-requests for this acceptor when shutting down.
|
||||
// weak_ptr emulates signals2's .track(): if the acceptor has already been
|
||||
// released by the time StopRequests fires, the slot is a no-op.
|
||||
{
|
||||
boost::weak_ptr<ip::tcp::acceptor> weak_acceptor(acceptor);
|
||||
StopRequests.connect([weak_acceptor]() {
|
||||
if (auto a = weak_acceptor.lock()) a->close();
|
||||
});
|
||||
}
|
||||
|
||||
fListening = true;
|
||||
}
|
||||
@@ -929,10 +960,13 @@ void ThreadRPCServer2(void* parg)
|
||||
acceptor->listen(socket_base::max_listen_connections);
|
||||
|
||||
RPCListen(acceptor, context, fUseSSL);
|
||||
// Cancel outstanding listen-requests for this acceptor when shutting down
|
||||
StopRequests.connect(signals2::slot<void ()>(
|
||||
static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
|
||||
.track(acceptor));
|
||||
// See note above on weak_ptr-based .track() emulation.
|
||||
{
|
||||
boost::weak_ptr<ip::tcp::acceptor> weak_acceptor(acceptor);
|
||||
StopRequests.connect([weak_acceptor]() {
|
||||
if (auto a = weak_acceptor.lock()) a->close();
|
||||
});
|
||||
}
|
||||
|
||||
fListening = true;
|
||||
}
|
||||
@@ -1123,6 +1157,7 @@ void ThreadRPCServer3(void* parg)
|
||||
AcceptedConnection *conn = (AcceptedConnection *) parg;
|
||||
|
||||
bool fRun = true;
|
||||
try {
|
||||
while (true)
|
||||
{
|
||||
if (fShutdown || !fRun)
|
||||
@@ -1242,6 +1277,13 @@ void ThreadRPCServer3(void* parg)
|
||||
}
|
||||
}
|
||||
|
||||
} // end try
|
||||
catch (std::exception& e) {
|
||||
PrintException(&e, "ThreadRPCServer3()");
|
||||
} catch (...) {
|
||||
PrintException(NULL, "ThreadRPCServer3()");
|
||||
}
|
||||
|
||||
delete conn;
|
||||
{
|
||||
LOCK(cs_THREAD_RPCHANDLER);
|
||||
@@ -1395,12 +1437,6 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
|
||||
if (strMethod == "walletpassphrase" && n > 2) ConvertTo<bool>(params[2]);
|
||||
if (strMethod == "listsinceblock" && n > 1) ConvertTo<boost::int64_t>(params[1]);
|
||||
|
||||
if (strMethod == "sendalert" && n > 2) ConvertTo<boost::int64_t>(params[2]);
|
||||
if (strMethod == "sendalert" && n > 3) ConvertTo<boost::int64_t>(params[3]);
|
||||
if (strMethod == "sendalert" && n > 4) ConvertTo<boost::int64_t>(params[4]);
|
||||
if (strMethod == "sendalert" && n > 5) ConvertTo<boost::int64_t>(params[5]);
|
||||
if (strMethod == "sendalert" && n > 6) ConvertTo<boost::int64_t>(params[6]);
|
||||
|
||||
if (strMethod == "sendmany" && n > 1) ConvertTo<Object>(params[1]);
|
||||
if (strMethod == "sendmany" && n > 2) ConvertTo<boost::int64_t>(params[2]);
|
||||
if (strMethod == "reservebalance" && n > 0) ConvertTo<bool>(params[0]);
|
||||
|
||||
+3
-6
@@ -148,6 +148,7 @@ extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, b
|
||||
extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getseedlist(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getnetworkstability(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value disconnectnode(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getwalletinfo(const json_spirit::Array& params, bool fHelp);
|
||||
@@ -156,8 +157,6 @@ extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fH
|
||||
extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
|
||||
extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
extern json_spirit::Value sendalert(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
extern json_spirit::Value getsubsidy(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getstakinginfo(const json_spirit::Array& params, bool fHelp);
|
||||
@@ -227,15 +226,13 @@ extern json_spirit::Value getchaintips(const json_spirit::Array& params, bool fH
|
||||
extern json_spirit::Value invalidateblock(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value reconsiderblock(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value recalculatesupply(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value auditsignatures(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value dumputxoset(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
extern json_spirit::Value getaddressbalance(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getaddressutxos(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value getaddresstxids(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
extern json_spirit::Value clearwallettransactions(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
|
||||
|
||||
extern json_spirit::Value smsgenable(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value smsgdisable(const json_spirit::Array& params, bool fHelp);
|
||||
extern json_spirit::Value smsglocalkeys(const json_spirit::Array& params, bool fHelp);
|
||||
|
||||
@@ -0,0 +1,457 @@
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers.
|
||||
// Copyright (c) 2026 The Triangles developers.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "txdb-base.h"
|
||||
|
||||
#include "addressindex.h"
|
||||
#include "main.h"
|
||||
#include "sync.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// ============================================================================
|
||||
// Schema versioning
|
||||
// ============================================================================
|
||||
bool CTxDBBase::ReadVersion(int& nVersion)
|
||||
{
|
||||
nVersion = 0;
|
||||
return Read(string("version"), nVersion);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteVersion(int nVersion)
|
||||
{
|
||||
return Write(string("version"), nVersion);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadDbFormat(int& nDbFormat)
|
||||
{
|
||||
nDbFormat = 1;
|
||||
return Read(string("dbformat"), nDbFormat);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteDbFormat(int nDbFormat)
|
||||
{
|
||||
return Write(string("dbformat"), nDbFormat);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tx index
|
||||
// ============================================================================
|
||||
bool CTxDBBase::ReadTxIndex(uint256 hash, CTxIndex& txindex)
|
||||
{
|
||||
assert(!fClient);
|
||||
txindex.SetNull();
|
||||
return Read(make_pair(string("tx"), hash), txindex);
|
||||
}
|
||||
|
||||
bool CTxDBBase::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
|
||||
{
|
||||
assert(!fClient);
|
||||
return Write(make_pair(string("tx"), hash), txindex);
|
||||
}
|
||||
|
||||
bool CTxDBBase::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
|
||||
{
|
||||
assert(!fClient);
|
||||
uint256 hash = tx.GetHash();
|
||||
CTxIndex txindex(pos, tx.vout.size());
|
||||
return Write(make_pair(string("tx"), hash), txindex);
|
||||
}
|
||||
|
||||
bool CTxDBBase::EraseTxIndex(const CTransaction& tx)
|
||||
{
|
||||
assert(!fClient);
|
||||
uint256 hash = tx.GetHash();
|
||||
return Erase(make_pair(string("tx"), hash));
|
||||
}
|
||||
|
||||
bool CTxDBBase::ContainsTx(uint256 hash)
|
||||
{
|
||||
assert(!fClient);
|
||||
return Exists(make_pair(string("tx"), hash));
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
|
||||
{
|
||||
assert(!fClient);
|
||||
tx.SetNull();
|
||||
if (!ReadTxIndex(hash, txindex))
|
||||
return false;
|
||||
return tx.ReadFromDisk(txindex.pos);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadDiskTx(uint256 hash, CTransaction& tx)
|
||||
{
|
||||
CTxIndex txindex;
|
||||
return ReadDiskTx(hash, tx, txindex);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
|
||||
{
|
||||
return ReadDiskTx(outpoint.hash, tx, txindex);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
|
||||
{
|
||||
CTxIndex txindex;
|
||||
return ReadDiskTx(outpoint.hash, tx, txindex);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Block index
|
||||
// ============================================================================
|
||||
bool CTxDBBase::WriteBlockIndex(const CDiskBlockIndex& blockindex)
|
||||
{
|
||||
return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Best chain / checkpoint metadata
|
||||
// ============================================================================
|
||||
bool CTxDBBase::ReadHashBestChain(uint256& hashBestChain)
|
||||
{
|
||||
return Read(string("hashBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteHashBestChain(uint256 hashBestChain)
|
||||
{
|
||||
return Write(string("hashBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadAddressIndexBestChain(uint256& hashBestChain)
|
||||
{
|
||||
return Read(string("addressIndexBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteAddressIndexBestChain(uint256 hashBestChain)
|
||||
{
|
||||
return Write(string("addressIndexBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadAddressIndexStartHeight(int& nHeight)
|
||||
{
|
||||
return Read(string("addressIndexStartHeight"), nHeight);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteAddressIndexStartHeight(int nHeight)
|
||||
{
|
||||
return Write(string("addressIndexStartHeight"), nHeight);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
|
||||
{
|
||||
return Read(string("bnBestInvalidTrust"), bnBestInvalidTrust);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
|
||||
{
|
||||
return Write(string("bnBestInvalidTrust"), bnBestInvalidTrust);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadSyncCheckpoint(uint256& hashCheckpoint)
|
||||
{
|
||||
return Read(string("hashSyncCheckpoint"), hashCheckpoint);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteSyncCheckpoint(uint256 hashCheckpoint)
|
||||
{
|
||||
return Write(string("hashSyncCheckpoint"), hashCheckpoint);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadCheckpointPubKey(string& strPubKey)
|
||||
{
|
||||
return Read(string("strCheckpointPubKey"), strPubKey);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteCheckpointPubKey(const string& strPubKey)
|
||||
{
|
||||
return Write(string("strCheckpointPubKey"), strPubKey);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Address index
|
||||
// ============================================================================
|
||||
bool CTxDBBase::ReadAddressBalance(int nType, const uint160& hashBytes, int64_t& nBalance)
|
||||
{
|
||||
return Read(make_pair(string("addrbal"), CAddressBalanceKey(nType, hashBytes)), nBalance);
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteAddressBalance(int nType, const uint160& hashBytes, int64_t nBalance)
|
||||
{
|
||||
return Write(make_pair(string("addrbal"), CAddressBalanceKey(nType, hashBytes)), nBalance);
|
||||
}
|
||||
|
||||
bool CTxDBBase::ReadAddressUtxo(int nType, const uint160& hashBytes,
|
||||
const uint256& txhash, int nIndex,
|
||||
int64_t& nValue, int& nHeight)
|
||||
{
|
||||
CAddressUtxoValue val;
|
||||
if (!Read(make_pair(string("addrutxo"),
|
||||
CAddressUtxoKey(nType, hashBytes, txhash, nIndex)), val))
|
||||
return false;
|
||||
nValue = val.nValue;
|
||||
nHeight = val.nHeight;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteAddressUtxo(int nType, const uint160& hashBytes,
|
||||
const uint256& txhash, int nIndex,
|
||||
int64_t nValue, int nHeight, const CScript& script)
|
||||
{
|
||||
return Write(make_pair(string("addrutxo"),
|
||||
CAddressUtxoKey(nType, hashBytes, txhash, nIndex)),
|
||||
CAddressUtxoValue(nValue, nHeight, script));
|
||||
}
|
||||
|
||||
bool CTxDBBase::EraseAddressUtxo(int nType, const uint160& hashBytes,
|
||||
const uint256& txhash, int nIndex)
|
||||
{
|
||||
return Erase(make_pair(string("addrutxo"),
|
||||
CAddressUtxoKey(nType, hashBytes, txhash, nIndex)));
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteAddressTxId(int nType, const uint160& hashBytes, int nHeight,
|
||||
int nTxIndex, const uint256& txhash)
|
||||
{
|
||||
return Write(make_pair(string("addrtxid"),
|
||||
CAddressTxIdKey(nType, hashBytes, nHeight, nTxIndex, txhash)),
|
||||
(char)0);
|
||||
}
|
||||
|
||||
bool CTxDBBase::EraseAddressTxId(int nType, const uint160& hashBytes, int nHeight,
|
||||
int nTxIndex, const uint256& txhash)
|
||||
{
|
||||
return Erase(make_pair(string("addrtxid"),
|
||||
CAddressTxIdKey(nType, hashBytes, nHeight, nTxIndex, txhash)));
|
||||
}
|
||||
|
||||
bool CTxDBBase::GetAddressUtxos(int nType, const uint160& hashBytes,
|
||||
std::vector<std::pair<COutPoint, std::pair<int64_t, int> > >& vUtxos)
|
||||
{
|
||||
vUtxos.clear();
|
||||
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("addrutxo"),
|
||||
CAddressUtxoKey(nType, hashBytes, uint256(0), 0));
|
||||
string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
auto it = NewIterator();
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
const string keyStr = it->KeyStr();
|
||||
CDataStream ssKey(keyStr.data(), keyStr.data() + keyStr.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
string strKeyType;
|
||||
CAddressUtxoKey utxoKey;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "addrutxo")
|
||||
break;
|
||||
ssKey >> utxoKey;
|
||||
if (utxoKey.nType != nType || utxoKey.hashBytes != hashBytes)
|
||||
break;
|
||||
|
||||
const string valueStr = it->ValueStr();
|
||||
CDataStream ssValue(valueStr.data(), valueStr.data() + valueStr.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
CAddressUtxoValue utxoValue;
|
||||
ssValue >> utxoValue;
|
||||
|
||||
COutPoint outpoint(utxoKey.txhash, utxoKey.nIndex);
|
||||
vUtxos.push_back(make_pair(outpoint,
|
||||
make_pair(utxoValue.nValue, utxoValue.nHeight)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDBBase::GetAddressTxIds(int nType, const uint160& hashBytes,
|
||||
int nStartHeight, int nEndHeight,
|
||||
std::vector<uint256>& vTxIds)
|
||||
{
|
||||
vTxIds.clear();
|
||||
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("addrtxid"),
|
||||
CAddressTxIdKey(nType, hashBytes, nStartHeight, 0, uint256(0)));
|
||||
string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
auto it = NewIterator();
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
const string keyStr = it->KeyStr();
|
||||
CDataStream ssKey(keyStr.data(), keyStr.data() + keyStr.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
string strKeyType;
|
||||
CAddressTxIdKey txIdKey;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "addrtxid")
|
||||
break;
|
||||
ssKey >> txIdKey;
|
||||
if (txIdKey.nType != nType || txIdKey.hashBytes != hashBytes)
|
||||
break;
|
||||
if (txIdKey.nHeight > nEndHeight)
|
||||
break;
|
||||
|
||||
vTxIds.push_back(txIdKey.txhash);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// In-memory UTXO cache (read-through, backend-agnostic)
|
||||
//
|
||||
// Avoids hitting the underlying KV store for every FetchInputs call. On a 2M+
|
||||
// block chain with millions of UTXOs, this dramatically reduces I/O during
|
||||
// both IBD (ConnectBlock validation reads inputs) and steady-state (mempool
|
||||
// acceptance, staking). Writes/erases update both cache and the backend.
|
||||
// ============================================================================
|
||||
namespace {
|
||||
|
||||
struct COutPointHasher {
|
||||
size_t operator()(const COutPoint& op) const {
|
||||
return op.hash.Get64() ^
|
||||
(std::hash<unsigned int>()(op.n) * 0x9e3779b97f4a7c15ULL);
|
||||
}
|
||||
};
|
||||
|
||||
struct CUtxoCacheEntry {
|
||||
CUtxoEntry utxo;
|
||||
bool fPresent; // true = exists, false = known absent (negative cache)
|
||||
CUtxoCacheEntry() : fPresent(false) {}
|
||||
CUtxoCacheEntry(const CUtxoEntry& u, bool p) : utxo(u), fPresent(p) {}
|
||||
};
|
||||
|
||||
std::unordered_map<COutPoint, CUtxoCacheEntry, COutPointHasher> g_mapUtxoCache;
|
||||
CCriticalSection g_cs_utxoCache;
|
||||
const size_t UTXO_CACHE_MAX_ENTRIES = 2000000; // ~400MB at ~200 bytes each
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool CTxDBBase::ReadUtxo(const uint256& hash, unsigned int n, CUtxoEntry& entry)
|
||||
{
|
||||
entry.SetNull();
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(g_cs_utxoCache);
|
||||
auto it = g_mapUtxoCache.find(outpoint);
|
||||
if (it != g_mapUtxoCache.end())
|
||||
{
|
||||
if (it->second.fPresent) {
|
||||
entry = it->second.utxo;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool fFound = Read(make_pair(string("u"), make_pair(hash, n)), entry);
|
||||
|
||||
{
|
||||
LOCK(g_cs_utxoCache);
|
||||
if (g_mapUtxoCache.size() < UTXO_CACHE_MAX_ENTRIES)
|
||||
{
|
||||
if (fFound)
|
||||
g_mapUtxoCache[outpoint] = CUtxoCacheEntry(entry, true);
|
||||
else
|
||||
g_mapUtxoCache[outpoint] = CUtxoCacheEntry(CUtxoEntry(), false);
|
||||
}
|
||||
}
|
||||
|
||||
return fFound;
|
||||
}
|
||||
|
||||
bool CTxDBBase::WriteUtxo(const uint256& hash, unsigned int n, const CUtxoEntry& entry)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(g_cs_utxoCache);
|
||||
g_mapUtxoCache[outpoint] = CUtxoCacheEntry(entry, true);
|
||||
|
||||
// Periodic eviction: clear half when over the limit. Simple but
|
||||
// effective — the cache repopulates with the hot working set.
|
||||
if (g_mapUtxoCache.size() > UTXO_CACHE_MAX_ENTRIES)
|
||||
{
|
||||
size_t nTarget = UTXO_CACHE_MAX_ENTRIES / 2;
|
||||
auto it = g_mapUtxoCache.begin();
|
||||
while (g_mapUtxoCache.size() > nTarget && it != g_mapUtxoCache.end())
|
||||
it = g_mapUtxoCache.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
return Write(make_pair(string("u"), make_pair(hash, n)), entry);
|
||||
}
|
||||
|
||||
bool CTxDBBase::EraseUtxo(const uint256& hash, unsigned int n)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(g_cs_utxoCache);
|
||||
g_mapUtxoCache[outpoint] = CUtxoCacheEntry(CUtxoEntry(), false);
|
||||
}
|
||||
|
||||
return Erase(make_pair(string("u"), make_pair(hash, n)));
|
||||
}
|
||||
|
||||
bool CTxDBBase::HaveUtxo(const uint256& hash, unsigned int n)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(g_cs_utxoCache);
|
||||
auto it = g_mapUtxoCache.find(outpoint);
|
||||
if (it != g_mapUtxoCache.end())
|
||||
return it->second.fPresent;
|
||||
}
|
||||
|
||||
if (Exists(make_pair(string("u"), make_pair(hash, n))))
|
||||
return true;
|
||||
|
||||
// Lazy fallback: check old CTxIndex vSpent for databases upgrading from
|
||||
// pre-UTXO format. vSpent[n] null = output not spent = UTXO exists.
|
||||
CTxIndex txindex;
|
||||
if (ReadTxIndex(hash, txindex))
|
||||
{
|
||||
if (n < txindex.vSpent.size() && txindex.vSpent[n].IsNull())
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t CTxDBBase::SumUtxoValues(int& nCount)
|
||||
{
|
||||
nCount = 0;
|
||||
int64_t nTotal = 0;
|
||||
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("u"), make_pair(uint256(0), (unsigned int)0));
|
||||
string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
auto it = NewIterator();
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
const string keyStr = it->KeyStr();
|
||||
CDataStream ssKey(keyStr.data(), keyStr.data() + keyStr.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
string strKeyType;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "u")
|
||||
break;
|
||||
|
||||
const string valueStr = it->ValueStr();
|
||||
CDataStream ssValue(valueStr.data(), valueStr.data() + valueStr.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
CUtxoEntry entry;
|
||||
ssValue >> entry;
|
||||
|
||||
nTotal += entry.nValue;
|
||||
nCount++;
|
||||
}
|
||||
return nTotal;
|
||||
}
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers.
|
||||
// Copyright (c) 2026 The Triangles developers.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef TRIANGLES_TXDB_BASE_H
|
||||
#define TRIANGLES_TXDB_BASE_H
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
class CScript;
|
||||
class CTransaction;
|
||||
class CDiskTxPos;
|
||||
class CTxIndex;
|
||||
class CDiskBlockIndex;
|
||||
class CUtxoEntry;
|
||||
class CBigNum;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Backend-agnostic key/value iterator.
|
||||
//
|
||||
// Each CTxDBBase backend returns a std::unique_ptr<CTxDBIteratorBase> from
|
||||
// NewIterator(). Iterators yield raw serialized key/value bytes; callers
|
||||
// deserialize using the same SER_DISK / CLIENT_VERSION conventions used by
|
||||
// CTxDBBase's templated Read/Write paths.
|
||||
//
|
||||
// Iterators do NOT see uncommitted writes in an active batch. All current
|
||||
// iteration sites (block-index scan, address-index range queries, UTXO sum)
|
||||
// run outside transactions, so this is safe.
|
||||
// ----------------------------------------------------------------------------
|
||||
class CTxDBIteratorBase
|
||||
{
|
||||
public:
|
||||
virtual ~CTxDBIteratorBase() = default;
|
||||
|
||||
virtual void Seek(const std::string& key) = 0;
|
||||
virtual bool Valid() const = 0;
|
||||
virtual void Next() = 0;
|
||||
virtual std::string KeyStr() const = 0;
|
||||
virtual std::string ValueStr() const = 0;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Abstract chain database interface.
|
||||
//
|
||||
// All key/value serialization happens in this base class via CDataStream with
|
||||
// SER_DISK / CLIENT_VERSION. Backends only implement byte-level I/O, so every
|
||||
// backend produces bit-identical key bytes — required for migration and
|
||||
// dual-backend parity testing.
|
||||
//
|
||||
// Named operations (ReadTxIndex, WriteBlockIndex, etc.) are implemented in
|
||||
// terms of the templated Read/Write/Erase/Exists, which dispatch to the
|
||||
// virtual byte-level methods. To add a new backend:
|
||||
//
|
||||
// 1. Subclass CTxDBBase.
|
||||
// 2. Implement Close, TxnBegin/Commit/Abort.
|
||||
// 3. Implement ReadRaw, WriteRaw, EraseRaw, ExistsRaw.
|
||||
// 4. Implement NewIterator (return a subclass of CTxDBIteratorBase).
|
||||
// 5. Implement LoadBlockIndex (still backend-specific in M1; will be
|
||||
// extracted to the base in a later phase).
|
||||
// ----------------------------------------------------------------------------
|
||||
class CTxDBBase
|
||||
{
|
||||
public:
|
||||
virtual ~CTxDBBase() = default;
|
||||
|
||||
// Destroys the underlying shared global state accessed by this DB.
|
||||
virtual void Close() = 0;
|
||||
|
||||
// Batches (transaction-like atomic groups of writes/deletes).
|
||||
virtual bool TxnBegin() = 0;
|
||||
virtual bool TxnCommit() = 0;
|
||||
virtual bool TxnAbort() = 0;
|
||||
|
||||
bool IsReadOnly() const { return fReadOnly; }
|
||||
|
||||
// ── Schema versioning ────────────────────────────────────────────────────
|
||||
bool ReadVersion(int& nVersion);
|
||||
bool WriteVersion(int nVersion);
|
||||
bool ReadDbFormat(int& nDbFormat);
|
||||
bool WriteDbFormat(int nDbFormat);
|
||||
|
||||
// ── Tx index ─────────────────────────────────────────────────────────────
|
||||
bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
|
||||
bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
|
||||
bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
|
||||
bool EraseTxIndex(const CTransaction& tx);
|
||||
bool ContainsTx(uint256 hash);
|
||||
bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
|
||||
bool ReadDiskTx(uint256 hash, CTransaction& tx);
|
||||
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
|
||||
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
|
||||
|
||||
// ── Block index ──────────────────────────────────────────────────────────
|
||||
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
|
||||
|
||||
// ── Best chain / checkpoint metadata ─────────────────────────────────────
|
||||
bool ReadHashBestChain(uint256& hashBestChain);
|
||||
bool WriteHashBestChain(uint256 hashBestChain);
|
||||
bool ReadAddressIndexBestChain(uint256& hashBestChain);
|
||||
bool WriteAddressIndexBestChain(uint256 hashBestChain);
|
||||
bool ReadAddressIndexStartHeight(int& nHeight);
|
||||
bool WriteAddressIndexStartHeight(int nHeight);
|
||||
bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
|
||||
bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
|
||||
bool ReadSyncCheckpoint(uint256& hashCheckpoint);
|
||||
bool WriteSyncCheckpoint(uint256 hashCheckpoint);
|
||||
bool ReadCheckpointPubKey(std::string& strPubKey);
|
||||
bool WriteCheckpointPubKey(const std::string& strPubKey);
|
||||
|
||||
virtual bool LoadBlockIndex() = 0;
|
||||
|
||||
// ── Address index ────────────────────────────────────────────────────────
|
||||
bool ReadAddressBalance(int nType, const uint160& hashBytes, int64_t& nBalance);
|
||||
bool WriteAddressBalance(int nType, const uint160& hashBytes, int64_t nBalance);
|
||||
bool ReadAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash,
|
||||
int nIndex, int64_t& nValue, int& nHeight);
|
||||
bool WriteAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash,
|
||||
int nIndex, int64_t nValue, int nHeight, const CScript& script);
|
||||
bool EraseAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash,
|
||||
int nIndex);
|
||||
bool WriteAddressTxId(int nType, const uint160& hashBytes, int nHeight,
|
||||
int nTxIndex, const uint256& txhash);
|
||||
bool EraseAddressTxId(int nType, const uint160& hashBytes, int nHeight,
|
||||
int nTxIndex, const uint256& txhash);
|
||||
bool GetAddressUtxos(int nType, const uint160& hashBytes,
|
||||
std::vector<std::pair<COutPoint, std::pair<int64_t, int> > >& vUtxos);
|
||||
bool GetAddressTxIds(int nType, const uint160& hashBytes, int nStartHeight,
|
||||
int nEndHeight, std::vector<uint256>& vTxIds);
|
||||
|
||||
// ── UTXO set ─────────────────────────────────────────────────────────────
|
||||
bool ReadUtxo(const uint256& hash, unsigned int n, CUtxoEntry& entry);
|
||||
bool WriteUtxo(const uint256& hash, unsigned int n, const CUtxoEntry& entry);
|
||||
bool EraseUtxo(const uint256& hash, unsigned int n);
|
||||
bool HaveUtxo(const uint256& hash, unsigned int n);
|
||||
int64_t SumUtxoValues(int& nCount);
|
||||
|
||||
// Range scans use this directly (e.g. UtxoSnapshot::DumpSnapshot iterating
|
||||
// the "u" keyspace). The iterator yields raw serialized key/value bytes.
|
||||
virtual std::unique_ptr<CTxDBIteratorBase> NewIterator() const = 0;
|
||||
|
||||
protected:
|
||||
bool fReadOnly = false;
|
||||
|
||||
// Byte-level I/O — backends implement these.
|
||||
virtual bool ReadRaw(const std::string& key, std::string& value) const = 0;
|
||||
virtual bool WriteRaw(const std::string& key, const std::string& value) = 0;
|
||||
virtual bool EraseRaw(const std::string& key) = 0;
|
||||
virtual bool ExistsRaw(const std::string& key) const = 0;
|
||||
|
||||
// Templated Read/Write/Erase/Exists are non-virtual (templates can't be
|
||||
// virtual in C++) — they serialize and dispatch to the byte-level virtuals.
|
||||
template<typename K, typename T>
|
||||
bool Read(const K& key, T& value) const
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
std::string strValue;
|
||||
if (!ReadRaw(ssKey.str(), strValue))
|
||||
return false;
|
||||
try {
|
||||
CDataStream ssValue(strValue.data(),
|
||||
strValue.data() + strValue.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
ssValue >> value;
|
||||
} catch (std::exception&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
bool Write(const K& key, const T& value)
|
||||
{
|
||||
if (fReadOnly)
|
||||
assert(!"Write called on database in read-only mode");
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue.reserve(10000);
|
||||
ssValue << value;
|
||||
return WriteRaw(ssKey.str(), ssValue.str());
|
||||
}
|
||||
|
||||
template<typename K>
|
||||
bool Erase(const K& key)
|
||||
{
|
||||
if (fReadOnly)
|
||||
assert(!"Erase called on database in read-only mode");
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
return EraseRaw(ssKey.str());
|
||||
}
|
||||
|
||||
template<typename K>
|
||||
bool Exists(const K& key) const
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
return ExistsRaw(ssKey.str());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // TRIANGLES_TXDB_BASE_H
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2026 The Triangles developers.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "txdb.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
// Pick the backend once per process. -chaindb is a startup flag; switching at
|
||||
// runtime would require reopening every CTxDB instance, which the codebase
|
||||
// doesn't currently support. We cache the resolved choice so subsequent
|
||||
// MakeChainDB calls don't re-parse the argument.
|
||||
enum class ChainDbKind { LevelDB, RocksDB };
|
||||
|
||||
ChainDbKind ResolveChainDbKind()
|
||||
{
|
||||
static const ChainDbKind kKind = []() {
|
||||
std::string s = GetArg("-chaindb", std::string("leveldb"));
|
||||
for (auto& c : s) c = std::tolower(static_cast<unsigned char>(c));
|
||||
|
||||
if (s == "leveldb")
|
||||
return ChainDbKind::LevelDB;
|
||||
if (s == "rocksdb")
|
||||
return ChainDbKind::RocksDB;
|
||||
|
||||
throw std::runtime_error(
|
||||
"-chaindb=" + s + " is not a recognized backend. "
|
||||
"Valid values: leveldb, rocksdb.");
|
||||
}();
|
||||
return kKind;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
std::unique_ptr<CTxDBBase> MakeChainDB(const char* pszMode)
|
||||
{
|
||||
switch (ResolveChainDbKind()) {
|
||||
case ChainDbKind::LevelDB:
|
||||
return std::unique_ptr<CTxDBBase>(new CTxDB(pszMode));
|
||||
case ChainDbKind::RocksDB:
|
||||
return std::unique_ptr<CTxDBBase>(new CRocksTxDB(pszMode));
|
||||
}
|
||||
// Unreachable — ResolveChainDbKind throws on bad input.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool IsRocksDbChainBackend()
|
||||
{
|
||||
return ResolveChainDbKind() == ChainDbKind::RocksDB;
|
||||
}
|
||||
|
||||
std::filesystem::path GetChainDataDir()
|
||||
{
|
||||
switch (ResolveChainDbKind()) {
|
||||
case ChainDbKind::LevelDB: return GetDataDir() / "txleveldb";
|
||||
case ChainDbKind::RocksDB: return GetDataDir() / "rocksdb";
|
||||
}
|
||||
return GetDataDir() / "txleveldb"; // unreachable
|
||||
}
|
||||
|
||||
void WipeChainDataDir()
|
||||
{
|
||||
fs::path p = GetChainDataDir();
|
||||
if (fs::exists(p))
|
||||
fs::remove_all(p);
|
||||
}
|
||||
+90
-449
@@ -4,11 +4,10 @@
|
||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include <boost/version.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
|
||||
#include <leveldb/env.h>
|
||||
#include <leveldb/cache.h>
|
||||
@@ -25,8 +24,7 @@
|
||||
#include "main.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
leveldb::DB *txdb; // global pointer for LevelDB object instance
|
||||
|
||||
@@ -41,29 +39,22 @@ static leveldb::Options GetOptions() {
|
||||
// memtable flushes and compactions, which is a big win during IBD
|
||||
// when millions of tx index entries are written sequentially.
|
||||
options.write_buffer_size = 64 * 1048576;
|
||||
// Allow more open files for better read performance on large chains
|
||||
options.max_open_files = 1000;
|
||||
return options;
|
||||
}
|
||||
|
||||
void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
|
||||
// First time init.
|
||||
fs::path directory = GetDataDir() / "txleveldb";
|
||||
|
||||
if (fRemoveOld) {
|
||||
fs::remove_all(directory); // remove directory
|
||||
fs::remove_all(directory);
|
||||
unsigned int nFile = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
fs::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
|
||||
|
||||
// Break if no such file
|
||||
if( !fs::exists( strBlockFile ) )
|
||||
if(!fs::exists(strBlockFile))
|
||||
break;
|
||||
|
||||
fs::remove(strBlockFile);
|
||||
|
||||
nFile++;
|
||||
}
|
||||
}
|
||||
@@ -76,8 +67,6 @@ void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
|
||||
}
|
||||
}
|
||||
|
||||
// CDB subclasses are created and destroyed VERY OFTEN. That's why
|
||||
// we shouldn't treat this as a free operations.
|
||||
CTxDB::CTxDB(const char* pszMode)
|
||||
{
|
||||
assert(pszMode);
|
||||
@@ -95,7 +84,7 @@ CTxDB::CTxDB(const char* pszMode)
|
||||
options.create_if_missing = fCreate;
|
||||
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
|
||||
|
||||
init_blockindex(options); // Init directory
|
||||
init_blockindex(options);
|
||||
pdb = txdb;
|
||||
|
||||
if (Exists(string("version")))
|
||||
@@ -107,18 +96,17 @@ CTxDB::CTxDB(const char* pszMode)
|
||||
{
|
||||
printf("Required index version is %d, removing old database\n", DATABASE_VERSION);
|
||||
|
||||
// Leveldb instance destruction
|
||||
delete txdb;
|
||||
txdb = pdb = NULL;
|
||||
delete activeBatch;
|
||||
activeBatch = NULL;
|
||||
|
||||
init_blockindex(options, true); // Remove directory and create new database
|
||||
init_blockindex(options, true);
|
||||
pdb = txdb;
|
||||
|
||||
bool fTmp = fReadOnly;
|
||||
fReadOnly = false;
|
||||
WriteVersion(DATABASE_VERSION); // Save transaction index version
|
||||
WriteVersion(DATABASE_VERSION);
|
||||
fReadOnly = fTmp;
|
||||
}
|
||||
}
|
||||
@@ -171,6 +159,8 @@ bool CTxDB::TxnCommit()
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class CBatchScanner : public leveldb::WriteBatch::Handler {
|
||||
public:
|
||||
std::string needle;
|
||||
@@ -196,16 +186,32 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// When performing a read, if we have an active batch we need to check it first
|
||||
// before reading from the database, as the rest of the code assumes that once
|
||||
// a database transaction begins reads are consistent with it. It would be good
|
||||
// to change that assumption in future and avoid the performance hit, though in
|
||||
// practice it does not appear to be large.
|
||||
bool CTxDB::ScanBatch(const CDataStream &key, string *value, bool *deleted) const {
|
||||
class CLevelDBIterator final : public CTxDBIteratorBase {
|
||||
public:
|
||||
explicit CLevelDBIterator(leveldb::Iterator* pit) : pit(pit) {}
|
||||
~CLevelDBIterator() override { delete pit; }
|
||||
|
||||
void Seek(const std::string& key) override { pit->Seek(key); }
|
||||
bool Valid() const override { return pit->Valid(); }
|
||||
void Next() override { pit->Next(); }
|
||||
std::string KeyStr() const override { return pit->key().ToString(); }
|
||||
std::string ValueStr() const override { return pit->value().ToString(); }
|
||||
|
||||
private:
|
||||
leveldb::Iterator* pit;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
// When performing a read with an active batch, check the batch first. The
|
||||
// rest of the codebase assumes that once a batch is open, reads are
|
||||
// consistent with the pending writes inside it.
|
||||
bool CTxDB::ScanBatch(const std::string& key, string* value, bool* deleted) const
|
||||
{
|
||||
assert(activeBatch);
|
||||
*deleted = false;
|
||||
CBatchScanner scanner;
|
||||
scanner.needle = key.str();
|
||||
scanner.needle = key;
|
||||
scanner.deleted = deleted;
|
||||
scanner.foundValue = value;
|
||||
leveldb::Status status = activeBatch->Iterate(&scanner);
|
||||
@@ -215,132 +221,71 @@ bool CTxDB::ScanBatch(const CDataStream &key, string *value, bool *deleted) cons
|
||||
return scanner.foundEntry;
|
||||
}
|
||||
|
||||
bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
|
||||
bool CTxDB::ReadRaw(const std::string& key, std::string& value) const
|
||||
{
|
||||
assert(!fClient);
|
||||
txindex.SetNull();
|
||||
return Read(make_pair(string("tx"), hash), txindex);
|
||||
bool readFromDb = true;
|
||||
if (activeBatch) {
|
||||
bool deleted = false;
|
||||
readFromDb = ScanBatch(key, &value, &deleted) == false;
|
||||
if (deleted)
|
||||
return false;
|
||||
}
|
||||
if (readFromDb) {
|
||||
leveldb::Status status = pdb->Get(leveldb::ReadOptions(), key, &value);
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
printf("LevelDB read failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
|
||||
bool CTxDB::WriteRaw(const std::string& key, const std::string& value)
|
||||
{
|
||||
assert(!fClient);
|
||||
return Write(make_pair(string("tx"), hash), txindex);
|
||||
}
|
||||
|
||||
bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
|
||||
{
|
||||
assert(!fClient);
|
||||
|
||||
// Add to tx index
|
||||
uint256 hash = tx.GetHash();
|
||||
CTxIndex txindex(pos, tx.vout.size());
|
||||
return Write(make_pair(string("tx"), hash), txindex);
|
||||
}
|
||||
|
||||
bool CTxDB::EraseTxIndex(const CTransaction& tx)
|
||||
{
|
||||
assert(!fClient);
|
||||
uint256 hash = tx.GetHash();
|
||||
|
||||
return Erase(make_pair(string("tx"), hash));
|
||||
}
|
||||
|
||||
bool CTxDB::ContainsTx(uint256 hash)
|
||||
{
|
||||
assert(!fClient);
|
||||
return Exists(make_pair(string("tx"), hash));
|
||||
}
|
||||
|
||||
bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
|
||||
{
|
||||
assert(!fClient);
|
||||
tx.SetNull();
|
||||
if (!ReadTxIndex(hash, txindex))
|
||||
if (activeBatch) {
|
||||
activeBatch->Put(key, value);
|
||||
return true;
|
||||
}
|
||||
leveldb::Status status = pdb->Put(leveldb::WriteOptions(), key, value);
|
||||
if (!status.ok()) {
|
||||
printf("LevelDB write failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
return (tx.ReadFromDisk(txindex.pos));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
|
||||
bool CTxDB::EraseRaw(const std::string& key)
|
||||
{
|
||||
CTxIndex txindex;
|
||||
return ReadDiskTx(hash, tx, txindex);
|
||||
if (!pdb)
|
||||
return false;
|
||||
if (activeBatch) {
|
||||
activeBatch->Delete(key);
|
||||
return true;
|
||||
}
|
||||
leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), key);
|
||||
return (status.ok() || status.IsNotFound());
|
||||
}
|
||||
|
||||
bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
|
||||
bool CTxDB::ExistsRaw(const std::string& key) const
|
||||
{
|
||||
return ReadDiskTx(outpoint.hash, tx, txindex);
|
||||
std::string unused;
|
||||
|
||||
if (activeBatch) {
|
||||
bool deleted = false;
|
||||
if (ScanBatch(key, &unused, &deleted) && !deleted)
|
||||
return true;
|
||||
}
|
||||
|
||||
leveldb::Status status = pdb->Get(leveldb::ReadOptions(), key, &unused);
|
||||
return status.IsNotFound() == false;
|
||||
}
|
||||
|
||||
bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
|
||||
std::unique_ptr<CTxDBIteratorBase> CTxDB::NewIterator() const
|
||||
{
|
||||
CTxIndex txindex;
|
||||
return ReadDiskTx(outpoint.hash, tx, txindex);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
|
||||
{
|
||||
return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
|
||||
{
|
||||
return Read(string("hashBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
|
||||
{
|
||||
return Write(string("hashBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadAddressIndexBestChain(uint256& hashBestChain)
|
||||
{
|
||||
return Read(string("addressIndexBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteAddressIndexBestChain(uint256 hashBestChain)
|
||||
{
|
||||
return Write(string("addressIndexBestChain"), hashBestChain);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadAddressIndexStartHeight(int& nHeight)
|
||||
{
|
||||
return Read(string("addressIndexStartHeight"), nHeight);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteAddressIndexStartHeight(int nHeight)
|
||||
{
|
||||
return Write(string("addressIndexStartHeight"), nHeight);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
|
||||
{
|
||||
return Read(string("bnBestInvalidTrust"), bnBestInvalidTrust);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
|
||||
{
|
||||
return Write(string("bnBestInvalidTrust"), bnBestInvalidTrust);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
|
||||
{
|
||||
return Read(string("hashSyncCheckpoint"), hashCheckpoint);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
|
||||
{
|
||||
return Write(string("hashSyncCheckpoint"), hashCheckpoint);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadCheckpointPubKey(string& strPubKey)
|
||||
{
|
||||
return Read(string("strCheckpointPubKey"), strPubKey);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
|
||||
{
|
||||
return Write(string("strCheckpointPubKey"), strPubKey);
|
||||
return std::unique_ptr<CTxDBIteratorBase>(
|
||||
new CLevelDBIterator(pdb->NewIterator(leveldb::ReadOptions())));
|
||||
}
|
||||
|
||||
static CBlockIndex *InsertBlockIndex(uint256 hash)
|
||||
@@ -348,12 +293,10 @@ static CBlockIndex *InsertBlockIndex(uint256 hash)
|
||||
if (hash == 0)
|
||||
return NULL;
|
||||
|
||||
// Return existing
|
||||
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
|
||||
if (mi != mapBlockIndex.end())
|
||||
return (*mi).second;
|
||||
|
||||
// Create new
|
||||
CBlockIndex* pindexNew = new CBlockIndex();
|
||||
if (!pindexNew)
|
||||
throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
|
||||
@@ -366,8 +309,7 @@ static CBlockIndex *InsertBlockIndex(uint256 hash)
|
||||
bool CTxDB::LoadBlockIndex()
|
||||
{
|
||||
if (mapBlockIndex.size() > 0) {
|
||||
// Already loaded once in this session. It can happen during migration
|
||||
// from BDB.
|
||||
// Already loaded once in this session. Can happen during BDB migration.
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -377,39 +319,32 @@ bool CTxDB::LoadBlockIndex()
|
||||
CDiskBlockIndex::fSerializeChainTrust = (nDbFormat >= 2);
|
||||
|
||||
if (CDiskBlockIndex::fSerializeChainTrust)
|
||||
printf("LoadBlockIndex(): DB format v%d — nChainTrust persisted\n", nDbFormat);
|
||||
printf("LoadBlockIndex(): DB format v%d - nChainTrust persisted\n", nDbFormat);
|
||||
else
|
||||
printf("LoadBlockIndex(): DB format v%d — will recalculate nChainTrust (one-time upgrade)\n", nDbFormat);
|
||||
printf("LoadBlockIndex(): DB format v%d - will recalculate nChainTrust (one-time upgrade)\n", nDbFormat);
|
||||
|
||||
// The block index is an in-memory structure that maps hashes to on-disk
|
||||
// locations where the contents of the block can be found. Here, we scan it
|
||||
// out of the DB and into mapBlockIndex.
|
||||
// Scan the block index out of the DB into mapBlockIndex.
|
||||
int64_t nPhaseStart = GetTimeMillis();
|
||||
int64_t nTotalStart = nPhaseStart;
|
||||
leveldb::Iterator *iterator = pdb->NewIterator(leveldb::ReadOptions());
|
||||
// Seek to start key.
|
||||
CDataStream ssStartKey(SER_DISK, CLIENT_VERSION);
|
||||
ssStartKey << make_pair(string("blockindex"), uint256(0));
|
||||
iterator->Seek(ssStartKey.str());
|
||||
// Now read each entry.
|
||||
int nBlocksLoaded = 0;
|
||||
while (iterator->Valid())
|
||||
{
|
||||
// Report progress every 100k blocks
|
||||
if (++nBlocksLoaded % 100000 == 0)
|
||||
{
|
||||
std::string strMsg = strprintf(_("Loading block index... (%d blocks)"), nBlocksLoaded);
|
||||
uiInterface.InitMessage(strMsg);
|
||||
}
|
||||
|
||||
// Unpack keys and values.
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.write(iterator->key().data(), iterator->key().size());
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue.write(iterator->value().data(), iterator->value().size());
|
||||
string strType;
|
||||
ssKey >> strType;
|
||||
// Did we reach the end of the data to read?
|
||||
if (fRequestShutdown || strType != "blockindex")
|
||||
break;
|
||||
CDiskBlockIndex diskindex;
|
||||
@@ -417,7 +352,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
|
||||
uint256 blockHash = diskindex.GetBlockHash();
|
||||
|
||||
// Construct block index object
|
||||
CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
|
||||
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
|
||||
pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
|
||||
@@ -436,10 +370,8 @@ bool CTxDB::LoadBlockIndex()
|
||||
pindexNew->nTime = diskindex.nTime;
|
||||
pindexNew->nBits = diskindex.nBits;
|
||||
pindexNew->nNonce = diskindex.nNonce;
|
||||
// nChainTrust is populated from disk if fSerializeChainTrust, else stays 0
|
||||
pindexNew->nChainTrust = diskindex.nChainTrust;
|
||||
|
||||
// Watch for genesis block
|
||||
if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlockOfficial : hashGenesisBlockTestNet))
|
||||
pindexGenesisBlock = pindexNew;
|
||||
|
||||
@@ -448,8 +380,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
|
||||
}
|
||||
|
||||
// setStakeSeen is populated below for recent blocks only (Change D)
|
||||
|
||||
iterator->Next();
|
||||
}
|
||||
delete iterator;
|
||||
@@ -513,7 +443,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
ssValue << diskindex;
|
||||
batch.Put(ssKey.str(), ssValue.str());
|
||||
|
||||
// Flush in chunks to limit memory usage
|
||||
if (++nCount % 100000 == 0)
|
||||
{
|
||||
pdb->Write(leveldb::WriteOptions(), &batch);
|
||||
@@ -521,7 +450,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
printf("LoadBlockIndex(): upgraded %d / %d block index entries\n", nCount, (int)vSortedByHeight.size());
|
||||
}
|
||||
}
|
||||
// Write remaining entries + format version
|
||||
CDataStream ssFmtKey(SER_DISK, CLIENT_VERSION);
|
||||
ssFmtKey << string("dbformat");
|
||||
CDataStream ssFmtValue(SER_DISK, CLIENT_VERSION);
|
||||
@@ -536,8 +464,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
}
|
||||
else
|
||||
{
|
||||
// nChainTrust was loaded from disk. Only need stake modifier checksums
|
||||
// for blocks above the last checkpoint (typically very few or zero).
|
||||
int nLastCheckpointHeight = Checkpoints::GetTotalBlocksEstimate();
|
||||
bool fNeedModifierCheck = false;
|
||||
for (const auto& item : mapBlockIndex)
|
||||
@@ -569,16 +495,12 @@ bool CTxDB::LoadBlockIndex()
|
||||
|
||||
printf("STARTUP-PERF: chain_trust_and_modifiers %" PRId64 "ms\n", GetTimeMillis() - nPhaseStart);
|
||||
|
||||
// Bump dbformat to 3 if needed (databases that already had v2 nChainTrust upgrade).
|
||||
// UTXO entries are written by ConnectBlock during normal sync. For databases upgrading
|
||||
// from older versions, FetchInputs has a lazy fallback to the old CTxIndex path.
|
||||
if (nDbFormat < 3)
|
||||
{
|
||||
WriteDbFormat(3);
|
||||
printf("LoadBlockIndex(): bumped dbformat to v3 (UTXO model with lazy fallback)\n");
|
||||
}
|
||||
|
||||
// Load hashBestChain pointer to end of best chain
|
||||
nPhaseStart = GetTimeMillis();
|
||||
if (!ReadHashBestChain(hashBestChain))
|
||||
{
|
||||
@@ -594,7 +516,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
|
||||
printf("STARTUP-PERF: best_chain %" PRId64 "ms\n", GetTimeMillis() - nPhaseStart);
|
||||
|
||||
// ---- setStakeSeen: only populate for recent blocks (DoS protection) ----
|
||||
nPhaseStart = GetTimeMillis();
|
||||
{
|
||||
int nStakeSeenDepth = 500;
|
||||
@@ -617,7 +538,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
|
||||
|
||||
// Re-evaluate best chain: scan for competing tips with equal or greater trust.
|
||||
// This fixes nodes stuck on the wrong fork after consensus rule changes.
|
||||
{
|
||||
CBlockIndex* pindexBetter = NULL;
|
||||
for (const auto& item : mapBlockIndex)
|
||||
@@ -660,29 +580,25 @@ bool CTxDB::LoadBlockIndex()
|
||||
}
|
||||
}
|
||||
|
||||
// triangles: load hashSyncCheckpoint (best-effort, non-fatal)
|
||||
if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
|
||||
printf("LoadBlockIndex(): no sync checkpoint in DB, using default\n");
|
||||
else
|
||||
printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
|
||||
// If the stored checkpoint isn't in our index, reset to genesis so we don't assert-crash
|
||||
if (!mapBlockIndex.count(Checkpoints::hashSyncCheckpoint))
|
||||
{
|
||||
printf("LoadBlockIndex(): sync checkpoint not in index, resetting to genesis\n");
|
||||
Checkpoints::hashSyncCheckpoint = (!fTestNet ? hashGenesisBlockOfficial : hashGenesisBlockTestNet);
|
||||
}
|
||||
|
||||
// Load bnBestInvalidTrust, OK if it doesn't exist
|
||||
CBigNum bnBestInvalidTrust;
|
||||
ReadBestInvalidTrust(bnBestInvalidTrust);
|
||||
nBestInvalidTrust = bnBestInvalidTrust.getuint256();
|
||||
|
||||
// Verify blocks in the best chain
|
||||
nPhaseStart = GetTimeMillis();
|
||||
int nCheckLevel = GetArg("-checklevel", 1);
|
||||
int nCheckDepth = GetArg( "-checkblocks", 50);
|
||||
if (nCheckDepth == 0)
|
||||
nCheckDepth = 1000000000; // suffices until the year 19000
|
||||
nCheckDepth = 1000000000;
|
||||
if (nCheckDepth > nBestHeight)
|
||||
nCheckDepth = nBestHeight;
|
||||
printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
|
||||
@@ -695,14 +611,11 @@ bool CTxDB::LoadBlockIndex()
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(pindex))
|
||||
return error("LoadBlockIndex() : block.ReadFromDisk failed");
|
||||
// check level 1: verify block validity
|
||||
// check level 7: verify block signature too
|
||||
if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6)))
|
||||
{
|
||||
printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
// check level 2: verify transaction index validity
|
||||
if (nCheckLevel>1)
|
||||
{
|
||||
pair<unsigned int, unsigned int> pos = make_pair(pindex->nFile, pindex->nBlockPos);
|
||||
@@ -713,10 +626,8 @@ bool CTxDB::LoadBlockIndex()
|
||||
CTxIndex txindex;
|
||||
if (ReadTxIndex(hashTx, txindex))
|
||||
{
|
||||
// check level 3: checker transaction hashes
|
||||
if (nCheckLevel>2 || pindex->nFile != txindex.pos.nFile || pindex->nBlockPos != txindex.pos.nBlockPos)
|
||||
{
|
||||
// either an error or a duplicate transaction
|
||||
CTransaction txFound;
|
||||
if (!txFound.ReadFromDisk(txindex.pos))
|
||||
{
|
||||
@@ -724,13 +635,12 @@ bool CTxDB::LoadBlockIndex()
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
else
|
||||
if (txFound.GetHash() != hashTx) // not a duplicate tx
|
||||
if (txFound.GetHash() != hashTx)
|
||||
{
|
||||
printf("LoadBlockIndex(): *** invalid tx position for %s\n", hashTx.ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
}
|
||||
// check level 4: verify spent inputs were removed from UTXO set
|
||||
if (nCheckLevel>3 && !tx.IsCoinBase())
|
||||
{
|
||||
for (const CTxIn &txin : tx.vin)
|
||||
@@ -749,7 +659,6 @@ bool CTxDB::LoadBlockIndex()
|
||||
}
|
||||
if (pindexFork && !fRequestShutdown)
|
||||
{
|
||||
// Reorg back to the fork
|
||||
printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(pindexFork))
|
||||
@@ -762,271 +671,3 @@ bool CTxDB::LoadBlockIndex()
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Address index methods
|
||||
// ============================================================================
|
||||
|
||||
bool CTxDB::ReadAddressBalance(int nType, const uint160& hashBytes, int64_t& nBalance)
|
||||
{
|
||||
return Read(make_pair(string("addrbal"), CAddressBalanceKey(nType, hashBytes)), nBalance);
|
||||
}
|
||||
|
||||
bool CTxDB::WriteAddressBalance(int nType, const uint160& hashBytes, int64_t nBalance)
|
||||
{
|
||||
return Write(make_pair(string("addrbal"), CAddressBalanceKey(nType, hashBytes)), nBalance);
|
||||
}
|
||||
|
||||
bool CTxDB::ReadAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex, int64_t& nValue, int& nHeight)
|
||||
{
|
||||
CAddressUtxoValue val;
|
||||
if (!Read(make_pair(string("addrutxo"), CAddressUtxoKey(nType, hashBytes, txhash, nIndex)), val))
|
||||
return false;
|
||||
nValue = val.nValue;
|
||||
nHeight = val.nHeight;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDB::WriteAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex, int64_t nValue, int nHeight, const CScript& script)
|
||||
{
|
||||
return Write(make_pair(string("addrutxo"), CAddressUtxoKey(nType, hashBytes, txhash, nIndex)),
|
||||
CAddressUtxoValue(nValue, nHeight, script));
|
||||
}
|
||||
|
||||
bool CTxDB::EraseAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex)
|
||||
{
|
||||
return Erase(make_pair(string("addrutxo"), CAddressUtxoKey(nType, hashBytes, txhash, nIndex)));
|
||||
}
|
||||
|
||||
bool CTxDB::WriteAddressTxId(int nType, const uint160& hashBytes, int nHeight, int nTxIndex, const uint256& txhash)
|
||||
{
|
||||
return Write(make_pair(string("addrtxid"), CAddressTxIdKey(nType, hashBytes, nHeight, nTxIndex, txhash)), (char)0);
|
||||
}
|
||||
|
||||
bool CTxDB::EraseAddressTxId(int nType, const uint160& hashBytes, int nHeight, int nTxIndex, const uint256& txhash)
|
||||
{
|
||||
return Erase(make_pair(string("addrtxid"), CAddressTxIdKey(nType, hashBytes, nHeight, nTxIndex, txhash)));
|
||||
}
|
||||
|
||||
bool CTxDB::GetAddressUtxos(int nType, const uint160& hashBytes, std::vector<std::pair<COutPoint, std::pair<int64_t, int> > >& vUtxos)
|
||||
{
|
||||
vUtxos.clear();
|
||||
|
||||
// Build the key prefix to seek to
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("addrutxo"), CAddressUtxoKey(nType, hashBytes, uint256(0), 0));
|
||||
std::string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
leveldb::Iterator* it = pdb->NewIterator(leveldb::ReadOptions());
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
// Deserialize the key
|
||||
CDataStream ssKey(it->key().data(), it->key().data() + it->key().size(), SER_DISK, CLIENT_VERSION);
|
||||
std::string strKeyType;
|
||||
CAddressUtxoKey utxoKey;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "addrutxo")
|
||||
break;
|
||||
ssKey >> utxoKey;
|
||||
if (utxoKey.nType != nType || utxoKey.hashBytes != hashBytes)
|
||||
break;
|
||||
|
||||
// Deserialize the value
|
||||
CDataStream ssValue(it->value().data(), it->value().data() + it->value().size(), SER_DISK, CLIENT_VERSION);
|
||||
CAddressUtxoValue utxoValue;
|
||||
ssValue >> utxoValue;
|
||||
|
||||
COutPoint outpoint(utxoKey.txhash, utxoKey.nIndex);
|
||||
vUtxos.push_back(make_pair(outpoint, make_pair(utxoValue.nValue, utxoValue.nHeight)));
|
||||
}
|
||||
delete it;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTxDB::GetAddressTxIds(int nType, const uint160& hashBytes, int nStartHeight, int nEndHeight, std::vector<uint256>& vTxIds)
|
||||
{
|
||||
vTxIds.clear();
|
||||
|
||||
// Build the key prefix to seek to
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("addrtxid"), CAddressTxIdKey(nType, hashBytes, nStartHeight, 0, uint256(0)));
|
||||
std::string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
leveldb::Iterator* it = pdb->NewIterator(leveldb::ReadOptions());
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
CDataStream ssKey(it->key().data(), it->key().data() + it->key().size(), SER_DISK, CLIENT_VERSION);
|
||||
std::string strKeyType;
|
||||
CAddressTxIdKey txIdKey;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "addrtxid")
|
||||
break;
|
||||
ssKey >> txIdKey;
|
||||
if (txIdKey.nType != nType || txIdKey.hashBytes != hashBytes)
|
||||
break;
|
||||
if (txIdKey.nHeight > nEndHeight)
|
||||
break;
|
||||
|
||||
vTxIds.push_back(txIdKey.txhash);
|
||||
}
|
||||
delete it;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------- In-memory UTXO cache ----------
|
||||
//
|
||||
// Read-through cache that avoids hitting LevelDB for every FetchInputs call.
|
||||
// On a 2M+ block chain with millions of UTXOs, this dramatically reduces I/O
|
||||
// during both IBD (ConnectBlock validation reads inputs) and normal operation
|
||||
// (mempool acceptance, staking). Writes/erases update both cache and LevelDB.
|
||||
|
||||
struct COutPointHasher {
|
||||
size_t operator()(const COutPoint& op) const {
|
||||
// Mix the lower 64 bits of the hash with the output index
|
||||
return op.hash.Get64() ^ (std::hash<unsigned int>()(op.n) * 0x9e3779b97f4a7c15ULL);
|
||||
}
|
||||
};
|
||||
|
||||
// Cache entry: the UTXO data plus a flag indicating "known absent from DB"
|
||||
struct CUtxoCacheEntry {
|
||||
CUtxoEntry utxo;
|
||||
bool fPresent; // true = UTXO exists, false = known deleted/absent
|
||||
CUtxoCacheEntry() : fPresent(false) {}
|
||||
CUtxoCacheEntry(const CUtxoEntry& u, bool p) : utxo(u), fPresent(p) {}
|
||||
};
|
||||
|
||||
static std::unordered_map<COutPoint, CUtxoCacheEntry, COutPointHasher> mapUtxoCache;
|
||||
static CCriticalSection cs_utxoCache;
|
||||
static const size_t UTXO_CACHE_MAX_ENTRIES = 2000000; // ~400MB at ~200 bytes each
|
||||
|
||||
// ---------- UTXO database methods ----------
|
||||
|
||||
bool CTxDB::ReadUtxo(const uint256& hash, unsigned int n, CUtxoEntry& entry)
|
||||
{
|
||||
entry.SetNull();
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(cs_utxoCache);
|
||||
auto it = mapUtxoCache.find(outpoint);
|
||||
if (it != mapUtxoCache.end())
|
||||
{
|
||||
if (it->second.fPresent) {
|
||||
entry = it->second.utxo;
|
||||
return true;
|
||||
}
|
||||
return false; // cached as absent
|
||||
}
|
||||
}
|
||||
|
||||
// Cache miss — read from LevelDB
|
||||
bool fFound = Read(make_pair(string("u"), make_pair(hash, n)), entry);
|
||||
|
||||
{
|
||||
LOCK(cs_utxoCache);
|
||||
// Only cache if under limit (don't evict here — eviction is periodic)
|
||||
if (mapUtxoCache.size() < UTXO_CACHE_MAX_ENTRIES)
|
||||
{
|
||||
if (fFound)
|
||||
mapUtxoCache[outpoint] = CUtxoCacheEntry(entry, true);
|
||||
else
|
||||
mapUtxoCache[outpoint] = CUtxoCacheEntry(CUtxoEntry(), false);
|
||||
}
|
||||
}
|
||||
|
||||
return fFound;
|
||||
}
|
||||
|
||||
bool CTxDB::WriteUtxo(const uint256& hash, unsigned int n, const CUtxoEntry& entry)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(cs_utxoCache);
|
||||
mapUtxoCache[outpoint] = CUtxoCacheEntry(entry, true);
|
||||
|
||||
// Periodic eviction: if cache is over limit, clear half of it.
|
||||
// This is a simple but effective strategy — the cache will quickly
|
||||
// repopulate with the hot working set.
|
||||
if (mapUtxoCache.size() > UTXO_CACHE_MAX_ENTRIES)
|
||||
{
|
||||
size_t nTarget = UTXO_CACHE_MAX_ENTRIES / 2;
|
||||
auto it = mapUtxoCache.begin();
|
||||
while (mapUtxoCache.size() > nTarget && it != mapUtxoCache.end())
|
||||
it = mapUtxoCache.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
return Write(make_pair(string("u"), make_pair(hash, n)), entry);
|
||||
}
|
||||
|
||||
bool CTxDB::EraseUtxo(const uint256& hash, unsigned int n)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(cs_utxoCache);
|
||||
// Mark as absent in cache (negative cache) so future reads don't hit DB
|
||||
mapUtxoCache[outpoint] = CUtxoCacheEntry(CUtxoEntry(), false);
|
||||
}
|
||||
|
||||
return Erase(make_pair(string("u"), make_pair(hash, n)));
|
||||
}
|
||||
|
||||
bool CTxDB::HaveUtxo(const uint256& hash, unsigned int n)
|
||||
{
|
||||
COutPoint outpoint(hash, n);
|
||||
|
||||
{
|
||||
LOCK(cs_utxoCache);
|
||||
auto it = mapUtxoCache.find(outpoint);
|
||||
if (it != mapUtxoCache.end())
|
||||
return it->second.fPresent;
|
||||
}
|
||||
|
||||
if (Exists(make_pair(string("u"), make_pair(hash, n))))
|
||||
return true;
|
||||
|
||||
// Lazy fallback: check old CTxIndex vSpent for databases upgrading from pre-UTXO format
|
||||
CTxIndex txindex;
|
||||
if (ReadTxIndex(hash, txindex))
|
||||
{
|
||||
if (n < txindex.vSpent.size() && txindex.vSpent[n].IsNull())
|
||||
return true; // vSpent[n] is null = output NOT spent = UTXO exists
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t CTxDB::SumUtxoValues(int& nCount)
|
||||
{
|
||||
nCount = 0;
|
||||
int64_t nTotal = 0;
|
||||
|
||||
// Seek to the start of UTXO entries (key prefix "u")
|
||||
CDataStream ssKeyPrefix(SER_DISK, CLIENT_VERSION);
|
||||
ssKeyPrefix << make_pair(string("u"), make_pair(uint256(0), (unsigned int)0));
|
||||
std::string strPrefixBegin = ssKeyPrefix.str();
|
||||
|
||||
leveldb::Iterator* it = pdb->NewIterator(leveldb::ReadOptions());
|
||||
for (it->Seek(strPrefixBegin); it->Valid(); it->Next())
|
||||
{
|
||||
// Check key prefix is still "u"
|
||||
CDataStream ssKey(it->key().data(), it->key().data() + it->key().size(), SER_DISK, CLIENT_VERSION);
|
||||
std::string strKeyType;
|
||||
ssKey >> strKeyType;
|
||||
if (strKeyType != "u")
|
||||
break;
|
||||
|
||||
// Deserialize the UTXO entry and sum the value
|
||||
CDataStream ssValue(it->value().data(), it->value().data() + it->value().size(), SER_DISK, CLIENT_VERSION);
|
||||
CUtxoEntry entry;
|
||||
ssValue >> entry;
|
||||
|
||||
nTotal += entry.nValue;
|
||||
nCount++;
|
||||
}
|
||||
delete it;
|
||||
return nTotal;
|
||||
}
|
||||
|
||||
|
||||
+33
-213
@@ -6,241 +6,61 @@
|
||||
#ifndef TRIANGLES_LEVELDB_H
|
||||
#define TRIANGLES_LEVELDB_H
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "txdb-base.h"
|
||||
|
||||
#include <leveldb/db.h>
|
||||
#include <leveldb/write_batch.h>
|
||||
|
||||
// Class that provides access to a LevelDB. Note that this class is frequently
|
||||
// instantiated on the stack and then destroyed again, so instantiation has to
|
||||
// be very cheap. Unfortunately that means, a CTxDB instance is actually just a
|
||||
// wrapper around some global state.
|
||||
// LevelDB backend for the chain database.
|
||||
//
|
||||
// A LevelDB is a key/value store that is optimized for fast usage on hard
|
||||
// disks. It prefers long read/writes to seeks and is based on a series of
|
||||
// sorted key/value mapping files that are stacked on top of each other, with
|
||||
// newer files overriding older files. A background thread compacts them
|
||||
// together when too many files stack up.
|
||||
// Cheap to construct/destruct: every instance shares a single global
|
||||
// leveldb::DB pointer, opened lazily on first use. Most of the codebase
|
||||
// instantiates a CTxDB on the stack for short-lived operations.
|
||||
//
|
||||
// Learn more: http://code.google.com/p/leveldb/
|
||||
class CTxDB
|
||||
// The protected templated Read/Write/Erase/Exists live in CTxDBBase and
|
||||
// dispatch to ReadRaw/WriteRaw/EraseRaw/ExistsRaw below, which handle the
|
||||
// active-batch logic so reads-after-writes within an open batch see their
|
||||
// own pending changes.
|
||||
class CTxDB final : public CTxDBBase
|
||||
{
|
||||
public:
|
||||
CTxDB(const char* pszMode="r+");
|
||||
~CTxDB() {
|
||||
// Note that this is not the same as Close() because it deletes only
|
||||
// data scoped to this TxDB object.
|
||||
CTxDB(const char* pszMode = "r+");
|
||||
~CTxDB() override {
|
||||
delete activeBatch;
|
||||
}
|
||||
|
||||
// Destroys the underlying shared global state accessed by this TxDB.
|
||||
void Close();
|
||||
void Close() override;
|
||||
|
||||
private:
|
||||
leveldb::DB *pdb; // Points to the global instance.
|
||||
|
||||
// A batch stores up writes and deletes for atomic application. When this
|
||||
// field is non-NULL, writes/deletes go there instead of directly to disk.
|
||||
leveldb::WriteBatch *activeBatch;
|
||||
leveldb::Options options;
|
||||
bool fReadOnly;
|
||||
int nVersion;
|
||||
|
||||
protected:
|
||||
// Returns true and sets (value,false) if activeBatch contains the given key
|
||||
// or leaves value alone and sets deleted = true if activeBatch contains a
|
||||
// delete for it.
|
||||
bool ScanBatch(const CDataStream &key, std::string *value, bool *deleted) const;
|
||||
|
||||
template<typename K, typename T>
|
||||
bool Read(const K& key, T& value)
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
std::string strValue;
|
||||
|
||||
bool readFromDb = true;
|
||||
if (activeBatch) {
|
||||
// First we must search for it in the currently pending set of
|
||||
// changes to the db. If not found in the batch, go on to read disk.
|
||||
bool deleted = false;
|
||||
readFromDb = ScanBatch(ssKey, &strValue, &deleted) == false;
|
||||
if (deleted) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (readFromDb) {
|
||||
leveldb::Status status = pdb->Get(leveldb::ReadOptions(),
|
||||
ssKey.str(), &strValue);
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
// Some unexpected error.
|
||||
printf("LevelDB read failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Unserialize value
|
||||
try {
|
||||
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
|
||||
SER_DISK, CLIENT_VERSION);
|
||||
ssValue >> value;
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
bool Write(const K& key, const T& value)
|
||||
{
|
||||
if (fReadOnly)
|
||||
assert(!"Write called on database in read-only mode");
|
||||
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue.reserve(10000);
|
||||
ssValue << value;
|
||||
|
||||
if (activeBatch) {
|
||||
activeBatch->Put(ssKey.str(), ssValue.str());
|
||||
return true;
|
||||
}
|
||||
leveldb::Status status = pdb->Put(leveldb::WriteOptions(), ssKey.str(), ssValue.str());
|
||||
if (!status.ok()) {
|
||||
printf("LevelDB write failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename K>
|
||||
bool Erase(const K& key)
|
||||
{
|
||||
if (!pdb)
|
||||
return false;
|
||||
if (fReadOnly)
|
||||
assert(!"Erase called on database in read-only mode");
|
||||
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
if (activeBatch) {
|
||||
activeBatch->Delete(ssKey.str());
|
||||
return true;
|
||||
}
|
||||
leveldb::Status status = pdb->Delete(leveldb::WriteOptions(), ssKey.str());
|
||||
return (status.ok() || status.IsNotFound());
|
||||
}
|
||||
|
||||
template<typename K>
|
||||
bool Exists(const K& key)
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(1000);
|
||||
ssKey << key;
|
||||
std::string unused;
|
||||
|
||||
if (activeBatch) {
|
||||
bool deleted;
|
||||
if (ScanBatch(ssKey, &unused, &deleted) && !deleted) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
leveldb::Status status = pdb->Get(leveldb::ReadOptions(), ssKey.str(), &unused);
|
||||
return status.IsNotFound() == false;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
bool TxnBegin();
|
||||
bool TxnCommit();
|
||||
bool TxnAbort()
|
||||
bool TxnBegin() override;
|
||||
bool TxnCommit() override;
|
||||
bool TxnAbort() override
|
||||
{
|
||||
delete activeBatch;
|
||||
activeBatch = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadVersion(int& nVersion)
|
||||
{
|
||||
nVersion = 0;
|
||||
return Read(std::string("version"), nVersion);
|
||||
}
|
||||
bool LoadBlockIndex() override;
|
||||
|
||||
bool WriteVersion(int nVersion)
|
||||
{
|
||||
return Write(std::string("version"), nVersion);
|
||||
}
|
||||
|
||||
bool ReadDbFormat(int& nDbFormat)
|
||||
{
|
||||
nDbFormat = 1;
|
||||
return Read(std::string("dbformat"), nDbFormat);
|
||||
}
|
||||
|
||||
bool WriteDbFormat(int nDbFormat)
|
||||
{
|
||||
return Write(std::string("dbformat"), nDbFormat);
|
||||
}
|
||||
|
||||
bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
|
||||
bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
|
||||
bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
|
||||
bool EraseTxIndex(const CTransaction& tx);
|
||||
bool ContainsTx(uint256 hash);
|
||||
bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
|
||||
bool ReadDiskTx(uint256 hash, CTransaction& tx);
|
||||
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
|
||||
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
|
||||
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
|
||||
bool ReadHashBestChain(uint256& hashBestChain);
|
||||
bool WriteHashBestChain(uint256 hashBestChain);
|
||||
bool ReadAddressIndexBestChain(uint256& hashBestChain);
|
||||
bool WriteAddressIndexBestChain(uint256 hashBestChain);
|
||||
bool ReadAddressIndexStartHeight(int& nHeight);
|
||||
bool WriteAddressIndexStartHeight(int nHeight);
|
||||
bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
|
||||
bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
|
||||
bool ReadSyncCheckpoint(uint256& hashCheckpoint);
|
||||
bool WriteSyncCheckpoint(uint256 hashCheckpoint);
|
||||
bool ReadCheckpointPubKey(std::string& strPubKey);
|
||||
bool WriteCheckpointPubKey(const std::string& strPubKey);
|
||||
bool LoadBlockIndex();
|
||||
|
||||
// Address index methods
|
||||
bool ReadAddressBalance(int nType, const uint160& hashBytes, int64_t& nBalance);
|
||||
bool WriteAddressBalance(int nType, const uint160& hashBytes, int64_t nBalance);
|
||||
bool ReadAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex, int64_t& nValue, int& nHeight);
|
||||
bool WriteAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex, int64_t nValue, int nHeight, const CScript& script);
|
||||
bool EraseAddressUtxo(int nType, const uint160& hashBytes, const uint256& txhash, int nIndex);
|
||||
bool WriteAddressTxId(int nType, const uint160& hashBytes, int nHeight, int nTxIndex, const uint256& txhash);
|
||||
bool EraseAddressTxId(int nType, const uint160& hashBytes, int nHeight, int nTxIndex, const uint256& txhash);
|
||||
|
||||
// Address index iteration (for RPC queries)
|
||||
bool GetAddressUtxos(int nType, const uint160& hashBytes, std::vector<std::pair<COutPoint, std::pair<int64_t, int> > >& vUtxos);
|
||||
bool GetAddressTxIds(int nType, const uint160& hashBytes, int nStartHeight, int nEndHeight, std::vector<uint256>& vTxIds);
|
||||
|
||||
// UTXO database methods
|
||||
bool ReadUtxo(const uint256& hash, unsigned int n, CUtxoEntry& entry);
|
||||
bool WriteUtxo(const uint256& hash, unsigned int n, const CUtxoEntry& entry);
|
||||
bool EraseUtxo(const uint256& hash, unsigned int n);
|
||||
bool HaveUtxo(const uint256& hash, unsigned int n);
|
||||
int64_t SumUtxoValues(int& nCount);
|
||||
protected:
|
||||
bool ReadRaw(const std::string& key, std::string& value) const override;
|
||||
bool WriteRaw(const std::string& key, const std::string& value) override;
|
||||
bool EraseRaw(const std::string& key) override;
|
||||
bool ExistsRaw(const std::string& key) const override;
|
||||
std::unique_ptr<CTxDBIteratorBase> NewIterator() const override;
|
||||
|
||||
private:
|
||||
leveldb::DB* pdb; // Points to the global instance.
|
||||
leveldb::WriteBatch* activeBatch; // When non-NULL, writes/deletes go here.
|
||||
leveldb::Options options;
|
||||
int nVersion;
|
||||
|
||||
// Returns true and sets (value,false) if activeBatch contains the given
|
||||
// key, or leaves value alone and sets deleted=true if activeBatch contains
|
||||
// a delete for it.
|
||||
bool ScanBatch(const std::string& key, std::string* value, bool* deleted) const;
|
||||
|
||||
bool LoadBlockIndexGuts();
|
||||
};
|
||||
|
||||
|
||||
#endif // TRIANGLES_LEVELDB_H
|
||||
|
||||
@@ -0,0 +1,710 @@
|
||||
// Copyright (c) 2026 The Triangles developers.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "txdb-rocksdb.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#include <rocksdb/cache.h>
|
||||
#include <rocksdb/filter_policy.h>
|
||||
#include <rocksdb/iterator.h>
|
||||
#include <rocksdb/slice.h>
|
||||
#include <rocksdb/table.h>
|
||||
#include <rocksdb/write_batch.h>
|
||||
|
||||
#include "kernel.h"
|
||||
#include "checkpoints.h"
|
||||
#include "txdb.h"
|
||||
#include "util.h"
|
||||
#include "ui_interface.h"
|
||||
#include "addressindex.h"
|
||||
#include "main.h"
|
||||
|
||||
using namespace std;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// Global pointer for the RocksDB instance, shared across CRocksTxDB instances
|
||||
// the same way the LevelDB backend shares its txdb singleton.
|
||||
static rocksdb::DB* g_rocksdb = nullptr;
|
||||
|
||||
namespace {
|
||||
|
||||
// rocksdb::DB::Open shipped a raw DB** overload for years; newer releases
|
||||
// (Homebrew's macOS rocksdb 10.x) replaced it with std::unique_ptr<DB>*.
|
||||
// SFINAE picks whichever overload the linked rocksdb actually has —
|
||||
// `int` is preferred over `long`, so when DB** exists, the first overload
|
||||
// wins; otherwise the unique_ptr fallback runs.
|
||||
template<typename T>
|
||||
inline auto OpenRocksDBImpl(const rocksdb::Options& opts, const std::string& path,
|
||||
T** dbptr, int)
|
||||
-> decltype(rocksdb::DB::Open(opts, path, dbptr))
|
||||
{
|
||||
return rocksdb::DB::Open(opts, path, dbptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline rocksdb::Status OpenRocksDBImpl(const rocksdb::Options& opts, const std::string& path,
|
||||
T** dbptr, long)
|
||||
{
|
||||
std::unique_ptr<T> tmp;
|
||||
auto s = rocksdb::DB::Open(opts, path, &tmp);
|
||||
if (s.ok()) *dbptr = tmp.release();
|
||||
return s;
|
||||
}
|
||||
|
||||
inline rocksdb::Status OpenRocksDB(const rocksdb::Options& opts,
|
||||
const std::string& path,
|
||||
rocksdb::DB** dbptr)
|
||||
{
|
||||
return OpenRocksDBImpl(opts, path, dbptr, 0);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
static rocksdb::Options GetRocksOptions()
|
||||
{
|
||||
rocksdb::Options opts;
|
||||
opts.create_if_missing = false;
|
||||
opts.compression = rocksdb::kSnappyCompression;
|
||||
opts.max_open_files = 1000;
|
||||
opts.write_buffer_size = 64 * 1048576;
|
||||
opts.IncreaseParallelism(); // Multi-threaded compaction.
|
||||
opts.OptimizeLevelStyleCompaction(); // Sensible defaults for a LSM workload.
|
||||
|
||||
rocksdb::BlockBasedTableOptions table_opts;
|
||||
int nCacheSizeMB = GetArg("-dbcache", 2048);
|
||||
table_opts.block_cache = rocksdb::NewLRUCache(static_cast<size_t>(nCacheSizeMB) * 1048576);
|
||||
table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
||||
opts.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_opts));
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
static void open_rocksdb(rocksdb::Options& options, bool fRemoveOld = false)
|
||||
{
|
||||
fs::path directory = GetDataDir() / "rocksdb";
|
||||
|
||||
if (fRemoveOld) {
|
||||
fs::remove_all(directory);
|
||||
}
|
||||
|
||||
fs::create_directory(directory);
|
||||
printf("Opening RocksDB in %s\n", directory.string().c_str());
|
||||
rocksdb::Status status = OpenRocksDB(options, directory.string(), &g_rocksdb);
|
||||
if (!status.ok()) {
|
||||
throw runtime_error(strprintf("open_rocksdb(): error opening database: %s",
|
||||
status.ToString().c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
CRocksTxDB::CRocksTxDB(const char* pszMode)
|
||||
: pdb(nullptr), activeBatch(nullptr), nVersion(0)
|
||||
{
|
||||
assert(pszMode);
|
||||
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
|
||||
|
||||
if (g_rocksdb) {
|
||||
pdb = g_rocksdb;
|
||||
return;
|
||||
}
|
||||
|
||||
bool fCreate = strchr(pszMode, 'c');
|
||||
options = GetRocksOptions();
|
||||
options.create_if_missing = fCreate;
|
||||
|
||||
open_rocksdb(options);
|
||||
pdb = g_rocksdb;
|
||||
|
||||
if (Exists(string("version")))
|
||||
{
|
||||
ReadVersion(nVersion);
|
||||
printf("RocksDB transaction index version is %d\n", nVersion);
|
||||
|
||||
if (nVersion < DATABASE_VERSION)
|
||||
{
|
||||
printf("Required index version is %d, removing old RocksDB database\n",
|
||||
DATABASE_VERSION);
|
||||
|
||||
delete g_rocksdb;
|
||||
g_rocksdb = pdb = nullptr;
|
||||
delete activeBatch;
|
||||
activeBatch = nullptr;
|
||||
|
||||
open_rocksdb(options, true);
|
||||
pdb = g_rocksdb;
|
||||
|
||||
bool fTmp = fReadOnly;
|
||||
fReadOnly = false;
|
||||
WriteVersion(DATABASE_VERSION);
|
||||
fReadOnly = fTmp;
|
||||
}
|
||||
}
|
||||
else if (fCreate)
|
||||
{
|
||||
bool fTmp = fReadOnly;
|
||||
fReadOnly = false;
|
||||
WriteVersion(DATABASE_VERSION);
|
||||
fReadOnly = fTmp;
|
||||
}
|
||||
|
||||
printf("Opened RocksDB successfully\n");
|
||||
}
|
||||
|
||||
CRocksTxDB::~CRocksTxDB()
|
||||
{
|
||||
delete activeBatch;
|
||||
}
|
||||
|
||||
void CRocksTxDB::Close()
|
||||
{
|
||||
delete g_rocksdb;
|
||||
g_rocksdb = pdb = nullptr;
|
||||
delete activeBatch;
|
||||
activeBatch = nullptr;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::TxnBegin()
|
||||
{
|
||||
if (activeBatch)
|
||||
return true;
|
||||
activeBatch = new rocksdb::WriteBatch();
|
||||
pendingBatch.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::TxnCommit()
|
||||
{
|
||||
assert(activeBatch);
|
||||
rocksdb::Status status = pdb->Write(rocksdb::WriteOptions(), activeBatch);
|
||||
delete activeBatch;
|
||||
activeBatch = nullptr;
|
||||
pendingBatch.clear();
|
||||
if (!status.ok()) {
|
||||
printf("ERROR: RocksDB batch commit failure: %s\n", status.ToString().c_str());
|
||||
printf("ERROR: This may indicate disk full, corruption, or permissions issue.\n");
|
||||
printf("ERROR: Chain state may be inconsistent - immediate investigation required!\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::TxnAbort()
|
||||
{
|
||||
delete activeBatch;
|
||||
activeBatch = nullptr;
|
||||
pendingBatch.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class CRocksDBIterator final : public CTxDBIteratorBase {
|
||||
public:
|
||||
explicit CRocksDBIterator(rocksdb::Iterator* pit) : pit(pit) {}
|
||||
~CRocksDBIterator() override { delete pit; }
|
||||
|
||||
void Seek(const std::string& key) override { pit->Seek(key); }
|
||||
bool Valid() const override { return pit->Valid(); }
|
||||
void Next() override { pit->Next(); }
|
||||
std::string KeyStr() const override { return pit->key().ToString(); }
|
||||
std::string ValueStr() const override { return pit->value().ToString(); }
|
||||
|
||||
private:
|
||||
rocksdb::Iterator* pit;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool CRocksTxDB::ScanBatch(const std::string& key, std::string* value, bool* deleted) const
|
||||
{
|
||||
assert(activeBatch);
|
||||
*deleted = false;
|
||||
auto it = pendingBatch.find(key);
|
||||
if (it == pendingBatch.end())
|
||||
return false;
|
||||
if (!it->second.has_value()) {
|
||||
*deleted = true;
|
||||
return true;
|
||||
}
|
||||
*value = *it->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::ReadRaw(const std::string& key, std::string& value) const
|
||||
{
|
||||
bool readFromDb = true;
|
||||
if (activeBatch) {
|
||||
bool deleted = false;
|
||||
readFromDb = ScanBatch(key, &value, &deleted) == false;
|
||||
if (deleted)
|
||||
return false;
|
||||
}
|
||||
if (readFromDb) {
|
||||
rocksdb::Status status = pdb->Get(rocksdb::ReadOptions(), key, &value);
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
printf("RocksDB read failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::WriteRaw(const std::string& key, const std::string& value)
|
||||
{
|
||||
if (activeBatch) {
|
||||
activeBatch->Put(key, value);
|
||||
pendingBatch[key] = value;
|
||||
return true;
|
||||
}
|
||||
rocksdb::Status status = pdb->Put(rocksdb::WriteOptions(), key, value);
|
||||
if (!status.ok()) {
|
||||
printf("RocksDB write failure: %s\n", status.ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::EraseRaw(const std::string& key)
|
||||
{
|
||||
if (!pdb)
|
||||
return false;
|
||||
if (activeBatch) {
|
||||
activeBatch->Delete(key);
|
||||
pendingBatch[key] = std::nullopt;
|
||||
return true;
|
||||
}
|
||||
rocksdb::Status status = pdb->Delete(rocksdb::WriteOptions(), key);
|
||||
return (status.ok() || status.IsNotFound());
|
||||
}
|
||||
|
||||
bool CRocksTxDB::ExistsRaw(const std::string& key) const
|
||||
{
|
||||
std::string unused;
|
||||
|
||||
if (activeBatch) {
|
||||
bool deleted = false;
|
||||
if (ScanBatch(key, &unused, &deleted) && !deleted)
|
||||
return true;
|
||||
}
|
||||
|
||||
rocksdb::Status status = pdb->Get(rocksdb::ReadOptions(), key, &unused);
|
||||
return status.IsNotFound() == false;
|
||||
}
|
||||
|
||||
std::unique_ptr<CTxDBIteratorBase> CRocksTxDB::NewIterator() const
|
||||
{
|
||||
return std::unique_ptr<CTxDBIteratorBase>(
|
||||
new CRocksDBIterator(pdb->NewIterator(rocksdb::ReadOptions())));
|
||||
}
|
||||
|
||||
// ─── LoadBlockIndex ─────────────────────────────────────────────────────────
|
||||
// Mirrors CTxDB::LoadBlockIndex with rocksdb:: substitutions. The dbformat
|
||||
// upgrade path is preserved verbatim because a freshly-imported RocksDB may
|
||||
// have been migrated from a v1 LevelDB and still need the chain-trust pass.
|
||||
//
|
||||
// This duplication is acknowledged debt — CTxDBBase will absorb LoadBlockIndex
|
||||
// into the base class in a later phase once the iterator/batch abstractions
|
||||
// have proven stable across both backends.
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
static CBlockIndex *InsertBlockIndexRocks(uint256 hash)
|
||||
{
|
||||
if (hash == 0)
|
||||
return nullptr;
|
||||
|
||||
auto mi = mapBlockIndex.find(hash);
|
||||
if (mi != mapBlockIndex.end())
|
||||
return mi->second;
|
||||
|
||||
CBlockIndex* pindexNew = new CBlockIndex();
|
||||
if (!pindexNew)
|
||||
throw runtime_error("LoadBlockIndex(): new CBlockIndex failed");
|
||||
mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
|
||||
pindexNew->phashBlock = &mi->first;
|
||||
|
||||
return pindexNew;
|
||||
}
|
||||
|
||||
bool CRocksTxDB::LoadBlockIndex()
|
||||
{
|
||||
if (mapBlockIndex.size() > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int nDbFormat = 1;
|
||||
ReadDbFormat(nDbFormat);
|
||||
CDiskBlockIndex::fSerializeChainTrust = (nDbFormat >= 2);
|
||||
|
||||
if (CDiskBlockIndex::fSerializeChainTrust)
|
||||
printf("LoadBlockIndex(): RocksDB format v%d - nChainTrust persisted\n", nDbFormat);
|
||||
else
|
||||
printf("LoadBlockIndex(): RocksDB format v%d - will recalculate nChainTrust\n", nDbFormat);
|
||||
|
||||
int64_t nPhaseStart = GetTimeMillis();
|
||||
int64_t nTotalStart = nPhaseStart;
|
||||
rocksdb::Iterator* iterator = pdb->NewIterator(rocksdb::ReadOptions());
|
||||
CDataStream ssStartKey(SER_DISK, CLIENT_VERSION);
|
||||
ssStartKey << make_pair(string("blockindex"), uint256(0));
|
||||
iterator->Seek(ssStartKey.str());
|
||||
int nBlocksLoaded = 0;
|
||||
while (iterator->Valid())
|
||||
{
|
||||
if (++nBlocksLoaded % 100000 == 0)
|
||||
{
|
||||
std::string strMsg = strprintf(_("Loading block index... (%d blocks)"), nBlocksLoaded);
|
||||
uiInterface.InitMessage(strMsg);
|
||||
}
|
||||
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.write(iterator->key().data(), iterator->key().size());
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue.write(iterator->value().data(), iterator->value().size());
|
||||
string strType;
|
||||
ssKey >> strType;
|
||||
if (fRequestShutdown || strType != "blockindex")
|
||||
break;
|
||||
CDiskBlockIndex diskindex;
|
||||
ssValue >> diskindex;
|
||||
|
||||
uint256 blockHash = diskindex.GetBlockHash();
|
||||
|
||||
CBlockIndex* pindexNew = InsertBlockIndexRocks(blockHash);
|
||||
pindexNew->pprev = InsertBlockIndexRocks(diskindex.hashPrev);
|
||||
pindexNew->pnext = InsertBlockIndexRocks(diskindex.hashNext);
|
||||
pindexNew->nFile = diskindex.nFile;
|
||||
pindexNew->nBlockPos = diskindex.nBlockPos;
|
||||
pindexNew->nHeight = diskindex.nHeight;
|
||||
pindexNew->nMint = diskindex.nMint;
|
||||
pindexNew->nMoneySupply = diskindex.nMoneySupply;
|
||||
pindexNew->nFlags = diskindex.nFlags;
|
||||
pindexNew->nStakeModifier = diskindex.nStakeModifier;
|
||||
pindexNew->prevoutStake = diskindex.prevoutStake;
|
||||
pindexNew->nStakeTime = diskindex.nStakeTime;
|
||||
pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
|
||||
pindexNew->nVersion = diskindex.nVersion;
|
||||
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
|
||||
pindexNew->nTime = diskindex.nTime;
|
||||
pindexNew->nBits = diskindex.nBits;
|
||||
pindexNew->nNonce = diskindex.nNonce;
|
||||
pindexNew->nChainTrust = diskindex.nChainTrust;
|
||||
|
||||
if (pindexGenesisBlock == nullptr && blockHash == (!fTestNet ? hashGenesisBlockOfficial : hashGenesisBlockTestNet))
|
||||
pindexGenesisBlock = pindexNew;
|
||||
|
||||
if (!pindexNew->CheckIndex()) {
|
||||
delete iterator;
|
||||
return error("LoadBlockIndex(): CheckIndex failed at %d", pindexNew->nHeight);
|
||||
}
|
||||
|
||||
iterator->Next();
|
||||
}
|
||||
delete iterator;
|
||||
printf("STARTUP-PERF: block_index_deserialize %" PRId64 "ms blocks=%d\n",
|
||||
GetTimeMillis() - nPhaseStart, nBlocksLoaded);
|
||||
|
||||
if (fRequestShutdown)
|
||||
return true;
|
||||
|
||||
nPhaseStart = GetTimeMillis();
|
||||
bool fNeedChainTrustRecalc = !CDiskBlockIndex::fSerializeChainTrust;
|
||||
|
||||
if (fNeedChainTrustRecalc)
|
||||
{
|
||||
uiInterface.InitMessage(_("Calculating chain trust (one-time upgrade)..."));
|
||||
|
||||
vector<pair<int, CBlockIndex*> > vSortedByHeight;
|
||||
vSortedByHeight.reserve(mapBlockIndex.size());
|
||||
for (const auto& item : mapBlockIndex)
|
||||
vSortedByHeight.push_back(make_pair(item.second->nHeight, item.second));
|
||||
sort(vSortedByHeight.begin(), vSortedByHeight.end());
|
||||
|
||||
int nLastCheckpointHeight = Checkpoints::GetTotalBlocksEstimate();
|
||||
int nProgressInterval = std::max((int)vSortedByHeight.size() / 20, 1);
|
||||
int nCount = 0;
|
||||
|
||||
for (const auto& item : vSortedByHeight)
|
||||
{
|
||||
CBlockIndex* pindex = item.second;
|
||||
pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0)
|
||||
+ pindex->GetBlockTrust();
|
||||
|
||||
if (pindex->nHeight >= nLastCheckpointHeight)
|
||||
{
|
||||
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
|
||||
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
|
||||
return error("LoadBlockIndex(): Failed stake modifier checkpoint h=%d, mod=0x%016"PRIx64,
|
||||
pindex->nHeight, pindex->nStakeModifier);
|
||||
}
|
||||
|
||||
if (++nCount % nProgressInterval == 0)
|
||||
{
|
||||
std::string strMsg = strprintf(_("Calculating chain trust... (%d%%)"),
|
||||
nCount * 100 / vSortedByHeight.size());
|
||||
uiInterface.InitMessage(strMsg);
|
||||
}
|
||||
}
|
||||
|
||||
printf("LoadBlockIndex(): upgrading RocksDB to format v3...\n");
|
||||
uiInterface.InitMessage(_("Upgrading block index..."));
|
||||
CDiskBlockIndex::fSerializeChainTrust = true;
|
||||
|
||||
rocksdb::WriteBatch batch;
|
||||
nCount = 0;
|
||||
for (const auto& item : vSortedByHeight)
|
||||
{
|
||||
CBlockIndex* pindex = item.second;
|
||||
CDiskBlockIndex diskindex(pindex);
|
||||
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey << make_pair(string("blockindex"), *pindex->phashBlock);
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
ssValue << diskindex;
|
||||
batch.Put(ssKey.str(), ssValue.str());
|
||||
|
||||
if (++nCount % 100000 == 0)
|
||||
{
|
||||
pdb->Write(rocksdb::WriteOptions(), &batch);
|
||||
batch.Clear();
|
||||
printf("LoadBlockIndex(): upgraded %d / %d entries\n",
|
||||
nCount, (int)vSortedByHeight.size());
|
||||
}
|
||||
}
|
||||
CDataStream ssFmtKey(SER_DISK, CLIENT_VERSION);
|
||||
ssFmtKey << string("dbformat");
|
||||
CDataStream ssFmtValue(SER_DISK, CLIENT_VERSION);
|
||||
ssFmtValue << (int)3;
|
||||
batch.Put(ssFmtKey.str(), ssFmtValue.str());
|
||||
|
||||
rocksdb::Status status = pdb->Write(rocksdb::WriteOptions(), &batch);
|
||||
if (!status.ok())
|
||||
return error("LoadBlockIndex(): failed to write upgraded block index: %s",
|
||||
status.ToString().c_str());
|
||||
|
||||
printf("LoadBlockIndex(): RocksDB upgraded to format v3 (%d entries)\n", nCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
int nLastCheckpointHeight = Checkpoints::GetTotalBlocksEstimate();
|
||||
bool fNeedModifierCheck = false;
|
||||
for (const auto& item : mapBlockIndex)
|
||||
{
|
||||
if (item.second->nHeight >= nLastCheckpointHeight)
|
||||
{
|
||||
fNeedModifierCheck = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fNeedModifierCheck)
|
||||
{
|
||||
vector<pair<int, CBlockIndex*> > vAboveCheckpoint;
|
||||
for (const auto& item : mapBlockIndex)
|
||||
if (item.second->nHeight >= nLastCheckpointHeight)
|
||||
vAboveCheckpoint.push_back(make_pair(item.second->nHeight, item.second));
|
||||
sort(vAboveCheckpoint.begin(), vAboveCheckpoint.end());
|
||||
|
||||
for (const auto& item : vAboveCheckpoint)
|
||||
{
|
||||
CBlockIndex* pindex = item.second;
|
||||
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
|
||||
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
|
||||
return error("LoadBlockIndex(): Failed stake modifier checkpoint h=%d, mod=0x%016"PRIx64,
|
||||
pindex->nHeight, pindex->nStakeModifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("STARTUP-PERF: chain_trust_and_modifiers %" PRId64 "ms\n",
|
||||
GetTimeMillis() - nPhaseStart);
|
||||
|
||||
if (nDbFormat < 3)
|
||||
{
|
||||
WriteDbFormat(3);
|
||||
printf("LoadBlockIndex(): bumped RocksDB dbformat to v3\n");
|
||||
}
|
||||
|
||||
nPhaseStart = GetTimeMillis();
|
||||
if (!ReadHashBestChain(hashBestChain))
|
||||
{
|
||||
if (pindexGenesisBlock == nullptr)
|
||||
return true;
|
||||
return error("LoadBlockIndex(): hashBestChain not loaded");
|
||||
}
|
||||
if (!mapBlockIndex.count(hashBestChain))
|
||||
return error("LoadBlockIndex(): hashBestChain not found in the block index");
|
||||
pindexBest = mapBlockIndex[hashBestChain];
|
||||
nBestHeight = pindexBest->nHeight;
|
||||
nBestChainTrust = pindexBest->nChainTrust;
|
||||
|
||||
printf("STARTUP-PERF: best_chain %" PRId64 "ms\n", GetTimeMillis() - nPhaseStart);
|
||||
|
||||
nPhaseStart = GetTimeMillis();
|
||||
{
|
||||
int nStakeSeenDepth = 500;
|
||||
CBlockIndex* pindex = pindexBest;
|
||||
int nLoaded = 0;
|
||||
while (pindex && nLoaded < nStakeSeenDepth)
|
||||
{
|
||||
if (pindex->IsProofOfStake())
|
||||
setStakeSeen.insert(make_pair(pindex->prevoutStake, pindex->nStakeTime));
|
||||
pindex = pindex->pprev;
|
||||
nLoaded++;
|
||||
}
|
||||
printf("LoadBlockIndex(): populated setStakeSeen with %d entries (last %d blocks)\n",
|
||||
(int)setStakeSeen.size(), nLoaded);
|
||||
}
|
||||
printf("STARTUP-PERF: stake_seen %" PRId64 "ms\n", GetTimeMillis() - nPhaseStart);
|
||||
|
||||
printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
|
||||
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
|
||||
CBigNum(nBestChainTrust).ToString().c_str(),
|
||||
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
|
||||
|
||||
{
|
||||
CBlockIndex* pindexBetter = nullptr;
|
||||
for (const auto& item : mapBlockIndex)
|
||||
{
|
||||
CBlockIndex* pindex = item.second;
|
||||
if (pindex == pindexBest)
|
||||
continue;
|
||||
if (pindex->nChainTrust > nBestChainTrust)
|
||||
{
|
||||
pindexBetter = pindex;
|
||||
break;
|
||||
}
|
||||
if (pindex->nChainTrust == nBestChainTrust &&
|
||||
pindex->GetBlockHash() < pindexBest->GetBlockHash())
|
||||
{
|
||||
if (!pindexBetter || pindex->GetBlockHash() < pindexBetter->GetBlockHash())
|
||||
pindexBetter = pindex;
|
||||
}
|
||||
}
|
||||
if (pindexBetter)
|
||||
{
|
||||
printf("LoadBlockIndex(): better chain tip %s at %d (trust %s vs %s)\n",
|
||||
pindexBetter->GetBlockHash().ToString().substr(0,20).c_str(),
|
||||
pindexBetter->nHeight,
|
||||
CBigNum(pindexBetter->nChainTrust).ToString().c_str(),
|
||||
CBigNum(nBestChainTrust).ToString().c_str());
|
||||
CBlock block;
|
||||
if (block.ReadFromDisk(pindexBetter))
|
||||
{
|
||||
CRocksTxDB txdb2;
|
||||
if (block.SetBestChain(txdb2, pindexBetter))
|
||||
{
|
||||
hashBestChain = pindexBetter->GetBlockHash();
|
||||
pindexBest = pindexBetter;
|
||||
nBestHeight = pindexBetter->nHeight;
|
||||
nBestChainTrust = pindexBetter->nChainTrust;
|
||||
printf("LoadBlockIndex(): switched to better chain tip\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
|
||||
printf("LoadBlockIndex(): no sync checkpoint in DB, using default\n");
|
||||
else
|
||||
printf("LoadBlockIndex(): synchronized checkpoint %s\n",
|
||||
Checkpoints::hashSyncCheckpoint.ToString().c_str());
|
||||
if (!mapBlockIndex.count(Checkpoints::hashSyncCheckpoint))
|
||||
{
|
||||
printf("LoadBlockIndex(): sync checkpoint not in index, resetting to genesis\n");
|
||||
Checkpoints::hashSyncCheckpoint = (!fTestNet ? hashGenesisBlockOfficial
|
||||
: hashGenesisBlockTestNet);
|
||||
}
|
||||
|
||||
CBigNum bnBestInvalidTrust;
|
||||
ReadBestInvalidTrust(bnBestInvalidTrust);
|
||||
nBestInvalidTrust = bnBestInvalidTrust.getuint256();
|
||||
|
||||
nPhaseStart = GetTimeMillis();
|
||||
int nCheckLevel = GetArg("-checklevel", 1);
|
||||
int nCheckDepth = GetArg("-checkblocks", 50);
|
||||
if (nCheckDepth == 0)
|
||||
nCheckDepth = 1000000000;
|
||||
if (nCheckDepth > nBestHeight)
|
||||
nCheckDepth = nBestHeight;
|
||||
printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
|
||||
CBlockIndex* pindexFork = nullptr;
|
||||
map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
|
||||
for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
|
||||
{
|
||||
if (fRequestShutdown || pindex->nHeight < nBestHeight - nCheckDepth)
|
||||
break;
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(pindex))
|
||||
return error("LoadBlockIndex(): block.ReadFromDisk failed");
|
||||
if (nCheckLevel > 0 && !block.CheckBlock(true, true, (nCheckLevel > 6)))
|
||||
{
|
||||
printf("LoadBlockIndex(): bad block at %d, hash=%s\n",
|
||||
pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
if (nCheckLevel > 1)
|
||||
{
|
||||
pair<unsigned int, unsigned int> pos = make_pair(pindex->nFile, pindex->nBlockPos);
|
||||
mapBlockPos[pos] = pindex;
|
||||
for (const CTransaction &tx : block.vtx)
|
||||
{
|
||||
uint256 hashTx = tx.GetHash();
|
||||
CTxIndex txindex;
|
||||
if (ReadTxIndex(hashTx, txindex))
|
||||
{
|
||||
if (nCheckLevel > 2 || pindex->nFile != txindex.pos.nFile
|
||||
|| pindex->nBlockPos != txindex.pos.nBlockPos)
|
||||
{
|
||||
CTransaction txFound;
|
||||
if (!txFound.ReadFromDisk(txindex.pos))
|
||||
{
|
||||
printf("LoadBlockIndex(): cannot read mislocated transaction %s\n",
|
||||
hashTx.ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
else if (txFound.GetHash() != hashTx)
|
||||
{
|
||||
printf("LoadBlockIndex(): invalid tx position for %s\n",
|
||||
hashTx.ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
}
|
||||
if (nCheckLevel > 3 && !tx.IsCoinBase())
|
||||
{
|
||||
for (const CTxIn &txin : tx.vin)
|
||||
{
|
||||
if (HaveUtxo(txin.prevout.hash, txin.prevout.n))
|
||||
{
|
||||
printf("LoadBlockIndex(): spent input still in UTXO set: %s:%i in %s\n",
|
||||
txin.prevout.hash.ToString().c_str(), txin.prevout.n,
|
||||
hashTx.ToString().c_str());
|
||||
pindexFork = pindex->pprev;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pindexFork && !fRequestShutdown)
|
||||
{
|
||||
printf("LoadBlockIndex(): moving best chain pointer back to block %d\n",
|
||||
pindexFork->nHeight);
|
||||
CBlock block;
|
||||
if (!block.ReadFromDisk(pindexFork))
|
||||
return error("LoadBlockIndex(): block.ReadFromDisk failed");
|
||||
CRocksTxDB txdb;
|
||||
block.SetBestChain(txdb, pindexFork);
|
||||
}
|
||||
printf("STARTUP-PERF: verify_blocks %" PRId64 "ms depth=%d level=%d\n",
|
||||
GetTimeMillis() - nPhaseStart, nCheckDepth, nCheckLevel);
|
||||
printf("STARTUP-PERF: load_block_index_total %" PRId64 "ms\n",
|
||||
GetTimeMillis() - nTotalStart);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) 2026 The Triangles developers.
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef TRIANGLES_TXDB_ROCKSDB_H
|
||||
#define TRIANGLES_TXDB_ROCKSDB_H
|
||||
|
||||
#include "txdb-base.h"
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <rocksdb/db.h>
|
||||
#include <rocksdb/options.h>
|
||||
#include <rocksdb/write_batch.h>
|
||||
|
||||
// RocksDB backend for the chain database.
|
||||
//
|
||||
// Mirrors CTxDB (LevelDB) for byte-level compatibility. CTxDBBase owns all
|
||||
// key serialization, so keys produced by this backend are bit-identical to
|
||||
// the LevelDB backend. That property is what lets the M1.4 dual-backend
|
||||
// parity harness verify equivalence.
|
||||
//
|
||||
// Data lives under <datadir>/rocksdb/, separate from <datadir>/txleveldb/,
|
||||
// so both backends can coexist for migration and side-by-side testing.
|
||||
class CRocksTxDB final : public CTxDBBase
|
||||
{
|
||||
public:
|
||||
CRocksTxDB(const char* pszMode = "r+");
|
||||
~CRocksTxDB() override;
|
||||
|
||||
void Close() override;
|
||||
|
||||
bool TxnBegin() override;
|
||||
bool TxnCommit() override;
|
||||
bool TxnAbort() override;
|
||||
|
||||
bool LoadBlockIndex() override;
|
||||
|
||||
protected:
|
||||
bool ReadRaw(const std::string& key, std::string& value) const override;
|
||||
bool WriteRaw(const std::string& key, const std::string& value) override;
|
||||
bool EraseRaw(const std::string& key) override;
|
||||
bool ExistsRaw(const std::string& key) const override;
|
||||
std::unique_ptr<CTxDBIteratorBase> NewIterator() const override;
|
||||
|
||||
private:
|
||||
rocksdb::DB* pdb; // Points to the global instance.
|
||||
rocksdb::WriteBatch* activeBatch; // When non-NULL, writes/deletes go here.
|
||||
rocksdb::Options options;
|
||||
int nVersion;
|
||||
|
||||
// Parallel record of every pending write (value) or delete (nullopt) on
|
||||
// activeBatch. Used by ScanBatch to answer "is this key already in the
|
||||
// active batch?" without iterating the WriteBatch via Handler — Ubuntu's
|
||||
// librocksdb-dev hides typeinfo for rocksdb::WriteBatch::Handler so a
|
||||
// subclass-based scan fails to link there.
|
||||
std::map<std::string, std::optional<std::string>> pendingBatch;
|
||||
|
||||
bool ScanBatch(const std::string& key, std::string* value, bool* deleted) const;
|
||||
};
|
||||
|
||||
#endif // TRIANGLES_TXDB_ROCKSDB_H
|
||||
+30
-1
@@ -1,11 +1,40 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||
// Copyright (c) 2026 The Triangles developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef TRIANGLES_TXDB_H
|
||||
#define TRIANGLES_TXDB_H
|
||||
|
||||
#include "txdb-base.h"
|
||||
#include "txdb-leveldb.h"
|
||||
#include "txdb-rocksdb.h"
|
||||
|
||||
#endif // TRIANGLES_TXDB_H
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
// Factory: returns a chain-database handle whose concrete backend is chosen
|
||||
// by the -chaindb command-line argument:
|
||||
//
|
||||
// -chaindb=leveldb (default — pending Phase-4 retirement)
|
||||
// -chaindb=rocksdb
|
||||
//
|
||||
// Callers receive a CTxDBBase*, so the rest of the codebase stays
|
||||
// backend-agnostic. Mode strings ("r", "r+", "cr+") match the pre-existing
|
||||
// CTxDB constructor convention.
|
||||
std::unique_ptr<CTxDBBase> MakeChainDB(const char* pszMode = "r+");
|
||||
|
||||
// True when the configured chain-DB backend is RocksDB.
|
||||
bool IsRocksDbChainBackend();
|
||||
|
||||
// On-disk directory of the chain DB for the configured backend, e.g.
|
||||
// <datadir>/txleveldb (LevelDB) or <datadir>/rocksdb (RocksDB).
|
||||
std::filesystem::path GetChainDataDir();
|
||||
|
||||
// Remove the chain DB directory for the configured backend. Callers that
|
||||
// need a fresh DB (-reindex, snapshot load) must invoke this BEFORE
|
||||
// MakeChainDB() opens the global handle for the first time.
|
||||
void WipeChainDataDir();
|
||||
|
||||
#endif // TRIANGLES_TXDB_H
|
||||
|
||||
+15
-22
@@ -6,11 +6,9 @@
|
||||
#ifndef TRIANGLES_UI_INTERFACE_H
|
||||
#define TRIANGLES_UI_INTERFACE_H
|
||||
|
||||
#include <boost/signals2/last_value.hpp>
|
||||
#include <boost/signals2/signal.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
using namespace boost::placeholders;
|
||||
#include "util_signal.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <stdint.h>
|
||||
@@ -65,46 +63,41 @@ public:
|
||||
};
|
||||
|
||||
/** Show message box. */
|
||||
boost::signals2::signal<void (const std::string& message, const std::string& caption, int style)> ThreadSafeMessageBox;
|
||||
CSignal<void(const std::string& message, const std::string& caption, int style)> ThreadSafeMessageBox;
|
||||
|
||||
/** Ask the user whether they want to pay a fee or not. */
|
||||
boost::signals2::signal<bool (int64_t nFeeRequired, const std::string& strCaption), boost::signals2::last_value<bool> > ThreadSafeAskFee;
|
||||
CSignal<bool(int64_t nFeeRequired, const std::string& strCaption)> ThreadSafeAskFee;
|
||||
|
||||
/** Handle a URL passed at the command line. */
|
||||
boost::signals2::signal<void (const std::string& strURI)> ThreadSafeHandleURI;
|
||||
CSignal<void(const std::string& strURI)> ThreadSafeHandleURI;
|
||||
|
||||
/** Progress message during initialization. */
|
||||
boost::signals2::signal<void (const std::string &message)> InitMessage;
|
||||
CSignal<void(const std::string& message)> InitMessage;
|
||||
|
||||
/** Initiate client shutdown. */
|
||||
boost::signals2::signal<void ()> QueueShutdown;
|
||||
CSignal<void()> QueueShutdown;
|
||||
|
||||
/** Translate a message to the native language of the user. */
|
||||
boost::signals2::signal<std::string (const char* psz)> Translate;
|
||||
CSignal<std::string(const char* psz)> Translate;
|
||||
|
||||
/** Block chain changed. */
|
||||
boost::signals2::signal<void ()> NotifyBlocksChanged;
|
||||
CSignal<void()> NotifyBlocksChanged;
|
||||
|
||||
/** Number of network connections changed. */
|
||||
boost::signals2::signal<void (int newNumConnections)> NotifyNumConnectionsChanged;
|
||||
|
||||
/**
|
||||
* New, updated or cancelled alert.
|
||||
* @note called with lock cs_mapAlerts held.
|
||||
*/
|
||||
boost::signals2::signal<void (const uint256 &hash, ChangeType status)> NotifyAlertChanged;
|
||||
CSignal<void(int newNumConnections)> NotifyNumConnectionsChanged;
|
||||
};
|
||||
|
||||
extern CClientUIInterface uiInterface;
|
||||
|
||||
/**
|
||||
* Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
|
||||
* If no translation slot is registered, nothing is returned, and simply return the input.
|
||||
* Translation function: Call Translate signal on UI interface, which returns
|
||||
* an std::optional. If no translation slot is registered, fall back to the
|
||||
* untranslated input.
|
||||
*/
|
||||
inline std::string _(const char* psz)
|
||||
{
|
||||
boost::optional<std::string> rv = uiInterface.Translate(psz);
|
||||
return rv ? (*rv) : psz;
|
||||
std::optional<std::string> rv = uiInterface.Translate(psz);
|
||||
return rv ? *rv : psz;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user