Skip to content

Commit

Permalink
make migrate only blockstore / tx_index
Browse files Browse the repository at this point in the history
  • Loading branch information
trevormil committed Nov 14, 2024
1 parent aed03cf commit a236396
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
40 changes: 40 additions & 0 deletions scripts/handle-fork-711315.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

#TODO: Add your own paths here
homeDir="/home/trevormil/.bitbadgeschain"
binaryDir="/home/trevormil/go/bin"
binaryName="bitbadgeschaind"
dataPath="$homeDir/data"
backupPath="/tmp/711315-data.bak"

currDir=$(pwd)
git clone https://github.com/BitBadges/bitbadgeschain.git

# We assume you currently have 1 - 711315 saved in the data folder


# Backup the current data folder (only if not already backed up)
# Helps avoid the case where we overwrite the backup with new non-snapshot data folder
if [ ! -d "$backupPath" ]; then
mv $dataPath $backupPath
else
echo "Backup already exists, skipping to avoid backup corruption..."
fi

cd $homeDir/config
rm genesis.json
curl -o genesis.json https://raw.githubusercontent.com/BitBadges/bitbadgeschain/master/genesis-711316.json

cd $homeDir
# Reset and start chain as new from 711316+
$binaryDir/$binaryName comet unsafe-reset-all --home $homeDir
# Sync a few blocks (it will give a halt error once done but we just ignore that)
$binaryDir/$binaryName start --pruning=nothing --rpc.laddr=tcp://0.0.0.0:26657 --rpc.unsafe --halt-height=711320 --home $homeDir

cd $currDir
cd ./bitbadgeschain/scripts
go run migrate.go -source $backupPath -target $dataPath

# Cleanup
cd $currDir
rm -rf bitbadgeschain
80 changes: 80 additions & 0 deletions scripts/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"github.com/syndtr/goleveldb/leveldb"
)

//NOTE: This only handles blockstore and tx_index.db
// It does not handle application.db, state.db, or evidence.db or cs.wal
//I tried and state / evidence seemed to be enough for the node to start up, but
//ran into trouble when I tried to add application.db back in

func main() {
// Define command line flags
sourcePtr := flag.String("source", "", "Path to the source snapshot directory (required)")
Expand All @@ -32,11 +37,26 @@ func main() {
sourceDBPath := *sourcePtr
targetDBPath := *targetPtr

// applicationSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/application.db", nil)
// if err != nil {
// log.Fatal(err)
// }

txIndexSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/tx_index.db", nil)
if err != nil {
log.Fatal(err)
}

// stateSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/state.db", nil)
// if err != nil {
// log.Fatal(err)
// }

// evidenceSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/evidence.db", nil)
// if err != nil {
// log.Fatal(err)
// }

blockstoreSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/blockstore.db", nil)
if err != nil {
log.Fatal(err)
Expand All @@ -52,10 +72,70 @@ func main() {
log.Fatal(err)
}

// targetStateDB, err := leveldb.OpenFile(targetDBPath+"/state.db", nil)
// if err != nil {
// log.Fatal(err)
// }

// targetEvidenceDB, err := leveldb.OpenFile(targetDBPath+"/evidence.db", nil)
// if err != nil {
// log.Fatal(err)
// }

// targetApplicationDB, err := leveldb.OpenFile(targetDBPath+"/application.db", nil)
// if err != nil {
// log.Fatal(err)
// }

defer txIndexSnapshotDB.Close()
defer blockstoreSnapshotDB.Close()
defer targetTxIndexDB.Close()
defer targetBlockstoreDB.Close()
// defer targetStateDB.Close()
// defer targetEvidenceDB.Close()
// defer targetApplicationDB.Close()
// defer applicationSnapshotDB.Close()
// defer stateSnapshotDB.Close()
// defer evidenceSnapshotDB.Close()

// iter := applicationSnapshotDB.NewIterator(nil, nil)
// i := 0
// for iter.Next() {
// key := iter.Key()
// value := iter.Value()
// i++
// // if i%100 == 0 {
// // fmt.Printf("Key: %X\n", key)
// // }
// if _, err := targetApplicationDB.Get(key, nil); err == nil {
// continue
// }

// targetApplicationDB.Put(key, value, nil)
// }

// iter = stateSnapshotDB.NewIterator(nil, nil)
// for iter.Next() {
// key := iter.Key()
// value := iter.Value()
// if _, err := targetStateDB.Get(key, nil); err == nil {
// fmt.Printf("Skipping key: %X\n", key)
// continue
// }

// targetStateDB.Put(key, value, nil)
// }

// iter = evidenceSnapshotDB.NewIterator(nil, nil)
// for iter.Next() {
// key := iter.Key()
// value := iter.Value()
// if _, err := targetEvidenceDB.Get(key, nil); err == nil {
// continue
// }

// targetEvidenceDB.Put(key, value, nil)
// }

//Iterate over the tx_index.db and copy the keys and values to the targetDB
//If we already have the key in the targetDB, skip it and do not overwrite
Expand Down

0 comments on commit a236396

Please sign in to comment.