diff --git a/go.mod b/go.mod index d554cad..ff9817f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module bitbadgeschain -go 1.21 +go 1.22 + +toolchain go1.23.3 replace ( // fix upstream GHSA-h395-qcrw-5vmq vulnerability. @@ -60,6 +62,7 @@ require ( github.com/rodaine/table v1.2.0 github.com/spf13/cast v1.6.0 github.com/storyicon/sigverify v1.1.0 + github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d github.com/tidwall/gjson v1.17.1 github.com/tidwall/sjson v1.2.5 github.com/unisat-wallet/libbrc20-indexer v1.1.0 @@ -116,6 +119,7 @@ require ( github.com/cockroachdb/redact v1.1.5 // indirect github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/cometbft/cometbft-db v0.11.0 // indirect + github.com/cometbft/cometbft/api v1.0.0-rc.1 github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect github.com/containerd/continuity v0.4.3 // indirect @@ -258,7 +262,6 @@ require ( github.com/stoewer/go-strcase v1.3.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/supranational/blst v0.3.12 // indirect - github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tendermint/go-amino v0.16.0 // indirect github.com/tidwall/btree v1.7.0 // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/go.sum b/go.sum index 3cd9e19..128ac41 100644 --- a/go.sum +++ b/go.sum @@ -402,6 +402,8 @@ github.com/cometbft/cometbft v0.38.12 h1:OWsLZN2KcSSFe8bet9xCn07VwhBnavPea3VyPnN github.com/cometbft/cometbft v0.38.12/go.mod h1:GPHp3/pehPqgX1930HmK1BpBLZPxB75v/dZg8Viwy+o= github.com/cometbft/cometbft-db v0.11.0 h1:M3Lscmpogx5NTbb1EGyGDaFRdsoLWrUWimFEyf7jej8= github.com/cometbft/cometbft-db v0.11.0/go.mod h1:GDPJAC/iFHNjmZZPN8V8C1yr/eyityhi2W1hz2MGKSc= +github.com/cometbft/cometbft/api v1.0.0-rc.1 h1:GtdXwDGlqwHYs16A4egjwylfYOMYyEacLBrs3Zvpt7g= +github.com/cometbft/cometbft/api v1.0.0-rc.1/go.mod h1:NDFKiBBD8HJC6QQLAoUI99YhsiRZtg2+FJWfk6A6m6o= github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M= diff --git a/scripts/migrate.go b/scripts/migrate.go new file mode 100644 index 0000000..b07b259 --- /dev/null +++ b/scripts/migrate.go @@ -0,0 +1,112 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "slices" + "strings" + + cmtstore "github.com/cometbft/cometbft/proto/tendermint/store" + proto "github.com/cosmos/gogoproto/proto" + "github.com/syndtr/goleveldb/leveldb" +) + +func main() { + // Define command line flags + sourcePtr := flag.String("source", "", "Path to the source snapshot directory (required)") + targetPtr := flag.String("target", "", "Path to the target node directory (required)") + + // Parse flags + flag.Parse() + + // Validate required flags + if *sourcePtr == "" || *targetPtr == "" { + fmt.Println("Error: Both source and target paths are required") + fmt.Println("Usage: go run migrate.go -source /path/to/source -target /path/to/target") + os.Exit(1) + } + + // Use the provided paths + sourceDBPath := *sourcePtr + targetDBPath := *targetPtr + + txIndexSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/tx_index.db", nil) + if err != nil { + log.Fatal(err) + } + + blockstoreSnapshotDB, err := leveldb.OpenFile(sourceDBPath+"/blockstore.db", nil) + if err != nil { + log.Fatal(err) + } + + targetTxIndexDB, err := leveldb.OpenFile(targetDBPath+"/tx_index.db", nil) + if err != nil { + log.Fatal(err) + } + + targetBlockstoreDB, err := leveldb.OpenFile(targetDBPath+"/blockstore.db", nil) + if err != nil { + log.Fatal(err) + } + + defer txIndexSnapshotDB.Close() + defer blockstoreSnapshotDB.Close() + defer targetTxIndexDB.Close() + defer targetBlockstoreDB.Close() + + //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 + iter := txIndexSnapshotDB.NewIterator(nil, nil) + for iter.Next() { + key := iter.Key() + value := iter.Value() + if _, err := targetTxIndexDB.Get(key, nil); err == nil { + continue + } + + targetTxIndexDB.Put(key, value, nil) + } + + iter = blockstoreSnapshotDB.NewIterator(nil, nil) + for iter.Next() { + key := iter.Key() + value := iter.Value() + if _, err := targetBlockstoreDB.Get(key, nil); err == nil { + allowedPrefixes := []string{"BH", "SC", "C", "P", "H"} + parts := strings.Split(string(key), ":") + prefix := parts[0] + if slices.Contains(allowedPrefixes, prefix) { + //These are fine if we already have them + continue + } else { + //We need to update the starting height of what we have (key == "blockStore") + bytes, err := targetBlockstoreDB.Get([]byte(key), nil) + if err != nil { + panic(err) + } + + //Set the base to 1 (since we have height 1 on now from the snapshot) + var bsj cmtstore.BlockStoreState + if err := proto.Unmarshal(bytes, &bsj); err != nil { + panic(fmt.Sprintf("Could not unmarshal bytes: %X", bytes)) + } + + bsj.Base = 1 + + bytes, err = proto.Marshal(&bsj) + if err != nil { + panic(err) + } + + targetBlockstoreDB.Put([]byte(key), bytes, nil) + + continue + } + } + + targetBlockstoreDB.Put(key, value, nil) + } +}