From 9572cd7ee464dd155f5faa0f2650c14dac23dc40 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 26 Nov 2024 13:58:42 +0700 Subject: [PATCH] Create state_machine.py --- .../src/core/state_machine.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 QuantumNexusProtocol/src/core/state_machine.py diff --git a/QuantumNexusProtocol/src/core/state_machine.py b/QuantumNexusProtocol/src/core/state_machine.py new file mode 100644 index 000000000..c45ddae9e --- /dev/null +++ b/QuantumNexusProtocol/src/core/state_machine.py @@ -0,0 +1,61 @@ +class StateMachine: + def __init__(self): + self.state = 'INITIAL' # Initial state + self.transitions = { + 'INITIAL': ['WAITING_FOR_TRANSACTION'], + 'WAITING_FOR_TRANSACTION': ['PROCESSING_TRANSACTION', 'MINING'], + 'PROCESSING_TRANSACTION': ['WAITING_FOR_TRANSACTION', 'MINING'], + 'MINING': ['WAITING_FOR_TRANSACTION', 'FINALIZED'], + 'FINALIZED': ['INITIAL'] + } + + def transition(self, event): + """Handle state transitions based on events.""" + if self.state == 'INITIAL' and event == 'start': + self.state = 'WAITING_FOR_TRANSACTION' + elif self.state == 'WAITING_FOR_TRANSACTION' and event == 'transaction_received': + self.state = 'PROCESSING_TRANSACTION' + elif self.state == 'PROCESSING_TRANSACTION' and event == 'process_complete': + self.state = 'MINING' + elif self.state == 'MINING' and event == 'mining_complete': + self.state = 'FINALIZED' + elif self.state == 'FINALIZED' and event == 'reset': + self.state = 'INITIAL' + else: + raise Exception(f"Invalid transition from {self.state} on event {event}") + + def get_state(self): + """Return the current state.""" + return self.state + + def is_in_state(self, state): + """Check if the state machine is in a specific state.""" + return self.state == state + + def available_transitions(self): + """Return the available transitions from the current state.""" + return self.transitions.get(self.state, []) + +# Example usage +if __name__ == '__main__': + sm = StateMachine() + print(f"Initial State: {sm.get_state()}") + + # Simulate state transitions + try: + sm.transition('start') + print(f"State after starting: {sm.get_state()}") + + sm.transition('transaction_received') + print(f"State after transaction received: {sm.get_state()}") + + sm.transition('process_complete') + print(f"State after processing transaction: {sm.get_state()}") + + sm.transition('mining_complete') + print(f"State after mining: {sm.get_state()}") + + sm.transition('reset') + print(f"State after reset: {sm.get_state()}") + except Exception as e: + print(e)