32
32
testServer2 = testServer ("testServer2" )
33
33
34
34
testBlock1 = types .NewBeaconBlock (& deneb.BeaconBlock {
35
- Slot : 123 ,
35
+ Slot : 127 ,
36
36
Body : deneb.BeaconBlockBody {
37
37
ExecutionPayload : deneb.ExecutionPayload {
38
38
BlockNumber : 456 ,
@@ -41,14 +41,22 @@ var (
41
41
},
42
42
})
43
43
testBlock2 = types .NewBeaconBlock (& deneb.BeaconBlock {
44
- Slot : 124 ,
44
+ Slot : 128 ,
45
45
Body : deneb.BeaconBlockBody {
46
46
ExecutionPayload : deneb.ExecutionPayload {
47
47
BlockNumber : 457 ,
48
48
BlockHash : zrntcommon .Hash32 (common .HexToHash ("011703f39c664efc1c6cf5f49ca09b595581eec572d4dfddd3d6179a9e63e655" )),
49
49
},
50
50
},
51
51
})
52
+ testFinal1 = types .NewExecutionHeader (& deneb.ExecutionPayloadHeader {
53
+ BlockNumber : 395 ,
54
+ BlockHash : zrntcommon .Hash32 (common .HexToHash ("abbe7625624bf8ddd84723709e2758956289465dd23475f02387e0854942666" )),
55
+ })
56
+ testFinal2 = types .NewExecutionHeader (& deneb.ExecutionPayloadHeader {
57
+ BlockNumber : 420 ,
58
+ BlockHash : zrntcommon .Hash32 (common .HexToHash ("9182a6ef8723654de174283750932ccc092378549836bf4873657eeec474598" )),
59
+ })
52
60
)
53
61
54
62
type testServer string
@@ -66,29 +74,37 @@ func TestBlockSync(t *testing.T) {
66
74
ts .AddServer (testServer1 , 1 )
67
75
ts .AddServer (testServer2 , 1 )
68
76
69
- expHeadBlock := func (expHead * types.BeaconBlock ) {
77
+ expHeadEvent := func (expHead * types.BeaconBlock , expFinal * types. ExecutionHeader ) {
70
78
t .Helper ()
71
79
var expNumber , headNumber uint64
80
+ var expFinalHash , finalHash common.Hash
72
81
if expHead != nil {
73
82
p , err := expHead .ExecutionPayload ()
74
83
if err != nil {
75
84
t .Fatalf ("expHead.ExecutionPayload() failed: %v" , err )
76
85
}
77
86
expNumber = p .NumberU64 ()
78
87
}
88
+ if expFinal != nil {
89
+ expFinalHash = expFinal .BlockHash ()
90
+ }
79
91
select {
80
92
case event := <- headCh :
81
93
headNumber = event .Block .NumberU64 ()
94
+ finalHash = event .Finalized
82
95
default :
83
96
}
84
97
if headNumber != expNumber {
85
98
t .Errorf ("Wrong head block, expected block number %d, got %d)" , expNumber , headNumber )
86
99
}
100
+ if finalHash != expFinalHash {
101
+ t .Errorf ("Wrong finalized block, expected block hash %064x, got %064x)" , expFinalHash [:], finalHash [:])
102
+ }
87
103
}
88
104
89
105
// no block requests expected until head tracker knows about a head
90
106
ts .Run (1 )
91
- expHeadBlock ( nil )
107
+ expHeadEvent ( nil , nil )
92
108
93
109
// set block 1 as prefetch head, announced by server 2
94
110
head1 := blockHeadInfo (testBlock1 )
@@ -103,12 +119,13 @@ func TestBlockSync(t *testing.T) {
103
119
ts .AddAllowance (testServer2 , 1 )
104
120
ts .Run (3 )
105
121
// head block still not expected as the fetched block is not the validated head yet
106
- expHeadBlock ( nil )
122
+ expHeadEvent ( nil , nil )
107
123
108
124
// set as validated head, expect no further requests but block 1 set as head block
109
125
ht .validated .Header = testBlock1 .Header ()
126
+ ht .finalized , ht .finalizedPayload = testBlock1 .Header (), testFinal1
110
127
ts .Run (4 )
111
- expHeadBlock (testBlock1 )
128
+ expHeadEvent (testBlock1 , testFinal1 )
112
129
113
130
// set block 2 as prefetch head, announced by server 1
114
131
head2 := blockHeadInfo (testBlock2 )
@@ -126,17 +143,26 @@ func TestBlockSync(t *testing.T) {
126
143
// expect req2 retry to server 2
127
144
ts .Run (7 , testServer2 , sync .ReqBeaconBlock (head2 .BlockRoot ))
128
145
// now head block should be unavailable again
129
- expHeadBlock ( nil )
146
+ expHeadEvent ( nil , nil )
130
147
131
148
// valid response, now head block should be block 2 immediately as it is already validated
149
+ // but head event is still not expected because an epoch boundary was crossed and the
150
+ // expected finality update has not arrived yet
132
151
ts .RequestEvent (request .EvResponse , ts .Request (7 , 1 ), testBlock2 )
133
152
ts .Run (8 )
134
- expHeadBlock (testBlock2 )
153
+ expHeadEvent (nil , nil )
154
+
155
+ // expected finality update arrived, now a head event is expected
156
+ ht .finalized , ht .finalizedPayload = testBlock2 .Header (), testFinal2
157
+ ts .Run (9 )
158
+ expHeadEvent (testBlock2 , testFinal2 )
135
159
}
136
160
137
161
type testHeadTracker struct {
138
- prefetch types.HeadInfo
139
- validated types.SignedHeader
162
+ prefetch types.HeadInfo
163
+ validated types.SignedHeader
164
+ finalized types.Header
165
+ finalizedPayload * types.ExecutionHeader
140
166
}
141
167
142
168
func (h * testHeadTracker ) PrefetchHead () types.HeadInfo {
@@ -151,13 +177,14 @@ func (h *testHeadTracker) ValidatedOptimistic() (types.OptimisticUpdate, bool) {
151
177
}, h .validated .Header != (types.Header {})
152
178
}
153
179
154
- // TODO add test case for finality
155
180
func (h * testHeadTracker ) ValidatedFinality () (types.FinalityUpdate , bool ) {
156
- finalized := types .NewExecutionHeader (new (deneb.ExecutionPayloadHeader ))
181
+ if h .validated .Header == (types.Header {}) || h .finalizedPayload == nil {
182
+ return types.FinalityUpdate {}, false
183
+ }
157
184
return types.FinalityUpdate {
158
- Attested : types.HeaderWithExecProof {Header : h .validated . Header },
159
- Finalized : types.HeaderWithExecProof {PayloadHeader : finalized },
185
+ Attested : types.HeaderWithExecProof {Header : h .finalized },
186
+ Finalized : types.HeaderWithExecProof {Header : h . finalized , PayloadHeader : h . finalizedPayload },
160
187
Signature : h .validated .Signature ,
161
188
SignatureSlot : h .validated .SignatureSlot ,
162
- }, h . validated . Header != (types. Header {})
189
+ }, true
163
190
}
0 commit comments