-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cc7bbb
commit a067e96
Showing
3 changed files
with
234 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package sync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/light/request" | ||
"github.com/ethereum/go-ethereum/beacon/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var ( | ||
testServer1 = 1 | ||
testServer2 = 2 | ||
testServer3 = 3 | ||
testServer4 = 4 | ||
|
||
testHead0 = types.HeadInfo{} | ||
testHead1 = types.HeadInfo{Slot: 123, BlockRoot: common.Hash{1}} | ||
testHead2 = types.HeadInfo{Slot: 124, BlockRoot: common.Hash{2}} | ||
testHead3 = types.HeadInfo{Slot: 124, BlockRoot: common.Hash{3}} | ||
testHead4 = types.HeadInfo{Slot: 125, BlockRoot: common.Hash{4}} | ||
|
||
testSHead1 = types.SignedHeader{Header: types.Header{Slot: 123, StateRoot: common.Hash{1}}} | ||
) | ||
|
||
func TestValidatedHead(t *testing.T) { | ||
tracker := &testTracker{} | ||
chain := &testCommitteeChain{} | ||
ht := &testHeadTracker{} | ||
headSync := NewHeadSync(ht, chain) | ||
|
||
ht.expValidated(t, 1, nil) | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer1, Type: request.EvRegistered}, | ||
{Server: testServer1, Type: EvNewSignedHead, Data: testSHead1}, | ||
}) | ||
ht.expValidated(t, 2, nil) | ||
chain.init = true | ||
headSync.Process(tracker, nil, nil) | ||
ht.expValidated(t, 3, []types.SignedHeader{testSHead1}) | ||
} | ||
|
||
func TestPrefetchHead(t *testing.T) { | ||
tracker := &testTracker{} | ||
chain := &testCommitteeChain{} | ||
ht := &testHeadTracker{} | ||
headSync := NewHeadSync(ht, chain) | ||
|
||
ht.expPrefetch(t, 1, testHead0) // no servers registered | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer1, Type: request.EvRegistered}, | ||
{Server: testServer1, Type: EvNewHead, Data: testHead1}, | ||
}) | ||
ht.expPrefetch(t, 2, testHead1) // s1: h1 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer2, Type: request.EvRegistered}, | ||
{Server: testServer2, Type: EvNewHead, Data: testHead2}, | ||
}) | ||
ht.expPrefetch(t, 3, testHead2) // s1: h1, s2: h2 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer1, Type: EvNewHead, Data: testHead2}, | ||
}) | ||
ht.expPrefetch(t, 4, testHead2) // s1: h2, s2: h2 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer3, Type: request.EvRegistered}, | ||
{Server: testServer3, Type: EvNewHead, Data: testHead3}, | ||
}) | ||
ht.expPrefetch(t, 5, testHead2) // s1: h2, s2: h2, s3: h3 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer4, Type: request.EvRegistered}, | ||
{Server: testServer4, Type: EvNewHead, Data: testHead4}, | ||
}) | ||
ht.expPrefetch(t, 6, testHead2) // s1: h2, s2: h2, s3: h3, s4: h4 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer2, Type: EvNewHead, Data: testHead3}, | ||
}) | ||
ht.expPrefetch(t, 7, testHead3) // s1: h2, s2: h3, s3: h3, s4: h4 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer3, Type: request.EvUnregistered}, | ||
}) | ||
ht.expPrefetch(t, 8, testHead4) // s1: h2, s2: h3, s4: h4 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer1, Type: request.EvUnregistered}, | ||
}) | ||
ht.expPrefetch(t, 9, testHead4) // s2: h3, s4: h4 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer4, Type: request.EvUnregistered}, | ||
}) | ||
ht.expPrefetch(t, 10, testHead3) // s2: h3 | ||
headSync.Process(tracker, nil, []request.ServerEvent{ | ||
{Server: testServer2, Type: request.EvUnregistered}, | ||
}) | ||
ht.expPrefetch(t, 11, testHead0) // no servers registered | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package sync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/light/request" | ||
"github.com/ethereum/go-ethereum/beacon/types" | ||
) | ||
|
||
type testTracker struct{} | ||
|
||
func (t *testTracker) TryRequest(requestFn func(server any) (request.Request, float32)) (request.ServerAndId, request.Request) { | ||
return request.ServerAndId{}, nil | ||
} | ||
|
||
func (t *testTracker) InvalidResponse(id request.ServerAndId, desc string) { | ||
return | ||
} | ||
|
||
type testCommitteeChain struct { | ||
nsp uint64 | ||
init bool | ||
} | ||
|
||
func (t *testCommitteeChain) CheckpointInit(bootstrap types.BootstrapData) error { | ||
return nil | ||
} | ||
|
||
func (t *testCommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommittee *types.SerializedSyncCommittee) error { | ||
return nil | ||
} | ||
|
||
func (t *testCommitteeChain) NextSyncPeriod() (uint64, bool) { | ||
return t.nsp, t.init | ||
} | ||
|
||
type testHeadTracker struct { | ||
phead types.HeadInfo | ||
validated []types.SignedHeader | ||
} | ||
|
||
func (ht *testHeadTracker) Validate(head types.SignedHeader) (bool, error) { | ||
ht.validated = append(ht.validated, head) | ||
return true, nil | ||
} | ||
|
||
func (ht *testHeadTracker) expValidated(t *testing.T, tci int, expHeads []types.SignedHeader) { | ||
for i, expHead := range expHeads { | ||
if i >= len(ht.validated) { | ||
t.Errorf("Missing validated head in test case #%d index #%d (expected {slot %d blockRoot %x}, got none)", tci, i, expHead.Header.Slot, expHead.Header.Hash()) | ||
} | ||
if ht.validated[i] != expHead { | ||
vhead := ht.validated[i].Header | ||
t.Errorf("Wrong validated head in test case #%d index #%d (expected {slot %d blockRoot %x}, got {slot %d blockRoot %x})", tci, i, expHead.Header.Slot, expHead.Header.Hash(), vhead.Slot, vhead.Hash()) | ||
} | ||
} | ||
for i := len(expHeads); i < len(ht.validated); i++ { | ||
vhead := ht.validated[i].Header | ||
t.Errorf("Unexpected validated head in test case #%d index #%d (expected none, got {slot %d blockRoot %x})", tci, i, vhead.Slot, vhead.Hash()) | ||
} | ||
ht.validated = nil | ||
} | ||
|
||
func (ht *testHeadTracker) SetPrefetchHead(head types.HeadInfo) { | ||
ht.phead = head | ||
} | ||
|
||
func (ht *testHeadTracker) expPrefetch(t *testing.T, tci int, exp types.HeadInfo) { | ||
if ht.phead != exp { | ||
t.Errorf("Wrong prefetch head in test case #%d (expected {slot %d blockRoot %x}, got {slot %d blockRoot %x})", tci, exp.Slot, exp.BlockRoot, ht.phead.Slot, ht.phead.BlockRoot) | ||
} | ||
} |