diff --git a/.gitattributes b/.gitattributes index 4618eff54..62c4e72eb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ ci/official/*.patch -text +packaging/setup/*.bat text eol=crlf diff --git a/.github/workflows/setup-test.yml b/.github/workflows/setup-test.yml new file mode 100644 index 000000000..2c830c79c --- /dev/null +++ b/.github/workflows/setup-test.yml @@ -0,0 +1,206 @@ +name: Set-up script test + +# The guided set-up (packaging/setup) in the published packages of Linux, Windows and macOS, used as a +# user would: answers typed on stdin, the snapshot downloaded, the node started up to the mainnet +# head; then a second run that must change nothing, and a full berkeleydb configuration with a wrong +# answer first. +on: + push: + tags: ['setup-test-*'] + workflow_dispatch: + +permissions: + contents: read + +env: + TAG: community-v4.2.532-community.14 + +jobs: + setup: + name: Set-up (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + include: + - os: linux + runner: ubuntu-24.04 + package: '*-linux-x64.tar.gz' + node: ./node + - os: windows + runner: windows-2022 + package: '*-windows-x64.zip' + node: ./node.exe + - os: macos + runner: macos-14 + package: '*-macos-arm64.tar.gz' + node: ./node + runs-on: ${{ matrix.runner }} + timeout-minutes: 90 + defaults: + run: + shell: bash + steps: + - uses: actions/checkout@v4 + + - name: Checkout submodules + run: git submodule update --init -- third-party/thrift-interface-definitions + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Python packages + run: python -m pip install --quiet thriftpy2 base58 pynacl + + - name: Package with the set-up script + env: + GH_TOKEN: ${{ github.token }} + PACKAGE: ${{ matrix.package }} + run: | + mkdir packages + gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --dir packages --pattern "$PACKAGE" + pkg=$(ls packages/$PACKAGE | grep -v glibc | head -1) + case "$pkg" in *.zip) unzip -q "$pkg" ;; *) tar -xzf "$pkg" ;; esac + if [ "${{ matrix.os }}" = windows ]; then + cp packaging/setup/setup.bat packaging/setup/setup.ps1 csnode/ + else + cp packaging/setup/setup.sh csnode/ && chmod +x csnode/setup.sh + fi + sed -i.bak 's/^executor_multi_instance=.*/executor_multi_instance=true/' csnode/config.ini && rm csnode/config.ini.bak + [ "${{ matrix.os }}" != macos ] || xattr -dr com.apple.quarantine csnode 2>/dev/null || true + python ci/testnet/gen_keys.py 1 keys >/dev/null + cp keys/n1/NodePublic.txt keys/n1/NodePrivate.txt csnode/ + cp -a csnode csnode-clean + cat > setup-run.sh <<'EOF' + # setup-run.sh + dir=$1; answers=$2 + cd "$dir" + if [ -f setup.bat ]; then + printf "$answers" | cmd //c setup.bat + else + printf "$answers" | ./setup.sh + fi + EOF + cat > check-config.py <<'EOF' + # check-config.py
... + import sys + lines = open(sys.argv[1], newline="").read().replace("\r\n", "\n").split("\n") + sections, cur = {}, None + for l in lines: + s = l.strip() + if s.startswith("["): + cur = s + sections.setdefault(cur, []) + elif cur and s and not s.startswith(";"): + sections[cur].append(s) + bad = 0 + for i in range(2, len(sys.argv), 2): + got = " ".join(sections.get("[" + sys.argv[i] + "]", [])) + ok = got == sys.argv[i + 1] + bad += not ok + print(("ok " if ok else "FAIL") + f" [{sys.argv[i]}] {got!r}" + ("" if ok else f" expected {sys.argv[i + 1]!r}")) + sys.exit(1 if bad else 0) + EOF + + - name: First set-up (pruned, lmdb, snapshot from GitHub, share, do not start) + run: | + # storage, database, first start, share, executor, start, write + bash setup-run.sh csnode '\n\n\n\n\nno\n\n' 2>&1 | tee setup1.txt | grep -av '^download-snapshot: .*%' | tail -40 + python check-config.py csnode/config.ini snapshot "serve=on fetch=off" \ + storage "db_backend=lmdb checkpoint_every=100000 checkpoint_keep=2 prune_keep_blocks=1000" + ls csnode/config.ini.bak-* && test "$(ls csnode/config.ini.bak-* | wc -l)" -eq 1 + test -f csnode/snapshot.json && echo "snapshot restored" + ls csnode/db + + - name: Start the node up to the head + run: | + cd csnode + ( rc=0; ${{ matrix.node }} --config-file config.ini stdout.log 2>&1 || rc=$?; echo "the node exited with status $rc" > node-exit.txt ) & + sleep 2 + pid= + [ "${{ matrix.os }}" = windows ] || pid=$(pgrep -n -f -- '--config-file config.ini' || true) + echo "NODE_PID=$pid" >> "$GITHUB_ENV" + cd .. + python ci/snapshot/check_node.py wait-head --api 127.0.0.1:9090 --timeout-min 45 \ + --exit-file csnode/node-exit.txt --api-grace-min 6 || { tail -n 60 csnode/stdout.log | cut -c1-300; exit 1; } + grep -ah 'snapshot.json verified\|STATE DIGEST' csnode/log/log_*.txt | tail -3 | cut -c1-200 + + - name: Stop the node + if: always() + run: | + if [ "${{ matrix.os }}" = windows ]; then + taskkill //F //IM node.exe >/dev/null 2>&1 || true + taskkill //F //IM java.exe >/dev/null 2>&1 || true + else + [ -z "${NODE_PID:-}" ] || kill -TERM "$NODE_PID" 2>/dev/null || true + for _ in $(seq 1 120); do [ -z "${NODE_PID:-}" ] || kill -0 "$NODE_PID" 2>/dev/null || break; sleep 1; done + pkill -f '[c]ontract-executor[.]jar' || true + fi + sleep 5 + + - name: Second set-up (Enter everywhere, do not start) changes nothing + run: | + sleep 1 + # storage, database, share, executor, start, write (no first-start question: the database exists) + bash setup-run.sh csnode '\n\n\n\nno\n\n' 2>&1 | tee setup2.txt | tail -20 + grep -a "already holds a lmdb database" setup2.txt + if grep -a "How should the node get the chain" setup2.txt; then echo "asked about the first start"; exit 1; fi + python check-config.py csnode/config.ini snapshot "serve=on fetch=off" \ + storage "db_backend=lmdb checkpoint_every=100000 checkpoint_keep=2 prune_keep_blocks=1000" + test "$(grep -c '^; written by setup' csnode/config.ini)" -eq 1 + test "$(ls csnode/config.ini.bak-* | wc -l)" -eq 2 + + - name: Full berkeleydb set-up with a wrong answer first + run: | + # storage (wrong, then full), database, share (no), executor, start, write + bash setup-run.sh csnode-clean 'maybe\nfull\n\nno\n\nno\n\n' 2>&1 | tee setup3.txt | tail -20 + grep -a "Please answer one of" setup3.txt + python check-config.py csnode-clean/config.ini snapshot "serve=off fetch=off" storage "db_backend=berkeleydb" + # the rest of config.ini is the package's + python - <<'EOF' + import glob + def body(p): + out = [] + for l in open(p, newline="").read().replace("\r\n", "\n").split("\n"): + if l.startswith("; written by setup"): + break + if l.strip(): + out.append(l) + return out + new, old = body("csnode-clean/config.ini"), body(glob.glob("csnode-clean/config.ini.bak-*")[0]) + assert new == old, "the rest of config.ini changed" + print("the rest of config.ini is unchanged") + EOF + + - name: Executor of a folder upgraded by copying the node only + run: | + build=$(python -c "import re, zipfile; print(re.search(r'git.total.commit.count=(\d+)', zipfile.ZipFile('csnode-clean/contract-executor.jar').read('git.properties').decode()).group(1))") + echo "community executor in the package: build $build" + # the official config.ini kept: the node would refuse the community executor (max 1518) + cp -a csnode-clean csnode-exec && rm -f csnode-exec/config.ini.bak-* + sed -i.orig 's/^executor_commit_max=.*/executor_commit_max=1518/' csnode-exec/config.ini && rm csnode-exec/config.ini.orig + # storage, database, first start (sync), share, executor (Enter: community), start, write + bash setup-run.sh csnode-exec 'pruned\nlmdb\nsync\nno\n\nno\n\n' 2>&1 | tee setup4.txt | tail -12 + grep -a "In this folder: build $build (community); config.ini accepts 1518..1518" setup4.txt + grep -q "^executor_commit_max=$build" csnode-exec/config.ini && echo "executor_commit_max=$build" + # no executor jars at all: the community one is downloaded from GitHub and checked + rm csnode-exec/contract-executor.jar csnode-exec/sc-api-support.jar + # storage, database, first start (sync), share, executor (community), download (Enter: yes), start, write + bash setup-run.sh csnode-exec 'pruned\nlmdb\nsync\nno\ncommunity\n\nno\n\n' 2>&1 | tee setup5.txt | tail -12 + grep -a "contract-executor.jar and sc-api-support.jar replaced: build [0-9]* (community)" setup5.txt + test -f csnode-exec/contract-executor.jar && test -f csnode-exec/sc-api-support.jar + + - name: Collect + if: always() + run: | + mkdir -p result + cp setup*.txt csnode/config.ini result/ 2>/dev/null || true + cp csnode-clean/config.ini result/config-full.ini 2>/dev/null || true + tail -c 1000000 csnode/stdout.log > result/stdout.log 2>/dev/null || true + + - uses: actions/upload-artifact@v4 + if: always() + with: + name: setup-${{ matrix.os }} + path: result/ + retention-days: 14 diff --git a/docs/CHANGES.md b/docs/CHANGES.md index f4c2f4cb2..816f8ef07 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -44,6 +44,7 @@ lines under the new release. | #126 | api: after a quick start at the last stored block, contract calls through the API returned "Transaction in progress" and never their result; the node now marks its stored blocks as read when it runs | | #127 | ci: token tests: `ci/contracts/executor_harness.py` compares contract executors on token contracts without a node, and `TOKENS=1` runs `ci/testnet/tokens.py` (v1 and v0 tokens, balances and states on every node, the token API; `TOKENS_VIA` through another node) on the private network | | #128 | api: the token cache (`[api] tokens_cache=true`, any build) follows v1/v2 tokens (BigDecimal amounts, standards 3 and 4) and only successful calls. It reads balances from the contract for the wallets a call involves, refreshes them in the background at start, and is rebuilt from the chain when the checkpoint holds none. `approve()` without arguments no longer reads out of bounds. `TokensListGet` filters before paging, and the token lists sort without exceptions or invalid orderings. `TokenTransfersListGet` reports the signer as the sender. See [tokens.md](tokens.md) | +| #131 | packaging: a guided set-up in every package (`setup.sh`, `setup.bat`): pruned or full, database, how the first start gets the chain, snapshot sharing, and the contract executor (community or official: it shows the build in the folder, can download either, checked, and sets `executor_commit_min/max` so that the node starts it; an upgrade by copying only the node binary keeps the old executor) | ## community.14 — 2026-10-27 diff --git a/packaging/make-package.sh b/packaging/make-package.sh index 587d721d8..ba512fecf 100755 --- a/packaging/make-package.sh +++ b/packaging/make-package.sh @@ -47,6 +47,17 @@ install -m 644 "${hosts:-$here/mainnet/known_hosts.txt}" "$dir/known_hosts.txt" # the serving nodes the weekly survey found (ci/known-hosts/snapshot_sources.py), when it has found any [ ! -f "$here/mainnet/snapshot_sources.txt" ] || install -m 644 "$here/mainnet/snapshot_sources.txt" "$dir/snapshot_sources.txt" +# the guided set-up (setup.sh, or setup.bat with setup.ps1 on Windows): writes [snapshot] and [storage] +if [ "$os" = windows ]; then + install -m 644 "$here/setup/setup.bat" "$here/setup/setup.ps1" "$dir/" + setup=setup.bat + setup_files="setup.bat, setup.ps1" +else + install -m 755 "$here/setup/setup.sh" "$dir/" + setup=./setup.sh + setup_files=setup.sh +fi + # the node accepts an executor only within [executor_commit_min, executor_commit_max], and the # executor reports its git commit count, which our fixes raised above the release's 1518 commits=$(unzip -p "$executor/contract-executor.jar" git.properties | sed -n 's/^git.total.commit.count=//p' | tr -d '\r') @@ -104,6 +115,21 @@ Changing the block database (db_backend) only reads berkeleydb: convert back before running it on this folder. In Docker, use CREDITS_CONVERT_DB=yes. Every [storage] setting and the conversion in detail: https://github.com/EnzinoBB/node/blob/master/docs/storage.md +First time: cd csnode && $setup + asks a few questions (pruned or full, database, snapshot, sharing, contract executor; Enter keeps + each default), writes config.ini (the previous one is kept as config.ini.bak-) and starts + the node. + +Upgrading an existing folder (an official one too): stop the node, copy $exe, contract-executor.jar, + sc-api-support.jar and $setup_files from this package into it, then run $setup there. Copying only + $exe keeps the old executor. The node starts an executor only if its build is within + executor_commit_min..executor_commit_max of config.ini: with the official config.ini (1518..1518) + it stops the community executor (build $commits) and runs no smart contract. The set-up shows the + build in the folder, asks whether to run the community executor or the official one (it can + download either: the community one from https://github.com/EnzinoBB/contract-executor/releases, + the official one from the CREDITSCOM release), keeps the replaced jars aside and sets the range in + config.ini. Both executors give the same contract results: either is compatible with the network. + Start: cd csnode && $start (full storage, as the official node) or: cd csnode && $start --config-file config.pruned.ini (pruned storage) A pruned node can start from the published snapshot: $start --config-file config.pruned.ini --download-snapshot diff --git a/packaging/setup/setup.bat b/packaging/setup/setup.bat new file mode 100644 index 000000000..17070a57e --- /dev/null +++ b/packaging/setup/setup.bat @@ -0,0 +1,13 @@ +@echo off +rem Guided set-up of the node in this folder: double-click it, or run "setup.bat --defaults" to take +rem every default. The questions and the changes to config.ini are in setup.ps1, next to this file. +setlocal +set "ARGS=" +if /i "%~1"=="--defaults" set "ARGS=-Defaults" +if /i "%~1"=="-y" set "ARGS=-Defaults" +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0setup.ps1" %ARGS% +set "RC=%ERRORLEVEL%" +rem a double-clicked window stays open to show the result +echo. +pause +exit /b %RC% diff --git a/packaging/setup/setup.ps1 b/packaging/setup/setup.ps1 new file mode 100644 index 000000000..3e6a15e97 --- /dev/null +++ b/packaging/setup/setup.ps1 @@ -0,0 +1,399 @@ +# Guided set-up of the node in this folder (Windows): the same questions, the same config.ini and the +# same contract executor choice as setup.sh. Started by setup.bat; "setup.bat --defaults" takes every +# default without asking. +param([switch]$Defaults) + +$ErrorActionPreference = 'Stop' +Set-Location -LiteralPath $PSScriptRoot + +$node = '.\node.exe' +$config = 'config.ini' +if (-not (Test-Path $node) -or -not (Test-Path $config)) { + Write-Host 'Run setup.bat from the csnode folder of the package (node.exe and config.ini are not here).' + exit 1 +} + +function Ask([string]$Question, [string]$Default, [string[]]$Choices) { + while ($true) { + if ($Defaults) { Write-Host "$Question [$Default]: $Default"; return $Default } + if ([Console]::IsInputRedirected) { + Write-Host -NoNewline "${Question} [$Default]: " + $reply = [Console]::In.ReadLine() + if ($null -eq $reply) { $reply = '' } + Write-Host $reply + } else { + $reply = Read-Host "$Question [$Default]" + } + $reply = $reply.Trim().ToLower() + if ($reply -eq '') { return $Default } + foreach ($c in $Choices) { if ($c.StartsWith($reply)) { return $c } } + Write-Host " Please answer one of: $($Choices -join ' ')" + } +} + +function Existing-Backend { + if (Test-Path 'db\.backend') { return (Get-Content 'db\.backend' -Raw).Trim() } + if ((Test-Path 'db\blockchain.db') -or (Test-Path 'db\sequence.db')) { return 'berkeleydb' } + if (Test-Path 'db\lmdb\data.mdb') { return 'lmdb' } + if (Test-Path 'db\CURRENT') { return 'rocksdb' } + return '' +} + +$lines = @(Get-Content -LiteralPath $config) + +function Current([string]$Section, [string]$Key) { + $in = $false; $value = '' + foreach ($l in $lines) { + if ($l -match '^\s*\[') { $in = ($l -match "^\s*\[$Section\]"); continue } + if ($in -and $l -match "^\s*$Key\s*=\s*(.*?)\s*$") { $value = $Matches[1] } + } + return $value +} + +# --- contract executor --------------------------------------------------------------------------- +# The node runs smart contracts with contract-executor.jar (it loads sc-api-support.jar from the same +# folder) and starts it only if the build the jar reports (its git commit count) is within +# [executor_commit_min, executor_commit_max] of [api]; otherwise it stops the executor and runs no +# contract. The official release is build 1518; community builds add fixes on top and count higher. +$officialBuild = 1518 +$officialUrl = 'https://github.com/CREDITSCOM/node/releases/download/mainnet-4.2.532.0/Ubuntu20_Mainnet-4.2.532.0.tar.gz' +$officialShaExecutor = '592510d1586818348c65bea9b443e7f58aa6947113e43aa8906af8426aeef028' +$officialShaSupport = '395d9a108a66fe1d92c1d2096a9d11f4c7662b51b47af792db0ff4cc91df9807' +$communityUrl = 'https://github.com/EnzinoBB/contract-executor/releases/latest/download' +Add-Type -AssemblyName System.IO.Compression.FileSystem + +# the build (git commit count) an executor jar reports, 0 when it cannot be read +function Jar-Build([string]$Jar) { + if (-not (Test-Path -LiteralPath $Jar)) { return 0 } + try { + $zip = [System.IO.Compression.ZipFile]::OpenRead((Resolve-Path -LiteralPath $Jar).Path) + try { + $entry = $zip.GetEntry('git.properties') + if ($null -eq $entry) { return 0 } + $reader = New-Object System.IO.StreamReader($entry.Open()) + try { $text = $reader.ReadToEnd() } finally { $reader.Dispose() } + } finally { $zip.Dispose() } + if ($text -match '(?m)^git\.total\.commit\.count=(\d+)') { return [int]$Matches[1] } + } catch { } + return 0 +} + +function Describe-Build([int]$Build) { + if ($Build -eq 0) { return 'none or unreadable' } + if ($Build -eq $officialBuild) { return "build $Build (official)" } + if ($Build -gt $officialBuild) { return "build $Build (community)" } + return "build $Build" +} + +function Sha256([string]$Path) { return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLower() } + +# downloads the jars of the chosen executor into $Dir; prints the reason and returns $false when it cannot +function Download-Executor([string]$Kind, [string]$Dir) { + if (-not (Get-Command curl.exe -ErrorAction SilentlyContinue)) { Write-Host ' curl.exe is not available.'; return $false } + if ($Kind -eq 'community') { + foreach ($f in @('contract-executor.jar', 'sc-api-support.jar', 'SHA256SUMS')) { + & curl.exe -fsSL -o (Join-Path $Dir $f) "$communityUrl/$f" + if ($LASTEXITCODE -ne 0) { Write-Host ' The download from GitHub failed.'; return $false } + } + $sums = Get-Content -LiteralPath (Join-Path $Dir 'SHA256SUMS') + foreach ($f in @('contract-executor.jar', 'sc-api-support.jar')) { + $sha = Sha256 (Join-Path $Dir $f) + if (-not ($sums | Where-Object { $_ -match "^$sha\s+\*?$([regex]::Escape($f))$" })) { + Write-Host " $f does not match the SHA256SUMS of the release."; return $false + } + } + } else { + Write-Host ' The jars come from the official release package (about 350 MB to download, only they are kept).' + $archive = Join-Path $Dir 'official.tar.gz' + & curl.exe -fsSL -o $archive $officialUrl + if ($LASTEXITCODE -ne 0) { Write-Host ' The download of the official release failed.'; return $false } + & tar.exe -xzf $archive -C $Dir csnode/contract-executor.jar csnode/sc-api-support.jar + if ($LASTEXITCODE -ne 0) { Write-Host ' The official release could not be extracted.'; return $false } + Remove-Item -LiteralPath $archive + $extracted = Join-Path $Dir 'csnode' + Move-Item -LiteralPath (Join-Path $extracted 'contract-executor.jar'), (Join-Path $extracted 'sc-api-support.jar') -Destination $Dir + if ((Sha256 (Join-Path $Dir 'contract-executor.jar')) -ne $officialShaExecutor -or + (Sha256 (Join-Path $Dir 'sc-api-support.jar')) -ne $officialShaSupport) { + Write-Host ' The jars of the official release do not have the expected checksums.'; return $false + } + } + return $true +} + +# key=value in [api] of the lines, added at the end of the section when missing +function Set-ApiKey([System.Collections.Generic.List[string]]$Lines, [string]$Key, [string]$Value) { + $in = $false; $end = -1 + for ($i = 0; $i -lt $Lines.Count; $i++) { + if ($Lines[$i] -match '^\s*\[') { + if ($in -and $end -lt 0) { $end = $i } + $in = ($Lines[$i] -match '^\s*\[api\]') + continue + } + if ($in -and $Lines[$i] -match "^\s*$Key\s*=") { $Lines[$i] = "$Key=$Value"; return } + } + if ($in) { $Lines.Add("$Key=$Value") } elseif ($end -ge 0) { $Lines.Insert($end, "$Key=$Value") } +} + +$version = if (Test-Path 'VERSION') { (Get-Content 'VERSION' -Raw).Trim() } else { 'unknown version' } +Write-Host "Credits node set-up ($version)" +Write-Host 'Each question shows the default in brackets: press Enter to keep it.' +Write-Host '' + +$have = Existing-Backend +if ($have) { Write-Host "This folder already holds a $have database: it is kept."; Write-Host '' } + +$curBackend = Current 'storage' 'db_backend' +if (-not $curBackend) { $curBackend = Current 'storage' 'block_db' } +$curPrune = Current 'storage' 'prune_keep_blocks' +$curServe = Current 'snapshot' 'serve' + +# 1. pruned or full +$storageDefault = 'pruned' +if (($curBackend + $curPrune) -and (-not $curPrune -or $curPrune -eq '0')) { $storageDefault = 'full' } +if ($have -eq 'berkeleydb') { $storageDefault = 'full' } +Write-Host '1. Storage' +Write-Host ' pruned: keeps only recent blocks (a few GB), starts in minutes from a snapshot. Enough to run a node.' +Write-Host ' full: keeps every block since the start of the chain (tens of GB), for history and explorers.' +$storage = Ask ' Pruned or full node? (pruned/full)' $storageDefault @('pruned', 'full') +if ($storage -eq 'full' -and $have -and $curPrune -and $curPrune -ne '0') { + Write-Host ' This folder holds a pruned database: its removed blocks are gone. To get a full node,' + Write-Host ' move db, caches and qs away and run the set-up again on the empty folder.' +} +Write-Host '' + +# 2. database +Write-Host '2. Database' +if ($storage -eq 'pruned') { + $backendDefault = if ($curBackend -eq 'rocksdb' -or $have -eq 'rocksdb') { 'rocksdb' } else { 'lmdb' } + Write-Host ' lmdb: the usual choice for pruned nodes.' + Write-Host ' rocksdb: compressed, smaller on disk, a little more CPU.' + $backend = Ask ' Database? (lmdb/rocksdb)' $backendDefault @('lmdb', 'rocksdb') +} else { + $backendDefault = 'berkeleydb' + if (($curBackend -eq 'lmdb' -or $curBackend -eq 'rocksdb') -and (-not $curPrune -or $curPrune -eq '0')) { $backendDefault = $curBackend } + if ($have) { $backendDefault = $have } + Write-Host " berkeleydb: the official node's format (the official node can open this folder too)." + Write-Host ' rocksdb: the smallest full node (about a third of berkeleydb).' + Write-Host ' lmdb: fast, larger on disk.' + $backend = Ask ' Database? (berkeleydb/rocksdb/lmdb)' $backendDefault @('berkeleydb', 'rocksdb', 'lmdb') +} +Write-Host '' + +$convert = $false +if ($have -and $have -ne $backend) { + Write-Host " The database in this folder is $have, you chose $backend." + $a = Ask " Convert it to $backend at the next start (it can take hours)? (yes/no)" 'yes' @('yes', 'no') + if ($a -eq 'yes') { $convert = $true } else { $backend = $have; Write-Host " Keeping $have." } + Write-Host '' +} + +# 3. how the first start gets the chain (only for an empty folder) +$first = 'keep' +if (-not $have) { + Write-Host '3. First start' + if ($backend -eq 'berkeleydb') { + Write-Host ' A berkeleydb node syncs from block 0 (about 35 hours on mainnet).' + $first = 'sync' + } else { + Write-Host ' github: download the snapshot published on GitHub now, check it and restore it (minutes).' + Write-Host ' network: the node fetches a snapshot from other nodes that share it; it needs at least two' + Write-Host ' of them, and few nodes share yet: without them it syncs from block 0 after 10 minutes.' + Write-Host ' sync: sync from block 0 by itself (about 35 hours on mainnet).' + $firstDefault = 'github' + if ($storage -eq 'full') { + Write-Host ' A full node started from a snapshot holds the blocks from the snapshot on only.' + $firstDefault = 'sync' + } + $first = Ask ' How should the node get the chain? (github/network/sync)' $firstDefault @('github', 'network', 'sync') + } + Write-Host '' +} + +# 4. sharing snapshots +$serveDefault = if ($curServe -eq 'off') { 'no' } else { 'yes' } +Write-Host '4. Sharing' +Write-Host ' The node can share snapshots of the chain with new nodes, so that they start in minutes.' +Write-Host ' It uses at most 2 MB/s of upload, only while the node is synced, and about 200 MB of disk.' +$serve = Ask ' Share snapshots with other nodes? (yes/no)' $serveDefault @('yes', 'no') +Write-Host '' + +# 5. the contract executor (when config.ini runs one) +$executorChoice = 'none' +$executorAction = 'keep' +if (Current 'api' 'executor_command') { + $haveBuild = Jar-Build 'contract-executor.jar' + $supportBuild = Jar-Build 'sc-api-support.jar' + $curMin = Current 'api' 'executor_commit_min' + $curMax = Current 'api' 'executor_commit_max' + Write-Host '5. Contract executor' + Write-Host ' The node runs smart contracts with contract-executor.jar and sc-api-support.jar, and starts' + Write-Host ' the executor only if its build is within executor_commit_min..executor_commit_max of config.ini.' + Write-Host (" In this folder: $(Describe-Build $haveBuild); config.ini accepts " + $(if ($curMin) { $curMin } else { '1506' }) + '..' + $(if ($curMax) { $curMax } else { 'any' }) + '.') + if ($haveBuild -and $supportBuild -and $haveBuild -ne $supportBuild) { + Write-Host " sc-api-support.jar is $(Describe-Build $supportBuild): the two jars come from different packages." + } + Write-Host ' community: the latest community build, the official one with fixes (code of a contract that never' + Write-Host ' returns is stopped, tokens of every standard are reported to the API). Contracts give' + Write-Host ' the same results as with the official one.' + Write-Host " official: build $officialBuild of the official release." + # unattended (--defaults), nothing is downloaded: the jars in the folder are kept + $downloadDefault = if ($Defaults) { 'no' } else { 'yes' } + $executorDefault = if ($haveBuild -gt $officialBuild) { 'community' } else { 'official' } + $executorChoice = Ask ' Which executor? (community/official)' $executorDefault @('community', 'official') + if ($executorChoice -eq 'community' -and $haveBuild -le $officialBuild) { + Write-Host ' The jars in this folder are not a community build (copying only node.exe keeps the old ones).' + $a = Ask ' Download the latest community executor from GitHub now (about 25 MB)? (yes/no)' $downloadDefault @('yes', 'no') + $executorAction = if ($a -eq 'yes') { 'download' } else { 'manual' } + } elseif ($executorChoice -eq 'official' -and $haveBuild -ne $officialBuild) { + if ((Test-Path 'contract-executor.jar.official') -and (Test-Path 'sc-api-support.jar.official')) { + Write-Host ' The official jars set aside by an earlier set-up are here: they are put back.' + $executorAction = 'restore' + } else { + $a = Ask ' Download the official executor now (from the official release, about 350 MB)? (yes/no)' $downloadDefault @('yes', 'no') + $executorAction = if ($a -eq 'yes') { 'download' } else { 'manual' } + } + } + Write-Host '' +} + +# 6. start now +$start = Ask '6. Start the node when the set-up is done? (yes/no)' 'yes' @('yes', 'no') +Write-Host '' + +Write-Host 'Summary' +Write-Host " storage: $storage" +Write-Host (" database: $backend" + $(if ($convert) { " (converted from $have at the next start)" } else { '' })) +switch ($first) { + 'keep' { Write-Host ' first start: the database in this folder is kept' } + 'network' { Write-Host ' first start: snapshot from other nodes' } + 'github' { Write-Host ' first start: snapshot downloaded from GitHub now' } + 'sync' { Write-Host ' first start: sync from block 0' } +} +Write-Host " share: $serve" +if ($executorChoice -ne 'none') { + switch ($executorAction) { + 'keep' { Write-Host " executor: $executorChoice, the jars in this folder are kept" } + 'download' { Write-Host " executor: $executorChoice, downloaded now (the jars in this folder are set aside)" } + 'restore' { Write-Host ' executor: official, the jars set aside before are put back' } + 'manual' { Write-Host " executor: $executorChoice, to copy by hand (see below)" } + } +} +Write-Host " start now: $start" +if (-not $Defaults) { + if ((Ask 'Write config.ini? (yes/no)' 'yes' @('yes', 'no')) -ne 'yes') { Write-Host 'Nothing changed.'; exit 0 } +} + +$stamp = Get-Date -Format 'yyyyMMdd-HHmmss' +$backup = "$config.bak-$stamp" +Copy-Item -LiteralPath $config -Destination $backup + +$fetch = if ($first -eq 'network') { 'auto' } else { 'off' } +$serveValue = if ($serve -eq 'yes') { 'on' } else { 'off' } + +# config.ini without its [snapshot] and [storage] sections, then the new ones ([snapshot] first) +$out = New-Object System.Collections.Generic.List[string] +$skip = $false; $blank = $false +foreach ($l in $lines) { + if ($l -match '^\s*\[') { $skip = ($l -match '^\s*\[(snapshot|storage)\]') } + if ($skip -or $l -match '^; written by setup\.(sh|bat) on ') { continue } + if ($l.Trim() -eq '') { if ($blank) { continue }; $blank = $true } else { $blank = $false } + $out.Add($l) +} +$out.Add('') +$out.Add("; written by setup.bat on $(Get-Date -Format 'yyyy-MM-dd HH:mm'), the previous file is $backup") +$out.Add('[snapshot]') +$out.Add("serve=$serveValue") +$out.Add("fetch=$fetch") +$out.Add('') +$out.Add('[storage]') +$out.Add("db_backend=$backend") +if ($storage -eq 'pruned') { + $out.Add('checkpoint_every=100000') + $out.Add('checkpoint_keep=2') + $out.Add('prune_keep_blocks=1000') +} + +# the executor: put the chosen jars in place, then make config.ini accept their build +$executorNotes = New-Object System.Collections.Generic.List[string] +if ($executorChoice -ne 'none') { + # keeps the jars in this folder aside: as .official when they are the official ones (a later + # set-up puts them back without a download), as .bak- otherwise + function Set-Aside { + $suffix = if ((Jar-Build 'contract-executor.jar') -eq $officialBuild) { '.official' } else { ".bak-$stamp" } + foreach ($f in @('contract-executor.jar', 'sc-api-support.jar')) { + if (Test-Path $f) { Move-Item -LiteralPath $f -Destination "$f$suffix" -Force } + } + $executorNotes.Add("The previous executor jars are kept as contract-executor.jar$suffix and sc-api-support.jar$suffix.") + } + if ($executorAction -eq 'download') { + Write-Host '' + Write-Host "Downloading the $executorChoice executor..." + $dl = Join-Path ([System.IO.Path]::GetTempPath()) ("credits-executor-" + [guid]::NewGuid()) + New-Item -ItemType Directory -Path $dl | Out-Null + if (Download-Executor $executorChoice $dl) { + Set-Aside + Move-Item -LiteralPath (Join-Path $dl 'contract-executor.jar'), (Join-Path $dl 'sc-api-support.jar') -Destination $PSScriptRoot + $executorNotes.Add("contract-executor.jar and sc-api-support.jar replaced: $(Describe-Build (Jar-Build 'contract-executor.jar')).") + } else { + $executorAction = 'manual' + $executorNotes.Add('The jars in this folder are kept.') + } + Remove-Item -LiteralPath $dl -Recurse -Force + } elseif ($executorAction -eq 'restore') { + Set-Aside + Move-Item -LiteralPath 'contract-executor.jar.official' -Destination 'contract-executor.jar' + Move-Item -LiteralPath 'sc-api-support.jar.official' -Destination 'sc-api-support.jar' + $executorNotes.Add('The official executor jars are back.') + } + $build = Jar-Build 'contract-executor.jar' + if ($build) { + $min = Current 'api' 'executor_commit_min' + $max = Current 'api' 'executor_commit_max' + if ($min -and $build -lt [int]$min) { Set-ApiKey $out 'executor_commit_min' "$build"; $executorNotes.Add("config.ini: executor_commit_min=$build") } + if ($max -and $max -ne '-1' -and $build -gt [int]$max) { + Set-ApiKey $out 'executor_commit_max' "$build" + $executorNotes.Add("config.ini: executor_commit_max=$build (it was ${max}: the node would not start this executor)") + } + } + if ($executorAction -eq 'manual') { + $executorNotes.Add('') + $executorNotes.Add("To use the $executorChoice executor, with the node stopped:") + if ($executorChoice -eq 'community') { + $executorNotes.Add(' copy contract-executor.jar and sc-api-support.jar, both, from the csnode folder of the community') + $executorNotes.Add(" package (or from $communityUrl/...) into $PSScriptRoot,") + } else { + $executorNotes.Add(' copy contract-executor.jar and sc-api-support.jar, both, from the official release') + $executorNotes.Add(" ($officialUrl) into $PSScriptRoot,") + } + $executorNotes.Add(' then run this set-up again: it makes config.ini accept their build (executor_commit_min/max).') + } + $executorNotes.Add('') + $executorNotes.Add('Updating this folder later: a new node.exe is not enough when its package brings a new executor.') + $executorNotes.Add('Copy node.exe, contract-executor.jar and sc-api-support.jar together, then run the set-up again.') +} + +# ASCII without a byte-order mark, as the package ships it +[System.IO.File]::WriteAllLines((Join-Path $PSScriptRoot $config), $out, (New-Object System.Text.ASCIIEncoding)) +Write-Host "config.ini written; the previous one is $backup" +foreach ($n in $executorNotes) { Write-Host $n } + +if ($first -eq 'github') { + Write-Host '' + Write-Host 'Downloading the published snapshot...' + & $node --config-file $config --download-snapshot + if ($LASTEXITCODE -ne 0) { + Write-Host 'The download did not finish. Run setup.bat again, or start the node: it can get the snapshot from other nodes.' + exit 1 + } +} + +$nodeArgs = @('--config-file', $config) +if ($convert) { $nodeArgs += '--convert-db' } +if ($start -eq 'yes') { + Write-Host '' + Write-Host 'Starting the node (Ctrl+C stops it and saves its state)...' + & $node @nodeArgs + exit $LASTEXITCODE +} +Write-Host '' +Write-Host "Start the node with: cd `"$PSScriptRoot`" ; node.exe $($nodeArgs -join ' ')" diff --git a/packaging/setup/setup.sh b/packaging/setup/setup.sh new file mode 100755 index 000000000..dcb6cf2d5 --- /dev/null +++ b/packaging/setup/setup.sh @@ -0,0 +1,427 @@ +#!/usr/bin/env bash +# Guided set-up of the node in this folder: storage (pruned or full), database, how the first start +# gets the chain, whether to share snapshots, which contract executor to run. Writes the [snapshot] +# and [storage] sections of config.ini and the executor builds it accepts (the previous file is +# kept as config.ini.bak-), can replace the executor jars, and can start the node. +# +# ./setup.sh asks, showing each default: Enter keeps it +# ./setup.sh --defaults takes every default without asking +# +# Runs with the bash of Linux and of macOS (3.2). +set -eu + +cd "$(dirname "$0")" + +defaults=no +case "${1:-}" in + --defaults|-y) defaults=yes ;; + "") ;; + *) echo "usage: $0 [--defaults]"; exit 2 ;; +esac + +node=./node +config=config.ini +[ -x "$node" ] || { echo "No node binary here: run setup.sh from the csnode folder of the package."; exit 1; } +[ -f "$config" ] || { echo "No $config here: run setup.sh from the csnode folder of the package."; exit 1; } + +# ask "question" default choice... -> sets $answer to one of the choices (the first letters are enough) +ask() { + local question=$1 default=$2 reply c + shift 2 + while :; do + if [ "$defaults" = yes ]; then + answer=$default + echo "$question [$default]: $default" + return + fi + printf '%s [%s]: ' "$question" "$default" + read -r reply || reply= + reply=$(printf '%s' "$reply" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]') + [ -n "$reply" ] || { answer=$default; return; } + for c in "$@"; do + case "$c" in "$reply"*) answer=$c; return ;; esac + done + echo " Please answer one of: $*" + done +} + +# the backend of an existing database (as the node recognises it), empty when there is none +existing_backend() { + if [ -f db/.backend ]; then tr -d '[:space:]' < db/.backend; return; fi + if [ -f db/blockchain.db ] || [ -f db/sequence.db ]; then echo berkeleydb; return; fi + if [ -f db/lmdb/data.mdb ]; then echo lmdb; return; fi + if [ -f db/CURRENT ]; then echo rocksdb; return; fi +} + +# the value of key in [section] of the current config.ini, empty when not set +current() { + awk -v s="[$1]" -v k="$2" ' + /^[[:space:]]*\[/ { in_s = ($0 ~ "^[[:space:]]*\\[" substr(s, 2, length(s) - 2) "\\]") ; next } + in_s && $0 ~ "^[[:space:]]*" k "[[:space:]]*=" { sub(/^[^=]*=[[:space:]]*/, ""); sub(/[[:space:]]+$/, ""); v = $0 } + END { print v } + ' "$config" | tr -d '\r' +} + +# --- contract executor --------------------------------------------------------------------------- +# The node runs smart contracts with contract-executor.jar (it loads sc-api-support.jar from the same +# folder) and starts it only if the build the jar reports (its git commit count) is within +# [executor_commit_min, executor_commit_max] of [api]; otherwise it stops the executor and runs no +# contract. The official release is build 1518; community builds add fixes on top and count higher. +official_build=1518 +official_url=https://github.com/CREDITSCOM/node/releases/download/mainnet-4.2.532.0/Ubuntu20_Mainnet-4.2.532.0.tar.gz +official_sha_executor=592510d1586818348c65bea9b443e7f58aa6947113e43aa8906af8426aeef028 +official_sha_support=395d9a108a66fe1d92c1d2096a9d11f4c7662b51b47af792db0ff4cc91df9807 +community_url=https://github.com/EnzinoBB/contract-executor/releases/latest/download + +# the java of executor_command, to find the JDK's jar tool when neither unzip nor python3 is there +executor_java() { + current api executor_command | awk '{ print $1 }' +} + +# the build (git commit count) an executor jar reports, empty when it cannot be read +jar_build() { + local jar=$1 props="" java tmp + [ -f "$jar" ] || return 0 + if command -v unzip >/dev/null 2>&1; then + props=$(unzip -p "$jar" git.properties 2>/dev/null || true) + elif command -v python3 >/dev/null 2>&1; then + props=$(python3 -c 'import sys, zipfile; print(zipfile.ZipFile(sys.argv[1]).read("git.properties").decode())' "$jar" 2>/dev/null || true) + else + java=$(executor_java) + case "$java" in /*) ;; *) java="$(pwd)/$java" ;; esac + if [ -n "$java" ] && [ -x "$(dirname "$java")/jar" ]; then + tmp=$(mktemp -d) + (cd "$tmp" && "$(dirname "$java")/jar" xf "$OLDPWD/$jar" git.properties 2>/dev/null) || true + props=$(cat "$tmp/git.properties" 2>/dev/null || true) + rm -rf "$tmp" + fi + fi + printf '%s\n' "$props" | sed -n 's/^git.total.commit.count=//p' | tr -d '\r' +} + +sha256() { + if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{ print $1 }'; else shasum -a 256 "$1" | awk '{ print $1 }'; fi +} + +describe_build() { + case "$1" in + "") echo "none or unreadable" ;; + "$official_build") echo "build $1 (official)" ;; + *) [ "$1" -gt "$official_build" ] 2>/dev/null && echo "build $1 (community)" || echo "build $1" ;; + esac +} + +# downloads the jars of the chosen executor into $1; prints the reason and fails when it cannot +download_executor() { + local kind=$1 dir=$2 + command -v curl >/dev/null 2>&1 || { echo " curl is not installed."; return 1; } + if [ "$kind" = community ]; then + curl -fsSL -o "$dir/contract-executor.jar" "$community_url/contract-executor.jar" \ + && curl -fsSL -o "$dir/sc-api-support.jar" "$community_url/sc-api-support.jar" \ + && curl -fsSL -o "$dir/SHA256SUMS" "$community_url/SHA256SUMS" \ + || { echo " The download from GitHub failed."; return 1; } + for f in contract-executor.jar sc-api-support.jar; do + grep -q "^$(sha256 "$dir/$f") *\*\{0,1\}$f\$" "$dir/SHA256SUMS" \ + || { echo " $f does not match the SHA256SUMS of the release."; return 1; } + done + else + echo " The jars come from the official release package (about 350 MB to download, only they are kept)." + curl -fsSL "$official_url" | tar -xzf - -C "$dir" csnode/contract-executor.jar csnode/sc-api-support.jar \ + || { echo " The download of the official release failed."; return 1; } + mv "$dir/csnode/contract-executor.jar" "$dir/csnode/sc-api-support.jar" "$dir/" + [ "$(sha256 "$dir/contract-executor.jar")" = "$official_sha_executor" ] \ + && [ "$(sha256 "$dir/sc-api-support.jar")" = "$official_sha_support" ] \ + || { echo " The jars of the official release do not have the expected checksums."; return 1; } + fi +} + +# sets key=value in [api] of config.ini, adding the key at the end of the section when missing +set_api_key() { + local tmp="$config.tmp.$$" + awk -v k="$1" -v v="$2" ' + /^[[:space:]]*\[/ { if (in_api && !done) { print k "=" v; done = 1 } in_api = ($0 ~ /^[[:space:]]*\[api\]/) } + in_api && $0 ~ "^[[:space:]]*" k "[[:space:]]*=" { if (!done) print k "=" v; done = 1; next } + { print } + END { if (in_api && !done) print k "=" v } + ' "$config" > "$tmp" && mv "$tmp" "$config" +} + +echo "Credits node set-up ($(cat VERSION 2>/dev/null || echo 'unknown version'))" +echo "Each question shows the default in brackets: press Enter to keep it." +echo + +have=$(existing_backend) +if [ -n "$have" ]; then + echo "This folder already holds a $have database: it is kept." + echo +fi + +# defaults from the current configuration, so that running the set-up again changes nothing by Enter +cur_backend=$(current storage db_backend) +[ -n "$cur_backend" ] || cur_backend=$(current storage block_db) +cur_prune=$(current storage prune_keep_blocks) +cur_serve=$(current snapshot serve) + +# 1. pruned or full +storage_default=pruned +if [ -n "$cur_backend$cur_prune" ] && { [ -z "$cur_prune" ] || [ "$cur_prune" = 0 ]; }; then storage_default=full; fi +[ "$have" != berkeleydb ] || storage_default=full +echo "1. Storage" +echo " pruned: keeps only recent blocks (a few GB), starts in minutes from a snapshot. Enough to run a node." +echo " full: keeps every block since the start of the chain (tens of GB), for history and explorers." +ask " Pruned or full node? (pruned/full)" "$storage_default" pruned full +storage=$answer +if [ "$storage" = full ] && [ -n "$have" ] && [ -n "$cur_prune" ] && [ "$cur_prune" != 0 ]; then + echo " This folder holds a pruned database: its removed blocks are gone. To get a full node," + echo " move db, caches and qs away and run the set-up again on the empty folder." +fi +echo + +# 2. database +if [ "$storage" = pruned ]; then + backend_default=lmdb + [ "$cur_backend" != rocksdb ] && [ "$have" != rocksdb ] || backend_default=rocksdb + echo "2. Database" + echo " lmdb: the usual choice for pruned nodes." + echo " rocksdb: compressed, smaller on disk, a little more CPU." + ask " Database? (lmdb/rocksdb)" "$backend_default" lmdb rocksdb +else + backend_default=berkeleydb + case "$cur_backend" in lmdb|rocksdb) [ "$cur_prune" != "" ] && [ "$cur_prune" != 0 ] || backend_default=$cur_backend ;; esac + [ -z "$have" ] || backend_default=$have + echo "2. Database" + echo " berkeleydb: the official node's format (the official node can open this folder too)." + echo " rocksdb: the smallest full node (about a third of berkeleydb)." + echo " lmdb: fast, larger on disk." + ask " Database? (berkeleydb/rocksdb/lmdb)" "$backend_default" berkeleydb rocksdb lmdb +fi +backend=$answer +echo + +convert=no +if [ -n "$have" ] && [ "$have" != "$backend" ]; then + echo " The database in this folder is $have, you chose $backend." + if [ "$backend" = berkeleydb ] && [ "$storage" = pruned ]; then + echo " A pruned node cannot use berkeleydb." + fi + ask " Convert it to $backend at the next start (it can take hours)? (yes/no)" yes yes no + if [ "$answer" = yes ]; then + convert=yes + else + backend=$have + echo " Keeping $have." + fi + echo +fi + +# 3. how the first start gets the chain (only for an empty folder) +first=keep +if [ -z "$have" ]; then + echo "3. First start" + if [ "$backend" = berkeleydb ]; then + echo " A berkeleydb node syncs from block 0 (about 35 hours on mainnet)." + first=sync + else + echo " github: download the snapshot published on GitHub now, check it and restore it (minutes)." + echo " network: the node fetches a snapshot from other nodes that share it; it needs at least two" + echo " of them, and few nodes share yet: without them it syncs from block 0 after 10 minutes." + echo " sync: sync from block 0 by itself (about 35 hours on mainnet)." + first_default=github + if [ "$storage" = full ]; then + echo " A full node started from a snapshot holds the blocks from the snapshot on only." + first_default=sync + fi + ask " How should the node get the chain? (github/network/sync)" "$first_default" github network sync + first=$answer + fi + echo +fi + +# 4. sharing snapshots +serve_default=yes +[ "$cur_serve" != off ] || serve_default=no +echo "4. Sharing" +echo " The node can share snapshots of the chain with new nodes, so that they start in minutes." +echo " It uses at most 2 MB/s of upload, only while the node is synced, and about 200 MB of disk." +ask " Share snapshots with other nodes? (yes/no)" "$serve_default" yes no +serve=$answer +echo + +# 5. the contract executor (when config.ini runs one) +executor_choice=none +executor_action=keep +if [ -n "$(current api executor_command)" ]; then + have_build=$(jar_build contract-executor.jar) + support_build=$(jar_build sc-api-support.jar) + cur_min=$(current api executor_commit_min) + cur_max=$(current api executor_commit_max) + echo "5. Contract executor" + echo " The node runs smart contracts with contract-executor.jar and sc-api-support.jar, and starts" + echo " the executor only if its build is within executor_commit_min..executor_commit_max of config.ini." + echo " In this folder: $(describe_build "$have_build"); config.ini accepts ${cur_min:-1506}..${cur_max:-any}." + if [ -n "$have_build" ] && [ -n "$support_build" ] && [ "$have_build" != "$support_build" ]; then + echo " sc-api-support.jar is $(describe_build "$support_build"): the two jars come from different packages." + fi + echo " community: the latest community build, the official one with fixes (code of a contract that never" + echo " returns is stopped, tokens of every standard are reported to the API). Contracts give" + echo " the same results as with the official one." + echo " official: build $official_build of the official release." + # unattended (--defaults), nothing is downloaded: the jars in the folder are kept + download_default=yes + [ "$defaults" = no ] || download_default=no + executor_default=official + if [ -n "$have_build" ] && [ "$have_build" -gt "$official_build" ] 2>/dev/null; then executor_default=community; fi + ask " Which executor? (community/official)" "$executor_default" community official + executor_choice=$answer + if [ "$executor_choice" = community ] && { [ -z "$have_build" ] || [ "$have_build" -le "$official_build" ] 2>/dev/null; }; then + echo " The jars in this folder are not a community build (copying only the node binary keeps the old ones)." + ask " Download the latest community executor from GitHub now (about 25 MB)? (yes/no)" "$download_default" yes no + [ "$answer" = yes ] && executor_action=download || executor_action=manual + elif [ "$executor_choice" = official ] && [ "$have_build" != "$official_build" ]; then + if [ -f contract-executor.jar.official ] && [ -f sc-api-support.jar.official ]; then + echo " The official jars set aside by an earlier set-up are here: they are put back." + executor_action=restore + else + ask " Download the official executor now (from the official release, about 350 MB)? (yes/no)" "$download_default" yes no + [ "$answer" = yes ] && executor_action=download || executor_action=manual + fi + fi + echo +fi + +# 6. start now +ask "6. Start the node when the set-up is done? (yes/no)" yes yes no +start=$answer +echo + +echo "Summary" +echo " storage: $storage" +echo " database: $backend$( [ "$convert" = yes ] && echo " (converted from $have at the next start)")" +case "$first" in + keep) echo " first start: the database in this folder is kept" ;; + network) echo " first start: snapshot from other nodes" ;; + github) echo " first start: snapshot downloaded from GitHub now" ;; + sync) echo " first start: sync from block 0" ;; +esac +echo " share: $serve" +case "$executor_choice/$executor_action" in + none/*) ;; + */keep) echo " executor: $executor_choice, the jars in this folder are kept" ;; + */download) echo " executor: $executor_choice, downloaded now (the jars in this folder are set aside)" ;; + */restore) echo " executor: official, the jars set aside before are put back" ;; + */manual) echo " executor: $executor_choice, to copy by hand (see below)" ;; +esac +echo " start now: $start" +if [ "$defaults" = no ]; then + ask "Write config.ini? (yes/no)" yes yes no + [ "$answer" = yes ] || { echo "Nothing changed."; exit 0; } +fi + +backup="$config.bak-$(date +%Y%m%d-%H%M%S)" +cp -p "$config" "$backup" + +case "$first" in network) fetch=auto ;; *) fetch=off ;; esac +[ "$serve" = yes ] && serve_value=on || serve_value=off + +# config.ini without its [snapshot] and [storage] sections, then the new ones ([snapshot] first) +tmp="$config.tmp.$$" +awk ' + /^[[:space:]]*\[/ { skip = ($0 ~ /^[[:space:]]*\[(snapshot|storage)\]/) } + !skip && !/^; written by setup\.(sh|bat) on / { print } +' "$backup" | tr -d '\r' | awk 'NF { blank = 0; print; next } !blank++ { print }' > "$tmp" +{ + echo + echo "; written by setup.sh on $(date '+%Y-%m-%d %H:%M'), the previous file is $backup" + echo "[snapshot]" + echo "serve=$serve_value" + echo "fetch=$fetch" + echo + echo "[storage]" + echo "db_backend=$backend" + if [ "$storage" = pruned ]; then + echo "checkpoint_every=100000" + echo "checkpoint_keep=2" + echo "prune_keep_blocks=1000" + fi +} >> "$tmp" +mv "$tmp" "$config" +echo "config.ini written; the previous one is $backup" + +# the executor: put the chosen jars in place, then make config.ini accept their build +if [ "$executor_choice" != none ]; then + stamp=$(date +%Y%m%d-%H%M%S) + # keeps the jars in this folder aside: as .official when they are the official ones (a later + # set-up puts them back without a download), as .bak- otherwise + set_aside() { + local suffix=".bak-$stamp" + [ "$(jar_build contract-executor.jar)" != "$official_build" ] || suffix=.official + for f in contract-executor.jar sc-api-support.jar; do + [ ! -f "$f" ] || mv "$f" "$f$suffix" + done + echo "The previous executor jars are kept as contract-executor.jar$suffix and sc-api-support.jar$suffix." + } + case "$executor_action" in + download) + echo + echo "Downloading the $executor_choice executor..." + dl=$(mktemp -d) + if download_executor "$executor_choice" "$dl"; then + set_aside + mv "$dl/contract-executor.jar" "$dl/sc-api-support.jar" . + echo "contract-executor.jar and sc-api-support.jar replaced: $(describe_build "$(jar_build contract-executor.jar)")." + else + executor_action=manual + echo "The jars in this folder are kept." + fi + rm -rf "$dl" + ;; + restore) + set_aside + mv contract-executor.jar.official contract-executor.jar + mv sc-api-support.jar.official sc-api-support.jar + echo "The official executor jars are back." + ;; + esac + build=$(jar_build contract-executor.jar) + if [ -n "$build" ]; then + min=$(current api executor_commit_min) + max=$(current api executor_commit_max) + if [ -n "$min" ] && [ "$build" -lt "$min" ] 2>/dev/null; then set_api_key executor_commit_min "$build"; echo "config.ini: executor_commit_min=$build"; fi + if [ -n "$max" ] && [ "$max" != -1 ] && [ "$build" -gt "$max" ] 2>/dev/null; then set_api_key executor_commit_max "$build"; echo "config.ini: executor_commit_max=$build (it was $max: the node would not start this executor)"; fi + fi + if [ "$executor_action" = manual ]; then + echo + echo "To use the $executor_choice executor, with the node stopped:" + if [ "$executor_choice" = community ]; then + echo " copy contract-executor.jar and sc-api-support.jar, both, from the csnode folder of the community" + echo " package (or from $community_url/...) into $(pwd)," + else + echo " copy contract-executor.jar and sc-api-support.jar, both, from the official release" + echo " ($official_url) into $(pwd)," + fi + echo " then run this set-up again: it makes config.ini accept their build (executor_commit_min/max)." + fi + echo + echo "Updating this folder later: a new node binary is not enough when its package brings a new executor." + echo "Copy node, contract-executor.jar and sc-api-support.jar together, then run the set-up again." +fi + +if [ "$first" = github ]; then + echo + echo "Downloading the published snapshot..." + if ! "$node" --config-file "$config" --download-snapshot; then + echo "The download did not finish. Run ./setup.sh again, or start the node: it can get the snapshot from other nodes." + exit 1 + fi +fi + +args="--config-file $config" +[ "$convert" = no ] || args="$args --convert-db" +if [ "$start" = yes ]; then + echo + echo "Starting the node (Ctrl+C stops it and saves its state)..." + # shellcheck disable=SC2086 + exec "$node" $args +fi +echo +echo "Start the node with: cd \"$(pwd)\" && $node $args"