rainbowadn/rainbowadn/v13/bankprotocol.py

60 lines
2.7 KiB
Python

from rainbowadn.chain.reduction.reduced import Reduced
from rainbowadn.chain.reduction.reduction import Reduction
from rainbowadn.chain.reduction.reductionprotocol import ReductionProtocol
from rainbowadn.chain.reduction.reductionresult import ReductionResult
from rainbowadn.data.atomic.integer import Integer
from rainbowadn.data.collection.keymetadata import KeyMetadataFactory
from rainbowadn.data.collection.stack.stack import Stack
from rainbowadn.data.collection.trees.binary.binarytree import BinaryTreeFactory
from rainbowadn.core.hashpoint import HashPoint
from rainbowadn.core.nullability.null import Null
from rainbowadn.core.nullability.nullablereference import NullableReference
from rainbowadn.core.rainbow_factory import RainbowFactory
from rainbowadn.core.static import StaticFactory
from rainbowadn.v13.bankstate import BankState
from rainbowadn.v13.subject import Subject
from rainbowadn.v13.transaction import Coin, Transaction
__all__ = ('BankProtocol',)
class BankProtocol(ReductionProtocol[NullableReference[Stack[Transaction]], BankState]):
def reduce(
self,
reduction: Reduction[NullableReference[Stack[Transaction]], BankState]
) -> ReductionResult[NullableReference[Stack[Transaction]], BankState]:
assert isinstance(reduction, Reduction)
bank_state: BankState = reduction.accumulator.resolve()
assert isinstance(bank_state, BankState)
reference: NullableReference[Stack[Transaction]] = reduction.reductor.resolve()
if reference.null():
return Reduced(HashPoint.of(bank_state.without_miner()))
else:
stack: Stack[Transaction] = reference.resolve()
assert isinstance(stack, Stack)
return Reduction(
HashPoint.of(stack.previous),
HashPoint.of(bank_state.push(stack.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))
)
)
def header_filter(self, state: HashPoint[BankState]) -> HashPoint[BankState]:
assert isinstance(state, HashPoint)
return HashPoint.of(state.resolve().without_miner().advance())