Skip to content

Commit

Permalink
Merge pull request #211 from SiaFoundation/nate/announce-fixes
Browse files Browse the repository at this point in the history
Extend Auto-Announce Test
  • Loading branch information
n8maninger authored Nov 17, 2023
2 parents afc9e12 + 184237a commit 825b353
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
11 changes: 3 additions & 8 deletions host/settings/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

const (
autoAnnounceInterval = (144 * 180) // reannounce every 180 days
announcementDebounce = 18 // blocks
announcementDebounce = 18 // blocks
)

type (
Expand Down Expand Up @@ -152,11 +151,6 @@ func (cm *ConfigManager) ProcessConsensusChange(cc modules.ConsensusChange) {
blockHeight++
}

var minAnnounceHeight uint64
if cc.BlockHeight > 144*180 {
minAnnounceHeight = uint64(cc.BlockHeight) - autoAnnounceInterval
}

// get the current net address
cm.mu.Lock()
defer cm.mu.Unlock()
Expand All @@ -169,8 +163,9 @@ func (cm *ConfigManager) ProcessConsensusChange(cc modules.ConsensusChange) {
return
}

nextAnnounceHeight := lastAnnouncement.Index.Height + autoAnnounceInterval
// if the address hasn't changed, don't reannounce
if currentNetAddress == lastAnnouncement.Address && lastAnnouncement.Index.Height > minAnnounceHeight {
if currentNetAddress == lastAnnouncement.Address && cm.scanHeight < nextAnnounceHeight {
return
}

Expand Down
35 changes: 29 additions & 6 deletions host/settings/announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAutoAnnounce(t *testing.T) {
defer node.Close()

// fund the wallet
if err := node.MineBlocks(node.Address(), 20); err != nil {
if err := node.MineBlocks(node.Address(), 99); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -53,23 +53,24 @@ func TestAutoAnnounce(t *testing.T) {
time.Sleep(time.Second)

// confirm the announcement
if err := node.MineBlocks(node.Address(), 5); err != nil {
if err := node.MineBlocks(node.Address(), 2); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)

lastAnnouncement, err := manager.LastAnnouncement()
if err != nil {
t.Fatal(err)
} else if lastAnnouncement.Index.Height == 0 {
t.Fatal("announcement not recorded")
} else if lastAnnouncement.Index.Height != 101 {
t.Fatalf("expected height 100, got %v", lastAnnouncement.Index.Height)
} else if lastAnnouncement.Address != "foo.bar:1234" {
t.Fatal("announcement not updated")
}
lastHeight := lastAnnouncement.Index.Height

// mine more blocks to ensure another announcement is not triggered
if err := node.MineBlocks(node.Address(), 10); err != nil {
// mine until right before the next announcement to ensure that the
// announcement is not triggered early
if err := node.MineBlocks(node.Address(), 99); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
Expand All @@ -80,4 +81,26 @@ func TestAutoAnnounce(t *testing.T) {
} else if lastAnnouncement.Index.Height != lastHeight {
t.Fatal("announcement triggered unexpectedly")
}

// trigger an auto-announce
if err := node.MineBlocks(node.Address(), 1); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)

// confirm the announcement
if err := node.MineBlocks(node.Address(), 2); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)

nextHeight := lastHeight + 1 + 100 // off-by-one because the announcement is mined in the next block
lastAnnouncement, err = manager.LastAnnouncement()
if err != nil {
t.Fatal(err)
} else if lastAnnouncement.Index.Height != nextHeight {
t.Fatalf("expected height %v, got %v", nextHeight, lastAnnouncement.Index.Height)
} else if lastAnnouncement.Address != "foo.bar:1234" {
t.Fatal("announcement not updated")
}
}
7 changes: 7 additions & 0 deletions host/settings/consts_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !testing

package settings

const (
autoAnnounceInterval = (144 * 180) // reannounce every 180 days
)
7 changes: 7 additions & 0 deletions host/settings/consts_testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build testing

package settings

const (
autoAnnounceInterval = 100 // reannounce every 100 blocks
)

0 comments on commit 825b353

Please sign in to comment.