-
Notifications
You must be signed in to change notification settings - Fork 22
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
Root and SignedRoot #58
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
Container, | ||
List, | ||
Serializable, | ||
SignedSerializable, | ||
UInt, | ||
Vector, | ||
boolean, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
|
||
|
||
class Boolean(BasicSedes[bool, bool]): | ||
|
||
def __init__(self) -> None: | ||
super().__init__(size=1) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
|
||
class Byte(BasicSedes[bytes, bytes]): | ||
|
||
def __init__(self) -> None: | ||
super().__init__(1) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import ( | ||
NamedTuple, | ||
Optional, | ||
Tuple, | ||
) | ||
|
||
import ssz | ||
from ssz.constants import ( | ||
SIGNATURE_FIELD_NAME, | ||
) | ||
from ssz.sedes.base import ( | ||
BaseSedes, | ||
) | ||
from ssz.sedes.container import ( | ||
Container, | ||
) | ||
from ssz.sedes.serializable import ( | ||
BaseSerializable, | ||
MetaSerializable, | ||
) | ||
|
||
|
||
class SignedMeta(NamedTuple): | ||
has_fields: bool | ||
fields: Optional[Tuple[Tuple[str, BaseSedes]]] | ||
container_sedes: Optional[Container] | ||
signed_container_sedes: Optional[Container] | ||
field_names: Optional[Tuple[str, ...]] | ||
field_attrs: Optional[Tuple[str, ...]] | ||
|
||
|
||
class MetaSignedSerializable(MetaSerializable): | ||
def __new__(mcls, name, bases, namespace): | ||
cls = super().__new__(mcls, name, bases, namespace) | ||
|
||
if cls._meta.has_fields: | ||
if len(cls._meta.fields) < 2: | ||
raise TypeError(f"Signed serializables need to have at least two fields") | ||
if cls._meta.field_names[-1] != SIGNATURE_FIELD_NAME: | ||
raise TypeError( | ||
f"Last field of signed serializable must be {SIGNATURE_FIELD_NAME}, but is " | ||
f"{cls._meta.field_names[-1]}" | ||
) | ||
|
||
signed_container_sedes = Container(cls._meta.fields[:-1]) | ||
else: | ||
signed_container_sedes = None | ||
|
||
meta = SignedMeta( | ||
has_fields=cls._meta.has_fields, | ||
fields=cls._meta.fields, | ||
container_sedes=cls._meta.container_sedes, | ||
signed_container_sedes=signed_container_sedes, | ||
field_names=cls._meta.field_names, | ||
field_attrs=cls._meta.field_attrs, | ||
) | ||
cls._meta = meta | ||
|
||
return cls | ||
|
||
|
||
BaseSedes.register(MetaSignedSerializable) | ||
|
||
|
||
class SignedSerializable(BaseSerializable, metaclass=MetaSignedSerializable): | ||
@property | ||
def signing_root(self): | ||
return ssz.hash_tree_root(self, self._meta.signed_container_sedes) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
|
||
import ssz | ||
from ssz.sedes import ( | ||
byte_list, | ||
uint8, | ||
) | ||
|
||
|
||
def test_field_number_check(): | ||
with pytest.raises(TypeError): | ||
class TestA(ssz.SignedSerializable): | ||
fields = () | ||
|
||
with pytest.raises(TypeError): | ||
class TestB(ssz.SignedSerializable): | ||
fields = ( | ||
("signature", byte_list), | ||
) | ||
|
||
class TestC(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("signature", byte_list), | ||
) | ||
|
||
|
||
def test_field_name_check(): | ||
with pytest.raises(TypeError): | ||
class TestA(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
) | ||
|
||
with pytest.raises(TypeError): | ||
class TestB(ssz.SignedSerializable): | ||
fields = ( | ||
("signature", uint8), | ||
("field1", byte_list), | ||
) | ||
|
||
|
||
def test_signing_root(): | ||
class Signed(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
("signature", byte_list), | ||
) | ||
|
||
class Unsigned(ssz.Serializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
) | ||
|
||
signed = Signed(123, b"\xaa", b"\x00") | ||
unsigned = Unsigned(123, b"\xaa") | ||
assert signed.signing_root == unsigned.root |
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.
example about formatting here