Merge branch 'fsm-draft' of ssh://github.com/depools/dc4bc into feat/airgapped-client-tests

This commit is contained in:
programmer10110 2020-08-21 19:25:17 +03:00
commit 692c256f0a
13 changed files with 138 additions and 40 deletions

View File

@ -276,14 +276,24 @@ func MustNewFSM(machineName string, initialState State, events []EventDesc, call
} }
// WithState returns FSM copy with custom setup state // 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() f.stateMu.RLock()
defer f.stateMu.RUnlock() defer f.stateMu.RUnlock()
if state != "" { 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 f.currentState = state
} }
return &f return f
} }
func (f *FSM) DoInternal(event Event, args ...interface{}) (resp *Response, err error) { func (f *FSM) DoInternal(event Event, args ...interface{}) (resp *Response, err error) {
@ -468,7 +478,7 @@ func (f *FSM) EventsList() (events []Event) {
return return
} }
func (f *FSM) StatesSourcesList() (states []State) { func (f *FSM) StatesList() (states []State) {
var allStates = map[State]bool{} var allStates = map[State]bool{}
if len(f.transitions) > 0 { if len(f.transitions) > 0 {
for trKey, _ := range f.transitions { for trKey, _ := range f.transitions {

View File

@ -297,7 +297,26 @@ func TestFSM_StatesList(t *testing.T) {
stateStage2, stateStage2,
} }
if !compareStatesArr(testingFSM.StatesSourcesList(), statesList) { if !compareStatesArr(testingFSM.StatesList(), statesList) {
t.Error("expected states", 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 EventsList() []fsm.Event
StatesSourcesList() []fsm.State StatesList() []fsm.State
IsFinState(state fsm.State) bool IsFinState(state fsm.State) bool
} }
@ -106,7 +106,7 @@ func Init(machines ...MachineProvider) *FSMPool {
// Fill up states with initial and exit states checking // Fill up states with initial and exit states checking
for _, machine := range machines { for _, machine := range machines {
machineName := machine.Name() machineName := machine.Name()
machineStates := machine.StatesSourcesList() machineStates := machine.StatesList()
for _, state := range machineStates { for _, state := range machineStates {
if machine.IsFinState(state) { if machine.IsFinState(state) {
// If state is initial for another machine, // If state is initial for another machine,

View File

@ -7,6 +7,7 @@ import (
"github.com/depools/dc4bc/fsm/fsm" "github.com/depools/dc4bc/fsm/fsm"
"github.com/depools/dc4bc/fsm/state_machines/internal" "github.com/depools/dc4bc/fsm/state_machines/internal"
"github.com/depools/dc4bc/fsm/types/requests" "github.com/depools/dc4bc/fsm/types/requests"
"github.com/depools/dc4bc/fsm/types/responses"
"reflect" "reflect"
) )
@ -49,7 +50,20 @@ func (m *DKGProposalFSM) actionInitDKGProposal(inEvent fsm.Event, args ...interf
// Remove m.payload.SignatureProposalPayload? // Remove m.payload.SignatureProposalPayload?
return // Make response
responseData := make(responses.DKGProposalPubKeysParticipantResponse, 0)
for participantId, participant := range m.payload.DKGProposalPayload.Quorum {
responseEntry := &responses.DKGProposalPubKeysParticipantEntry{
ParticipantId: participantId,
Addr: participant.Addr,
DkgPubKey: participant.DkgPubKey,
}
responseData = append(responseData, responseEntry)
}
return inEvent, responseData, nil
} }
// Commits // Commits
@ -86,7 +100,7 @@ func (m *DKGProposalFSM) actionCommitConfirmationReceived(inEvent fsm.Event, arg
return return
} }
copy(dkgProposalParticipant.Commit, request.Commit) copy(dkgProposalParticipant.DkgCommit, request.Commit)
dkgProposalParticipant.Status = internal.CommitConfirmed dkgProposalParticipant.Status = internal.CommitConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
@ -135,6 +149,21 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitCommits(inEvent fsm.Event
participant.Status = internal.DealAwaitConfirmation participant.Status = internal.DealAwaitConfirmation
} }
// Make response
responseData := make(responses.DKGProposalCommitParticipantResponse, 0)
for participantId, participant := range m.payload.DKGProposalPayload.Quorum {
responseEntry := &responses.DKGProposalCommitParticipantEntry{
ParticipantId: participantId,
Addr: participant.Addr,
DkgCommit: participant.DkgCommit,
}
responseData = append(responseData, responseEntry)
}
response = responseData
return return
} }
@ -172,7 +201,7 @@ func (m *DKGProposalFSM) actionDealConfirmationReceived(inEvent fsm.Event, args
return return
} }
copy(dkgProposalParticipant.Deal, request.Deal) copy(dkgProposalParticipant.DkgDeal, request.Deal)
dkgProposalParticipant.Status = internal.DealConfirmed dkgProposalParticipant.Status = internal.DealConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
@ -221,6 +250,21 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitDeals(inEvent fsm.Event,
participant.Status = internal.ResponseAwaitConfirmation participant.Status = internal.ResponseAwaitConfirmation
} }
// Make response
responseData := make(responses.DKGProposalDealParticipantResponse, 0)
for participantId, participant := range m.payload.DKGProposalPayload.Quorum {
responseEntry := &responses.DKGProposalDealParticipantEntry{
ParticipantId: participantId,
Addr: participant.Addr,
DkgDeal: participant.DkgDeal,
}
responseData = append(responseData, responseEntry)
}
response = responseData
return return
} }
@ -258,7 +302,7 @@ func (m *DKGProposalFSM) actionResponseConfirmationReceived(inEvent fsm.Event, a
return return
} }
copy(dkgProposalParticipant.Response, request.Response) copy(dkgProposalParticipant.DkgResponse, request.Response)
dkgProposalParticipant.Status = internal.ResponseConfirmed dkgProposalParticipant.Status = internal.ResponseConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
@ -307,6 +351,21 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitResponses(inEvent fsm.Eve
participant.Status = internal.MasterKeyAwaitConfirmation participant.Status = internal.MasterKeyAwaitConfirmation
} }
// Make response
responseData := make(responses.DKGProposalResponseParticipantResponse, 0)
for participantId, participant := range m.payload.DKGProposalPayload.Quorum {
responseEntry := &responses.DKGProposalResponseParticipantEntry{
ParticipantId: participantId,
Addr: participant.Addr,
DkgResponse: participant.DkgResponse,
}
responseData = append(responseData, responseEntry)
}
response = responseData
return return
} }
@ -344,7 +403,7 @@ func (m *DKGProposalFSM) actionMasterKeyConfirmationReceived(inEvent fsm.Event,
return return
} }
copy(dkgProposalParticipant.MasterKey, request.MasterKey) copy(dkgProposalParticipant.DkgMasterKey, request.MasterKey)
dkgProposalParticipant.Status = internal.MasterKeyConfirmed dkgProposalParticipant.Status = internal.MasterKeyConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
@ -375,7 +434,7 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitMasterKey(inEvent fsm.Eve
if participant.Status == internal.MasterKeyConfirmationError { if participant.Status == internal.MasterKeyConfirmationError {
isContainsError = true isContainsError = true
} else if participant.Status == internal.MasterKeyConfirmed { } else if participant.Status == internal.MasterKeyConfirmed {
masterKeys = append(masterKeys, participant.MasterKey) masterKeys = append(masterKeys, participant.DkgMasterKey)
unconfirmedParticipants-- unconfirmedParticipants--
} }
} }

