-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stable Containers - EIP-7495 #8444
Open
tbenr
wants to merge
12
commits into
Consensys:master
Choose a base branch
from
tbenr:stable-containers-eip7495
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
78c4f5d
eip7495
tbenr ae30b64
implement some todos
tbenr bf55118
Merge branch 'master' into stable-containers-eip7495
tbenr 4e31e71
some finals
tbenr ff3b40d
improve mutability with `calcNewSize`
tbenr 5b13a1e
code consolidation in util
tbenr dc15b31
some finals
tbenr d5d1b22
Merge branch 'master' into stable-containers-eip7495
tbenr bd1fe84
Merge branch 'master' into stable-containers-eip7495
tbenr 90b0c1d
add deserialization validation
tbenr fa042de
Merge branch 'master' into stable-containers-eip7495
tbenr c09a74c
Merge branch 'master' into stable-containers-eip7495
tbenr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions
16
infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/SszProfile.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,16 @@ | ||
/* | ||
* 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.infrastructure.ssz; | ||
|
||
public interface SszProfile extends SszStableContainerBase {} |
16 changes: 16 additions & 0 deletions
16
...astructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/SszStableContainer.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,16 @@ | ||
/* | ||
* 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.infrastructure.ssz; | ||
|
||
public interface SszStableContainer extends SszStableContainerBase {} |
39 changes: 39 additions & 0 deletions
39
...ucture/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/SszStableContainerBase.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,39 @@ | ||
/* | ||
* 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.infrastructure.ssz; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector; | ||
|
||
public interface SszStableContainerBase extends SszContainer { | ||
boolean isFieldActive(int index); | ||
|
||
SszBitvector getActiveFields(); | ||
|
||
default Optional<SszData> getOptional(final int index) { | ||
try { | ||
return Optional.of(get(index)); | ||
} catch (final NoSuchElementException __) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) | ||
// container is heterogeneous by its nature so making unsafe cast here | ||
// is more convenient and is not less safe | ||
default <C extends SszData> Optional<C> getAnyOptional(final int index) { | ||
return (Optional<C>) getOptional(index); | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
...ain/java/tech/pegasys/teku/infrastructure/ssz/impl/SszMutableStableContainerBaseImpl.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,69 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2022 | ||
* | ||
* 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.infrastructure.ssz.impl; | ||
|
||
import java.util.NoSuchElementException; | ||
import tech.pegasys.teku.infrastructure.ssz.SszData; | ||
import tech.pegasys.teku.infrastructure.ssz.cache.IntCache; | ||
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; | ||
|
||
public class SszMutableStableContainerBaseImpl extends SszMutableContainerImpl { | ||
protected SszStableContainerBaseImpl backingStableContainerBaseView; | ||
|
||
public SszMutableStableContainerBaseImpl(final SszStableContainerBaseImpl backingImmutableView) { | ||
super(backingImmutableView); | ||
this.backingStableContainerBaseView = backingImmutableView; | ||
} | ||
|
||
@Override | ||
protected SszContainerImpl createImmutableSszComposite( | ||
final TreeNode backingNode, final IntCache<SszData> viewCache) { | ||
return new SszStableContainerBaseImpl( | ||
getSchema().toStableContainerSchemaBaseRequired(), backingNode, viewCache); | ||
} | ||
|
||
@Override | ||
protected void checkIndex(final int index, final boolean set) { | ||
// we currently not support StableContainers with optional fields, so we expect get\set over an | ||
// already active field | ||
if (backingStableContainerBaseView.isFieldActive(index)) { | ||
return; | ||
} | ||
|
||
if (index > backingStableContainerBaseView.getActiveFields().getLastSetBitIndex()) { | ||
throw new IndexOutOfBoundsException( | ||
"Invalid index " | ||
+ index | ||
+ " for container with last active index " | ||
+ backingStableContainerBaseView.getActiveFields().getLastSetBitIndex()); | ||
} | ||
|
||
throw new NoSuchElementException("Index " + index + " is not active in the stable container"); | ||
} | ||
|
||
@Override | ||
protected int calcNewSize(final int index) { | ||
// StableContainer have sparse index so size cannot be compared with index. | ||
// Currently, the only mutable StableContainer we support is a Profile with no optional | ||
// fields, so we can assume the size never changes. | ||
// See: | ||
// tech.pegasys.teku.infrastructure.ssz.impl.SszStableContainerBaseImpl.createWritableCopy | ||
return size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Mutable " + backingStableContainerBaseView.toString(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...structure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/impl/SszProfileImpl.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,63 @@ | ||
/* | ||
* 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.infrastructure.ssz.impl; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.util.Arrays; | ||
import tech.pegasys.teku.infrastructure.ssz.SszData; | ||
import tech.pegasys.teku.infrastructure.ssz.SszProfile; | ||
import tech.pegasys.teku.infrastructure.ssz.cache.ArrayIntCache; | ||
import tech.pegasys.teku.infrastructure.ssz.cache.IntCache; | ||
import tech.pegasys.teku.infrastructure.ssz.schema.SszProfileSchema; | ||
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; | ||
|
||
public class SszProfileImpl extends SszStableContainerBaseImpl implements SszProfile { | ||
|
||
public SszProfileImpl(final SszProfileSchema<?> type) { | ||
super(type); | ||
} | ||
|
||
public SszProfileImpl(final SszProfileSchema<?> type, final TreeNode backingNode) { | ||
super(type, backingNode); | ||
} | ||
|
||
public SszProfileImpl( | ||
final SszProfileSchema<?> type, final TreeNode backingNode, final IntCache<SszData> cache) { | ||
super(type, backingNode, cache); | ||
} | ||
|
||
public SszProfileImpl(final SszProfileSchema<?> type, final SszData... memberValues) { | ||
super( | ||
type, | ||
type.createTreeFromFieldValues(Arrays.asList(memberValues)), | ||
createCache(memberValues)); | ||
|
||
for (int i = 0; i < memberValues.length; i++) { | ||
Preconditions.checkArgument( | ||
memberValues[i].getSchema().equals(type.getChildSchema(i)), | ||
"Wrong child schema at index %s. Expected: %s, was %s", | ||
i, | ||
type.getChildSchema(i), | ||
memberValues[i].getSchema()); | ||
} | ||
} | ||
|
||
private static IntCache<SszData> createCache(final SszData... memberValues) { | ||
final ArrayIntCache<SszData> cache = new ArrayIntCache<>(memberValues.length); | ||
for (int i = 0; i < memberValues.length; i++) { | ||
cache.invalidateWithNewValue(i, memberValues[i]); | ||
} | ||
return cache; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably just call this
calculateNewSize
rather than abbreviate...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
problem is that in the ssz module seems like "calc" is the normal:
I wanted to maintain the style here. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok i guess but i do hate abbreviations...