2024-03-06 10:50:22 -06:00
|
|
|
// 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 blsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/light/request"
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/light/sync"
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-03-20 13:22:44 -05:00
|
|
|
zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common"
|
|
|
|
"github.com/protolambda/zrnt/eth2/beacon/deneb"
|
2024-03-06 10:50:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-04-22 06:19:42 -05:00
|
|
|
testServer1 = testServer("testServer1")
|
|
|
|
testServer2 = testServer("testServer2")
|
2024-03-06 10:50:22 -06:00
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
testBlock1 = types.NewBeaconBlock(&deneb.BeaconBlock{
|
2024-03-06 10:50:22 -06:00
|
|
|
Slot: 123,
|
2024-03-20 13:22:44 -05:00
|
|
|
Body: deneb.BeaconBlockBody{
|
|
|
|
ExecutionPayload: deneb.ExecutionPayload{
|
|
|
|
BlockNumber: 456,
|
|
|
|
BlockHash: zrntcommon.Hash32(common.HexToHash("905ac721c4058d9ed40b27b6b9c1bdd10d4333e4f3d9769100bf9dfb80e5d1f6")),
|
|
|
|
},
|
2024-03-06 10:50:22 -06:00
|
|
|
},
|
2024-03-20 13:22:44 -05:00
|
|
|
})
|
|
|
|
testBlock2 = types.NewBeaconBlock(&deneb.BeaconBlock{
|
2024-03-06 10:50:22 -06:00
|
|
|
Slot: 124,
|
2024-03-20 13:22:44 -05:00
|
|
|
Body: deneb.BeaconBlockBody{
|
|
|
|
ExecutionPayload: deneb.ExecutionPayload{
|
|
|
|
BlockNumber: 457,
|
|
|
|
BlockHash: zrntcommon.Hash32(common.HexToHash("011703f39c664efc1c6cf5f49ca09b595581eec572d4dfddd3d6179a9e63e655")),
|
|
|
|
},
|
2024-03-06 10:50:22 -06:00
|
|
|
},
|
2024-03-20 13:22:44 -05:00
|
|
|
})
|
2024-03-06 10:50:22 -06:00
|
|
|
)
|
|
|
|
|
2024-04-22 06:19:42 -05:00
|
|
|
type testServer string
|
|
|
|
|
|
|
|
func (t testServer) Name() string {
|
|
|
|
return string(t)
|
|
|
|
}
|
|
|
|
|
2024-03-06 10:50:22 -06:00
|
|
|
func TestBlockSync(t *testing.T) {
|
|
|
|
ht := &testHeadTracker{}
|
2024-03-20 13:22:44 -05:00
|
|
|
blockSync := newBeaconBlockSync(ht)
|
2024-03-06 10:50:22 -06:00
|
|
|
headCh := make(chan types.ChainHeadEvent, 16)
|
2024-03-20 13:22:44 -05:00
|
|
|
blockSync.SubscribeChainHead(headCh)
|
2024-03-06 10:50:22 -06:00
|
|
|
ts := sync.NewTestScheduler(t, blockSync)
|
|
|
|
ts.AddServer(testServer1, 1)
|
|
|
|
ts.AddServer(testServer2, 1)
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock := func(expHead *types.BeaconBlock) {
|
|
|
|
t.Helper()
|
2024-03-06 10:50:22 -06:00
|
|
|
var expNumber, headNumber uint64
|
|
|
|
if expHead != nil {
|
2024-03-20 13:22:44 -05:00
|
|
|
p, _ := expHead.ExecutionPayload()
|
|
|
|
expNumber = p.NumberU64()
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case event := <-headCh:
|
2024-03-20 13:22:44 -05:00
|
|
|
headNumber = event.Block.NumberU64()
|
2024-03-06 10:50:22 -06:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
if headNumber != expNumber {
|
2024-03-20 13:22:44 -05:00
|
|
|
t.Errorf("Wrong head block, expected block number %d, got %d)", expNumber, headNumber)
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no block requests expected until head tracker knows about a head
|
|
|
|
ts.Run(1)
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock(nil)
|
2024-03-06 10:50:22 -06:00
|
|
|
|
|
|
|
// set block 1 as prefetch head, announced by server 2
|
|
|
|
head1 := blockHeadInfo(testBlock1)
|
|
|
|
ht.prefetch = head1
|
|
|
|
ts.ServerEvent(sync.EvNewHead, testServer2, head1)
|
2024-03-20 13:22:44 -05:00
|
|
|
|
2024-03-06 10:50:22 -06:00
|
|
|
// expect request to server 2 which has announced the head
|
|
|
|
ts.Run(2, testServer2, sync.ReqBeaconBlock(head1.BlockRoot))
|
|
|
|
|
|
|
|
// valid response
|
|
|
|
ts.RequestEvent(request.EvResponse, ts.Request(2, 1), testBlock1)
|
|
|
|
ts.AddAllowance(testServer2, 1)
|
|
|
|
ts.Run(3)
|
|
|
|
// head block still not expected as the fetched block is not the validated head yet
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock(nil)
|
2024-03-06 10:50:22 -06:00
|
|
|
|
|
|
|
// set as validated head, expect no further requests but block 1 set as head block
|
2024-03-20 13:22:44 -05:00
|
|
|
ht.validated.Header = testBlock1.Header()
|
2024-03-06 10:50:22 -06:00
|
|
|
ts.Run(4)
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock(testBlock1)
|
2024-03-06 10:50:22 -06:00
|
|
|
|
|
|
|
// set block 2 as prefetch head, announced by server 1
|
|
|
|
head2 := blockHeadInfo(testBlock2)
|
|
|
|
ht.prefetch = head2
|
|
|
|
ts.ServerEvent(sync.EvNewHead, testServer1, head2)
|
|
|
|
// expect request to server 1
|
|
|
|
ts.Run(5, testServer1, sync.ReqBeaconBlock(head2.BlockRoot))
|
|
|
|
|
|
|
|
// req2 fails, no further requests expected because server 2 has not announced it
|
|
|
|
ts.RequestEvent(request.EvFail, ts.Request(5, 1), nil)
|
|
|
|
ts.Run(6)
|
|
|
|
|
|
|
|
// set as validated head before retrieving block; now it's assumed to be available from server 2 too
|
2024-03-20 13:22:44 -05:00
|
|
|
ht.validated.Header = testBlock2.Header()
|
2024-03-06 10:50:22 -06:00
|
|
|
// expect req2 retry to server 2
|
|
|
|
ts.Run(7, testServer2, sync.ReqBeaconBlock(head2.BlockRoot))
|
|
|
|
// now head block should be unavailable again
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock(nil)
|
2024-03-06 10:50:22 -06:00
|
|
|
|
|
|
|
// valid response, now head block should be block 2 immediately as it is already validated
|
|
|
|
ts.RequestEvent(request.EvResponse, ts.Request(7, 1), testBlock2)
|
|
|
|
ts.Run(8)
|
2024-03-20 13:22:44 -05:00
|
|
|
expHeadBlock(testBlock2)
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type testHeadTracker struct {
|
|
|
|
prefetch types.HeadInfo
|
|
|
|
validated types.SignedHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testHeadTracker) PrefetchHead() types.HeadInfo {
|
|
|
|
return h.prefetch
|
|
|
|
}
|
|
|
|
|
2024-04-23 06:31:32 -05:00
|
|
|
func (h *testHeadTracker) ValidatedOptimistic() (types.OptimisticUpdate, bool) {
|
|
|
|
return types.OptimisticUpdate{
|
|
|
|
Attested: types.HeaderWithExecProof{Header: h.validated.Header},
|
|
|
|
Signature: h.validated.Signature,
|
|
|
|
SignatureSlot: h.validated.SignatureSlot,
|
|
|
|
}, h.validated.Header != (types.Header{})
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO add test case for finality
|
|
|
|
func (h *testHeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) {
|
2024-03-20 13:22:44 -05:00
|
|
|
finalized := types.NewExecutionHeader(new(deneb.ExecutionPayloadHeader))
|
2024-03-06 10:50:22 -06:00
|
|
|
return types.FinalityUpdate{
|
|
|
|
Attested: types.HeaderWithExecProof{Header: h.validated.Header},
|
2024-03-20 13:22:44 -05:00
|
|
|
Finalized: types.HeaderWithExecProof{PayloadHeader: finalized},
|
2024-03-06 10:50:22 -06:00
|
|
|
Signature: h.validated.Signature,
|
|
|
|
SignatureSlot: h.validated.SignatureSlot,
|
|
|
|
}, h.validated.Header != (types.Header{})
|
|
|
|
}
|