Skip to content

Commit

Permalink
disable time-based forks in ForkID
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Oct 18, 2024
1 parent 9e69bf8 commit b92db10
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
26 changes: 5 additions & 21 deletions core/forkid/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewID(config *params.ChainConfig, genesis *types.Block, head, time uint64)
hash := crc32.ChecksumIEEE(genesis.Hash().Bytes())

// Calculate the current fork checksum and the next fork block
forksByBlock, forksByTime := gatherForks(config, genesis.Time())
forksByBlock, _ := gatherForks(config, genesis.Time())
for _, fork := range forksByBlock {
if fork <= head {
// Fork already passed, checksum the previous hash and the fork number
Expand All @@ -86,14 +86,6 @@ func NewID(config *params.ChainConfig, genesis *types.Block, head, time uint64)
}
return ID{Hash: checksumToBytes(hash), Next: fork}
}
for _, fork := range forksByTime {
if fork <= time {
// Fork already passed, checksum the previous hash and fork timestamp
hash = checksumUpdate(hash, fork)
continue
}
return ID{Hash: checksumToBytes(hash), Next: fork}
}
return ID{Hash: checksumToBytes(hash), Next: 0}
}

Expand Down Expand Up @@ -134,9 +126,8 @@ func NewStaticFilter(config *params.ChainConfig, genesis *types.Block) Filter {
func newFilter(config *params.ChainConfig, genesis *types.Block, headfn func() (uint64, uint64)) Filter {
// Calculate the all the valid fork hash and fork next combos
var (
forksByBlock, forksByTime = gatherForks(config, genesis.Time())
forks = append(append([]uint64{}, forksByBlock...), forksByTime...)
sums = make([][4]byte, len(forks)+1) // 0th is the genesis
forks, _ = gatherForks(config, genesis.Time())
sums = make([][4]byte, len(forks)+1) // 0th is the genesis
)
hash := crc32.ChecksumIEEE(genesis.Hash().Bytes())
sums[0] = checksumToBytes(hash)
Expand All @@ -147,10 +138,6 @@ func newFilter(config *params.ChainConfig, genesis *types.Block, headfn func() (
// Add two sentries to simplify the fork checks and don't require special
// casing the last one.
forks = append(forks, math.MaxUint64) // Last fork will never be passed
if len(forksByTime) == 0 {
// In purely block based forks, avoid the sentry spilling into timestapt territory
forksByBlock = append(forksByBlock, math.MaxUint64) // Last fork will never be passed
}
// Create a validator that will filter out incompatible chains
return func(id ID) error {
// Run the fork checksum validation ruleset:
Expand All @@ -172,13 +159,10 @@ func newFilter(config *params.ChainConfig, genesis *types.Block, headfn func() (
// the remote, but at this current point in time we don't have enough
// information.
// 4. Reject in all other cases.
block, time := headfn()
block, _ := headfn()
for i, fork := range forks {
// Pick the head comparison based on fork progression
head := block
if i >= len(forksByBlock) {
head = time
}
// If our head is beyond this fork, continue to the next (we have a dummy
// fork of maxuint64 as the last item to always fail this check eventually).
if head >= fork {
Expand All @@ -189,7 +173,7 @@ func newFilter(config *params.ChainConfig, genesis *types.Block, headfn func() (
if sums[i] == id.Hash {
// Fork checksum matched, check if a remote future fork block already passed
// locally without the local node being aware of it (rule #1a).
if id.Next > 0 && (head >= id.Next || (id.Next > timestampThreshold && time >= id.Next)) {
if id.Next > 0 && head >= id.Next {
return ErrLocalIncompatibleOrStale
}
// Haven't passed locally a remote-only fork, accept the connection (rule #1b).
Expand Down
25 changes: 25 additions & 0 deletions core/forkid/forkid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,28 @@ func TestTimeBasedForkInGenesis(t *testing.T) {
}
}
}

func TestScroll(t *testing.T) {
tests := []struct {
config *params.ChainConfig
genesis *types.Block
head uint64
time uint64
want ID
}{
// Scroll test cases
{
params.ScrollMainnetChainConfig,
core.DefaultScrollMainnetGenesisBlock().ToBlock(),
10281275,
1729250728, // omit timestamp-based forks
ID{Hash: checksumToBytes(0x18d3c8d9), Next: 0}, // 0x18d3c8d9 is fetched from develop branch
},
}

for i, tt := range tests {
if have := NewID(tt.config, tt.genesis, tt.head, tt.time); have != tt.want {
t.Errorf("test %d: fork ID mismatch: have %x, want %x", i, have, tt.want)
}
}
}

0 comments on commit b92db10

Please sign in to comment.