forked from ethereum/py-evm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New ImmutableSerializable with a memoizable .hash property
- Loading branch information
Showing
6 changed files
with
50 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import functools | ||
|
||
import rlp | ||
|
||
from eth_utils import keccak | ||
|
||
|
||
# TODO: Document, and explain that it must only be used on methods with no arguments as well as | ||
# subclasses of ImmutableSerializable. Maybe instead of a decorator it should actually be a method | ||
# on ImmutableSerializable? | ||
def memoize(method): | ||
attr_name = '_memoized_' + method.__name__ | ||
|
||
@functools.wraps(method) | ||
def memoized_method(obj): | ||
if not hasattr(obj, attr_name): | ||
setattr(obj, attr_name, method(obj)) | ||
return getattr(obj, attr_name) | ||
|
||
return memoized_method | ||
|
||
|
||
class ImmutableSerializable(rlp.Serializable): | ||
|
||
_hash = None | ||
|
||
@classmethod | ||
def deserialize(cls, serial, exclude=None, mutable=False, **kwargs): | ||
obj = super().deserialize(serial, exclude=exclude, mutable=True, **kwargs) | ||
obj._cached_rlp = rlp.codec.encode_raw(serial) | ||
return rlp.sedes.make_immutable(obj) | ||
|
||
@property | ||
@memoize | ||
def hash(self) -> bytes: | ||
if self._cached_rlp: | ||
return keccak(self._cached_rlp) | ||
else: | ||
return keccak(rlp.encode(self)) |
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