56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from typing import TypeVar
|
|
|
|
from rainbowadn.chain.states import *
|
|
from rainbowadn.core import *
|
|
from .wrisbtindex import WrisbtIndex
|
|
from .wrisbtparametres import WrisbtParametres
|
|
from .wrisbtroot import WrisbtRoot
|
|
|
|
__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
|
|
)
|
|
)
|