Skip to content

Commit

Permalink
fix: reinit statestore
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Jun 10, 2024
1 parent 3851618 commit 28719eb
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,6 @@ func NewBee(
if err != nil {
return nil, err
}
b.stateStoreCloser = stateStore

// Check if the batchstore exists. If not, we can assume it's missing
// due to a migration or it's a fresh install.
batchStoreExists, err := batchStoreExists(stateStore)
if err != nil {
return nil, fmt.Errorf("batchstore: exists: %w", err)
}

addressbook := addressbook.New(stateStore)

pubKey, err := signer.PublicKey()
if err != nil {
Expand Down Expand Up @@ -294,8 +284,15 @@ func NewBee(
}

if swarm.Proximity(swarmAddress.Bytes(), neighborhood.Bytes()) < uint8(len(targetNeighborhood)) {
// mine the overlay
logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood)
newSwarmAddress, newNonce, err := nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood)
if err != nil {
return nil, fmt.Errorf("mine overlay address: %w", err)
}

if nonceExists {
logger.Info("Override nonce %d and clean state for neighborhood %s", nonce, targetNeighborhood)
logger.Info("Override nonce %d to %d and clean state for neighborhood %s", nonce, newNonce, targetNeighborhood)
logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...")
time.Sleep(10 * time.Second)

Expand All @@ -306,18 +303,19 @@ func NewBee(
)
dirsToNuke := []string{localstore, kademlia, statestore}
for _, dir := range dirsToNuke {
err = ioutil.RemoveContent(filepath.Join(o.DataDir, dir))
err := ioutil.RemoveContent(filepath.Join(o.DataDir, dir))
if err != nil {
return nil, fmt.Errorf("delete %s: %w", dir, err)
}
}
}

// mine the overlay
logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood)
swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood)
// reinit states variables
swarmAddress = newSwarmAddress
nonce = newNonce
stateStore, stateStoreMetrics, err = InitStateStore(logger, o.DataDir, o.StatestoreCacheCapacity)
if err != nil {
return nil, fmt.Errorf("mine overlay address: %w", err)
return nil, err
}

err = setOverlay(stateStore, swarmAddress, nonce)
Expand All @@ -327,6 +325,16 @@ func NewBee(
}
}

b.stateStoreCloser = stateStore
// Check if the batchstore exists. If not, we can assume it's missing
// due to a migration or it's a fresh install.
batchStoreExists, err := batchStoreExists(stateStore)
if err != nil {
return nil, fmt.Errorf("batchstore: exists: %w", err)
}

addressbook := addressbook.New(stateStore)

logger.Info("using overlay address", "address", swarmAddress)

// this will set overlay if it was not set before
Expand Down

0 comments on commit 28719eb

Please sign in to comment.