From 899415f580ccf0653aab572b24f9d60e7cfa04ef Mon Sep 17 00:00:00 2001 From: timotheyca Date: Thu, 19 May 2022 11:03:16 +0300 Subject: [PATCH] origin protocol --- main.py | 61 ++++--- rainbowadn/chain/block.py | 26 +-- rainbowadn/chain/blockchain.py | 16 +- rainbowadn/chain/blockchainprotocol.py | 3 - rainbowadn/chain/blockcollectioninterface.py | 10 +- rainbowadn/chain/reduction/reduction.py | 16 +- .../chain/reduction/reductionprotocol.py | 5 - .../chain/reduction/reductionstageprotocol.py | 8 +- .../stages/derivation/activestageprotocol.py | 7 +- .../derivation/activestagestateprotocol.py | 1 - rainbowadn/chain/stages/stage.py | 51 +++--- rainbowadn/chain/stages/stageprotocol.py | 5 - rainbowadn/chain/stages/stagestateprotocol.py | 2 +- rainbowadn/chain/states/stateprotocol.py | 5 - rainbowadn/data/atomic/atomic.py | 13 +- rainbowadn/data/atomic/integer.py | 2 +- rainbowadn/data/atomic/plain.py | 2 +- rainbowadn/data/collection/array/array.py | 12 +- rainbowadn/data/collection/keymetadata.py | 18 +-- .../collection/keymetadataquerycollection.py | 6 +- rainbowadn/data/collection/keyvalue.py | 10 +- .../data/collection/mapping/querymapping.py | 8 +- rainbowadn/data/collection/stack/stack.py | 15 +- .../trees/binary/activebinarytree.py | 10 +- .../data/collection/trees/binary/avl.py | 5 +- .../binary/balancedtreecreationprotocol.py | 3 - .../collection/trees/binary/binarytree.py | 25 +-- .../binary/binarytreebalancingprotocol.py | 3 - .../trees/binary/querybinarytree.py | 7 +- .../collection/trees/comparison/comparator.py | 5 - .../trees/comparison/keyedcomparator.py | 6 +- .../trees/comparison/plaincomparator.py | 4 +- .../trees/comparison/protocolcomparator.py | 5 +- rainbowadn/encryption/__init__.py | 0 rainbowadn/hashing/hash_point_format.py | 8 +- rainbowadn/hashing/hashpoint.py | 24 +-- rainbowadn/hashing/hashresolver.py | 21 +-- rainbowadn/hashing/localmetaorigin.py | 22 +++ rainbowadn/hashing/localorigin.py | 17 ++ rainbowadn/hashing/metaorigin.py | 20 +++ .../hashing/nullability/nullablereference.py | 14 +- rainbowadn/hashing/origin.py | 10 ++ rainbowadn/hashing/rainbow_factory.py | 4 +- rainbowadn/hashing/recursivementionable.py | 4 +- .../hashing/reduce_nullable_reference.py | 4 +- rainbowadn/hashing/resolvermetaorigin.py | 28 ++++ rainbowadn/hashing/resolverorigin.py | 37 +++++ rainbowadn/hashing/static.py | 10 +- rainbowadn/testing/failresolver.py | 2 +- rainbowadn/v13/bankchain.py | 18 +-- rainbowadn/v13/bankprotocol.py | 12 +- rainbowadn/v13/bankstate.py | 47 +++--- rainbowadn/v13/signature.py | 2 +- rainbowadn/v13/subject.py | 2 +- rainbowadn/v13/transaction.py | 150 ++++++++---------- .../wrisbt/weakreferenceindexsetbtree.py | 86 +++++----- rainbowadn/wrisbt/wrisbtchainprotocol.py | 5 +- rainbowadn/wrisbt/wrisbtindex.py | 16 +- rainbowadn/wrisbt/wrisbtprotocol.py | 13 +- rainbowadn/wrisbt/wrisbtroot.py | 51 +++--- 60 files changed, 537 insertions(+), 465 deletions(-) create mode 100644 rainbowadn/encryption/__init__.py create mode 100644 rainbowadn/hashing/localmetaorigin.py create mode 100644 rainbowadn/hashing/localorigin.py create mode 100644 rainbowadn/hashing/metaorigin.py create mode 100644 rainbowadn/hashing/origin.py create mode 100644 rainbowadn/hashing/resolvermetaorigin.py create mode 100644 rainbowadn/hashing/resolverorigin.py diff --git a/main.py b/main.py index f970217..37f793b 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,6 @@ from rainbowadn.chain.chaincollectioninterface import ChainCollectionInterface from rainbowadn.chain.reduction.reductionchainmetafactory import ReductionChainMetaFactory from rainbowadn.data.atomic.integer import Integer from rainbowadn.data.atomic.plain import Plain -from rainbowadn.data.collection.array.array import Array from rainbowadn.data.collection.trees.binary.activebinarytree import ActiveBinaryTree from rainbowadn.data.collection.trees.binary.avl import AVLBTBP from rainbowadn.data.collection.trees.comparison.comparator import Replace @@ -20,6 +19,7 @@ from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolvermetaorigin import ResolverMetaOrigin from rainbowadn.v13.algo import MINT_CONST from rainbowadn.v13.bankchain import BankChain from rainbowadn.v13.subject import Subject @@ -33,7 +33,7 @@ class DumbResolver(HashResolver): def __init__(self): self.table: MutableMapping[bytes, bytes] = OrderedDict() - def _resolve(self, point: bytes) -> bytes: + def resolve_bytes(self, point: bytes) -> bytes: assert isinstance(point, bytes) return self.table[point] @@ -41,26 +41,25 @@ class DumbResolver(HashResolver): assert isinstance(hash_point, HashPoint) if hash_point.point in self.table: pass - elif isinstance(hash_point.value, NotNull): - value: HashMentionable = hash_point.value.value + else: + value: HashMentionable = hash_point.resolve() + assert isinstance(value, HashMentionable) self.table[hash_point.point] = HashPoint.bytes_of_mentioned(value) if isinstance(value, RecursiveMentionable): for hash_point in value.points(): self.save(hash_point) - else: - raise TypeError def main0(): dr = DumbResolver() - bank = BankChain.empty(ReductionChainMetaFactory(), dr) + bank = BankChain.empty(ReductionChainMetaFactory()) key_0 = nacl.signing.SigningKey.generate() transaction_0 = Transaction.make( [], [CoinData.of(Subject(key_0.verify_key), 1_000_000)], [] ) - coin_0, coin_1 = transaction_0.coins(dr, MINT_CONST, NotNull(HashPoint.of(Subject(key_0.verify_key)))) + coin_0, coin_1 = transaction_0.coins(MINT_CONST, NotNull(HashPoint.of(Subject(key_0.verify_key)))) bank = bank.adds( [ transaction_0, @@ -77,7 +76,9 @@ def main0(): print(bank) print(bank.verify()) dr.save(HashPoint.of(bank.reference)) - bank = BankChain.from_reference(ReductionChainMetaFactory(), dr.resolve(HashPoint.of(bank.reference).loose()), dr) + bank = BankChain.from_reference( + ReductionChainMetaFactory(), ResolverMetaOrigin(dr).migrate(HashPoint.of(bank.reference)).resolve() + ) print(bank) print(bank.verify()) # for key, value in dr.table.items(): @@ -87,50 +88,58 @@ def main0(): def main1(): stoptime = time.process_time() - def measure(message: str): + def measure(message: str) -> float: nonlocal stoptime now = time.process_time() - print(message, now - stoptime) + delta = now - stoptime + print(message, delta) stoptime = now + return delta dr = DumbResolver() btree = WrisbtRoot.empty(WrisbtParametres(1, 5)) + n = 10000 measure('init') - for _ in range(10000): + for _ in range(n): key = os.urandom(5) - assert not btree.contains(dr, key) - btree = btree.add(dr, key) - assert btree.contains(dr, key) + assert not btree.contains(key) + btree = btree.add(key) + assert btree.contains(key) measure('add') dr.save(HashPoint.of(btree)) measure('save') - btree = dr.resolve(HashPoint.of(btree).loose()) + btree = ResolverMetaOrigin(dr).migrate(HashPoint.of(btree)).resolve() + assert len(btree.keys()) == n print(btree.height) - measure('resolve') + measure('resolve and iterate') + for _ in range(n): + key = os.urandom(5) + assert not btree.contains(key) + btree = btree.add(key) + assert btree.contains(key) + measure('resolve and add') def main2(): - dr = DumbResolver() - chain: ChainCollectionInterface[Any, Plain, Array[WrisbtRoot]] = BlockChainFactory( - WrisbtChainProtocol(dr, Plain.factory(), 2).loose() - ).loose().empty().loose() + chain: ChainCollectionInterface[Any, Plain, WrisbtRoot] = BlockChainFactory( + WrisbtChainProtocol(Plain.factory(), 2).loose() + ).empty().loose() for _ in range(1000): chain = chain.add(HashPoint.of(Plain(os.urandom(16)))) assert chain # noinspection PyUnresolvedReferences - print(dr.resolve(chain.actual_state().reference.value).height) + print(chain.actual_state().reference.value.resolve().height) def main(): - dr = DumbResolver() tree: ActiveBinaryTree[Plain, Integer] = ActiveBinaryTree.empty( - AVLBTBP(PlainComparator(dr, Replace())), Plain.factory() + AVLBTBP(PlainComparator(Replace())), Plain.factory() ) for i in range(26): tree = tree.add(HashPoint.of(Plain(bytes([ord('A') + i])))) - print(tree.reference.str(dr, 0)) + print(tree.reference.str(0)) if __name__ == '__main__': - main1() + main0() diff --git a/rainbowadn/chain/block.py b/rainbowadn/chain/block.py index 1108448..33c2bae 100644 --- a/rainbowadn/chain/block.py +++ b/rainbowadn/chain/block.py @@ -1,12 +1,12 @@ from typing import Generic, Iterable, TypeVar -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('Block', 'BlockFactory',) @@ -40,16 +40,15 @@ class Block(RecursiveMentionable, Generic[HeaderType, StateType]): self.state.factory, ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{self.previous.str(resolver, tab)}' \ + return f'{self.previous.str(tab)}' \ f'{tabulate(tab)}(' \ f'{tabulate(tab + 1)}block' \ f'{tabulate(tab + 1)}(header)' \ - f'{tabulate(tab + 1)}{hash_point_format(self.header, resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{hash_point_format(self.header, tab + 1)}' \ f'{tabulate(tab + 1)}(state)' \ - f'{tabulate(tab + 1)}{hash_point_format(self.state, resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{hash_point_format(self.state, tab + 1)}' \ f'{tabulate(tab)})' @@ -64,10 +63,15 @@ class BlockFactory(RainbowFactory[Block[HeaderType, StateType]], Generic[HeaderT self.header_factory = header_factory self.state_factory = state_factory - def from_bytes(self, source: bytes) -> Block[HeaderType, StateType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> Block[HeaderType, StateType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return Block( - NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH]), - HashPoint(self.header_factory, source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH], Null()), - HashPoint(self.state_factory, source[2 * HashPoint.HASH_LENGTH:3 * HashPoint.HASH_LENGTH], Null()), + NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH], resolver), + ResolverOrigin( + self.header_factory, source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH], resolver + ).hash_point(), + ResolverOrigin( + self.state_factory, source[2 * HashPoint.HASH_LENGTH:3 * HashPoint.HASH_LENGTH], resolver + ).hash_point(), ) diff --git a/rainbowadn/chain/blockchain.py b/rainbowadn/chain/blockchain.py index 2932412..9e550d9 100644 --- a/rainbowadn/chain/blockchain.py +++ b/rainbowadn/chain/blockchain.py @@ -26,7 +26,7 @@ class BlockChain( HeaderType, ActualStateType ], - Generic[HeaderType, StateType, ActualStateType], + Generic[HeaderType, ActualStateType, StateType], ): def __init__( self, @@ -40,7 +40,7 @@ class BlockChain( ): assert isinstance(reference, NullableReference) assert isinstance(protocol, BlockChainProtocol) - super().__init__(reference, protocol.resolver) + super().__init__(reference) self.protocol = protocol def factory(self) -> ChainCollectionFactory[ @@ -85,7 +85,7 @@ class BlockChain( block: Block[ HeaderType, StateType - ] = self.resolver.resolve(self.reference.reference.value) + ] = self.reference.reference.value.resolve() assert isinstance(block, Block) return NullableReference.of(block.state) else: @@ -122,7 +122,7 @@ class BlockChain( block: Block[ HeaderType, StateType - ] = self.resolver.resolve(self.reference.reference.value) + ] = self.reference.reference.value.resolve() assert isinstance(block, Block) return self.factory().from_reference(block.previous) @@ -171,7 +171,7 @@ class BlockChain( ] ) -> HashPoint[ActualStateType]: assert isinstance(block, Block) - return self.protocol.actual_state(self.resolver.resolve(block.state)) + return self.protocol.actual_state(block.state.resolve()) def loose(self) -> ChainCollectionInterface[ Block[ @@ -193,7 +193,7 @@ class BlockChainFactory( HeaderType, ActualStateType ], - Generic[HeaderType, StateType, ActualStateType] + Generic[HeaderType, ActualStateType, StateType] ): def __init__( self, @@ -202,7 +202,7 @@ class BlockChainFactory( assert isinstance(protocol, BlockChainProtocol) self.protocol = protocol - def empty(self) -> BlockChain[HeaderType, StateType, ActualStateType]: + def empty(self) -> BlockChain[HeaderType, ActualStateType, StateType]: return BlockChain( NullableReference( Null(), @@ -223,7 +223,7 @@ class BlockChainFactory( ], ] ) -> BlockChain[ - HeaderType, StateType, ActualStateType + HeaderType, ActualStateType, StateType ]: assert isinstance(reference, NullableReference) return BlockChain( diff --git a/rainbowadn/chain/blockchainprotocol.py b/rainbowadn/chain/blockchainprotocol.py index c8b2599..76095b2 100644 --- a/rainbowadn/chain/blockchainprotocol.py +++ b/rainbowadn/chain/blockchainprotocol.py @@ -2,7 +2,6 @@ from typing import Generic, TypeVar from rainbowadn.chain.states.activestateprotocol import ActiveStateProtocol from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.rainbow_factory import RainbowFactory __all__ = ('BlockChainProtocol',) @@ -27,8 +26,6 @@ class BlockChainProtocol( self.state_protocol = protocol self.header_factory = header_factory self.state_factory = state_factory - self.resolver = protocol.resolver - assert isinstance(self.resolver, HashResolver) def actual_state_factory(self) -> RainbowFactory[ActualStateType]: raise NotImplementedError diff --git a/rainbowadn/chain/blockcollectioninterface.py b/rainbowadn/chain/blockcollectioninterface.py index 1dd5008..e36437e 100644 --- a/rainbowadn/chain/blockcollectioninterface.py +++ b/rainbowadn/chain/blockcollectioninterface.py @@ -2,7 +2,6 @@ from typing import Generic, TypeVar from rainbowadn.data.collection.collection_interface.collectioninterface import CollectionInterface from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference @@ -23,12 +22,9 @@ class BlockCollectionInterface( ): def __init__( self, - reference: NullableReference[BlockType], - resolver: HashResolver + reference: NullableReference[BlockType] ): assert isinstance(reference, NullableReference) - assert isinstance(resolver, HashResolver) - self.resolver = resolver super().__init__(reference) def _verify_link( @@ -47,7 +43,7 @@ class BlockCollectionInterface( return True elif isinstance(self.reference.reference, NotNull): return self._verify_link( - self.resolver.resolve(self.reference.reference.value), + self.reference.reference.value.resolve(), previous ) else: @@ -63,7 +59,7 @@ class BlockCollectionInterface( if isinstance(self.reference.reference, Null): return NullableReference(Null(), self._actual_state_factory()) elif isinstance(self.reference.reference, NotNull): - return NullableReference.of(self._actual_state(self.resolver.resolve(self.reference.reference.value))) + return NullableReference.of(self._actual_state(self.reference.reference.value.resolve())) else: raise TypeError diff --git a/rainbowadn/chain/reduction/reduction.py b/rainbowadn/chain/reduction/reduction.py index 27679df..cae6a86 100644 --- a/rainbowadn/chain/reduction/reduction.py +++ b/rainbowadn/chain/reduction/reduction.py @@ -1,12 +1,12 @@ from typing import Generic, Iterable, TypeVar from rainbowadn.chain.reduction.reductionresult import ReductionResult -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('Reduction', 'ReductionFactory',) @@ -34,11 +34,10 @@ class Reduction( def __factory__(self) -> RainbowFactory['Reduction[ReductorType, AccumulatorType]']: return ReductionFactory(self.reductor.factory, self.accumulator.factory) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(reduction)' \ - f'{tabulate(tab)}{hash_point_format(self.accumulator, resolver, tab)}' + f'{tabulate(tab)}{hash_point_format(self.accumulator, tab)}' class ReductionFactory( @@ -55,9 +54,10 @@ class ReductionFactory( self.reductor_factory = reductor_factory self.accumulator_factory = accumulator_factory - def from_bytes(self, source: bytes) -> Reduction[ReductorType, AccumulatorType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> Reduction[ReductorType, AccumulatorType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return Reduction( - HashPoint(self.reductor_factory, source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(self.accumulator_factory, source[HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(self.reductor_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin(self.accumulator_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(), ) diff --git a/rainbowadn/chain/reduction/reductionprotocol.py b/rainbowadn/chain/reduction/reductionprotocol.py index b991045..eb2284d 100644 --- a/rainbowadn/chain/reduction/reductionprotocol.py +++ b/rainbowadn/chain/reduction/reductionprotocol.py @@ -3,7 +3,6 @@ from typing import Generic, TypeVar from rainbowadn.chain.reduction.reduction import Reduction from rainbowadn.chain.reduction.reductionresult import ReductionResult from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.rainbow_factory import RainbowFactory __all__ = ('ReductionProtocol',) @@ -13,10 +12,6 @@ AccumulatorType = TypeVar('AccumulatorType') class ReductionProtocol(Generic[ReductorType, AccumulatorType]): - def __init__(self, resolver: HashResolver): - assert isinstance(resolver, HashResolver) - self.resolver = resolver - def reduce( self, reduction: Reduction[ReductorType, AccumulatorType] diff --git a/rainbowadn/chain/reduction/reductionstageprotocol.py b/rainbowadn/chain/reduction/reductionstageprotocol.py index a6ea0c8..c5baeef 100644 --- a/rainbowadn/chain/reduction/reductionstageprotocol.py +++ b/rainbowadn/chain/reduction/reductionstageprotocol.py @@ -26,7 +26,7 @@ class ReductionStageProtocol( def __init__(self, protocol: ReductionProtocol[ReductorType, AccumulatorType]): assert isinstance(protocol, ReductionProtocol) self.protocol = protocol - super().__init__(protocol.resolver) + super().__init__() def _derive_accumulator( self, @@ -72,8 +72,4 @@ class ReductionStageProtocol( stage: HashPoint[Reduction[ReductorType, AccumulatorType]] ) -> Derived[ReductorType, Reduction[ReductorType, AccumulatorType]]: assert isinstance(stage, HashPoint) - return self._derived_from_reduced( - self.protocol.reduce( - self.resolver.resolve(stage) - ) - ) + return self._derived_from_reduced(self.protocol.reduce(stage.resolve())) diff --git a/rainbowadn/chain/stages/derivation/activestageprotocol.py b/rainbowadn/chain/stages/derivation/activestageprotocol.py index 6410079..3210a3d 100644 --- a/rainbowadn/chain/stages/derivation/activestageprotocol.py +++ b/rainbowadn/chain/stages/derivation/activestageprotocol.py @@ -35,8 +35,9 @@ class ActiveStageProtocol( ) -> Derived[BaseStateType, StageType]: raise NotImplementedError + @classmethod def _previous_base_state( - self, + cls, previous_reference: NullableReference[StateStage[HeaderType, BaseStateType, StageType]], base_state_factory: RainbowFactory[BaseStateType], ) -> NullableReference[BaseStateType]: @@ -45,8 +46,8 @@ class ActiveStageProtocol( if isinstance(previous_reference.reference, Null): return NullableReference(Null(), base_state_factory) elif isinstance(previous_reference.reference, NotNull): - previous_state_stage: StateStage[HeaderType, BaseStateType, StageType] = self.resolver.resolve( - previous_reference.reference.value + previous_state_stage: StateStage[HeaderType, BaseStateType, StageType] = ( + previous_reference.reference.value.resolve() ) assert isinstance(previous_state_stage, StateStage) return NullableReference.of(previous_state_stage.state) diff --git a/rainbowadn/chain/stages/derivation/activestagestateprotocol.py b/rainbowadn/chain/stages/derivation/activestagestateprotocol.py index b4d248b..62bbe36 100644 --- a/rainbowadn/chain/stages/derivation/activestagestateprotocol.py +++ b/rainbowadn/chain/stages/derivation/activestagestateprotocol.py @@ -33,7 +33,6 @@ class ActiveStageStateProtocol( assert isinstance(protocol, ActiveStageProtocol) assert isinstance(stage_factory, RainbowFactory) assert isinstance(base_state_factory, RainbowFactory) - super().__init__(protocol.resolver) self.protocol = protocol self.stage_factory = stage_factory self.base_state_factory = base_state_factory diff --git a/rainbowadn/chain/stages/stage.py b/rainbowadn/chain/stages/stage.py index 34b4e53..7e325ab 100644 --- a/rainbowadn/chain/stages/stage.py +++ b/rainbowadn/chain/stages/stage.py @@ -1,14 +1,15 @@ from typing import Generic, Iterable, TypeVar from rainbowadn.chain.stages.stageprotocol import StageProtocol -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ( 'StageStage', @@ -46,8 +47,9 @@ class StageStage( self.previous = previous self.stage = stage + @classmethod def _previous_state( - self, + cls, previous: NullableReference['StateStage[HeaderType, BaseStateType, StageType]'], base_factory: RainbowFactory[BaseStateType], ) -> NullableReference[BaseStateType]: @@ -56,9 +58,7 @@ class StageStage( if isinstance(previous.reference, Null): return NullableReference(Null(), base_factory) elif isinstance(previous.reference, NotNull): - state_stage: StateStage[HeaderType, BaseStateType, StageType] = self.protocol.resolver.resolve( - previous.reference.value - ) + state_stage: StateStage[HeaderType, BaseStateType, StageType] = previous.reference.value.resolve() assert isinstance(state_stage, StateStage) return NullableReference.of(state_stage.state) else: @@ -80,9 +80,7 @@ class StageStage( self.stage ) elif isinstance(self.previous.reference, NotNull): - previous_stage: StageStage[HeaderType, BaseStateType, StageType] = self.protocol.resolver.resolve( - self.previous.reference.value - ) + previous_stage: StageStage[HeaderType, BaseStateType, StageType] = self.previous.reference.value.resolve() assert isinstance(previous_stage, StageStage) return self.protocol.verify_stage( previous_stage.stage, @@ -107,11 +105,10 @@ class StageStage( self.stage.factory ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{self.previous.str(resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.stage, resolver, tab)}' + return f'{self.previous.str(tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.stage, tab)}' class StageStageFactory(RainbowFactory[StageStage[HeaderType, BaseStateType, StageType]]): @@ -125,12 +122,13 @@ class StageStageFactory(RainbowFactory[StageStage[HeaderType, BaseStateType, Sta self.protocol = protocol self.stage_factory = stage_factory - def from_bytes(self, source: bytes) -> StageStage[HeaderType, BaseStateType, StageType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> StageStage[HeaderType, BaseStateType, StageType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return StageStage( self.protocol, - NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH]), - HashPoint(self.stage_factory, source[HashPoint.HASH_LENGTH:], Null()) + NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH], resolver), + ResolverOrigin(self.stage_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point() ) @@ -162,9 +160,7 @@ class StateStage( ) -> bool: assert isinstance(previous, NullableReference) assert isinstance(header, HashPoint) - previous_stage: StageStage[HeaderType, BaseStateType, StageType] = self.protocol.resolver.resolve( - self.previous - ) + previous_stage: StageStage[HeaderType, BaseStateType, StageType] = self.previous.resolve() assert isinstance(previous_stage, StageStage) return self.protocol.verify_state( previous_stage.stage, @@ -188,11 +184,10 @@ class StateStage( self.state.factory ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{hash_point_format(self.previous, resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.state, resolver, tab)}' + return f'{hash_point_format(self.previous, tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.state, tab)}' class StateStageFactory(RainbowFactory[StateStage[HeaderType, BaseStateType, StageType]]): @@ -209,17 +204,19 @@ class StateStageFactory(RainbowFactory[StateStage[HeaderType, BaseStateType, Sta self.stage_factory = stage_factory self.state_factory = state_factory - def from_bytes(self, source: bytes) -> StateStage[HeaderType, BaseStateType, StageType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> StateStage[HeaderType, BaseStateType, StageType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return StateStage( self.protocol, - HashPoint( + ResolverOrigin( StageStageFactory( self.protocol, self.stage_factory ), - source[:HashPoint.HASH_LENGTH], Null() - ), - HashPoint(self.state_factory, source[HashPoint.HASH_LENGTH:], Null()), + source[:HashPoint.HASH_LENGTH], + resolver + ).hash_point(), + ResolverOrigin(self.state_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(), self.stage_factory ) diff --git a/rainbowadn/chain/stages/stageprotocol.py b/rainbowadn/chain/stages/stageprotocol.py index 03b29b6..75bd1e7 100644 --- a/rainbowadn/chain/stages/stageprotocol.py +++ b/rainbowadn/chain/stages/stageprotocol.py @@ -1,7 +1,6 @@ from typing import Generic, TypeVar from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.nullablereference import NullableReference __all__ = ('StageProtocol',) @@ -12,10 +11,6 @@ StageType = TypeVar('StageType') class StageProtocol(Generic[HeaderType, BaseStateType, StageType]): - def __init__(self, resolver: HashResolver): - assert isinstance(resolver, HashResolver) - self.resolver = resolver - def verify_header( self, previous: NullableReference[BaseStateType], diff --git a/rainbowadn/chain/stages/stagestateprotocol.py b/rainbowadn/chain/stages/stagestateprotocol.py index 4f2bcd3..a5bd4cd 100644 --- a/rainbowadn/chain/stages/stagestateprotocol.py +++ b/rainbowadn/chain/stages/stagestateprotocol.py @@ -25,4 +25,4 @@ class StageStateProtocol( assert isinstance(previous, NullableReference) assert isinstance(header, HashPoint) assert isinstance(state, HashPoint) - return self.resolver.resolve(state).verify(previous, header) + return state.resolve().verify(previous, header) diff --git a/rainbowadn/chain/states/stateprotocol.py b/rainbowadn/chain/states/stateprotocol.py index ffcbce1..81b3fe4 100644 --- a/rainbowadn/chain/states/stateprotocol.py +++ b/rainbowadn/chain/states/stateprotocol.py @@ -1,7 +1,6 @@ from typing import Generic, TypeVar from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.nullablereference import NullableReference __all__ = ('StateProtocol',) @@ -11,10 +10,6 @@ StateType = TypeVar('StateType') class StateProtocol(Generic[HeaderType, StateType]): - def __init__(self, resolver: HashResolver): - assert isinstance(resolver, HashResolver) - self.resolver = resolver - def verify( self, previous: NullableReference[StateType], diff --git a/rainbowadn/data/atomic/atomic.py b/rainbowadn/data/atomic/atomic.py index 4a2c02d..885e5d3 100644 --- a/rainbowadn/data/atomic/atomic.py +++ b/rainbowadn/data/atomic/atomic.py @@ -1,7 +1,8 @@ import abc -from typing import TypeVar +from typing import Type, TypeVar from rainbowadn.hashing.hashpoint import HashPoint +from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.static import StaticMentionable __all__ = ('Atomic',) @@ -12,3 +13,13 @@ AtomicMentioned = TypeVar('AtomicMentioned') class Atomic(StaticMentionable, abc.ABC): def __topology_hash__(self) -> bytes: return HashPoint.hash(b'') + + @classmethod + def from_bytes(cls: Type[AtomicMentioned], source: bytes, resolver: HashResolver) -> AtomicMentioned: + assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) + return cls._from_bytes(source) + + @classmethod + def _from_bytes(cls: Type[AtomicMentioned], source: bytes) -> AtomicMentioned: + raise NotImplementedError diff --git a/rainbowadn/data/atomic/integer.py b/rainbowadn/data/atomic/integer.py index f037849..0bcf44d 100644 --- a/rainbowadn/data/atomic/integer.py +++ b/rainbowadn/data/atomic/integer.py @@ -10,7 +10,7 @@ class Integer(Atomic): self.integer = integer @classmethod - def from_bytes(cls, source: bytes) -> 'Integer': + def _from_bytes(cls, source: bytes) -> 'Integer': assert isinstance(source, bytes) return cls(int.from_bytes(source, 'little')) diff --git a/rainbowadn/data/atomic/plain.py b/rainbowadn/data/atomic/plain.py index 38c277a..9d52b9d 100644 --- a/rainbowadn/data/atomic/plain.py +++ b/rainbowadn/data/atomic/plain.py @@ -9,7 +9,7 @@ class Plain(Atomic): self.source = source @classmethod - def from_bytes(cls, source: bytes) -> 'Plain': + def _from_bytes(cls, source: bytes) -> 'Plain': assert isinstance(source, bytes) return cls(source) diff --git a/rainbowadn/data/collection/array/array.py b/rainbowadn/data/collection/array/array.py index cd7d51c..1921810 100644 --- a/rainbowadn/data/collection/array/array.py +++ b/rainbowadn/data/collection/array/array.py @@ -3,9 +3,9 @@ from typing import Generic, Iterable, TypeVar from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('Array', 'ArrayFactory',) @@ -32,12 +32,11 @@ class Array(RecursiveMentionable, Generic[ElementType]): def __factory__(self) -> RainbowFactory['Array']: return ArrayFactory(self.factory) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) formatted = f'(' for hash_point in self.array: - formatted += f'{tabulate(tab+1)}{hash_point_format(hash_point,resolver,tab+1)}' + formatted += f'{tabulate(tab + 1)}{hash_point_format(hash_point, tab + 1)}' return f'{formatted}' \ f'{tabulate(tab)})' @@ -50,12 +49,13 @@ class ArrayFactory(RainbowFactory[Array], Generic[ElementType]): assert isinstance(factory, RainbowFactory) self.factory = factory - def from_bytes(self, source: bytes) -> Array: + def from_bytes(self, source: bytes, resolver: HashResolver) -> Array: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return Array( self.factory, tuple( - HashPoint(self.factory, source[i:i + HashPoint.HASH_LENGTH], Null()) + ResolverOrigin(self.factory, source[i:i + HashPoint.HASH_LENGTH], resolver).hash_point() for i in diff --git a/rainbowadn/data/collection/keymetadata.py b/rainbowadn/data/collection/keymetadata.py index b668b8f..20e8d44 100644 --- a/rainbowadn/data/collection/keymetadata.py +++ b/rainbowadn/data/collection/keymetadata.py @@ -1,11 +1,11 @@ from typing import Generic, Iterable, TypeVar from rainbowadn.data.collection.keyed import Keyed -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.rainbow_factory import RainbowFactory +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('KeyMetadata', 'KeyMetadataFactory',) @@ -29,11 +29,10 @@ class KeyMetadata(Keyed[ActiveKeyType], Generic[ActiveKeyType, MetaDataType]): def __factory__(self) -> RainbowFactory['KeyMetadata[ActiveKeyType, MetaDataType]']: return KeyMetadataFactory(self.key.factory, self.metadata.factory) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{hash_point_format(self.key, resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.metadata, resolver, tab)}' + return f'{hash_point_format(self.key, tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.metadata, tab)}' class KeyMetadataFactory( @@ -46,11 +45,12 @@ class KeyMetadataFactory( self.key_factory = key_factory self.metadata_factory = metadata_factory - def from_bytes(self, source: bytes) -> KeyMetadata[ActiveKeyType, MetaDataType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> KeyMetadata[ActiveKeyType, MetaDataType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return KeyMetadata( - HashPoint(self.key_factory, source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(self.metadata_factory, source[HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(self.key_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin(self.metadata_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(), ) def loose(self) -> RainbowFactory[KeyMetadata[ActiveKeyType, MetaDataType]]: diff --git a/rainbowadn/data/collection/keymetadataquerycollection.py b/rainbowadn/data/collection/keymetadataquerycollection.py index a68f03d..975ccf4 100644 --- a/rainbowadn/data/collection/keymetadataquerycollection.py +++ b/rainbowadn/data/collection/keymetadataquerycollection.py @@ -3,7 +3,6 @@ from typing import Generic, TypeVar from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface from rainbowadn.data.collection.keymetadata import KeyMetadata from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullable import Nullable @@ -19,14 +18,11 @@ class KeyMetadataQueryCollection(QueryCollectionInterface[ActiveKeyType], Generi self, metadata: HashPoint[MetaDataType], collection: QueryCollectionInterface[KeyMetadata[ActiveKeyType, MetaDataType]], - resolver: HashResolver ): assert isinstance(metadata, HashPoint) assert isinstance(collection, QueryCollectionInterface) - assert isinstance(resolver, HashResolver) self.metadata = metadata self.collection = collection - self.resolver = resolver def query(self, key: HashPoint[ActiveKeyType]) -> Nullable[HashPoint[ActiveKeyType]]: assert isinstance(key, HashPoint) @@ -39,6 +35,6 @@ class KeyMetadataQueryCollection(QueryCollectionInterface[ActiveKeyType], Generi elif isinstance(result, NotNull): hash_point: HashPoint[KeyMetadata[ActiveKeyType, MetaDataType]] = result.value assert isinstance(hash_point, HashPoint) - return NotNull(self.resolver.resolve(hash_point).key) + return NotNull(hash_point.resolve().key) else: raise TypeError diff --git a/rainbowadn/data/collection/keyvalue.py b/rainbowadn/data/collection/keyvalue.py index 82ca7ac..56ab033 100644 --- a/rainbowadn/data/collection/keyvalue.py +++ b/rainbowadn/data/collection/keyvalue.py @@ -2,8 +2,9 @@ from typing import Generic, Iterable, TypeVar from rainbowadn.data.collection.keyed import Keyed from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.nullability.null import Null +from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.rainbow_factory import RainbowFactory +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('KeyValue', 'KeyValueFactory',) @@ -38,9 +39,10 @@ class KeyValueFactory( self.key_factory = key_factory self.value_factory = value_factory - def from_bytes(self, source: bytes) -> KeyValue[KVKeyType, KVValueType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> KeyValue[KVKeyType, KVValueType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return KeyValue( - HashPoint(self.key_factory, source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(self.value_factory, source[HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(self.key_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin(self.value_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(), ) diff --git a/rainbowadn/data/collection/mapping/querymapping.py b/rainbowadn/data/collection/mapping/querymapping.py index 8cc18cd..4e9dbbd 100644 --- a/rainbowadn/data/collection/mapping/querymapping.py +++ b/rainbowadn/data/collection/mapping/querymapping.py @@ -3,7 +3,6 @@ from typing import Generic, TypeVar from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface from rainbowadn.data.collection.keyvalue import KeyValue from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullable import Nullable @@ -18,15 +17,12 @@ class QueryMapping(Generic[KVKeyType, KVValueType]): def __init__( self, collection: QueryCollectionInterface[KeyValue[KVKeyType, KVValueType]], - empty_value: HashPoint[KVValueType], - resoler: HashResolver + empty_value: HashPoint[KVValueType] ): assert isinstance(collection, QueryCollectionInterface) assert isinstance(empty_value, HashPoint) - assert isinstance(resoler, HashResolver) self.collection = collection self.empty_value = empty_value - self.resolver = resoler def query(self, key: HashPoint[KVKeyType]) -> Nullable[HashPoint[KVValueType]]: assert isinstance(key, HashPoint) @@ -37,7 +33,7 @@ class QueryMapping(Generic[KVKeyType, KVValueType]): if isinstance(query_result, Null): return Null() elif isinstance(query_result, NotNull): - key_value: KeyValue[KVKeyType, KVValueType] = self.resolver.resolve(query_result.value) + key_value: KeyValue[KVKeyType, KVValueType] = query_result.value.resolve() assert isinstance(key_value, KeyValue) return NotNull(key_value.value) else: diff --git a/rainbowadn/data/collection/stack/stack.py b/rainbowadn/data/collection/stack/stack.py index 3004a87..f291670 100644 --- a/rainbowadn/data/collection/stack/stack.py +++ b/rainbowadn/data/collection/stack/stack.py @@ -7,6 +7,7 @@ from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('Stack', 'StackFactory',) @@ -34,11 +35,10 @@ class Stack(RecursiveMentionable, Generic[ElementType]): def __bytes__(self): return bytes(self.previous) + bytes(self.element) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{self.previous.str(resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.element, resolver, tab)}' + return f'{self.previous.str(tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.element, tab)}' @classmethod def of( @@ -74,11 +74,12 @@ class StackFactory(RainbowFactory[Stack[ElementType]], Generic[ElementType]): assert isinstance(factory, RainbowFactory) self.factory = factory - def from_bytes(self, source: bytes) -> Stack[ElementType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> Stack[ElementType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return Stack( - NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH]), - HashPoint(self.factory, source[HashPoint.HASH_LENGTH:], Null()) + NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH], resolver), + ResolverOrigin(self.factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point() ) def loose(self) -> RainbowFactory[Stack[ElementType]]: diff --git a/rainbowadn/data/collection/trees/binary/activebinarytree.py b/rainbowadn/data/collection/trees/binary/activebinarytree.py index f3c144b..48b9221 100644 --- a/rainbowadn/data/collection/trees/binary/activebinarytree.py +++ b/rainbowadn/data/collection/trees/binary/activebinarytree.py @@ -12,7 +12,6 @@ from rainbowadn.data.collection.trees.binary.querybinarytree import QueryBinaryT from rainbowadn.data.collection.trees.comparison.comparator import (Comparator, Comparison, Left, Replace, Right) from rainbowadn.data.collection.trees.comparison.keyedcomparator import KeyedComparator from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference @@ -39,8 +38,6 @@ class ActiveBinaryTree( self.protocol = protocol self.comparator = protocol.comparator assert isinstance(self.comparator, Comparator) - self.resolver = protocol.resolver - assert isinstance(self.resolver, HashResolver) self.creation = ActiveCreationProtocol(protocol) assert isinstance(self.creation, BinaryTreeCreationProtocol) @@ -96,8 +93,7 @@ class ActiveBinaryTree( def query_tree(self) -> QueryCollectionInterface[ActiveKeyType]: return KeyMetadataQueryCollection( self.protocol.empty_metadata(), - QueryBinaryTree(KeyedComparator(self.comparator), self.reference), - self.resolver + QueryBinaryTree(KeyedComparator(self.comparator), self.reference) ) @@ -119,9 +115,9 @@ class ActiveCreationProtocol(BalancedTreeCreationProtocol[ActiveKeyType, MetaDat assert isinstance(tree.reference.reference, NotNull) hash_point: HashPoint[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]] = tree.reference.reference.value assert isinstance(hash_point, HashPoint) - resolved: BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]] = self.resolver.resolve(hash_point) + resolved: BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]] = hash_point.resolve() assert isinstance(resolved, BinaryTree) - key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = self.resolver.resolve(resolved.key) + key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = resolved.key.resolve() assert isinstance(key_metadata, KeyMetadata) return tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer) diff --git a/rainbowadn/data/collection/trees/binary/avl.py b/rainbowadn/data/collection/trees/binary/avl.py index 8083b90..8b82c35 100644 --- a/rainbowadn/data/collection/trees/binary/avl.py +++ b/rainbowadn/data/collection/trees/binary/avl.py @@ -26,14 +26,15 @@ class AVLBTBP(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]): Integer(1 + max(self.height(treel, protocol), self.height(treer, protocol))) ) + @classmethod def height( - self, + cls, tree: TreeType, protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType] ) -> int: if protocol.splittable(tree): _, _, metadata, _ = protocol.split(tree) - return self.resolver.resolve(metadata).integer + return metadata.resolve().integer else: return 0 diff --git a/rainbowadn/data/collection/trees/binary/balancedtreecreationprotocol.py b/rainbowadn/data/collection/trees/binary/balancedtreecreationprotocol.py index 7a3a38d..37f2b52 100644 --- a/rainbowadn/data/collection/trees/binary/balancedtreecreationprotocol.py +++ b/rainbowadn/data/collection/trees/binary/balancedtreecreationprotocol.py @@ -3,7 +3,6 @@ from typing import Generic, TypeVar from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver __all__ = ('BalancedTreeCreationProtocol',) @@ -22,8 +21,6 @@ class BalancedTreeCreationProtocol( ): assert isinstance(protocol, BinaryTreeBalancingProtocol) self.protocol = protocol - self.resolver = protocol.resolver - assert isinstance(self.resolver, HashResolver) def splittable(self, tree: TreeType) -> bool: raise NotImplementedError diff --git a/rainbowadn/data/collection/trees/binary/binarytree.py b/rainbowadn/data/collection/trees/binary/binarytree.py index 33601ae..751f32a 100644 --- a/rainbowadn/data/collection/trees/binary/binarytree.py +++ b/rainbowadn/data/collection/trees/binary/binarytree.py @@ -1,12 +1,12 @@ from typing import Generic, Iterable, TypeVar -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('BinaryTree', 'BinaryTreeFactory',) @@ -40,12 +40,11 @@ class BinaryTree(RecursiveMentionable, Generic[TreeKeyType]): def __bytes__(self): return bytes(self.treel) + bytes(self.treer) + bytes(self.key) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{self.treel.str(resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.key, resolver, tab)}' \ - f'{tabulate(tab)}{self.treer.str(resolver, tab)}' + return f'{self.treel.str(tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.key, tab)}' \ + f'{tabulate(tab)}{self.treer.str(tab)}' class BinaryTreeFactory(RainbowFactory[BinaryTree[TreeKeyType]], Generic[TreeKeyType]): @@ -53,12 +52,16 @@ class BinaryTreeFactory(RainbowFactory[BinaryTree[TreeKeyType]], Generic[TreeKey assert isinstance(factory, RainbowFactory) self.factory = factory - def from_bytes(self, source: bytes) -> BinaryTree[TreeKeyType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> BinaryTree[TreeKeyType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return BinaryTree( - NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH]), - NullableReferenceFactory(self).from_bytes(source[HashPoint.HASH_LENGTH:HashPoint.HASH_LENGTH * 2]), - HashPoint(self.factory, source[HashPoint.HASH_LENGTH * 2:], Null()) + NullableReferenceFactory(self).from_bytes(source[:HashPoint.HASH_LENGTH], resolver), + NullableReferenceFactory(self).from_bytes( + source[HashPoint.HASH_LENGTH:HashPoint.HASH_LENGTH * 2], + resolver + ), + ResolverOrigin(self.factory, source[HashPoint.HASH_LENGTH * 2:], resolver).hash_point() ) def loose(self) -> RainbowFactory[BinaryTree[TreeKeyType]]: diff --git a/rainbowadn/data/collection/trees/binary/binarytreebalancingprotocol.py b/rainbowadn/data/collection/trees/binary/binarytreebalancingprotocol.py index 0e95f39..80cc48d 100644 --- a/rainbowadn/data/collection/trees/binary/binarytreebalancingprotocol.py +++ b/rainbowadn/data/collection/trees/binary/binarytreebalancingprotocol.py @@ -3,7 +3,6 @@ from typing import Generic, TypeVar from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol from rainbowadn.data.collection.trees.comparison.comparator import Comparator from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver __all__ = ('BinaryTreeBalancingProtocol',) @@ -19,8 +18,6 @@ class BinaryTreeBalancingProtocol(Generic[ActiveKeyType, MetaDataType, TreeType] ): assert isinstance(comparator, Comparator) self.comparator = comparator - self.resolver = comparator.resolver - assert isinstance(self.resolver, HashResolver) def empty_metadata(self) -> HashPoint[MetaDataType]: raise NotImplementedError diff --git a/rainbowadn/data/collection/trees/binary/querybinarytree.py b/rainbowadn/data/collection/trees/binary/querybinarytree.py index 844e168..b0198c3 100644 --- a/rainbowadn/data/collection/trees/binary/querybinarytree.py +++ b/rainbowadn/data/collection/trees/binary/querybinarytree.py @@ -1,10 +1,9 @@ from typing import Generic, TypeVar +from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface from rainbowadn.data.collection.trees.binary.binarytree import BinaryTree from rainbowadn.data.collection.trees.comparison.comparator import Comparator, Comparison, Equal, Left, Right -from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullable import Nullable @@ -23,8 +22,6 @@ class QueryBinaryTree(QueryCollectionInterface[KeyType], Generic[KeyType]): assert isinstance(reference, NullableReference) self.comparator = comparator self.reference = reference - self.resolver = comparator.resolver - assert isinstance(self.resolver, HashResolver) def query(self, key: HashPoint[KeyType]) -> Nullable[HashPoint[KeyType]]: assert isinstance(key, HashPoint) @@ -35,7 +32,7 @@ class QueryBinaryTree(QueryCollectionInterface[KeyType], Generic[KeyType]): elif isinstance(reference, NotNull): hash_point: HashPoint[BinaryTree[KeyType]] = reference.value assert isinstance(hash_point, HashPoint) - tree: BinaryTree[KeyType] = self.resolver.resolve(hash_point) + tree: BinaryTree[KeyType] = hash_point.resolve() assert isinstance(tree, BinaryTree) comparison: Comparison = self.comparator.compare(tree.key, key) assert isinstance(comparison, Comparison) diff --git a/rainbowadn/data/collection/trees/comparison/comparator.py b/rainbowadn/data/collection/trees/comparison/comparator.py index af50d20..3f58def 100644 --- a/rainbowadn/data/collection/trees/comparison/comparator.py +++ b/rainbowadn/data/collection/trees/comparison/comparator.py @@ -2,7 +2,6 @@ import abc from typing import Generic, TypeVar from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver __all__ = ( 'Comparison', @@ -48,9 +47,5 @@ KeyType = TypeVar('KeyType') class Comparator(Generic[KeyType]): - def __init__(self, resolver: HashResolver): - assert isinstance(resolver, HashResolver) - self.resolver = resolver - def compare(self, original: HashPoint[KeyType], key: HashPoint[KeyType]) -> Comparison: raise NotImplementedError diff --git a/rainbowadn/data/collection/trees/comparison/keyedcomparator.py b/rainbowadn/data/collection/trees/comparison/keyedcomparator.py index 126d625..4fdc739 100644 --- a/rainbowadn/data/collection/trees/comparison/keyedcomparator.py +++ b/rainbowadn/data/collection/trees/comparison/keyedcomparator.py @@ -15,7 +15,7 @@ class KeyedComparator( def __init__(self, comparator: Comparator[ComparatorKeyType]): assert isinstance(comparator, Comparator) self.comparator = comparator - super().__init__(comparator.resolver) + super().__init__() def compare( self, @@ -25,6 +25,6 @@ class KeyedComparator( assert isinstance(original, HashPoint) assert isinstance(key, HashPoint) return self.comparator.compare( - self.resolver.resolve(original).key, - self.resolver.resolve(key).key, + original.resolve().key, + key.resolve().key, ) diff --git a/rainbowadn/data/collection/trees/comparison/plaincomparator.py b/rainbowadn/data/collection/trees/comparison/plaincomparator.py index 40418c5..4f5bd3e 100644 --- a/rainbowadn/data/collection/trees/comparison/plaincomparator.py +++ b/rainbowadn/data/collection/trees/comparison/plaincomparator.py @@ -10,9 +10,9 @@ class PlainComparator(ProtocolComparator[Plain]): def compare(self, original: HashPoint[Plain], key: HashPoint[Plain]) -> Comparison: assert isinstance(original, HashPoint) assert isinstance(key, HashPoint) - original_value: Plain = self.resolver.resolve(original) + original_value: Plain = original.resolve() assert isinstance(original_value, Plain) - key_value: Plain = self.resolver.resolve(key) + key_value: Plain = key.resolve() assert isinstance(key_value, Plain) if key_value.source < original_value.source: return Left() diff --git a/rainbowadn/data/collection/trees/comparison/protocolcomparator.py b/rainbowadn/data/collection/trees/comparison/protocolcomparator.py index 46b1c99..ad5df13 100644 --- a/rainbowadn/data/collection/trees/comparison/protocolcomparator.py +++ b/rainbowadn/data/collection/trees/comparison/protocolcomparator.py @@ -2,7 +2,6 @@ import abc from typing import Generic, TypeVar from rainbowadn.data.collection.trees.comparison.comparator import Comparator, Equal -from rainbowadn.hashing.hashresolver import HashResolver __all__ = ('ProtocolComparator',) @@ -10,8 +9,6 @@ KeyType = TypeVar('KeyType') class ProtocolComparator(Comparator[KeyType], abc.ABC, Generic[KeyType]): - def __init__(self, resolver: HashResolver, equal: Equal): - assert isinstance(resolver, HashResolver) + def __init__(self, equal: Equal): assert isinstance(equal, Equal) - super().__init__(resolver) self.equal = equal diff --git a/rainbowadn/encryption/__init__.py b/rainbowadn/encryption/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rainbowadn/hashing/hash_point_format.py b/rainbowadn/hashing/hash_point_format.py index b67c35e..80ff52c 100644 --- a/rainbowadn/hashing/hash_point_format.py +++ b/rainbowadn/hashing/hash_point_format.py @@ -1,17 +1,15 @@ from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.recursivementionable import RecursiveMentionable __all__ = ('hash_point_format', 'tabulate',) -def hash_point_format(hash_point: HashPoint, resolver: HashResolver, tab: int) -> str: +def hash_point_format(hash_point: HashPoint, tab: int) -> str: assert isinstance(hash_point, HashPoint) - assert isinstance(resolver, HashResolver) assert isinstance(tab, int) - value = resolver.resolve(hash_point) + value = hash_point.resolve() if isinstance(value, RecursiveMentionable): - return value.str(resolver, tab) + return value.str(tab) else: return str(value) diff --git a/rainbowadn/hashing/hashpoint.py b/rainbowadn/hashing/hashpoint.py index e4a6a1b..9db8cfb 100644 --- a/rainbowadn/hashing/hashpoint.py +++ b/rainbowadn/hashing/hashpoint.py @@ -2,9 +2,8 @@ import hashlib from typing import Generic, TypeVar from rainbowadn.hashing.hashmentionable import HashMentionable -from rainbowadn.hashing.nullability.notnull import NotNull -from rainbowadn.hashing.nullability.null import Null -from rainbowadn.hashing.nullability.nullable import Nullable +from rainbowadn.hashing.localorigin import LocalOrigin +from rainbowadn.hashing.origin import Origin from rainbowadn.hashing.rainbow_factory import RainbowFactory __all__ = ('HashPoint',) @@ -18,14 +17,19 @@ def _hash(source: bytes) -> bytes: class HashPoint(Generic[HashMentioned]): - def __init__(self, factory: RainbowFactory[HashMentioned], point: bytes, value: Nullable[HashMentioned]): + def __init__( + self, + factory: RainbowFactory[HashMentioned], + point: bytes, + origin: Origin[HashMentioned] + ): assert isinstance(factory, RainbowFactory) assert isinstance(point, bytes) - assert isinstance(value, Nullable) + assert isinstance(origin, Origin) assert len(point) == self.HASH_LENGTH self.factory = factory self.point = point - self.value = value + self.origin = origin def __bytes__(self): return self.point @@ -50,11 +54,13 @@ class HashPoint(Generic[HashMentioned]): def of(cls, mentioned: HashMentioned) -> 'HashPoint[HashMentioned]': assert isinstance(mentioned, HashMentionable) return cls( - mentioned.__factory__(), cls.hash(cls.bytes_of_mentioned(mentioned)), NotNull(mentioned) + mentioned.__factory__(), cls.hash(cls.bytes_of_mentioned(mentioned)), LocalOrigin(mentioned) ) - def loose(self) -> 'HashPoint[HashMentioned]': - return HashPoint(self.factory, self.point, Null()) + def resolve(self) -> HashMentioned: + resolved = self.origin.resolve() + assert isinstance(resolved, HashMentionable) + return resolved def __eq__(self, other): if isinstance(other, HashPoint): diff --git a/rainbowadn/hashing/hashresolver.py b/rainbowadn/hashing/hashresolver.py index 4057d12..33a7418 100644 --- a/rainbowadn/hashing/hashresolver.py +++ b/rainbowadn/hashing/hashresolver.py @@ -1,29 +1,10 @@ from typing import TypeVar -from rainbowadn.hashing.hashmentionable import HashMentionable -from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.nullability.notnull import NotNull -from rainbowadn.hashing.nullability.null import Null - __all__ = ('HashResolver',) RHashMentioned = TypeVar('RHashMentioned') class HashResolver: - def _resolve(self, point: bytes) -> bytes: + def resolve_bytes(self, point: bytes) -> bytes: raise NotImplementedError - - def resolve(self, hashpoint: HashPoint[RHashMentioned]) -> RHashMentioned: - assert isinstance(hashpoint, HashPoint) - if isinstance(hashpoint.value, NotNull): - return hashpoint.value.value - elif isinstance(hashpoint.value, Null): - resolved: bytes = self._resolve(bytes(hashpoint)) - assert isinstance(resolved, bytes) - mentioned: RHashMentioned = hashpoint.factory.from_bytes(resolved[HashPoint.HASH_LENGTH:]) - assert isinstance(mentioned, HashMentionable) - assert mentioned.__topology_hash__() == resolved[:HashPoint.HASH_LENGTH] - return mentioned - else: - raise TypeError diff --git a/rainbowadn/hashing/localmetaorigin.py b/rainbowadn/hashing/localmetaorigin.py new file mode 100644 index 0000000..adc0fd4 --- /dev/null +++ b/rainbowadn/hashing/localmetaorigin.py @@ -0,0 +1,22 @@ +from typing import Generic, TypeVar + +from rainbowadn.hashing.hashpoint import HashPoint +from rainbowadn.hashing.metaorigin import MetaOrigin +from rainbowadn.hashing.origin import Origin +from rainbowadn.hashing.rainbow_factory import RainbowFactory + +__all__ = ('LocalMetaOrigin',) + +OriginType = TypeVar('OriginType') + + +class LocalMetaOrigin(MetaOrigin[OriginType], Generic[OriginType]): + def __init__(self, origin: Origin[OriginType]): + assert isinstance(origin, Origin) + self._origin = origin + + def origin(self, factory: RainbowFactory[OriginType], point: bytes) -> Origin[OriginType]: + assert isinstance(factory, RainbowFactory) + assert isinstance(point, bytes) + assert len(point) == HashPoint.HASH_LENGTH + return self._origin diff --git a/rainbowadn/hashing/localorigin.py b/rainbowadn/hashing/localorigin.py new file mode 100644 index 0000000..b6ad665 --- /dev/null +++ b/rainbowadn/hashing/localorigin.py @@ -0,0 +1,17 @@ +from typing import Generic, TypeVar + +from rainbowadn.hashing.hashmentionable import HashMentionable +from rainbowadn.hashing.origin import Origin + +__all__ = ('LocalOrigin',) + +OriginType = TypeVar('OriginType') + + +class LocalOrigin(Origin[OriginType], Generic[OriginType]): + def __init__(self, value: OriginType): + assert isinstance(value, HashMentionable) + self.value: OriginType = value + + def resolve(self) -> OriginType: + return self.value diff --git a/rainbowadn/hashing/metaorigin.py b/rainbowadn/hashing/metaorigin.py new file mode 100644 index 0000000..0674f51 --- /dev/null +++ b/rainbowadn/hashing/metaorigin.py @@ -0,0 +1,20 @@ +from typing import Generic, TypeVar + +from rainbowadn.hashing.hashpoint import HashPoint +from rainbowadn.hashing.origin import Origin +from rainbowadn.hashing.rainbow_factory import RainbowFactory + +__all__ = ('MetaOrigin',) + +OriginType = TypeVar('OriginType') + + +class MetaOrigin(Generic[OriginType]): + def origin(self, factory: RainbowFactory[OriginType], point: bytes) -> Origin[OriginType]: + raise NotImplementedError + + def hash_point(self, factory: RainbowFactory[OriginType], point: bytes) -> HashPoint[OriginType]: + assert isinstance(factory, RainbowFactory) + assert isinstance(point, bytes) + assert len(point) == HashPoint.HASH_LENGTH + return HashPoint(factory, point, self.origin(factory, point)) diff --git a/rainbowadn/hashing/nullability/nullablereference.py b/rainbowadn/hashing/nullability/nullablereference.py index fa2f2e5..f472a20 100644 --- a/rainbowadn/hashing/nullability/nullablereference.py +++ b/rainbowadn/hashing/nullability/nullablereference.py @@ -1,13 +1,14 @@ from typing import Generic, Iterable, TypeVar -from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hash_point_format import hash_point_format +from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.notnull import NotNull from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.nullability.nullable import Nullable from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin __all__ = ('NullableReference', 'NullableReferenceFactory',) @@ -53,11 +54,11 @@ class NullableReference(RecursiveMentionable, Generic[ReferencedType]): def off(cls, value: ReferencedType) -> 'NullableReference[ReferencedType]': return cls.of(HashPoint.of(value)) - def str(self, resolver: HashResolver, tab: int) -> str: + def str(self, tab: int) -> str: if isinstance(self.reference, Null): return f'-' elif isinstance(self.reference, NotNull): - return f'{hash_point_format(self.reference.value, resolver, tab)}' + return f'{hash_point_format(self.reference.value, tab)}' else: raise TypeError @@ -76,12 +77,15 @@ class NullableReferenceFactory(RainbowFactory[NullableReference[FReferencedType] assert isinstance(factory, RainbowFactory) self.factory = factory - def from_bytes(self, source: bytes) -> NullableReference[FReferencedType]: + def from_bytes(self, source: bytes, resolver: HashResolver) -> NullableReference[FReferencedType]: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) if source == HashPoint.NULL_HASH: return NullableReference(Null(), self.factory) else: - return NullableReference.of(HashPoint(self.factory, source, Null())) + return NullableReference.of( + ResolverOrigin(self.factory, source, resolver).hash_point() + ) def loose(self) -> RainbowFactory[NullableReference[FReferencedType]]: return self diff --git a/rainbowadn/hashing/origin.py b/rainbowadn/hashing/origin.py new file mode 100644 index 0000000..9c6fd3a --- /dev/null +++ b/rainbowadn/hashing/origin.py @@ -0,0 +1,10 @@ +from typing import Generic, TypeVar + +__all__ = ('Origin',) + +OriginType = TypeVar('OriginType') + + +class Origin(Generic[OriginType]): + def resolve(self) -> OriginType: + raise NotImplementedError diff --git a/rainbowadn/hashing/rainbow_factory.py b/rainbowadn/hashing/rainbow_factory.py index 6cf2e44..05db69e 100644 --- a/rainbowadn/hashing/rainbow_factory.py +++ b/rainbowadn/hashing/rainbow_factory.py @@ -1,5 +1,7 @@ from typing import Generic, TypeVar +from rainbowadn.hashing.hashresolver import HashResolver + __all__ = ('RainbowFactory',) FHashMentioned = TypeVar('FHashMentioned') @@ -8,5 +10,5 @@ FHashMentioned = TypeVar('FHashMentioned') class RainbowFactory(Generic[FHashMentioned]): """вперёд, уроды, вас ждут заводы""" - def from_bytes(self, source: bytes) -> FHashMentioned: + def from_bytes(self, source: bytes, resolver: HashResolver) -> FHashMentioned: raise NotImplementedError diff --git a/rainbowadn/hashing/recursivementionable.py b/rainbowadn/hashing/recursivementionable.py index b4ea8b5..1c4e82b 100644 --- a/rainbowadn/hashing/recursivementionable.py +++ b/rainbowadn/hashing/recursivementionable.py @@ -3,7 +3,6 @@ from typing import Iterable from rainbowadn.hashing.hashmentionable import HashMentionable from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver __all__ = ('RecursiveMentionable',) @@ -12,8 +11,7 @@ class RecursiveMentionable(HashMentionable, abc.ABC): def points(self) -> Iterable[HashPoint]: raise NotImplementedError - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(recursive {self.__class__.__name__})' diff --git a/rainbowadn/hashing/reduce_nullable_reference.py b/rainbowadn/hashing/reduce_nullable_reference.py index 40c5d2e..bcd9532 100644 --- a/rainbowadn/hashing/reduce_nullable_reference.py +++ b/rainbowadn/hashing/reduce_nullable_reference.py @@ -18,8 +18,8 @@ def reduce_nullable_reference( assert isinstance(reference, NullableReference) assert isinstance(resolver, HashResolver) if isinstance(reference.reference, Null): - return reference.factory.from_bytes(HashPoint.NULL_HASH) + return reference.factory.from_bytes(HashPoint.NULL_HASH, resolver) elif isinstance(reference.reference, NotNull): - return resolver.resolve(reference.reference.value) + return reference.reference.value.resolve() else: raise TypeError diff --git a/rainbowadn/hashing/resolvermetaorigin.py b/rainbowadn/hashing/resolvermetaorigin.py new file mode 100644 index 0000000..d864153 --- /dev/null +++ b/rainbowadn/hashing/resolvermetaorigin.py @@ -0,0 +1,28 @@ +from typing import Generic, TypeVar + +from rainbowadn.hashing.hashpoint import HashPoint +from rainbowadn.hashing.hashresolver import HashResolver +from rainbowadn.hashing.metaorigin import MetaOrigin +from rainbowadn.hashing.origin import Origin +from rainbowadn.hashing.rainbow_factory import RainbowFactory +from rainbowadn.hashing.resolverorigin import ResolverOrigin + +__all__ = ('ResolverMetaOrigin',) + +OriginType = TypeVar('OriginType') + + +class ResolverMetaOrigin(MetaOrigin[OriginType], Generic[OriginType]): + def __init__(self, resolver: HashResolver): + assert isinstance(resolver, HashResolver) + self.resolver = resolver + + def origin(self, factory: RainbowFactory[OriginType], point: bytes) -> Origin[OriginType]: + assert isinstance(factory, RainbowFactory) + assert isinstance(point, bytes) + assert len(point) == HashPoint.HASH_LENGTH + return ResolverOrigin(factory, point, self.resolver) + + def migrate(self, hash_point: HashPoint[OriginType]) -> HashPoint[OriginType]: + assert isinstance(hash_point, HashPoint) + return self.hash_point(hash_point.factory, hash_point.point) diff --git a/rainbowadn/hashing/resolverorigin.py b/rainbowadn/hashing/resolverorigin.py new file mode 100644 index 0000000..36c67bd --- /dev/null +++ b/rainbowadn/hashing/resolverorigin.py @@ -0,0 +1,37 @@ +from typing import Generic, TypeVar + +from rainbowadn.hashing.hashmentionable import HashMentionable +from rainbowadn.hashing.hashpoint import HashPoint +from rainbowadn.hashing.hashresolver import HashResolver +from rainbowadn.hashing.origin import Origin +from rainbowadn.hashing.rainbow_factory import RainbowFactory + +__all__ = ('ResolverOrigin',) + +OriginType = TypeVar('OriginType') + + +class ResolverOrigin(Origin[OriginType], Generic[OriginType]): + def __init__( + self, + factory: RainbowFactory[OriginType], + point: bytes, + resolver: HashResolver + ): + assert isinstance(factory, RainbowFactory) + assert isinstance(point, bytes) + assert isinstance(resolver, HashResolver) + self.factory = factory + self.point = point + self.resolver = resolver + + def resolve(self) -> OriginType: + resolved: bytes = self.resolver.resolve_bytes(self.point) + assert isinstance(resolved, bytes) + mentioned: OriginType = self.factory.from_bytes(resolved[HashPoint.HASH_LENGTH:], self.resolver) + assert isinstance(mentioned, HashMentionable) + assert mentioned.__topology_hash__() == resolved[:HashPoint.HASH_LENGTH] + return mentioned + + def hash_point(self) -> HashPoint[OriginType]: + return HashPoint(self.factory, self.point, self) diff --git a/rainbowadn/hashing/static.py b/rainbowadn/hashing/static.py index 913dafb..4cd989a 100644 --- a/rainbowadn/hashing/static.py +++ b/rainbowadn/hashing/static.py @@ -2,6 +2,7 @@ import abc from typing import Generic, Type, TypeVar from rainbowadn.hashing.hashmentionable import HashMentionable +from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.rainbow_factory import RainbowFactory __all__ = ('StaticMentionable', 'StaticFactory',) @@ -11,7 +12,7 @@ StaticMentioned = TypeVar('StaticMentioned') class StaticMentionable(HashMentionable, abc.ABC): @classmethod - def from_bytes(cls: Type[StaticMentioned], source: bytes) -> StaticMentioned: + def from_bytes(cls: Type[StaticMentioned], source: bytes, resolver: HashResolver) -> StaticMentioned: raise NotImplementedError def __factory__(self) -> RainbowFactory[StaticMentioned]: @@ -24,11 +25,12 @@ class StaticMentionable(HashMentionable, abc.ABC): class StaticFactory(RainbowFactory[StaticMentioned], Generic[StaticMentioned]): def __init__(self, cls: Type[StaticMentioned]): - self.cls = cls + self.cls: Type[StaticMentioned] = cls - def from_bytes(self, source: bytes) -> StaticMentioned: + def from_bytes(self, source: bytes, resolver: HashResolver) -> StaticMentioned: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) if issubclass(self.cls, StaticMentionable): - return self.cls.from_bytes(source) + return self.cls.from_bytes(source, resolver) else: raise TypeError diff --git a/rainbowadn/testing/failresolver.py b/rainbowadn/testing/failresolver.py index a65d7df..1ed8c75 100644 --- a/rainbowadn/testing/failresolver.py +++ b/rainbowadn/testing/failresolver.py @@ -2,5 +2,5 @@ from rainbowadn.hashing.hashresolver import HashResolver class FailResolver(HashResolver): - def _resolve(self, point: bytes) -> bytes: + def resolve_bytes(self, point: bytes) -> bytes: raise TypeError('fail-resolver always fails') diff --git a/rainbowadn/v13/bankchain.py b/rainbowadn/v13/bankchain.py index c36224b..e6453c3 100644 --- a/rainbowadn/v13/bankchain.py +++ b/rainbowadn/v13/bankchain.py @@ -4,7 +4,6 @@ from rainbowadn.chain.abstractreductionchainmetafactory import AbstractReduction from rainbowadn.chain.chaincollectioninterface import ChainCollectionInterface from rainbowadn.data.collection.stack.stack import Stack, StackFactory from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.v13.bankprotocol import BankProtocol from rainbowadn.v13.bankstate import BankState @@ -28,22 +27,18 @@ class BankChain(Generic[BlockType]): self.chain = chain self.reference = self.chain.reference assert isinstance(self.reference, NullableReference) - self.resolver = self.chain.resolver - assert isinstance(self.resolver, HashResolver) @classmethod def empty( cls, - factory: AbstractReductionChainMetaFactory[BlockType, NullableReference[Stack[Transaction]], BankState], - resolver: HashResolver + factory: AbstractReductionChainMetaFactory[BlockType, NullableReference[Stack[Transaction]], BankState] ) -> 'BankChain[BlockType]': assert isinstance(factory, AbstractReductionChainMetaFactory) - assert isinstance(resolver, HashResolver) return cls( factory.factory( NullableReferenceFactory(StackFactory(Transaction.factory()).loose()).loose(), BankState.factory(), - BankProtocol(resolver), + BankProtocol(), ).empty() ) @@ -53,17 +48,15 @@ class BankChain(Generic[BlockType]): factory: AbstractReductionChainMetaFactory[BlockType, NullableReference[Stack[Transaction]], BankState], reference: NullableReference[ BlockType - ], - resolver: HashResolver + ] ) -> 'BankChain[BlockType]': assert isinstance(factory, AbstractReductionChainMetaFactory) assert isinstance(reference, NullableReference) - assert isinstance(resolver, HashResolver) return cls( factory.factory( NullableReferenceFactory(StackFactory(Transaction.factory()).loose()).loose(), BankState.factory(), - BankProtocol(resolver), + BankProtocol(), ).from_reference( reference ) @@ -76,6 +69,7 @@ class BankChain(Generic[BlockType]): ) def adds(self, transactions: list[Transaction]) -> 'BankChain[BlockType]': + assert isinstance(transactions, list) return self.add( Stack.off( Transaction.factory(), @@ -87,4 +81,4 @@ class BankChain(Generic[BlockType]): return self.chain.verify() def __str__(self): - return self.reference.str(self.resolver, 0) + return self.reference.str(0) diff --git a/rainbowadn/v13/bankprotocol.py b/rainbowadn/v13/bankprotocol.py index a01b9eb..389100a 100644 --- a/rainbowadn/v13/bankprotocol.py +++ b/rainbowadn/v13/bankprotocol.py @@ -26,18 +26,18 @@ class BankProtocol(ReductionProtocol[NullableReference[Stack[Transaction]], Bank reduction: Reduction[NullableReference[Stack[Transaction]], BankState] ) -> ReductionResult[NullableReference[Stack[Transaction]], BankState]: assert isinstance(reduction, Reduction) - bank_state: BankState = self.resolver.resolve(reduction.accumulator) + bank_state: BankState = reduction.accumulator.resolve() assert isinstance(bank_state, BankState) - reference: Nullable[HashPoint[Stack[Transaction]]] = self.resolver.resolve(reduction.reductor).reference + reference: Nullable[HashPoint[Stack[Transaction]]] = reduction.reductor.resolve().reference assert isinstance(reference, Nullable) if isinstance(reference, Null): - return Reduced(HashPoint.of(bank_state.loose())) + return Reduced(HashPoint.of(bank_state.without_miner())) elif isinstance(reference, NotNull): - stack: Stack[Transaction] = self.resolver.resolve(reference.value) + stack: Stack[Transaction] = reference.value.resolve() assert isinstance(stack, Stack) return Reduction( HashPoint.of(stack.previous), - HashPoint.of(bank_state.push(stack.element, self.resolver)) + HashPoint.of(bank_state.push(stack.element)) ) else: raise TypeError @@ -61,4 +61,4 @@ class BankProtocol(ReductionProtocol[NullableReference[Stack[Transaction]], Bank def header_filter(self, state: HashPoint[BankState]) -> HashPoint[BankState]: assert isinstance(state, HashPoint) - return HashPoint.of(self.resolver.resolve(state).loose().advance(self.resolver)) + return HashPoint.of(state.resolve().without_miner().advance()) diff --git a/rainbowadn/v13/bankstate.py b/rainbowadn/v13/bankstate.py index 69eae1c..614a77b 100644 --- a/rainbowadn/v13/bankstate.py +++ b/rainbowadn/v13/bankstate.py @@ -16,6 +16,7 @@ from rainbowadn.hashing.nullability.nullable import Nullable from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin from rainbowadn.hashing.static import StaticFactory, StaticMentionable from rainbowadn.v13.algo import MINT_CONST from rainbowadn.v13.subject import Subject @@ -48,8 +49,9 @@ class BankState(RecursiveMentionable, StaticMentionable): return bytes(self.minted) + bytes(self.used) + bytes(self.miner) + bytes(self.length) @classmethod - def from_bytes(cls, source: bytes) -> 'BankState': + def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'BankState': assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) reference_factory: RainbowFactory[ NullableReference[BinaryTree[KeyMetadata[Coin, Integer]]] ] = NullableReferenceFactory( @@ -62,15 +64,15 @@ class BankState(RecursiveMentionable, StaticMentionable): ).loose() assert isinstance(reference_factory, RainbowFactory) return cls( - reference_factory.from_bytes(source[:HashPoint.HASH_LENGTH]), - reference_factory.from_bytes(source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH]), + reference_factory.from_bytes(source[:HashPoint.HASH_LENGTH], resolver), + reference_factory.from_bytes(source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH], resolver), NullableReferenceFactory( StaticFactory(Subject) - ).from_bytes(source[2 * HashPoint.HASH_LENGTH:3 * HashPoint.HASH_LENGTH]), - HashPoint(Integer.factory(), source[3 * HashPoint.HASH_LENGTH:], Null()) + ).from_bytes(source[2 * HashPoint.HASH_LENGTH:3 * HashPoint.HASH_LENGTH], resolver), + ResolverOrigin(Integer.factory(), source[3 * HashPoint.HASH_LENGTH:], resolver).hash_point() ) - def loose(self) -> 'BankState': + def without_miner(self) -> 'BankState': return BankState( self.minted, self.used, @@ -78,20 +80,18 @@ class BankState(RecursiveMentionable, StaticMentionable): self.length ) - def advance(self, resolver: HashResolver) -> 'BankState': - assert isinstance(resolver, HashResolver) + def advance(self) -> 'BankState': return BankState( self.minted, self.used, NullableReference(Null(), self.miner.factory), - HashPoint.of(Integer(resolver.resolve(self.length).integer + 1)) + HashPoint.of(Integer(self.length.resolve().integer + 1)) ) - def push(self, transaction: HashPoint[Transaction], resolver: HashResolver) -> 'BankState': + def push(self, transaction: HashPoint[Transaction]) -> 'BankState': assert isinstance(transaction, HashPoint) - assert isinstance(resolver, HashResolver) - transaction_resolved = resolver.resolve(transaction) + transaction_resolved = transaction.resolve() assert isinstance(transaction_resolved, Transaction) miner: Nullable[HashPoint[Subject]] = self.miner.reference @@ -103,21 +103,21 @@ class BankState(RecursiveMentionable, StaticMentionable): else: raise TypeError - assert transaction_resolved.verify(resolver, mint) - transaction_data: TransactionData = resolver.resolve(transaction_resolved.data) + assert transaction_resolved.verify(mint) + transaction_data: TransactionData = transaction_resolved.data.resolve() assert isinstance(transaction_data, TransactionData) minted: ActiveBinaryTree[Coin, Integer] = ActiveBinaryTree( - AVLBTBP(HashComparator(resolver, Fail())), self.minted + AVLBTBP(HashComparator(Fail())), self.minted ) assert isinstance(minted, ActiveBinaryTree) used: ActiveBinaryTree[Coin, Integer] = ActiveBinaryTree( - AVLBTBP(HashComparator(resolver, Fail())), self.used + AVLBTBP(HashComparator(Fail())), self.used ) assert isinstance(used, ActiveBinaryTree) in_coin: HashPoint[Coin] - for in_coin in transaction_data.iter_in_coins(resolver): + for in_coin in transaction_data.iter_in_coins(): assert isinstance(in_coin, HashPoint) assert isinstance(minted.query_tree().query(in_coin), NotNull) assert isinstance(used.query_tree().query(in_coin), Null) @@ -125,7 +125,7 @@ class BankState(RecursiveMentionable, StaticMentionable): assert isinstance(used, ActiveBinaryTree) coin: Coin - for coin, miner in transaction_resolved.iter_coins(resolver, mint, miner): + for coin, miner in transaction_resolved.iter_coins(mint, miner): assert isinstance(coin, Coin) assert isinstance(miner, Nullable) assert isinstance(minted.query_tree().query(HashPoint.of(coin)), Null) @@ -139,17 +139,16 @@ class BankState(RecursiveMentionable, StaticMentionable): self.length ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(' \ f'{tabulate(tab + 1)}bank' \ f'{tabulate(tab + 1)}(miner)' \ - f'{tabulate(tab + 1)}{self.miner.str(resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{self.miner.str(tab + 1)}' \ f'{tabulate(tab + 1)}(minted)' \ - f'{tabulate(tab + 1)}{self.minted.str(resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{self.minted.str(tab + 1)}' \ f'{tabulate(tab + 1)}(used)' \ - f'{tabulate(tab + 1)}{self.used.str(resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{self.used.str(tab + 1)}' \ f'{tabulate(tab + 1)}(length)' \ - f'{tabulate(tab + 1)}{resolver.resolve(self.length)}' \ + f'{tabulate(tab + 1)}{self.length.resolve()}' \ f'{tabulate(tab)})' diff --git a/rainbowadn/v13/signature.py b/rainbowadn/v13/signature.py index ac29b5c..5a78e27 100644 --- a/rainbowadn/v13/signature.py +++ b/rainbowadn/v13/signature.py @@ -28,7 +28,7 @@ class Signature(Atomic): ) @classmethod - def from_bytes(cls, source: bytes) -> 'Signature': + def _from_bytes(cls, source: bytes) -> 'Signature': assert isinstance(source, bytes) return cls(source) diff --git a/rainbowadn/v13/subject.py b/rainbowadn/v13/subject.py index cf05169..6968fbb 100644 --- a/rainbowadn/v13/subject.py +++ b/rainbowadn/v13/subject.py @@ -14,7 +14,7 @@ class Subject(Atomic): assert isinstance(self.public_key, PublicKey) @classmethod - def from_bytes(cls, source: bytes) -> 'Subject': + def _from_bytes(cls, source: bytes) -> 'Subject': assert isinstance(source, bytes) return cls(VerifyKey(source)) diff --git a/rainbowadn/v13/transaction.py b/rainbowadn/v13/transaction.py index 0223486..7614ab3 100644 --- a/rainbowadn/v13/transaction.py +++ b/rainbowadn/v13/transaction.py @@ -13,6 +13,7 @@ from rainbowadn.hashing.nullability.nullable import Nullable from rainbowadn.hashing.nullability.nullablereference import NullableReference, NullableReferenceFactory from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin from rainbowadn.hashing.static import StaticMentionable from rainbowadn.v13.signature import Signature from rainbowadn.v13.subject import Subject @@ -33,6 +34,8 @@ class CoinData(RecursiveMentionable, StaticMentionable): @classmethod def of(cls, owner: Subject, value: int) -> 'CoinData': + assert isinstance(owner, Subject) + assert isinstance(value, int) return cls(HashPoint.of(owner), HashPoint.of(Integer(value))) def points(self) -> Iterable[HashPoint]: @@ -42,18 +45,18 @@ class CoinData(RecursiveMentionable, StaticMentionable): return bytes(self.owner) + bytes(self.value) @classmethod - def from_bytes(cls, source: bytes) -> 'CoinData': + def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'CoinData': assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return cls( - HashPoint(Subject.factory(), source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(Integer.factory(), source[HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(Subject.factory(), source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin(Integer.factory(), source[HashPoint.HASH_LENGTH:], resolver).hash_point(), ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) - return f'{resolver.resolve(self.owner)}' \ - f'{tabulate(tab)}{resolver.resolve(self.value)}' + return f'{self.owner.resolve()}' \ + f'{tabulate(tab)}{self.value.resolve()}' class Coin(RecursiveMentionable, StaticMentionable): @@ -77,25 +80,27 @@ class Coin(RecursiveMentionable, StaticMentionable): return bytes(self.data) + bytes(self.origin) + bytes(self.index) @classmethod - def from_bytes(cls, source: bytes) -> 'Coin': + def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'Coin': assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return cls( - HashPoint(CoinData.factory(), source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(Transaction.factory(), source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH], Null()), - HashPoint(Integer.factory(), source[2 * HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(CoinData.factory(), source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin( + Transaction.factory(), source[HashPoint.HASH_LENGTH:2 * HashPoint.HASH_LENGTH], resolver + ).hash_point(), + ResolverOrigin(Integer.factory(), source[2 * HashPoint.HASH_LENGTH:], resolver).hash_point(), ) def __str__(self): return f'(coin)' - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(' \ f'{tabulate(tab + 1)}coin' \ - f'{tabulate(tab + 1)}{hash_point_format(self.data, resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{hash_point_format(self.data, tab + 1)}' \ f'{tabulate(tab + 1)}(origin)' \ - f'{tabulate(tab + 1)}{resolver.resolve(self.index)}' \ + f'{tabulate(tab + 1)}{self.index.resolve()}' \ f'{tabulate(tab)})' @@ -119,24 +124,23 @@ class TransactionData(RecursiveMentionable, StaticMentionable): return bytes(self.in_coins) + bytes(self.out_coins) @classmethod - def from_bytes(cls, source: bytes) -> 'TransactionData': + def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'TransactionData': assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return cls( NullableReferenceFactory( StackFactory(Coin.factory()).loose() - ).from_bytes(source[:HashPoint.HASH_LENGTH]), + ).from_bytes(source[:HashPoint.HASH_LENGTH], resolver), NullableReferenceFactory( StackFactory(CoinData.factory()).loose() - ).from_bytes(source[HashPoint.HASH_LENGTH:]), + ).from_bytes(source[HashPoint.HASH_LENGTH:], resolver), ) def _verify_signatures( self, - resolver: HashResolver, in_coins: NullableReference[Stack[Coin]], signatures: NullableReference[Stack[Signature]], ) -> bool: - assert isinstance(resolver, HashResolver) assert isinstance(in_coins, NullableReference) assert isinstance(signatures, NullableReference) if isinstance(in_coins.reference, Null): @@ -144,17 +148,17 @@ class TransactionData(RecursiveMentionable, StaticMentionable): return True elif isinstance(in_coins.reference, NotNull): assert isinstance(signatures.reference, NotNull) - in_coins_stack: Stack[Coin] = resolver.resolve(in_coins.reference.value) + in_coins_stack: Stack[Coin] = in_coins.reference.value.resolve() assert isinstance(in_coins_stack, Stack) - signatures_stack: Stack[Signature] = resolver.resolve(signatures.reference.value) + signatures_stack: Stack[Signature] = signatures.reference.value.resolve() assert isinstance(signatures_stack, Stack) - coin: Coin = resolver.resolve(in_coins_stack.element) + coin: Coin = in_coins_stack.element.resolve() assert isinstance(coin, Coin) - coin_data: CoinData = resolver.resolve(coin.data) + coin_data: CoinData = coin.data.resolve() assert isinstance(coin_data, CoinData) - owner: Subject = resolver.resolve(coin_data.owner) + owner: Subject = coin_data.owner.resolve() assert isinstance(owner, Subject) - signature: Signature = resolver.resolve(signatures_stack.element) + signature: Signature = signatures_stack.element.resolve() assert isinstance(signature, Signature) return signature.verify( owner, @@ -163,12 +167,11 @@ class TransactionData(RecursiveMentionable, StaticMentionable): else: raise TypeError - def iter_in_coins(self, resolver: HashResolver) -> Iterable[HashPoint[Coin]]: - assert isinstance(resolver, HashResolver) + def iter_in_coins(self) -> Iterable[HashPoint[Coin]]: in_coins: NullableReference[Stack[Coin]] = self.in_coins assert isinstance(in_coins, NullableReference) while isinstance(in_coins.reference, NotNull): - in_coins_stack: Stack[Coin] = resolver.resolve(in_coins.reference.value) + in_coins_stack: Stack[Coin] = in_coins.reference.value.resolve() assert isinstance(in_coins_stack, Stack) coin: HashPoint[Coin] = in_coins_stack.element assert isinstance(coin, HashPoint) @@ -177,27 +180,19 @@ class TransactionData(RecursiveMentionable, StaticMentionable): assert isinstance(in_coins, NullableReference) assert isinstance(in_coins.reference, Null) - def _total_in(self, resolver: HashResolver) -> int: - assert isinstance(resolver, HashResolver) + def _total_in(self) -> int: total_in = 0 coin: HashPoint[Coin] - for coin in self.iter_in_coins(resolver): + for coin in self.iter_in_coins(): assert isinstance(coin, HashPoint) - total_in += resolver.resolve( - resolver.resolve( - resolver.resolve( - coin - ).data - ).value - ).integer + total_in += coin.resolve().data.resolve().value.resolve().integer return total_in - def iter_out_coins(self, resolver: HashResolver) -> Iterable[HashPoint[CoinData]]: - assert isinstance(resolver, HashResolver) + def iter_out_coins(self) -> Iterable[HashPoint[CoinData]]: out_coins: NullableReference[Stack[CoinData]] = self.out_coins assert isinstance(out_coins, NullableReference) while isinstance(out_coins.reference, NotNull): - out_coins_stack: Stack[CoinData] = resolver.resolve(out_coins.reference.value) + out_coins_stack: Stack[CoinData] = out_coins.reference.value.resolve() assert isinstance(out_coins_stack, Stack) coin: HashPoint[CoinData] = out_coins_stack.element assert isinstance(coin, HashPoint) @@ -206,48 +201,38 @@ class TransactionData(RecursiveMentionable, StaticMentionable): assert isinstance(out_coins, NullableReference) assert isinstance(out_coins.reference, Null) - def _total_out(self, resolver: HashResolver) -> int: - assert isinstance(resolver, HashResolver) + def _total_out(self) -> int: total_out = 0 coin: HashPoint[CoinData] - for coin in self.iter_out_coins(resolver): + for coin in self.iter_out_coins(): assert isinstance(coin, HashPoint) - total_out += resolver.resolve( - resolver.resolve( - coin - ).value - ).integer + total_out += coin.resolve().value.resolve().integer return total_out - def _verify_values(self, resolver: HashResolver, mint: int) -> bool: - assert isinstance(resolver, HashResolver) + def _verify_values(self, mint: int) -> bool: assert isinstance(mint, int) - assert self._total_out(resolver) <= self._total_in(resolver) + mint + assert self._total_out() <= self._total_in() + mint return True - def extra(self, resolver: HashResolver, mint: int) -> int: - assert isinstance(resolver, HashResolver) + def extra(self, mint: int) -> int: assert isinstance(mint, int) - return self._total_in(resolver) + mint - self._total_out(resolver) + return self._total_in() + mint - self._total_out() def verify( self, - resolver: HashResolver, signatures: NullableReference[Stack[Signature]], mint: int ) -> bool: - assert isinstance(resolver, HashResolver) assert isinstance(signatures, NullableReference) assert isinstance(mint, int) - return self._verify_signatures(resolver, self.in_coins, signatures) and self._verify_values(resolver, mint) + return self._verify_signatures(self.in_coins, signatures) and self._verify_values(mint) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(in)' \ - f'{tabulate(tab)}{self.in_coins.str(resolver, tab)}' \ + f'{tabulate(tab)}{self.in_coins.str(tab)}' \ f'{tabulate(tab)}(out)' \ - f'{tabulate(tab)}{self.out_coins.str(resolver, tab)}' + f'{tabulate(tab)}{self.out_coins.str(tab)}' class Transaction(RecursiveMentionable, StaticMentionable): @@ -270,33 +255,31 @@ class Transaction(RecursiveMentionable, StaticMentionable): return bytes(self.data) + bytes(self.signatures) @classmethod - def from_bytes(cls, source: bytes) -> 'Transaction': + def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'Transaction': assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) signature_factory: RainbowFactory[Signature] = Signature.factory() assert isinstance(signature_factory, RainbowFactory) stack_factory: RainbowFactory[Stack[Signature]] = StackFactory(signature_factory).loose() assert isinstance(stack_factory, RainbowFactory) return cls( - HashPoint(TransactionData.factory(), source[:HashPoint.HASH_LENGTH], Null()), - NullableReferenceFactory(stack_factory).from_bytes(source[HashPoint.HASH_LENGTH:]), + ResolverOrigin(TransactionData.factory(), source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + NullableReferenceFactory(stack_factory).from_bytes(source[HashPoint.HASH_LENGTH:], resolver), ) def iter_coins( self, - resolver: HashResolver, mint: int, miner: Nullable[HashPoint[Subject]] ) -> Iterable[tuple[Coin, Nullable[HashPoint[Subject]]]]: - transaction_data = resolver.resolve(self.data) + transaction_data = self.data.resolve() assert isinstance(transaction_data, TransactionData) index = 0 out_coin: HashPoint[CoinData] - for out_coin in transaction_data.iter_out_coins(resolver): + for out_coin in transaction_data.iter_out_coins(): assert isinstance(out_coin, HashPoint) if isinstance(miner, Null): - miner = NotNull( - resolver.resolve(out_coin).owner - ) + miner = NotNull(out_coin.resolve().owner) assert isinstance(miner, Nullable) coin: Coin = Coin(out_coin, self.hash_point, HashPoint.of(Integer(index))) assert isinstance(coin, Coin) @@ -307,7 +290,7 @@ class Transaction(RecursiveMentionable, StaticMentionable): HashPoint.of( CoinData( miner.value, - HashPoint.of(Integer(transaction_data.extra(resolver, mint))) + HashPoint.of(Integer(transaction_data.extra(mint))) ) ), self.hash_point, @@ -318,29 +301,28 @@ class Transaction(RecursiveMentionable, StaticMentionable): def coins( self, - resolver: HashResolver, mint: int, miner: Nullable[HashPoint[Subject]] ) -> list[Coin]: - return [coin for coin, _ in self.iter_coins(resolver, mint, miner)] - - def verify(self, resolver: HashResolver, mint: int): - assert isinstance(resolver, HashResolver) assert isinstance(mint, int) - data: TransactionData = resolver.resolve(self.data) + assert isinstance(miner, Nullable) + return [coin for coin, _ in self.iter_coins(mint, miner)] + + def verify(self, mint: int): + assert isinstance(mint, int) + data: TransactionData = self.data.resolve() assert isinstance(data, TransactionData) - return data.verify(resolver, self.signatures, mint) + return data.verify(self.signatures, mint) def __str__(self): return f'(transaction)' - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(' \ f'{tabulate(tab + 1)}transaction' \ - f'{tabulate(tab + 1)}{hash_point_format(self.data, resolver, tab + 1)}' \ - f'{tabulate(tab + 1)}{self.signatures.str(resolver, tab + 1)}' \ + f'{tabulate(tab + 1)}{hash_point_format(self.data, tab + 1)}' \ + f'{tabulate(tab + 1)}{self.signatures.str(tab + 1)}' \ f'{tabulate(tab)})' @classmethod diff --git a/rainbowadn/wrisbt/weakreferenceindexsetbtree.py b/rainbowadn/wrisbt/weakreferenceindexsetbtree.py index 207a502..adaf709 100644 --- a/rainbowadn/wrisbt/weakreferenceindexsetbtree.py +++ b/rainbowadn/wrisbt/weakreferenceindexsetbtree.py @@ -4,16 +4,16 @@ from typing import Iterable, Sequence from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.notnull import NotNull -from rainbowadn.hashing.nullability.null import Null -from rainbowadn.hashing.nullability.nullable import Nullable +from rainbowadn.hashing.localmetaorigin import LocalMetaOrigin +from rainbowadn.hashing.localorigin import LocalOrigin +from rainbowadn.hashing.metaorigin import MetaOrigin from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolvermetaorigin import ResolverMetaOrigin +from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres __all__ = ('WeakReferenceIndexSetBTree', 'WrisbtFactory',) -from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres - class WeakReferenceIndexSetBTree(RecursiveMentionable): def __init__( @@ -22,7 +22,7 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): height: int, parametres: WrisbtParametres, root: bool, - cache: tuple[HashPoint['WeakReferenceIndexSetBTree'], ...] + cache: tuple[MetaOrigin['WeakReferenceIndexSetBTree'], ...] ): assert isinstance(source, bytes) assert isinstance(height, int) @@ -63,17 +63,13 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): assert len(cache) == self.children self.cache = cache - if self.balanced(): - self._hash_point = HashPoint.of(self) - - def hash_point(self) -> HashPoint['WeakReferenceIndexSetBTree']: - return self._hash_point - def full(self) -> bool: return self.keys == 2 * self.keymin + 1 def bytes_no(self, index: int, start: int, size: int) -> bytes: assert isinstance(index, int) + assert isinstance(start, int) + assert isinstance(size, int) assert 0 <= index < self.length return self.source[start + size * index:start + size * (index + 1)] @@ -82,25 +78,21 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): assert 0 <= index < self.keys return self.bytes_no(index, 0, self.keysize) - def cached_no(self, index: int) -> Nullable['WeakReferenceIndexSetBTree']: + def cached_no(self, index: int) -> MetaOrigin['WeakReferenceIndexSetBTree']: assert isinstance(index, int) assert 0 <= index < self.children assert not self.leaf - cached: Nullable[WeakReferenceIndexSetBTree] = self.cache[index] - assert isinstance(cached, Nullable) - if isinstance(cached, NotNull): - tree: WeakReferenceIndexSetBTree = cached.value - assert self.bytes_no(index, self.keyend, HashPoint.HASH_LENGTH) == tree.hash_point().point + cached: MetaOrigin[WeakReferenceIndexSetBTree] = self.cache[index] + assert isinstance(cached, MetaOrigin) return cached def child_no(self, index: int) -> HashPoint['WeakReferenceIndexSetBTree']: assert isinstance(index, int) assert 0 <= index < self.children assert not self.leaf - return HashPoint( + return self.cached_no(index).hash_point( WrisbtFactory(self.height - 1, self.parametres, False), - self.bytes_no(index, self.keyend, HashPoint.HASH_LENGTH), - self.cached_no(index) + self.bytes_no(index, self.keyend, HashPoint.HASH_LENGTH) ) def balanced(self) -> bool: @@ -159,8 +151,7 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): self.range(self.keymin + 1, 2 * self.keymin + 1), ) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) formatted = f'{self.height}' \ @@ -168,13 +159,12 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): for key_index in range(self.keys): formatted += f'{tabulate(tab + 1)}{self.key_no(key_index).hex()}' for child_index in range(self.children): - formatted += f'{tabulate(tab + 1)}{hash_point_format(self.child_no(child_index), resolver, tab + 1)}' + formatted += f'{tabulate(tab + 1)}{hash_point_format(self.child_no(child_index), tab + 1)}' return f'{formatted}' \ f'{tabulate(tab)})' - def contains(self, resolver: HashResolver, key: bytes) -> bool: - assert isinstance(resolver, HashResolver) + def contains(self, key: bytes) -> bool: assert isinstance(key, bytes) assert len(key) == self.keysize @@ -191,12 +181,11 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): assert key > self.key_no(index - 1) if self.leaf: return False - child: WeakReferenceIndexSetBTree = resolver.resolve(self.child_no(index)) + child: WeakReferenceIndexSetBTree = self.child_no(index).resolve() assert isinstance(child, WeakReferenceIndexSetBTree) - return child.contains(resolver, key) + return child.contains(key) - def add(self, resolver: HashResolver, key: bytes) -> 'WeakReferenceIndexSetBTree': - assert isinstance(resolver, HashResolver) + def add(self, key: bytes) -> 'WeakReferenceIndexSetBTree': assert isinstance(key, bytes) assert len(key) == self.keysize @@ -219,9 +208,9 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): self.root, () ) - child: WeakReferenceIndexSetBTree = resolver.resolve(self.child_no(index)) + child: WeakReferenceIndexSetBTree = self.child_no(index).resolve() assert isinstance(child, WeakReferenceIndexSetBTree) - child: WeakReferenceIndexSetBTree = child.add(resolver, key) + child: WeakReferenceIndexSetBTree = child.add(key) assert isinstance(child, WeakReferenceIndexSetBTree) if child.full(): left, middle, right = child.split() @@ -236,16 +225,22 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): + self.source[self.keysize * index:self.keyend + HashPoint.HASH_LENGTH * index] + - bytes(left.hash_point()) + bytes(HashPoint.of(left)) + - bytes(right.hash_point()) + bytes(HashPoint.of(right)) + self.source[self.keyend + HashPoint.HASH_LENGTH * (index + 1):] ), self.height, self.parametres, self.root, - self.cache[:index] + (NotNull(left), NotNull(right)) + self.cache[index + 1:] + ( + self.cache[:index] + + + (LocalMetaOrigin(LocalOrigin(left)), LocalMetaOrigin(LocalOrigin(right))) + + + self.cache[index + 1:] + ) ) else: return WeakReferenceIndexSetBTree( @@ -259,20 +254,26 @@ class WeakReferenceIndexSetBTree(RecursiveMentionable): self.height, self.parametres, self.root, - self.cache[:index] + (NotNull(child),) + self.cache[index + 1:] + ( + self.cache[:index] + + + (LocalMetaOrigin(LocalOrigin(child)),) + + + self.cache[index + 1:] + ) ) - def iter_keys(self, resolver: HashResolver) -> Iterable[bytes]: + def iter_keys(self) -> Iterable[bytes]: if self.leaf: for key_index in range(self.keys): yield self.key_no(key_index) else: - for index in range(self.length): + for index in range(self.keys * 2 + 1): real_index, mode = divmod(index, 2) if mode: yield self.key_no(real_index) else: - yield from resolver.resolve(self.child_no(real_index)).iter_keys(resolver) + yield from self.child_no(real_index).resolve().iter_keys() class KeyView(Sequence[WeakReferenceIndexSetBTree]): @@ -297,12 +298,15 @@ class WrisbtFactory(RainbowFactory[WeakReferenceIndexSetBTree]): self.parametres = parametres self.root = root - def from_bytes(self, source: bytes) -> WeakReferenceIndexSetBTree: + def from_bytes(self, source: bytes, resolver: HashResolver) -> WeakReferenceIndexSetBTree: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return WeakReferenceIndexSetBTree( source, self.height, self.parametres, self.root, - (Null(),) * (len(source) // (HashPoint.HASH_LENGTH + self.parametres.keysize) + 1) if self.height else () + ( + ResolverMetaOrigin(resolver), + ) * (len(source) // (HashPoint.HASH_LENGTH + self.parametres.keysize) + 1) if self.height else () ) diff --git a/rainbowadn/wrisbt/wrisbtchainprotocol.py b/rainbowadn/wrisbt/wrisbtchainprotocol.py index b02f292..5fe159e 100644 --- a/rainbowadn/wrisbt/wrisbtchainprotocol.py +++ b/rainbowadn/wrisbt/wrisbtchainprotocol.py @@ -2,7 +2,6 @@ from typing import Generic, TypeVar from rainbowadn.chain.blockchainprotocol import BlockChainProtocol from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.wrisbt.wrisbtindex import WrisbtIndex, WrisbtIndexFactory from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres @@ -24,7 +23,6 @@ class WrisbtChainProtocol( ): def __init__( self, - resolver: HashResolver, total_factory: RainbowFactory[TargetType], keymin: int ): @@ -33,7 +31,7 @@ class WrisbtChainProtocol( assert keymin >= 2 self.keymin = keymin super().__init__( - WrisbtProtocol(resolver, keymin), + WrisbtProtocol(keymin), total_factory, WrisbtIndexFactory(keymin) ) @@ -43,4 +41,5 @@ class WrisbtChainProtocol( return WrisbtRootFactory(WrisbtParametres(self.keymin, HashPoint.HASH_LENGTH)) def actual_state(self, state: WrisbtIndex) -> HashPoint[WrisbtRoot]: + assert isinstance(state, WrisbtIndex) return state.total diff --git a/rainbowadn/wrisbt/wrisbtindex.py b/rainbowadn/wrisbt/wrisbtindex.py index 0cecad1..620a64a 100644 --- a/rainbowadn/wrisbt/wrisbtindex.py +++ b/rainbowadn/wrisbt/wrisbtindex.py @@ -3,9 +3,9 @@ from typing import Iterable from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.null import Null from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres from rainbowadn.wrisbt.wrisbtroot import WrisbtRoot, WrisbtRootFactory @@ -36,12 +36,11 @@ class WrisbtIndex(RecursiveMentionable): def __factory__(self) -> RainbowFactory['WrisbtIndex']: return WrisbtIndexFactory(self.keymin) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(index)' \ - f'{tabulate(tab)}{hash_point_format(self.total, resolver, tab)}' \ - f'{tabulate(tab)}{hash_point_format(self.delta, resolver, tab)}' + f'{tabulate(tab)}{hash_point_format(self.total, tab)}' \ + f'{tabulate(tab)}{hash_point_format(self.delta, tab)}' class WrisbtIndexFactory(RainbowFactory[WrisbtIndex]): @@ -54,10 +53,11 @@ class WrisbtIndexFactory(RainbowFactory[WrisbtIndex]): ) assert isinstance(self.root_factory, RainbowFactory) - def from_bytes(self, source: bytes) -> WrisbtIndex: + def from_bytes(self, source: bytes, resolver: HashResolver) -> WrisbtIndex: assert isinstance(source, bytes) + assert isinstance(resolver, HashResolver) return WrisbtIndex( - HashPoint(self.root_factory, source[:HashPoint.HASH_LENGTH], Null()), - HashPoint(self.root_factory, source[HashPoint.HASH_LENGTH:], Null()), + ResolverOrigin(self.root_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(), + ResolverOrigin(self.root_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(), self.keymin ) diff --git a/rainbowadn/wrisbt/wrisbtprotocol.py b/rainbowadn/wrisbt/wrisbtprotocol.py index f4911bd..7140527 100644 --- a/rainbowadn/wrisbt/wrisbtprotocol.py +++ b/rainbowadn/wrisbt/wrisbtprotocol.py @@ -2,7 +2,6 @@ from typing import TypeVar from rainbowadn.chain.states.activestateprotocol import ActiveStateProtocol from rainbowadn.hashing.hashpoint import HashPoint -from rainbowadn.hashing.hashresolver import HashResolver from rainbowadn.wrisbt.wrisbtindex import WrisbtIndex from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres from rainbowadn.wrisbt.wrisbtroot import WrisbtRoot @@ -13,12 +12,10 @@ TargetType = TypeVar('TargetType') class WrisbtProtocol(ActiveStateProtocol[TargetType, WrisbtIndex]): - def __init__(self, resolver: HashResolver, keymin: int): - assert isinstance(resolver, HashResolver) + def __init__(self, keymin: int): assert isinstance(keymin, int) assert keymin >= 2 self.keymin = keymin - super().__init__(resolver) def _initial_state(self) -> HashPoint[WrisbtIndex]: return HashPoint.of( @@ -36,18 +33,18 @@ class WrisbtProtocol(ActiveStateProtocol[TargetType, WrisbtIndex]): ) -> HashPoint[WrisbtIndex]: assert isinstance(previous, HashPoint) assert isinstance(header, HashPoint) - index = self.resolver.resolve(previous) + index: WrisbtIndex = previous.resolve() assert isinstance(index, WrisbtIndex) empty: WrisbtRoot = WrisbtRoot.empty(WrisbtParametres(self.keymin, HashPoint.HASH_LENGTH)) assert isinstance(empty, WrisbtRoot) - total: WrisbtRoot = self.resolver.resolve(index.total) + total: WrisbtRoot = index.total.resolve() assert isinstance(total, WrisbtRoot) return HashPoint.of( WrisbtIndex( - HashPoint.of(total.index(self.resolver, header, empty)), - HashPoint.of(empty.index(self.resolver, header, total)), + HashPoint.of(total.index(header, empty)), + HashPoint.of(empty.index(header, total)), index.keymin ) ) diff --git a/rainbowadn/wrisbt/wrisbtroot.py b/rainbowadn/wrisbt/wrisbtroot.py index 678bfe8..16c79d3 100644 --- a/rainbowadn/wrisbt/wrisbtroot.py +++ b/rainbowadn/wrisbt/wrisbtroot.py @@ -5,16 +5,16 @@ from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate from rainbowadn.hashing.hashmentionable import HashMentionable from rainbowadn.hashing.hashpoint import HashPoint from rainbowadn.hashing.hashresolver import HashResolver -from rainbowadn.hashing.nullability.notnull import NotNull -from rainbowadn.hashing.nullability.null import Null +from rainbowadn.hashing.localmetaorigin import LocalMetaOrigin +from rainbowadn.hashing.localorigin import LocalOrigin from rainbowadn.hashing.rainbow_factory import RainbowFactory from rainbowadn.hashing.recursivementionable import RecursiveMentionable +from rainbowadn.hashing.resolverorigin import ResolverOrigin from rainbowadn.wrisbt.weakreferenceindexsetbtree import WeakReferenceIndexSetBTree, WrisbtFactory +from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres __all__ = ('WrisbtRoot', 'WrisbtRootFactory',) -from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres - class WrisbtRoot(RecursiveMentionable): def __init__(self, root: HashPoint[WeakReferenceIndexSetBTree], height: int, parametres: WrisbtParametres): @@ -39,29 +39,26 @@ class WrisbtRoot(RecursiveMentionable): assert isinstance(parametres, WrisbtParametres) return WrisbtRoot(HashPoint.of(WeakReferenceIndexSetBTree(b'', 0, parametres, True, ())), 0, parametres) - def str(self, resolver: HashResolver, tab: int) -> str: - assert isinstance(resolver, HashResolver) + def str(self, tab: int) -> str: assert isinstance(tab, int) return f'(root)' \ f'{tabulate(tab)}{self.height}' \ - f'{tabulate(tab)}{hash_point_format(self.root, resolver, tab)}' + f'{tabulate(tab)}{hash_point_format(self.root, tab)}' - def contains(self, resolver: HashResolver, key: bytes) -> bool: - assert isinstance(resolver, HashResolver) + def contains(self, key: bytes) -> bool: assert isinstance(key, bytes) assert len(key) == self.parametres.keysize - root: WeakReferenceIndexSetBTree = resolver.resolve(self.root) + root: WeakReferenceIndexSetBTree = self.root.resolve() assert isinstance(root, WeakReferenceIndexSetBTree) - return root.contains(resolver, key) + return root.contains(key) - def add(self, resolver: HashResolver, key: bytes) -> 'WrisbtRoot': - assert isinstance(resolver, HashResolver) + def add(self, key: bytes) -> 'WrisbtRoot': assert isinstance(key, bytes) assert len(key) == self.parametres.keysize - root: WeakReferenceIndexSetBTree = resolver.resolve(self.root).add(resolver, key) + root: WeakReferenceIndexSetBTree = self.root.resolve().add(key) assert isinstance(root, WeakReferenceIndexSetBTree) if root.full(): @@ -74,7 +71,7 @@ class WrisbtRoot(RecursiveMentionable): root.height + 1, root.parametres, True, - (NotNull(left), NotNull(right)) + (LocalMetaOrigin(LocalOrigin(left)), LocalMetaOrigin(LocalOrigin(right))) ) assert isinstance(root, WeakReferenceIndexSetBTree) return self.of(root) @@ -84,28 +81,27 @@ class WrisbtRoot(RecursiveMentionable): assert isinstance(root, WeakReferenceIndexSetBTree) return cls(HashPoint.of(root), root.height, root.parametres) - def keys(self, resolver: HashResolver) -> list[bytes]: - return list(resolver.resolve(self.root).iter_keys(resolver)) + def keys(self) -> list[bytes]: + return list(self.root.resolve().iter_keys()) def index( - self, resolver: HashResolver, target: HashPoint, exclude: Optional['WrisbtRoot'] + self, target: HashPoint, exclude: Optional['WrisbtRoot'] ) -> 'WrisbtRoot': - assert isinstance(resolver, HashResolver) assert isinstance(target, HashPoint) if exclude is None: exclude = self.empty(self.parametres) assert isinstance(exclude, WrisbtRoot) key: bytes = target.point assert isinstance(key, bytes) - if exclude.contains(resolver, key) or self.contains(resolver, key): + if exclude.contains(key) or self.contains(key): return self tree = self - value: HashMentionable = resolver.resolve(target) + value: HashMentionable = target.resolve() assert isinstance(value, HashMentionable) if isinstance(value, RecursiveMentionable): for hash_point in value.points(): - tree = tree.index(resolver, hash_point, exclude) - tree = tree.add(resolver, key) + tree = tree.index(hash_point, exclude) + tree = tree.add(key) return tree @@ -114,12 +110,15 @@ class WrisbtRootFactory(RainbowFactory[WrisbtRoot]): assert isinstance(parametres, WrisbtParametres) self.parametres = parametres - def from_bytes(self, source: bytes) -> WrisbtRoot: + def from_bytes(self, source: bytes, resolver: HashResolver) -> WrisbtRoot: assert isinstance(source, bytes) - height: int = Integer.from_bytes(source[HashPoint.HASH_LENGTH:]).integer + assert isinstance(resolver, HashResolver) + height: int = Integer.from_bytes(source[HashPoint.HASH_LENGTH:], resolver).integer assert isinstance(height, int) return WrisbtRoot( - HashPoint(WrisbtFactory(height, self.parametres, True), source[:HashPoint.HASH_LENGTH], Null()), + ResolverOrigin( + WrisbtFactory(height, self.parametres, True), source[:HashPoint.HASH_LENGTH], resolver + ).hash_point(), height, self.parametres )