diff --git a/.github/script/amd64-18.04.Dockerfile b/.github/script/amd64-18.04.Dockerfile
new file mode 100644
index 00000000..e11678b0
--- /dev/null
+++ b/.github/script/amd64-18.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:18.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id create-hardfork dht-server lite-client
\ No newline at end of file
diff --git a/.github/script/amd64-20.04.Dockerfile b/.github/script/amd64-20.04.Dockerfile
new file mode 100644
index 00000000..eb7fa890
--- /dev/null
+++ b/.github/script/amd64-20.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:20.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id create-hardfork dht-server lite-client
\ No newline at end of file
diff --git a/.github/script/amd64-22.04.Dockerfile b/.github/script/amd64-22.04.Dockerfile
new file mode 100644
index 00000000..68d1f652
--- /dev/null
+++ b/.github/script/amd64-22.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:22.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id create-hardfork dht-server lite-client
\ No newline at end of file
diff --git a/.github/script/arm64-18.04.Dockerfile b/.github/script/arm64-18.04.Dockerfile
new file mode 100644
index 00000000..88b6a77e
--- /dev/null
+++ b/.github/script/arm64-18.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:18.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DTON_ARCH= ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id dht-server lite-client
\ No newline at end of file
diff --git a/.github/script/arm64-20.04.Dockerfile b/.github/script/arm64-20.04.Dockerfile
new file mode 100644
index 00000000..267f97ac
--- /dev/null
+++ b/.github/script/arm64-20.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:20.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DTON_ARCH= ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id dht-server lite-client
\ No newline at end of file
diff --git a/.github/script/arm64-22.04.Dockerfile b/.github/script/arm64-22.04.Dockerfile
new file mode 100644
index 00000000..47d24821
--- /dev/null
+++ b/.github/script/arm64-22.04.Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:22.04
+
+RUN apt update
+RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
+RUN apt install -y build-essential cmake clang openssl libssl-dev zlib1g-dev gperf wget git curl libreadline-dev ccache libmicrohttpd-dev ninja-build
+
+WORKDIR /
+
+RUN git clone --recurse-submodules https://github.com/ton-blockchain/ton.git
+
+WORKDIR /ton
+RUN mkdir /ton/build
+WORKDIR /ton/build
+ENV CC clang
+ENV CXX clang++
+ENV CCACHE_DISABLE 1
+RUN cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DTON_ARCH= ..
+RUN ninja tonlibjson blockchain-explorer fift func validator-engine validator-engine-console create-state generate-random-id dht-server lite-client
\ No newline at end of file
diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml
new file mode 100644
index 00000000..089af583
--- /dev/null
+++ b/.github/workflows/create-release.yml
@@ -0,0 +1,167 @@
+name: Create release
+
+on: [workflow_dispatch]
+
+permissions: write-all
+
+jobs:
+ create-release:
+ runs-on: ubuntu-22.04
+
+ steps:
+ - uses: actions/checkout@v3
+
+ - name: Show all artifacts
+ run: |
+ mkdir artifacts
+ ls -lart artifacts
+
+ - name: Download Ubuntu x86-64 artifacts
+ uses: dawidd6/action-download-artifact@v2
+ with:
+ workflow: ubuntu-compile.yml
+ path: artifacts
+ workflow_conclusion: success
+ skip_unpack: true
+
+ - name: Download Ubuntu arm64 artifacts
+ uses: dawidd6/action-download-artifact@v2
+ with:
+ workflow: docker-compile-ubuntu.yml
+ path: artifacts
+ workflow_conclusion: success
+ skip_unpack: true
+
+ - name: Download MacOS 11.7 artifacts
+ uses: dawidd6/action-download-artifact@v2
+ with:
+ workflow: macos-11.7-compile.yml
+ path: artifacts
+ workflow_conclusion: success
+ skip_unpack: true
+
+ - name: Download MacOS 12.6 artifacts
+ uses: dawidd6/action-download-artifact@v2
+ with:
+ workflow: macos-12.6-compile.yml
+ path: artifacts
+ workflow_conclusion: success
+ skip_unpack: true
+
+ - name: Download Windows artifacts
+ uses: dawidd6/action-download-artifact@v2
+ with:
+ workflow: win-2019-compile.yml
+ path: artifacts
+ workflow_conclusion: success
+ skip_unpack: true
+
+ - name: Show all artifacts
+ run: |
+ tree artifacts
+
+# create release
+
+ - name: Read Changelog.md and use it as a body of new release
+ id: read_release
+ shell: bash
+ run: |
+ r=$(cat Changelog.md)
+ r="${r//'%'/'%25'}"
+ r="${r//$'\n'/'%0A'}"
+ r="${r//$'\r'/'%0D'}"
+ echo "::set-output name=CHANGELOG_BODY::$r"
+
+ - name: Get current date
+ id: date
+ run: echo "::set-output name=date::$(date +'%Y.%m')"
+
+ - name: Get registration token
+ id: getRegToken
+ run: |
+ curl -X POST -H \"Accept: application/vnd.github+json\" -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' https://api.github.com/repos/neodix42/HardTestDevelopment/actions/runners/registration-token
+
+ - name: Create release
+ id: create_release
+ uses: actions/create-release@v1
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ with:
+ tag_name: v${{ steps.date.outputs.date }}
+ release_name: v${{ steps.date.outputs.date }}
+ body: |
+ ${{ steps.read_release.outputs.CHANGELOG_BODY }}
+ draft: false
+ prerelease: false
+
+ - name: Upload Windows 2019 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-win-binaries.zip
+ asset_name: ton-windows-2019-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload MacOS 11.7 x86-64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-macos-11.7.zip
+ asset_name: ton-macos-11.7-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload MacOS 12.6 x86-64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-macos-12.6.zip
+ asset_name: ton-macos-12.6-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 18.04 x86-64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-binaries-ubuntu-18.04.zip
+ asset_name: ton-ubuntu-18.04-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 20.04 x86-64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-binaries-ubuntu-20.04.zip
+ asset_name: ton-ubuntu-20.04-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 22.04 x86-64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-binaries-ubuntu-22.04.zip
+ asset_name: ton-ubuntu-22.04-x86-64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 18.04 arm64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-ubuntu-18.04-arm64.zip
+ asset_name: ton-ubuntu-18.04-arm64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 20.04 arm64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-ubuntu-20.04-arm64.zip
+ asset_name: ton-ubuntu-20.04-arm64.zip
+ tag: v${{ steps.date.outputs.date }}
+
+ - name: Upload Ubuntu 22.04 arm64 artifacts
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: artifacts/ton-ubuntu-22.04-arm64.zip
+ asset_name: ton-ubuntu-22.04-arm64.zip
+ tag: v${{ steps.date.outputs.date }}
\ No newline at end of file
diff --git a/.github/workflows/docker-compile-ubuntu.yml b/.github/workflows/docker-compile-ubuntu.yml
new file mode 100644
index 00000000..9c02625e
--- /dev/null
+++ b/.github/workflows/docker-compile-ubuntu.yml
@@ -0,0 +1,47 @@
+name: Docker Ubuntu Compile arm64
+
+on: [push,workflow_dispatch,workflow_call]
+
+jobs:
+ build:
+ strategy:
+ fail-fast: false
+ max-parallel: 3
+ matrix:
+ arch: [arm64]
+ ver: [22.04, 18.04, 20.04 ]
+
+ runs-on: ubuntu-22.04
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v3
+
+ - name: Set up QEMU
+ uses: docker/setup-qemu-action@v2
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v2
+
+ - name: Build with docker buildx
+ run: |
+ mkdir build-${{matrix.ver}}-${{matrix.arch}}
+ docker buildx build --platform=linux/${{matrix.arch}} --progress=plain --load . -t build-${{matrix.ver}}-${{matrix.arch}} -f .github/script/${{matrix.arch}}-${{matrix.ver}}.Dockerfile
+ container_id=$(docker create --platform=linux/${{matrix.arch}} build-${{matrix.ver}}-${{matrix.arch}})
+ docker cp $container_id:/ton/build/dht-server/dht-server build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/validator-engine/validator-engine build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/validator-engine-console/validator-engine-console build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/crypto/fift build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/crypto/func build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/crypto/create-state build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/blockchain-explorer/blockchain-explorer build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/lite-client/lite-client build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/utils/generate-random-id build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/build/tonlib/libtonlibjson.so.0.5 build-${{matrix.ver}}-${{matrix.arch}}/tonlibjson.so
+ docker cp -a $container_id:/ton/crypto/smartcont build-${{matrix.ver}}-${{matrix.arch}}/
+ docker cp -a $container_id:/ton/crypto/fift/lib build-${{matrix.ver}}-${{matrix.arch}}/
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@v1
+ with:
+ name: ton-ubuntu-${{matrix.ver}}-${{matrix.arch}}
+ path: build-${{matrix.ver}}-${{matrix.arch}}
diff --git a/.github/workflows/docker-ubuntu-image.yml b/.github/workflows/docker-ubuntu-image.yml
index ad83c099..b28cb381 100644
--- a/.github/workflows/docker-ubuntu-image.yml
+++ b/.github/workflows/docker-ubuntu-image.yml
@@ -1,4 +1,4 @@
-name: Docker Ubuntu 18.04 image
+name: Docker Ubuntu 20.04 image
on:
workflow_dispatch:
@@ -10,10 +10,10 @@ env:
jobs:
build-and-push:
- runs-on: ubuntu-18.04
+ runs-on: ubuntu-20.04
steps:
- name: Checkout
- uses: actions/checkout@v2
+ uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
diff --git a/.github/workflows/macos-10.15-compile.yml b/.github/workflows/macos-10.15-compile.yml
deleted file mode 100644
index 5f2d94b0..00000000
--- a/.github/workflows/macos-10.15-compile.yml
+++ /dev/null
@@ -1,41 +0,0 @@
-name: C/C++ CI macOS-10.15 Compile
-
-on:
- workflow_dispatch:
- push:
-
-jobs:
- build:
-
- runs-on: macos-10.15
-
- steps:
- - name: Check out repository
- uses: actions/checkout@v2
- with:
- submodules: 'recursive'
- - name: Compile OpenSSL
- run: |
- git clone https://github.com/openssl/openssl openssl_1_1_1
- cd openssl_1_1_1
- git checkout OpenSSL_1_1_1-stable
- ./Configure --prefix=/usr/local/macos darwin64-x86_64-cc -static -mmacosx-version-min=10.15
- make build_libs -j4
-
- - name: Build all
- run: |
- rootPath=`pwd`
- mkdir build
- cd build
- cmake -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=$rootPath/openssl_1_1_1/include -DOPENSSL_CRYPTO_LIBRARY=$rootPath/openssl_1_1_1/libcrypto.a -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=10.15 -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_BUILD_TYPE=Release ..
- make -j4
-
- - name: Find & copy binaries
- run: |
- rsync -r --exclude 'CMakeFiles' --exclude 'Makefile' --exclude '*.a' --exclude '*.cmake' --exclude 'third-party' --exclude 'test-*' --exclude '*.cc' --exclude '*.json' --exclude '*.txt' build/* artifacts/
-
- - name: Upload artifacts
- uses: actions/upload-artifact@master
- with:
- name: ton-macos-binaries
- path: artifacts
diff --git a/.github/workflows/macos-10.15-tonlib-java.yml b/.github/workflows/macos-10.15-tonlib-java.yml
deleted file mode 100644
index e5199f58..00000000
--- a/.github/workflows/macos-10.15-tonlib-java.yml
+++ /dev/null
@@ -1,60 +0,0 @@
-name: macOS-10.15 tonlib-java
-
-
-on:
- workflow_dispatch:
- push:
- branches:
- - 'wallets'
-jobs:
- build:
-
- runs-on: macos-10.15
-
- steps:
- - name: Check out repository
- uses: actions/checkout@v2
- with:
- submodules: 'recursive'
-
- - name: Compile OpenSSL
- run: |
- git clone https://github.com/openssl/openssl openssl_1_1_1
- cd openssl_1_1_1
- git checkout OpenSSL_1_1_1-stable
- ./Configure --prefix=/usr/local/macos darwin64-x86_64-cc -static -mmacosx-version-min=10.15
- make build_libs -j4
-
- - name: Configure & Build
- run: |
- rootPath=`pwd`
-
- export CC=$(which clang)
- export CXX=$(which clang++)
- export CCACHE_DISABLE=1
-
- export JAVA_AWT_LIBRARY=NotNeeded
- export JAVA_JVM_LIBRARY=NotNeeded
- export JAVA_INCLUDE_PATH=${JAVA_HOME}/include
- export JAVA_AWT_INCLUDE_PATH=${JAVA_HOME}/include
- export JAVA_INCLUDE_PATH2=${JAVA_HOME}/include/darwin
-
- cd example/android/
- mkdir build
- cd build
-
- cmake -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=$rootPath/openssl_1_1_1/include -DOPENSSL_CRYPTO_LIBRARY=$rootPath/openssl_1_1_1/libcrypto.a -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=10.15 -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DTON_ONLY_TONLIB=ON ..
- cmake --build . --target prepare_cross_compiling
- cmake --build . --target native-lib --config Release
-
- - name: find & copy binaries
- run: |
- mkdir -p artifacts/tonlib-java
- cp example/android/src/drinkless/org/ton/TonApi.java artifacts/tonlib-java/
- cp example/android/build/libnative-lib.dylib artifacts/tonlib-java/
-
- - name: Upload artifacts
- uses: actions/upload-artifact@master
- with:
- name: tonlib-macos-java
- path: artifacts
diff --git a/.github/workflows/macos-11.7-compile.yml b/.github/workflows/macos-11.7-compile.yml
new file mode 100644
index 00000000..59c50539
--- /dev/null
+++ b/.github/workflows/macos-11.7-compile.yml
@@ -0,0 +1,59 @@
+name: MacOS 11.7 Big Sur x86-64 Compile
+
+on: [push,workflow_dispatch,workflow_call]
+
+jobs:
+ build:
+
+ runs-on: macos-11
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@v3
+ with:
+ submodules: 'recursive'
+ - name: Compile OpenSSL
+ run: |
+ git clone https://github.com/openssl/openssl openssl_1_1_1
+ cd openssl_1_1_1
+ git checkout OpenSSL_1_1_1-stable
+ ./Configure --prefix=/usr/local/macos darwin64-x86_64-cc -static -mmacosx-version-min=11.7
+ make build_libs -j4
+
+ - name: Build all
+ run: |
+ export NONINTERACTIVE=1
+ brew install ninja
+ rootPath=`pwd`
+ mkdir build
+ cd build
+ cmake -GNinja -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=$rootPath/openssl_1_1_1/include -DOPENSSL_CRYPTO_LIBRARY=$rootPath/openssl_1_1_1/libcrypto.a -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=11.7 -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_BUILD_TYPE=Release ..
+ ninja fift func tonlib tonlibjson tonlib-cli validator-engine lite-client pow-miner validator-engine-console generate-random-id json2tlo dht-server http-proxy rldp-http-proxy adnl-proxy create-state create-hardfork tlbc
+
+ - name: Find & copy binaries
+ run: |
+ mkdir artifacts
+ cp build/crypto/fift artifacts/
+ cp build/crypto/func artifacts/
+ cp build/crypto/create-state artifacts/
+ cp build/crypto/tlbc artifacts/
+ cp build/validator-engine-console/validator-engine-console artifacts/
+ cp build/tonlib/tonlib-cli artifacts/
+ cp build/tonlib/libtonlibjson.0.5.dylib artifacts/
+ cp build/http/http-proxy artifacts/
+ cp build/rldp-http-proxy/rldp-http-proxy artifacts/
+ cp build/dht-server/dht-server artifacts/
+ cp build/lite-client/lite-client artifacts/
+ cp build/validator-engine/validator-engine artifacts/
+ cp build/utils/generate-random-id artifacts/
+ cp build/utils/json2tlo artifacts/
+ cp build/adnl/adnl-proxy artifacts/
+ rsync -r crypto/smartcont artifacts/
+ rsync -r crypto/fift/lib artifacts/
+ ls -laRt artifacts
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@master
+ with:
+ name: ton-macos-11.7
+ path: artifacts
diff --git a/.github/workflows/macos-12.6-compile.yml b/.github/workflows/macos-12.6-compile.yml
new file mode 100644
index 00000000..f9b3ed1f
--- /dev/null
+++ b/.github/workflows/macos-12.6-compile.yml
@@ -0,0 +1,59 @@
+name: MacOS 12.6 Monterey x86-64 Compile
+
+on: [push,workflow_dispatch,workflow_call]
+
+jobs:
+ build:
+
+ runs-on: macos-12
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@v3
+ with:
+ submodules: 'recursive'
+ - name: Compile OpenSSL
+ run: |
+ git clone https://github.com/openssl/openssl openssl_1_1_1
+ cd openssl_1_1_1
+ git checkout OpenSSL_1_1_1-stable
+ ./Configure --prefix=/usr/local/macos darwin64-x86_64-cc -static -mmacosx-version-min=12.6
+ make build_libs -j4
+
+ - name: Build all
+ run: |
+ export NONINTERACTIVE=1
+ brew install ninja
+ rootPath=`pwd`
+ mkdir build
+ cd build
+ cmake -GNinja -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=$rootPath/openssl_1_1_1/include -DOPENSSL_CRYPTO_LIBRARY=$rootPath/openssl_1_1_1/libcrypto.a -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=12.6 -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_BUILD_TYPE=Release ..
+ ninja fift func tonlib tonlibjson tonlib-cli validator-engine lite-client pow-miner validator-engine-console generate-random-id json2tlo dht-server http-proxy rldp-http-proxy adnl-proxy create-state create-hardfork tlbc
+
+ - name: Find & copy binaries
+ run: |
+ mkdir artifacts
+ cp build/crypto/fift artifacts/
+ cp build/crypto/func artifacts/
+ cp build/crypto/create-state artifacts/
+ cp build/crypto/tlbc artifacts/
+ cp build/validator-engine-console/validator-engine-console artifacts/
+ cp build/tonlib/tonlib-cli artifacts/
+ cp build/tonlib/libtonlibjson.0.5.dylib artifacts/
+ cp build/http/http-proxy artifacts/
+ cp build/rldp-http-proxy/rldp-http-proxy artifacts/
+ cp build/dht-server/dht-server artifacts/
+ cp build/lite-client/lite-client artifacts/
+ cp build/validator-engine/validator-engine artifacts/
+ cp build/utils/generate-random-id artifacts/
+ cp build/utils/json2tlo artifacts/
+ cp build/adnl/adnl-proxy artifacts/
+ rsync -r crypto/smartcont artifacts/
+ rsync -r crypto/fift/lib artifacts/
+ ls -laRt artifacts
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@master
+ with:
+ name: ton-macos-12.6
+ path: artifacts
diff --git a/.github/workflows/tonlib-android-jni.yml b/.github/workflows/tonlib-android-jni.yml
new file mode 100644
index 00000000..cdc16841
--- /dev/null
+++ b/.github/workflows/tonlib-android-jni.yml
@@ -0,0 +1,56 @@
+name: Tonlib Android JNI
+
+on: [push,workflow_dispatch,workflow_call]
+
+jobs:
+ build:
+
+ runs-on: ubuntu-22.04
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@v3
+ with:
+ submodules: 'recursive'
+
+ - name: Install libraries
+ run: |
+ sudo apt update
+ sudo apt install -y build-essential git make cmake clang libgflags-dev zlib1g-dev libssl-dev libreadline-dev libmicrohttpd-dev pkg-config libgsl-dev python3 python3-dev ninja-build
+
+ - name: Configure & Build
+ run: |
+ wget https://dl.google.com/android/repository/android-ndk-r25b-linux.zip
+ unzip android-ndk-r25b-linux.zip
+ export JAVA_AWT_LIBRARY=NotNeeded
+ export JAVA_JVM_LIBRARY=NotNeeded
+ export JAVA_INCLUDE_PATH=${JAVA_HOME}/include
+ export JAVA_AWT_INCLUDE_PATH=${JAVA_HOME}/include
+ export JAVA_INCLUDE_PATH2=${JAVA_HOME}/include/linux
+
+ export ANDROID_NDK_ROOT=$(pwd)/android-ndk-r25b
+ export OPENSSL_DIR=$(pwd)/example/android/third_party/crypto
+
+ rm -rf example/android/src/drinkless/org/ton/TonApi.java
+ cd example/android/
+ cmake -GNinja -DTON_ONLY_TONLIB=ON .
+ ninja prepare_cross_compiling
+ rm CMakeCache.txt
+ ./build-all.sh
+ ../../android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip libs/x86/libnative-lib.so
+ ../../android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip libs/x86_64/libnative-lib.so
+ ../../android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip libs/armeabi-v7a/libnative-lib.so
+ ../../android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip libs/arm64-v8a/libnative-lib.so
+ find . -name "*.debug" -type f -delete
+
+ - name: Find & copy binaries
+ run: |
+ mkdir -p artifacts/tonlib-android-jni
+ cp example/android/src/drinkless/org/ton/TonApi.java artifacts/tonlib-android-jni/
+ cp -R example/android/libs/* artifacts/tonlib-android-jni/
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@master
+ with:
+ name: Tonlib JNI libraries for Android
+ path: artifacts
\ No newline at end of file
diff --git a/.github/workflows/ubuntu-18.04-compile.yml b/.github/workflows/ubuntu-18.04-compile.yml
index e215c7bd..6709bf83 100644
--- a/.github/workflows/ubuntu-18.04-compile.yml
+++ b/.github/workflows/ubuntu-18.04-compile.yml
@@ -1,6 +1,6 @@
-name: C/C++ CI Ubuntu 18.04 Compile
+name: Ubuntu 18.04 Compile
-on: [push,workflow_dispatch]
+on: [push,workflow_dispatch,workflow_call]
jobs:
build:
@@ -9,7 +9,7 @@ jobs:
steps:
- name: Check out repository
- uses: actions/checkout@v2
+ uses: actions/checkout@v3
with:
submodules: 'recursive'
@@ -32,9 +32,11 @@ jobs:
run: |
mkdir artifacts
cp build/crypto/fift build/crypto/tlbc build/crypto/func build/crypto/create-state build/validator-engine-console/validator-engine-console build/tonlib/tonlib-cli build/tonlib/libtonlibjson.so.0.5 build/http/http-proxy build/rldp-http-proxy/rldp-http-proxy build/dht-server/dht-server build/lite-client/lite-client build/validator-engine/validator-engine build/utils/generate-random-id build/utils/json2tlo build/adnl/adnl-proxy artifacts
+ cp -R crypto/smartcont artifacts/
+ cp -R crypto/fift/lib artifacts/
- name: Upload artifacts
uses: actions/upload-artifact@master
with:
- name: ton-binaries
+ name: ton-ubuntu-binaries
path: artifacts
diff --git a/.github/workflows/ubuntu-18.04-ton-ccpcheck.yml b/.github/workflows/ubuntu-18.04-ton-ccpcheck.yml
index e8d4548e..f440d7a5 100644
--- a/.github/workflows/ubuntu-18.04-ton-ccpcheck.yml
+++ b/.github/workflows/ubuntu-18.04-ton-ccpcheck.yml
@@ -1,8 +1,6 @@
-name: Ubuntu 18.04 TON ccpcheck
+name: TON ccpcheck
-on:
- workflow_dispatch:
- push:
+on: [push,workflow_dispatch,workflow_call]
jobs:
build:
@@ -11,7 +9,7 @@ jobs:
steps:
- name: Check out repository
- uses: actions/checkout@v2
+ uses: actions/checkout@v3
with:
submodules: 'recursive'
diff --git a/.github/workflows/ubuntu-18.04-tonlib-java.yml b/.github/workflows/ubuntu-18.04-tonlib-java.yml
deleted file mode 100644
index 8c746b26..00000000
--- a/.github/workflows/ubuntu-18.04-tonlib-java.yml
+++ /dev/null
@@ -1,50 +0,0 @@
-name: Ubuntu 18.04 tonlib-java
-
-on:
- workflow_dispatch:
- push:
- branches:
- - 'wallets'
-
-jobs:
- build:
-
- runs-on: ubuntu-18.04
-
- steps:
- - name: Check out repository
- uses: actions/checkout@v2
- with:
- submodules: 'recursive'
-
- - name: Install libraries
- run: |
- sudo apt update
- sudo apt install -y build-essential git make cmake clang libgflags-dev zlib1g-dev libssl-dev libreadline-dev libmicrohttpd-dev pkg-config libgsl-dev python3 python3-dev ninja-build
-
- - name: Configure & Build
- run: |
- export JAVA_AWT_LIBRARY=NotNeeded
- export JAVA_JVM_LIBRARY=NotNeeded
- export JAVA_INCLUDE_PATH=${JAVA_HOME}/include
- export JAVA_AWT_INCLUDE_PATH=${JAVA_HOME}/include
- export JAVA_INCLUDE_PATH2=${JAVA_HOME}/include/linux
-
- cd example/android/
- mkdir build
- cd build
- cmake -DTON_ONLY_TONLIB=ON ..
- cmake --build . --target prepare_cross_compiling
- cmake --build . --target native-lib
-
- - name: find & copy binaries
- run: |
- mkdir -p artifacts/tonlib-java
- cp example/android/src/drinkless/org/ton/TonApi.java artifacts/tonlib-java/
- cp example/android/build/libnative-lib.so artifacts/tonlib-java/
-
- - name: Upload artifacts
- uses: actions/upload-artifact@master
- with:
- name: tonlib-ubuntu-java
- path: artifacts
\ No newline at end of file
diff --git a/.github/workflows/ubuntu-compile.yml b/.github/workflows/ubuntu-compile.yml
index 2862a471..653d7c0a 100644
--- a/.github/workflows/ubuntu-compile.yml
+++ b/.github/workflows/ubuntu-compile.yml
@@ -1,6 +1,6 @@
-name: Ubuntu Compile
+name: Ubuntu Compile x86-64
-on: [push,workflow_dispatch]
+on: [push,workflow_dispatch,workflow_call]
jobs:
build:
@@ -12,7 +12,7 @@ jobs:
steps:
- name: Check out repository
- uses: actions/checkout@v2
+ uses: actions/checkout@v3
with:
submodules: 'recursive'
@@ -29,10 +29,12 @@ jobs:
cd build-${{ matrix.os }}
cmake -GNinja -DCMAKE_BUILD_TYPE=Release ..
ninja fift func tonlib tonlibjson tonlib-cli validator-engine lite-client pow-miner validator-engine-console generate-random-id json2tlo dht-server http-proxy rldp-http-proxy adnl-proxy create-state create-hardfork
- - name: find & copy binaries
+ - name: Find & copy binaries
run: |
mkdir artifacts-${{ matrix.os }}
cp build-${{ matrix.os }}/crypto/fift build-${{ matrix.os }}/crypto/tlbc build-${{ matrix.os }}/crypto/func build-${{ matrix.os }}/crypto/create-state build-${{ matrix.os }}/validator-engine-console/validator-engine-console build-${{ matrix.os }}/tonlib/tonlib-cli build-${{ matrix.os }}/tonlib/libtonlibjson.so.0.5 build-${{ matrix.os }}/http/http-proxy build-${{ matrix.os }}/rldp-http-proxy/rldp-http-proxy build-${{ matrix.os }}/dht-server/dht-server build-${{ matrix.os }}/lite-client/lite-client build-${{ matrix.os }}/validator-engine/validator-engine build-${{ matrix.os }}/utils/generate-random-id build-${{ matrix.os }}/utils/json2tlo build-${{ matrix.os }}/adnl/adnl-proxy artifacts-${{ matrix.os }}
+ cp -R crypto/smartcont artifacts-${{ matrix.os }}
+ cp -R crypto/fift/lib artifacts-${{ matrix.os }}
- name: Upload artifacts
uses: actions/upload-artifact@master
with:
diff --git a/.github/workflows/win-2019-compile.yml b/.github/workflows/win-2019-compile.yml
new file mode 100644
index 00000000..a2ccab66
--- /dev/null
+++ b/.github/workflows/win-2019-compile.yml
@@ -0,0 +1,88 @@
+name: Windows Server 2019 x64 Compile
+
+on: [push,workflow_dispatch,workflow_call]
+
+defaults:
+ run:
+ shell: cmd
+
+jobs:
+ build:
+
+ runs-on: windows-2019
+
+ steps:
+ - name: Get Current OS version
+ run: |
+ systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
+
+ - name: Check out current repository
+ uses: actions/checkout@v3
+ with:
+ submodules: 'recursive'
+
+ - name: Check out zlib repository
+ uses: actions/checkout@v3
+ with:
+ repository: desktop-app/zlib
+ path: zlib
+
+ - name: Setup msbuild.exe
+ uses: microsoft/setup-msbuild@v1.0.2
+
+ - name: Compile zlib Win64
+ run: |
+ cd zlib\contrib\vstudio\vc14
+ msbuild zlibstat.vcxproj /p:Configuration=ReleaseWithoutAsm /p:platform=x64 -p:PlatformToolset=v142
+
+ - name: Install pre-compiled OpenSSL Win64
+ run: |
+ curl -Lo openssl-1.1.1o.zip https://github.com/neodiX42/precompiled-openssl-win64/raw/main/openssl-1.1.1o.zip
+ jar xf openssl-1.1.1o.zip
+
+ - name: Install pre-compiled libmicrohttpd Win64
+ run: |
+ curl -Lo libmicrohttpd-latest-w32-bin.zip https://ftpmirror.gnu.org/libmicrohttpd/libmicrohttpd-latest-w32-bin.zip
+ unzip libmicrohttpd-latest-w32-bin.zip
+
+ - name: Install pre-compiled Readline Win64
+ run: |
+ curl -Lo readline-5.0-1-lib.zip https://github.com/neodiX42/precompiled-openssl-win64/raw/main/readline-5.0-1-lib.zip
+ unzip readline-5.0-1-lib.zip
+
+ - name: Compile
+ run: |
+ set root=%cd%
+ echo %root%
+ mkdir build
+ cd build
+ cmake -DREADLINE_INCLUDE_DIR=%root%\readline-5.0-1-lib\include\readline -DREADLINE_LIBRARY=%root%\readline-5.0-1-lib\lib\readline.lib -DZLIB_FOUND=1 -DMHD_FOUND=1 -DMHD_LIBRARY=%root%\libmicrohttpd-0.9.75-w32-bin\x86_64\VS2019\Release-static\libmicrohttpd.lib -DMHD_INCLUDE_DIR=%root%\libmicrohttpd-0.9.75-w32-bin\x86_64\VS2019\Release-static -DZLIB_INCLUDE_DIR=%root%\zlib -DZLIB_LIBRARY=%root%\zlib\contrib\vstudio\vc14\x64\ZlibStatReleaseWithoutAsm\zlibstat.lib -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=%root%/openssl-1.1/x64/include -DOPENSSL_CRYPTO_LIBRARY=%root%/openssl-1.1/x64/lib/libcrypto.lib -DCMAKE_CXX_FLAGS="/DTD_WINDOWS=1 /EHsc /bigobj /W0" ..
+ cmake --build . --target fift func tonlib tonlibjson tonlib-cli validator-engine lite-client pow-miner validator-engine-console generate-random-id json2tlo dht-server http-proxy rldp-http-proxy adnl-proxy create-state create-hardfork --config Release
+
+ - name: Show executables
+ run: |
+ cd build
+ del Release\test-*
+ dir *.exe /a-D /S /B
+ dir *.dll /a-D /S /B
+
+ - name: Check if validator-engine.exe exists
+ run: |
+ set root=%cd%
+ copy %root%\build\validator-engine\Release\validator-engine.exe test
+
+ - name: Find & copy binaries
+ run: |
+ mkdir artifacts
+ mkdir artifacts\smartcont
+ mkdir artifacts\lib
+
+ for %%I in (build\crypto\Release\fift.exe build\crypto\Release\tlbc.exe build\crypto\Release\func.exe build\crypto\Release\create-state.exe build\validator-engine-console\Release\validator-engine-console.exe build\tonlib\Release\tonlib-cli.exe build\tonlib\Release\tonlibjson.dll build\http\Release\http-proxy.exe build\rldp-http-proxy\Release\rldp-http-proxy.exe build\dht-server\Release\dht-server.exe build\lite-client\Release\lite-client.exe build\validator-engine\Release\validator-engine.exe build\utils\Release\generate-random-id.exe build\utils\Release\json2tlo.exe build\adnl\Release\adnl-proxy.exe) do copy %%I artifacts\
+ xcopy /e /k /h /i crypto\smartcont artifacts\smartcont
+ xcopy /e /k /h /i crypto\fift\lib artifacts\lib
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@master
+ with:
+ name: ton-win-binaries
+ path: artifacts
diff --git a/.github/workflows/windows2019x64-compile.yml b/.github/workflows/windows2019x64-compile.yml
deleted file mode 100644
index f1dce51c..00000000
--- a/.github/workflows/windows2019x64-compile.yml
+++ /dev/null
@@ -1,80 +0,0 @@
-name: Windows Server 2019 x64 Compile
-
-on:
- workflow_dispatch:
- push:
-
-defaults:
- run:
- shell: cmd
-
-jobs:
- build:
-
- runs-on: windows-2019
-
- steps:
- - name: Get Current OS version
- run: |
- systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
-
- - name: Check out current repository
- uses: actions/checkout@v2
- with:
- submodules: 'recursive'
-
- - name: Check out zlib repository
- uses: actions/checkout@v2
- with:
- repository: desktop-app/zlib
- path: zlib
-
- - name: Setup msbuild.exe
- uses: microsoft/setup-msbuild@v1.0.2
-
- - name: Compile zlib Win64
- run: |
- cd zlib\contrib\vstudio\vc14
- msbuild zlibstat.vcxproj /p:Configuration=ReleaseWithoutAsm /p:platform=x64 -p:PlatformToolset=v142
-
- - name: Install pre-compiled OpenSSL Win64
- run: |
- curl -Lo openssl-1.1.1o.zip https://github.com/neodiX42/precompiled-openssl-win64/raw/main/openssl-1.1.1o.zip
- jar xf openssl-1.1.1o.zip
-
- - name: Configure
- run: |
- set root=%cd%
- echo %root%
- mkdir build
- cd build
- cmake -DZLIB_FOUND=1 -DZLIB_INCLUDE_DIR=%root%\zlib -DZLIB_LIBRARY=%root%\zlib\contrib\vstudio\vc14\x64\ZlibStatReleaseWithoutAsm\zlibstat.lib -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=%root%/openssl-1.1/x64/include -DOPENSSL_CRYPTO_LIBRARY=%root%/openssl-1.1/x64/lib/libcrypto.lib -DCMAKE_CXX_FLAGS="/DTD_WINDOWS=1 /EHsc /bigobj /W0" ..
-
- - name: Build
- run: |
- cd build
- cmake --build . --target fift func tonlib tonlibjson tonlib-cli validator-engine lite-client pow-miner validator-engine-console generate-random-id json2tlo dht-server http-proxy rldp-http-proxy adnl-proxy create-state create-hardfork --config Release
-
- - name: Show executables
- run: |
- cd build
- del Release\test-*
- dir *.exe /a-D /S /B
- dir *.dll /a-D /S /B
-
- - name: Check if validator-engine.exe exists
- run: |
- set root=%cd%
- copy %root%\build\validator-engine\Release\validator-engine.exe test
-
- - name: Find & copy binaries
- run: |
- mkdir artifacts
- for /f %%a in ('dir *.exe /b /a /s') do copy /Y %%a artifacts
- copy build\tonlib\Release\tonlibjson.dll artifacts
-
- - name: Upload artifacts
- uses: actions/upload-artifact@master
- with:
- name: ton-win64-binaries
- path: artifacts
diff --git a/.github/workflows/windows2019x64-tonlib-java.yml b/.github/workflows/windows2019x64-tonlib-java.yml
deleted file mode 100644
index f03212b8..00000000
--- a/.github/workflows/windows2019x64-tonlib-java.yml
+++ /dev/null
@@ -1,71 +0,0 @@
-name: Windows 2019 tonlib-java
-
-on:
- workflow_dispatch:
- push:
- branches:
- - 'wallets'
-
-defaults:
- run:
- shell: cmd
-
-jobs:
- build:
-
- runs-on: windows-2019
-
- steps:
- - name: Get Current OS version
- run: |
- systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
- - name: Check out current repository
- uses: actions/checkout@v2
- with:
- submodules: 'recursive'
-
- - name: Check out zlib repository
- uses: actions/checkout@v2
- with:
- repository: desktop-app/zlib
- path: zlib
-
- - name: Setup msbuild.exe
- uses: microsoft/setup-msbuild@v1.0.2
-
- - name: Compile zlib Win64
- run: |
- cd zlib\contrib\vstudio\vc14
- msbuild zlibstat.vcxproj /p:Configuration=ReleaseWithoutAsm /p:platform=x64 -p:PlatformToolset=v142
-
- - name: Install precompiled OpenSSL Win64
- run: |
- curl -Lo openssl-1.1.1o.zip https://github.com/neodiX42/precompiled-openssl-win64/raw/main/openssl-1.1.1o.zip
- jar xf openssl-1.1.1o.zip
-
- - name: Configure & Build
- run: |
- set JAVA_AWT_LIBRARY=NotNeeded
- set JAVA_JVM_LIBRARY=NotNeeded
- set JAVA_INCLUDE_PATH=${JAVA_HOME}/include
- set JAVA_AWT_INCLUDE_PATH=${JAVA_HOME}/include
- set JAVA_INCLUDE_PATH2=${JAVA_HOME}/include/win32
-
- set root=%cd%
- echo %root%
- cd example/android
- mkdir build
- cd build
- cmake -DZLIB_FOUND=1 -DZLIB_INCLUDE_DIR=%root%\zlib -DZLIB_LIBRARY=%root%\zlib\contrib\vstudio\vc14\x64\ZlibStatReleaseWithoutAsm\zlibstat.lib -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=%root%/openssl-1.1/x64/include -DOPENSSL_CRYPTO_LIBRARY=%root%/openssl-1.1/x64/lib/libcrypto.lib -DTON_ONLY_TONLIB=ON -DCMAKE_CXX_FLAGS="/DTD_WINDOWS=1 /EHsc /bigobj /W0" ..
- cmake --build . --target native-lib --config Release
-
- - name: Find & copy binaries
- run: |
- mkdir tonlib-java
- cp example/android/build/Release/native-lib.dll tonlib-java/
-
- - name: Upload artifacts
- uses: actions/upload-artifact@master
- with:
- name: tonlib-win64-java
- path: tonlib-java
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 75a89c41..1e52f969 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -290,8 +290,9 @@ if (MSVC)
endif()
if (NOT MSVC)
add_cxx_compiler_flag("-Wall")
+ add_cxx_compiler_flag("-Wextra")
endif()
-add_cxx_compiler_flag("-Wextra")
+
add_cxx_compiler_flag("-Wimplicit-fallthrough=2")
add_cxx_compiler_flag("-Wpointer-arith")
add_cxx_compiler_flag("-Wcast-qual")
diff --git a/Changelog.md b/Changelog.md
index b4749780..a5dc5402 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -26,3 +26,15 @@ Besides the work of the core team, this update is based on the efforts of @tvoro
Update coincided with persistent state serialization event which lead to block production speed deterioration (issue substantially mitigated in update itself). This phenomena was aggravated by the fact that after update some validators lost ability to participate in block creation. The last was caused by threshold based hardcoded protocol version bump, where threshold was set in such manner (based on block height with value higher than 9m), that it eluded detection in private net tests. The update was temporarily paused and resumed after persistent state serialization ended and issues with block creation were resolved.
Besides the work of the core team, this update is based on the efforts of @awesome-doge (help with abseil-cpp upgrade), @rec00rsiff (noted issues for exotic endianess and implemented network stats) and third-party security auditors.
+
+## 10.2022 Update
+* Added extended block creation and general perfomance stats gathering
+* Forbidden report data on blocks not committed to the master chain for LS
+* Improved debug in TVM
+* FunC 0.3.0: multi-line asms, bitwise operations for constants, duplication of identical definition for constants and asms now allowed
+* New tonlib methods: sendMessageReturnHash, getTransactionsV2, getMasterchainBlockSignatures, getShardBlockProof, getLibraries.
+* Fixed bugs related to invalid TVM output (c4, c5, libaries) and non-validated network data; avoided too deep recursion in libraries loading
+* Fixed multiple undefined behavior issues
+* Added build of FunC and Fift to WASM
+
+Besides the work of the core team, this update is based on the efforts of @tvorogme (debug improvements), @AlexeyFSL (WASM builds) and third-party security auditors.
diff --git a/README.md b/README.md
index 177bc3ee..893717ba 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,12 @@
+
+
+
+
+
+
+
+
+[![TON Overflow Group][ton-overflow-badge]][ton-overflow-url]
[![Stack Overflow Group][stack-overflow-badge]][stack-overflow-url]
[![Telegram Foundation Group][telegram-foundation-badge]][telegram-foundation-url]
[![Telegram Community Group][telegram-community-badge]][telegram-community-url]
@@ -11,11 +20,11 @@
[twitter-url]: https://twitter.com/ton_blockchain
[stack-overflow-badge]: https://img.shields.io/badge/-Stack%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white
[stack-overflow-url]: https://stackoverflow.com/questions/tagged/ton
+[ton-overflow-badge]: https://img.shields.io/badge/-TON%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white
+[ton-overflow-url]: https://answers.ton.org
-# TON
-
Main TON monorepo, which includes the code of the node/validator, lite-client, tonlib, FunC compiler, etc.
## Updates flow:
diff --git a/adnl/adnl-address-list.cpp b/adnl/adnl-address-list.cpp
index 7bd36374..912943a8 100644
--- a/adnl/adnl-address-list.cpp
+++ b/adnl/adnl-address-list.cpp
@@ -185,7 +185,8 @@ td::Ref AdnlAddressImpl::create(const tl_object_ptr(addr.get()),
td::overloaded([&](const ton_api::adnl_address_udp &obj) { res = td::make_ref(obj); },
[&](const ton_api::adnl_address_udp6 &obj) { res = td::make_ref(obj); },
- [&](const ton_api::adnl_address_tunnel &obj) { res = td::make_ref(obj); }));
+ [&](const ton_api::adnl_address_tunnel &obj) { res = td::make_ref(obj); },
+ [&](const ton_api::adnl_address_reverse &obj) { res = td::make_ref(); }));
return res;
}
@@ -202,7 +203,12 @@ AdnlAddressList::AdnlAddressList(const tl_object_ptr
version_ = static_cast(addrs->version_);
std::vector> vec;
for (auto &addr : addrs->addrs_) {
- vec.push_back(AdnlAddressImpl::create(addr));
+ auto obj = AdnlAddressImpl::create(addr);
+ if (obj->is_reverse()) {
+ has_reverse_ = true;
+ } else {
+ vec.push_back(std::move(obj));
+ }
}
addrs_ = std::move(vec);
reinit_date_ = addrs->reinit_date_;
@@ -215,6 +221,9 @@ tl_object_ptr AdnlAddressList::tl() const {
for (auto &v : addrs_) {
addrs.emplace_back(v->tl());
}
+ if (has_reverse_) {
+ addrs.push_back(create_tl_object());
+ }
return create_tl_object(std::move(addrs), version_, reinit_date_, priority_, expire_at_);
}
diff --git a/adnl/adnl-address-list.h b/adnl/adnl-address-list.h
index 80d7f96b..ebc7473a 100644
--- a/adnl/adnl-address-list.h
+++ b/adnl/adnl-address-list.h
@@ -39,6 +39,9 @@ class AdnlAddressImpl : public td::CntObject {
virtual td::actor::ActorOwn create_connection(
td::actor::ActorId network_manager, td::actor::ActorId adnl,
std::unique_ptr callback) const = 0;
+ virtual bool is_reverse() const {
+ return false;
+ }
static td::Ref create(const tl_object_ptr &addr);
};
@@ -54,6 +57,7 @@ class AdnlAddressList {
td::int32 priority_;
td::int32 expire_at_;
std::vector addrs_;
+ bool has_reverse_{false};
public:
static constexpr td::uint32 max_serialized_size() {
@@ -102,6 +106,13 @@ class AdnlAddressList {
static td::Result create(const tl_object_ptr &addr_list);
td::Status add_udp_address(td::IPAddress addr);
+
+ void set_reverse(bool x = true) {
+ has_reverse_ = x;
+ }
+ bool has_reverse() const {
+ return has_reverse_;
+ }
};
} // namespace adnl
diff --git a/adnl/adnl-address-list.hpp b/adnl/adnl-address-list.hpp
index efc226d2..0c869378 100644
--- a/adnl/adnl-address-list.hpp
+++ b/adnl/adnl-address-list.hpp
@@ -116,6 +116,31 @@ class AdnlAddressTunnel : public AdnlAddressImpl {
std::unique_ptr callback) const override;
};
+class AdnlAddressReverse : public AdnlAddressImpl {
+ public:
+ AdnlAddressReverse *make_copy() const override {
+ return new AdnlAddressReverse();
+ }
+ bool is_public() const override {
+ return true;
+ }
+ td::uint32 serialized_size() const override {
+ return 4;
+ }
+ tl_object_ptr tl() const override {
+ return create_tl_object();
+ }
+ td::actor::ActorOwn create_connection(
+ td::actor::ActorId network_manager, td::actor::ActorId adnl,
+ std::unique_ptr callback) const override {
+ LOG(ERROR) << "Cannot create connection for AdnlAddressReverse";
+ return {};
+ }
+ bool is_reverse() const override {
+ return true;
+ }
+};
+
} // namespace adnl
} // namespace ton
diff --git a/adnl/adnl-local-id.cpp b/adnl/adnl-local-id.cpp
index 3461efe1..b4818276 100644
--- a/adnl/adnl-local-id.cpp
+++ b/adnl/adnl-local-id.cpp
@@ -121,7 +121,7 @@ void AdnlLocalId::update_address_list(AdnlAddressList addr_list) {
}
void AdnlLocalId::publish_address_list() {
- if (dht_node_.empty() || addr_list_.empty() || addr_list_.size() == 0) {
+ if (dht_node_.empty() || addr_list_.empty() || (addr_list_.size() == 0 && !addr_list_.has_reverse())) {
VLOG(ADNL_NOTICE) << this << ": skipping public addr list, because localid (or dht node) not fully initialized";
return;
}
@@ -175,6 +175,17 @@ void AdnlLocalId::publish_address_list() {
td::actor::send_closure(keyring_, &keyring::Keyring::sign_message, short_id_.pubkey_hash(), std::move(B),
std::move(P));
+
+ if (addr_list_.has_reverse()) {
+ td::actor::send_closure(
+ dht_node_, &dht::Dht::register_reverse_connection, id_, [print_id = print_id()](td::Result R) {
+ if (R.is_error()) {
+ VLOG(ADNL_NOTICE) << print_id << ": failed to register reverse connection in DHT: " << R.move_as_error();
+ } else {
+ VLOG(ADNL_INFO) << print_id << ": registered reverse connection";
+ }
+ });
+ }
}
AdnlLocalId::AdnlLocalId(AdnlNodeIdFull id, AdnlAddressList addr_list, td::uint32 mode,
diff --git a/adnl/adnl-peer.cpp b/adnl/adnl-peer.cpp
index 46a097d9..528dd78f 100644
--- a/adnl/adnl-peer.cpp
+++ b/adnl/adnl-peer.cpp
@@ -113,6 +113,7 @@ void AdnlPeerPairImpl::discover() {
}
void AdnlPeerPairImpl::receive_packet_checked(AdnlPacket packet) {
+ request_reverse_ping_after_ = td::Timestamp::in(15.0);
auto d = Adnl::adnl_start_time();
if (packet.dst_reinit_date() > d) {
VLOG(ADNL_WARNING) << this << ": dropping IN message: too new our reinit date " << packet.dst_reinit_date();
@@ -669,10 +670,15 @@ td::Result, bool>> AdnlPeerP
}
if (conns_.size() == 0 && priority_conns_.size() == 0) {
- return td::Status::Error(ErrorCode::notready, PSTRING()
- << "empty network information: version=" << addr_list_.version()
- << " reinit_date=" << addr_list_.reinit_date()
- << " real_reinit_date=" << reinit_date_);
+ if (has_reverse_addr_) {
+ request_reverse_ping();
+ return td::Status::Error(ErrorCode::notready, "waiting for reverse ping");
+ } else {
+ return td::Status::Error(ErrorCode::notready, PSTRING()
+ << "empty network information: version=" << addr_list_.version()
+ << " reinit_date=" << addr_list_.reinit_date()
+ << " real_reinit_date=" << reinit_date_);
+ }
}
for (auto &conn : priority_conns_) {
@@ -720,11 +726,18 @@ void AdnlPeerPairImpl::update_addr_list(AdnlAddressList addr_list) {
VLOG(ADNL_INFO) << this << ": updating addr list to version " << addr_list.version() << " size=" << addr_list.size();
const auto addrs = addr_list.addrs();
+ has_reverse_addr_ = addr_list.has_reverse();
+ if (has_reverse_addr_ && addrs.empty()) {
+ return;
+ }
std::vector conns;
auto &old_conns = priority ? priority_conns_ : conns_;
size_t idx = 0;
for (const auto &addr : addrs) {
+ if (addr->is_reverse()) {
+ continue;
+ }
if ((mode_ & static_cast(AdnlLocalIdMode::direct_only)) && !addr->is_public()) {
continue;
}
@@ -746,7 +759,7 @@ void AdnlPeerPairImpl::get_conn_ip_str(td::Promise promise) {
promise.set_value("undefined");
return;
}
-
+
for (auto &conn : priority_conns_) {
if (conn.ready()) {
td::actor::send_closure(conn.conn, &AdnlNetworkConnection::get_ip_str, std::move(promise));
@@ -759,7 +772,7 @@ void AdnlPeerPairImpl::get_conn_ip_str(td::Promise promise) {
return;
}
}
-
+
promise.set_value("undefined");
}
@@ -827,7 +840,7 @@ void AdnlPeerImpl::receive_packet(AdnlNodeIdShort dst, td::uint32 dst_mode, td::
}
}
- td::actor::send_closure(it->second.get(), &AdnlPeerPair::receive_packet_checked, std::move(packet));
+ td::actor::send_closure(it->second.get(), &AdnlPeerPair::receive_packet, std::move(packet));
}
void AdnlPeerImpl::send_messages(AdnlNodeIdShort src, td::uint32 src_mode, td::actor::ActorId src_actor,
@@ -884,7 +897,7 @@ void AdnlPeerImpl::get_conn_ip_str(AdnlNodeIdShort l_id, td::Promise
if (it == peer_pairs_.end()) {
promise.set_value("undefined");
return;
- }
+ }
td::actor::send_closure(it->second, &AdnlPeerPair::get_conn_ip_str, std::move(promise));
}
@@ -960,6 +973,36 @@ void AdnlPeerPairImpl::update_peer_id(AdnlNodeIdFull id) {
CHECK(!peer_id_.empty());
}
+void AdnlPeerPairImpl::request_reverse_ping() {
+ if (request_reverse_ping_active_ || !request_reverse_ping_after_.is_in_past()) {
+ return;
+ }
+ VLOG(ADNL_INFO) << this << ": requesting reverse ping";
+ request_reverse_ping_after_ = td::Timestamp::in(15.0);
+ request_reverse_ping_active_ = true;
+ td::actor::send_closure(
+ local_actor_, &AdnlLocalId::get_self_node,
+ [SelfId = actor_id(this), peer = peer_id_short_, dht = dht_node_](td::Result R) {
+ if (R.is_error()) {
+ td::actor::send_closure(SelfId, &AdnlPeerPairImpl::request_reverse_ping_result, R.move_as_error());
+ return;
+ }
+ td::actor::send_closure(
+ dht, &dht::Dht::request_reverse_ping, R.move_as_ok(), peer, [SelfId](td::Result R) {
+ td::actor::send_closure(SelfId, &AdnlPeerPairImpl::request_reverse_ping_result, std::move(R));
+ });
+ });
+}
+
+void AdnlPeerPairImpl::request_reverse_ping_result(td::Result R) {
+ request_reverse_ping_active_ = false;
+ if (R.is_ok()) {
+ VLOG(ADNL_INFO) << this << ": reverse ping requested";
+ } else {
+ VLOG(ADNL_INFO) << this << ": failed to request reverse ping: " << R.move_as_error();
+ }
+}
+
} // namespace adnl
} // namespace ton
diff --git a/adnl/adnl-peer.hpp b/adnl/adnl-peer.hpp
index ec7166ed..4e1d8db7 100644
--- a/adnl/adnl-peer.hpp
+++ b/adnl/adnl-peer.hpp
@@ -154,6 +154,9 @@ class AdnlPeerPairImpl : public AdnlPeerPair {
}
}
+ void request_reverse_ping();
+ void request_reverse_ping_result(td::Result R);
+
struct Conn {
class ConnCallback : public AdnlNetworkConnection::Callback {
public:
@@ -251,6 +254,10 @@ class AdnlPeerPairImpl : public AdnlPeerPair {
td::Timestamp next_dht_query_at_ = td::Timestamp::never();
td::Timestamp next_db_update_at_ = td::Timestamp::never();
td::Timestamp retry_send_at_ = td::Timestamp::never();
+
+ bool has_reverse_addr_ = false;
+ td::Timestamp request_reverse_ping_after_ = td::Timestamp::now();
+ bool request_reverse_ping_active_ = false;
};
class AdnlPeerImpl : public AdnlPeer {
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index c8c85370..fab75bfc 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -275,7 +275,12 @@ target_include_directories(ton_crypto PUBLIC $)
target_link_libraries(ton_crypto PUBLIC ${OPENSSL_CRYPTO_LIBRARY} tdutils tddb_utils)
if (NOT WIN32)
- target_link_libraries(ton_crypto PUBLIC dl z)
+ find_library(DL dl)
+ if (DL)
+ target_link_libraries(ton_crypto PUBLIC dl z)
+ else()
+ target_link_libraries(ton_crypto PUBLIC z)
+ endif()
endif()
target_include_directories(ton_crypto SYSTEM PUBLIC $)
diff --git a/crypto/block/block.tlb b/crypto/block/block.tlb
index c1bd9c66..40ab4dee 100644
--- a/crypto/block/block.tlb
+++ b/crypto/block/block.tlb
@@ -113,7 +113,10 @@ var_uint$_ {n:#} len:(#< n) value:(uint (len * 8))
= VarUInteger n;
var_int$_ {n:#} len:(#< n) value:(int (len * 8))
= VarInteger n;
-nanograms$_ amount:(VarUInteger 16) = Grams;
+nanograms$_ amount:(VarUInteger 16) = Grams;
+
+_ grams:Grams = Coins;
+
//
extra_currencies$_ dict:(HashmapE 32 (VarUInteger 32))
= ExtraCurrencyCollection;
@@ -611,15 +614,29 @@ wfmt_ext#0 min_addr_len:(## 12) max_addr_len:(## 12) addr_len_step:(## 12)
workchain_type_id:(## 32) { workchain_type_id >= 1 }
= WorkchainFormat 0;
-workchain#a6 enabled_since:uint32 actual_min_split:(## 8)
- min_split:(## 8) max_split:(## 8) { actual_min_split <= min_split }
+wc_split_merge_timings#0
+ split_merge_delay:uint32 split_merge_interval:uint32
+ min_split_merge_interval:uint32 max_split_merge_delay:uint32
+ = WcSplitMergeTimings;
+
//workchain#a5 enabled_since:uint32 min_split:(## 8) max_split:(## 8)
// { min_split <= max_split } { max_split <= 60 }
+
+workchain#a6 enabled_since:uint32 actual_min_split:(## 8)
+ min_split:(## 8) max_split:(## 8) { actual_min_split <= min_split }
basic:(## 1) active:Bool accept_msgs:Bool flags:(## 13) { flags = 0 }
zerostate_root_hash:bits256 zerostate_file_hash:bits256
version:uint32 format:(WorkchainFormat basic)
= WorkchainDescr;
+workchain_v2#a7 enabled_since:uint32 actual_min_split:(## 8)
+ min_split:(## 8) max_split:(## 8) { actual_min_split <= min_split }
+ basic:(## 1) active:Bool accept_msgs:Bool flags:(## 13) { flags = 0 }
+ zerostate_root_hash:bits256 zerostate_file_hash:bits256
+ version:uint32 format:(WorkchainFormat basic)
+ split_merge_timings:WcSplitMergeTimings
+ = WorkchainDescr;
+
_ workchains:(HashmapE 32 WorkchainDescr) = ConfigParam 12;
complaint_prices#1a deposit:Grams bit_price:Grams cell_price:Grams = ComplaintPricing;
@@ -739,11 +756,32 @@ collator_info#0 full_node_id:(Maybe uint256) = CollatorInfo;
colator_config#a0 full_collated_data:Bool collator_nodes:(HashmapE 352 CollatorInfo) = CollatorConfig;
_ CollatorConfig = ConfigParam 41;
+size_limits_config#01 max_msg_bits:uint32 max_msg_cells:uint32 max_library_cells:uint32 max_vm_data_depth:uint16
+ max_ext_msg_size:uint32 max_ext_msg_depth:uint16 = SizeLimitsConfig;
+size_limits_config_v2#02 max_msg_bits:uint32 max_msg_cells:uint32 max_library_cells:uint32 max_vm_data_depth:uint16
+ max_ext_msg_size:uint32 max_ext_msg_depth:uint16 max_acc_state_cells:uint32 max_acc_state_bits:uint32 = SizeLimitsConfig;
+_ SizeLimitsConfig = ConfigParam 43;
+
oracle_bridge_params#_ bridge_address:bits256 oracle_mutlisig_address:bits256 oracles:(HashmapE 256 uint256) external_chain_address:bits256 = OracleBridgeParams;
_ OracleBridgeParams = ConfigParam 71; // Ethereum bridge
_ OracleBridgeParams = ConfigParam 72; // Binance Smart Chain bridge
_ OracleBridgeParams = ConfigParam 73; // Polygon bridge
+// Note that chains in which bridge, minter and jetton-wallet operate are fixated
+jetton_bridge_prices#_ bridge_burn_fee:Coins bridge_mint_fee:Coins
+ wallet_min_tons_for_storage:Coins
+ wallet_gas_consumption:Coins
+ minter_min_tons_for_storage:Coins
+ discover_gas_consumption:Coins = JettonBridgePrices;
+
+jetton_bridge_params_v0#00 bridge_address:bits256 oracles_address:bits256 oracles:(HashmapE 256 uint256) state_flags:uint8 burn_bridge_fee:Coins = JettonBridgeParams;
+jetton_bridge_params_v1#01 bridge_address:bits256 oracles_address:bits256 oracles:(HashmapE 256 uint256) state_flags:uint8 prices:^JettonBridgePrices external_chain_address:bits256 = JettonBridgeParams;
+
+_ JettonBridgeParams = ConfigParam 79; // ETH->TON token bridge
+_ JettonBridgeParams = ConfigParam 80; // BNB->TON token bridge
+_ JettonBridgeParams = ConfigParam 81; // Polygon->TON token bridge
+
+
//
// PROOFS
//
@@ -870,4 +908,3 @@ chan_op_cmd#912838d1 msg:ChanSignedMsg = ChanOp;
chan_data$_ config:^ChanConfig state:^ChanState = ChanData;
-
diff --git a/crypto/block/mc-config.cpp b/crypto/block/mc-config.cpp
index 6b3e51ff..c8b9a470 100644
--- a/crypto/block/mc-config.cpp
+++ b/crypto/block/mc-config.cpp
@@ -1913,6 +1913,38 @@ std::vector Config::compute_total_validator_set(int next) c
return res.move_as_ok()->export_validator_set();
}
+td::Result Config::get_size_limits_config() const {
+ SizeLimitsConfig limits;
+ td::Ref param = get_config_param(43);
+ if (param.is_null()) {
+ return limits;
+ }
+ auto unpack_v1 = [&](auto& rec) {
+ limits.max_msg_bits = rec.max_msg_bits;
+ limits.max_msg_cells = rec.max_msg_cells;
+ limits.max_library_cells = rec.max_library_cells;
+ limits.max_vm_data_depth = static_cast(rec.max_vm_data_depth);
+ limits.ext_msg_limits.max_size = rec.max_ext_msg_size;
+ limits.ext_msg_limits.max_depth = static_cast(rec.max_ext_msg_depth);
+ };
+
+ auto unpack_v2 = [&](auto& rec) {
+ unpack_v1(rec);
+ limits.max_acc_state_bits = rec.max_acc_state_bits;
+ limits.max_acc_state_cells = rec.max_acc_state_cells;
+ };
+ gen::SizeLimitsConfig::Record_size_limits_config rec_v1;
+ gen::SizeLimitsConfig::Record_size_limits_config_v2 rec_v2;
+ if (tlb::unpack_cell(param, rec_v1)) {
+ unpack_v1(rec_v1);
+ } else if (tlb::unpack_cell(param, rec_v2)) {
+ unpack_v2(rec_v2);
+ } else {
+ return td::Status::Error("configuration parameter 43 is invalid");
+ }
+ return limits;
+}
+
td::Result> Config::unpack_validator_set_start_stop(Ref vset_root) {
if (vset_root.is_null()) {
return td::Status::Error("validator set absent");
@@ -1942,31 +1974,58 @@ bool WorkchainInfo::unpack(ton::WorkchainId wc, vm::CellSlice& cs) {
if (wc == ton::workchainInvalid) {
return false;
}
- block::gen::WorkchainDescr::Record info;
- if (!tlb::unpack(cs, info)) {
- return false;
- }
- enabled_since = info.enabled_since;
- actual_min_split = info.actual_min_split;
- min_split = info.min_split;
- max_split = info.max_split;
- basic = info.basic;
- active = info.active;
- accept_msgs = info.accept_msgs;
- flags = info.flags;
- zerostate_root_hash = info.zerostate_root_hash;
- zerostate_file_hash = info.zerostate_file_hash;
- version = info.version;
- if (basic) {
- min_addr_len = max_addr_len = addr_len_step = 256;
- } else {
- block::gen::WorkchainFormat::Record_wfmt_ext ext;
- if (!tlb::type_unpack(cs, block::gen::WorkchainFormat{basic}, ext)) {
+ auto unpack_v1 = [this](auto& info) {
+ enabled_since = info.enabled_since;
+ actual_min_split = info.actual_min_split;
+ min_split = info.min_split;
+ max_split = info.max_split;
+ basic = info.basic;
+ active = info.active;
+ accept_msgs = info.accept_msgs;
+ flags = info.flags;
+ zerostate_root_hash = info.zerostate_root_hash;
+ zerostate_file_hash = info.zerostate_file_hash;
+ version = info.version;
+ if (basic) {
+ min_addr_len = max_addr_len = addr_len_step = 256;
+ } else {
+ block::gen::WorkchainFormat::Record_wfmt_ext ext;
+ if (!tlb::csr_type_unpack(info.format, block::gen::WorkchainFormat{basic}, ext)) {
+ return false;
+ }
+ min_addr_len = ext.min_addr_len;
+ max_addr_len = ext.max_addr_len;
+ addr_len_step = ext.addr_len_step;
+ }
+ return true;
+ };
+ auto unpack_v2 = [&, this](auto& info) {
+ if (!unpack_v1(info)) {
return false;
}
- min_addr_len = ext.min_addr_len;
- max_addr_len = ext.max_addr_len;
- addr_len_step = ext.addr_len_step;
+ block::gen::WcSplitMergeTimings::Record rec;
+ if (!tlb::csr_unpack(info.split_merge_timings, rec)) {
+ return false;
+ }
+ split_merge_delay = rec.split_merge_delay;
+ split_merge_interval = rec.split_merge_interval;
+ min_split_merge_interval = rec.min_split_merge_interval;
+ max_split_merge_delay = rec.max_split_merge_delay;
+ return true;
+ };
+ block::gen::WorkchainDescr::Record_workchain info_v1;
+ block::gen::WorkchainDescr::Record_workchain_v2 info_v2;
+ vm::CellSlice cs0 = cs;
+ if (tlb::unpack(cs, info_v1)) {
+ if (!unpack_v1(info_v1)) {
+ return false;
+ }
+ } else if (tlb::unpack(cs = cs0, info_v2)) {
+ if (!unpack_v2(info_v2)) {
+ return false;
+ }
+ } else {
+ return false;
}
workchain = wc;
LOG(DEBUG) << "unpacked info for workchain " << wc << ": basic=" << basic << ", active=" << active
diff --git a/crypto/block/mc-config.h b/crypto/block/mc-config.h
index e00a045e..29467ae8 100644
--- a/crypto/block/mc-config.h
+++ b/crypto/block/mc-config.h
@@ -376,6 +376,21 @@ struct MsgPrices {
td::RefInt256 get_next_part(td::RefInt256 total) const;
};
+struct SizeLimitsConfig {
+ // Default values are used when not present in global config
+ struct ExtMsgLimits {
+ td::uint32 max_size = 65535;
+ td::uint16 max_depth = 512;
+ };
+ td::uint32 max_msg_bits = 1 << 21;
+ td::uint32 max_msg_cells = 1 << 13;
+ td::uint32 max_library_cells = 1000;
+ td::uint16 max_vm_data_depth = 512;
+ ExtMsgLimits ext_msg_limits;
+ td::uint32 max_acc_state_cells = 1 << 16;
+ td::uint32 max_acc_state_bits = (1 << 16) * 1023;
+};
+
struct CatchainValidatorsConfig {
td::uint32 mc_cc_lifetime, shard_cc_lifetime, shard_val_lifetime, shard_val_num;
bool shuffle_mc_val;
@@ -402,6 +417,13 @@ struct WorkchainInfo : public td::CntObject {
ton::RootHash zerostate_root_hash;
ton::FileHash zerostate_file_hash;
int min_addr_len, max_addr_len, addr_len_step;
+
+ // Default values are used when split_merge_timings is not set in config
+ unsigned split_merge_delay = 100; // prepare (delay) split/merge for 100 seconds
+ unsigned split_merge_interval = 100; // split/merge is enabled during 60 second interval
+ unsigned min_split_merge_interval = 30; // split/merge interval must be at least 30 seconds
+ unsigned max_split_merge_delay = 1000; // end of split/merge interval must be at most 1000 seconds in the future
+
bool is_valid() const {
return workchain != ton::workchainInvalid;
}
@@ -605,6 +627,7 @@ class Config {
ton::CatchainSeqno cc_seqno) const;
std::vector compute_total_validator_set(int next) const;
CollatorConfig get_collator_config(bool need_collator_nodes) const;
+ td::Result get_size_limits_config() const;
static std::vector do_compute_validator_set(const block::CatchainValidatorsConfig& ccv_conf,
ton::ShardIdFull shard,
const block::ValidatorSet& vset, ton::UnixTime time,
diff --git a/crypto/block/transaction.cpp b/crypto/block/transaction.cpp
index 3b2334d6..293677ca 100644
--- a/crypto/block/transaction.cpp
+++ b/crypto/block/transaction.cpp
@@ -24,6 +24,7 @@
#include "td/utils/uint128.h"
#include "ton/ton-shard.h"
#include "vm/vm.h"
+#include "td/utils/Timer.h"
namespace {
class StringLoggerTail : public td::LogInterface {
@@ -345,7 +346,7 @@ bool Account::unpack(Ref shard_account, Ref extra,
block::gen::AccountStorage::Record storage;
if (!(tlb::unpack_exact(acc_cs, acc) && (my_addr = acc.addr).not_null() && unpack_address(acc.addr.write()) &&
compute_my_addr() && unpack_storage_info(acc.storage_stat.write()) &&
- tlb::csr_unpack(std::move(acc.storage), storage) &&
+ tlb::csr_unpack(this->storage = std::move(acc.storage), storage) &&
std::max(storage.last_trans_lt, 1ULL) > acc_info.last_trans_lt && balance.unpack(std::move(storage.balance)))) {
return false;
}
@@ -459,7 +460,6 @@ bool Account::deactivate() {
return true;
}
-
bool Account::belongs_to_shard(ton::ShardIdFull shard) const {
return workchain == shard.workchain && ton::shard_is_ancestor(shard.shard, addr);
}
@@ -593,7 +593,7 @@ bool Transaction::unpack_input_msg(bool ihr_delivered, const ActionPhaseConfig*
sstat.bits -= cs.size(); // bits in the root cells are free
sstat.cells--; // the root cell itself is not counted as a cell
LOG(DEBUG) << "storage paid for a message: " << sstat.cells << " cells, " << sstat.bits << " bits";
- if (sstat.bits > max_msg_bits || sstat.cells > max_msg_cells) {
+ if (sstat.bits > cfg->size_limits.max_msg_bits || sstat.cells > cfg->size_limits.max_msg_cells) {
LOG(DEBUG) << "inbound external message too large, invalid";
return false;
}
@@ -1043,12 +1043,15 @@ bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) {
vm_log.log_options = td::LogOptions(VERBOSITY_NAME(DEBUG), true, false);
}
vm::VmState vm{new_code, std::move(stack), gas, 1, new_data, vm_log, compute_vm_libraries(cfg)};
+ vm.set_max_data_depth(cfg.max_vm_data_depth);
vm.set_c7(prepare_vm_c7(cfg)); // tuple with SmartContractInfo
// vm.incr_stack_trace(1); // enable stack dump after each step
LOG(DEBUG) << "starting VM";
cp.vm_init_state_hash = vm.get_state_hash();
+ td::Timer timer;
cp.exit_code = ~vm.run();
+ double elapsed = timer.elapsed();
LOG(DEBUG) << "VM terminated with exit code " << cp.exit_code;
cp.out_of_gas = (cp.exit_code == ~(int)vm::Excno::out_of_gas);
cp.vm_final_state_hash = vm.get_final_state_hash(cp.exit_code);
@@ -1064,7 +1067,8 @@ bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) {
}
LOG(INFO) << "steps: " << vm.get_steps_count() << " gas: used=" << gas.gas_consumed() << ", max=" << gas.gas_max
<< ", limit=" << gas.gas_limit << ", credit=" << gas.gas_credit;
- LOG(INFO) << "out_of_gas=" << cp.out_of_gas << ", accepted=" << cp.accepted << ", success=" << cp.success;
+ LOG(INFO) << "out_of_gas=" << cp.out_of_gas << ", accepted=" << cp.accepted << ", success=" << cp.success
+ << ", time=" << elapsed << "s";
if (logger != nullptr) {
cp.vm_log = logger->get_log();
}
@@ -1121,6 +1125,25 @@ bool Transaction::prepare_action_phase(const ActionPhaseConfig& cfg) {
ap.total_action_fees = td::zero_refint();
ap.reserved_balance.set_zero();
+ td::Ref old_code = new_code, old_data = new_data, old_library = new_library;
+ auto enforce_state_size_limits = [&]() {
+ if (account.is_special) {
+ return true;
+ }
+ if (!check_state_size_limit(cfg)) {
+ // Rollback changes to state, fail action phase
+ LOG(INFO) << "Account state size exceeded limits";
+ new_storage_stat.clear();
+ new_code = old_code;
+ new_data = old_data;
+ new_library = old_library;
+ ap.result_code = 50;
+ ap.state_size_too_big = true;
+ return false;
+ }
+ return true;
+ };
+
int n = 0;
while (true) {
ap.action_list.push_back(list);
@@ -1196,9 +1219,21 @@ bool Transaction::prepare_action_phase(const ActionPhaseConfig& cfg) {
ap.no_funds = true;
}
LOG(DEBUG) << "invalid action " << ap.result_arg << " in action list: error code " << ap.result_code;
+ // This is reuqired here because changes to libraries are applied even if actipn phase fails
+ enforce_state_size_limits();
return true;
}
}
+
+ end_lt = ap.end_lt;
+ if (ap.new_code.not_null()) {
+ new_code = ap.new_code;
+ }
+ new_data = compute_phase->new_data; // tentative persistent data update applied
+ if (!enforce_state_size_limits()) {
+ return true;
+ }
+
ap.result_arg = 0;
ap.result_code = 0;
CHECK(ap.remaining_balance.grams->sgn() >= 0);
@@ -1212,12 +1247,7 @@ bool Transaction::prepare_action_phase(const ActionPhaseConfig& cfg) {
was_deleted = true;
}
ap.success = true;
- end_lt = ap.end_lt;
out_msgs = std::move(ap.out_msgs);
- if (ap.new_code.not_null()) {
- new_code = ap.new_code;
- }
- new_data = compute_phase->new_data; // tentative persistent data update applied
total_fees +=
ap.total_action_fees; // NB: forwarding fees are not accounted here (they are not collected by the validators in this transaction)
balance = ap.remaining_balance;
@@ -1272,6 +1302,11 @@ int Transaction::try_action_change_library(vm::CellSlice& cs, ActionPhase& ap, c
// library code not found
return 41;
}
+ vm::CellStorageStat sstat;
+ sstat.compute_used_storage(lib_ref);
+ if (sstat.cells > cfg.size_limits.max_library_cells) {
+ return 43;
+ }
vm::CellBuilder cb;
CHECK(cb.store_bool_bool(rec.mode >> 1) && cb.store_ref_bool(std::move(lib_ref)));
CHECK(dict.set_builder(hash, cb));
@@ -1546,7 +1581,7 @@ int Transaction::try_action_send_msg(const vm::CellSlice& cs0, ActionPhase& ap,
sstat.add_used_storage(info.value->prefetch_ref());
}
LOG(DEBUG) << "storage paid for a message: " << sstat.cells << " cells, " << sstat.bits << " bits";
- if (sstat.bits > max_msg_bits || sstat.cells > max_msg_cells) {
+ if (sstat.bits > cfg.size_limits.max_msg_bits || sstat.cells > cfg.size_limits.max_msg_cells) {
LOG(DEBUG) << "message too large, invalid";
return skip_invalid ? 0 : 40;
}
@@ -1801,6 +1836,35 @@ int Transaction::try_action_reserve_currency(vm::CellSlice& cs, ActionPhase& ap,
return 0;
}
+bool Transaction::check_state_size_limit(const ActionPhaseConfig& cfg) {
+ auto cell_equal = [](const td::Ref& a, const td::Ref& b) -> bool {
+ if (a.is_null()) {
+ return b.is_null();
+ }
+ if (b.is_null()) {
+ return false;
+ }
+ return a->get_hash() == b->get_hash();
+ };
+ if (cell_equal(account.code, new_code) && cell_equal(account.data, new_data) &&
+ cell_equal(account.library, new_library)) {
+ return true;
+ }
+ // new_storage_stat is used here beause these stats will be reused in compute_state()
+ new_storage_stat.limit_cells = cfg.size_limits.max_acc_state_cells;
+ new_storage_stat.limit_bits = cfg.size_limits.max_acc_state_bits;
+ new_storage_stat.add_used_storage(new_code);
+ new_storage_stat.add_used_storage(new_data);
+ new_storage_stat.add_used_storage(new_library);
+ if (acc_status == Account::acc_active) {
+ new_storage_stat.clear_limit();
+ } else {
+ new_storage_stat.clear();
+ }
+ return new_storage_stat.cells <= cfg.size_limits.max_acc_state_cells &&
+ new_storage_stat.bits <= cfg.size_limits.max_acc_state_bits;
+}
+
bool Transaction::prepare_bounce_phase(const ActionPhaseConfig& cfg) {
if (in_msg.is_null() || !bounce_enabled) {
return false;
@@ -1924,6 +1988,32 @@ bool Account::store_acc_status(vm::CellBuilder& cb, int acc_status) const {
return cb.store_long_bool(v, 2);
}
+static td::optional try_update_storage_stat(const vm::CellStorageStat& old_stat,
+ td::Ref old_cs,
+ td::Ref new_cell) {
+ if (old_stat.cells == 0 || old_cs.is_null()) {
+ return {};
+ }
+ vm::CellSlice new_cs = vm::CellSlice(vm::NoVm(), new_cell);
+ if (old_cs->size_refs() != new_cs.size_refs()) {
+ return {};
+ }
+ for (unsigned i = 0; i < old_cs->size_refs(); ++i) {
+ if (old_cs->prefetch_ref(i)->get_hash() != new_cs.prefetch_ref(i)->get_hash()) {
+ return {};
+ }
+ }
+ if (old_stat.bits < old_cs->size()) {
+ return {};
+ }
+
+ vm::CellStorageStat new_stat;
+ new_stat.cells = old_stat.cells;
+ new_stat.bits = old_stat.bits - old_cs->size() + new_cs.size();
+ new_stat.public_cells = old_stat.public_cells;
+ return new_stat;
+}
+
bool Transaction::compute_state() {
if (new_total_state.not_null()) {
return true;
@@ -1985,6 +2075,7 @@ bool Transaction::compute_state() {
// code:(Maybe ^Cell) data:(Maybe ^Cell) library:(HashmapE 256 SimpleLib)
}
auto storage = cb.finalize();
+ new_storage = td::Ref(true, vm::NoVm(), storage);
if (si_pos) {
auto cs_ref = load_cell_slice_ref(storage);
CHECK(cs_ref.unique_write().skip_ext(si_pos));
@@ -1993,7 +2084,16 @@ bool Transaction::compute_state() {
new_inner_state.clear();
}
vm::CellStorageStat& stats = new_storage_stat;
- CHECK(stats.compute_used_storage(Ref(storage)));
+ auto new_stats = try_update_storage_stat(account.storage_stat, account.storage, storage);
+ if (new_stats) {
+ stats = new_stats.unwrap();
+ } else {
+ td::Timer timer;
+ CHECK(stats.add_used_storage(Ref(storage)));
+ if (timer.elapsed() > 0.1) {
+ LOG(INFO) << "Compute used storage took " << timer.elapsed() << "s";
+ }
+ }
CHECK(cb.store_long_bool(1, 1) // account$1
&& cb.append_cellslice_bool(account.my_addr) // addr:MsgAddressInt
&& block::store_UInt7(cb, stats.cells) // storage_used$_ cells:(VarUInteger 7)
@@ -2265,9 +2365,15 @@ bool Transaction::would_fit(unsigned cls, const block::BlockLimitStatus& blimst)
return blimst.would_fit(cls, end_lt, gas_used(), &extra);
}
-bool Transaction::update_limits(block::BlockLimitStatus& blimst) const {
- return blimst.update_lt(end_lt) && blimst.update_gas(gas_used()) && blimst.add_proof(new_total_state) &&
- blimst.add_cell(root) && blimst.add_transaction() && blimst.add_account(is_first);
+bool Transaction::update_limits(block::BlockLimitStatus& blimst, bool with_size) const {
+ if (!(blimst.update_lt(end_lt) && blimst.update_gas(gas_used()))) {
+ return false;
+ }
+ if (with_size) {
+ return blimst.add_proof(new_total_state) && blimst.add_cell(root) && blimst.add_transaction() &&
+ blimst.add_account(is_first);
+ }
+ return true;
}
/*
@@ -2296,6 +2402,7 @@ Ref Transaction::commit(Account& acc) {
acc.last_trans_hash_ = root->get_hash().bits();
acc.last_paid = last_paid;
acc.storage_stat = new_storage_stat;
+ acc.storage = new_storage;
acc.balance = std::move(balance);
acc.due_payment = std::move(due_payment);
acc.total_state = std::move(new_total_state);
diff --git a/crypto/block/transaction.h b/crypto/block/transaction.h
index 75762260..6a171418 100644
--- a/crypto/block/transaction.h
+++ b/crypto/block/transaction.h
@@ -107,6 +107,7 @@ struct ComputePhaseConfig {
Ref global_config;
td::BitArray<256> block_rand_seed;
bool with_vm_log{false};
+ td::uint16 max_vm_data_depth = 512;
ComputePhaseConfig(td::uint64 _gas_price = 0, td::uint64 _gas_limit = 0, td::uint64 _gas_credit = 0)
: gas_price(_gas_price), gas_limit(_gas_limit), special_gas_limit(_gas_limit), gas_credit(_gas_credit) {
compute_threshold();
@@ -143,6 +144,7 @@ struct ActionPhaseConfig {
int bounce_msg_body{0}; // usually 0 or 256 bits
MsgPrices fwd_std;
MsgPrices fwd_mc; // from/to masterchain
+ SizeLimitsConfig size_limits;
const WorkchainSet* workchains{nullptr};
const MsgPrices& fetch_msg_prices(bool is_masterchain) const {
return is_masterchain ? fwd_mc : fwd_std;
@@ -182,6 +184,7 @@ struct ActionPhase {
bool code_changed{false};
bool action_list_invalid{false};
bool acc_delete_req{false};
+ bool state_size_too_big{false};
enum { acst_unchanged = 0, acst_frozen = 2, acst_deleted = 3 };
int acc_status_change{acst_unchanged};
td::RefInt256 total_fwd_fees; // all fees debited from the account
@@ -235,6 +238,7 @@ struct Account {
td::RefInt256 due_payment;
Ref orig_total_state; // ^Account
Ref total_state; // ^Account
+ Ref storage; // AccountStorage
Ref inner_state; // StateInit
ton::Bits256 state_hash; // hash of StateInit for frozen accounts
Ref code, data, library, orig_library;
@@ -283,7 +287,6 @@ struct Account {
};
struct Transaction {
- static constexpr unsigned max_msg_bits = (1 << 21), max_msg_cells = (1 << 13);
enum {
tr_none,
tr_ord,
@@ -323,6 +326,7 @@ struct Transaction {
ton::UnixTime last_paid;
Ref root;
Ref new_total_state;
+ Ref new_storage;
Ref new_inner_state;
Ref new_code, new_data, new_library;
Ref in_msg, in_msg_state;
@@ -348,6 +352,7 @@ struct Transaction {
std::vector[> compute_vm_libraries(const ComputePhaseConfig& cfg);
bool prepare_compute_phase(const ComputePhaseConfig& cfg);
bool prepare_action_phase(const ActionPhaseConfig& cfg);
+ bool check_state_size_limit(const ActionPhaseConfig& cfg);
bool prepare_bounce_phase(const ActionPhaseConfig& cfg);
bool compute_state();
bool serialize();
@@ -359,7 +364,7 @@ struct Transaction {
const vm::NewCellStorageStat& store_stat, const vm::CellUsageTree* usage_tree) const;
bool update_block_storage_profile(vm::NewCellStorageStat& store_stat, const vm::CellUsageTree* usage_tree) const;
bool would_fit(unsigned cls, const block::BlockLimitStatus& blk_lim_st) const;
- bool update_limits(block::BlockLimitStatus& blk_lim_st) const;
+ bool update_limits(block::BlockLimitStatus& blk_lim_st, bool with_size = true) const;
Ref commit(Account& _account); // _account should point to the same account
LtCellRef extract_out_msg(unsigned i);
diff --git a/crypto/func/analyzer.cpp b/crypto/func/analyzer.cpp
index 47cb141a..006d119e 100644
--- a/crypto/func/analyzer.cpp
+++ b/crypto/func/analyzer.cpp
@@ -520,6 +520,14 @@ bool Op::compute_used_vars(const CodeBlob& code, bool edit) {
} while (changes <= edit);
return set_var_info(std::move(new_var_info));
}
+ case _TryCatch: {
+ code.compute_used_code_vars(block0, next_var_info, edit);
+ code.compute_used_code_vars(block1, next_var_info, edit);
+ VarDescrList merge_info = block0->var_info + block1->var_info + next_var_info;
+ merge_info -= left;
+ merge_info.clear_last();
+ return set_var_info(std::move(merge_info));
+ }
default:
std::cerr << "fatal: unknown operation ?" << cl << "> in compute_used_vars()\n";
throw src::ParseError{where, "unknown operation"};
@@ -645,6 +653,10 @@ bool prune_unreachable(std::unique_ptr& ops) {
reach = true;
break;
}
+ case Op::_TryCatch: {
+ reach = prune_unreachable(op.block0) | prune_unreachable(op.block1);
+ break;
+ }
default:
std::cerr << "fatal: unknown operation ?" << op.cl << ">\n";
throw src::ParseError{op.where, "unknown operation in prune_unreachable()"};
@@ -825,6 +837,12 @@ VarDescrList Op::fwd_analyze(VarDescrList values) {
values = block0->fwd_analyze(values);
break;
}
+ case _TryCatch: {
+ VarDescrList val1 = block0->fwd_analyze(values);
+ VarDescrList val2 = block1->fwd_analyze(std::move(values));
+ values = val1 | val2;
+ break;
+ }
default:
std::cerr << "fatal: unknown operation ?" << cl << ">\n";
throw src::ParseError{where, "unknown operation in fwd_analyze()"};
@@ -866,6 +884,7 @@ bool Op::mark_noreturn() {
case _Return:
return set_noreturn(true);
case _If:
+ case _TryCatch:
return set_noreturn((block0->mark_noreturn() & (block1 && block1->mark_noreturn())) | next->mark_noreturn());
case _Again:
block0->mark_noreturn();
diff --git a/crypto/func/auto-tests/stress_tester.py b/crypto/func/auto-tests/stress_tester.py
index 8f3ec624..c7b63934 100644
--- a/crypto/func/auto-tests/stress_tester.py
+++ b/crypto/func/auto-tests/stress_tester.py
@@ -13,7 +13,7 @@ def getenv(name, default=None):
print("Environemnt variable", name, "is not set", file=sys.stderr)
exit(1)
-VAR_CNT = 5
+VAR_CNT = 10
TMP_DIR = tempfile.mkdtemp()
FUNC_EXECUTABLE = getenv("FUNC_EXECUTABLE", "func")
FIFT_EXECUTABLE = getenv("FIFT_EXECUTABLE", "fift")
@@ -31,6 +31,15 @@ class State:
self.x = x
self.vs = [0] * VAR_CNT
+ def copy(self):
+ s = State(self.x)
+ s.vs = self.vs.copy()
+ return s
+
+ def copy_from(self, s):
+ self.x = s.x
+ self.vs = s.vs.copy()
+
class Code:
pass
@@ -136,6 +145,37 @@ class CodeRepeat(Code):
print(" " * (indent + 1) + "%s += 1;" % var, file=f)
print(" " * indent + "} until (%s >= %d);" % (var, self.n), file=f)
+class CodeThrow(Code):
+ def __init__(self):
+ pass
+
+ def execute(self, state):
+ return "EXCEPTION"
+
+ def write(self, f, indent=0):
+ print(" " * indent + "throw(42);", file=f)
+
+class CodeTryCatch(Code):
+ def __init__(self, c1, c2):
+ self.c1 = c1
+ self.c2 = c2
+
+ def execute(self, state):
+ state0 = state.copy()
+ res = self.c1.execute(state)
+ if res == "EXCEPTION":
+ state.copy_from(state0)
+ return self.c2.execute(state)
+ else:
+ return res
+
+ def write(self, f, indent=0):
+ print(" " * indent + "try {", file=f)
+ self.c1.write(f, indent + 1)
+ print(" " * indent + "} catch (_, _) {", file=f)
+ self.c2.write(f, indent + 1)
+ print(" " * indent + "}", file=f)
+
def write_function(f, name, body, inline=False, inline_ref=False, method_id=None):
print("_ %s(int x)" % name, file=f, end="")
if inline:
@@ -147,31 +187,37 @@ def write_function(f, name, body, inline=False, inline_ref=False, method_id=None
print(" {", file=f)
for i in range(VAR_CNT):
print(" int v%d = 0;" % i, file=f)
- body.write(f, 1);
+ body.write(f, 1)
print("}", file=f)
-def gen_code(xl, xr, with_return, loop_depth=0):
+def gen_code(xl, xr, with_return, loop_depth=0, try_catch_depth=0, can_throw=False):
+ if try_catch_depth < 3 and random.randint(0, 5) == 0:
+ c1 = gen_code(xl, xr, with_return, loop_depth, try_catch_depth + 1, random.randint(0, 1) == 0)
+ c2 = gen_code(xl, xr, with_return, loop_depth, try_catch_depth + 1, can_throw)
+ return CodeTryCatch(c1, c2)
code = []
for _ in range(random.randint(0, 2)):
if random.randint(0, 3) == 0 and loop_depth < 3:
- c = gen_code(xl, xr, False, loop_depth + 1)
+ c = gen_code(xl, xr, False, loop_depth + 1, try_catch_depth, can_throw)
code.append(CodeRepeat(random.randint(0, 3), c, random.randint(0, 2)))
elif xr - xl > 1:
xmid = random.randrange(xl + 1, xr)
ret = random.choice((0, 0, 0, 0, 0, 1, 2))
- c1 = gen_code(xl, xmid, ret == 1, loop_depth)
+ c1 = gen_code(xl, xmid, ret == 1, loop_depth, try_catch_depth, can_throw)
if random.randrange(5) == 0:
c2 = CodeEmpty()
else:
- c2 = gen_code(xmid, xr, ret == 2, loop_depth)
+ c2 = gen_code(xmid, xr, ret == 2, loop_depth, try_catch_depth, can_throw)
code.append(CodeIfRange(xl, xmid, c1, c2))
+ if xr - xl == 1 and can_throw and random.randint(0, 5) == 0:
+ code.append(CodeThrow())
if with_return:
if xr - xl == 1:
code.append(CodeReturn(random.randrange(10**9)))
else:
xmid = random.randrange(xl + 1, xr)
- c1 = gen_code(xl, xmid, True, loop_depth)
- c2 = gen_code(xmid, xr, True, loop_depth)
+ c1 = gen_code(xl, xmid, True, loop_depth, try_catch_depth, can_throw)
+ c2 = gen_code(xmid, xr, True, loop_depth, try_catch_depth, can_throw)
code.append(CodeIfRange(xl, xmid, c1, c2))
for _ in range(random.randint(0, 3)):
pos = random.randint(0, len(code))
@@ -203,6 +249,7 @@ def runvm(compiled_fif, xl, xr):
output.append(list(map(int, s.split())))
return output
+
cnt_ok = 0
cnt_fail = 0
for test_id in range(0, 1000000):
diff --git a/crypto/func/builtins.cpp b/crypto/func/builtins.cpp
index 22f4282c..2c103d46 100644
--- a/crypto/func/builtins.cpp
+++ b/crypto/func/builtins.cpp
@@ -229,16 +229,25 @@ int emulate_xor(int a, int b) {
}
int emulate_not(int a) {
+ if ((a & VarDescr::ConstZero) == VarDescr::ConstZero) {
+ return VarDescr::ConstTrue;
+ }
+ if ((a & VarDescr::ConstTrue) == VarDescr::ConstTrue) {
+ return VarDescr::ConstZero;
+ }
+ int a2 = a;
int f = VarDescr::_Even | VarDescr::_Odd;
- if ((a & f) && (~a & f)) {
- a ^= f;
+ if ((a2 & f) && (~a2 & f)) {
+ a2 ^= f;
}
- f = VarDescr::_Pos | VarDescr::_Neg;
- if ((a & f) && (~a & f)) {
- a ^= f;
+ a2 &= ~(VarDescr::_Zero | VarDescr::_NonZero | VarDescr::_Bit | VarDescr::_Pos | VarDescr::_Neg);
+ if ((a & VarDescr::_Neg) && (a & VarDescr::_NonZero)) {
+ a2 |= VarDescr::_Pos;
}
- a &= ~(VarDescr::_Zero | VarDescr::_NonZero | VarDescr::_Bit);
- return a;
+ if (a & VarDescr::_Pos) {
+ a2 |= VarDescr::_Neg;
+ }
+ return a2;
}
int emulate_lshift(int a, int b) {
@@ -977,6 +986,38 @@ AsmOp compile_cond_throw(std::vector& res, std::vector& args
}
}
+AsmOp compile_throw_arg(std::vector& res, std::vector& args) {
+ assert(res.empty() && args.size() == 2);
+ VarDescr &x = args[1];
+ if (x.is_int_const() && x.int_const->unsigned_fits_bits(11)) {
+ x.unused();
+ return exec_arg_op("THROWARG", x.int_const, 1, 0);
+ } else {
+ return exec_op("THROWARGANY", 2, 0);
+ }
+}
+
+AsmOp compile_cond_throw_arg(std::vector& res, std::vector& args, bool mode) {
+ assert(res.empty() && args.size() == 3);
+ VarDescr &x = args[1], &y = args[2];
+ std::string suff = (mode ? "IF" : "IFNOT");
+ bool skip_cond = false;
+ if (y.always_true() || y.always_false()) {
+ y.unused();
+ skip_cond = true;
+ if (y.always_true() != mode) {
+ x.unused();
+ return AsmOp::Nop();
+ }
+ }
+ if (x.is_int_const() && x.int_const->unsigned_fits_bits(11)) {
+ x.unused();
+ return skip_cond ? exec_arg_op("THROWARG", x.int_const, 1, 0) : exec_arg_op("THROWARG"s + suff, x.int_const, 2, 0);
+ } else {
+ return skip_cond ? exec_op("THROWARGANY", 2, 0) : exec_op("THROWARGANY"s + suff, 3, 0);
+ }
+}
+
AsmOp compile_bool_const(std::vector& res, std::vector& args, bool val) {
assert(res.size() == 1 && args.empty());
VarDescr& r = res[0];
@@ -1102,6 +1143,8 @@ void define_builtins() {
auto fetch_slice_op = TypeExpr::new_map(SliceInt, TypeExpr::new_tensor({Slice, Slice}));
auto prefetch_slice_op = TypeExpr::new_map(SliceInt, Slice);
//auto arith_null_op = TypeExpr::new_map(TypeExpr::new_unit(), Int);
+ auto throw_arg_op = TypeExpr::new_forall({X}, TypeExpr::new_map(TypeExpr::new_tensor({X, Int}), Unit));
+ auto cond_throw_arg_op = TypeExpr::new_forall({X}, TypeExpr::new_map(TypeExpr::new_tensor({X, Int, Int}), Unit));
define_builtin_func("_+_", arith_bin_op, compile_add);
define_builtin_func("_-_", arith_bin_op, compile_sub);
define_builtin_func("-_", arith_un_op, compile_negate);
@@ -1161,6 +1204,9 @@ void define_builtins() {
define_builtin_func("throw", impure_un_op, compile_throw, true);
define_builtin_func("throw_if", impure_bin_op, std::bind(compile_cond_throw, _1, _2, true), true);
define_builtin_func("throw_unless", impure_bin_op, std::bind(compile_cond_throw, _1, _2, false), true);
+ define_builtin_func("throw_arg", throw_arg_op, compile_throw_arg, true);
+ define_builtin_func("throw_arg_if", cond_throw_arg_op, std::bind(compile_cond_throw_arg, _1, _2, true), true);
+ define_builtin_func("throw_arg_unless", cond_throw_arg_op, std::bind(compile_cond_throw_arg, _1, _2, false), true);
define_builtin_func("load_int", fetch_int_op, std::bind(compile_fetch_int, _1, _2, true, true), {}, {1, 0});
define_builtin_func("load_uint", fetch_int_op, std::bind(compile_fetch_int, _1, _2, true, false), {}, {1, 0});
define_builtin_func("preload_int", prefetch_int_op, std::bind(compile_fetch_int, _1, _2, false, true));
diff --git a/crypto/func/codegen.cpp b/crypto/func/codegen.cpp
index 1f504bcd..13b808ea 100644
--- a/crypto/func/codegen.cpp
+++ b/crypto/func/codegen.cpp
@@ -782,6 +782,77 @@ bool Op::generate_code_step(Stack& stack) {
return false;
}
}
+ case _TryCatch: {
+ if (block0->is_empty() && block1->is_empty()) {
+ return true;
+ }
+ if (block0->noreturn() || block1->noreturn()) {
+ stack.o.retalt_ = true;
+ }
+ Stack catch_stack{stack.o};
+ std::vector catch_vars;
+ std::vector catch_last;
+ for (const VarDescr& var : block1->var_info.list) {
+ if (stack.find(var.idx) >= 0) {
+ catch_vars.push_back(var.idx);
+ catch_last.push_back(!block0->var_info[var.idx]);
+ }
+ }
+ const size_t block_size = 255;
+ for (size_t begin = catch_vars.size(), end = begin; end > 0; end = begin) {
+ begin = end >= block_size ? end - block_size : 0;
+ for (size_t i = begin; i < end; ++i) {
+ catch_stack.push_new_var(catch_vars[i]);
+ }
+ }
+ catch_stack.push_new_var(left[0]);
+ catch_stack.push_new_var(left[1]);
+ stack.rearrange_top(catch_vars, catch_last);
+ stack.opt_show();
+ stack.o << "c4 PUSH";
+ stack.o << "c5 PUSH";
+ stack.o << "c7 PUSH";
+ stack.o << "<{";
+ stack.o.indent();
+ if (block1->noreturn()) {
+ catch_stack.mode |= Stack::_NeedRetAlt;
+ }
+ block1->generate_code_all(catch_stack);
+ catch_stack.drop_vars_except(next->var_info);
+ catch_stack.opt_show();
+ stack.o.undent();
+ stack.o << "}>CONT";
+ stack.o << "c7 SETCONT";
+ stack.o << "c5 SETCONT";
+ stack.o << "c4 SETCONT";
+ for (size_t begin = catch_vars.size(), end = begin; end > 0; end = begin) {
+ begin = end >= block_size ? end - block_size : 0;
+ stack.o << std::to_string(end - begin) + " PUSHINT";
+ stack.o << "-1 PUSHINT";
+ stack.o << "SETCONTVARARGS";
+ }
+ stack.s.erase(stack.s.end() - catch_vars.size(), stack.s.end());
+ stack.modified();
+ stack.o << "<{";
+ stack.o.indent();
+ if (block0->noreturn()) {
+ stack.mode |= Stack::_NeedRetAlt;
+ }
+ block0->generate_code_all(stack);
+ if (block0->noreturn()) {
+ stack.s = std::move(catch_stack.s);
+ } else if (!block1->noreturn()) {
+ stack.merge_state(catch_stack);
+ }
+ stack.opt_show();
+ stack.o.undent();
+ stack.o << "}>CONT";
+ stack.o << "c1 PUSH";
+ stack.o << "COMPOSALT";
+ stack.o << "SWAP";
+ stack.o << "TRY";
+ return true;
+ }
default:
std::cerr << "fatal: unknown operation ?" << cl << ">\n";
throw src::ParseError{where, "unknown operation in generate_code()"};
diff --git a/crypto/func/func.h b/crypto/func/func.h
index 5db99725..0f4182bf 100644
--- a/crypto/func/func.h
+++ b/crypto/func/func.h
@@ -53,6 +53,8 @@ enum Keyword {
_Do,
_While,
_Until,
+ _Try,
+ _Catch,
_If,
_Ifnot,
_Then,
@@ -537,6 +539,7 @@ struct Op {
_Until,
_Repeat,
_Again,
+ _TryCatch,
_SliceConst
};
int cl;
@@ -1559,6 +1562,9 @@ struct Stack {
int find_outside(var_idx_t var, int from, int to) const;
void forget_const();
void validate(int i) const {
+ if (i > 255) {
+ throw src::Fatal{"Too deep stack"};
+ }
assert(i >= 0 && i < depth() && "invalid stack reference");
}
void modified() {
@@ -1593,6 +1599,7 @@ struct Stack {
void apply_wrappers() {
if (o.retalt_) {
o.insert(0, "SAMEALTSAVE");
+ o.insert(0, "c2 SAVE");
if (mode & _InlineFunc) {
o.indent_all();
o.insert(0, "CONT:<{");
diff --git a/crypto/func/keywords.cpp b/crypto/func/keywords.cpp
index 3f0fa230..fedce9db 100644
--- a/crypto/func/keywords.cpp
+++ b/crypto/func/keywords.cpp
@@ -97,6 +97,8 @@ void define_keywords() {
.add_keyword("do", Kw::_Do)
.add_keyword("while", Kw::_While)
.add_keyword("until", Kw::_Until)
+ .add_keyword("try", Kw::_Try)
+ .add_keyword("catch", Kw::_Catch)
.add_keyword("if", Kw::_If)
.add_keyword("ifnot", Kw::_Ifnot)
.add_keyword("then", Kw::_Then)
diff --git a/crypto/func/parse-func.cpp b/crypto/func/parse-func.cpp
index 92dfe47b..fe86bc1e 100644
--- a/crypto/func/parse-func.cpp
+++ b/crypto/func/parse-func.cpp
@@ -1102,6 +1102,36 @@ blk_fl::val parse_do_stmt(Lexer& lex, CodeBlob& code) {
return res & ~blk_fl::empty;
}
+blk_fl::val parse_try_catch_stmt(Lexer& lex, CodeBlob& code) {
+ lex.expect(_Try);
+ Op& try_catch_op = code.emplace_back(lex.cur().loc, Op::_TryCatch);
+ code.push_set_cur(try_catch_op.block0);
+ blk_fl::val res0 = parse_block_stmt(lex, code);
+ code.close_pop_cur(lex.cur().loc);
+ lex.expect(_Catch);
+ code.push_set_cur(try_catch_op.block1);
+ sym::open_scope(lex);
+ Expr* expr = parse_expr(lex, code, true);
+ expr->chk_lvalue(lex.cur());
+ TypeExpr* tvm_error_type = TypeExpr::new_tensor(TypeExpr::new_var(), TypeExpr::new_atomic(_Int));
+ try {
+ unify(expr->e_type, tvm_error_type);
+ } catch (UnifyError& ue) {
+ std::ostringstream os;
+ os << "`catch` arguments have incorrect type " << expr->e_type << ": " << ue;
+ lex.cur().error(os.str());
+ }
+ expr->predefine_vars();
+ expr->define_new_vars(code);
+ try_catch_op.left = expr->pre_compile(code);
+ assert(try_catch_op.left.size() == 2);
+ blk_fl::val res1 = parse_block_stmt(lex, code);
+ sym::close_scope(lex);
+ code.close_pop_cur(lex.cur().loc);
+ blk_fl::combine_parallel(res0, res1);
+ return res0;
+}
+
blk_fl::val parse_if_stmt(Lexer& lex, CodeBlob& code, int first_lex = _If) {
SrcLocation loc{lex.cur().loc};
lex.expect(first_lex);
@@ -1165,6 +1195,8 @@ blk_fl::val parse_stmt(Lexer& lex, CodeBlob& code) {
return parse_do_stmt(lex, code);
case _While:
return parse_while_stmt(lex, code);
+ case _Try:
+ return parse_try_catch_stmt(lex, code);
default: {
auto expr = parse_expr(lex, code);
expr->chk_rvalue(lex.cur());
diff --git a/crypto/func/test/tc1.fc b/crypto/func/test/tc1.fc
new file mode 100644
index 00000000..245fc521
--- /dev/null
+++ b/crypto/func/test/tc1.fc
@@ -0,0 +1,113 @@
+() test1() impure {
+ int i = 3;
+ repeat (3) {
+ try {
+ int j = i;
+ i *= 2;
+ throw_unless(500, j <= 10);
+ } catch (x, e) {
+ i -= 2;
+ }
+ i += i + 1;
+ }
+ throw_unless(501, i == 43);
+}
+
+int divide_by_ten(int num) {
+ try {
+ throw_unless(500, num < 10);
+ } catch (x, e) {
+ return divide_by_ten(num - 10) + 1;
+ }
+ return 0;
+}
+
+() test2() impure {
+ int n = divide_by_ten(37);
+ throw_unless(502, n == 3);
+}
+
+(int, int) swap_int(int a, int b) {
+ try {
+ a = a * b;
+ b = a / b;
+ a = a / b;
+ return (a, b);
+ } catch (x, e) {
+ throw_unless(500, b == 0);
+ }
+ return (0, a);
+}
+
+() test3() impure {
+ int a = 0;
+ int b = 57;
+ try {
+ (a, b) = swap_int(a, b);
+ } catch (x, e) {
+ throw_unless(500, a == 0);
+ a = b;
+ b = 0;
+ }
+ throw_unless(503, (a == 57) & (b == 0));
+}
+
+int get_x(int x, int y) {
+ try {
+ } catch (x, e) {
+ return -1;
+ }
+ return x;
+}
+
+int get_y(int x, int y) {
+ try {
+ return -1;
+ } catch (x, e) {
+ }
+ return y;
+}
+
+() test4() impure {
+ throw_unless(504, get_x(3, 4) == 3);
+ throw_unless(504, get_y(3, 4) == -1);
+}
+
+(int, int, int, int, int) foo(int a, int b, int c, int d, int e) {
+ try {
+ throw(11);
+ } catch (x, y) {
+ a += 1;
+ b += 2;
+ c += 3;
+ d += 4;
+ e += 5;
+ }
+ return (a, b, c, d, e);
+}
+
+() test5() impure {
+ var (a, b, c, d, e) = foo(10, 20, 30, 40, 50);
+ throw_unless(505, (a == 11) & (b == 22) & (c == 33) & (d == 44) & (e == 55));
+}
+
+() test6() impure {
+ int a = 0;
+ int b = 0;
+ int c = 0;
+ try {
+ b = 3;
+ } catch (x, y) {
+ b = 12;
+ }
+ throw_unless(506, (a == 0) & (b == 3) & (c == 0));
+}
+
+() main() {
+ test1();
+ test2();
+ test3();
+ test4();
+ test5();
+ test6();
+}
diff --git a/crypto/func/test/tc2.fc b/crypto/func/test/tc2.fc
new file mode 100644
index 00000000..2bde6813
--- /dev/null
+++ b/crypto/func/test/tc2.fc
@@ -0,0 +1,84 @@
+forall X -> int cast_to_int(X x) asm "NOP";
+forall X -> builder cast_to_builder(X x) asm "NOP";
+
+_ test1_body() {
+ int a = 3;
+ builder b = begin_cell();
+ int c = 1;
+ try {
+ c = 3;
+ throw_arg(b, 100);
+ } catch (x, y) {
+ return (a + c + y, cast_to_builder(x));
+ }
+ return (0, null());
+}
+
+() test1() impure {
+ var (x, y) = test1_body();
+ throw_unless(101, x == 104);
+ throw_unless(102, y.builder_refs() == y.builder_bits());
+}
+
+_ test2_body(int a, int b, int c) {
+ try {
+ try {
+ try {
+ try {
+ throw_arg_if(1, 201, a + b + c == 3);
+ throw_arg_if(2, 201, a == 3);
+ throw_arg_unless(1, 202, b == 4);
+ return 1;
+ } catch (y, x) {
+ int y = y.cast_to_int();
+ throw_arg_unless(y, x, x == 202);
+ throw_arg(y + 1, 200);
+ }
+ } catch (y, x) {
+ int y = y.cast_to_int();
+ throw_arg_if(y, x, x == 200);
+ throw_arg_if(y + 2, x, y < 2);
+ throw_arg_if(y + 3, 203, a + b + c == 4);
+ throw_arg_unless(y + 4, 204, b == 4);
+ return 3;
+ }
+ } catch (y, x) {
+ int y = y.cast_to_int();
+ try {
+ throw_arg_if(y, x, x == 200);
+ throw_arg_if(y + 1, 200, x == 201);
+ throw_arg_if(x - 203, 200, x == 202);
+ throw_arg_if(y, 200, x == 203);
+ throw_arg_if(a + 4, 205, a + b + c == 5);
+ throw_arg(7, 200);
+ } catch (v, u) {
+ int v = v.cast_to_int();
+ throw_arg_unless(v, u, u == 205);
+ if (c == 0) {
+ return b + 4;
+ }
+ throw_arg(v + 1, 200);
+ }
+ }
+ } catch (y, x) {
+ throw_unless(x, x == 200);
+ return y.cast_to_int();
+ }
+ return null();
+}
+
+() test2() impure {
+ throw_unless(201, test2_body(0, 4, 0) == 1);
+ throw_unless(202, test2_body(0, 5, 0) == 2);
+ throw_unless(203, test2_body(3, 4, 0) == 3);
+ throw_unless(204, test2_body(3, 0, 0) == 4);
+ throw_unless(205, test2_body(3, 1, 0) == 5);
+ throw_unless(206, test2_body(3, 2, 0) == 6);
+ throw_unless(207, test2_body(3, 1, 2) == 7);
+ throw_unless(208, test2_body(3, 1, 1) == 8);
+}
+
+() main() {
+ test1();
+ test2();
+}
diff --git a/crypto/smartcont/stdlib.fc b/crypto/smartcont/stdlib.fc
index 0b98eeb4..3531608a 100644
--- a/crypto/smartcont/stdlib.fc
+++ b/crypto/smartcont/stdlib.fc
@@ -1,132 +1,525 @@
;; Standard library for funC
;;
+{-
+ # Tuple manipulation primitives
+ The names and the types are mostly self-explaining.
+ See [polymorhism with forall](https://ton.org/docs/#/func/functions?id=polymorphism-with-forall)
+ for more info on the polymorphic functions.
+
+ Note that currently values of atomic type `tuple` can't be cast to composite tuple type (e.g. `[int, cell]`)
+ and vise versa.
+-}
+
+{-
+ # Lisp-style lists
+
+ Lists can be represented as nested 2-elements tuples.
+ Empty list is conventionally represented as TVM `null` value (it can be obtained by calling [null()]).
+ For example, tuple `(1, (2, (3, null)))` represents list `[1, 2, 3]`. Elements of a list can be of different types.
+-}
+
+;;; Adds an element to the beginning of lisp-style list.
forall X -> tuple cons(X head, tuple tail) asm "CONS";
+
+;;; Extracts the head and the tail of lisp-style list.
forall X -> (X, tuple) uncons(tuple list) asm "UNCONS";
+
+;;; Extracts the tail and the head of lisp-style list.
forall X -> (tuple, X) list_next(tuple list) asm( -> 1 0) "UNCONS";
+
+;;; Returns the head of lisp-style list.
forall X -> X car(tuple list) asm "CAR";
+
+;;; Returns the tail of lisp-style list.
tuple cdr(tuple list) asm "CDR";
+
+;;; Creates tuple with zero elements.
tuple empty_tuple() asm "NIL";
+
+;;; Appends a value `x` to a `Tuple t = (x1, ..., xn)`, but only if the resulting `Tuple t' = (x1, ..., xn, x)`
+;;; is of length at most 255. Otherwise throws a type check exception.
forall X -> tuple tpush(tuple t, X value) asm "TPUSH";
forall X -> (tuple, ()) ~tpush(tuple t, X value) asm "TPUSH";
+
+;;; Creates a tuple of length one with given argument as element.
forall X -> [X] single(X x) asm "SINGLE";
+
+;;; Unpacks a tuple of length one
forall X -> X unsingle([X] t) asm "UNSINGLE";
+
+;;; Creates a tuple of length two with given arguments as elements.
forall X, Y -> [X, Y] pair(X x, Y y) asm "PAIR";
+
+;;; Unpacks a tuple of length two
forall X, Y -> (X, Y) unpair([X, Y] t) asm "UNPAIR";
+
+;;; Creates a tuple of length three with given arguments as elements.
forall X, Y, Z -> [X, Y, Z] triple(X x, Y y, Z z) asm "TRIPLE";
+
+;;; Unpacks a tuple of length three
forall X, Y, Z -> (X, Y, Z) untriple([X, Y, Z] t) asm "UNTRIPLE";
+
+;;; Creates a tuple of length four with given arguments as elements.
forall X, Y, Z, W -> [X, Y, Z, W] tuple4(X x, Y y, Z z, W w) asm "4 TUPLE";
+
+;;; Unpacks a tuple of length four
forall X, Y, Z, W -> (X, Y, Z, W) untuple4([X, Y, Z, W] t) asm "4 UNTUPLE";
+
+;;; Returns the first element of a tuple (with unknown element types).
forall X -> X first(tuple t) asm "FIRST";
+
+;;; Returns the second element of a tuple (with unknown element types).
forall X -> X second(tuple t) asm "SECOND";
+
+;;; Returns the third element of a tuple (with unknown element types).
forall X -> X third(tuple t) asm "THIRD";
+
+;;; Returns the fourth element of a tuple (with unknown element types).
forall X -> X fourth(tuple t) asm "3 INDEX";
+
+;;; Returns the first element of a pair tuple.
forall X, Y -> X pair_first([X, Y] p) asm "FIRST";
+
+;;; Returns the second element of a pair tuple.
forall X, Y -> Y pair_second([X, Y] p) asm "SECOND";
+
+;;; Returns the first element of a triple tuple.
forall X, Y, Z -> X triple_first([X, Y, Z] p) asm "FIRST";
+
+;;; Returns the second element of a triple tuple.
forall X, Y, Z -> Y triple_second([X, Y, Z] p) asm "SECOND";
+
+;;; Returns the third element of a triple tuple.
forall X, Y, Z -> Z triple_third([X, Y, Z] p) asm "THIRD";
+
+
+;;; Push null element (casted to given type)
+;;; By the TVM type `Null` FunC represents absence of a value of some atomic type.
+;;; So `null` can actually have any atomic type.
forall X -> X null() asm "PUSHNULL";
+
+;;; Moves a variable [x] to the top of the stack
forall X -> (X, ()) ~impure_touch(X x) impure asm "NOP";
+
+
+;;; Returns the current Unix time as an Integer
int now() asm "NOW";
+
+;;; Returns the internal address of the current smart contract as a Slice with a `MsgAddressInt`.
+;;; If necessary, it can be parsed further using primitives such as [parse_std_addr].
slice my_address() asm "MYADDR";
+
+;;; Returns the balance of the smart contract as a tuple consisting of an int
+;;; (balance in nanotoncoins) and a `cell`
+;;; (a dictionary with 32-bit keys representing the balance of "extra currencies")
+;;; at the start of Computation Phase.
+;;; Note that RAW primitives such as [send_raw_message] do not update this field.
[int, cell] get_balance() asm "BALANCE";
+
+;;; Returns the logical time of the current transaction.
int cur_lt() asm "LTIME";
+
+;;; Returns the starting logical time of the current block.
int block_lt() asm "BLOCKLT";
+;;; Computes the representation hash of a `cell` [c] and returns it as a 256-bit unsigned integer `x`.
+;;; Useful for signing and checking signatures of arbitrary entities represented by a tree of cells.
int cell_hash(cell c) asm "HASHCU";
+
+;;; Computes the hash of a `slice s` and returns it as a 256-bit unsigned integer `x`.
+;;; The result is the same as if an ordinary cell containing only data and references from `s` had been created
+;;; and its hash computed by [cell_hash].
int slice_hash(slice s) asm "HASHSU";
+
+;;; Computes sha256 of the data bits of `slice` [s]. If the bit length of `s` is not divisible by eight,
+;;; throws a cell underflow exception. The hash value is returned as a 256-bit unsigned integer `x`.
int string_hash(slice s) asm "SHA256U";
+{-
+ # Signature checks
+-}
+
+;;; Checks the Ed25519-`signature` of a `hash` (a 256-bit unsigned integer, usually computed as the hash of some data)
+;;; using [public_key] (also represented by a 256-bit unsigned integer).
+;;; The signature must contain at least 512 data bits; only the first 512 bits are used.
+;;; The result is `−1` if the signature is valid, `0` otherwise.
+;;; Note that `CHKSIGNU` creates a 256-bit slice with the hash and calls `CHKSIGNS`.
+;;; That is, if [hash] is computed as the hash of some data, these data are hashed twice,
+;;; the second hashing occurring inside `CHKSIGNS`.
int check_signature(int hash, slice signature, int public_key) asm "CHKSIGNU";
+
+;;; Checks whether [signature] is a valid Ed25519-signature of the data portion of `slice data` using `public_key`,
+;;; similarly to [check_signature].
+;;; If the bit length of [data] is not divisible by eight, throws a cell underflow exception.
+;;; The verification of Ed25519 signatures is the standard one,
+;;; with sha256 used to reduce [data] to the 256-bit number that is actually signed.
int check_data_signature(slice data, slice signature, int public_key) asm "CHKSIGNS";
+{---
+ # Computation of boc size
+ The primitives below may be useful for computing storage fees of user-provided data.
+-}
+
+;;; Returns `(x, y, z, -1)` or `(null, null, null, 0)`.
+;;; Recursively computes the count of distinct cells `x`, data bits `y`, and cell references `z`
+;;; in the DAG rooted at `cell` [c], effectively returning the total storage used by this DAG taking into account
+;;; the identification of equal cells.
+;;; The values of `x`, `y`, and `z` are computed by a depth-first traversal of this DAG,
+;;; with a hash table of visited cell hashes used to prevent visits of already-visited cells.
+;;; The total count of visited cells `x` cannot exceed non-negative [max_cells];
+;;; otherwise the computation is aborted before visiting the `(max_cells + 1)`-st cell and
+;;; a zero flag is returned to indicate failure. If [c] is `null`, returns `x = y = z = 0`.
(int, int, int) compute_data_size(cell c, int max_cells) impure asm "CDATASIZE";
+
+;;; Similar to [compute_data_size?], but accepting a `slice` [s] instead of a `cell`.
+;;; The returned value of `x` does not take into account the cell that contains the `slice` [s] itself;
+;;; however, the data bits and the cell references of [s] are accounted for in `y` and `z`.
(int, int, int) slice_compute_data_size(slice s, int max_cells) impure asm "SDATASIZE";
+
+;;; A non-quiet version of [compute_data_size?] that throws a cell overflow exception (`8`) on failure.
(int, int, int, int) compute_data_size?(cell c, int max_cells) asm "CDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
+
+;;; A non-quiet version of [slice_compute_data_size?] that throws a cell overflow exception (8) on failure.
(int, int, int, int) slice_compute_data_size?(cell c, int max_cells) asm "SDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
+;;; Throws an exception with exit_code excno if cond is not 0 (commented since implemented in compilator)
;; () throw_if(int excno, int cond) impure asm "THROWARGIF";
+{--
+ # Debug primitives
+ Only works for local TVM execution with debug level verbosity
+-}
+;;; Dumps the stack (at most the top 255 values) and shows the total stack depth.
() dump_stack() impure asm "DUMPSTK";
+{-
+ # Persistent storage save and load
+-}
+
+;;; Returns the persistent contract storage cell. It can be parsed or modified with slice and builder primitives later.
cell get_data() asm "c4 PUSH";
+
+;;; Sets `cell` [c] as persistent contract data. You can update persistent contract storage with this primitive.
() set_data(cell c) impure asm "c4 POP";
+
+{-
+ # Continuation primitives
+-}
+;;; Usually `c3` has a continuation initialized by the whole code of the contract. It is used for function calls.
+;;; The primitive returns the current value of `c3`.
cont get_c3() impure asm "c3 PUSH";
+
+;;; Updates the current value of `c3`. Usually, it is used for updating smart contract code in run-time.
+;;; Note that after execution of this primitive the current code
+;;; (and the stack of recursive function calls) won't change,
+;;; but any other function call will use a function from the new code.
() set_c3(cont c) impure asm "c3 POP";
+
+;;; Transforms a `slice` [s] into a simple ordinary continuation `c`, with `c.code = s` and an empty stack and savelist.
cont bless(slice s) impure asm "BLESS";
-() accept_message() impure asm "ACCEPT";
-() set_gas_limit(int limit) impure asm "SETGASLIMIT";
-() commit() impure asm "COMMIT";
-() buy_gas(int gram) impure asm "BUYGAS";
+{---
+ # Gas related primitives
+-}
+;;; Sets current gas limit `gl` to its maximal allowed value `gm`, and resets the gas credit `gc` to zero,
+;;; decreasing the value of `gr` by `gc` in the process.
+;;; In other words, the current smart contract agrees to buy some gas to finish the current transaction.
+;;; This action is required to process external messages, which bring no value (hence no gas) with themselves.
+;;;
+;;; For more details check [accept_message effects](https://ton.org/docs/#/smart-contracts/accept).
+() accept_message() impure asm "ACCEPT";
+
+;;; Sets current gas limit `gl` to the minimum of limit and `gm`, and resets the gas credit `gc` to zero.
+;;; If the gas consumed so far (including the present instruction) exceeds the resulting value of `gl`,
+;;; an (unhandled) out of gas exception is thrown before setting new gas limits.
+;;; Notice that [set_gas_limit] with an argument `limit ≥ 2^63 − 1` is equivalent to [accept_message].
+() set_gas_limit(int limit) impure asm "SETGASLIMIT";
+
+;;; Commits the current state of registers `c4` (“persistent data”) and `c5` (“actions”)
+;;; so that the current execution is considered “successful” with the saved values even if an exception
+;;; in Computation Phase is thrown later.
+() commit() impure asm "COMMIT";
+
+;;; Not implemented
+;;() buy_gas(int gram) impure asm "BUYGAS";
+
+;;; Computes the amount of gas that can be bought for `amount` nanoTONs,
+;;; and sets `gl` accordingly in the same way as [set_gas_limit].
+() buy_gas(int amount) impure asm "BUYGAS";
+
+;;; Computes the minimum of two integers [x] and [y].
int min(int x, int y) asm "MIN";
+
+;;; Computes the maximum of two integers [x] and [y].
int max(int x, int y) asm "MAX";
+
+;;; Sorts two integers.
(int, int) minmax(int x, int y) asm "MINMAX";
+
+;;; Computes the absolute value of an integer [x].
int abs(int x) asm "ABS";
+{-
+ # Slice primitives
+
+ It is said that a primitive _loads_ some data,
+ if it returns the data and the remainder of the slice
+ (so it can also be used as [modifying method](https://ton.org/docs/#/func/statements?id=modifying-methods)).
+
+ It is said that a primitive _preloads_ some data, if it returns only the data
+ (it can be used as [non-modifying method](https://ton.org/docs/#/func/statements?id=non-modifying-methods)).
+
+ Unless otherwise stated, loading and preloading primitives read the data from a prefix of the slice.
+-}
+
+
+;;; Converts a `cell` [c] into a `slice`. Notice that [c] must be either an ordinary cell,
+;;; or an exotic cell (see [TVM.pdf](https://ton-blockchain.github.io/docs/tvm.pdf), 3.1.2)
+;;; which is automatically loaded to yield an ordinary cell `c'`, converted into a `slice` afterwards.
slice begin_parse(cell c) asm "CTOS";
+
+;;; Checks if [s] is empty. If not, throws an exception.
() end_parse(slice s) impure asm "ENDS";
+
+;;; Loads the first reference from the slice.
(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF";
+
+;;; Preloads the first reference from the slice.
cell preload_ref(slice s) asm "PLDREF";
+
+ {- Functions below are commented because are implemented on compilator level for optimisation -}
+
+;;; Loads a signed [len]-bit integer from a slice [s].
;; (slice, int) ~load_int(slice s, int len) asm(s len -> 1 0) "LDIX";
+
+;;; Loads an unsigned [len]-bit integer from a slice [s].
;; (slice, int) ~load_uint(slice s, int len) asm( -> 1 0) "LDUX";
+
+;;; Preloads a signed [len]-bit integer from a slice [s].
;; int preload_int(slice s, int len) asm "PLDIX";
+
+;;; Preloads an unsigned [len]-bit integer from a slice [s].
;; int preload_uint(slice s, int len) asm "PLDUX";
+
+;;; Loads the first `0 ≤ len ≤ 1023` bits from slice [s] into a separate `slice s''`.
;; (slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX";
+
+;;; Preloads the first `0 ≤ len ≤ 1023` bits from slice [s] into a separate `slice s''`.
;; slice preload_bits(slice s, int len) asm "PLDSLICEX";
+
+;;; Loads serialized amount of TonCoins (any unsigned integer up to `2^128 - 1`).
(slice, int) load_grams(slice s) asm( -> 1 0) "LDGRAMS";
+(slice, int) load_coins(slice s) asm( -> 1 0) "LDGRAMS";
+
+;;; Returns all but the first `0 ≤ len ≤ 1023` bits of `slice` [s].
slice skip_bits(slice s, int len) asm "SDSKIPFIRST";
(slice, ()) ~skip_bits(slice s, int len) asm "SDSKIPFIRST";
+
+;;; Returns the first `0 ≤ len ≤ 1023` bits of `slice` [s].
slice first_bits(slice s, int len) asm "SDCUTFIRST";
+
+;;; Returns all but the last `0 ≤ len ≤ 1023` bits of `slice` [s].
slice skip_last_bits(slice s, int len) asm "SDSKIPLAST";
(slice, ()) ~skip_last_bits(slice s, int len) asm "SDSKIPLAST";
+
+;;; Returns the last `0 ≤ len ≤ 1023` bits of `slice` [s].
slice slice_last(slice s, int len) asm "SDCUTLAST";
+
+;;; Loads a dictionary `D` (HashMapE) from `slice` [s].
+;;; (returns `null` if `nothing` constructor is used).
(slice, cell) load_dict(slice s) asm( -> 1 0) "LDDICT";
+
+;;; Preloads a dictionary `D` from `slice` [s].
cell preload_dict(slice s) asm "PLDDICT";
+
+;;; Loads a dictionary as [load_dict], but returns only the remainder of the slice.
slice skip_dict(slice s) asm "SKIPDICT";
+;;; Loads (Maybe ^Cell) from `slice` [s].
+;;; In other words loads 1 bit and if it is true
+;;; loads first ref and return it with slice remainder
+;;; otherwise returns `null` and slice remainder
(slice, cell) load_maybe_ref(slice s) asm( -> 1 0) "LDOPTREF";
-cell preload_maybe_ref(slice s) asm "PLDOPTREF";
-builder store_maybe_ref(builder b, cell c) asm(c b) "STOPTREF";
+;;; Preloads (Maybe ^Cell) from `slice` [s].
+cell preload_maybe_ref(slice s) asm "PLDOPTREF";
+
+
+;;; Returns the depth of `cell` [c].
+;;; If [c] has no references, then return `0`;
+;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [c].
+;;; If [c] is a `null` instead of a cell, returns zero.
int cell_depth(cell c) asm "CDEPTH";
+
+{-
+ # Slice size primitives
+-}
+
+;;; Returns the number of references in `slice` [s].
int slice_refs(slice s) asm "SREFS";
+
+;;; Returns the number of data bits in `slice` [s].
int slice_bits(slice s) asm "SBITS";
+
+;;; Returns both the number of data bits and the number of references in `slice` [s].
(int, int) slice_bits_refs(slice s) asm "SBITREFS";
+
+;;; Checks whether a `slice` [s] is empty (i.e., contains no bits of data and no cell references).
int slice_empty?(slice s) asm "SEMPTY";
+
+;;; Checks whether `slice` [s] has no bits of data.
int slice_data_empty?(slice s) asm "SDEMPTY";
+
+;;; Checks whether `slice` [s] has no references.
int slice_refs_empty?(slice s) asm "SREMPTY";
+
+;;; Returns the depth of `slice` [s].
+;;; If [s] has no references, then returns `0`;
+;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [s].
int slice_depth(slice s) asm "SDEPTH";
+{-
+ # Builder size primitives
+-}
+
+;;; Returns the number of cell references already stored in `builder` [b]
int builder_refs(builder b) asm "BREFS";
+
+;;; Returns the number of data bits already stored in `builder` [b].
int builder_bits(builder b) asm "BBITS";
+
+;;; Returns the depth of `builder` [b].
+;;; If no cell references are stored in [b], then returns 0;
+;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [b].
int builder_depth(builder b) asm "BDEPTH";
+{-
+ # Builder primitives
+ It is said that a primitive _stores_ a value `x` into a builder `b`
+ if it returns a modified version of the builder `b'` with the value `x` stored at the end of it.
+ It can be used as [non-modifying method](https://ton.org/docs/#/func/statements?id=non-modifying-methods).
+
+ All the primitives below first check whether there is enough space in the `builder`,
+ and only then check the range of the value being serialized.
+-}
+
+;;; Creates a new empty `builder`.
builder begin_cell() asm "NEWC";
+
+;;; Converts a `builder` into an ordinary `cell`.
cell end_cell(builder b) asm "ENDC";
+
+;;; Stores a reference to `cell` [c] into `builder` [b].
builder store_ref(builder b, cell c) asm(c b) "STREF";
+
+;;; Stores an unsigned [len]-bit integer `x` into `b` for `0 ≤ len ≤ 256`.
;; builder store_uint(builder b, int x, int len) asm(x b len) "STUX";
+
+;;; Stores a signed [len]-bit integer `x` into `b` for` 0 ≤ len ≤ 257`.
;; builder store_int(builder b, int x, int len) asm(x b len) "STIX";
+
+
+;;; Stores `slice` [s] into `builder` [b]
builder store_slice(builder b, slice s) asm "STSLICER";
+
+;;; Stores (serializes) an integer [x] in the range `0..2^128 − 1` into `builder` [b].
+;;; The serialization of [x] consists of a 4-bit unsigned big-endian integer `l`,
+;;; which is the smallest integer `l ≥ 0`, such that `x < 2^8l`,
+;;; followed by an `8l`-bit unsigned big-endian representation of [x].
+;;; If [x] does not belong to the supported range, a range check exception is thrown.
+;;;
+;;; Store amounts of TonCoins to the builder as VarUInteger 16
builder store_grams(builder b, int x) asm "STGRAMS";
+builder store_coins(builder b, int x) asm "STGRAMS";
+
+;;; Stores dictionary `D` represented by `cell` [c] or `null` into `builder` [b].
+;;; In other words, stores a `1`-bit and a reference to [c] if [c] is not `null` and `0`-bit otherwise.
builder store_dict(builder b, cell c) asm(c b) "STDICT";
+;;; Stores (Maybe ^Cell) to builder:
+;;; if cell is null store 1 zero bit
+;;; otherwise store 1 true bit and ref to cell
+builder store_maybe_ref(builder b, cell c) asm(c b) "STOPTREF";
+
+
+{-
+ # Address manipulation primitives
+ The address manipulation primitives listed below serialize and deserialize values according to the following TL-B scheme:
+ ```TL-B
+ addr_none$00 = MsgAddressExt;
+ addr_extern$01 len:(## 8) external_address:(bits len)
+ = MsgAddressExt;
+ anycast_info$_ depth:(#<= 30) { depth >= 1 }
+ rewrite_pfx:(bits depth) = Anycast;
+ addr_std$10 anycast:(Maybe Anycast)
+ workchain_id:int8 address:bits256 = MsgAddressInt;
+ addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9)
+ workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
+ _ _:MsgAddressInt = MsgAddress;
+ _ _:MsgAddressExt = MsgAddress;
+
+ int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
+ src:MsgAddress dest:MsgAddressInt
+ value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
+ created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
+ ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt
+ created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
+ ```
+ A deserialized `MsgAddress` is represented by a tuple `t` as follows:
+
+ - `addr_none` is represented by `t = (0)`,
+ i.e., a tuple containing exactly one integer equal to zero.
+ - `addr_extern` is represented by `t = (1, s)`,
+ where slice `s` contains the field `external_address`. In other words, `
+ t` is a pair (a tuple consisting of two entries), containing an integer equal to one and slice `s`.
+ - `addr_std` is represented by `t = (2, u, x, s)`,
+ where `u` is either a `null` (if `anycast` is absent) or a slice `s'` containing `rewrite_pfx` (if anycast is present).
+ Next, integer `x` is the `workchain_id`, and slice `s` contains the address.
+ - `addr_var` is represented by `t = (3, u, x, s)`,
+ where `u`, `x`, and `s` have the same meaning as for `addr_std`.
+-}
+
+;;; Loads from slice [s] the only prefix that is a valid `MsgAddress`,
+;;; and returns both this prefix `s'` and the remainder `s''` of [s] as slices.
(slice, slice) load_msg_addr(slice s) asm( -> 1 0) "LDMSGADDR";
+
+;;; Decomposes slice [s] containing a valid `MsgAddress` into a `tuple t` with separate fields of this `MsgAddress`.
+;;; If [s] is not a valid `MsgAddress`, a cell deserialization exception is thrown.
tuple parse_addr(slice s) asm "PARSEMSGADDR";
+
+;;; Parses slice [s] containing a valid `MsgAddressInt` (usually a `msg_addr_std`),
+;;; applies rewriting from the anycast (if present) to the same-length prefix of the address,
+;;; and returns both the workchain and the 256-bit address as integers.
+;;; If the address is not 256-bit, or if [s] is not a valid serialization of `MsgAddressInt`,
+;;; throws a cell deserialization exception.
(int, int) parse_std_addr(slice s) asm "REWRITESTDADDR";
+
+;;; A variant of [parse_std_addr] that returns the (rewritten) address as a slice [s],
+;;; even if it is not exactly 256 bit long (represented by a `msg_addr_var`).
(int, slice) parse_var_addr(slice s) asm "REWRITEVARADDR";
+{-
+ # Dictionary primitives
+-}
+
+
+;;; Sets the value associated with [key_len]-bit key signed index in dictionary [dict] to [value] (cell),
+;;; and returns the resulting dictionary.
cell idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF";
(cell, ()) ~idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF";
+
+;;; Sets the value associated with [key_len]-bit key unsigned index in dictionary [dict] to [value] (cell),
+;;; and returns the resulting dictionary.
cell udict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETREF";
(cell, ()) ~udict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETREF";
+
cell idict_get_ref(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETOPTREF";
-(cell, int) idict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETREF";
-(cell, int) udict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGETREF";
+(cell, int) idict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETREF" "NULLSWAPIFNOT";
+(cell, int) udict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGETREF" "NULLSWAPIFNOT";
(cell, cell) idict_set_get_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETGETOPTREF";
(cell, cell) udict_set_get_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETGETOPTREF";
(cell, int) idict_delete?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDEL";
@@ -185,24 +578,47 @@ cell dict_set_builder(cell dict, int key_len, slice index, builder value) asm(va
(int, slice, int) idict_get_nexteq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETNEXTEQ" "NULLSWAPIFNOT2";
(int, slice, int) idict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETPREV" "NULLSWAPIFNOT2";
(int, slice, int) idict_get_preveq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETPREVEQ" "NULLSWAPIFNOT2";
+
+;;; Creates an empty dictionary, which is actually a null value. Equivalent to PUSHNULL
cell new_dict() asm "NEWDICT";
+;;; Checks whether a dictionary is empty. Equivalent to cell_null?.
int dict_empty?(cell c) asm "DICTEMPTY";
+
+{- Prefix dictionary primitives -}
(slice, slice, slice, int) pfxdict_get?(cell dict, int key_len, slice key) asm(key dict key_len) "PFXDICTGETQ" "NULLSWAPIFNOT2";
(cell, int) pfxdict_set?(cell dict, int key_len, slice key, slice value) asm(value key dict key_len) "PFXDICTSET";
(cell, int) pfxdict_delete?(cell dict, int key_len, slice key) asm(key dict key_len) "PFXDICTDEL";
+;;; Returns the value of the global configuration parameter with integer index `i` as a `cell` or `null` value.
cell config_param(int x) asm "CONFIGOPTPARAM";
+;;; Checks whether c is a null. Note, that FunC also has polymorphic null? built-in.
int cell_null?(cell c) asm "ISNULL";
+;;; Creates an output action which would reserve exactly amount nanotoncoins (if mode = 0), at most amount nanotoncoins (if mode = 2), or all but amount nanotoncoins (if mode = 1 or mode = 3), from the remaining balance of the account. It is roughly equivalent to creating an outbound message carrying amount nanotoncoins (or b − amount nanotoncoins, where b is the remaining balance) to oneself, so that the subsequent output actions would not be able to spend more money than the remainder. Bit +2 in mode means that the external action does not fail if the specified amount cannot be reserved; instead, all remaining balance is reserved. Bit +8 in mode means `amount <- -amount` before performing any further actions. Bit +4 in mode means that amount is increased by the original balance of the current account (before the compute phase), including all extra currencies, before performing any other checks and actions. Currently, amount must be a non-negative integer, and mode must be in the range 0..15.
() raw_reserve(int amount, int mode) impure asm "RAWRESERVE";
+;;; Similar to raw_reserve, but also accepts a dictionary extra_amount (represented by a cell or null) with extra currencies. In this way currencies other than TonCoin can be reserved.
() raw_reserve_extra(int amount, cell extra_amount, int mode) impure asm "RAWRESERVEX";
+;;; Sends a raw message contained in msg, which should contain a correctly serialized object Message X, with the only exception that the source address is allowed to have dummy value addr_none (to be automatically replaced with the current smart contract address), and ihr_fee, fwd_fee, created_lt and created_at fields can have arbitrary values (to be rewritten with correct values during the action phase of the current transaction). Integer parameter mode contains the flags. Currently mode = 0 is used for ordinary messages; mode = 128 is used for messages that are to carry all the remaining balance of the current smart contract (instead of the value originally indicated in the message); mode = 64 is used for messages that carry all the remaining value of the inbound message in addition to the value initially indicated in the new message (if bit 0 is not set, the gas fees are deducted from this amount); mode' = mode + 1 means that the sender wants to pay transfer fees separately; mode' = mode + 2 means that any errors arising while processing this message during the action phase should be ignored. Finally, mode' = mode + 32 means that the current account must be destroyed if its resulting balance is zero. This flag is usually employed together with +128.
() send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG";
+;;; Creates an output action that would change this smart contract code to that given by cell new_code. Notice that this change will take effect only after the successful termination of the current run of the smart contract
() set_code(cell new_code) impure asm "SETCODE";
+;;; Generates a new pseudo-random unsigned 256-bit integer x. The algorithm is as follows: if r is the old value of the random seed, considered as a 32-byte array (by constructing the big-endian representation of an unsigned 256-bit integer), then its sha512(r) is computed; the first 32 bytes of this hash are stored as the new value r' of the random seed, and the remaining 32 bytes are returned as the next random value x.
int random() impure asm "RANDU256";
+;;; Generates a new pseudo-random integer z in the range 0..range−1 (or range..−1, if range < 0). More precisely, an unsigned random value x is generated as in random; then z := x * range / 2^256 is computed.
int rand(int range) impure asm "RAND";
+;;; Returns the current random seed as an unsigned 256-bit Integer.
int get_seed() impure asm "RANDSEED";
-int set_seed() impure asm "SETRAND";
+;;; Sets the random seed to unsigned 256-bit seed.
+() set_seed(int) impure asm "SETRAND";
+;;; Mixes unsigned 256-bit integer x into the random seed r by setting the random seed to sha256 of the concatenation of two 32-byte strings: the first with the big-endian representation of the old seed r, and the second with the big-endian representation of x.
() randomize(int x) impure asm "ADDRAND";
+;;; Equivalent to randomize(cur_lt());.
() randomize_lt() impure asm "LTIME" "ADDRAND";
+
+;;; Checks whether the data parts of two slices coinside
+int equal_slice_bits (slice a, slice b) asm "SDEQ";
+
+;;; Concatenates two builders
+builder store_builder(builder to, builder from) asm "STBR";
diff --git a/crypto/vm/boc.cpp b/crypto/vm/boc.cpp
index d35291d3..f438d480 100644
--- a/crypto/vm/boc.cpp
+++ b/crypto/vm/boc.cpp
@@ -1039,9 +1039,15 @@ bool CellStorageStat::add_used_storage(Ref cs_ref, bool kill_dup,
bool CellStorageStat::add_used_storage(const CellSlice& cs, bool kill_dup, unsigned skip_count_root) {
if (!(skip_count_root & 1)) {
++cells;
+ if (cells > limit_cells) {
+ return false;
+ }
}
if (!(skip_count_root & 2)) {
bits += cs.size();
+ if (bits > limit_bits) {
+ return false;
+ }
}
for (unsigned i = 0; i < cs.size_refs(); i++) {
if (!add_used_storage(cs.prefetch_ref(i), kill_dup)) {
@@ -1054,9 +1060,15 @@ bool CellStorageStat::add_used_storage(const CellSlice& cs, bool kill_dup, unsig
bool CellStorageStat::add_used_storage(CellSlice&& cs, bool kill_dup, unsigned skip_count_root) {
if (!(skip_count_root & 1)) {
++cells;
+ if (cells > limit_cells) {
+ return false;
+ }
}
if (!(skip_count_root & 2)) {
bits += cs.size();
+ if (bits > limit_bits) {
+ return false;
+ }
}
while (cs.size_refs()) {
if (!add_used_storage(cs.fetch_ref(), kill_dup)) {
diff --git a/crypto/vm/boc.h b/crypto/vm/boc.h
index 2fae1846..dd74a6d1 100644
--- a/crypto/vm/boc.h
+++ b/crypto/vm/boc.h
@@ -117,8 +117,13 @@ struct CellStorageStat {
}
void clear() {
cells = bits = public_cells = 0;
+ clear_limit();
clear_seen();
}
+ void clear_limit() {
+ limit_cells = std::numeric_limits::max();
+ limit_bits = std::numeric_limits::max();
+ }
bool compute_used_storage(Ref cs_ref, bool kill_dup = true, unsigned skip_count_root = 0);
bool compute_used_storage(const CellSlice& cs, bool kill_dup = true, unsigned skip_count_root = 0);
bool compute_used_storage(CellSlice&& cs, bool kill_dup = true, unsigned skip_count_root = 0);
@@ -128,6 +133,9 @@ struct CellStorageStat {
bool add_used_storage(const CellSlice& cs, bool kill_dup = true, unsigned skip_count_root = 0);
bool add_used_storage(CellSlice&& cs, bool kill_dup = true, unsigned skip_count_root = 0);
bool add_used_storage(Ref cell, bool kill_dup = true, unsigned skip_count_root = 0);
+
+ unsigned long long limit_cells = std::numeric_limits::max();
+ unsigned long long limit_bits = std::numeric_limits::max();
};
struct VmStorageStat {
diff --git a/crypto/vm/db/DynamicBagOfCellsDb.cpp b/crypto/vm/db/DynamicBagOfCellsDb.cpp
index 4ff4ec30..5441feea 100644
--- a/crypto/vm/db/DynamicBagOfCellsDb.cpp
+++ b/crypto/vm/db/DynamicBagOfCellsDb.cpp
@@ -293,7 +293,9 @@ class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreat
return db_->load_cell(hash);
}
TRY_RESULT(load_result, cell_loader_->load(hash, true, *this));
- CHECK(load_result.status == CellLoader::LoadResult::Ok);
+ if (load_result.status != CellLoader::LoadResult::Ok) {
+ return td::Status::Error("cell not found");
+ }
return std::move(load_result.cell());
}
diff --git a/crypto/vm/vm.h b/crypto/vm/vm.h
index 44aa7f23..17ce1aa3 100644
--- a/crypto/vm/vm.h
+++ b/crypto/vm/vm.h
@@ -97,6 +97,7 @@ class VmState final : public VmStateInterface {
int stack_trace{0}, debug_off{0};
bool chksig_always_succeed{false};
td::ConstBitPtr missing_library{0};
+ td::uint16 max_data_depth = 512; // Default value
public:
enum {
@@ -108,8 +109,7 @@ class VmState final : public VmStateInterface {
implicit_jmpref_gas_price = 10,
implicit_ret_gas_price = 5,
free_stack_depth = 32,
- stack_entry_gas_price = 1,
- max_data_depth = 512
+ stack_entry_gas_price = 1
};
VmState();
VmState(Ref _code);
@@ -325,6 +325,9 @@ class VmState final : public VmStateInterface {
td::ConstBitPtr get_missing_library() const {
return missing_library;
}
+ void set_max_data_depth(td::uint16 depth) {
+ max_data_depth = depth;
+ }
private:
void init_cregs(bool same_c3 = false, bool push_0 = true);
diff --git a/dht/CMakeLists.txt b/dht/CMakeLists.txt
index 36cfdd82..e50a7497 100644
--- a/dht/CMakeLists.txt
+++ b/dht/CMakeLists.txt
@@ -30,3 +30,8 @@ target_include_directories(dht PUBLIC
)
target_link_libraries(dht PRIVATE tdutils tdactor adnl tl_api)
+add_executable(dht-ping-servers utils/dht-ping-servers.cpp)
+target_link_libraries(dht-ping-servers PRIVATE tdutils tdactor adnl dht terminal)
+
+add_executable(dht-resolve utils/dht-resolve.cpp)
+target_link_libraries(dht-resolve PRIVATE tdutils tdactor adnl dht terminal)
diff --git a/dht/dht-bucket.cpp b/dht/dht-bucket.cpp
index 4f9d75eb..f60f9961 100644
--- a/dht/dht-bucket.cpp
+++ b/dht/dht-bucket.cpp
@@ -66,38 +66,66 @@ td::uint32 DhtBucket::active_cnt() {
}
td::Status DhtBucket::add_full_node(DhtKeyId id, DhtNode newnode, td::actor::ActorId adnl,
- adnl::AdnlNodeIdShort self_id) {
+ adnl::AdnlNodeIdShort self_id, td::int32 our_network_id, bool set_active) {
for (auto &node : active_nodes_) {
if (node && node->get_key() == id) {
- return node->update_value(std::move(newnode), adnl, self_id);
+ if (set_active) {
+ return node->receive_ping(std::move(newnode), adnl, self_id);
+ } else {
+ return node->update_value(std::move(newnode), adnl, self_id);
+ }
}
}
- for (auto &node : backup_nodes_) {
+ for (size_t i = 0; i < backup_nodes_.size(); ++i) {
+ auto &node = backup_nodes_[i];
if (node && node->get_key() == id) {
- return node->update_value(std::move(newnode), adnl, self_id);
+ if (set_active) {
+ TRY_STATUS(node->receive_ping(std::move(newnode), adnl, self_id));
+ if (node->is_ready()) {
+ promote_node(i);
+ }
+ return td::Status::OK();
+ } else {
+ return node->update_value(std::move(newnode), adnl, self_id);
+ }
}
}
- TRY_RESULT_PREFIX(N, DhtRemoteNode::create(std::move(newnode), max_missed_pings_), "failed to add new node: ");
-
- for (auto &node : backup_nodes_) {
- if (node == nullptr) {
- node = std::move(N);
- return td::Status::OK();
+ TRY_RESULT_PREFIX(N, DhtRemoteNode::create(std::move(newnode), max_missed_pings_, our_network_id),
+ "failed to add new node: ");
+ if (set_active) {
+ for (auto &node : active_nodes_) {
+ if (node == nullptr) {
+ node = std::move(N);
+ node->receive_ping();
+ return td::Status::OK();
+ }
}
}
- for (auto &node : backup_nodes_) {
- CHECK(node);
- if (node->ready_from() == 0 && node->failed_from() + 60 < td::Time::now_cached()) {
- node = std::move(N);
- return td::Status::OK();
- }
+ size_t idx = select_backup_node_to_drop();
+ if (idx < backup_nodes_.size()) {
+ backup_nodes_[idx] = std::move(N);
}
-
return td::Status::OK();
}
+size_t DhtBucket::select_backup_node_to_drop() const {
+ size_t result = backup_nodes_.size();
+ for (size_t idx = 0; idx < backup_nodes_.size(); ++idx) {
+ const auto &node = backup_nodes_[idx];
+ if (node == nullptr) {
+ return idx;
+ }
+ if (node->ready_from() == 0 && node->failed_from() + 60 < td::Time::now_cached()) {
+ if (result == backup_nodes_.size() || node->failed_from() < backup_nodes_[result]->failed_from()) {
+ result = idx;
+ }
+ }
+ }
+ return result;
+}
+
void DhtBucket::receive_ping(DhtKeyId id, DhtNode result, td::actor::ActorId adnl,
adnl::AdnlNodeIdShort self_id) {
for (auto &node : active_nodes_) {
@@ -119,17 +147,9 @@ void DhtBucket::receive_ping(DhtKeyId id, DhtNode result, td::actor::ActorIdready_from() == 0 && node->failed_from() + 60 < td::Time::now_cached()) {
- node = std::move(active_nodes_[idx]);
- return;
- }
+ size_t new_idx = select_backup_node_to_drop();
+ if (new_idx < backup_nodes_.size()) {
+ backup_nodes_[new_idx] = std::move(active_nodes_[idx]);
}
active_nodes_[idx] = nullptr;
}
@@ -150,7 +170,7 @@ void DhtBucket::check(bool client_only, td::actor::ActorId adnl, td:
size_t have_space = 0;
for (size_t i = 0; i < active_nodes_.size(); i++) {
auto &node = active_nodes_[i];
- if (node && td::Time::now_cached() - node->last_ping_at() > ping_timeout_) {
+ if (node && td::Time::now_cached() - node->last_ping_at() > node->ping_interval()) {
node->send_ping(client_only, adnl, dht, src);
if (node->ready_from() == 0) {
demote_node(i);
@@ -162,7 +182,7 @@ void DhtBucket::check(bool client_only, td::actor::ActorId adnl, td:
}
for (size_t i = 0; i < backup_nodes_.size(); i++) {
auto &node = backup_nodes_[i];
- if (node && td::Time::now_cached() - node->last_ping_at() > ping_timeout_) {
+ if (node && td::Time::now_cached() - node->last_ping_at() > node->ping_interval()) {
node->send_ping(client_only, adnl, dht, src);
}
if (node && have_space > 0 && node->is_ready()) {
@@ -200,6 +220,9 @@ DhtNodesList DhtBucket::export_nodes() const {
list.push_back(node->get_node());
}
}
+ if (list.size() > k_) {
+ list.list().resize(k_);
+ }
return list;
}
diff --git a/dht/dht-bucket.hpp b/dht/dht-bucket.hpp
index 812f670d..e12fe6a4 100644
--- a/dht/dht-bucket.hpp
+++ b/dht/dht-bucket.hpp
@@ -31,7 +31,6 @@ class DhtMember;
class DhtBucket {
private:
- double ping_timeout_ = 60;
td::uint32 max_missed_pings_ = 3;
std::vector> active_nodes_;
@@ -43,6 +42,7 @@ class DhtBucket {
// const DhtMember::PrintId &print_id);
void demote_node(size_t idx);
void promote_node(size_t idx);
+ size_t select_backup_node_to_drop() const;
public:
DhtBucket(td::uint32 k) : k_(k) {
@@ -51,7 +51,7 @@ class DhtBucket {
}
td::uint32 active_cnt();
td::Status add_full_node(DhtKeyId id, DhtNode node, td::actor::ActorId adnl,
- adnl::AdnlNodeIdShort self_id);
+ adnl::AdnlNodeIdShort self_id, td::int32 our_network_id, bool set_active = false);
void check(bool client_only, td::actor::ActorId adnl, td::actor::ActorId node,
adnl::AdnlNodeIdShort src);
void receive_ping(DhtKeyId id, DhtNode result, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id);
diff --git a/dht/dht-in.hpp b/dht/dht-in.hpp
index c4f67819..59ce2184 100644
--- a/dht/dht-in.hpp
+++ b/dht/dht-in.hpp
@@ -46,6 +46,7 @@ class DhtMemberImpl : public DhtMember {
DhtKeyId key_;
td::uint32 k_;
td::uint32 a_;
+ td::int32 network_id_{-1};
td::uint32 max_cache_time_ = 60;
td::uint32 max_cache_size_ = 100;
@@ -66,6 +67,15 @@ class DhtMemberImpl : public DhtMember {
DhtKeyId last_republish_key_ = DhtKeyId::zero();
DhtKeyId last_check_key_ = DhtKeyId::zero();
+ adnl::AdnlNodeIdShort last_check_reverse_conn_ = adnl::AdnlNodeIdShort::zero();
+
+ struct ReverseConnection {
+ adnl::AdnlNodeIdShort dht_node_;
+ DhtKeyId key_id_;
+ td::Timestamp ttl_;
+ };
+ std::map reverse_connections_;
+ std::set our_reverse_connections_;
class Callback : public adnl::Adnl::Callback {
public:
@@ -122,17 +132,33 @@ class DhtMemberImpl : public DhtMember {
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_store &query, td::Promise promise);
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_getSignedAddressList &query,
td::Promise promise);
+ void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_registerReverseConnection &query,
+ td::Promise promise);
+ void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_requestReversePing &query,
+ td::Promise promise);
public:
DhtMemberImpl(adnl::AdnlNodeIdShort id, std::string db_root, td::actor::ActorId keyring,
- td::actor::ActorId adnl, td::uint32 k, td::uint32 a = 3, bool client_only = false)
- : id_(id), key_{id_}, k_(k), a_(a), db_root_(db_root), keyring_(keyring), adnl_(adnl), client_only_(client_only) {
+ td::actor::ActorId adnl, td::int32 network_id, td::uint32 k, td::uint32 a = 3,
+ bool client_only = false)
+ : id_(id)
+ , key_{id_}
+ , k_(k)
+ , a_(a)
+ , network_id_(network_id)
+ , db_root_(db_root)
+ , keyring_(keyring)
+ , adnl_(adnl)
+ , client_only_(client_only) {
for (size_t i = 0; i < 256; i++) {
buckets_.emplace_back(k_);
}
}
- void add_full_node(DhtKeyId id, DhtNode node) override;
+ void add_full_node(DhtKeyId id, DhtNode node) override {
+ add_full_node_impl(id, std::move(node));
+ }
+ void add_full_node_impl(DhtKeyId id, DhtNode node, bool set_active = false);
adnl::AdnlNodeIdShort get_id() const override {
return id_;
@@ -143,6 +169,12 @@ class DhtMemberImpl : public DhtMember {
void set_value(DhtValue key_value, td::Promise result) override;
td::uint32 distance(DhtKeyId key_id, td::uint32 max_value);
+ void register_reverse_connection(adnl::AdnlNodeIdFull client, td::Promise promise) override;
+ void request_reverse_ping(adnl::AdnlNode target, adnl::AdnlNodeIdShort client,
+ td::Promise promise) override;
+ void request_reverse_ping_cont(adnl::AdnlNode target, td::BufferSlice signature, adnl::AdnlNodeIdShort client,
+ td::Promise promise);
+
td::Status store_in(DhtValue value) override;
void send_store(DhtValue value, td::Promise promise);
diff --git a/dht/dht-node.cpp b/dht/dht-node.cpp
index 2b366ab3..409e3f68 100644
--- a/dht/dht-node.cpp
+++ b/dht/dht-node.cpp
@@ -23,31 +23,46 @@ namespace ton {
namespace dht {
-td::Status DhtNode::update(tl_object_ptr obj) {
+td::Status DhtNode::update(tl_object_ptr obj, td::int32 our_network_id) {
if (version_ && obj->version_ <= version_) {
return td::Status::Error(ErrorCode::notready, "too old version");
}
- auto signature = std::move(obj->signature_);
- auto B = serialize_tl_object(obj, true);
-
+ td::BufferSlice signature;
+ td::int32 network_id = -1;
+ if (obj->signature_.size() == 64) {
+ signature = std::move(obj->signature_);
+ } else if (obj->signature_.size() == 64 + 4) {
+ signature = td::BufferSlice{obj->signature_.as_slice().remove_prefix(4)};
+ network_id = *(td::uint32 *)obj->signature_.as_slice().remove_suffix(64).data();
+ } else {
+ return td::Status::Error(ErrorCode::notready, "invalid length of signature");
+ }
+ if (network_id != our_network_id && network_id != -1 && our_network_id != -1) {
+ // Remove (network_id != -1 && our_network_id != -1) after network update
+ return td::Status::Error(ErrorCode::notready, PSTRING() << "wrong network id (expected " << our_network_id
+ << ", found " << network_id << ")");
+ }
TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(obj->id_));
TRY_RESULT(addr_list, adnl::AdnlAddressList::create(std::move(obj->addr_list_)));
-
if (!addr_list.public_only()) {
return td::Status::Error(ErrorCode::notready, "dht node must have only public addresses");
}
if (!addr_list.size()) {
return td::Status::Error(ErrorCode::notready, "dht node must have >0 addresses");
}
+ DhtNode new_node{std::move(pub), std::move(addr_list), obj->version_, network_id, std::move(signature)};
+ TRY_STATUS(new_node.check_signature());
- TRY_RESULT(E, pub.pubkey().create_encryptor());
- TRY_STATUS(E->check_signature(B.as_slice(), signature.as_slice()));
-
- id_ = pub;
- addr_list_ = addr_list;
- version_ = obj->version_;
- signature_ = td::SharedSlice(signature.as_slice());
+ *this = std::move(new_node);
+ return td::Status::OK();
+}
+td::Status DhtNode::check_signature() const {
+ TRY_RESULT(enc, id_.pubkey().create_encryptor());
+ auto node2 = clone();
+ node2.signature_ = {};
+ TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(node2.tl(), true).as_slice(), signature_.as_slice()),
+ "bad node signature: ");
return td::Status::OK();
}
diff --git a/dht/dht-node.hpp b/dht/dht-node.hpp
index 03f8d134..d5860f91 100644
--- a/dht/dht-node.hpp
+++ b/dht/dht-node.hpp
@@ -22,6 +22,8 @@
#include "adnl/adnl-address-list.hpp"
#include "dht-types.h"
+#include "auto/tl/ton_api.hpp"
+#include "td/utils/overloaded.h"
namespace ton {
@@ -32,26 +34,26 @@ class DhtNode {
adnl::AdnlNodeIdFull id_;
adnl::AdnlAddressList addr_list_;
td::int32 version_{0};
+ td::int32 network_id_{-1};
td::SharedSlice signature_;
public:
- DhtNode() {
+ DhtNode() = default;
+ DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::int32 network_id, td::BufferSlice signature)
+ : id_(id), addr_list_(addr_list), version_(version), network_id_(network_id), signature_(signature.as_slice()) {
}
- DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::BufferSlice signature)
- : id_(id), addr_list_(addr_list), version_(version), signature_(signature.as_slice()) {
+ DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::int32 network_id, td::SharedSlice signature)
+ : id_(id), addr_list_(addr_list), version_(version), network_id_(network_id), signature_(std::move(signature)) {
}
- DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::SharedSlice signature)
- : id_(id), addr_list_(addr_list), version_(version), signature_(std::move(signature)) {
- }
- static td::Result create(tl_object_ptr obj) {
+ static td::Result create(tl_object_ptr obj, td::int32 our_network_id) {
if (obj->version_ == 0) {
return td::Status::Error(ErrorCode::protoviolation, "zero version");
}
DhtNode n;
- TRY_STATUS(n.update(std::move(obj)));
+ TRY_STATUS(n.update(std::move(obj), our_network_id));
return std::move(n);
}
- td::Status update(tl_object_ptr obj);
+ td::Status update(tl_object_ptr obj, td::int32 our_network_id);
DhtKeyId get_key() const {
CHECK(!id_.empty());
return DhtKeyId{id_.compute_short_id()};
@@ -68,20 +70,30 @@ class DhtNode {
}
tl_object_ptr tl() const {
- return create_tl_object(id_.tl(), addr_list_.tl(), version_, signature_.clone_as_buffer_slice());
+ td::BufferSlice signature_ext;
+ if (network_id_ == -1) {
+ signature_ext = signature_.clone_as_buffer_slice();
+ } else {
+ signature_ext = td::BufferSlice{4 + signature_.size()};
+ td::MutableSlice s = signature_ext.as_slice();
+ s.copy_from(td::Slice(reinterpret_cast(&network_id_), 4));
+ s.remove_prefix(4);
+ s.copy_from(signature_.as_slice());
+ }
+ return create_tl_object(id_.tl(), addr_list_.tl(), version_, std::move(signature_ext));
}
DhtNode clone() const {
- return DhtNode{id_, addr_list_, version_, signature_.clone()};
+ return DhtNode{id_, addr_list_, version_, network_id_, signature_.clone()};
}
+ td::Status check_signature() const;
};
class DhtNodesList {
public:
- DhtNodesList() {
- }
- DhtNodesList(tl_object_ptr R) {
+ DhtNodesList() = default;
+ DhtNodesList(tl_object_ptr R, td::int32 our_network_id) {
for (auto &n : R->nodes_) {
- auto N = DhtNode::create(std::move(n));
+ auto N = DhtNode::create(std::move(n), our_network_id);
if (N.is_ok()) {
list_.emplace_back(N.move_as_ok());
} else {
diff --git a/dht/dht-query.cpp b/dht/dht-query.cpp
index 7e1f1b92..bc61242d 100644
--- a/dht/dht-query.cpp
+++ b/dht/dht-query.cpp
@@ -20,14 +20,11 @@
#include "td/utils/tl_storers.h"
#include "td/utils/crypto.h"
-#include "td/utils/tl_parsers.h"
#include "td/utils/Random.h"
#include "td/utils/overloaded.h"
#include "td/utils/format.h"
-#include "keys/encryptor.h"
-
#include "auto/tl/ton_api.hpp"
#include "dht-query.hpp"
@@ -123,7 +120,7 @@ void DhtQueryFindNodes::on_result(td::Result R, adnl::AdnlNodeI
VLOG(DHT_WARNING) << this << ": incorrect result on dht.findNodes query from " << dst << ": "
<< Res.move_as_error();
} else {
- add_nodes(DhtNodesList{Res.move_as_ok()});
+ add_nodes(DhtNodesList{Res.move_as_ok(), our_network_id()});
}
finish_query();
}
@@ -149,6 +146,23 @@ void DhtQueryFindValue::send_one_query(adnl::AdnlNodeIdShort id) {
td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(B));
}
+void DhtQueryFindValue::send_one_query_nodes(adnl::AdnlNodeIdShort id) {
+ auto P = create_serialize_tl_object(get_key().tl(), get_k());
+ td::BufferSlice B;
+ if (client_only_) {
+ B = std::move(P);
+ } else {
+ B = create_serialize_tl_object_suffix(P.as_slice(), self_.tl());
+ }
+
+ auto Pr = td::PromiseCreator::lambda([SelfId = actor_id(this), dst = id](td::Result R) {
+ td::actor::send_closure(SelfId, &DhtQueryFindValue::on_result_nodes, std::move(R), dst);
+ });
+
+ td::actor::send_closure(adnl_, &adnl::Adnl::send_query, get_src(), id, "dht findValue", std::move(Pr),
+ td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(B));
+}
+
void DhtQueryFindValue::on_result(td::Result R, adnl::AdnlNodeIdShort dst) {
if (R.is_error()) {
VLOG(DHT_INFO) << this << ": failed find value query " << get_src() << "->" << dst << ": " << R.move_as_error();
@@ -164,44 +178,72 @@ void DhtQueryFindValue::on_result(td::Result R, adnl::AdnlNodeI
}
bool need_stop = false;
+ bool send_get_nodes = false;
auto A = Res.move_as_ok();
ton_api::downcast_call(
- *A.get(), td::overloaded(
- [&](ton_api::dht_valueFound &v) {
- auto valueR = DhtValue::create(std::move(v.value_), true);
- if (valueR.is_error()) {
- VLOG(DHT_WARNING) << this << ": received incorrect dht answer on find value query from " << dst
- << ": " << valueR.move_as_error();
- return;
- }
- auto value = valueR.move_as_ok();
- if (value.key_id() != key_) {
- VLOG(DHT_WARNING) << this << ": received value for bad key on find value query from " << dst;
- return;
- }
- promise_.set_value(std::move(value));
- need_stop = true;
- },
- [&](ton_api::dht_valueNotFound &v) { add_nodes(DhtNodesList{std::move(v.nodes_)}); }));
+ *A, td::overloaded(
+ [&](ton_api::dht_valueFound &v) {
+ auto valueR = DhtValue::create(std::move(v.value_), true);
+ if (valueR.is_error()) {
+ VLOG(DHT_WARNING) << this << ": received incorrect dht answer on find value query from " << dst
+ << ": " << valueR.move_as_error();
+ return;
+ }
+ auto value = valueR.move_as_ok();
+ if (value.key_id() != key_) {
+ VLOG(DHT_WARNING) << this << ": received value for bad key on find value query from " << dst;
+ return;
+ }
+ if (!value.check_is_acceptable()) {
+ send_get_nodes = true;
+ return;
+ }
+ promise_.set_value(std::move(value));
+ need_stop = true;
+ },
+ [&](ton_api::dht_valueNotFound &v) {
+ add_nodes(DhtNodesList{std::move(v.nodes_), our_network_id()});
+ }));
if (need_stop) {
stop();
+ } else if (send_get_nodes) {
+ send_one_query_nodes(dst);
} else {
finish_query();
}
}
+void DhtQueryFindValue::on_result_nodes(td::Result R, adnl::AdnlNodeIdShort dst) {
+ if (R.is_error()) {
+ VLOG(DHT_INFO) << this << ": failed find nodes query " << get_src() << "->" << dst << ": " << R.move_as_error();
+ finish_query();
+ return;
+ }
+ auto Res = fetch_tl_object(R.move_as_ok(), true);
+ if (Res.is_error()) {
+ VLOG(DHT_WARNING) << this << ": dropping incorrect answer on dht.findNodes query from " << dst << ": "
+ << Res.move_as_error();
+ finish_query();
+ return;
+ }
+ auto r = Res.move_as_ok();
+ add_nodes(DhtNodesList{create_tl_object(std::move(r->nodes_)), our_network_id()});
+ finish_query();
+}
+
void DhtQueryFindValue::finish(DhtNodesList list) {
promise_.set_error(td::Status::Error(ErrorCode::notready, "dht key not found"));
}
DhtQueryStore::DhtQueryStore(DhtValue key_value, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src,
- DhtNodesList list, td::uint32 k, td::uint32 a, DhtNode self, bool client_only,
- td::actor::ActorId node, td::actor::ActorId adnl,
+ DhtNodesList list, td::uint32 k, td::uint32 a, td::int32 our_network_id, DhtNode self,
+ bool client_only, td::actor::ActorId node, td::actor::ActorId adnl,
td::Promise promise)
: print_id_(print_id)
, k_(k)
, a_(a)
+ , our_network_id_(our_network_id)
, promise_(std::move(promise))
, value_(std::move(key_value))
, list_(std::move(list))
@@ -219,7 +261,8 @@ void DhtQueryStore::start_up() {
auto key = value_.key_id();
auto A = td::actor::create_actor("FindNodesQuery", key, print_id_, src_, std::move(list_), k_, a_,
- self_.clone(), client_only_, node_, adnl_, std::move(P));
+ our_network_id_, self_.clone(), client_only_, node_, adnl_,
+ std::move(P));
A.release();
}
@@ -279,6 +322,133 @@ void DhtQueryStore::store_ready(td::Result R) {
}
}
+DhtQueryRegisterReverseConnection::DhtQueryRegisterReverseConnection(
+ DhtKeyId key_id, adnl::AdnlNodeIdFull client, td::uint32 ttl, td::BufferSlice signature,
+ DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, td::uint32 k, td::uint32 a,
+ td::int32 our_network_id, DhtNode self, bool client_only, td::actor::ActorId node,
+ td::actor::ActorId adnl, td::Promise promise)
+ : print_id_(print_id)
+ , k_(k)
+ , a_(a)
+ , our_network_id_(our_network_id)
+ , promise_(std::move(promise))
+ , key_id_(key_id)
+ , list_(std::move(list))
+ , self_(std::move(self))
+ , client_only_(client_only) {
+ node_ = node;
+ adnl_ = adnl;
+ src_ = src;
+ query_ = create_serialize_tl_object(client.tl(), ttl, std::move(signature));
+}
+
+void DhtQueryRegisterReverseConnection::start_up() {
+ auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result res) {
+ td::actor::send_closure(SelfId, &DhtQueryRegisterReverseConnection::send_queries, std::move(res));
+ });
+
+ auto A = td::actor::create_actor("FindNodesQuery", key_id_, print_id_, src_, std::move(list_), k_,
+ a_, our_network_id_, self_.clone(), client_only_, node_, adnl_,
+ std::move(P));
+ A.release();
+}
+
+void DhtQueryRegisterReverseConnection::send_queries(td::Result R) {
+ if (R.is_error()) {
+ auto S = R.move_as_error();
+ VLOG(DHT_NOTICE) << this << ": failed to get nearest nodes to " << key_id_ << ": " << S;
+ promise_.set_error(std::move(S));
+ stop();
+ return;
+ }
+ auto list = R.move_as_ok();
+
+ remaining_ = static_cast(list.size());
+ if (remaining_ == 0) {
+ VLOG(DHT_NOTICE) << this << ": failed to get nearest nodes to " << key_id_ << ": no nodes";
+ promise_.set_error(td::Status::Error("no dht nodes"));
+ stop();
+ return;
+ }
+
+ for (auto &node : list.list()) {
+ auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) {
+ td::actor::send_closure(SelfId, &DhtQueryRegisterReverseConnection::ready, std::move(R));
+ });
+ td::actor::send_closure(adnl_, &adnl::Adnl::send_query, src_, node.adnl_id().compute_short_id(), "dht regrevcon",
+ std::move(P), td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), query_.clone());
+ }
+}
+
+void DhtQueryRegisterReverseConnection::ready(td::Result R) {
+ if (R.is_error()) {
+ fail_++;
+ VLOG(DHT_INFO) << this << ": failed register reverse connection query: " << R.move_as_error();
+ } else {
+ auto R2 = fetch_tl_object(R.move_as_ok(), true);
+ if (R2.is_error()) {
+ fail_++;
+ VLOG(DHT_WARNING) << this << ": can not parse answer (expected dht.stored): " << R2.move_as_error();
+ } else {
+ success_++;
+ }
+ }
+ CHECK(remaining_ > 0);
+ remaining_--;
+ if (remaining_ == 0) {
+ if (success_ > 0) {
+ promise_.set_value(td::Unit());
+ } else {
+ promise_.set_result(td::Status::Error("failed to make actual query"));
+ }
+ stop();
+ }
+}
+
+void DhtQueryRequestReversePing::send_one_query(adnl::AdnlNodeIdShort id) {
+ td::BufferSlice B;
+ if (client_only_) {
+ B = query_.clone();
+ } else {
+ B = create_serialize_tl_object_suffix(query_.as_slice(), self_.tl());
+ }
+ auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), dst = id](td::Result R) {
+ td::actor::send_closure(SelfId, &DhtQueryRequestReversePing::on_result, std::move(R), dst);
+ });
+ td::actor::send_closure(adnl_, &adnl::Adnl::send_query, get_src(), id, "dht requestReversePing", std::move(P),
+ td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(B));
+}
+
+void DhtQueryRequestReversePing::on_result(td::Result R, adnl::AdnlNodeIdShort dst) {
+ if (R.is_error()) {
+ VLOG(DHT_INFO) << this << ": failed reverse ping query " << get_src() << "->" << dst << ": " << R.move_as_error();
+ finish_query();
+ return;
+ }
+ auto Res = fetch_tl_object(R.move_as_ok(), true);
+ if (Res.is_error()) {
+ VLOG(DHT_WARNING) << this << ": dropping incorrect answer on dht.requestReversePing query from " << dst << ": "
+ << Res.move_as_error();
+ finish_query();
+ return;
+ }
+
+ auto A = Res.move_as_ok();
+ ton_api::downcast_call(*A, td::overloaded(
+ [&](ton_api::dht_reversePingOk &v) {
+ promise_.set_value(td::Unit());
+ stop();
+ },
+ [&](ton_api::dht_clientNotFound &v) {
+ add_nodes(DhtNodesList{std::move(v.nodes_), our_network_id()});
+ finish_query();
+ }));
+}
+
+void DhtQueryRequestReversePing::finish(DhtNodesList list) {
+ promise_.set_error(td::Status::Error(ErrorCode::notready, "dht key not found"));
+}
+
} // namespace dht
} // namespace ton
diff --git a/dht/dht-query.hpp b/dht/dht-query.hpp
index aa607f56..cf085e25 100644
--- a/dht/dht-query.hpp
+++ b/dht/dht-query.hpp
@@ -45,7 +45,7 @@ class DhtQuery : public td::actor::Actor {
public:
DhtQuery(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, td::uint32 k,
- td::uint32 a, DhtNode self, bool client_only, td::actor::ActorId node,
+ td::uint32 a, td::int32 our_network_id, DhtNode self, bool client_only, td::actor::ActorId node,
td::actor::ActorId adnl)
: key_(key)
, self_(std::move(self))
@@ -54,6 +54,7 @@ class DhtQuery : public td::actor::Actor {
, src_(src)
, k_(k)
, a_(a)
+ , our_network_id_(our_network_id)
, node_(node)
, adnl_(adnl) {
add_nodes(std::move(list));
@@ -77,6 +78,9 @@ class DhtQuery : public td::actor::Actor {
td::uint32 get_k() const {
return k_;
}
+ td::int32 our_network_id() const {
+ return our_network_id_;
+ }
void start_up() override {
send_queries();
}
@@ -91,6 +95,7 @@ class DhtQuery : public td::actor::Actor {
std::set pending_ids_;
td::uint32 k_;
td::uint32 a_;
+ td::int32 our_network_id_;
td::actor::ActorId node_;
td::uint32 active_queries_ = 0;
@@ -104,9 +109,10 @@ class DhtQueryFindNodes : public DhtQuery {
public:
DhtQueryFindNodes(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list,
- td::uint32 k, td::uint32 a, DhtNode self, bool client_only, td::actor::ActorId node,
- td::actor::ActorId adnl, td::Promise promise)
- : DhtQuery(key, print_id, src, std::move(list), k, a, std::move(self), client_only, node, adnl)
+ td::uint32 k, td::uint32 a, td::int32 our_network_id, DhtNode self, bool client_only,
+ td::actor::ActorId node, td::actor::ActorId adnl,
+ td::Promise promise)
+ : DhtQuery(key, print_id, src, std::move(list), k, a, our_network_id, std::move(self), client_only, node, adnl)
, promise_(std::move(promise)) {
}
void send_one_query(adnl::AdnlNodeIdShort id) override;
@@ -123,13 +129,16 @@ class DhtQueryFindValue : public DhtQuery {
public:
DhtQueryFindValue(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list,
- td::uint32 k, td::uint32 a, DhtNode self, bool client_only, td::actor::ActorId node,
- td::actor::ActorId adnl, td::Promise promise)
- : DhtQuery(key, print_id, src, std::move(list), k, a, std::move(self), client_only, node, adnl)
+ td::uint32 k, td::uint32 a, td::int32 our_network_id, DhtNode self, bool client_only,
+ td::actor::ActorId node, td::actor::ActorId adnl,
+ td::Promise promise)
+ : DhtQuery(key, print_id, src, std::move(list), k, a, our_network_id, std::move(self), client_only, node, adnl)
, promise_(std::move(promise)) {
}
void send_one_query(adnl::AdnlNodeIdShort id) override;
+ void send_one_query_nodes(adnl::AdnlNodeIdShort id);
void on_result(td::Result R, adnl::AdnlNodeIdShort dst);
+ void on_result_nodes(td::Result R, adnl::AdnlNodeIdShort dst);
void finish(DhtNodesList list) override;
std::string get_name() const override {
return "find value";
@@ -141,6 +150,7 @@ class DhtQueryStore : public td::actor::Actor {
DhtMember::PrintId print_id_;
td::uint32 k_;
td::uint32 a_;
+ td::int32 our_network_id_;
td::Promise promise_;
td::actor::ActorId node_;
td::actor::ActorId adnl_;
@@ -155,7 +165,7 @@ class DhtQueryStore : public td::actor::Actor {
public:
DhtQueryStore(DhtValue key_value, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list,
- td::uint32 k, td::uint32 a, DhtNode self, bool client_only, td::actor::ActorId node,
+ td::uint32 k, td::uint32 a, td::int32 our_network_id, DhtNode self, bool client_only, td::actor::ActorId node,
td::actor::ActorId adnl, td::Promise promise);
void send_stores(td::Result res);
void store_ready(td::Result res);
@@ -165,6 +175,64 @@ class DhtQueryStore : public td::actor::Actor {
}
};
+class DhtQueryRegisterReverseConnection : public td::actor::Actor {
+ private:
+ DhtMember::PrintId print_id_;
+ td::uint32 k_;
+ td::uint32 a_;
+ td::int32 our_network_id_;
+ td::Promise promise_;
+ td::actor::ActorId node_;
+ td::actor::ActorId adnl_;
+ adnl::AdnlNodeIdShort src_;
+ DhtKeyId key_id_;
+ td::BufferSlice query_;
+ td::uint32 success_ = 0;
+ td::uint32 fail_ = 0;
+ td::uint32 remaining_;
+ DhtNodesList list_;
+ DhtNode self_;
+ bool client_only_;
+
+ public:
+ DhtQueryRegisterReverseConnection(DhtKeyId key_id, adnl::AdnlNodeIdFull client, td::uint32 ttl,
+ td::BufferSlice signature, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src,
+ DhtNodesList list, td::uint32 k, td::uint32 a, td::int32 our_network_id,
+ DhtNode self, bool client_only, td::actor::ActorId node,
+ td::actor::ActorId adnl, td::Promise promise);
+ void send_queries(td::Result R);
+ void ready(td::Result R);
+ void start_up() override;
+ DhtMember::PrintId print_id() const {
+ return print_id_;
+ }
+};
+
+class DhtQueryRequestReversePing : public DhtQuery {
+ private:
+ td::Promise promise_;
+ td::BufferSlice query_;
+
+ public:
+ DhtQueryRequestReversePing(adnl::AdnlNodeIdShort client, adnl::AdnlNode target, td::BufferSlice signature,
+ DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, td::uint32 k,
+ td::uint32 a, td::int32 our_network_id, DhtNode self, bool client_only,
+ td::actor::ActorId node, td::actor::ActorId adnl,
+ td::Promise promise)
+ : DhtQuery(DhtMember::get_reverse_connection_key(client).compute_key_id(), print_id, src, std::move(list), k, a,
+ our_network_id, std::move(self), client_only, node, adnl)
+ , promise_(std::move(promise))
+ , query_(create_serialize_tl_object(target.tl(), std::move(signature),
+ client.bits256_value(), k)) {
+ }
+ void send_one_query(adnl::AdnlNodeIdShort id) override;
+ void on_result(td::Result R, adnl::AdnlNodeIdShort dst);
+ void finish(DhtNodesList list) override;
+ std::string get_name() const override {
+ return "request remote ping";
+ }
+};
+
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtQuery &dht) {
sb << dht.print_id();
return sb;
diff --git a/dht/dht-remote-node.cpp b/dht/dht-remote-node.cpp
index f1ea2197..653de256 100644
--- a/dht/dht-remote-node.cpp
+++ b/dht/dht-remote-node.cpp
@@ -20,13 +20,10 @@
#include "td/utils/tl_storers.h"
#include "td/utils/crypto.h"
-#include "td/utils/tl_parsers.h"
#include "td/utils/Random.h"
#include "td/utils/format.h"
-#include "keys/encryptor.h"
-
#include "auto/tl/ton_api.hpp"
#include "dht-remote-node.hpp"
@@ -35,28 +32,43 @@ namespace ton {
namespace dht {
+static const double PING_INTERVAL_DEFAULT = 60.0;
+static const double PING_INTERVAL_MULTIPLIER = 1.1;
+static const double PING_INTERVAL_MAX = 3600.0 * 4;
+
+DhtRemoteNode::DhtRemoteNode(DhtNode node, td::uint32 max_missed_pings, td::int32 our_network_id)
+ : node_(std::move(node))
+ , max_missed_pings_(max_missed_pings)
+ , our_network_id_(our_network_id)
+ , ping_interval_(PING_INTERVAL_DEFAULT) {
+ failed_from_ = td::Time::now_cached();
+ id_ = node_.get_key();
+}
+
td::Status DhtRemoteNode::receive_ping(DhtNode node, td::actor::ActorId adnl,
adnl::AdnlNodeIdShort self_id) {
TRY_STATUS(update_value(std::move(node), adnl, self_id));
+ receive_ping();
+ return td::Status::OK();
+}
+
+void DhtRemoteNode::receive_ping() {
missed_pings_ = 0;
+ ping_interval_ = PING_INTERVAL_DEFAULT;
if (ready_from_ == 0) {
ready_from_ = td::Time::now_cached();
}
- return td::Status::OK();
}
td::Status DhtRemoteNode::update_value(DhtNode node, td::actor::ActorId adnl,
adnl::AdnlNodeIdShort self_id) {
- CHECK(node.adnl_id() == node_.adnl_id());
+ if (node.adnl_id() != node_.adnl_id()) {
+ return td::Status::Error("Wrong adnl id");
+ }
if (node.version() <= node_.version()) {
return td::Status::OK();
}
-
- TRY_RESULT(enc, node.adnl_id().pubkey().create_encryptor());
- auto tl = node.tl();
- auto sig = std::move(tl->signature_);
- TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(tl, true).as_slice(), sig.as_slice()),
- "bad node signature: ");
+ TRY_STATUS(node.check_signature());
node_ = std::move(node);
td::actor::send_closure(adnl, &adnl::Adnl::add_peer, self_id, node_.adnl_id(), node_.addr_list());
@@ -66,22 +78,25 @@ td::Status DhtRemoteNode::update_value(DhtNode node, td::actor::ActorId adnl, td::actor::ActorId node,
adnl::AdnlNodeIdShort src) {
missed_pings_++;
- if (missed_pings_ > max_missed_pings_ && ready_from_ > 0) {
- ready_from_ = 0;
- failed_from_ = td::Time::now_cached();
+ if (missed_pings_ > max_missed_pings_) {
+ ping_interval_ = std::min(ping_interval_ * PING_INTERVAL_MULTIPLIER, PING_INTERVAL_MAX);
+ if (ready_from_ > 0) {
+ ready_from_ = 0;
+ failed_from_ = td::Time::now_cached();
+ }
}
last_ping_at_ = td::Time::now_cached();
td::actor::send_closure(adnl, &adnl::Adnl::add_peer, src, node_.adnl_id(), node_.addr_list());
- auto P = td::PromiseCreator::lambda([key = id_, id = node_.adnl_id().compute_short_id(), client_only, node, src,
- adnl](td::Result R) mutable {
+ auto P = td::PromiseCreator::lambda([key = id_, id = node_.adnl_id().compute_short_id(), client_only, node, src, adnl,
+ our_network_id = our_network_id_](td::Result R) mutable {
if (R.is_error()) {
LOG(ERROR) << "[dht]: failed to get self node";
return;
}
- auto P = td::PromiseCreator::lambda([key, node, adnl](td::Result R) {
+ auto P = td::PromiseCreator::lambda([key, node, adnl, our_network_id](td::Result R) {
if (R.is_error()) {
VLOG(DHT_INFO) << "[dht]: received error for query to " << key << ": " << R.move_as_error();
return;
@@ -89,7 +104,7 @@ void DhtRemoteNode::send_ping(bool client_only, td::actor::ActorId a
auto F = fetch_tl_object(R.move_as_ok(), true);
if (F.is_ok()) {
- auto N = DhtNode::create(F.move_as_ok());
+ auto N = DhtNode::create(F.move_as_ok(), our_network_id);
if (N.is_ok()) {
td::actor::send_closure(node, &DhtMember::receive_ping, key, N.move_as_ok());
} else {
@@ -123,7 +138,8 @@ adnl::AdnlNodeIdFull DhtRemoteNode::get_full_id() const {
return node_.adnl_id();
}
-td::Result> DhtRemoteNode::create(DhtNode node, td::uint32 max_missed_pings) {
+td::Result> DhtRemoteNode::create(DhtNode node, td::uint32 max_missed_pings,
+ td::int32 our_network_id) {
TRY_RESULT(enc, node.adnl_id().pubkey().create_encryptor());
auto tl = node.tl();
auto sig = std::move(tl->signature_);
@@ -131,7 +147,7 @@ td::Result> DhtRemoteNode::create(DhtNode node, t
TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(tl, true).as_slice(), sig.as_slice()),
"bad node signature: ");
- return std::make_unique(std::move(node), max_missed_pings);
+ return std::make_unique(std::move(node), max_missed_pings, our_network_id);
}
} // namespace dht
diff --git a/dht/dht-remote-node.hpp b/dht/dht-remote-node.hpp
index e65c0429..8e7db489 100644
--- a/dht/dht-remote-node.hpp
+++ b/dht/dht-remote-node.hpp
@@ -40,19 +40,18 @@ class DhtRemoteNode {
DhtNode node_;
td::uint32 max_missed_pings_;
+ td::int32 our_network_id_;
td::uint32 missed_pings_ = 0;
double last_ping_at_ = 0;
double ready_from_ = 0;
double failed_from_ = 0;
+ double ping_interval_;
td::int32 version_;
public:
- DhtRemoteNode(DhtNode node, td::uint32 max_missed_pings)
- : node_(std::move(node)), max_missed_pings_(max_missed_pings) {
- failed_from_ = td::Time::now_cached();
- id_ = node_.get_key();
- }
- static td::Result> create(DhtNode node, td::uint32 max_missed_pings);
+ DhtRemoteNode(DhtNode node, td::uint32 max_missed_pings, td::int32 our_network_id);
+ static td::Result> create(DhtNode node, td::uint32 max_missed_pings,
+ td::int32 our_network_id);
DhtNode get_node() const {
return node_.clone();
}
@@ -76,9 +75,13 @@ class DhtRemoteNode {
double last_ping_at() const {
return last_ping_at_;
}
+ double ping_interval() const {
+ return ping_interval_;
+ }
void send_ping(bool client_only, td::actor::ActorId adnl, td::actor::ActorId node,
adnl::AdnlNodeIdShort src);
td::Status receive_ping(DhtNode node, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id);
+ void receive_ping();
td::Status update_value(DhtNode node, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id);
};
diff --git a/dht/dht-types.cpp b/dht/dht-types.cpp
index 89949bc1..118df2a8 100644
--- a/dht/dht-types.cpp
+++ b/dht/dht-types.cpp
@@ -209,6 +209,10 @@ td::Status DhtValue::check() const {
return key_.update_rule()->check_value(*this);
}
+bool DhtValue::check_is_acceptable() const {
+ return key_.update_rule()->check_is_acceptable(*this);
+}
+
DhtKeyId DhtValue::key_id() const {
return key_.key().compute_key_id();
}
@@ -360,6 +364,21 @@ td::Status DhtUpdateRuleOverlayNodes::update_value(DhtValue &value, DhtValue &&n
return td::Status::OK();
}
+bool DhtUpdateRuleOverlayNodes::check_is_acceptable(const ton::dht::DhtValue &value) {
+ auto F = fetch_tl_object(value.value().clone_as_buffer_slice(), true);
+ if (F.is_error()) {
+ return false;
+ }
+ auto L = F.move_as_ok();
+ auto now = td::Clocks::system();
+ for (auto &node : L->nodes_) {
+ if (node->version_ + 600 > now) {
+ return true;
+ }
+ }
+ return false;
+}
+
tl_object_ptr DhtUpdateRuleOverlayNodes::tl() const {
return create_tl_object();
}
diff --git a/dht/dht-types.h b/dht/dht-types.h
index 75efee55..45657d45 100644
--- a/dht/dht-types.h
+++ b/dht/dht-types.h
@@ -119,6 +119,9 @@ class DhtUpdateRule {
virtual td::Status check_value(const DhtValue &value) = 0;
virtual td::Status update_value(DhtValue &value, DhtValue &&new_value) = 0;
virtual bool need_republish() const = 0;
+ virtual bool check_is_acceptable(const DhtValue &value) {
+ return true;
+ }
virtual tl_object_ptr tl() const = 0;
static td::Result> create(tl_object_ptr obj);
};
@@ -210,6 +213,7 @@ class DhtValue {
void update_signature(td::BufferSlice signature);
void update_signature(td::SharedSlice signature);
td::Status check() const;
+ bool check_is_acceptable() const;
DhtKeyId key_id() const;
@@ -249,6 +253,7 @@ class DhtUpdateRuleOverlayNodes : public DhtUpdateRule {
bool need_republish() const override {
return false;
}
+ bool check_is_acceptable(const DhtValue &value) override;
tl_object_ptr tl() const override;
static td::Result> create();
};
diff --git a/dht/dht.cpp b/dht/dht.cpp
index 0d441427..e1e20d45 100644
--- a/dht/dht.cpp
+++ b/dht/dht.cpp
@@ -20,7 +20,6 @@
#include "td/utils/tl_storers.h"
#include "td/utils/crypto.h"
-#include "td/utils/tl_parsers.h"
#include "td/utils/Random.h"
#include "td/utils/base64.h"
@@ -28,9 +27,6 @@
#include "td/db/RocksDb.h"
-#include "keys/encryptor.h"
-#include "adnl/utils.hpp"
-
#include "auto/tl/ton_api.hpp"
#include "dht.h"
@@ -44,10 +40,9 @@ namespace dht {
td::actor::ActorOwn DhtMember::create(adnl::AdnlNodeIdShort id, std::string db_root,
td::actor::ActorId keyring,
- td::actor::ActorId adnl, td::uint32 k, td::uint32 a,
- bool client_only) {
- return td::actor::ActorOwn(
- td::actor::create_actor("dht", id, db_root, keyring, adnl, k, a, client_only));
+ td::actor::ActorId adnl, td::int32 network_id,
+ td::uint32 k, td::uint32 a, bool client_only) {
+ return td::actor::create_actor("dht", id, db_root, keyring, adnl, network_id, k, a, client_only);
}
td::Result> Dht::create(adnl::AdnlNodeIdShort id, std::string db_root,
@@ -57,7 +52,7 @@ td::Result> Dht::create(adnl::AdnlNodeIdShort id, std::
CHECK(conf->get_k() > 0);
CHECK(conf->get_a() > 0);
- auto D = DhtMember::create(id, db_root, keyring, adnl, conf->get_k(), conf->get_a());
+ auto D = DhtMember::create(id, db_root, keyring, adnl, conf->get_network_id(), conf->get_k(), conf->get_a());
auto &nodes = conf->nodes();
for (auto &node : nodes.list()) {
@@ -74,7 +69,7 @@ td::Result> Dht::create_client(adnl::AdnlNodeIdShort id
CHECK(conf->get_k() > 0);
CHECK(conf->get_a() > 0);
- auto D = DhtMember::create(id, db_root, keyring, adnl, conf->get_k(), conf->get_a(), true);
+ auto D = DhtMember::create(id, db_root, keyring, adnl, conf->get_network_id(), conf->get_k(), conf->get_a(), true);
auto &nodes = conf->nodes();
for (auto &node : nodes.list()) {
@@ -90,8 +85,11 @@ void DhtMemberImpl::start_up() {
ton_api::dht_findValue::ID,
ton_api::dht_store::ID,
ton_api::dht_ping::ID,
+ ton_api::dht_registerReverseConnection::ID,
+ ton_api::dht_requestReversePing::ID,
ton_api::dht_query::ID,
- ton_api::dht_message::ID};
+ ton_api::dht_message::ID,
+ ton_api::dht_requestReversePingCont::ID};
for (auto it : methods) {
td::actor::send_closure(adnl_, &adnl::Adnl::subscribe, id_, adnl::Adnl::int_to_bytestring(it),
@@ -112,12 +110,12 @@ void DhtMemberImpl::start_up() {
V.ensure();
auto nodes = std::move(V.move_as_ok()->nodes_);
auto s = nodes->nodes_.size();
- DhtNodesList list{std::move(nodes)};
- CHECK(list.size() == s);
+ DhtNodesList list{std::move(nodes), network_id_};
+ CHECK(list.size() <= s); // Some nodes can be dropped due to a wrong network id
auto &B = buckets_[bit];
for (auto &node : list.list()) {
auto key = node.get_key();
- B.add_full_node(key, std::move(node), adnl_, id_);
+ B.add_full_node(key, std::move(node), adnl_, id_, network_id_);
}
}
}
@@ -131,8 +129,11 @@ void DhtMemberImpl::tear_down() {
ton_api::dht_findValue::ID,
ton_api::dht_store::ID,
ton_api::dht_ping::ID,
+ ton_api::dht_registerReverseConnection::ID,
+ ton_api::dht_requestReversePing::ID,
ton_api::dht_query::ID,
- ton_api::dht_message::ID};
+ ton_api::dht_message::ID,
+ ton_api::dht_requestReversePingCont::ID};
for (auto it : methods) {
td::actor::send_closure(adnl_, &adnl::Adnl::unsubscribe, id_, adnl::Adnl::int_to_bytestring(it));
@@ -299,6 +300,61 @@ void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_getSig
get_self_node(std::move(P));
}
+static td::BufferSlice register_reverse_connection_to_sign(adnl::AdnlNodeIdShort client, adnl::AdnlNodeIdShort dht_id,
+ td::uint32 ttl) {
+ td::BufferSlice result(32 + 32 + 4);
+ td::MutableSlice s = result.as_slice();
+ s.copy_from(client.as_slice());
+ s.remove_prefix(32);
+ s.copy_from(dht_id.as_slice());
+ s.remove_prefix(32);
+ s.copy_from(std::string(reinterpret_cast(&ttl), 4));
+ return result;
+}
+
+void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_registerReverseConnection &query,
+ td::Promise promise) {
+ td::uint32 ttl = query.ttl_, now = (td::uint32)td::Clocks::system();
+ if (ttl <= now) {
+ return;
+ }
+ PublicKey pub{query.node_};
+ adnl::AdnlNodeIdShort client_id{pub.compute_short_id()};
+ td::BufferSlice to_sign = register_reverse_connection_to_sign(client_id, src, ttl);
+ TRY_RESULT_PROMISE(promise, encryptor, pub.create_encryptor());
+ TRY_STATUS_PROMISE(promise, encryptor->check_signature(to_sign, query.signature_));
+ DhtKeyId key_id = get_reverse_connection_key(client_id).compute_key_id();
+ reverse_connections_[client_id] = ReverseConnection{src, key_id, td::Timestamp::at_unix(std::min(ttl, now + 300))};
+ promise.set_value(create_serialize_tl_object());
+}
+
+void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_requestReversePing &query,
+ td::Promise promise) {
+ adnl::AdnlNodeIdShort client{query.client_};
+ auto it = reverse_connections_.find(client);
+ if (it != reverse_connections_.end()) {
+ if (it->second.ttl_.is_in_past()) {
+ reverse_connections_.erase(it);
+ } else {
+ PublicKey pub{query.target_->id_};
+ TRY_RESULT_PROMISE(promise, encryptor, pub.create_encryptor());
+ TRY_STATUS_PROMISE(promise,
+ encryptor->check_signature(serialize_tl_object(query.target_, true), query.signature_));
+ td::actor::send_closure(adnl_, &adnl::Adnl::send_message, id_, it->second.dht_node_,
+ create_serialize_tl_object(
+ std::move(query.target_), std::move(query.signature_), query.client_));
+ promise.set_result(create_serialize_tl_object]