Skip to content

Commit

Permalink
eth: refactoring mess-disabling head staleness check
Browse files Browse the repository at this point in the history
This just makes better sense and it cleaner logic.

Signed-off-by: meows <[email protected]>
  • Loading branch information
meowsbits committed Sep 25, 2020
1 parent fbe85f3 commit 443e272
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
18 changes: 5 additions & 13 deletions eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,16 @@ func (pm *ProtocolManager) artificialFinalitySafetyLoop() {
t := time.NewTicker(artificialFinalitySafetyInterval)
defer t.Stop()

var lastHead uint64

for {
select {
case <-t.C:
if pm.blockchain.IsArtificialFinalityEnabled() {
// Get the latest header we have.
n := pm.blockchain.CurrentHeader().Number.Uint64()
// If it has changed, we haven't gone stale or dark.
if lastHead != n {
lastHead = n
continue
// Check if your chain has grown stale.
// If it has, disable artificial finality, we could be on an attacker's
// chain getting starved.
if time.Since(time.Unix(int64(pm.blockchain.CurrentHeader().Time), 0)) > artificialFinalitySafetyInterval {
pm.blockchain.EnableArtificialFinality(false, "reason", "stale safety interval", "interval", artificialFinalitySafetyInterval)
}
// Else, it hasn't changed, which means we've been at the same
// header for the whole timer interval time.
pm.blockchain.EnableArtificialFinality(false, "reason", "stale safety interval", "interval", artificialFinalitySafetyInterval)
} else {
lastHead = 0 // reset
}
case <-pm.quitSync:
return
Expand Down
7 changes: 7 additions & 0 deletions eth/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
)

func TestFastSyncDisabling63(t *testing.T) { testFastSyncDisabling(t, 63) }
Expand Down Expand Up @@ -123,3 +124,9 @@ func TestArtificialFinalityFeatureEnablingDisabling(t *testing.T) {
t.Error("AF not disabled")
}
}

func TestArtificialFinalitySafetyLoopTimeComparison(t *testing.T) {
if !(time.Since(time.Unix(int64(params.DefaultMessNetGenesisBlock().Timestamp), 0)) > artificialFinalitySafetyInterval) {
t.Fatal("bad unit logic!")
}
}

0 comments on commit 443e272

Please sign in to comment.