remove chain package
This commit is contained in:
parent
8f43c157c1
commit
31284ca4b4
@ -1,13 +0,0 @@
|
||||
__all__ = (
|
||||
'BlockChain', 'BlockChainFactory',
|
||||
'BlockChainProtocol',
|
||||
'ChainCollectionFactory',
|
||||
'ChainCollectionInterface',
|
||||
'ReductionChainMetaFactory',
|
||||
)
|
||||
|
||||
from .blockchain import BlockChain, BlockChainFactory
|
||||
from .blockchainprotocol import BlockChainProtocol
|
||||
from .chaincollectionfactory import ChainCollectionFactory
|
||||
from .chaincollectioninterface import ChainCollectionInterface
|
||||
from .reductionchainmetafactory import ReductionChainMetaFactory
|
@ -1,80 +0,0 @@
|
||||
from typing import Generic, Iterable, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
|
||||
__all__ = ('Block', 'BlockFactory',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class Block(RecursiveMentionable, Generic[HeaderType, StateType]):
|
||||
def __init__(
|
||||
self,
|
||||
previous: NullableReference['Block[HeaderType, StateType]'],
|
||||
header: HashPoint[HeaderType],
|
||||
state: HashPoint[StateType],
|
||||
):
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
self.previous = previous
|
||||
self.header = header
|
||||
self.state = state
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [*self.previous.points(), self.header, self.state]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.previous) + bytes(self.header) + bytes(self.state)
|
||||
|
||||
def __factory__(self) -> RainbowFactory['Block[HeaderType, StateType]']:
|
||||
return BlockFactory(
|
||||
self.header.factory,
|
||||
self.state.factory,
|
||||
)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
previous_str, header_str, state_str = await gather(
|
||||
self.previous.str(tab),
|
||||
hash_point_format(self.header, tab + 1),
|
||||
hash_point_format(self.state, tab + 1),
|
||||
)
|
||||
assert isinstance(previous_str, str)
|
||||
assert isinstance(header_str, str)
|
||||
assert isinstance(state_str, str)
|
||||
return f'{previous_str}' \
|
||||
f'{tabulate(tab)}(' \
|
||||
f'{tabulate(tab + 1)}block' \
|
||||
f'{tabulate(tab + 1)}(header)' \
|
||||
f'{tabulate(tab + 1)}{header_str}' \
|
||||
f'{tabulate(tab + 1)}(state)' \
|
||||
f'{tabulate(tab + 1)}{state_str}' \
|
||||
f'{tabulate(tab)})'
|
||||
|
||||
|
||||
class BlockFactory(RainbowFactory[Block[HeaderType, StateType]], Generic[HeaderType, StateType]):
|
||||
def __init__(
|
||||
self,
|
||||
header_factory: RainbowFactory[HeaderType],
|
||||
state_factory: RainbowFactory[StateType],
|
||||
):
|
||||
assert isinstance(header_factory, RainbowFactory)
|
||||
assert isinstance(state_factory, RainbowFactory)
|
||||
self.header_factory = header_factory
|
||||
self.state_factory = state_factory
|
||||
|
||||
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], 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(),
|
||||
)
|
@ -1,213 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .block import *
|
||||
from .blockchainprotocol import *
|
||||
from .chaincollectionfactory import *
|
||||
from .chaincollectioninterface import *
|
||||
|
||||
__all__ = ('BlockChain', 'BlockChainFactory',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class BlockChain(
|
||||
ChainCollectionInterface[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
],
|
||||
Generic[HeaderType, StateType],
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
reference: NullableReference[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
]
|
||||
],
|
||||
protocol: BlockChainProtocol[HeaderType, StateType],
|
||||
):
|
||||
assert isinstance(reference, NullableReference)
|
||||
assert isinstance(protocol, BlockChainProtocol)
|
||||
super().__init__(reference)
|
||||
self.protocol = protocol
|
||||
|
||||
def factory(self) -> ChainCollectionFactory[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
]:
|
||||
return BlockChainFactory(self.protocol)
|
||||
|
||||
async def add(
|
||||
self,
|
||||
header: HashPoint[HeaderType]
|
||||
) -> ChainCollectionInterface[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
]:
|
||||
assert isinstance(header, HashPoint)
|
||||
return self.factory().from_reference(
|
||||
NullableReference.off(
|
||||
await self._add(header)
|
||||
)
|
||||
)
|
||||
|
||||
async def state(
|
||||
self
|
||||
) -> NullableReference[
|
||||
StateType
|
||||
]:
|
||||
if self.reference.null():
|
||||
return NullableReference(
|
||||
Null(),
|
||||
self.protocol.state_factory
|
||||
)
|
||||
else:
|
||||
block: Block[HeaderType, StateType] = await self.reference.resolve()
|
||||
assert isinstance(block, Block)
|
||||
return NullableReference.of(block.state)
|
||||
|
||||
async def _add(
|
||||
self,
|
||||
header: HashPoint[HeaderType]
|
||||
) -> Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
]:
|
||||
assert isinstance(header, HashPoint)
|
||||
return Block(
|
||||
self.reference,
|
||||
header,
|
||||
await self.protocol.state_protocol.derive(
|
||||
await self.state(),
|
||||
header,
|
||||
)
|
||||
)
|
||||
|
||||
async def previous(
|
||||
self
|
||||
) -> ChainCollectionInterface[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
]:
|
||||
assert not self.reference.null()
|
||||
block: Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
] = await self.reference.resolve()
|
||||
assert isinstance(block, Block)
|
||||
return self.factory().from_reference(block.previous)
|
||||
|
||||
async def verify(self) -> bool:
|
||||
if self.reference.null():
|
||||
return True
|
||||
else:
|
||||
previous: ChainCollectionInterface[
|
||||
Block[HeaderType, StateType], HeaderType
|
||||
] = await self.previous()
|
||||
assert isinstance(previous, ChainCollectionInterface)
|
||||
assert_trues(
|
||||
await gather(
|
||||
self.verify_link(previous.reference),
|
||||
previous.verify(),
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
async def _verify_link(
|
||||
self,
|
||||
block: Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
previous: NullableReference[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
]
|
||||
]
|
||||
) -> bool:
|
||||
assert isinstance(block, Block)
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert_eq(block.previous, previous)
|
||||
previous_chain = self.factory().from_reference(previous)
|
||||
assert isinstance(previous_chain, BlockChain)
|
||||
assert_true(
|
||||
await self.protocol.state_protocol.verify(
|
||||
await previous_chain.state(),
|
||||
block.header,
|
||||
block.state,
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
def loose(self) -> ChainCollectionInterface[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
]:
|
||||
return self
|
||||
|
||||
|
||||
class BlockChainFactory(
|
||||
ChainCollectionFactory[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
HeaderType
|
||||
],
|
||||
Generic[HeaderType, StateType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BlockChainProtocol[HeaderType, StateType],
|
||||
):
|
||||
assert isinstance(protocol, BlockChainProtocol)
|
||||
self.protocol = protocol
|
||||
|
||||
def empty(self) -> BlockChain[HeaderType, StateType]:
|
||||
return BlockChain(
|
||||
NullableReference(
|
||||
Null(),
|
||||
BlockFactory(
|
||||
self.protocol.header_factory,
|
||||
self.protocol.state_factory
|
||||
)
|
||||
),
|
||||
self.protocol
|
||||
)
|
||||
|
||||
def from_reference(
|
||||
self,
|
||||
reference: NullableReference[
|
||||
Block[
|
||||
HeaderType,
|
||||
StateType
|
||||
],
|
||||
]
|
||||
) -> BlockChain[
|
||||
HeaderType, StateType
|
||||
]:
|
||||
assert isinstance(reference, NullableReference)
|
||||
return BlockChain(
|
||||
reference,
|
||||
self.protocol
|
||||
)
|
@ -1,29 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .states import *
|
||||
|
||||
__all__ = ('BlockChainProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class BlockChainProtocol(
|
||||
Generic[HeaderType, StateType],
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: ActiveStateProtocol[HeaderType, StateType],
|
||||
header_factory: RainbowFactory[StateType],
|
||||
state_factory: RainbowFactory[StateType]
|
||||
):
|
||||
assert isinstance(protocol, ActiveStateProtocol)
|
||||
assert isinstance(header_factory, RainbowFactory)
|
||||
assert isinstance(state_factory, RainbowFactory)
|
||||
self.state_protocol = protocol
|
||||
self.header_factory = header_factory
|
||||
self.state_factory = state_factory
|
||||
|
||||
def loose(self) -> 'BlockChainProtocol[HeaderType, StateType]':
|
||||
return self
|
@ -1,50 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.collection.collectioninterface import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
|
||||
__all__ = ('BlockCollectionInterface',)
|
||||
|
||||
BlockType = TypeVar('BlockType')
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
|
||||
|
||||
class BlockCollectionInterface(
|
||||
CollectionInterface[
|
||||
BlockType
|
||||
],
|
||||
Generic[BlockType, HeaderType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
reference: NullableReference[BlockType]
|
||||
):
|
||||
assert isinstance(reference, NullableReference)
|
||||
super().__init__(reference)
|
||||
|
||||
async def _verify_link(
|
||||
self,
|
||||
block: BlockType,
|
||||
previous: NullableReference[BlockType]
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
async def verify_link(
|
||||
self,
|
||||
previous: NullableReference[BlockType]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
if self.reference.null():
|
||||
return True
|
||||
else:
|
||||
return await self._verify_link(
|
||||
await self.reference.resolve(),
|
||||
previous
|
||||
)
|
||||
|
||||
async def add(
|
||||
self,
|
||||
header: HashPoint[HeaderType]
|
||||
) -> 'BlockCollectionInterface[BlockType, HeaderType]':
|
||||
raise NotImplementedError
|
@ -1,23 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.nullability import *
|
||||
from .chaincollectioninterface import *
|
||||
|
||||
__all__ = ('ChainCollectionFactory',)
|
||||
|
||||
BlockType = TypeVar('BlockType')
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
|
||||
|
||||
class ChainCollectionFactory(Generic[BlockType, HeaderType]):
|
||||
def empty(self) -> ChainCollectionInterface[BlockType, HeaderType]:
|
||||
raise NotImplementedError
|
||||
|
||||
def from_reference(
|
||||
self,
|
||||
reference: NullableReference[BlockType]
|
||||
) -> ChainCollectionInterface[BlockType, HeaderType]:
|
||||
raise NotImplementedError
|
||||
|
||||
def loose(self) -> 'ChainCollectionFactory[BlockType, HeaderType]':
|
||||
return self
|
@ -1,28 +0,0 @@
|
||||
import abc
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .blockcollectioninterface import *
|
||||
|
||||
__all__ = ('ChainCollectionInterface',)
|
||||
|
||||
BlockType = TypeVar('BlockType')
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
|
||||
|
||||
class ChainCollectionInterface(
|
||||
BlockCollectionInterface[
|
||||
BlockType,
|
||||
HeaderType
|
||||
],
|
||||
Generic[BlockType, HeaderType],
|
||||
abc.ABC
|
||||
):
|
||||
async def add(
|
||||
self,
|
||||
header: HashPoint[HeaderType]
|
||||
) -> 'ChainCollectionInterface[BlockType, HeaderType]':
|
||||
raise NotImplementedError
|
||||
|
||||
async def verify(self) -> bool:
|
||||
raise NotImplementedError
|
@ -1,13 +0,0 @@
|
||||
__all__ = (
|
||||
'ActiveStageProtocol',
|
||||
'ActiveStageStateProtocol',
|
||||
'Derived',
|
||||
'DerivedStage',
|
||||
'DerivedState',
|
||||
)
|
||||
|
||||
from .activestageprotocol import ActiveStageProtocol
|
||||
from .activestagestateprotocol import ActiveStageStateProtocol
|
||||
from .derived import Derived
|
||||
from .derivedstage import DerivedStage
|
||||
from .derivedstate import DerivedState
|
@ -1,151 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.stages import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .derived import *
|
||||
from .derivedstage import *
|
||||
from .derivedstate import *
|
||||
|
||||
__all__ = ('ActiveStageProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class ActiveStageProtocol(
|
||||
StageProtocol[HeaderType, BaseStateType, StageType],
|
||||
Generic[HeaderType, BaseStateType, StageType]
|
||||
):
|
||||
async def derive_header(
|
||||
self,
|
||||
previous: NullableReference[BaseStateType],
|
||||
header: HashPoint[HeaderType],
|
||||
) -> HashPoint[StageType]:
|
||||
raise NotImplementedError
|
||||
|
||||
async def derive_stage_or_state(
|
||||
self,
|
||||
stage: HashPoint[StageType],
|
||||
) -> Derived[BaseStateType, StageType]:
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
async def _previous_base_state(
|
||||
cls,
|
||||
previous_reference: NullableReference[StateStage[HeaderType, BaseStateType, StageType]],
|
||||
base_state_factory: RainbowFactory[BaseStateType],
|
||||
) -> NullableReference[BaseStateType]:
|
||||
assert isinstance(previous_reference, NullableReference)
|
||||
assert isinstance(base_state_factory, RainbowFactory)
|
||||
if previous_reference.null():
|
||||
return NullableReference(Null(), base_state_factory)
|
||||
else:
|
||||
previous_state_stage: StateStage[HeaderType, BaseStateType, StageType] = await previous_reference.resolve()
|
||||
assert isinstance(previous_state_stage, StateStage)
|
||||
return NullableReference.of(previous_state_stage.state)
|
||||
|
||||
async def _derive_cycle(
|
||||
self,
|
||||
stage: StageStage[HeaderType, BaseStateType, StageType],
|
||||
stage_factory: RainbowFactory[StageType]
|
||||
) -> HashPoint[StateStage[HeaderType, BaseStateType, StageType]]:
|
||||
assert isinstance(stage, StageStage)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
while True:
|
||||
derived: Derived[BaseStateType, StageType] = await self.derive_stage_or_state(stage.stage)
|
||||
assert isinstance(derived, Derived)
|
||||
if isinstance(derived, DerivedStage):
|
||||
stage = StageStage(self, NullableReference.off(stage), derived.stage)
|
||||
assert isinstance(stage, StageStage)
|
||||
elif isinstance(derived, DerivedState):
|
||||
return HashPoint.of(StateStage(self, HashPoint.of(stage), derived.state, stage_factory))
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
async def _derive_initial_stage(
|
||||
self,
|
||||
previous: NullableReference[BaseStateType],
|
||||
header: HashPoint[HeaderType],
|
||||
stage_factory: RainbowFactory[StageType],
|
||||
) -> StageStage[HeaderType, BaseStateType, StageType]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
return StageStage(
|
||||
self,
|
||||
NullableReference(
|
||||
Null(),
|
||||
StageStageFactory(
|
||||
self,
|
||||
stage_factory
|
||||
)
|
||||
),
|
||||
await self.derive_header(previous, header)
|
||||
)
|
||||
|
||||
async def derive_full(
|
||||
self,
|
||||
previous_reference: NullableReference[StateStage[HeaderType, BaseStateType, StageType]],
|
||||
header: HashPoint[HeaderType],
|
||||
base_state_factory: RainbowFactory[BaseStateType],
|
||||
stage_factory: RainbowFactory[StageType],
|
||||
) -> HashPoint[StateStage[HeaderType, BaseStateType, StageType]]:
|
||||
assert isinstance(previous_reference, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(base_state_factory, RainbowFactory)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
return await self._derive_cycle(
|
||||
await self._derive_initial_stage(
|
||||
await self._previous_base_state(
|
||||
previous_reference,
|
||||
base_state_factory
|
||||
),
|
||||
header,
|
||||
stage_factory
|
||||
),
|
||||
stage_factory
|
||||
)
|
||||
|
||||
async def verify_header(
|
||||
self,
|
||||
previous: NullableReference[BaseStateType],
|
||||
header: HashPoint[HeaderType],
|
||||
stage: HashPoint[StageType]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(stage, HashPoint)
|
||||
assert_eq(stage, await self.derive_header(previous, header))
|
||||
return True
|
||||
|
||||
async def verify_stage(
|
||||
self,
|
||||
previous: HashPoint[StageType],
|
||||
stage: HashPoint[StageType]
|
||||
) -> bool:
|
||||
assert isinstance(previous, HashPoint)
|
||||
assert isinstance(stage, HashPoint)
|
||||
derived: Derived[BaseStateType, StageType] = await self.derive_stage_or_state(previous)
|
||||
assert isinstance(derived, Derived)
|
||||
if isinstance(derived, DerivedStage):
|
||||
assert_eq(stage, derived.stage)
|
||||
return True
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
async def verify_state(
|
||||
self,
|
||||
stage: HashPoint[StageType],
|
||||
state: HashPoint[BaseStateType]
|
||||
) -> bool:
|
||||
assert isinstance(stage, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
derived: Derived[BaseStateType, StageType] = await self.derive_stage_or_state(stage)
|
||||
assert isinstance(derived, Derived)
|
||||
if isinstance(derived, DerivedState):
|
||||
assert_eq(state, derived.state)
|
||||
return True
|
||||
else:
|
||||
raise TypeError
|
@ -1,97 +0,0 @@
|
||||
from typing import TypeVar
|
||||
|
||||
from rainbowadn.chain.stages import *
|
||||
from rainbowadn.chain.states import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .activestageprotocol import *
|
||||
|
||||
__all__ = ('ActiveStageStateProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class ActiveStageStateProtocol(
|
||||
ActiveStateProtocol[
|
||||
HeaderType,
|
||||
StateStage[
|
||||
HeaderType,
|
||||
BaseStateType,
|
||||
StageType
|
||||
]
|
||||
]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: ActiveStageProtocol,
|
||||
stage_factory: RainbowFactory[StageType],
|
||||
base_state_factory: RainbowFactory[BaseStateType],
|
||||
):
|
||||
assert isinstance(protocol, ActiveStageProtocol)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
assert isinstance(base_state_factory, RainbowFactory)
|
||||
self.protocol = protocol
|
||||
self.stage_factory = stage_factory
|
||||
self.base_state_factory = base_state_factory
|
||||
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference[
|
||||
StateStage[
|
||||
HeaderType,
|
||||
BaseStateType,
|
||||
StageType
|
||||
]
|
||||
],
|
||||
header: HashPoint[HeaderType],
|
||||
state: HashPoint[
|
||||
StateStage[
|
||||
HeaderType,
|
||||
BaseStateType,
|
||||
StageType
|
||||
]
|
||||
]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
return await self.stage_state_protocol().verify(
|
||||
previous,
|
||||
header,
|
||||
state
|
||||
)
|
||||
|
||||
async def derive(
|
||||
self,
|
||||
previous: NullableReference[
|
||||
StateStage[
|
||||
HeaderType,
|
||||
BaseStateType,
|
||||
StageType
|
||||
]
|
||||
],
|
||||
header: HashPoint[HeaderType],
|
||||
) -> HashPoint[
|
||||
StateStage[
|
||||
HeaderType,
|
||||
BaseStateType,
|
||||
StageType
|
||||
]
|
||||
]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
return await self.protocol.derive_full(
|
||||
previous,
|
||||
header,
|
||||
self.base_state_factory,
|
||||
self.stage_factory
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def stage_state_protocol(cls) -> StateProtocol[
|
||||
HeaderType,
|
||||
StateStage[HeaderType, BaseStateType, StageType]
|
||||
]:
|
||||
return StageStateProtocol()
|
@ -1,10 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
__all__ = ('Derived',)
|
||||
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class Derived(Generic[BaseStateType, StageType]):
|
||||
pass
|
@ -1,15 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .derived import *
|
||||
|
||||
__all__ = ('DerivedStage',)
|
||||
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class DerivedStage(Derived[BaseStateType, StageType], Generic[BaseStateType, StageType]):
|
||||
def __init__(self, stage: HashPoint[StageType]):
|
||||
assert isinstance(stage, HashPoint)
|
||||
self.stage = stage
|
@ -1,15 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .derived import *
|
||||
|
||||
__all__ = ('DerivedState',)
|
||||
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class DerivedState(Derived[BaseStateType, StageType], Generic[BaseStateType, StageType]):
|
||||
def __init__(self, state: HashPoint[BaseStateType]):
|
||||
assert isinstance(state, HashPoint)
|
||||
self.state = state
|
@ -1,15 +0,0 @@
|
||||
__all__ = (
|
||||
'AbstractReductionChainMetaFactory',
|
||||
'Reduced',
|
||||
'Reducible', 'ReducibleFactory',
|
||||
'ReductionChainProtocol',
|
||||
'ReductionProtocol',
|
||||
'ReductionResult',
|
||||
)
|
||||
|
||||
from .abstractreductionchainmetafactory import AbstractReductionChainMetaFactory
|
||||
from .reduced import Reduced
|
||||
from .reducible import Reducible, ReducibleFactory
|
||||
from .reductionchainprotocol import ReductionChainProtocol
|
||||
from .reductionprotocol import ReductionProtocol
|
||||
from .reductionresult import ReductionResult
|
@ -1,25 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.chaincollectionfactory import *
|
||||
from rainbowadn.core import *
|
||||
from .reductionprotocol import *
|
||||
|
||||
__all__ = ('AbstractReductionChainMetaFactory',)
|
||||
|
||||
BlockType = TypeVar('BlockType')
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class AbstractReductionChainMetaFactory(
|
||||
Generic[BlockType, ReductorType, AccumulatorType]
|
||||
):
|
||||
def factory(
|
||||
self,
|
||||
reductor_factory: RainbowFactory[ReductorType],
|
||||
accumulator_factory: RainbowFactory[AccumulatorType],
|
||||
protocol: ReductionProtocol[ReductorType, AccumulatorType],
|
||||
) -> ChainCollectionFactory[
|
||||
BlockType, ReductorType
|
||||
]:
|
||||
raise NotImplementedError
|
@ -1,15 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .reductionresult import *
|
||||
|
||||
__all__ = ('Reduced',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class Reduced(ReductionResult, Generic[ReductorType, AccumulatorType]):
|
||||
def __init__(self, reduced: HashPoint[AccumulatorType]):
|
||||
assert isinstance(reduced, HashPoint)
|
||||
self.reduced = reduced
|
@ -1,58 +0,0 @@
|
||||
from typing import Generic, Iterable, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .reductionresult import *
|
||||
|
||||
__all__ = ('Reducible', 'ReducibleFactory',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class Reducible(
|
||||
ReductionResult[ReductorType, AccumulatorType],
|
||||
RecursiveMentionable,
|
||||
Generic[ReductorType, AccumulatorType]
|
||||
):
|
||||
def __init__(self, reductor: HashPoint[ReductorType], accumulator: HashPoint[AccumulatorType]):
|
||||
assert isinstance(reductor, HashPoint)
|
||||
assert isinstance(accumulator, HashPoint)
|
||||
self.reductor = reductor
|
||||
self.accumulator = accumulator
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [self.reductor, self.accumulator]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.reductor) + bytes(self.accumulator)
|
||||
|
||||
def __factory__(self) -> RainbowFactory['Reducible[ReductorType, AccumulatorType]']:
|
||||
return ReducibleFactory(self.reductor.factory, self.accumulator.factory)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
return f'(reduction)' \
|
||||
f'{tabulate(tab)}{await hash_point_format(self.accumulator, tab)}'
|
||||
|
||||
|
||||
class ReducibleFactory(
|
||||
RainbowFactory[Reducible[ReductorType, AccumulatorType]],
|
||||
Generic[ReductorType, AccumulatorType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
reductor_factory: RainbowFactory[ReductorType],
|
||||
accumulator_factory: RainbowFactory[AccumulatorType],
|
||||
):
|
||||
assert isinstance(reductor_factory, RainbowFactory)
|
||||
assert isinstance(accumulator_factory, RainbowFactory)
|
||||
self.reductor_factory = reductor_factory
|
||||
self.accumulator_factory = accumulator_factory
|
||||
|
||||
def from_bytes(self, source: bytes, resolver: HashResolver) -> Reducible[ReductorType, AccumulatorType]:
|
||||
assert isinstance(source, bytes)
|
||||
assert isinstance(resolver, HashResolver)
|
||||
return Reducible(
|
||||
ResolverOrigin(self.reductor_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(),
|
||||
ResolverOrigin(self.accumulator_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(),
|
||||
)
|
@ -1,61 +0,0 @@
|
||||
from typing import TypeVar
|
||||
|
||||
from rainbowadn.chain.blockchainprotocol import *
|
||||
from rainbowadn.chain.derivation import *
|
||||
from rainbowadn.chain.stages import *
|
||||
from rainbowadn.core import *
|
||||
from .reducible import *
|
||||
from .reductionprotocol import *
|
||||
from .reductionstageprotocol import *
|
||||
|
||||
__all__ = ('ReductionChainProtocol',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class ReductionChainProtocol(
|
||||
BlockChainProtocol[
|
||||
ReductorType,
|
||||
StateStage[ReductorType, AccumulatorType, Reducible[ReductorType, AccumulatorType]]
|
||||
]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: ReductionProtocol[ReductorType, AccumulatorType],
|
||||
reductor_factory: RainbowFactory[ReductorType],
|
||||
accumulator_factory: RainbowFactory[AccumulatorType],
|
||||
):
|
||||
assert isinstance(protocol, ReductionProtocol)
|
||||
assert isinstance(reductor_factory, RainbowFactory)
|
||||
assert isinstance(accumulator_factory, RainbowFactory)
|
||||
|
||||
reducible_factory: RainbowFactory[
|
||||
Reducible[ReductorType, AccumulatorType]
|
||||
] = ReducibleFactory(
|
||||
reductor_factory, accumulator_factory
|
||||
)
|
||||
assert isinstance(reducible_factory, RainbowFactory)
|
||||
stage_protocol: ActiveStageProtocol[
|
||||
ReductorType,
|
||||
AccumulatorType,
|
||||
Reducible[ReductorType, AccumulatorType]
|
||||
] = ReductionStageProtocol(protocol)
|
||||
assert isinstance(stage_protocol, ActiveStageProtocol)
|
||||
|
||||
super().__init__(
|
||||
ActiveStageStateProtocol(
|
||||
stage_protocol,
|
||||
reducible_factory,
|
||||
accumulator_factory,
|
||||
),
|
||||
reductor_factory,
|
||||
StateStageFactory(
|
||||
stage_protocol,
|
||||
reducible_factory,
|
||||
accumulator_factory
|
||||
)
|
||||
)
|
||||
|
||||
self.reductor_factory = reductor_factory
|
||||
self.accumulator_factory = accumulator_factory
|
@ -1,27 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .reducible import *
|
||||
from .reductionresult import *
|
||||
|
||||
__all__ = ('ReductionProtocol',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class ReductionProtocol(Generic[ReductorType, AccumulatorType]):
|
||||
async def reduce(
|
||||
self,
|
||||
reduce: Reducible[ReductorType, AccumulatorType]
|
||||
) -> ReductionResult[ReductorType, AccumulatorType]:
|
||||
raise NotImplementedError
|
||||
|
||||
def initial(self, factory: RainbowFactory[AccumulatorType]) -> HashPoint[AccumulatorType]:
|
||||
raise NotImplementedError
|
||||
|
||||
async def header_filter(
|
||||
self,
|
||||
state: HashPoint[AccumulatorType]
|
||||
) -> HashPoint[AccumulatorType]:
|
||||
raise NotImplementedError
|
@ -1,10 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
__all__ = ('ReductionResult',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class ReductionResult(Generic[ReductorType, AccumulatorType]):
|
||||
pass
|
@ -1,68 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.derivation import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .reduced import *
|
||||
from .reducible import *
|
||||
from .reductionprotocol import *
|
||||
from .reductionresult import *
|
||||
|
||||
__all__ = ('ReductionStageProtocol',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class ReductionStageProtocol(
|
||||
ActiveStageProtocol[ReductorType, AccumulatorType, Reducible[ReductorType, AccumulatorType]],
|
||||
Generic[ReductorType, AccumulatorType]
|
||||
):
|
||||
def __init__(self, protocol: ReductionProtocol[ReductorType, AccumulatorType]):
|
||||
assert isinstance(protocol, ReductionProtocol)
|
||||
self.protocol = protocol
|
||||
super().__init__()
|
||||
|
||||
async def _derive_accumulator(
|
||||
self,
|
||||
previous: NullableReference[AccumulatorType],
|
||||
) -> HashPoint[AccumulatorType]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
if previous.null():
|
||||
return self.protocol.initial(previous.factory)
|
||||
else:
|
||||
return await self.protocol.header_filter(previous.hashpoint())
|
||||
|
||||
async def derive_header(
|
||||
self,
|
||||
previous: NullableReference[AccumulatorType],
|
||||
header: HashPoint[ReductorType]
|
||||
) -> HashPoint[Reducible[ReductorType, AccumulatorType]]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
return HashPoint.of(
|
||||
Reducible(
|
||||
header,
|
||||
await self._derive_accumulator(previous)
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _derived_from_reduced(
|
||||
cls,
|
||||
reduced: ReductionResult[ReductorType, AccumulatorType]
|
||||
) -> Derived[ReductorType, Reducible[ReductorType, AccumulatorType]]:
|
||||
assert isinstance(reduced, ReductionResult)
|
||||
if isinstance(reduced, Reducible):
|
||||
return DerivedStage(HashPoint.of(reduced))
|
||||
elif isinstance(reduced, Reduced):
|
||||
return DerivedState(reduced.reduced)
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
async def derive_stage_or_state(
|
||||
self,
|
||||
stage: HashPoint[Reducible[ReductorType, AccumulatorType]]
|
||||
) -> Derived[ReductorType, Reducible[ReductorType, AccumulatorType]]:
|
||||
assert isinstance(stage, HashPoint)
|
||||
return self._derived_from_reduced(await self.protocol.reduce(await stage.resolve()))
|
@ -1,58 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from .block import *
|
||||
from .blockchain import *
|
||||
from .chaincollectionfactory import *
|
||||
from .reduction import *
|
||||
from .stages import *
|
||||
|
||||
__all__ = ('ReductionChainMetaFactory',)
|
||||
|
||||
ReductorType = TypeVar('ReductorType')
|
||||
AccumulatorType = TypeVar('AccumulatorType')
|
||||
|
||||
|
||||
class ReductionChainMetaFactory(
|
||||
AbstractReductionChainMetaFactory[
|
||||
Block[
|
||||
ReductorType,
|
||||
StateStage[ReductorType, AccumulatorType, Reducible[ReductorType, AccumulatorType]]
|
||||
],
|
||||
ReductorType,
|
||||
AccumulatorType
|
||||
],
|
||||
Generic[ReductorType, AccumulatorType]
|
||||
):
|
||||
def factory(
|
||||
self,
|
||||
reductor_factory: RainbowFactory[ReductorType],
|
||||
accumulator_factory: RainbowFactory[AccumulatorType],
|
||||
protocol: ReductionProtocol[ReductorType, AccumulatorType],
|
||||
) -> ChainCollectionFactory[
|
||||
Block[
|
||||
ReductorType,
|
||||
StateStage[ReductorType, AccumulatorType, Reducible[ReductorType, AccumulatorType]]
|
||||
],
|
||||
ReductorType
|
||||
]:
|
||||
assert isinstance(reductor_factory, RainbowFactory)
|
||||
assert isinstance(accumulator_factory, RainbowFactory)
|
||||
assert isinstance(protocol, ReductionProtocol)
|
||||
return BlockChainFactory(
|
||||
ReductionChainProtocol(
|
||||
protocol,
|
||||
reductor_factory,
|
||||
accumulator_factory,
|
||||
)
|
||||
)
|
||||
|
||||
def loose(self) -> AbstractReductionChainMetaFactory[
|
||||
Block[
|
||||
ReductorType,
|
||||
StateStage[ReductorType, AccumulatorType, Reducible[ReductorType, AccumulatorType]]
|
||||
],
|
||||
ReductorType,
|
||||
AccumulatorType
|
||||
]:
|
||||
return self
|
@ -1,9 +0,0 @@
|
||||
__all__ = (
|
||||
'StageProtocol',
|
||||
'StageStage', 'StageStageFactory', 'StateStage', 'StateStageFactory',
|
||||
'StageStateProtocol',
|
||||
)
|
||||
|
||||
from .stageprotocol import StageProtocol
|
||||
from .stagestate import StageStage, StageStageFactory, StateStage, StateStageFactory
|
||||
from .stagestateprotocol import StageStateProtocol
|
@ -1,34 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
|
||||
__all__ = ('StageProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class StageProtocol(Generic[HeaderType, BaseStateType, StageType]):
|
||||
async def verify_header(
|
||||
self,
|
||||
previous: NullableReference[BaseStateType],
|
||||
header: HashPoint[HeaderType],
|
||||
stage: HashPoint[StageType]
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
async def verify_stage(
|
||||
self,
|
||||
previous: HashPoint[StageType],
|
||||
stage: HashPoint[StageType]
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
async def verify_state(
|
||||
self,
|
||||
stage: HashPoint[StageType],
|
||||
state: HashPoint[BaseStateType]
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
@ -1,232 +0,0 @@
|
||||
from typing import Generic, Iterable, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .stageprotocol import *
|
||||
|
||||
__all__ = (
|
||||
'StageStage', 'StageStageFactory', 'StateStage', 'StateStageFactory',
|
||||
)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class Stage(Generic[HeaderType, BaseStateType, StageType]):
|
||||
def __init__(self, protocol: StageProtocol[HeaderType, BaseStateType, StageType]):
|
||||
assert isinstance(protocol, StageProtocol)
|
||||
self.protocol = protocol
|
||||
|
||||
|
||||
class StageStage(
|
||||
RecursiveMentionable,
|
||||
Stage[HeaderType, BaseStateType, StageType],
|
||||
Generic[HeaderType, BaseStateType, StageType],
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: StageProtocol[HeaderType, BaseStateType, StageType],
|
||||
previous: NullableReference['StageStage[HeaderType, BaseStateType, StageType]'],
|
||||
stage: HashPoint[StageType],
|
||||
):
|
||||
assert isinstance(protocol, StageProtocol)
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(stage, HashPoint)
|
||||
super().__init__(protocol)
|
||||
self.previous = previous
|
||||
self.stage = stage
|
||||
|
||||
@classmethod
|
||||
async def _previous_state(
|
||||
cls,
|
||||
previous: NullableReference['StateStage[HeaderType, BaseStateType, StageType]'],
|
||||
base_factory: RainbowFactory[BaseStateType],
|
||||
) -> NullableReference[BaseStateType]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(base_factory, RainbowFactory)
|
||||
if previous.null():
|
||||
return NullableReference(Null(), base_factory)
|
||||
else:
|
||||
state_stage: StateStage[HeaderType, BaseStateType, StageType] = await previous.resolve()
|
||||
assert isinstance(state_stage, StateStage)
|
||||
return NullableReference.of(state_stage.state)
|
||||
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference['StateStage[HeaderType, BaseStateType, StageType]'],
|
||||
header: HashPoint[HeaderType],
|
||||
base_factory: RainbowFactory[BaseStateType],
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(base_factory, RainbowFactory)
|
||||
if self.previous.null():
|
||||
return await self.protocol.verify_header(
|
||||
await self._previous_state(previous, base_factory),
|
||||
header,
|
||||
self.stage
|
||||
)
|
||||
else:
|
||||
previous_stage: StageStage[HeaderType, BaseStateType, StageType] = await self.previous.resolve()
|
||||
assert isinstance(previous_stage, StageStage)
|
||||
assert_trues(
|
||||
await gather(
|
||||
self.protocol.verify_stage(
|
||||
previous_stage.stage,
|
||||
self.stage
|
||||
),
|
||||
previous_stage.verify(
|
||||
previous,
|
||||
header,
|
||||
base_factory
|
||||
),
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [*self.previous.points(), self.stage]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.previous) + bytes(self.stage)
|
||||
|
||||
def __factory__(self) -> RainbowFactory['StageStage[HeaderType, BaseStateType, StageType]']:
|
||||
return StageStageFactory(
|
||||
self.protocol,
|
||||
self.stage.factory
|
||||
)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
previous_str, stage_str = await gather(
|
||||
self.previous.str(tab),
|
||||
hash_point_format(self.stage, tab)
|
||||
)
|
||||
assert isinstance(previous_str, str)
|
||||
assert isinstance(stage_str, str)
|
||||
return f'{previous_str}' \
|
||||
f'{tabulate(tab)}{stage_str}'
|
||||
|
||||
|
||||
class StageStageFactory(RainbowFactory[StageStage[HeaderType, BaseStateType, StageType]]):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: StageProtocol[HeaderType, BaseStateType, StageType],
|
||||
stage_factory: RainbowFactory[StageType]
|
||||
):
|
||||
assert isinstance(protocol, StageProtocol)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
self.protocol = protocol
|
||||
self.stage_factory = stage_factory
|
||||
|
||||
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], resolver),
|
||||
ResolverOrigin(self.stage_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point()
|
||||
)
|
||||
|
||||
|
||||
class StateStage(
|
||||
RecursiveMentionable,
|
||||
Stage[HeaderType, BaseStateType, StageType],
|
||||
Generic[HeaderType, BaseStateType, StageType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: StageProtocol[HeaderType, BaseStateType, StageType],
|
||||
previous: HashPoint[StageStage[HeaderType, BaseStateType, StageType]],
|
||||
state: HashPoint[BaseStateType],
|
||||
stage_factory: RainbowFactory[StageType]
|
||||
):
|
||||
assert isinstance(protocol, StageProtocol)
|
||||
assert isinstance(previous, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
super().__init__(protocol)
|
||||
self.previous = previous
|
||||
self.state = state
|
||||
self.stage_factory = stage_factory
|
||||
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference['StateStage[HeaderType, BaseStateType, StageType]'],
|
||||
header: HashPoint[HeaderType]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
previous_stage: StageStage[HeaderType, BaseStateType, StageType] = await self.previous.resolve()
|
||||
assert isinstance(previous_stage, StageStage)
|
||||
assert_trues(
|
||||
await gather(
|
||||
self.protocol.verify_state(
|
||||
previous_stage.stage,
|
||||
self.state
|
||||
),
|
||||
previous_stage.verify(
|
||||
previous,
|
||||
header,
|
||||
self.state.factory
|
||||
)
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [self.previous, self.state]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.previous) + bytes(self.state)
|
||||
|
||||
def __factory__(self) -> RainbowFactory['StateStage[HeaderType, BaseStateType, StageType]']:
|
||||
return StateStageFactory(
|
||||
self.protocol,
|
||||
self.stage_factory,
|
||||
self.state.factory
|
||||
)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
previous_str, state_str = await gather(
|
||||
hash_point_format(self.previous, tab),
|
||||
hash_point_format(self.state, tab),
|
||||
)
|
||||
assert isinstance(previous_str, str)
|
||||
assert isinstance(state_str, str)
|
||||
return f'{previous_str}' \
|
||||
f'{tabulate(tab)}{state_str}'
|
||||
|
||||
|
||||
class StateStageFactory(RainbowFactory[StateStage[HeaderType, BaseStateType, StageType]]):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: StageProtocol[HeaderType, BaseStateType, StageType],
|
||||
stage_factory: RainbowFactory[StageType],
|
||||
state_factory: RainbowFactory[BaseStateType]
|
||||
):
|
||||
assert isinstance(protocol, StageProtocol)
|
||||
assert isinstance(stage_factory, RainbowFactory)
|
||||
assert isinstance(state_factory, RainbowFactory)
|
||||
self.protocol = protocol
|
||||
self.stage_factory = stage_factory
|
||||
self.state_factory = state_factory
|
||||
|
||||
def from_bytes(self, source: bytes, resolver: HashResolver) -> StateStage[HeaderType, BaseStateType, StageType]:
|
||||
assert isinstance(source, bytes)
|
||||
assert isinstance(resolver, HashResolver)
|
||||
return StateStage(
|
||||
self.protocol,
|
||||
ResolverOrigin(
|
||||
StageStageFactory(
|
||||
self.protocol,
|
||||
self.stage_factory
|
||||
),
|
||||
source[:HashPoint.HASH_LENGTH],
|
||||
resolver
|
||||
).hash_point(),
|
||||
ResolverOrigin(self.state_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(),
|
||||
self.stage_factory
|
||||
)
|
@ -1,29 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.states import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .stagestate import *
|
||||
|
||||
__all__ = ('StageStateProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
BaseStateType = TypeVar('BaseStateType')
|
||||
StageType = TypeVar('StageType')
|
||||
|
||||
|
||||
class StageStateProtocol(
|
||||
StateProtocol[HeaderType, StateStage[HeaderType, BaseStateType, StageType]],
|
||||
Generic[HeaderType, BaseStateType, StageType]
|
||||
):
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference[StateStage[HeaderType, BaseStateType, StageType]],
|
||||
header: HashPoint[HeaderType],
|
||||
state: HashPoint[StateStage[HeaderType, BaseStateType, StageType]]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
assert_true(await (await state.resolve()).verify(previous, header))
|
||||
return True
|
@ -1,9 +0,0 @@
|
||||
__all__ = (
|
||||
'ActiveStateProtocol',
|
||||
'MetaReductionStateProtocol',
|
||||
'StateProtocol',
|
||||
)
|
||||
|
||||
from .activestateprotocol import ActiveStateProtocol
|
||||
from .metareductionstateprotocol import MetaReductionStateProtocol
|
||||
from .stateprotocol import StateProtocol
|
@ -1,31 +0,0 @@
|
||||
from typing import TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .stateprotocol import *
|
||||
|
||||
__all__ = ('ActiveStateProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class ActiveStateProtocol(StateProtocol[HeaderType, StateType]):
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference[StateType],
|
||||
header: HashPoint[HeaderType],
|
||||
state: HashPoint[StateType]
|
||||
) -> bool:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
assert_eq(state, await self.derive(previous, header))
|
||||
return True
|
||||
|
||||
async def derive(
|
||||
self,
|
||||
previous: NullableReference[StateType],
|
||||
header: HashPoint[HeaderType],
|
||||
) -> HashPoint[StateType]:
|
||||
raise NotImplementedError
|
@ -1,36 +0,0 @@
|
||||
from typing import TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .activestateprotocol import *
|
||||
|
||||
__all__ = ('MetaReductionStateProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class MetaReductionStateProtocol(ActiveStateProtocol[HeaderType, StateType]):
|
||||
def _initial_state(self) -> HashPoint[StateType]:
|
||||
raise NotImplementedError
|
||||
|
||||
async def _derive(
|
||||
self,
|
||||
previous: HashPoint[StateType],
|
||||
header: HashPoint[HeaderType],
|
||||
) -> HashPoint[StateType]:
|
||||
raise NotImplementedError
|
||||
|
||||
async def derive(
|
||||
self,
|
||||
previous: NullableReference[StateType],
|
||||
header: HashPoint[HeaderType],
|
||||
) -> HashPoint[StateType]:
|
||||
assert isinstance(previous, NullableReference)
|
||||
assert isinstance(header, HashPoint)
|
||||
if previous.null():
|
||||
previous_state: HashPoint[StateType] = self._initial_state()
|
||||
else:
|
||||
previous_state: HashPoint[StateType] = previous.hashpoint()
|
||||
assert isinstance(previous_state, HashPoint)
|
||||
return await self._derive(previous_state, header)
|
@ -1,19 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
|
||||
__all__ = ('StateProtocol',)
|
||||
|
||||
HeaderType = TypeVar('HeaderType')
|
||||
StateType = TypeVar('StateType')
|
||||
|
||||
|
||||
class StateProtocol(Generic[HeaderType, StateType]):
|
||||
async def verify(
|
||||
self,
|
||||
previous: NullableReference[StateType],
|
||||
header: HashPoint[HeaderType],
|
||||
state: HashPoint[StateType]
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
@ -5,11 +5,7 @@ todo: deprecate
|
||||
__all__ = (
|
||||
'ListBridge',
|
||||
'StackBridge',
|
||||
'StageBridgeVP', 'StageBridgeM', 'stage_bridge',
|
||||
'StateBridgeM', 'StateBridgeV', 'state_bridge',
|
||||
)
|
||||
|
||||
from ._listbridge import ListBridge
|
||||
from ._stackbridge import StackBridge
|
||||
from ._stagebridge import StageBridgeM, StageBridgeVP, stage_bridge
|
||||
from ._statebridge import StateBridgeM, StateBridgeV, state_bridge
|
||||
|
@ -1,106 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.stages import *
|
||||
from rainbowadn.collection.linear import *
|
||||
from rainbowadn.collection.pair import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.flow.core import *
|
||||
from rainbowadn.flow.sequence import *
|
||||
from rainbowadn.flow.verification.core import *
|
||||
from rainbowadn.flow.verification.stages import *
|
||||
from rainbowadn.nullability import *
|
||||
from ._stackbridge import *
|
||||
|
||||
__all__ = ('StageBridgeVP', 'StageBridgeM', 'stage_bridge',)
|
||||
|
||||
BaseT = TypeVar('BaseT')
|
||||
StageT = TypeVar('StageT')
|
||||
HeaderT = TypeVar('HeaderT')
|
||||
|
||||
|
||||
class StageBridgeVP(
|
||||
StageVerificationProtocol[HashPoint[BaseT], HashPoint[StageT], HashPoint[HeaderT]],
|
||||
Generic[BaseT, StageT, HeaderT]
|
||||
):
|
||||
def __init__(self, stage_protocol: StageProtocol[HeaderT, BaseT, StageT], base_factory: RainbowFactory[BaseT]):
|
||||
assert isinstance(stage_protocol, StageProtocol)
|
||||
assert isinstance(base_factory, RainbowFactory)
|
||||
self.stage_protocol = stage_protocol
|
||||
self.base_factory = base_factory
|
||||
|
||||
async def initial(
|
||||
self, previous: Nullable[HashPoint[BaseT]], header: HashPoint[HeaderT], stage: HashPoint[StageT]
|
||||
) -> bool:
|
||||
assert_true(
|
||||
await self.stage_protocol.verify_header(NullableReference(previous, self.base_factory), header, stage)
|
||||
)
|
||||
return True
|
||||
|
||||
async def final(self, stage: HashPoint[StageT], base: HashPoint[BaseT]) -> bool:
|
||||
assert_true(
|
||||
await self.stage_protocol.verify_state(stage, base)
|
||||
)
|
||||
return True
|
||||
|
||||
async def intermediate(self, previous: HashPoint[StageT], stage: HashPoint[StageT]) -> bool:
|
||||
assert_true(
|
||||
await self.stage_protocol.verify_stage(previous, stage)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
class StageBridgeM(
|
||||
Mapper[
|
||||
HashPoint[Pair[BaseT, Stack[StageT]]],
|
||||
tuple[HashPoint[BaseT], Reducer[SequenceDispatcher[HashPoint[StageT], bool], bool]]
|
||||
],
|
||||
Generic[BaseT, StageT],
|
||||
):
|
||||
async def map(
|
||||
self,
|
||||
element: HashPoint[Pair[BaseT, Stack[StageT]]],
|
||||
) -> tuple[HashPoint[BaseT], Reducer[SequenceDispatcher[HashPoint[StageT], bool], bool]]:
|
||||
assert isinstance(element, HashPoint)
|
||||
pair: Pair[BaseT, Stack[StageT]] = await element.resolve()
|
||||
assert isinstance(pair, Pair)
|
||||
base: HashPoint[BaseT] = pair.element0
|
||||
assert isinstance(base, HashPoint)
|
||||
stages_stack: HashPoint[Stack[StageT]] = pair.element1
|
||||
assert isinstance(stages_stack, HashPoint)
|
||||
stack_bridge: Reducer[
|
||||
SequenceDispatcher[HashPoint[StageT], bool],
|
||||
bool
|
||||
] = StackBridge(stages_stack).over_elements()
|
||||
assert isinstance(stack_bridge, Reducer)
|
||||
return base, stack_bridge
|
||||
|
||||
|
||||
def stage_bridge(
|
||||
stage_protocol: StageProtocol[HeaderT, BaseT, StageT], base_factory: RainbowFactory[BaseT]
|
||||
) -> Verification[
|
||||
tuple[
|
||||
Nullable[HashPoint[Pair[BaseT, Stack[StageT]]]],
|
||||
HashPoint[HeaderT],
|
||||
HashPoint[Pair[BaseT, Stack[StageT]]]
|
||||
]
|
||||
]:
|
||||
assert isinstance(stage_protocol, StageProtocol)
|
||||
assert isinstance(base_factory, RainbowFactory)
|
||||
vp: StageVerificationProtocol[
|
||||
HashPoint[BaseT], HashPoint[StageT], HashPoint[HeaderT]
|
||||
] = StageBridgeVP(stage_protocol, base_factory)
|
||||
assert isinstance(vp, StageVerificationProtocol)
|
||||
m: Mapper[
|
||||
HashPoint[Pair[BaseT, Stack[StageT]]],
|
||||
tuple[HashPoint[BaseT], Reducer[SequenceDispatcher[HashPoint[StageT], bool], bool]]
|
||||
] = StageBridgeM()
|
||||
assert isinstance(m, Mapper)
|
||||
v: Verification[
|
||||
tuple[
|
||||
Nullable[HashPoint[Pair[BaseT, Stack[StageT]]]],
|
||||
HashPoint[HeaderT],
|
||||
HashPoint[Pair[BaseT, Stack[StageT]]]
|
||||
]
|
||||
] = StageVerification(vp, m).loose()
|
||||
assert isinstance(v, Verification)
|
||||
return v
|
@ -1,94 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain.states import *
|
||||
from rainbowadn.collection.pair import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.flow.core import *
|
||||
from rainbowadn.flow.verification.core import *
|
||||
from rainbowadn.flow.verification.stateverification import *
|
||||
from rainbowadn.nullability import *
|
||||
|
||||
__all__ = ('StateBridgeM', 'StateBridgeV', 'state_bridge',)
|
||||
|
||||
HeaderT = TypeVar('HeaderT')
|
||||
StateT = TypeVar('StateT')
|
||||
|
||||
|
||||
class StateBridgeM(
|
||||
Mapper[
|
||||
HashPoint[Pair[HeaderT, StateT]],
|
||||
tuple[HashPoint[HeaderT], HashPoint[StateT]],
|
||||
],
|
||||
Generic[HeaderT, StateT]
|
||||
):
|
||||
async def map(
|
||||
self,
|
||||
element: HashPoint[Pair[HeaderT, StateT]]
|
||||
) -> tuple[HashPoint[HeaderT], HashPoint[StateT]]:
|
||||
assert isinstance(element, HashPoint)
|
||||
resolved = await element.resolve()
|
||||
header: HashPoint[HeaderT] = resolved.element0
|
||||
assert isinstance(header, HashPoint)
|
||||
state: HashPoint[StateT] = resolved.element1
|
||||
assert isinstance(state, HashPoint)
|
||||
return header, state
|
||||
|
||||
|
||||
class StateBridgeV(
|
||||
Verification[
|
||||
tuple[Nullable[HashPoint[StateT]], HashPoint[HeaderT], HashPoint[StateT]]
|
||||
],
|
||||
Generic[HeaderT, StateT]
|
||||
):
|
||||
def __init__(self, protocol: StateProtocol[HeaderT, StateT]):
|
||||
assert isinstance(protocol, StateProtocol)
|
||||
self.protocol = protocol
|
||||
|
||||
async def verify(
|
||||
self,
|
||||
element: tuple[Nullable[HashPoint[StateT]], HashPoint[HeaderT], HashPoint[StateT]]
|
||||
) -> bool:
|
||||
assert isinstance(element, tuple)
|
||||
previous: Nullable[HashPoint[StateT]]
|
||||
header: HashPoint[HeaderT]
|
||||
state: HashPoint[StateT]
|
||||
previous, header, state = element
|
||||
assert isinstance(previous, Nullable)
|
||||
assert isinstance(header, HashPoint)
|
||||
assert isinstance(state, HashPoint)
|
||||
assert_true(
|
||||
await self.protocol.verify(
|
||||
NullableReference(previous, state.factory),
|
||||
header,
|
||||
state
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def state_bridge(
|
||||
protocol: StateProtocol[HeaderT, StateT]
|
||||
) -> Verification[
|
||||
tuple[
|
||||
Nullable[HashPoint[Pair[HeaderT, StateT]]],
|
||||
HashPoint[Pair[HeaderT, StateT]]
|
||||
]
|
||||
]:
|
||||
assert isinstance(protocol, StateProtocol)
|
||||
m: Mapper[
|
||||
HashPoint[Pair[HeaderT, StateT]],
|
||||
tuple[HashPoint[HeaderT], HashPoint[StateT]],
|
||||
] = StateBridgeM()
|
||||
assert isinstance(m, Mapper)
|
||||
sv: Verification[
|
||||
tuple[Nullable[HashPoint[StateT]], HashPoint[HeaderT], HashPoint[StateT]]
|
||||
] = StateBridgeV(protocol)
|
||||
assert isinstance(sv, Verification)
|
||||
v: Verification[
|
||||
tuple[
|
||||
Nullable[HashPoint[Pair[HeaderT, StateT]]],
|
||||
HashPoint[Pair[HeaderT, StateT]]
|
||||
]
|
||||
] = StateVerification(m, sv).loose()
|
||||
assert isinstance(v, Verification)
|
||||
return v
|
@ -5,7 +5,7 @@ from rainbowadn.collection.keyvalue import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.flow.core import *
|
||||
from rainbowadn.flow.verification.core import *
|
||||
from rainbowadn.v13 import MINT_CONST
|
||||
from rainbowadn.v13 import *
|
||||
from ._flowiterate import *
|
||||
from ._flowstandard import *
|
||||
from ._flowtransaction import *
|
||||
|
@ -7,7 +7,7 @@ from rainbowadn.collection.keyvalue import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.flow.core import *
|
||||
from rainbowadn.flow.verification.core import *
|
||||
from rainbowadn.v13 import Signature, Subject
|
||||
from rainbowadn.v13 import *
|
||||
from ._flowstandard import *
|
||||
|
||||
__all__ = ('FlowCoinData', 'FlowCoin', 'FlowTransactionData', 'FlowTransaction',)
|
||||
|
@ -18,7 +18,7 @@ from rainbowadn.flow.sequence import *
|
||||
from rainbowadn.flow.stacked import *
|
||||
from rainbowadn.flow13 import *
|
||||
from rainbowadn.nullability import *
|
||||
from rainbowadn.v13 import Subject
|
||||
from rainbowadn.v13 import *
|
||||
|
||||
|
||||
class PrintDispatch(SequenceDispatch[HashPoint, None]):
|
||||
|
@ -1,12 +1,9 @@
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
from typing import Any
|
||||
|
||||
from rainbowadn.atomic import *
|
||||
from rainbowadn.chain import *
|
||||
from rainbowadn.collection.comparison import *
|
||||
from rainbowadn.collection.pair import *
|
||||
from rainbowadn.collection.trees.binary import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.testing.resolvers import *
|
||||
@ -56,27 +53,6 @@ class TestTrees(unittest.IsolatedAsyncioTestCase):
|
||||
print(btree.height)
|
||||
measure('resolve and add')
|
||||
|
||||
async def test_wrisbt_index(self):
|
||||
set_gather_linear()
|
||||
with self.subTest('create empty'):
|
||||
factory: RainbowFactory[Pair[Plain, Plain]] = PairFactory(Plain.factory(), Plain.factory()).loose()
|
||||
chain: ChainCollectionInterface[Any, Pair[Plain, Plain], WrisbtRoot] = BlockChainFactory(
|
||||
WrisbtChainProtocol(factory, 2).loose()
|
||||
).empty().loose()
|
||||
with self.subTest('fill'):
|
||||
for _ in range(100):
|
||||
chain = await chain.add(
|
||||
HashPoint.of(
|
||||
Pair(
|
||||
HashPoint.of(Plain(os.urandom(16))),
|
||||
HashPoint.of(Plain(os.urandom(16)))
|
||||
)
|
||||
)
|
||||
)
|
||||
with self.subTest('check'):
|
||||
set_gather_asyncio()
|
||||
assert_true(await chain.verify())
|
||||
|
||||
async def test_avl(self):
|
||||
set_gather_linear()
|
||||
tree: ActiveBinaryTree[Plain, Integer] = ActiveBinaryTree.empty(
|
||||
|
@ -1,52 +0,0 @@
|
||||
import unittest
|
||||
|
||||
import nacl.signing
|
||||
|
||||
from rainbowadn.chain import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from rainbowadn.testing.resolvers import *
|
||||
from rainbowadn.v13 import *
|
||||
|
||||
|
||||
class TestV13(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_bankchain(self):
|
||||
set_gather_linear()
|
||||
with self.subTest('create empty'):
|
||||
bank: BankChain = BankChain.empty(ReductionChainMetaFactory().loose())
|
||||
with self.subTest('prepare transactions'):
|
||||
key_0 = nacl.signing.SigningKey.generate()
|
||||
transaction_0 = Transaction.make(
|
||||
[],
|
||||
[CoinData.of(Subject(key_0.verify_key), 1_000_000)],
|
||||
[]
|
||||
)
|
||||
coin_0, coin_1 = await transaction_0.coins(MINT_CONST, NotNull(HashPoint.of(Subject(key_0.verify_key))))
|
||||
with self.subTest('add transactions'):
|
||||
bank = await bank.adds(
|
||||
[
|
||||
transaction_0,
|
||||
Transaction.make(
|
||||
[coin_1],
|
||||
[CoinData.of(Subject(nacl.signing.SigningKey.generate().verify_key), 10_000)],
|
||||
[key_0]
|
||||
),
|
||||
]
|
||||
)
|
||||
with self.subTest('add empty'):
|
||||
bank = await bank.adds(
|
||||
[]
|
||||
)
|
||||
print(await bank.reference.str(0))
|
||||
with self.subTest('verify'):
|
||||
assert_true(await bank.verify())
|
||||
with self.subTest('recover'):
|
||||
bank = BankChain.from_reference(
|
||||
ReductionChainMetaFactory(), await default_resolver().migrate_resolved(bank.reference)
|
||||
)
|
||||
set_gather_asyncio()
|
||||
print('recovering')
|
||||
print(await bank.reference.str(0))
|
||||
print('recovered')
|
||||
with self.subTest('verify'):
|
||||
assert_true(await bank.verify())
|
@ -1,13 +1,9 @@
|
||||
__all__ = (
|
||||
'MINT_CONST',
|
||||
'BankChain',
|
||||
'BadSignature', 'Signature',
|
||||
'Subject',
|
||||
'CoinData', 'Coin', 'TransactionData', 'Transaction',
|
||||
)
|
||||
|
||||
from .algo import MINT_CONST
|
||||
from .bankchain import BankChain
|
||||
from .signature import BadSignature, Signature
|
||||
from .subject import Subject
|
||||
from .transaction import Coin, CoinData, Transaction, TransactionData
|
||||
|
@ -1,80 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain import *
|
||||
from rainbowadn.chain.reduction import *
|
||||
from rainbowadn.collection.linear import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .bankprotocol import *
|
||||
from .bankstate import *
|
||||
from .transaction import *
|
||||
|
||||
__all__ = ('BankChain',)
|
||||
|
||||
BlockType = TypeVar('BlockType')
|
||||
|
||||
|
||||
class BankChain(Generic[BlockType]):
|
||||
def __init__(
|
||||
self,
|
||||
chain: ChainCollectionInterface[
|
||||
BlockType,
|
||||
NullableReference[Stack[Transaction]],
|
||||
]
|
||||
):
|
||||
assert isinstance(chain, ChainCollectionInterface)
|
||||
self.chain = chain
|
||||
self.reference = self.chain.reference
|
||||
assert isinstance(self.reference, NullableReference)
|
||||
|
||||
@classmethod
|
||||
def empty(
|
||||
cls,
|
||||
factory: AbstractReductionChainMetaFactory[BlockType, NullableReference[Stack[Transaction]], BankState]
|
||||
) -> 'BankChain[BlockType]':
|
||||
assert isinstance(factory, AbstractReductionChainMetaFactory)
|
||||
return cls(
|
||||
factory.factory(
|
||||
NullableReferenceFactory(StackFactory(Transaction.factory()).loose()).loose(),
|
||||
BankState.factory(),
|
||||
BankProtocol(),
|
||||
).empty()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_reference(
|
||||
cls,
|
||||
factory: AbstractReductionChainMetaFactory[BlockType, NullableReference[Stack[Transaction]], BankState],
|
||||
reference: NullableReference[
|
||||
BlockType
|
||||
]
|
||||
) -> 'BankChain[BlockType]':
|
||||
assert isinstance(factory, AbstractReductionChainMetaFactory)
|
||||
assert isinstance(reference, NullableReference)
|
||||
return cls(
|
||||
factory.factory(
|
||||
NullableReferenceFactory(StackFactory(Transaction.factory()).loose()).loose(),
|
||||
BankState.factory(),
|
||||
BankProtocol(),
|
||||
).from_reference(
|
||||
reference
|
||||
)
|
||||
)
|
||||
|
||||
async def add(self, transactions: NullableReference[Stack[Transaction]]) -> 'BankChain[BlockType]':
|
||||
assert isinstance(transactions, NullableReference)
|
||||
return BankChain(
|
||||
await self.chain.add(HashPoint.of(transactions))
|
||||
)
|
||||
|
||||
async def adds(self, transactions: list[Transaction]) -> 'BankChain[BlockType]':
|
||||
assert isinstance(transactions, list)
|
||||
return await self.add(
|
||||
Stack.off(
|
||||
Transaction.factory(),
|
||||
reversed(transactions)
|
||||
)
|
||||
)
|
||||
|
||||
async def verify(self) -> bool:
|
||||
return await self.chain.verify()
|
@ -1,58 +0,0 @@
|
||||
from rainbowadn.atomic import *
|
||||
from rainbowadn.chain.reduction import *
|
||||
from rainbowadn.collection.keymetadata import *
|
||||
from rainbowadn.collection.linear import *
|
||||
from rainbowadn.collection.trees.binary import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .bankstate import *
|
||||
from .subject import *
|
||||
from .transaction import *
|
||||
|
||||
__all__ = ('BankProtocol',)
|
||||
|
||||
|
||||
class BankProtocol(ReductionProtocol[NullableReference[Stack[Transaction]], BankState]):
|
||||
async def reduce(
|
||||
self,
|
||||
reduce: Reducible[NullableReference[Stack[Transaction]], BankState]
|
||||
) -> ReductionResult[NullableReference[Stack[Transaction]], BankState]:
|
||||
assert isinstance(reduce, Reducible)
|
||||
bank_state: BankState
|
||||
reference: NullableReference[Stack[Transaction]]
|
||||
bank_state, reference = await gather(
|
||||
reduce.accumulator.resolve(),
|
||||
reduce.reductor.resolve(),
|
||||
)
|
||||
assert isinstance(bank_state, BankState)
|
||||
assert isinstance(reference, NullableReference)
|
||||
if reference.null():
|
||||
return Reduced(HashPoint.of(bank_state.without_miner()))
|
||||
else:
|
||||
transactions: Stack[Transaction] = await reference.resolve()
|
||||
assert isinstance(transactions, Stack)
|
||||
return Reducible(
|
||||
HashPoint.of(transactions.previous),
|
||||
HashPoint.of(await bank_state.push(transactions.element))
|
||||
)
|
||||
|
||||
def initial(self, factory: RainbowFactory[BankState]) -> HashPoint[BankState]:
|
||||
assert isinstance(factory, RainbowFactory)
|
||||
return HashPoint.of(
|
||||
BankState(
|
||||
NullableReference(
|
||||
Null(),
|
||||
BinaryTreeFactory(KeyMetadataFactory(StaticFactory(Coin), StaticFactory(Integer)).loose()).loose()
|
||||
),
|
||||
NullableReference(
|
||||
Null(),
|
||||
BinaryTreeFactory(KeyMetadataFactory(StaticFactory(Coin), StaticFactory(Integer)).loose()).loose()
|
||||
),
|
||||
NullableReference(Null(), Subject.factory()),
|
||||
HashPoint.of(Integer(0))
|
||||
)
|
||||
)
|
||||
|
||||
async def header_filter(self, state: HashPoint[BankState]) -> HashPoint[BankState]:
|
||||
assert isinstance(state, HashPoint)
|
||||
return HashPoint.of(await (await state.resolve()).without_miner().advance())
|
@ -1,183 +0,0 @@
|
||||
from typing import AsyncIterable, Iterable
|
||||
|
||||
from rainbowadn.atomic import *
|
||||
from rainbowadn.collection.comparison import *
|
||||
from rainbowadn.collection.keymetadata import *
|
||||
from rainbowadn.collection.trees.binary import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .algo import *
|
||||
from .subject import *
|
||||
from .transaction import *
|
||||
|
||||
__all__ = ('BankState',)
|
||||
|
||||
|
||||
class BankState(RecursiveMentionable, StaticMentionable):
|
||||
def __init__(
|
||||
self,
|
||||
minted: NullableReference[BinaryTree[KeyMetadata[Coin, Integer]]],
|
||||
used: NullableReference[BinaryTree[KeyMetadata[Coin, Integer]]],
|
||||
miner: NullableReference[Subject],
|
||||
length: HashPoint[Integer]
|
||||
):
|
||||
assert isinstance(miner, NullableReference)
|
||||
assert isinstance(used, NullableReference)
|
||||
assert isinstance(miner, NullableReference)
|
||||
assert isinstance(length, HashPoint)
|
||||
self.minted = minted
|
||||
self.used = used
|
||||
self.miner = miner
|
||||
self.length = length
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [*self.minted.points(), *self.used.points(), *self.miner.points(), self.length]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.minted) + bytes(self.used) + bytes(self.miner) + bytes(self.length)
|
||||
|
||||
@classmethod
|
||||
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(
|
||||
BinaryTreeFactory(
|
||||
KeyMetadataFactory(
|
||||
StaticFactory(Coin),
|
||||
StaticFactory(Integer),
|
||||
).loose()
|
||||
).loose()
|
||||
).loose()
|
||||
assert isinstance(reference_factory, RainbowFactory)
|
||||
return cls(
|
||||
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], resolver),
|
||||
ResolverOrigin(Integer.factory(), source[3 * HashPoint.HASH_LENGTH:], resolver).hash_point()
|
||||
)
|
||||
|
||||
def without_miner(self) -> 'BankState':
|
||||
return BankState(
|
||||
self.minted,
|
||||
self.used,
|
||||
NullableReference(Null(), self.miner.factory),
|
||||
self.length
|
||||
)
|
||||
|
||||
async def advance(self) -> 'BankState':
|
||||
return BankState(
|
||||
self.minted,
|
||||
self.used,
|
||||
NullableReference(Null(), self.miner.factory),
|
||||
HashPoint.of(Integer((await self.length.resolve()).integer + 1))
|
||||
)
|
||||
|
||||
def minted_tree(self) -> ActiveBinaryTree[Coin, Integer]:
|
||||
return ActiveBinaryTree(
|
||||
AVL(HashComparator(Fail())), self.minted
|
||||
)
|
||||
|
||||
def used_tree(self) -> ActiveBinaryTree[Coin, Integer]:
|
||||
return ActiveBinaryTree(
|
||||
AVL(HashComparator(Fail())), self.used
|
||||
)
|
||||
|
||||
async def use_coins(self, coins: AsyncIterable[HashPoint[Coin]]) -> 'BankState':
|
||||
minted: ActiveBinaryTree[Coin, Integer] = self.minted_tree()
|
||||
used: ActiveBinaryTree[Coin, Integer] = self.used_tree()
|
||||
async for in_coin in coins:
|
||||
minted_contains, used_contains, used = await gather(
|
||||
minted.contains(in_coin),
|
||||
used.contains(in_coin),
|
||||
used.add(in_coin)
|
||||
)
|
||||
assert_true(minted_contains)
|
||||
assert_false(used_contains)
|
||||
assert isinstance(used, ActiveBinaryTree)
|
||||
return BankState(self.minted, used.reference, self.miner, self.length)
|
||||
|
||||
async def _mint_coins(
|
||||
self,
|
||||
transaction: Transaction
|
||||
) -> 'BankState':
|
||||
assert isinstance(transaction, Transaction)
|
||||
miner = self.miner_nullable()
|
||||
assert isinstance(miner, Nullable)
|
||||
minted: ActiveBinaryTree[Coin, Integer] = self.minted_tree()
|
||||
assert isinstance(minted, ActiveBinaryTree)
|
||||
async for coin, miner in transaction.iter_coins(self.mint(), miner):
|
||||
minted_contains, minted = await gather(
|
||||
minted.contains(HashPoint.of(coin)),
|
||||
minted.add(HashPoint.of(coin)),
|
||||
)
|
||||
assert_false(minted_contains)
|
||||
assert isinstance(minted, ActiveBinaryTree)
|
||||
return BankState(minted.reference, self.used, NullableReference(miner, self.miner.factory), self.length)
|
||||
|
||||
async def mint_coins(
|
||||
self,
|
||||
transaction: Transaction
|
||||
) -> 'BankState':
|
||||
assert isinstance(transaction, Transaction)
|
||||
verified, bank_state = await gather(
|
||||
self.verify(transaction),
|
||||
self._mint_coins(transaction),
|
||||
)
|
||||
assert_true(verified)
|
||||
assert isinstance(bank_state, BankState)
|
||||
return bank_state
|
||||
|
||||
def miner_nullable(self) -> Nullable[HashPoint[Subject]]:
|
||||
return self.miner.reference
|
||||
|
||||
def mint(self) -> int:
|
||||
if self.miner_nullable().null():
|
||||
return MINT_CONST
|
||||
else:
|
||||
return 0
|
||||
|
||||
async def verify(self, transaction: Transaction) -> bool:
|
||||
assert isinstance(transaction, Transaction)
|
||||
assert_true(await transaction.verify(self.mint()))
|
||||
return True
|
||||
|
||||
async def _push(self, transaction: Transaction) -> 'BankState':
|
||||
assert isinstance(transaction, Transaction)
|
||||
return await (
|
||||
await self.use_coins(
|
||||
(await transaction.data_resolved()).iter_in_coins()
|
||||
)
|
||||
).mint_coins(
|
||||
transaction
|
||||
)
|
||||
|
||||
async def push(self, transaction: HashPoint[Transaction]) -> 'BankState':
|
||||
return await self._push(await transaction.resolve())
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
miner_str, minted_str, used_str, length_str = await gather(
|
||||
self.miner.str(tab + 1),
|
||||
self.minted.str(tab + 1),
|
||||
self.used.str(tab + 1),
|
||||
hash_point_format(self.length, tab + 1),
|
||||
)
|
||||
assert isinstance(miner_str, str)
|
||||
assert isinstance(minted_str, str)
|
||||
assert isinstance(used_str, str)
|
||||
assert isinstance(length_str, str)
|
||||
return f'(' \
|
||||
f'{tabulate(tab + 1)}bank' \
|
||||
f'{tabulate(tab + 1)}(miner)' \
|
||||
f'{tabulate(tab + 1)}{miner_str}' \
|
||||
f'{tabulate(tab + 1)}(minted)' \
|
||||
f'{tabulate(tab + 1)}{minted_str}' \
|
||||
f'{tabulate(tab + 1)}(used)' \
|
||||
f'{tabulate(tab + 1)}{used_str}' \
|
||||
f'{tabulate(tab + 1)}(length)' \
|
||||
f'{tabulate(tab + 1)}{length_str}' \
|
||||
f'{tabulate(tab)})'
|
@ -1,370 +0,0 @@
|
||||
from typing import AsyncIterable, Iterable
|
||||
|
||||
import nacl.signing
|
||||
|
||||
from rainbowadn.atomic import *
|
||||
from rainbowadn.collection.linear import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.nullability import *
|
||||
from .signature import *
|
||||
from .subject import *
|
||||
|
||||
__all__ = ('CoinData', 'Coin', 'TransactionData', 'Transaction',)
|
||||
|
||||
|
||||
class CoinData(RecursiveMentionable, StaticMentionable):
|
||||
def __init__(
|
||||
self,
|
||||
owner: HashPoint[Subject],
|
||||
value: HashPoint[Integer]
|
||||
):
|
||||
assert isinstance(owner, HashPoint)
|
||||
assert isinstance(value, HashPoint)
|
||||
self.owner = owner
|
||||
self.value = value
|
||||
|
||||
async def int_value(self) -> int:
|
||||
return (await self.value.resolve()).integer
|
||||
|
||||
@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]:
|
||||
return [self.owner, self.value]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.owner) + bytes(self.value)
|
||||
|
||||
@classmethod
|
||||
def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'CoinData':
|
||||
assert isinstance(source, bytes)
|
||||
assert isinstance(resolver, HashResolver)
|
||||
return cls(
|
||||
ResolverOrigin(Subject.factory(), source[:HashPoint.HASH_LENGTH], resolver).hash_point(),
|
||||
ResolverOrigin(Integer.factory(), source[HashPoint.HASH_LENGTH:], resolver).hash_point(),
|
||||
)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
owner_str, value_str = await gather(
|
||||
hash_point_format(self.owner, tab),
|
||||
hash_point_format(self.value, tab),
|
||||
)
|
||||
assert isinstance(owner_str, str)
|
||||
assert isinstance(value_str, str)
|
||||
return f'{owner_str}' \
|
||||
f'{tabulate(tab)}{value_str}'
|
||||
|
||||
|
||||
class Coin(RecursiveMentionable, StaticMentionable):
|
||||
def __init__(
|
||||
self,
|
||||
data: HashPoint[CoinData],
|
||||
transaction: HashPoint['Transaction'],
|
||||
index: HashPoint[Integer]
|
||||
):
|
||||
assert isinstance(data, HashPoint)
|
||||
assert isinstance(transaction, HashPoint)
|
||||
assert isinstance(index, HashPoint)
|
||||
self.data = data
|
||||
self.transaction = transaction
|
||||
self.index = index
|
||||
|
||||
async def data_resolved(self) -> CoinData:
|
||||
return await self.data.resolve()
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [self.data, self.transaction, self.index]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.data) + bytes(self.transaction) + bytes(self.index)
|
||||
|
||||
@classmethod
|
||||
def from_bytes(cls, source: bytes, resolver: HashResolver) -> 'Coin':
|
||||
assert isinstance(source, bytes)
|
||||
assert isinstance(resolver, HashResolver)
|
||||
return cls(
|
||||
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(),
|
||||
)
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
data_str, index_str = await gather(
|
||||
hash_point_format(self.data, tab + 1),
|
||||
hash_point_format(self.index, tab + 1),
|
||||
)
|
||||
assert isinstance(data_str, str)
|
||||
assert isinstance(index_str, str)
|
||||
return f'(' \
|
||||
f'{tabulate(tab + 1)}coin' \
|
||||
f'{tabulate(tab + 1)}{data_str}' \
|
||||
f'{tabulate(tab + 1)}(origin)' \
|
||||
f'{tabulate(tab + 1)}{index_str}' \
|
||||
f'{tabulate(tab)})'
|
||||
|
||||
async def int_value(self) -> int:
|
||||
return await (await self.data_resolved()).int_value()
|
||||
|
||||
async def owner_resolved(self) -> Subject:
|
||||
return await (await self.data_resolved()).owner.resolve()
|
||||
|
||||
|
||||
class TransactionData(RecursiveMentionable, StaticMentionable):
|
||||
def __init__(
|
||||
self,
|
||||
in_coins: NullableReference[Stack[Coin]],
|
||||
out_coins: NullableReference[Stack[CoinData]],
|
||||
):
|
||||
assert isinstance(in_coins, NullableReference)
|
||||
assert isinstance(out_coins, NullableReference)
|
||||
self.in_coins = in_coins
|
||||
self.out_coins = out_coins
|
||||
self.hash_point = HashPoint.of(self)
|
||||
assert isinstance(self.hash_point, HashPoint)
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [*self.in_coins.points(), *self.out_coins.points()]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.in_coins) + bytes(self.out_coins)
|
||||
|
||||
@classmethod
|
||||
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], resolver),
|
||||
NullableReferenceFactory(
|
||||
StackFactory(CoinData.factory()).loose()
|
||||
).from_bytes(source[HashPoint.HASH_LENGTH:], resolver),
|
||||
)
|
||||
|
||||
async def _signature_verify(self, coin: Coin, signature: Signature) -> bool:
|
||||
assert isinstance(coin, Coin)
|
||||
assert isinstance(signature, Signature)
|
||||
assert_true(
|
||||
signature.verify(
|
||||
await coin.owner_resolved(),
|
||||
self.hash_point
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
async def _verify_signatures(
|
||||
self,
|
||||
signatures: NullableReference[Stack[Signature]]
|
||||
) -> bool:
|
||||
assert isinstance(signatures, NullableReference)
|
||||
assert_trues(
|
||||
await gather(
|
||||
*[
|
||||
self._signature_verify(coin, signature)
|
||||
for
|
||||
coin, signature
|
||||
in
|
||||
zip(
|
||||
await self.in_coins_resolved(),
|
||||
await Stack.list(signatures),
|
||||
strict=True
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
def iter_in_coins(self) -> AsyncIterable[HashPoint[Coin]]:
|
||||
return Stack.iter(self.in_coins)
|
||||
|
||||
async def in_coins_resolved(self) -> list[Coin]:
|
||||
return await Stack.list(self.in_coins)
|
||||
|
||||
async def _total_in(self) -> int:
|
||||
return await asum(
|
||||
coin.int_value() for coin in await self.in_coins_resolved()
|
||||
)
|
||||
|
||||
def iter_out_coins(self) -> AsyncIterable[HashPoint[CoinData]]:
|
||||
return Stack.iter(self.out_coins)
|
||||
|
||||
async def out_coins_resolved(self) -> list[CoinData]:
|
||||
return await Stack.list(self.out_coins)
|
||||
|
||||
async def _total_out(self) -> int:
|
||||
return await asum(
|
||||
coin.int_value() for coin in await self.out_coins_resolved()
|
||||
)
|
||||
|
||||
async def _verify_values(self, mint: int) -> bool:
|
||||
assert isinstance(mint, int)
|
||||
assert (await self.extra(mint)) >= 0
|
||||
return True
|
||||
|
||||
async def extra(self, mint: int) -> int:
|
||||
assert isinstance(mint, int)
|
||||
total_in, total_out = await gather(
|
||||
self._total_in(),
|
||||
self._total_out(),
|
||||
)
|
||||
assert isinstance(total_in, int)
|
||||
assert isinstance(total_out, int)
|
||||
return total_in + mint - total_out
|
||||
|
||||
async def verify(
|
||||
self,
|
||||
signatures: NullableReference[Stack[Signature]],
|
||||
mint: int
|
||||
) -> bool:
|
||||
assert isinstance(signatures, NullableReference)
|
||||
assert isinstance(mint, int)
|
||||
assert_trues(
|
||||
await gather(
|
||||
self._verify_signatures(signatures),
|
||||
self._verify_values(mint),
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
in_str, out_str = await gather(
|
||||
self.in_coins.str(tab),
|
||||
self.out_coins.str(tab),
|
||||
)
|
||||
assert isinstance(in_str, str)
|
||||
assert isinstance(out_str, str)
|
||||
return f'(in)' \
|
||||
f'{tabulate(tab)}{in_str}' \
|
||||
f'{tabulate(tab)}(out)' \
|
||||
f'{tabulate(tab)}{out_str}'
|
||||
|
||||
|
||||
class Transaction(RecursiveMentionable, StaticMentionable):
|
||||
def __init__(
|
||||
self,
|
||||
data: HashPoint[TransactionData],
|
||||
signatures: NullableReference[Stack[Signature]]
|
||||
):
|
||||
assert isinstance(data, HashPoint)
|
||||
assert isinstance(signatures, NullableReference)
|
||||
self.data = data
|
||||
self.signatures = signatures
|
||||
self.hash_point = HashPoint.of(self)
|
||||
assert isinstance(self.hash_point, HashPoint)
|
||||
|
||||
async def data_resolved(self) -> TransactionData:
|
||||
return await self.data.resolve()
|
||||
|
||||
def points(self) -> Iterable[HashPoint]:
|
||||
return [self.data, *self.signatures.points()]
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.data) + bytes(self.signatures)
|
||||
|
||||
@classmethod
|
||||
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(
|
||||
ResolverOrigin(TransactionData.factory(), source[:HashPoint.HASH_LENGTH], resolver).hash_point(),
|
||||
NullableReferenceFactory(stack_factory).from_bytes(source[HashPoint.HASH_LENGTH:], resolver),
|
||||
)
|
||||
|
||||
async def iter_coins(
|
||||
self,
|
||||
mint: int,
|
||||
miner: Nullable[HashPoint[Subject]]
|
||||
) -> AsyncIterable[tuple[Coin, Nullable[HashPoint[Subject]]]]:
|
||||
assert isinstance(mint, int)
|
||||
assert isinstance(miner, Nullable)
|
||||
transaction_data: TransactionData = await self.data_resolved()
|
||||
assert isinstance(transaction_data, TransactionData)
|
||||
index = 0
|
||||
out_coin: HashPoint[CoinData]
|
||||
async for out_coin in transaction_data.iter_out_coins():
|
||||
assert isinstance(out_coin, HashPoint)
|
||||
if miner.null():
|
||||
miner = NotNull((await out_coin.resolve()).owner)
|
||||
assert isinstance(miner, Nullable)
|
||||
coin: Coin = Coin(out_coin, self.hash_point, HashPoint.of(Integer(index)))
|
||||
assert isinstance(coin, Coin)
|
||||
yield coin, miner
|
||||
index += 1
|
||||
if not miner.null():
|
||||
coin: Coin = Coin(
|
||||
HashPoint.of(
|
||||
CoinData(
|
||||
miner.resolve(),
|
||||
HashPoint.of(Integer(await transaction_data.extra(mint)))
|
||||
)
|
||||
),
|
||||
self.hash_point,
|
||||
HashPoint.of(Integer(index))
|
||||
)
|
||||
assert isinstance(coin, Coin)
|
||||
yield coin, miner
|
||||
|
||||
async def coins(
|
||||
self,
|
||||
mint: int,
|
||||
miner: Nullable[HashPoint[Subject]]
|
||||
) -> list[Coin]:
|
||||
assert isinstance(mint, int)
|
||||
assert isinstance(miner, Nullable)
|
||||
return [coin async for coin, _ in self.iter_coins(mint, miner)]
|
||||
|
||||
async def verify(self, mint: int):
|
||||
assert isinstance(mint, int)
|
||||
data: TransactionData = await self.data_resolved()
|
||||
assert isinstance(data, TransactionData)
|
||||
assert_true(await data.verify(self.signatures, mint))
|
||||
return True
|
||||
|
||||
async def str(self, tab: int) -> str:
|
||||
assert isinstance(tab, int)
|
||||
data_str, signatures_str = await gather(
|
||||
hash_point_format(self.data, tab + 1),
|
||||
self.signatures.str(tab + 1),
|
||||
)
|
||||
assert isinstance(data_str, str)
|
||||
assert isinstance(signatures_str, str)
|
||||
return f'(' \
|
||||
f'{tabulate(tab + 1)}transaction' \
|
||||
f'{tabulate(tab + 1)}{data_str}' \
|
||||
f'{tabulate(tab + 1)}{signatures_str}' \
|
||||
f'{tabulate(tab)})'
|
||||
|
||||
@classmethod
|
||||
def make(
|
||||
cls,
|
||||
in_coins: list[Coin],
|
||||
out_coins: list[CoinData],
|
||||
keys: list[nacl.signing.SigningKey],
|
||||
) -> 'Transaction':
|
||||
assert isinstance(in_coins, list)
|
||||
assert isinstance(out_coins, list)
|
||||
assert isinstance(keys, list)
|
||||
transaction_data = TransactionData(
|
||||
Stack.off(Coin.factory(), reversed(in_coins)),
|
||||
Stack.off(CoinData.factory(), reversed(out_coins)),
|
||||
)
|
||||
assert isinstance(transaction_data, TransactionData)
|
||||
return Transaction(
|
||||
HashPoint.of(transaction_data),
|
||||
Stack.off(
|
||||
Signature.factory(),
|
||||
(Signature.sign(key, HashPoint.of(transaction_data)) for key in reversed(keys))
|
||||
)
|
||||
)
|
@ -1,9 +1,7 @@
|
||||
__all__ = (
|
||||
'WrisbtChainProtocol',
|
||||
'WrisbtParametres',
|
||||
'WrisbtRoot', 'WrisbtRootFactory',
|
||||
)
|
||||
|
||||
from .wrisbtchainprotocol import WrisbtChainProtocol
|
||||
from .wrisbtparametres import WrisbtParametres
|
||||
from .wrisbtroot import WrisbtRoot, WrisbtRootFactory
|
||||
|
@ -1,34 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.chain import *
|
||||
from rainbowadn.core import *
|
||||
from .wrisbtindex import *
|
||||
from .wrisbtprotocol import *
|
||||
|
||||
__all__ = ('WrisbtChainProtocol',)
|
||||
|
||||
TargetType = TypeVar('TargetType')
|
||||
|
||||
|
||||
class WrisbtChainProtocol(
|
||||
BlockChainProtocol[
|
||||
TargetType,
|
||||
WrisbtIndex
|
||||
],
|
||||
Generic[TargetType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
total_factory: RainbowFactory[TargetType],
|
||||
keymin: int
|
||||
):
|
||||
assert isinstance(total_factory, RainbowFactory)
|
||||
assert isinstance(keymin, int)
|
||||
assert keymin > 0
|
||||
self.keymin = keymin
|
||||
super().__init__(
|
||||
WrisbtProtocol(keymin),
|
||||
total_factory,
|
||||
WrisbtIndexFactory(keymin)
|
||||
)
|
||||
self.total_factory = total_factory
|
@ -1,55 +0,0 @@
|
||||
from typing import TypeVar
|
||||
|
||||
from rainbowadn.chain.states import *
|
||||
from rainbowadn.core import *
|
||||
from .wrisbtindex import *
|
||||
from .wrisbtparametres import *
|
||||
from .wrisbtroot import *
|
||||
|
||||
__all__ = ('WrisbtProtocol',)
|
||||
|
||||
TargetType = TypeVar('TargetType')
|
||||
|
||||
|
||||
class WrisbtProtocol(MetaReductionStateProtocol[TargetType, WrisbtIndex]):
|
||||
def __init__(self, keymin: int):
|
||||
assert isinstance(keymin, int)
|
||||
assert keymin > 0
|
||||
self.keymin = keymin
|
||||
|
||||
def _initial_state(self) -> HashPoint[WrisbtIndex]:
|
||||
return HashPoint.of(
|
||||
WrisbtIndex(
|
||||
HashPoint.of(WrisbtRoot.empty(WrisbtParametres(self.keymin, HashPoint.HASH_LENGTH))),
|
||||
HashPoint.of(WrisbtRoot.empty(WrisbtParametres(self.keymin, HashPoint.HASH_LENGTH))),
|
||||
self.keymin
|
||||
)
|
||||
)
|
||||
|
||||
async def _derive(
|
||||
self,
|
||||
previous: HashPoint[WrisbtIndex],
|
||||
header: HashPoint[TargetType]
|
||||
) -> HashPoint[WrisbtIndex]:
|
||||
assert isinstance(previous, HashPoint)
|
||||
assert isinstance(header, HashPoint)
|
||||
index: WrisbtIndex = await previous.resolve()
|
||||
assert isinstance(index, WrisbtIndex)
|
||||
|
||||
empty: WrisbtRoot = WrisbtRoot.empty(WrisbtParametres(self.keymin, HashPoint.HASH_LENGTH))
|
||||
assert isinstance(empty, WrisbtRoot)
|
||||
total: WrisbtRoot = await index.total.resolve()
|
||||
assert isinstance(total, WrisbtRoot)
|
||||
|
||||
total_index, delta_index = await gather(
|
||||
total.index(header, empty),
|
||||
empty.index(header, total),
|
||||
)
|
||||
|
||||
return HashPoint.of(
|
||||
WrisbtIndex(
|
||||
HashPoint.of(total_index),
|
||||
HashPoint.of(delta_index),
|
||||
index.keymin
|
||||
)
|
||||
)
|
134
trace_chain.py
134
trace_chain.py
@ -1,134 +0,0 @@
|
||||
import asyncio
|
||||
import random
|
||||
from contextlib import ExitStack
|
||||
|
||||
from nacl.signing import SigningKey
|
||||
|
||||
from plot import *
|
||||
from rainbowadn.chain import *
|
||||
from rainbowadn.collection.linear import *
|
||||
from rainbowadn.collection.trees.binary import *
|
||||
from rainbowadn.core import *
|
||||
from rainbowadn.instrument import *
|
||||
from rainbowadn.nullability import *
|
||||
from rainbowadn.testing.resolvers import *
|
||||
from rainbowadn.v13 import *
|
||||
from trace_common import *
|
||||
|
||||
|
||||
async def mock(bank: BankChain) -> BankChain:
|
||||
key_0 = SigningKey.generate()
|
||||
transaction_0 = Transaction.make(
|
||||
[],
|
||||
[CoinData.of(Subject(key_0.verify_key), 100_000)],
|
||||
[]
|
||||
)
|
||||
coin_0, coin_1 = await transaction_0.coins(MINT_CONST, NotNull(HashPoint.of(Subject(key_0.verify_key))))
|
||||
bank = await bank.adds(
|
||||
[
|
||||
transaction_0,
|
||||
Transaction.make(
|
||||
[coin_1],
|
||||
[CoinData.of(Subject(SigningKey.generate().verify_key), 10_000)],
|
||||
[key_0]
|
||||
),
|
||||
]
|
||||
)
|
||||
return bank
|
||||
|
||||
|
||||
def get_instrumentations() -> list[Instrumentation]:
|
||||
sleep_cc = Concurrency(DelayedResolver, 'sleep')
|
||||
return [
|
||||
sleep_cc,
|
||||
EntryExit(ActiveBinaryTree, 'add', sleep_cc.point),
|
||||
Concurrency(ActiveBinaryTree, 'add'),
|
||||
Concurrency(ActiveBinaryTree, 'contains'),
|
||||
Concurrency(Stack, 'list'),
|
||||
]
|
||||
|
||||
|
||||
async def _generate(
|
||||
blocks: int,
|
||||
subjects_min: int,
|
||||
subjects_max: int,
|
||||
transactions_min: int,
|
||||
transactions_max: int,
|
||||
) -> BankChain:
|
||||
bank: BankChain = BankChain.empty(ReductionChainMetaFactory().loose())
|
||||
# bank = await mock(bank)
|
||||
for _ in range(blocks):
|
||||
bank = await bank.adds(
|
||||
[
|
||||
Transaction.make(
|
||||
[],
|
||||
[CoinData.of(Subject(SigningKey.generate().verify_key), 0)] * random.randint(
|
||||
subjects_min,
|
||||
subjects_max
|
||||
),
|
||||
[]
|
||||
)
|
||||
for
|
||||
_
|
||||
in
|
||||
range(
|
||||
random.randint(
|
||||
transactions_min,
|
||||
transactions_max
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
print('generated')
|
||||
return bank
|
||||
|
||||
|
||||
async def _migrate(bank: BankChain) -> BankChain:
|
||||
assert_true(await bank.verify())
|
||||
bank = BankChain.from_reference(
|
||||
ReductionChainMetaFactory(), await get_dr().migrate_resolved(bank.reference)
|
||||
)
|
||||
print('migrated')
|
||||
return bank
|
||||
|
||||
|
||||
async def _instrument(bank: BankChain) -> list[Instrumentation]:
|
||||
with ExitStack() as estack:
|
||||
instrumentations: list[Instrumentation] = get_instrumentations()
|
||||
for stacked in instrumentations:
|
||||
stacked.enter(estack)
|
||||
assert_true(await bank.verify())
|
||||
print('deinstrumentation (should be empty):', Instrumentation.deinstrumentation)
|
||||
print('instrumented')
|
||||
return instrumentations
|
||||
|
||||
|
||||
async def _trace():
|
||||
set_gather_linear()
|
||||
bank = await _generate(
|
||||
16,
|
||||
8, 15,
|
||||
8, 15,
|
||||
)
|
||||
bank = await _migrate(bank)
|
||||
set_gather_asyncio()
|
||||
with DeintrumentationSize(Instrumentation, 'deinstrument'):
|
||||
with Counter(DeintrumentationSize, 'instrument') as de_ctr:
|
||||
instrumentations = await _instrument(bank)
|
||||
print(jsonify(de_ctr))
|
||||
print('traced')
|
||||
return instrumentations
|
||||
|
||||
|
||||
async def main():
|
||||
instrumentations = await _trace()
|
||||
fn = get_fn()
|
||||
jsonified = jsonify_list(instrumentations)
|
||||
dump(fn, jsonified)
|
||||
copy(fn)
|
||||
plot(fn)
|
||||
print('plotted')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
@ -10,7 +10,7 @@ from rainbowadn.core import *
|
||||
from rainbowadn.flow13 import *
|
||||
from rainbowadn.instrument import *
|
||||
from rainbowadn.testing.resolvers import *
|
||||
from rainbowadn.v13 import Subject
|
||||
from rainbowadn.v13 import *
|
||||
from trace_common import *
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user