Skip to content

Commit

Permalink
added pruning feature
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Mar 29, 2023
1 parent 6e9aa8a commit fae652f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 11 deletions.
65 changes: 59 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,60 @@ If you run this command without a `--target-height` it will sync all blocks whic
available in the pool. KSYNC will automatically exit once a target height is reached, or you can simply exit the sync
process by killing KSYNC with CMD+C.

## Live Example: Sync Cosmos Hub over P2P-SYNC
## Examples

All examples below use test data from a KYVE test chain running on `http://35.158.99.65:26657`. This should not be
used in production and is only intended for demonstration purposes.

### 1. Sync Osmosis with DB-SYNC

To sync osmosis you have to download and set up the correct osmosis binary. To sync from genesis the version `v3.1.0` has
to be used. You can download them [here](https://github.com/osmosis-labs/osmosis/releases/tag/v3.1.0) or build them from source: [https://github.com/osmosis-labs/osmosis](https://github.com/osmosis-labs/osmosis)

Verify installation with

```bash
./osmosisd version
3.1.0
```

After the installation init the config

```bash
./osmosisd init <your-moniker> --chain-id osmosis-1
```

download the genesis

```bash
wget -O ~/.osmosisd/config/genesis.json https://github.com/osmosis-labs/networks/raw/main/osmosis-1/genesis.json
```

Important: Don't include an addrbook.json and make sure persistent_peers and etc. are empty for now or else the node will connect to other peers. It should only connect
to our peer.

when the config is done the node can be started

```bash
./osmosisd start --with-tendermint=false
```

After you see that the node is waiting for incoming connections you can open a **new** terminal and start
the sync.

```bash
./ksync start --mode=db --home="/Users/<user>/.osmosisd" --pool-id=3 --rest=http://35.158.99.65:1317
```

You should see KSYNC connecting to Osmosis and applying the blocks against the app. After the ~600 blocks were
applied KSYNC automatically exits.

When you want to continue to sync normally you can now add an addrbook or add peers in `persistent_peers`. When you start
the node again with the normal start command `./osmosisd start` the node should continue normally and tries to sync the remaining blocks.

### 2. Sync Cosmos Hub over P2P-SYNC

Since we want to sync Cosmos Hub from genesis and the genesis file is bigger than 100MB we have to use P2P sync.

To sync cosmos you have to download and set up the correct gaia binary. To sync from genesis the version `v4.2.1` has
to be used. You can download them [here](https://github.com/cosmos/gaia/releases/tag/v4.2.1) or build them from source:
Expand Down Expand Up @@ -242,18 +295,18 @@ allow_duplicate_ip = true
Important: Don't include an addrbook.json and make sure persistent_peers and etc. are empty for now or else the node
will connect to other peers. It should only connect to our peer.

when the config is done the node can be started. NOTE: this can take a while (~5mins) since the genesis file is
quite big.
When the config is done the node can be started. NOTE: this can take a while (~5mins) since the genesis file is
quite big. You can skip invariants checks to boot even fast, but it still takes a long time until the gaia node starts.

```bash
./gaiad start --x-crisis-skip-assert-invariants
```

After you see that the node is searching for peers you can start the tool. You can see the latest height which KYVE
has archived [here](https://app.korellia.kyve.network/#/pools/24) under _Latest Key_
After you see that the node is searching for peers you can start the tool. For testing KYVE has archived the first
5000 blocks of Cosmos Hub, so after that height is reached the sync will be done.

```bash
ksync start --mode=p2p --home="/Users/<user>/.gaia" --pool-id=24 --rest=https://api.korellia.kyve.network
ksync start --mode=p2p --home="/Users/<user>/.gaia" --pool-id=0 --rest=http://35.158.99.65:1317
```

You should see the peer connecting and sending over blocks to the gaia node. After all the blocks have been applied
Expand Down
59 changes: 59 additions & 0 deletions cmd/ksync/commands/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package commands

import (
"fmt"
cfg "github.com/KYVENetwork/ksync/config"
"github.com/KYVENetwork/ksync/executor/db/store"
"github.com/spf13/cobra"
"os"
)

var (
untilHeight int64
)

func init() {
pruneCmd.Flags().StringVar(&home, "home", "", "home directory")
if err := pruneCmd.MarkFlagRequired("home"); err != nil {
panic(fmt.Errorf("flag 'home' should be required: %w", err))
}

pruneCmd.Flags().Int64Var(&untilHeight, "until-height", 0, "prune blocks until this height (excluding)")
if err := pruneCmd.MarkFlagRequired("until-height"); err != nil {
panic(fmt.Errorf("flag 'until-height' should be required: %w", err))
}

rootCmd.AddCommand(pruneCmd)
}

var pruneCmd = &cobra.Command{
Use: "prune-blocks",
Short: "Prune blocks until a specific height",
Run: func(cmd *cobra.Command, args []string) {
config, err := cfg.LoadConfig(home)
if err != nil {
panic(fmt.Errorf("failed to load config: %w", err))
}

blockStoreDB, blockStore, err := store.GetBlockstoreDBs(config)
defer blockStoreDB.Close()

if err != nil {
panic(fmt.Errorf("failed to load blockstore db: %w", err))
}

base := blockStore.Base()

if untilHeight < base {
fmt.Printf("Error: base height %d is higher than prune height %d\n", base, untilHeight)
os.Exit(0)
}

blocks, err := blockStore.PruneBlocks(untilHeight)
if err != nil {
panic(err)
}

fmt.Printf("Pruned %d blocks, new base height is %d\n", blocks, blockStore.Base())
},
}
2 changes: 1 addition & 1 deletion cmd/ksync/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
func getVersion() string {
version, ok := debug.ReadBuildInfo()
if !ok {
panic("failed to get cosmovisor version")
panic("failed to get ksync version")
}

return strings.TrimSpace(version.Main.Version)
Expand Down
3 changes: 0 additions & 3 deletions executor/p2p/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package helpers

import (
"fmt"
bcv0 "github.com/tendermint/tendermint/blockchain/v0"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -41,8 +40,6 @@ func MakeNodeInfo(

nodeInfo.ListenAddr = lAddr

fmt.Println(fmt.Sprintf("%v", nodeInfo))

err := nodeInfo.Validate()
return nodeInfo, err
}
Expand Down
2 changes: 1 addition & 1 deletion utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package utils
const (
BundlesPageLimit = 100
DefaultRestEndpoint = "https://api-eu-1.kyve.network"
KSyncRuntime = "@kyvejs/tendermint-bsync"
KSyncRuntime = "@kyvejs/tendermint"
)

0 comments on commit fae652f

Please sign in to comment.