> For the complete documentation index, see [llms.txt](https://surftest.gitbook.io/axelar-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://surftest.gitbook.io/axelar-wiki/english/cosmos-sdk-state-sync.md).

# Cosmos SDK State Sync

Для чего нужен. Как создать. Как использовать.

Snapshots that based on [State-sync](https://medium.com/tendermint/tendermint-core-state-sync-for-developers-70a96ba3ee35), a feature built into the Cosmos SDK that allows validators to quickly synchronize by downloading a network snapshot from an RPC node.

The advantage of State Sync synchronization is that a snapshot weighs very little compared to a fully synchronized blockchain database. Synchronizing with a **State-sync snapshot** reduces the time it takes for a Validator or Sentry nodes to synchronize with the network to a few minutes. In addition, this method saves a lot of disk space. By synchronizing with the network using state-sync, the node avoids going through all update procedures and can only synchronize with the most recent binary update. There must be only one private-validator-state.json file in the data folder for synchronization. It is not recommended to produce and distribute to the community snapshots on a node with a validator, since distribution requires the operation of the node in the public blockchain network, while this exposes the Validator to the risk of DDoS attacks.

The downside of this synchronization option is that instead of the full transaction history, there is a snapshot of the last state of the network, which was saved by RPC with state-sync enabled. Therefore, **for nodes running to transfer data to sites and dapps** that require information about all transactions, synchronization using Snapshot **is not suitable**.

Official resources:

* Information about [RPC](https://docs.tendermint.com/master/tendermint-core/rpc.html)
* [Snapshot](https://docs.tendermint.com/master/spec/abci/apps.html#snapshot-connection) Theory&#x20;
* [State Sync](https://docs.tendermint.com/master/tendermint-core/state-sync/)
* [State Sync Snapshotting](https://pkg.go.dev/github.com/cosmos/cosmos-sdk/snapshots) Article on the Go website
* Another article from Erik Grinaker "[Tendermint Core State Sync](https://medium.com/tendermint/tendermint-core-state-sync-for-developers-70a96ba3ee35)"&#x20;
* Article by Erik Grinaker published on the Cosmos Blog [Cosmos SDK State Sync Guide](https://blog.cosmos.network/cosmos-sdk-state-sync-guide-99e4cf43be2f)

## Setting RPC options for State-sync snapshots

{% hint style="success" %}
You have to configure in your app.toml snapshot-interval and snapshot-keep-recent.
{% endhint %}

### Setting app.toml

You need to change certain parameters that are required to create a Snapshot. We will set them in the app.toml file

#### Snapshot-interval and snapshot-keep-recent

```python
sudo nano ~/.axelar/config/app.toml
```

To create snapshots, you need to set the `snapshot-interval` parameter. This is a parameter that sets the interval of blocks through which snapshots will be created.

For example, let's set it to 1000.\
We go down to the end of the file in the <mark style="color:green;">State Sync Configuration</mark> section. Set the values ​​below:

```python
# snapshot-interval specifies the block interval at which local state sync snapshots are
# taken (0 to disable). Must be a multiple of pruning-keep-every.
snapshot-interval = 1000

# snapshot-keep-recent specifies the number of recent snapshots to keep and serve (0 to keep all).
snapshot-keep-recent = 2
```

Рекомендуется сохранять как минимум 2 последних снэпшота, чтобы снэпшот не удалялся нодой-донором, в то время, пока нода-приемник пытается скачать файл снэпшота.  Для этого ставим `snapshot-keep-recent=2`.

It is recommended to save at least 2 recent snapshots so that the snapshot is not deleted by the donor node while the destination node is trying to download the snapshot file. To do this, set `snapshot-keep-recent=2`.

{% hint style="info" %}
`snapshot-interval` must be a multiple of the `pruning-keep-every` parameter to protect the snapshot from prunning while the snapshot is being written and writing the wrong block height to the snapshot.
{% endhint %}

#### Pruning

Если раньше вы уже настраивали pruning для экономии места на диске, то теперь надо изменить, подставив `pruning-keep-every = "1000"` ( так как snapshot-interval = 1000):

If you have already configured pruning to save disk space before, now you need to change it by substituting `pruning-keep-every = "1000"` (since `snapshot-interval = 1000`):

```python
pruning = "custom"
pruning-keep-recent = "100"
pruning-keep-every = "1000"
pruning-interval = "10"
```

### Setting config.toml

{% hint style="success" %}
You have to open your rpc port and configure it in config.toml to listen the desire address ( 0.0.0.0 if you want to listen to all )
{% endhint %}

#### Port и ip

Open the config.toml file

```python
sudo nano ~/.axelar/config/config.toml
```

and change the parameter as written below

```python
#######################################################################
###                 Advanced Configuration Options                  ###
#######################################################################

#######################################################
###       RPC Server Configuration Options          ###
#######################################################
[rpc]

# TCP or UNIX socket address for the RPC server to listen on
laddr = "tcp://0.0.0.0:26657"


```

Setting  `laddr = "tcp://0.0.0.0:26657"` allows listening to all addresses from port 26657.

### Configuring Visible RPC

{% hint style="success" %}
Also you have to open the p2p too . Clients needs it to check snapshots . add your rpcaddress:port on peers of clients
{% endhint %}

Opening the config file:

```python
sudo nano ~/.axelar/config/config.toml
```

set the `pex` parameter to allow our RPC to work on the public network

```python
###################################
###  P2P Configuration Options  ###
###################################
...

pex = true
```

Now our RPC is open and other nodes can be loaded from it.

### Restart and waiting for a snapshot

{% hint style="success" %}
restart yoru service and wait for your node create the snapshots in data/snapshots
{% endhint %}

Now restart the node for the changes to take effect.

Waiting for the production of snapshots. By setting above, snapshots will be taken once every 1000 blocks.

Snapshots will be saved to `~/.axelar/data/snapshots/`

### Usage

Now that the snapshot has been taken, check the result on another node:

```python
SNAP_RPC="http://<your_RPC_Snapshot_ip>:26657"

LATEST_HEIGHT=$(curl -s $SNAP_RPC/block | jq -r .result.block.header.height); \
BLOCK_HEIGHT=$((LATEST_HEIGHT - 1000)); \
TRUST_HASH=$(curl -s "$SNAP_RPC/block?height=$BLOCK_HEIGHT" | jq -r .result.block_id.hash)

echo $LATEST_HEIGHT $BLOCK_HEIGHT $TRUST_HASH
```

{% hint style="warning" %}
If `snapshot-interval` is set differently on RPC, then In the line `BLOCK_HEIGHT=$((LATEST_HEIGHT - 1000)); \` you need to substitute a number equal to your `snapshot-interval` instead of 1000.
{% endhint %}
