From cb461cdfaf57271eabbec2070261582a0f893c69 Mon Sep 17 00:00:00 2001 From: x88 Date: Fri, 31 Jul 2020 14:25:52 +0300 Subject: [PATCH] multi: added strict fsm types, updated package paths, fixes --- client/client.go | 4 +- client/client_test.go | 8 +- client/state_test.go | 2 +- fsm/cmd/test/test.go | 4 +- fsm/fsm/fsm.go | 92 +++++++++++-------- fsm/fsm/fsm_machines_data_test.go | 65 ++++++------- fsm/fsm/fsm_machines_test.go | 74 ++++++++++----- fsm/fsm_pool/fsm_pool.go | 50 +++++----- fsm/fsm_pool/fsm_pool_test.go | 18 ++++ fsm/state_machines/provider.go | 18 ++-- .../signature_construct_fsm/init.go | 12 +-- .../signature_proposal_fsm/actions.go | 15 +-- .../signature_proposal_fsm/helpers.go | 4 +- .../signature_proposal_fsm/init.go | 40 ++++---- fsm/types/requests/signature_proposal.go | 2 +- go.mod | 3 +- go.sum | 6 +- main.go | 2 +- mocks/clientMocks/state_mock.go | 2 +- mocks/storageMocks/storage_mock.go | 2 +- 20 files changed, 233 insertions(+), 190 deletions(-) create mode 100644 fsm/fsm_pool/fsm_pool_test.go diff --git a/client/client.go b/client/client.go index 66407b4..ff89631 100644 --- a/client/client.go +++ b/client/client.go @@ -8,8 +8,8 @@ import ( "path/filepath" "time" - "github.com/p2p-org/dc4bc/qr" - "github.com/p2p-org/dc4bc/storage" + "github.com/depools/dc4bc/qr" + "github.com/depools/dc4bc/storage" ) const ( diff --git a/client/client_test.go b/client/client_test.go index e9eca34..d539894 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -9,13 +9,13 @@ import ( "testing" "time" - "github.com/p2p-org/dc4bc/mocks/qrMocks" + "github.com/depools/dc4bc/mocks/qrMocks" - "github.com/p2p-org/dc4bc/client" + "github.com/depools/dc4bc/client" + "github.com/depools/dc4bc/mocks/clientMocks" + "github.com/depools/dc4bc/mocks/storageMocks" "github.com/golang/mock/gomock" - "github.com/p2p-org/dc4bc/mocks/clientMocks" - "github.com/p2p-org/dc4bc/mocks/storageMocks" "github.com/stretchr/testify/require" ) diff --git a/client/state_test.go b/client/state_test.go index 62e8eb0..018752e 100644 --- a/client/state_test.go +++ b/client/state_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/p2p-org/dc4bc/client" + "github.com/depools/dc4bc/client" "github.com/stretchr/testify/require" ) diff --git a/fsm/cmd/test/test.go b/fsm/cmd/test/test.go index 54f9f8d..2379dd3 100644 --- a/fsm/cmd/test/test.go +++ b/fsm/cmd/test/test.go @@ -1,8 +1,8 @@ package main import ( - "github.com/p2p-org/dc4bc/fsm/state_machines" - "github.com/p2p-org/dc4bc/fsm/types/requests" + "github.com/depools/dc4bc/fsm/state_machines" + "github.com/depools/dc4bc/fsm/types/requests" "log" ) diff --git a/fsm/fsm/fsm.go b/fsm/fsm/fsm.go index 94dfb50..bf0ddda 100644 --- a/fsm/fsm/fsm.go +++ b/fsm/fsm/fsm.go @@ -19,33 +19,45 @@ import ( // Temporary global finish state for deprecating operations const ( - StateGlobalIdle = "__idle" - StateGlobalDone = "__done" + StateGlobalIdle = State("__idle") + StateGlobalDone = State("__done") ) -// FSMResponse returns result for processing with clientMocks events -type FSMResponse struct { +type State string + +func (s *State) String() string { + return string(*s) +} + +type Event string + +func (e *Event) String() string { + return string(*e) +} + +// Response returns result for processing with clientMocks events +type Response struct { // Returns machine execution result state - State string + State State // Must be cast, according to mapper event_name->response_type Data interface{} } type FSM struct { name string - initialState string - currentState string + initialState State + currentState State // May be mapping must require pair source + event? transitions map[trKey]*trEvent callbacks Callbacks - initialEvent string + initialEvent Event // Finish states, for switch machine or fin, // These states cannot be linked as SrcState in this machine - finStates map[string]bool + finStates map[State]bool // stateMu guards access to the currentState state. stateMu sync.RWMutex @@ -55,36 +67,36 @@ type FSM struct { // Transition key source + dst type trKey struct { - source string - event string + source State + event Event } // Transition lightweight event description type trEvent struct { - dstState string + dstState State isInternal bool } -type Event struct { - Name string +type EventDesc struct { + Name Event - SrcState []string + SrcState []State // Dst state changes after callback - DstState string + DstState State // Internal events, cannot be emitted from external call IsInternal bool } -type Callback func(event string, args ...interface{}) (interface{}, error) +type Callback func(event Event, args ...interface{}) (interface{}, error) -type Callbacks map[string]Callback +type Callbacks map[Event]Callback // TODO: Exports -func MustNewFSM(machineName, initialState string, events []Event, callbacks map[string]Callback) *FSM { +func MustNewFSM(machineName string, initialState State, events []EventDesc, callbacks Callbacks) *FSM { machineName = strings.TrimSpace(machineName) - initialState = strings.TrimSpace(initialState) + initialState = State(strings.TrimSpace(initialState.String())) if machineName == "" { panic("machine name cannot be empty") @@ -104,20 +116,20 @@ func MustNewFSM(machineName, initialState string, events []Event, callbacks map[ currentState: initialState, initialState: initialState, transitions: make(map[trKey]*trEvent), - finStates: make(map[string]bool), - callbacks: make(map[string]Callback), + finStates: make(map[State]bool), + callbacks: make(map[Event]Callback), } - allEvents := make(map[string]bool) + allEvents := make(map[Event]bool) // Required for find finStates - allSources := make(map[string]bool) - allStates := make(map[string]bool) + allSources := make(map[State]bool) + allStates := make(map[State]bool) // Validate events for _, event := range events { - event.Name = strings.TrimSpace(event.Name) - event.DstState = strings.TrimSpace(event.DstState) + event.Name = Event(strings.TrimSpace(event.Name.String())) + event.DstState = State(strings.TrimSpace(event.DstState.String())) if event.Name == "" { panic("cannot init empty event") @@ -137,7 +149,7 @@ func MustNewFSM(machineName, initialState string, events []Event, callbacks map[ trimmedSourcesCounter := 0 for _, sourceState := range event.SrcState { - sourceState := strings.TrimSpace(sourceState) + sourceState := State(strings.TrimSpace(sourceState.String())) if sourceState == "" { continue @@ -209,7 +221,7 @@ func MustNewFSM(machineName, initialState string, events []Event, callbacks map[ return f } -func (f *FSM) Do(event string, args ...interface{}) (resp *FSMResponse, err error) { +func (f *FSM) Do(event Event, args ...interface{}) (resp *Response, err error) { f.eventMu.Lock() defer f.eventMu.Unlock() @@ -221,7 +233,7 @@ func (f *FSM) Do(event string, args ...interface{}) (resp *FSMResponse, err erro return nil, errors.New("event is internal") } - resp = &FSMResponse{ + resp = &Response{ State: f.State(), } @@ -238,7 +250,7 @@ func (f *FSM) Do(event string, args ...interface{}) (resp *FSMResponse, err erro } // State returns the currentState state of the FSM. -func (f *FSM) State() string { +func (f *FSM) State() State { f.stateMu.RLock() defer f.stateMu.RUnlock() return f.currentState @@ -246,7 +258,7 @@ func (f *FSM) State() string { // setState allows the user to move to the given state from currentState state. // The call does not trigger any callbacks, if defined. -func (f *FSM) setState(event string) error { +func (f *FSM) setState(event Event) error { f.stateMu.Lock() defer f.stateMu.Unlock() @@ -264,12 +276,12 @@ func (f *FSM) Name() string { return f.name } -func (f *FSM) InitialState() string { +func (f *FSM) InitialState() State { return f.initialState } // Check entry event for available emitting as global entry event -func (f *FSM) GlobalInitialEvent() (event string) { +func (f *FSM) GlobalInitialEvent() (event Event) { if initialEvent, exists := f.transitions[trKey{StateGlobalIdle, f.initialEvent}]; exists { if !initialEvent.isInternal { event = f.initialEvent @@ -278,7 +290,7 @@ func (f *FSM) GlobalInitialEvent() (event string) { return } -func (f *FSM) EntryEvent() (event string) { +func (f *FSM) EntryEvent() (event Event) { if entryEvent, exists := f.transitions[trKey{f.initialState, f.initialEvent}]; exists { if !entryEvent.isInternal { event = f.initialEvent @@ -287,8 +299,8 @@ func (f *FSM) EntryEvent() (event string) { return } -func (f *FSM) EventsList() (events []string) { - var eventsMap = map[string]bool{} +func (f *FSM) EventsList() (events []Event) { + var eventsMap = map[Event]bool{} if len(f.transitions) > 0 { for trKey, trEvent := range f.transitions { if !trEvent.isInternal { @@ -310,8 +322,8 @@ func (f *FSM) EventsList() (events []string) { return } -func (f *FSM) StatesSourcesList() (states []string) { - var allStates = map[string]bool{} +func (f *FSM) StatesSourcesList() (states []State) { + var allStates = map[State]bool{} if len(f.transitions) > 0 { for trKey, _ := range f.transitions { allStates[trKey.source] = true @@ -327,7 +339,7 @@ func (f *FSM) StatesSourcesList() (states []string) { return } -func (f *FSM) IsFinState(state string) bool { +func (f *FSM) IsFinState(state State) bool { _, exists := f.finStates[state] return exists } diff --git a/fsm/fsm/fsm_machines_data_test.go b/fsm/fsm/fsm_machines_data_test.go index ea3821e..46dc196 100644 --- a/fsm/fsm/fsm_machines_data_test.go +++ b/fsm/fsm/fsm_machines_data_test.go @@ -1,77 +1,66 @@ package fsm +type testMachineFSM struct { + *FSM +} + const ( FSM1Name = "fsm1" // Init process from global idle state FSM1StateInit = StateGlobalIdle // Set up data - FSM1StateStage1 = "state_fsm1_stage1" + FSM1StateStage1 = State("state_fsm1_stage1") // Process data - FSM1StateStage2 = "state_fsm1_stage2" + FSM1StateStage2 = State("state_fsm1_stage2") // Cancelled with internal event - FSM1StateCanceledByInternal = "state_fsm1_canceled1" + FSM1StateCanceledByInternal = State("state_fsm1_canceled") // Cancelled with external event - FSM1StateCanceled2 = "state_fsm1_canceled2" + FSM1StateCanceled2 = State("state_fsm1_canceled2") // Out endpoint to switch - FSM1StateOutToFSM2 = "state_fsm1_out_to_fsm2" - FSM1StateOutToFSM3 = "state_fsm1_out_to_fsm3" + FSM1StateOutToFSM2 = State("state_fsm1_out_to_fsm2") + FSM1StateOutToFSM3 = State("state_fsm1_out_to_fsm3") // Events - EventFSM1Init = "event_fsm1_init" - EventFSM1Cancel = "event_fsm1_cancel" - EventFSM1Process = "event_fsm1_process" + EventFSM1Init = Event("event_fsm1_init") + EventFSM1Cancel = Event("event_fsm1_cancel") + EventFSM1Process = Event("event_fsm1_process") // Internal events - EventFSM1Internal = "event_internal_fsm1" - EventFSM1CancelByInternal = "event_internal_fsm1_cancel" - EventFSM1InternalOut2 = "event_internal_fsm1_out" + EventFSM1Internal = Event("event_internal_fsm1") + EventFSM1CancelByInternal = Event("event_internal_fsm1_cancel") + EventFSM1InternalOut2 = Event("event_internal_fsm1_out") ) var ( - testingEvents = []Event{ + testing1Events = []EventDesc{ // Init - {Name: EventFSM1Init, SrcState: []string{FSM1StateInit}, DstState: FSM1StateStage1}, - {Name: EventFSM1Internal, SrcState: []string{FSM1StateStage1}, DstState: FSM1StateStage2, IsInternal: true}, + {Name: EventFSM1Init, SrcState: []State{FSM2StateInit}, DstState: FSM1StateStage1}, + {Name: EventFSM1Internal, SrcState: []State{FSM1StateStage1}, DstState: FSM1StateStage2, IsInternal: true}, // Cancellation events - {Name: EventFSM1CancelByInternal, SrcState: []string{FSM1StateStage2}, DstState: FSM1StateCanceledByInternal, IsInternal: true}, - {Name: EventFSM1Cancel, SrcState: []string{FSM1StateStage2}, DstState: FSM1StateCanceled2}, + {Name: EventFSM1CancelByInternal, SrcState: []State{FSM1StateStage2}, DstState: FSM1StateCanceledByInternal, IsInternal: true}, + {Name: EventFSM1Cancel, SrcState: []State{FSM1StateStage2}, DstState: FSM1StateCanceled2}, // Out - {Name: EventFSM1Process, SrcState: []string{FSM1StateStage2}, DstState: FSM1StateOutToFSM2}, - {Name: EventFSM1InternalOut2, SrcState: []string{FSM1StateStage2}, DstState: FSM1StateOutToFSM3, IsInternal: true}, + {Name: EventFSM1Process, SrcState: []State{FSM1StateStage2}, DstState: FSM1StateOutToFSM2}, + {Name: EventFSM1InternalOut2, SrcState: []State{FSM1StateStage2}, DstState: FSM1StateOutToFSM3, IsInternal: true}, } - testingCallbacks = Callbacks{ + testing1Callbacks = Callbacks{ EventFSM1Init: actionSetUpData, EventFSM1InternalOut2: actionEmitOut2, EventFSM1Process: actionProcessData, } ) -type testMachineFSM struct { - *FSM -} - -/*func new() fsm_pool.IStateMachine { - machine := &testMachineFSM{} - machine.FSM = MustNewFSM( - FSM1Name, - FSM1StateInit, - testingEvents, - testingCallbacks, - ) - return machine -}*/ - -func actionSetUpData(event string, args ...interface{}) (response interface{}, err error) { +func actionSetUpData(event Event, args ...interface{}) (response interface{}, err error) { return } -func actionProcessData(event string, args ...interface{}) (response interface{}, err error) { +func actionProcessData(event Event, args ...interface{}) (response interface{}, err error) { return } -func actionEmitOut2(event string, args ...interface{}) (response interface{}, err error) { +func actionEmitOut2(event Event, args ...interface{}) (response interface{}, err error) { return } diff --git a/fsm/fsm/fsm_machines_test.go b/fsm/fsm/fsm_machines_test.go index fa71f90..55b0ebd 100644 --- a/fsm/fsm/fsm_machines_test.go +++ b/fsm/fsm/fsm_machines_test.go @@ -1,7 +1,6 @@ package fsm import ( - "log" "testing" ) @@ -11,8 +10,8 @@ func init() { testingFSM = MustNewFSM( FSM1Name, FSM1StateInit, - testingEvents, - testingCallbacks, + testing1Events, + testing1Callbacks, ) } @@ -29,12 +28,38 @@ func compareRecoverStr(t *testing.T, r interface{}, assertion string) { } } -func compareArrays(src, dst []string) bool { +func compareStatesArr(src, dst []State) bool { if len(src) != len(dst) { return false } // create a map of string -> int - diff := make(map[string]int, len(src)) + diff := make(map[State]int, len(src)) + for _, _x := range src { + // 0 value for int is 0, so just increment a counter for the string + diff[_x]++ + } + for _, _y := range dst { + // If the string _y is not in diff bail out early + if _, ok := diff[_y]; !ok { + return false + } + diff[_y] -= 1 + if diff[_y] == 0 { + delete(diff, _y) + } + } + if len(diff) == 0 { + return true + } + return false +} + +func compareEventsArr(src, dst []Event) bool { + if len(src) != len(dst) { + return false + } + // create a map of string -> int + diff := make(map[Event]int, len(src)) for _, _x := range src { // 0 value for int is 0, so just increment a counter for the string diff[_x]++ @@ -62,7 +87,7 @@ func TestMustNewFSM_Empty_Name_Panic(t *testing.T) { testingFSM = MustNewFSM( "", "init_state", - []Event{}, + []EventDesc{}, nil, ) @@ -77,7 +102,7 @@ func TestMustNewFSM_Empty_Initial_State_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "", - []Event{}, + []EventDesc{}, nil, ) @@ -92,7 +117,7 @@ func TestMustNewFSM_Empty_Events_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{}, + []EventDesc{}, nil, ) @@ -107,8 +132,8 @@ func TestMustNewFSM_Event_Empty_Name_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{ - {Name: "", SrcState: []string{"init_state"}, DstState: StateGlobalDone}, + []EventDesc{ + {Name: "", SrcState: []State{"init_state"}, DstState: StateGlobalDone}, }, nil, ) @@ -124,8 +149,8 @@ func TestMustNewFSM_Event_Empty_Source_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{ - {Name: "event", SrcState: []string{}, DstState: StateGlobalDone}, + []EventDesc{ + {Name: "event", SrcState: []State{}, DstState: StateGlobalDone}, }, nil, ) @@ -141,8 +166,8 @@ func TestMustNewFSM_States_Min_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{ - {Name: "event", SrcState: []string{"init_state"}, DstState: StateGlobalDone}, + []EventDesc{ + {Name: "event", SrcState: []State{"init_state"}, DstState: StateGlobalDone}, }, nil, ) @@ -158,9 +183,9 @@ func TestMustNewFSM_State_Entry_Conflict_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{ - {Name: "event1", SrcState: []string{"init_state"}, DstState: "state"}, - {Name: "event2", SrcState: []string{"init_state"}, DstState: "state"}, + []EventDesc{ + {Name: "event1", SrcState: []State{"init_state"}, DstState: "state"}, + {Name: "event2", SrcState: []State{"init_state"}, DstState: "state"}, }, nil, ) @@ -176,9 +201,9 @@ func TestMustNewFSM_State_Final_Not_Found_Panic(t *testing.T) { testingFSM = MustNewFSM( "fsm", "init_state", - []Event{ - {Name: "event1", SrcState: []string{"init_state"}, DstState: "state2"}, - {Name: "event2", SrcState: []string{"state2"}, DstState: "init_state"}, + []EventDesc{ + {Name: "event1", SrcState: []State{"init_state"}, DstState: "state2"}, + {Name: "event2", SrcState: []State{"state2"}, DstState: "init_state"}, }, nil, ) @@ -199,27 +224,26 @@ func TestFSM_EntryEvent(t *testing.T) { } func TestFSM_EventsList(t *testing.T) { - eventsList := []string{ + eventsList := []Event{ EventFSM1Init, EventFSM1Cancel, EventFSM1Process, } - if !compareArrays(testingFSM.EventsList(), eventsList) { + if !compareEventsArr(testingFSM.EventsList(), eventsList) { t.Error("expected public events", eventsList) } } func TestFSM_StatesList(t *testing.T) { - log.Println(testingFSM.StatesSourcesList()) - statesList := []string{ + statesList := []State{ FSM1StateInit, FSM1StateStage1, FSM1StateStage2, } - if !compareArrays(testingFSM.StatesSourcesList(), statesList) { + if !compareStatesArr(testingFSM.StatesSourcesList(), statesList) { t.Error("expected states", statesList) } } diff --git a/fsm/fsm_pool/fsm_pool.go b/fsm/fsm_pool/fsm_pool.go index 3b24ec5..bba34b4 100644 --- a/fsm/fsm_pool/fsm_pool.go +++ b/fsm/fsm_pool/fsm_pool.go @@ -2,53 +2,55 @@ package fsm_pool import ( "errors" - "github.com/p2p-org/dc4bc/fsm/fsm" + "github.com/depools/dc4bc/fsm/fsm" ) -type IStateMachine interface { +type MachineProvider interface { // Returns machine state from scope dump // For nil argument returns fsm with process initiation - // Get() IStateMachine + // Get() MachineProvider Name() string - InitialState() string + InitialState() fsm.State // Process event - Do(event string, args ...interface{}) (*fsm.FSMResponse, error) + Do(event fsm.Event, args ...interface{}) (*fsm.Response, error) - GlobalInitialEvent() string + GlobalInitialEvent() fsm.Event - EventsList() []string + EventsList() []fsm.Event - StatesSourcesList() []string + StatesSourcesList() []fsm.State - IsFinState(state string) bool + IsFinState(state fsm.State) bool } -type FSMMapper map[string]IStateMachine +type FSMMapper map[string]MachineProvider -type FSMRouteMapper map[string]string +type FSMEventsMapper map[fsm.Event]string -type FSMPoolProvider struct { - fsmInitialEvent string +type FSMStatesMapper map[fsm.State]string + +type FSMPool struct { + fsmInitialEvent fsm.Event // Pool mapper by names mapper FSMMapper - events FSMRouteMapper - states FSMRouteMapper + events FSMEventsMapper + states FSMStatesMapper } -func Init(machines ...IStateMachine) *FSMPoolProvider { +func Init(machines ...MachineProvider) *FSMPool { if len(machines) == 0 { panic("cannot initialize empty pool") } - p := &FSMPoolProvider{ + p := &FSMPool{ mapper: make(FSMMapper), - events: make(FSMRouteMapper), - states: make(FSMRouteMapper), + events: make(FSMEventsMapper), + states: make(FSMStatesMapper), } - allInitStatesMap := make(map[string]string) + allInitStatesMap := make(map[fsm.State]string) // Fill up mapper for _, machine := range machines { @@ -98,7 +100,7 @@ func Init(machines ...IStateMachine) *FSMPoolProvider { if machine.IsFinState(state) { // If state is initial for another machine, if initMachineName, exists := allInitStatesMap[state]; exists { - p.states[allInitStatesMap[state]] = initMachineName + p.states[state] = initMachineName continue } } @@ -116,7 +118,7 @@ func Init(machines ...IStateMachine) *FSMPoolProvider { return p } -func (p *FSMPoolProvider) EntryPointMachine() (IStateMachine, error) { +func (p *FSMPool) EntryPointMachine() (MachineProvider, error) { // StateGlobalIdle // TODO: Short code entryStateMachineName := p.events[p.fsmInitialEvent] @@ -129,7 +131,7 @@ func (p *FSMPoolProvider) EntryPointMachine() (IStateMachine, error) { return machine, nil } -func (p *FSMPoolProvider) MachineByEvent(event string) (IStateMachine, error) { +func (p *FSMPool) MachineByEvent(event fsm.Event) (MachineProvider, error) { eventMachineName := p.events[event] machine, exists := p.mapper[eventMachineName] @@ -139,7 +141,7 @@ func (p *FSMPoolProvider) MachineByEvent(event string) (IStateMachine, error) { return machine, nil } -func (p *FSMPoolProvider) MachineByState(state string) (IStateMachine, error) { +func (p *FSMPool) MachineByState(state fsm.State) (MachineProvider, error) { eventMachineName := p.states[state] machine, exists := p.mapper[eventMachineName] diff --git a/fsm/fsm_pool/fsm_pool_test.go b/fsm/fsm_pool/fsm_pool_test.go new file mode 100644 index 0000000..c60acb2 --- /dev/null +++ b/fsm/fsm_pool/fsm_pool_test.go @@ -0,0 +1,18 @@ +package fsm_pool + +/* +import ( + "testing" +) + +var ( + +) + +func init() { + +} + +func Test_InitPool(t *testing.T) { + +} */ diff --git a/fsm/state_machines/provider.go b/fsm/state_machines/provider.go index c029694..185bbf0 100644 --- a/fsm/state_machines/provider.go +++ b/fsm/state_machines/provider.go @@ -3,27 +3,27 @@ package state_machines import ( "encoding/json" "errors" - "github.com/p2p-org/dc4bc/fsm/fsm" - "github.com/p2p-org/dc4bc/fsm/fsm_pool" - "github.com/p2p-org/dc4bc/fsm/state_machines/internal" - "github.com/p2p-org/dc4bc/fsm/state_machines/signature_construct_fsm" - "github.com/p2p-org/dc4bc/fsm/state_machines/signature_proposal_fsm" + "github.com/depools/dc4bc/fsm/fsm" + "github.com/depools/dc4bc/fsm/fsm_pool" + "github.com/depools/dc4bc/fsm/state_machines/internal" + "github.com/depools/dc4bc/fsm/state_machines/signature_construct_fsm" + "github.com/depools/dc4bc/fsm/state_machines/signature_proposal_fsm" ) // Is machine state scope dump will be locked? type FSMDump struct { Id string - State string + State fsm.State Payload internal.MachineStatePayload } type FSMInstance struct { - machine fsm_pool.IStateMachine + machine fsm_pool.MachineProvider dump *FSMDump } var ( - fsmPoolProvider *fsm_pool.FSMPoolProvider + fsmPoolProvider *fsm_pool.FSMPool ) func init() { @@ -52,7 +52,7 @@ func New(data []byte) (*FSMInstance, error) { return i, err } -func (i *FSMInstance) Do(event string, args ...interface{}) (*fsm.FSMResponse, []byte, error) { +func (i *FSMInstance) Do(event fsm.Event, args ...interface{}) (*fsm.Response, []byte, error) { // Provide payload as first argument ever result, err := i.machine.Do(event, append([]interface{}{i.dump.Payload}, args...)...) diff --git a/fsm/state_machines/signature_construct_fsm/init.go b/fsm/state_machines/signature_construct_fsm/init.go index 5f80f06..b8c3bbc 100644 --- a/fsm/state_machines/signature_construct_fsm/init.go +++ b/fsm/state_machines/signature_construct_fsm/init.go @@ -1,8 +1,8 @@ package signature_construct_fsm import ( - "github.com/p2p-org/dc4bc/fsm/fsm" - "github.com/p2p-org/dc4bc/fsm/fsm_pool" + "github.com/depools/dc4bc/fsm/fsm" + "github.com/depools/dc4bc/fsm/fsm_pool" ) const ( @@ -19,18 +19,18 @@ type SignatureConstructFSM struct { *fsm.FSM } -func New() fsm_pool.IStateMachine { +func New() fsm_pool.MachineProvider { machine := &SignatureConstructFSM{} machine.FSM = fsm.MustNewFSM( fsmName, stateConstructorEntryPoint, - []fsm.Event{ + []fsm.EventDesc{ // {Name: "", SrcState: []string{""}, DstState: ""}, // Init - {Name: eventInitSignatureConstructor, SrcState: []string{stateConstructorEntryPoint}, DstState: awaitConstructor}, - {Name: eventInitSignatureFinishTmp, SrcState: []string{awaitConstructor}, DstState: "dkg_proposal_fsm"}, + {Name: eventInitSignatureConstructor, SrcState: []fsm.State{stateConstructorEntryPoint}, DstState: awaitConstructor}, + {Name: eventInitSignatureFinishTmp, SrcState: []fsm.State{awaitConstructor}, DstState: "dkg_proposal_fsm"}, }, fsm.Callbacks{}, ) diff --git a/fsm/state_machines/signature_proposal_fsm/actions.go b/fsm/state_machines/signature_proposal_fsm/actions.go index aba5f4b..0a1d272 100644 --- a/fsm/state_machines/signature_proposal_fsm/actions.go +++ b/fsm/state_machines/signature_proposal_fsm/actions.go @@ -2,15 +2,16 @@ package signature_proposal_fsm import ( "errors" - "github.com/p2p-org/dc4bc/fsm/state_machines/internal" - "github.com/p2p-org/dc4bc/fsm/types/requests" - "github.com/p2p-org/dc4bc/fsm/types/responses" + "github.com/depools/dc4bc/fsm/fsm" + "github.com/depools/dc4bc/fsm/state_machines/internal" + "github.com/depools/dc4bc/fsm/types/requests" + "github.com/depools/dc4bc/fsm/types/responses" "log" ) // init -> awaitingConfirmations // args: payload, signing id, participants list -func (s *SignatureProposalFSM) actionInitProposal(event string, args ...interface{}) (response interface{}, err error) { +func (s *SignatureProposalFSM) actionInitProposal(event fsm.Event, args ...interface{}) (response interface{}, err error) { var payload internal.MachineStatePayload // Init proposal log.Println("I'm actionInitProposal") @@ -82,17 +83,17 @@ func (s *SignatureProposalFSM) actionInitProposal(event string, args ...interfac } // -func (s *SignatureProposalFSM) actionConfirmProposalByParticipant(event string, args ...interface{}) (response interface{}, err error) { +func (s *SignatureProposalFSM) actionConfirmProposalByParticipant(event fsm.Event, args ...interface{}) (response interface{}, err error) { log.Println("I'm actionConfirmProposalByParticipant") return } -func (s *SignatureProposalFSM) actionDeclineProposalByParticipant(event string, args ...interface{}) (response interface{}, err error) { +func (s *SignatureProposalFSM) actionDeclineProposalByParticipant(event fsm.Event, args ...interface{}) (response interface{}, err error) { log.Println("I'm actionDeclineProposalByParticipant") return } -func (s *SignatureProposalFSM) actionValidateProposal(event string, args ...interface{}) (response interface{}, err error) { +func (s *SignatureProposalFSM) actionValidateProposal(event fsm.Event, args ...interface{}) (response interface{}, err error) { log.Println("I'm actionValidateProposal") return } diff --git a/fsm/state_machines/signature_proposal_fsm/helpers.go b/fsm/state_machines/signature_proposal_fsm/helpers.go index 2da1020..452a7ee 100644 --- a/fsm/state_machines/signature_proposal_fsm/helpers.go +++ b/fsm/state_machines/signature_proposal_fsm/helpers.go @@ -3,8 +3,8 @@ package signature_proposal_fsm import ( "crypto/sha256" "encoding/base64" - "github.com/p2p-org/dc4bc/fsm/state_machines/internal" - "github.com/p2p-org/dc4bc/fsm/types/responses" + "github.com/depools/dc4bc/fsm/state_machines/internal" + "github.com/depools/dc4bc/fsm/types/responses" "math/rand" ) diff --git a/fsm/state_machines/signature_proposal_fsm/init.go b/fsm/state_machines/signature_proposal_fsm/init.go index 4ab0d18..c245adc 100644 --- a/fsm/state_machines/signature_proposal_fsm/init.go +++ b/fsm/state_machines/signature_proposal_fsm/init.go @@ -1,61 +1,61 @@ package signature_proposal_fsm import ( - "github.com/p2p-org/dc4bc/fsm/fsm" - "github.com/p2p-org/dc4bc/fsm/fsm_pool" + "github.com/depools/dc4bc/fsm/fsm" + "github.com/depools/dc4bc/fsm/fsm_pool" ) const ( fsmName = "signature_proposal_fsm" signingIdLen = 32 - stateAwaitProposalConfirmation = "validate_proposal" // waiting participants + stateAwaitProposalConfirmation = fsm.State("validate_proposal") // waiting participants - stateValidationCanceledByParticipant = "validation_canceled_by_participant" - stateValidationCanceledByTimeout = "validation_canceled_by_timeout" + stateValidationCanceledByParticipant = fsm.State("validation_canceled_by_participant") + stateValidationCanceledByTimeout = fsm.State("validation_canceled_by_timeout") stateProposed = "proposed" - eventInitProposal = "proposal_init" - eventConfirmProposal = "proposal_confirm_by_participant" - eventDeclineProposal = "proposal_decline_by_participant" - eventValidateProposal = "proposal_validate" - eventSetProposalValidated = "proposal_set_validated" + eventInitProposal = fsm.Event("proposal_init") + eventConfirmProposal = fsm.Event("proposal_confirm_by_participant") + eventDeclineProposal = fsm.Event("proposal_decline_by_participant") + eventValidateProposal = fsm.Event("proposal_validate") + eventSetProposalValidated = fsm.Event("proposal_set_validated") - eventSetValidationCanceledByTimeout = "proposal_canceled_timeout" - eventSwitchProposedToSigning = "switch_state_to_signing" + eventSetValidationCanceledByTimeout = fsm.Event("proposal_canceled_timeout") + eventSwitchProposedToSigning = fsm.Event("switch_state_to_signing") ) type SignatureProposalFSM struct { *fsm.FSM } -func New() fsm_pool.IStateMachine { +func New() fsm_pool.MachineProvider { machine := &SignatureProposalFSM{} machine.FSM = fsm.MustNewFSM( fsmName, fsm.StateGlobalIdle, - []fsm.Event{ + []fsm.EventDesc{ // {Name: "", SrcState: []string{""}, DstState: ""}, // Init - {Name: eventInitProposal, SrcState: []string{fsm.StateGlobalIdle}, DstState: stateAwaitProposalConfirmation}, + {Name: eventInitProposal, SrcState: []fsm.State{fsm.StateGlobalIdle}, DstState: stateAwaitProposalConfirmation}, // Validate by participants - {Name: eventConfirmProposal, SrcState: []string{stateAwaitProposalConfirmation}, DstState: stateAwaitProposalConfirmation}, + {Name: eventConfirmProposal, SrcState: []fsm.State{stateAwaitProposalConfirmation}, DstState: stateAwaitProposalConfirmation}, // Is decline event should auto change state to default, or it process will initiated by client (external emit)? // Now set for external emitting. - {Name: eventDeclineProposal, SrcState: []string{stateAwaitProposalConfirmation}, DstState: stateValidationCanceledByParticipant}, + {Name: eventDeclineProposal, SrcState: []fsm.State{stateAwaitProposalConfirmation}, DstState: stateValidationCanceledByParticipant}, - {Name: eventValidateProposal, SrcState: []string{stateAwaitProposalConfirmation}, DstState: stateAwaitProposalConfirmation}, + {Name: eventValidateProposal, SrcState: []fsm.State{stateAwaitProposalConfirmation}, DstState: stateAwaitProposalConfirmation}, // eventProposalValidate internal or from client? // yay // Exit point - {Name: eventSetProposalValidated, SrcState: []string{stateAwaitProposalConfirmation}, DstState: "process_sig", IsInternal: true}, + {Name: eventSetProposalValidated, SrcState: []fsm.State{stateAwaitProposalConfirmation}, DstState: "process_sig", IsInternal: true}, // nan - {Name: eventSetValidationCanceledByTimeout, SrcState: []string{stateAwaitProposalConfirmation}, DstState: stateValidationCanceledByTimeout, IsInternal: true}, + {Name: eventSetValidationCanceledByTimeout, SrcState: []fsm.State{stateAwaitProposalConfirmation}, DstState: stateValidationCanceledByTimeout, IsInternal: true}, }, fsm.Callbacks{ eventInitProposal: machine.actionInitProposal, diff --git a/fsm/types/requests/signature_proposal.go b/fsm/types/requests/signature_proposal.go index 06f6b1c..dc6b72c 100644 --- a/fsm/types/requests/signature_proposal.go +++ b/fsm/types/requests/signature_proposal.go @@ -2,7 +2,7 @@ package requests import ( "errors" - "github.com/p2p-org/dc4bc/fsm/config" + "github.com/depools/dc4bc/fsm/config" ) // Requests diff --git a/go.mod b/go.mod index 8fb0edc..1fcf577 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/p2p-org/dc4bc +module github.com/depools/dc4bc go 1.13 @@ -8,7 +8,6 @@ require ( github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b github.com/looplab/fsm v0.1.0 github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7 - github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/stretchr/testify v1.6.1 github.com/syndtr/goleveldb v1.0.0 diff --git a/go.sum b/go.sum index 5cb1e07..1bbc3d8 100644 --- a/go.sum +++ b/go.sum @@ -20,10 +20,6 @@ github.com/looplab/fsm v0.1.0 h1:Qte7Zdn/5hBNbXzP7yxVU4OIFHWXBovyTT2LaBTyC20= github.com/looplab/fsm v0.1.0/go.mod h1:m2VaOfDHxqXBBMgc26m6yUOwkFn8H2AlJDE+jd/uafI= github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7 h1:CfWnkHgRG8zmxQI7RAhLIUFPkg+RfDdWiEtoE3y1+4w= github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7/go.mod h1:WoI7z45M7ZNA5BJxiJHaB+x7+k8S/3phW5Y13IR4yWY= -github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5 h1:GMB3MVJnxysGrSvjWGsgK8L3XGI3F4etQQq37Py6W5A= -github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5/go.mod h1:PwzwfeB5syFHXORC3MtPylVcjIoTDT/9cvkKpEndGVI= -github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f h1:QTRRO+ozoYgT3CQRIzNVYJRU3DB8HRnkZv6mr4ISmMA= -github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -51,9 +47,11 @@ golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7 golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190124100055-b90733256f2e h1:3GIlrlVLfkoipSReOMNAgApI0ajnalyLa/EZHHca/XI= diff --git a/main.go b/main.go index 7636feb..59634fe 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "go.dedis.ch/kyber/v3" - dkglib "github.com/p2p-org/dc4bc/dkg" + dkglib "github.com/depools/dc4bc/dkg" _ "image/gif" _ "image/png" diff --git a/mocks/clientMocks/state_mock.go b/mocks/clientMocks/state_mock.go index 94e12b6..04db683 100644 --- a/mocks/clientMocks/state_mock.go +++ b/mocks/clientMocks/state_mock.go @@ -5,8 +5,8 @@ package clientMocks import ( + client "github.com/depools/dc4bc/client" gomock "github.com/golang/mock/gomock" - client "github.com/p2p-org/dc4bc/client" reflect "reflect" ) diff --git a/mocks/storageMocks/storage_mock.go b/mocks/storageMocks/storage_mock.go index 5f0716b..4fcf0d9 100644 --- a/mocks/storageMocks/storage_mock.go +++ b/mocks/storageMocks/storage_mock.go @@ -5,8 +5,8 @@ package storageMocks import ( + storage "github.com/depools/dc4bc/storage" gomock "github.com/golang/mock/gomock" - storage "github.com/p2p-org/dc4bc/storage" reflect "reflect" )