This commit is contained in:
x88 2020-08-21 17:29:52 +03:00
parent dc23e4faf2
commit bbaf781f80
9 changed files with 41 additions and 15 deletions

View File

@ -276,14 +276,24 @@ func MustNewFSM(machineName string, initialState State, events []EventDesc, call
}
// WithState returns FSM copy with custom setup state
func (f FSM) CopyWithState(state State) *FSM {
func (f *FSM) MustCopyWithState(state State) *FSM {
var exists bool
f.stateMu.RLock()
defer f.stateMu.RUnlock()
if state != "" {
for _, s := range f.StatesList() {
if s == state {
exists = true
}
}
if !exists {
panic(fmt.Sprintf("cannot set state, not exists \"%s\" for \"%s\"", state, f.name))
}
f.currentState = state
}
return &f
return f
}
func (f *FSM) DoInternal(event Event, args ...interface{}) (resp *Response, err error) {
@ -468,7 +478,7 @@ func (f *FSM) EventsList() (events []Event) {
return
}
func (f *FSM) StatesSourcesList() (states []State) {
func (f *FSM) StatesList() (states []State) {
var allStates = map[State]bool{}
if len(f.transitions) > 0 {
for trKey, _ := range f.transitions {

View File

@ -297,7 +297,26 @@ func TestFSM_StatesList(t *testing.T) {
stateStage2,
}
if !compareStatesArr(testingFSM.StatesSourcesList(), statesList) {
if !compareStatesArr(testingFSM.StatesList(), statesList) {
t.Error("expected states", statesList)
}
}
func TestFSM_CopyWithState(t *testing.T) {
testingFSM1 := MustNewFSM(
testName,
stateInit,
testingEvents,
testingCallbacks,
)
testingFSM2 := testingFSM.MustCopyWithState(stateStage2)
if testingFSM1.State() != stateInit {
t.Fatal("expect unchanged source state")
}
if testingFSM2.State() != stateStage2 {
t.Fatal("expect changed source state")
}
}

View File

@ -26,7 +26,7 @@ type MachineProvider interface {
EventsList() []fsm.Event
StatesSourcesList() []fsm.State
StatesList() []fsm.State
IsFinState(state fsm.State) bool
}
@ -106,7 +106,7 @@ func Init(machines ...MachineProvider) *FSMPool {
// Fill up states with initial and exit states checking
for _, machine := range machines {
machineName := machine.Name()
machineStates := machine.StatesSourcesList()
machineStates := machine.StatesList()
for _, state := range machineStates {
if machine.IsFinState(state) {
// If state is initial for another machine,

View File

@ -170,6 +170,6 @@ func (m *DKGProposalFSM) WithSetup(state fsm.State, payload *internal.DumpedMach
defer m.payloadMu.Unlock()
m.payload = payload
m.FSM = m.FSM.CopyWithState(state)
m.FSM = m.FSM.MustCopyWithState(state)
return m
}

View File

@ -2,7 +2,6 @@ package internal
import (
"crypto/ed25519"
"encoding/hex"
"errors"
"github.com/depools/dc4bc/fsm/fsm"
"github.com/depools/dc4bc/fsm/fsm_pool"
@ -119,12 +118,11 @@ func (p *DumpedMachineStatePayload) SigningQuorumUpdate(id int, participant *Sig
return
}
func (p *DumpedMachineStatePayload) SetAddrHexPubKey(addr string, pubKey ed25519.PublicKey) {
func (p *DumpedMachineStatePayload) SetPubKeyAddr(addr string, pubKey ed25519.PublicKey) {
if p.PubKeys == nil {
p.PubKeys = make(map[string]ed25519.PublicKey)
}
hexAddr := hex.EncodeToString([]byte(addr))
p.PubKeys[hexAddr] = pubKey
p.PubKeys[addr] = pubKey
return
}

View File

@ -48,7 +48,7 @@ func (m *SignatureProposalFSM) actionInitSignatureProposal(inEvent fsm.Event, ar
UpdatedAt: request.CreatedAt,
}
m.payload.SetAddrHexPubKey(participant.Addr, participant.PubKey)
m.payload.SetPubKeyAddr(participant.Addr, participant.PubKey)
}
// Checking fo quorum length

View File

@ -87,6 +87,6 @@ func (m *SignatureProposalFSM) WithSetup(state fsm.State, payload *internal.Dump
defer m.payloadMu.Unlock()
m.payload = payload
m.FSM = m.FSM.CopyWithState(state)
m.FSM = m.FSM.MustCopyWithState(state)
return m
}

View File

@ -110,6 +110,6 @@ func (m *SigningProposalFSM) WithSetup(state fsm.State, payload *internal.Dumped
defer m.payloadMu.Unlock()
m.payload = payload
m.FSM = m.FSM.CopyWithState(state)
m.FSM = m.FSM.MustCopyWithState(state)
return m
}

View File

@ -13,7 +13,6 @@ type SignatureProposalParticipantsListRequest struct {
}
type SignatureProposalParticipantsEntry struct {
// Public title for address, such as name, nickname, organization
Addr string
PubKey []byte
DkgPubKey []byte