1717
1818import java .util .Optional ;
1919import org .junit .jupiter .api .Test ;
20+ import tech .pegasys .teku .bls .BLSPublicKey ;
21+ import tech .pegasys .teku .infrastructure .ssz .SszList ;
22+ import tech .pegasys .teku .infrastructure .unsigned .UInt64 ;
2023import tech .pegasys .teku .spec .Spec ;
2124import tech .pegasys .teku .spec .TestSpecFactory ;
2225import tech .pegasys .teku .spec .datastructures .state .beaconstate .BeaconState ;
26+ import tech .pegasys .teku .spec .datastructures .state .beaconstate .BeaconStateCache ;
2327import tech .pegasys .teku .spec .datastructures .state .beaconstate .versions .gloas .BeaconStateGloas ;
2428import tech .pegasys .teku .spec .datastructures .state .versions .gloas .Builder ;
2529import tech .pegasys .teku .spec .util .DataStructureUtil ;
@@ -34,9 +38,10 @@ public class BeaconStateAccessorsGloasTest {
3438 @ Test
3539 void getBuilderIndex_shouldReturnBuilderIndex () {
3640 final BeaconStateGloas state = BeaconStateGloas .required (dataStructureUtil .randomBeaconState ());
37- assertThat (state .getBuilders ()).hasSizeGreaterThan (5 );
38- for (int i = 0 ; i < 5 ; i ++) {
39- final Builder builder = state .getBuilders ().get (i );
41+ final SszList <Builder > builders = state .getBuilders ();
42+ assertThat (builders ).isNotEmpty ();
43+ for (int i = 0 ; i < builders .size (); i ++) {
44+ final Builder builder = builders .get (i );
4045 assertThat (beaconStateAccessors .getBuilderIndex (state , builder .getPublicKey ())).contains (i );
4146 }
4247 }
@@ -48,4 +53,31 @@ public void getBuilderIndex_shouldReturnEmptyWhenBuilderNotFound() {
4853 beaconStateAccessors .getBuilderIndex (state , dataStructureUtil .randomPublicKey ());
4954 assertThat (index ).isEmpty ();
5055 }
56+
57+ @ Test
58+ public void getBuilderPubKey_shouldReturnBuilderPubKey () {
59+ final BeaconStateGloas state = BeaconStateGloas .required (dataStructureUtil .randomBeaconState ());
60+ final SszList <Builder > builders = state .getBuilders ();
61+ assertThat (builders ).isNotEmpty ();
62+ for (int i = 0 ; i < builders .size (); i ++) {
63+ final Builder builder = builders .get (i );
64+ final UInt64 builderIndex = UInt64 .valueOf (i );
65+ assertThat (beaconStateAccessors .getBuilderPubKey (state , builderIndex ))
66+ .contains (builder .getPublicKey ());
67+ // pubKey => builderIndex mapping is pre cached
68+ assertThat (
69+ BeaconStateCache .getTransitionCaches (state )
70+ .getBuildersPubKeys ()
71+ .getCached (builderIndex ))
72+ .isPresent ();
73+ }
74+ }
75+
76+ @ Test
77+ public void getBuilderPubKey_shouldReturnEmptyWhenBuilderNotExisting () {
78+ final BeaconState state = dataStructureUtil .randomBeaconState ();
79+ final Optional <BLSPublicKey > index =
80+ beaconStateAccessors .getBuilderPubKey (state , UInt64 .valueOf (999 ));
81+ assertThat (index ).isEmpty ();
82+ }
5183}
0 commit comments