rainbowadn/rainbowadn/chain/reduction/reductionchainprotocol.py

79 lines
2.8 KiB
Python

from typing import TypeVar
from rainbowadn.chain.blockchainprotocol import BlockChainProtocol
from rainbowadn.chain.reduction.reduction import Reduction, ReductionFactory
from rainbowadn.chain.reduction.reductionprotocol import ReductionProtocol
from rainbowadn.chain.reduction.reductionstageprotocol import ReductionStageProtocol
from rainbowadn.chain.stages.derivation.activestageprotocol import ActiveStageProtocol
from rainbowadn.chain.stages.derivation.activestagestateprotocol import ActiveStageStateProtocol
from rainbowadn.chain.stages.stage import StateStage, StateStageFactory
from rainbowadn.core.hashpoint import HashPoint
from rainbowadn.core.rainbow_factory import RainbowFactory
__all__ = ('ReductionChainProtocol',)
ReductorType = TypeVar('ReductorType')
AccumulatorType = TypeVar('AccumulatorType')
class ReductionChainProtocol(
BlockChainProtocol[
ReductorType,
StateStage[ReductorType, AccumulatorType, Reduction[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[
Reduction[ReductorType, AccumulatorType]
] = ReductionFactory(
reductor_factory, accumulator_factory
)
assert isinstance(reduction_factory, RainbowFactory)
stage_protocol: ActiveStageProtocol[
ReductorType,
AccumulatorType,
Reduction[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
def actual_state(
self,
state: StateStage[
ReductorType,
AccumulatorType,
Reduction[ReductorType, AccumulatorType]
]
) -> HashPoint[AccumulatorType]:
assert isinstance(state, StateStage)
return state.state