Skip to content

Commit

Permalink
main: Automatically fixup consensus.db tree
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Dec 12, 2023
1 parent 200ab7e commit da086c4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/walletd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func newNode(addr, dir string, chainNetwork string, useUPNP bool) (*node, error)
case "anagami":
network, genesisBlock = TestnetAnagami()
bootstrapPeers = anagamiBootstrap
testnetFixDBTree(dir)
default:
return nil, errors.New("invalid network: must be one of 'mainnet', 'zen', or 'anagami'")
}
Expand Down
58 changes: 58 additions & 0 deletions cmd/walletd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"log"
"math/big"
"os"
"path/filepath"
"reflect"
"time"

bolt "go.etcd.io/bbolt"
"go.sia.tech/core/chain"
"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
"go.sia.tech/walletd/api"
Expand Down Expand Up @@ -380,3 +383,58 @@ func printTestnetTxpool(c *api.Client, seed wallet.Seed) {
}
}
}

func testnetFixDBTree(dir string) {
if _, err := os.Stat(filepath.Join(dir, "consensus.db")); err != nil {
log.Fatal(err)
}
bdb, err := bolt.Open(filepath.Join(dir, "consensus.db"), 0600, nil)
if err != nil {
log.Fatal(err)
}
db := &boltDB{db: bdb}
defer db.Close()
if db.Bucket([]byte("tree-fix")) != nil {
return
}

fmt.Print("Fixing consensus.db Merkle tree...")

network, genesisBlock := TestnetAnagami()
dbstore, tipState, err := chain.NewDBStore(db, network, genesisBlock)
if err != nil {
log.Fatal(err)
}
cm := chain.NewManager(dbstore, tipState)

bdb2, err := bolt.Open(filepath.Join(dir, "consensus.db-fixed"), 0600, nil)
if err != nil {
log.Fatal(err)
}
db2 := &boltDB{db: bdb2}
defer db2.Close()
dbstore2, tipState2, err := chain.NewDBStore(db2, network, genesisBlock)
if err != nil {
log.Fatal(err)
}
cm2 := chain.NewManager(dbstore2, tipState2)

for cm2.Tip() != cm.Tip() {
index, _ := cm.BestIndex(cm2.Tip().Height + 1)
b, _ := cm.Block(index.ID)
if err := cm2.AddBlocks([]types.Block{b}); err != nil {
log.Fatal(err)
}
}

if _, err := db2.CreateBucket([]byte("tree-fix")); err != nil {
log.Fatal(err)
} else if err := db.Close(); err != nil {
log.Fatal(err)
} else if err := db2.Close(); err != nil {
log.Fatal(err)
} else if err := os.Rename(filepath.Join(dir, "consensus.db-fixed"), filepath.Join(dir, "consensus.db")); err != nil {
log.Fatal(err)
}
fmt.Println("done.")
}
3 changes: 3 additions & 0 deletions internal/walletutil/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ func (wm *JSONWalletManager) AddWallet(name string, info json.RawMessage) error
// update existing wallet
mw.info = info
return wm.save()
} else if _, err := os.Stat(filepath.Join(wm.dir, "wallets", name+".json")); err == nil {
// shouldn't happen in normal conditions
return errors.New("a wallet with that name already exists, but is absent from wallets.json")
}
store, _, err := NewJSONStore(filepath.Join(wm.dir, "wallets", name+".json"))
if err != nil {
Expand Down

0 comments on commit da086c4

Please sign in to comment.