Skip to content

Commit

Permalink
hostd,renterd: wait for sync before mining
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 26, 2024
1 parent 9a3ee1c commit bcc27ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
27 changes: 20 additions & 7 deletions nodes/hostd.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,32 @@ func (m *Manager) StartHostd(ctx context.Context, sk types.PrivateKey, ready cha
node.APIAddress = "http://" + httpListener.Addr().String()
node.Password = "sia is cool"

waitForSync := func() error {
// wait for sync
for m.chain.Tip() != index.Tip() {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
}
}
return nil
}

if err := waitForSync(); err != nil {
return fmt.Errorf("failed to wait for sync: %w", err)
}

log.Debug("node setup complete")

// mine blocks to fund the wallet
walletAddress := types.StandardUnlockHash(sk.PublicKey())
if err := m.MineBlocks(ctx, int(network.MaturityDelay)+20, walletAddress); err != nil {
return fmt.Errorf("failed to mine blocks: %w", err)
}

// wait for sync
for m.chain.Tip() != index.Tip() {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
}
if err := waitForSync(); err != nil {
return fmt.Errorf("failed to wait for sync: %w", err)
}

client := api.NewClient(node.APIAddress+"/api", node.Password)
Expand Down
37 changes: 21 additions & 16 deletions nodes/renterd.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,29 +383,34 @@ func (m *Manager) StartRenterd(ctx context.Context, sk types.PrivateKey, ready c
return fmt.Errorf("failed to update setting: %w", err)
}

if _, err := autopilotClient.Trigger(true); err != nil {
return fmt.Errorf("failed to trigger autopilot: %w", err)
waitForSync := func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
state, err := busClient.ConsensusState(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus state: %w", err)
} else if state.BlockHeight == m.chain.Tip().Height {
return nil
}
}
}
}

if err := waitForSync(); err != nil {
return fmt.Errorf("failed to wait for sync: %w", err)
}

// mine blocks to fund the wallet
walletAddress := types.StandardUnlockHash(pk)
walletAddress := types.StandardUnlockHash(sk.PublicKey())
if err := m.MineBlocks(ctx, int(network.MaturityDelay)+20, walletAddress); err != nil {
return fmt.Errorf("failed to mine blocks: %w", err)
}

waitForSync:
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
state, err := busClient.ConsensusState(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus state: %w", err)
} else if state.BlockHeight == m.chain.Tip().Height {
break waitForSync
}
}
if err := waitForSync(); err != nil {
return fmt.Errorf("failed to wait for sync: %w", err)
}

if _, err := autopilotClient.Trigger(true); err != nil {
Expand Down

0 comments on commit bcc27ab

Please sign in to comment.