-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into new-requests-2
- Loading branch information
Showing
30 changed files
with
1,471 additions
and
50 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
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
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
90 changes: 90 additions & 0 deletions
90
...um/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/AbstractSchemaProvider.java
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,90 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.spec.schemas.registry; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.TreeMap; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.config.SpecConfig; | ||
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SchemaId; | ||
|
||
abstract class AbstractSchemaProvider<T> implements SchemaProvider<T> { | ||
private final NavigableMap<SpecMilestone, SpecMilestone> milestoneToEffectiveMilestone = | ||
new TreeMap<>(); | ||
private final SchemaId<T> schemaId; | ||
|
||
protected AbstractSchemaProvider(final SchemaId<T> schemaId) { | ||
this.schemaId = schemaId; | ||
} | ||
|
||
protected void addMilestoneMapping( | ||
final SpecMilestone milestone, final SpecMilestone untilMilestone) { | ||
checkArgument( | ||
untilMilestone.isGreaterThan(milestone), | ||
"%s must be earlier than %s", | ||
milestone, | ||
untilMilestone); | ||
|
||
checkOverlappingVersionMappings(milestone, untilMilestone); | ||
|
||
SpecMilestone currentMilestone = untilMilestone; | ||
while (currentMilestone.isGreaterThan(milestone)) { | ||
milestoneToEffectiveMilestone.put(currentMilestone, milestone); | ||
currentMilestone = currentMilestone.getPreviousMilestone(); | ||
} | ||
} | ||
|
||
private void checkOverlappingVersionMappings( | ||
final SpecMilestone milestone, final SpecMilestone untilMilestone) { | ||
final Map.Entry<SpecMilestone, SpecMilestone> floorEntry = | ||
milestoneToEffectiveMilestone.floorEntry(untilMilestone); | ||
if (floorEntry != null && floorEntry.getValue().isGreaterThanOrEqualTo(milestone)) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Milestone %s is already mapped to %s", | ||
floorEntry.getKey(), getEffectiveMilestone(floorEntry.getValue()))); | ||
} | ||
final Map.Entry<SpecMilestone, SpecMilestone> ceilingEntry = | ||
milestoneToEffectiveMilestone.ceilingEntry(milestone); | ||
if (ceilingEntry != null && ceilingEntry.getKey().isLessThanOrEqualTo(untilMilestone)) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Milestone %s is already mapped to %s", | ||
ceilingEntry.getKey(), getEffectiveMilestone(ceilingEntry.getValue()))); | ||
} | ||
} | ||
|
||
@Override | ||
public SpecMilestone getEffectiveMilestone(final SpecMilestone milestone) { | ||
return milestoneToEffectiveMilestone.getOrDefault(milestone, milestone); | ||
} | ||
|
||
@Override | ||
public T getSchema(final SchemaRegistry registry) { | ||
final SpecMilestone milestone = registry.getMilestone(); | ||
final SpecMilestone effectiveMilestone = getEffectiveMilestone(milestone); | ||
return createSchema(registry, effectiveMilestone, registry.getSpecConfig()); | ||
} | ||
|
||
@Override | ||
public SchemaId<T> getSchemaId() { | ||
return schemaId; | ||
} | ||
|
||
protected abstract T createSchema( | ||
SchemaRegistry registry, SpecMilestone effectiveMilestone, SpecConfig specConfig); | ||
} |
49 changes: 49 additions & 0 deletions
49
ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaCache.java
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,49 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.spec.schemas.registry; | ||
|
||
import java.util.EnumMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SchemaId; | ||
|
||
interface SchemaCache { | ||
static SchemaCache createDefault() { | ||
return new SchemaCache() { | ||
private final Map<SpecMilestone, Map<SchemaId<?>, Object>> cache = | ||
new EnumMap<>(SpecMilestone.class); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> T get(final SpecMilestone milestone, final SchemaId<T> schemaId) { | ||
final Map<?, ?> milestoneSchemaIds = cache.get(milestone); | ||
if (milestoneSchemaIds == null) { | ||
return null; | ||
} | ||
return (T) milestoneSchemaIds.get(schemaId); | ||
} | ||
|
||
@Override | ||
public <T> void put( | ||
final SpecMilestone milestone, final SchemaId<T> schemaId, final T schema) { | ||
cache.computeIfAbsent(milestone, __ -> new HashMap<>()).put(schemaId, schema); | ||
} | ||
}; | ||
} | ||
|
||
<T> T get(SpecMilestone milestone, SchemaId<T> schemaId); | ||
|
||
<T> void put(SpecMilestone milestone, SchemaId<T> schemaId, T schema); | ||
} |
52 changes: 52 additions & 0 deletions
52
ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/registry/SchemaProvider.java
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,52 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.spec.schemas.registry; | ||
|
||
import static tech.pegasys.teku.spec.SpecMilestone.BELLATRIX; | ||
import static tech.pegasys.teku.spec.SpecMilestone.CAPELLA; | ||
import static tech.pegasys.teku.spec.SpecMilestone.DENEB; | ||
import static tech.pegasys.teku.spec.SpecMilestone.ELECTRA; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes.SchemaId; | ||
|
||
interface SchemaProvider<T> { | ||
Set<SpecMilestone> ALL_MILESTONES = EnumSet.allOf(SpecMilestone.class); | ||
Set<SpecMilestone> FROM_BELLATRIX = from(BELLATRIX); | ||
Set<SpecMilestone> FROM_CAPELLA = from(CAPELLA); | ||
Set<SpecMilestone> FROM_DENEB = from(DENEB); | ||
Set<SpecMilestone> FROM_ELECTRA = from(ELECTRA); | ||
|
||
static Set<SpecMilestone> from(final SpecMilestone milestone) { | ||
return EnumSet.copyOf(SpecMilestone.getAllMilestonesFrom(milestone)); | ||
} | ||
|
||
static Set<SpecMilestone> fromTo( | ||
final SpecMilestone fromMilestone, final SpecMilestone toMilestone) { | ||
return EnumSet.copyOf( | ||
SpecMilestone.getAllMilestonesFrom(fromMilestone).stream() | ||
.filter(toMilestone::isLessThanOrEqualTo) | ||
.toList()); | ||
} | ||
|
||
T getSchema(SchemaRegistry registry); | ||
|
||
Set<SpecMilestone> getSupportedMilestones(); | ||
|
||
SpecMilestone getEffectiveMilestone(SpecMilestone version); | ||
|
||
SchemaId<T> getSchemaId(); | ||
} |
Oops, something went wrong.