fix error in Consensus FSM definition

This commit is contained in:
Raycho Mukelov 2023-11-05 20:38:56 +02:00
parent d5312ec40a
commit 92cc9f2ad5
2 changed files with 33 additions and 30 deletions

View File

@ -35,61 +35,62 @@ type
# Define logical functions (conditions) computed from our NodeType etc. (Truth Table) # Define logical functions (conditions) computed from our NodeType etc. (Truth Table)
LogicalConditionValueType* = bool LogicalConditionValueType* = bool
LogicalCondition*[NodeTytpe, RaftMessageBase] = LogicalCondition*[NodeTytpe, RaftMessageType] =
proc(node: NodeTytpe, msg: Option[RaftMessageBase]): LogicalConditionValueType proc(node: NodeTytpe, msg: Option[RaftMessageType]): LogicalConditionValueType
LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageType] =
Table[(RaftNodeState, EventType), seq[LogicalCondition[NodeType, RaftMessageBase]]] Table[(RaftNodeState, EventType), seq[LogicalCondition[NodeType, RaftMessageType]]]
# Define Terminals as a tuple of a Event and a sequence of logical functions (conditions) and their respective values computed from NodeType, NodeTytpe and RaftMessageBase # Define Terminals as a tuple of a Event and a sequence of logical functions (conditions) and their respective values computed from NodeType, NodeTytpe and RaftMessageType
# (kind of Truth Table) # (kind of Truth Table)
TerminalSymbol*[EventType, NodeType, RaftMessageBase] = TerminalSymbol*[EventType, NodeType, RaftMessageType] =
(EventType, seq[LogicalConditionValueType]) (EventType, seq[LogicalConditionValueType])
# Define State Transition Rules LUT of the form ( NonTerminal -> Terminal ) -> NonTerminal ) # Define State Transition Rules LUT of the form ( NonTerminal -> Terminal ) -> NonTerminal )
# NonTerminal is a NodeState and Terminal is a TerminalSymbol - the tuple (EventType, seq[LogicalConditionValueType]) # NonTerminal is a NodeState and Terminal is a TerminalSymbol - the tuple (EventType, seq[LogicalConditionValueType])
StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = Table[ StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageType] = Table[
(RaftNodeState, TerminalSymbol[NodeType, EventType, RaftMessageBase]), (RaftNodeState, TerminalSymbol[NodeType, EventType, RaftMessageType]),
(RaftNodeState, Option[ConsensusFsmTransitionActionType]) (RaftNodeState, Option[ConsensusFsmTransitionActionType[NodeType]])
] ]
# FSM type definition # FSM type definition
ConsensusFsm*[RaftNodeState, EventType, NodeType, RaftMessageBase] = ref object ConsensusFsm*[RaftNodeState, EventType, NodeType, RaftMessageType] = ref object
mtx: RLock mtx: RLock
state: RaftNodeState state: RaftNodeState
logicalFunctionsLut: LogicalConditionsLut[RaftNodeState, EventType, NodeType, RaftMessageBase] logicalFunctionsLut: LogicalConditionsLut[RaftNodeState, EventType, NodeType, RaftMessageType]
stateTransitionsLut: StateTransitionsRulesLut[RaftNodeState, EventType, NodeType, RaftMessageBase] stateTransitionsLut: StateTransitionsRulesLut[RaftNodeState, EventType, NodeType, RaftMessageType]
# FSM type constructor # FSM type constructor
proc new*[RaftNodeState, EventType, NodeType, RaftNodeStates]( proc new*[RaftNodeState, EventType, NodeType, RaftMessageType](
T: type ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], startSymbol: RaftNodeState = rnsFollower): T = T: type ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType], startSymbol: RaftNodeState = rnsFollower): T =
result = ConsensusFsm[NodeType, EventType, RaftNodeStates]() result = T(mtx: RLock(), state: startSymbol)
initRLock(result.mtx) initRLock(result.mtx)
result.state = startSymbol result.state = startSymbol
debug "new: ", fsm=repr(result) debug "new: ", fsm=repr(result)
proc addFsmTransition*[RaftNodeState, EventType, NodeType, RaftMessageBase]( proc addFsmTransition*[RaftNodeState, EventType, NodeType, RaftMessageType](
fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType],
fromState: RaftNodeState, fromState: RaftNodeState,
termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase], termSymb: TerminalSymbol[EventType, NodeType, RaftMessageType],
toState: RaftNodeState, toState: RaftNodeState,
action: Option[ConsensusFsmTransitionActionType]) = action: Option[ConsensusFsmTransitionActionType]) =
fsm.stateTransitionsLut[(fromState.state, termSymb)] = (toState, action) fsm.stateTransitionsLut[(fromState.state, termSymb)] = (toState, action)
proc addFsmTransitionLogicalConditions*[RaftNodeState, EventType, NodeType, RaftMessageBase]( proc addFsmTransitionLogicalConditions*[RaftNodeState, EventType, NodeType, RaftMessageType](
fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType],
state: RaftNodeState, state: RaftNodeState,
event: EventType, event: EventType,
logicalConditions: seq[LogicalCondition[NodeType, RaftMessageBase]]) = logicalConditions: seq[LogicalCondition[NodeType, RaftMessageType]]) =
fsm.logicalFunctionsLut[(state, event)] = logicalConditions fsm.logicalFunctionsLut[(state, event)] = logicalConditions
proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventType, RaftMessageBase]( proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventType, RaftMessageType](
fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType],
node: NodeType, node: NodeType,
termSymb: TerminalSymbol, termSymb: TerminalSymbol,
msg: Option[RaftMessageBase]): TerminalSymbol = msg: Option[RaftMessageType]): TerminalSymbol =
let let
e = termSymb[0] e = termSymb[0]
@ -110,11 +111,11 @@ proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventTy
termSymb[1] = logicFunctionsConds termSymb[1] = logicFunctionsConds
result = termSymb result = termSymb
proc fsmAdvance*[RaftNodeState, EventType, NodeType, RaftMessageBase]( proc fsmAdvance*[RaftNodeState, EventType, NodeType, RaftMessageType](
fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType],
node: NodeType, node: NodeType,
termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase], termSymb: TerminalSymbol[EventType, NodeType, RaftMessageType],
msg: Option[RaftMessageBase]): RaftNodeState = msg: Option[RaftMessageType]): RaftNodeState =
withRLock(): withRLock():
var var

View File

@ -8,12 +8,14 @@
# those terms. # those terms.
import unittest2 import unittest2
import ../raft/types
import basic_state_machine import basic_state_machine
import ../raft/consensus_state_machine import ../raft/consensus_state_machine
import ../raft/types
suite "Create and test Consensus State Machine": suite "Create and test Consensus State Machine":
test "Create Consensus State Machine": test "Create Consensus State Machine":
let csm = ConsensusFsm[RaftNodeState, EventType, RaftNode[SmState, SmCommand], RaftMessageBase].new() var
csm = ConsensusFsm[RaftNodeState, EventType, RaftNode[SmCommand, SmState], RaftMessageBase[SmCommand, SmState]].new(rnsFollower)
check csm != nil check csm != nil