beacon/blsync: fixed block sync test

This commit is contained in:
Zsolt Felfoldi 2024-02-01 06:51:28 +01:00 committed by Felix Lange
parent 130732e399
commit 9c39132fec
4 changed files with 36 additions and 12 deletions

View file

@ -166,7 +166,7 @@ func getExecBlock(beaconBlock *capella.BeaconBlock) (*ctypes.Block, error) {
} }
execBlock := ctypes.NewBlockWithHeader(execHeader).WithBody(txs, nil).WithWithdrawals(withdrawals) execBlock := ctypes.NewBlockWithHeader(execHeader).WithBody(txs, nil).WithWithdrawals(withdrawals)
if execBlockHash := execBlock.Hash(); execBlockHash != common.Hash(payload.BlockHash) { if execBlockHash := execBlock.Hash(); execBlockHash != common.Hash(payload.BlockHash) {
return nil, fmt.Errorf("Sanity check failed, payload hash does not match (expected %x, got %x)", common.Hash(payload.BlockHash), execBlockHash) return execBlock, fmt.Errorf("Sanity check failed, payload hash does not match (expected %x, got %x)", common.Hash(payload.BlockHash), execBlockHash)
} }
return execBlock, nil return execBlock, nil
} }

View file

@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/beacon/light/sync" "github.com/ethereum/go-ethereum/beacon/light/sync"
"github.com/ethereum/go-ethereum/beacon/types" "github.com/ethereum/go-ethereum/beacon/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
"github.com/protolambda/zrnt/eth2/beacon/capella" "github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/configs" "github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/tree" "github.com/protolambda/ztyp/tree"
@ -32,27 +33,49 @@ var (
testServer1 = "testServer1" testServer1 = "testServer1"
testServer2 = "testServer2" testServer2 = "testServer2"
testBlock1 = &capella.BeaconBlock{Slot: 123} testBlock1 = &capella.BeaconBlock{
testBlock2 = &capella.BeaconBlock{Slot: 124} Slot: 123,
Body: capella.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 456},
},
}
testBlock2 = &capella.BeaconBlock{
Slot: 124,
Body: capella.BeaconBlockBody{
ExecutionPayload: capella.ExecutionPayload{BlockNumber: 457},
},
}
) )
func init() {
eb1, _ := getExecBlock(testBlock1)
testBlock1.Body.ExecutionPayload.BlockHash = tree.Root(eb1.Hash())
eb2, _ := getExecBlock(testBlock2)
testBlock2.Body.ExecutionPayload.BlockHash = tree.Root(eb2.Hash())
}
func TestBlockSync(t *testing.T) { func TestBlockSync(t *testing.T) {
ht := &testHeadTracker{} ht := &testHeadTracker{}
blockSync := newBeaconBlockSync(ht) eventFeed := new(event.Feed)
blockSync := newBeaconBlockSync(ht, eventFeed)
headCh := make(chan types.ChainHeadEvent, 16)
eventFeed.Subscribe(headCh)
ts := sync.NewTestScheduler(t, blockSync) ts := sync.NewTestScheduler(t, blockSync)
ts.AddServer(testServer1, 1) ts.AddServer(testServer1, 1)
ts.AddServer(testServer2, 1) ts.AddServer(testServer2, 1)
expHeadBlock := func(tci int, expHead *capella.BeaconBlock) { expHeadBlock := func(tci int, expHead *capella.BeaconBlock) {
expInfo := blockHeadInfo(expHead) var expNumber, headNumber uint64
var head headData if expHead != nil {
expNumber = uint64(expHead.Body.ExecutionPayload.BlockNumber)
}
select { select {
case head = <-blockSync.headCh: case event := <-headCh:
headNumber = event.HeadBlock.Number
default: default:
} }
headInfo := blockHeadInfo(head.block) if headNumber != expNumber {
if headInfo != expInfo { t.Errorf("Wrong head block in test case #%d (expected block number %d, got %d)", tci, expNumber, headNumber)
t.Errorf("Wrong head block in test case #%d (expected {slot %d blockRoot %x}, got {slot %d blockRoot %x})", tci, expInfo.Slot, expInfo.BlockRoot, headInfo.Slot, headInfo.BlockRoot)
} }
} }
@ -130,6 +153,7 @@ func (h *testHeadTracker) ValidatedHead() (types.SignedHeader, bool) {
func (h *testHeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) { func (h *testHeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) {
return types.FinalityUpdate{ return types.FinalityUpdate{
Attested: types.HeaderWithExecProof{Header: h.validated.Header}, Attested: types.HeaderWithExecProof{Header: h.validated.Header},
Finalized: types.HeaderWithExecProof{PayloadHeader: &capella.ExecutionPayloadHeader{}},
Signature: h.validated.Signature, Signature: h.validated.Signature,
SignatureSlot: h.validated.SignatureSlot, SignatureSlot: h.validated.SignatureSlot,
}, h.validated.Header != (types.Header{}) }, h.validated.Header != (types.Header{})

View file

@ -87,7 +87,7 @@ func sync(ctx *cli.Context) error {
verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name)) verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name))
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, verbosity, usecolor))) log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, verbosity, usecolor)))
headCh := make(chan types.ChainHeadEvent, 1) headCh := make(chan types.ChainHeadEvent, 16)
client := blsync.NewClient(ctx) client := blsync.NewClient(ctx)
sub := client.SubscribeChainHeadEvent(headCh) sub := client.SubscribeChainHeadEvent(headCh)
go updateEngineApi(makeRPCClient(ctx), headCh) go updateEngineApi(makeRPCClient(ctx), headCh)

View file

@ -46,7 +46,7 @@ func NewBlsync(client Client, eth *eth.Ethereum) *Blsync {
return &Blsync{ return &Blsync{
engine: newConsensusAPIWithoutHeartbeat(eth), engine: newConsensusAPIWithoutHeartbeat(eth),
client: client, client: client,
headCh: make(chan types.ChainHeadEvent, 1), headCh: make(chan types.ChainHeadEvent, 16),
quitCh: make(chan struct{}), quitCh: make(chan struct{}),
} }
} }