rainbowadn/rainbowadn/chain/reduction/reductionchainprotocol.py
2022-07-10 21:14:02 +03:00

77 lines
2.4 KiB
Python

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]],
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)
reduction_factory: RainbowFactory[
Reducible[ReductorType, AccumulatorType]
] = ReducibleFactory(
reductor_factory, accumulator_factory
)
assert isinstance(reduction_factory, RainbowFactory)
stage_protocol: ActiveStageProtocol[
ReductorType,
AccumulatorType,
Reducible[ReductorType, AccumulatorType]
] = ReductionStageProtocol(protocol)
assert isinstance(stage_protocol, ActiveStageProtocol)
super().__init__(
ActiveStageStateProtocol(
stage_protocol,
reduction_factory,
accumulator_factory,
),
reductor_factory,
StateStageFactory(
stage_protocol,
reduction_factory,
accumulator_factory
)
)
self.reductor_factory = reductor_factory
self.accumulator_factory = accumulator_factory
def actual_state_factory(self) -> RainbowFactory[AccumulatorType]:
return self.accumulator_factory
async def actual_state(
self,
state: StateStage[
ReductorType,
AccumulatorType,
Reducible[ReductorType, AccumulatorType]
]
) -> HashPoint[AccumulatorType]:
assert isinstance(state, StateStage)
return state.state