From 92cc9f2ad577796104d18940608daa696412f001 Mon Sep 17 00:00:00 2001 From: Raycho Mukelov Date: Sun, 5 Nov 2023 20:38:56 +0200 Subject: [PATCH] fix error in Consensus FSM definition --- raft/consensus_state_machine.nim | 57 +++++++++++++------------- tests/test_consensus_state_machine.nim | 6 ++- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/raft/consensus_state_machine.nim b/raft/consensus_state_machine.nim index 5b2a6b8..c75808f 100644 --- a/raft/consensus_state_machine.nim +++ b/raft/consensus_state_machine.nim @@ -35,61 +35,62 @@ type # Define logical functions (conditions) computed from our NodeType etc. (Truth Table) LogicalConditionValueType* = bool - LogicalCondition*[NodeTytpe, RaftMessageBase] = - proc(node: NodeTytpe, msg: Option[RaftMessageBase]): LogicalConditionValueType - LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = - Table[(RaftNodeState, EventType), seq[LogicalCondition[NodeType, RaftMessageBase]]] + LogicalCondition*[NodeTytpe, RaftMessageType] = + proc(node: NodeTytpe, msg: Option[RaftMessageType]): LogicalConditionValueType + LogicalConditionsLut*[RaftNodeState, EventType, NodeType, RaftMessageType] = + 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) - TerminalSymbol*[EventType, NodeType, RaftMessageBase] = + TerminalSymbol*[EventType, NodeType, RaftMessageType] = (EventType, seq[LogicalConditionValueType]) # 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]) - StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageBase] = Table[ - (RaftNodeState, TerminalSymbol[NodeType, EventType, RaftMessageBase]), - (RaftNodeState, Option[ConsensusFsmTransitionActionType]) + StateTransitionsRulesLut*[RaftNodeState, EventType, NodeType, RaftMessageType] = Table[ + (RaftNodeState, TerminalSymbol[NodeType, EventType, RaftMessageType]), + (RaftNodeState, Option[ConsensusFsmTransitionActionType[NodeType]]) ] # FSM type definition - ConsensusFsm*[RaftNodeState, EventType, NodeType, RaftMessageBase] = ref object + ConsensusFsm*[RaftNodeState, EventType, NodeType, RaftMessageType] = ref object mtx: RLock state: RaftNodeState - logicalFunctionsLut: LogicalConditionsLut[RaftNodeState, EventType, NodeType, RaftMessageBase] - stateTransitionsLut: StateTransitionsRulesLut[RaftNodeState, EventType, NodeType, RaftMessageBase] + logicalFunctionsLut: LogicalConditionsLut[RaftNodeState, EventType, NodeType, RaftMessageType] + stateTransitionsLut: StateTransitionsRulesLut[RaftNodeState, EventType, NodeType, RaftMessageType] # FSM type constructor -proc new*[RaftNodeState, EventType, NodeType, RaftNodeStates]( - T: type ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], startSymbol: RaftNodeState = rnsFollower): T = +proc new*[RaftNodeState, EventType, NodeType, RaftMessageType]( + 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) result.state = startSymbol + debug "new: ", fsm=repr(result) -proc addFsmTransition*[RaftNodeState, EventType, NodeType, RaftMessageBase]( - fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], +proc addFsmTransition*[RaftNodeState, EventType, NodeType, RaftMessageType]( + fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType], fromState: RaftNodeState, - termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase], + termSymb: TerminalSymbol[EventType, NodeType, RaftMessageType], toState: RaftNodeState, action: Option[ConsensusFsmTransitionActionType]) = fsm.stateTransitionsLut[(fromState.state, termSymb)] = (toState, action) -proc addFsmTransitionLogicalConditions*[RaftNodeState, EventType, NodeType, RaftMessageBase]( - fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], +proc addFsmTransitionLogicalConditions*[RaftNodeState, EventType, NodeType, RaftMessageType]( + fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType], state: RaftNodeState, event: EventType, - logicalConditions: seq[LogicalCondition[NodeType, RaftMessageBase]]) = + logicalConditions: seq[LogicalCondition[NodeType, RaftMessageType]]) = fsm.logicalFunctionsLut[(state, event)] = logicalConditions -proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventType, RaftMessageBase]( - fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], +proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventType, RaftMessageType]( + fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType], node: NodeType, termSymb: TerminalSymbol, - msg: Option[RaftMessageBase]): TerminalSymbol = + msg: Option[RaftMessageType]): TerminalSymbol = let e = termSymb[0] @@ -110,11 +111,11 @@ proc computeFsmLogicFunctionsPermutationValuе*[RaftNodeState, NodeType, EventTy termSymb[1] = logicFunctionsConds result = termSymb -proc fsmAdvance*[RaftNodeState, EventType, NodeType, RaftMessageBase]( - fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageBase], +proc fsmAdvance*[RaftNodeState, EventType, NodeType, RaftMessageType]( + fsm: ConsensusFsm[RaftNodeState, EventType, NodeType, RaftMessageType], node: NodeType, - termSymb: TerminalSymbol[EventType, NodeType, RaftMessageBase], - msg: Option[RaftMessageBase]): RaftNodeState = + termSymb: TerminalSymbol[EventType, NodeType, RaftMessageType], + msg: Option[RaftMessageType]): RaftNodeState = withRLock(): var diff --git a/tests/test_consensus_state_machine.nim b/tests/test_consensus_state_machine.nim index f85008b..55e15a9 100644 --- a/tests/test_consensus_state_machine.nim +++ b/tests/test_consensus_state_machine.nim @@ -8,12 +8,14 @@ # those terms. import unittest2 +import ../raft/types import basic_state_machine import ../raft/consensus_state_machine -import ../raft/types suite "Create and test 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 \ No newline at end of file