View File

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

View File

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

View File

@ -76,10 +76,10 @@ const (
type DKGProposalParticipant struct { type DKGProposalParticipant struct {
Addr string Addr string
DkgPubKey []byte DkgPubKey []byte
Commit []byte DkgCommit []byte
Deal []byte DkgDeal []byte
Response []byte DkgResponse []byte
MasterKey []byte DkgMasterKey []byte
Status DKGParticipantStatus Status DKGParticipantStatus
Error error Error error
UpdatedAt time.Time UpdatedAt time.Time

View File

@ -48,7 +48,7 @@ func (m *SignatureProposalFSM) actionInitSignatureProposal(inEvent fsm.Event, ar
UpdatedAt: request.CreatedAt, UpdatedAt: request.CreatedAt,
} }
m.payload.SetAddrHexPubKey(participant.Addr, participant.PubKey) m.payload.SetPubKeyAddr(participant.Addr, participant.PubKey)
} }
// Checking fo quorum length // Checking fo quorum length
@ -61,15 +61,14 @@ func (m *SignatureProposalFSM) actionInitSignatureProposal(inEvent fsm.Event, ar
responseData := make(responses.SignatureProposalParticipantInvitationsResponse, 0) responseData := make(responses.SignatureProposalParticipantInvitationsResponse, 0)
for participantId, proposal := range m.payload.SignatureProposalPayload.Quorum { for participantId, participant := range m.payload.SignatureProposalPayload.Quorum {
responseEntry := &responses.SignatureProposalParticipantInvitationEntry{ responseEntry := &responses.SignatureProposalParticipantInvitationEntry{
ParticipantId: participantId, ParticipantId: participantId,
Addr: proposal.Addr, Addr: participant.Addr,
} }
responseData = append(responseData, responseEntry) responseData = append(responseData, responseEntry)
} }
// Change state
return inEvent, responseData, nil return inEvent, responseData, nil
} }
@ -167,7 +166,6 @@ func (m *SignatureProposalFSM) actionValidateSignatureProposal(inEvent fsm.Event
responseEntry := &responses.SignatureProposalParticipantStatusEntry{ responseEntry := &responses.SignatureProposalParticipantStatusEntry{
ParticipantId: participantId, ParticipantId: participantId,
Addr: participant.Addr, Addr: participant.Addr,
DkgPubKey: participant.DkgPubKey,
Status: uint8(participant.Status), Status: uint8(participant.Status),
} }
responseData = append(responseData, responseEntry) responseData = append(responseData, responseEntry)
@ -186,7 +184,6 @@ func (m *SignatureProposalFSM) actionSignatureProposalCanceledByTimeout(inEvent
responseEntry := &responses.SignatureProposalParticipantStatusEntry{ responseEntry := &responses.SignatureProposalParticipantStatusEntry{
ParticipantId: participantId, ParticipantId: participantId,
Addr: participant.Addr, Addr: participant.Addr,
DkgPubKey: participant.DkgPubKey,
Status: uint8(participant.Status), Status: uint8(participant.Status),
} }
responseData = append(responseData, responseEntry) responseData = append(responseData, responseEntry)

View File

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

View File

@ -134,7 +134,7 @@ func (m *SigningProposalFSM) actionProposalResponseByParticipant(inEvent fsm.Eve
return return
} }
// copy(signingProposalParticipant.Commit, request.Commit) // copy(signingProposalParticipant.DkgCommit, request.DkgCommit)
switch inEvent { switch inEvent {
case EventConfirmSigningConfirmation: case EventConfirmSigningConfirmation:
signingProposalParticipant.Status = internal.SigningConfirmed signingProposalParticipant.Status = internal.SigningConfirmed

View File

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

View File

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

View File

@ -1,19 +1,35 @@
package responses package responses
type DKGProposalPubKeysParticipantResponse []*DKGProposalPubKeysParticipantEntry
type DKGProposalPubKeysParticipantEntry struct {
ParticipantId int
Addr string
DkgPubKey []byte
}
type DKGProposalCommitParticipantResponse []*DKGProposalCommitParticipantEntry type DKGProposalCommitParticipantResponse []*DKGProposalCommitParticipantEntry
type DKGProposalCommitParticipantEntry struct { type DKGProposalCommitParticipantEntry struct {
ParticipantId int ParticipantId int
Title string Addr string
Commit []byte DkgCommit []byte
} }
type DKGProposalDealParticipantResponse []*DKGProposalDealParticipantEntry type DKGProposalDealParticipantResponse []*DKGProposalDealParticipantEntry
type DKGProposalDealParticipantEntry struct { type DKGProposalDealParticipantEntry struct {
ParticipantId int ParticipantId int
Title string Addr string
Deal []byte DkgDeal []byte
}
type DKGProposalResponseParticipantResponse []*DKGProposalResponseParticipantEntry
type DKGProposalResponseParticipantEntry struct {
ParticipantId int
Addr string
DkgResponse []byte
} }
type DKGProposalResponsesParticipantResponse []*DKGProposalResponsesParticipantEntry type DKGProposalResponsesParticipantResponse []*DKGProposalResponsesParticipantEntry