2020-08-06 17:20:13 -07:00
|
|
|
package dkg_proposal_fsm
|
|
|
|
|
|
|
|
import (
|
2020-08-07 16:20:20 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-06 17:20:13 -07:00
|
|
|
"github.com/depools/dc4bc/fsm/fsm"
|
2020-08-07 16:20:20 -07:00
|
|
|
"github.com/depools/dc4bc/fsm/state_machines/internal"
|
|
|
|
"github.com/depools/dc4bc/fsm/types/requests"
|
2020-08-06 17:20:13 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pub keys
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionPubKeyPrepareConfirmations(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-06 17:20:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionPubKeyConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-07 16:20:20 -07:00
|
|
|
m.payloadMu.Lock()
|
|
|
|
defer m.payloadMu.Unlock()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
err = errors.New("{arg0} required {DKGProposalPubKeyConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, ok := args[0].(requests.DKGProposalPubKeyConfirmationRequest)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot cast {arg0} to type {DKGProposalPubKeyConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = request.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant, ok := m.payload.DKGProposalPayload[request.ParticipantId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("{ParticipantId} not exist in quorum")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
copy(dkgProposalParticipant.PublicKey, request.PubKey)
|
2020-08-07 16:20:20 -07:00
|
|
|
dkgProposalParticipant.UpdatedAt = request.CreatedAt
|
|
|
|
dkgProposalParticipant.Status = internal.PubKeyConfirmed
|
|
|
|
|
|
|
|
m.payload.DKGProposalPayload[request.ParticipantId] = dkgProposalParticipant
|
|
|
|
|
2020-08-06 17:20:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commits
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionCommitConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-07 16:20:20 -07:00
|
|
|
m.payloadMu.Lock()
|
|
|
|
defer m.payloadMu.Unlock()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
err = errors.New("{arg0} required {DKGProposalCommitConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, ok := args[0].(requests.DKGProposalCommitConfirmationRequest)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot cast {arg0} to type {DKGProposalCommitConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = request.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant, ok := m.payload.DKGProposalPayload[request.ParticipantId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("{ParticipantId} not exist in quorum")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
copy(dkgProposalParticipant.Commit, request.Commit)
|
2020-08-07 16:20:20 -07:00
|
|
|
dkgProposalParticipant.UpdatedAt = request.CreatedAt
|
|
|
|
dkgProposalParticipant.Status = internal.CommitConfirmed
|
|
|
|
|
|
|
|
m.payload.DKGProposalPayload[request.ParticipantId] = dkgProposalParticipant
|
2020-08-06 17:20:13 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deals
|
2020-08-07 16:20:20 -07:00
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionDealConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-07 16:20:20 -07:00
|
|
|
m.payloadMu.Lock()
|
|
|
|
defer m.payloadMu.Unlock()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
err = errors.New("{arg0} required {DKGProposalDealConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, ok := args[0].(requests.DKGProposalDealConfirmationRequest)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot cast {arg0} to type {DKGProposalDealConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = request.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant, ok := m.payload.DKGProposalPayload[request.ParticipantId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("{ParticipantId} not exist in quorum")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
copy(dkgProposalParticipant.Deal, request.Deal)
|
2020-08-07 16:20:20 -07:00
|
|
|
dkgProposalParticipant.UpdatedAt = request.CreatedAt
|
|
|
|
dkgProposalParticipant.Status = internal.DealConfirmed
|
|
|
|
|
|
|
|
m.payload.DKGProposalPayload[request.ParticipantId] = dkgProposalParticipant
|
|
|
|
|
2020-08-06 17:20:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-07 16:20:20 -07:00
|
|
|
// Responses
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionResponseConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-07 16:20:20 -07:00
|
|
|
m.payloadMu.Lock()
|
|
|
|
defer m.payloadMu.Unlock()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
err = errors.New("{arg0} required {DKGProposalResponseConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, ok := args[0].(requests.DKGProposalResponseConfirmationRequest)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot cast {arg0} to type {DKGProposalResponseConfirmationRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = request.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant, ok := m.payload.DKGProposalPayload[request.ParticipantId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("{ParticipantId} not exist in quorum")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
copy(dkgProposalParticipant.Response, request.Response)
|
2020-08-07 16:20:20 -07:00
|
|
|
dkgProposalParticipant.UpdatedAt = request.CreatedAt
|
|
|
|
dkgProposalParticipant.Status = internal.ResponseConfirmed
|
|
|
|
|
|
|
|
m.payload.DKGProposalPayload[request.ParticipantId] = dkgProposalParticipant
|
|
|
|
|
2020-08-06 17:20:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-07 16:20:20 -07:00
|
|
|
// Errors
|
2020-08-08 14:44:52 -07:00
|
|
|
func (m *DKGProposalFSM) actionConfirmationError(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
|
2020-08-07 16:20:20 -07:00
|
|
|
m.payloadMu.Lock()
|
|
|
|
defer m.payloadMu.Unlock()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
err = errors.New("{arg0} required {DKGProposalConfirmationErrorRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, ok := args[0].(requests.DKGProposalConfirmationErrorRequest)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot cast {arg0} to type {DKGProposalConfirmationErrorRequest}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = request.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant, ok := m.payload.DKGProposalPayload[request.ParticipantId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("{ParticipantId} not exist in quorum")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move to methods
|
2020-08-08 14:44:52 -07:00
|
|
|
switch inEvent {
|
2020-08-07 16:20:20 -07:00
|
|
|
case EventDKGPubKeyConfirmationError:
|
|
|
|
switch dkgProposalParticipant.Status {
|
|
|
|
case internal.PubKeyConAwaitConfirmation:
|
|
|
|
dkgProposalParticipant.Status = internal.PubKeyConfirmationError
|
|
|
|
case internal.PubKeyConfirmed:
|
|
|
|
err = errors.New("{Status} already confirmed")
|
|
|
|
case internal.PubKeyConfirmationError:
|
|
|
|
err = errors.New(fmt.Sprintf("{Status} already has {\"%s\"}", internal.PubKeyConfirmationError))
|
|
|
|
default:
|
|
|
|
err = errors.New(fmt.Sprintf(
|
|
|
|
"{Status} now is \"%s\" and cannot set to {\"%s\"}",
|
|
|
|
dkgProposalParticipant.Status,
|
|
|
|
internal.PubKeyConfirmationError,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
case EventDKGCommitConfirmationError:
|
|
|
|
switch dkgProposalParticipant.Status {
|
|
|
|
case internal.CommitAwaitConfirmation:
|
|
|
|
dkgProposalParticipant.Status = internal.CommitConfirmationError
|
|
|
|
case internal.CommitConfirmed:
|
|
|
|
err = errors.New("{Status} already confirmed")
|
|
|
|
case internal.CommitConfirmationError:
|
|
|
|
err = errors.New(fmt.Sprintf("{Status} already has {\"%s\"}", internal.CommitConfirmationError))
|
|
|
|
default:
|
|
|
|
err = errors.New(fmt.Sprintf(
|
|
|
|
"{Status} now is \"%s\" and cannot set to {\"%s\"}",
|
|
|
|
dkgProposalParticipant.Status,
|
|
|
|
internal.PubKeyConfirmationError,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
case EventDKGDealConfirmationError:
|
|
|
|
switch dkgProposalParticipant.Status {
|
|
|
|
case internal.DealAwaitConfirmation:
|
|
|
|
dkgProposalParticipant.Status = internal.PubKeyConfirmationError
|
|
|
|
case internal.DealConfirmed:
|
|
|
|
err = errors.New("{Status} already confirmed")
|
|
|
|
case internal.DealConfirmationError:
|
|
|
|
err = errors.New(fmt.Sprintf("{Status} already has {\"%s\"}", internal.DealConfirmationError))
|
|
|
|
default:
|
|
|
|
err = errors.New(fmt.Sprintf(
|
|
|
|
"{Status} now is \"%s\" and cannot set to {\"%s\"}",
|
|
|
|
dkgProposalParticipant.Status,
|
|
|
|
internal.PubKeyConfirmationError,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
case EventDKGResponseConfirmationError:
|
|
|
|
switch dkgProposalParticipant.Status {
|
|
|
|
case internal.ResponseAwaitConfirmation:
|
|
|
|
dkgProposalParticipant.Status = internal.PubKeyConfirmationError
|
|
|
|
case internal.ResponseConfirmed:
|
|
|
|
err = errors.New("{Status} already confirmed")
|
|
|
|
case internal.ResponseConfirmationError:
|
|
|
|
err = errors.New(fmt.Sprintf("{Status} already has {\"%s\"}", internal.ResponseConfirmationError))
|
|
|
|
default:
|
|
|
|
err = errors.New(fmt.Sprintf(
|
|
|
|
"{Status} now is \"%s\" and cannot set to {\"%s\"}",
|
|
|
|
dkgProposalParticipant.Status,
|
|
|
|
internal.PubKeyConfirmationError,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
default:
|
2020-08-08 14:44:52 -07:00
|
|
|
err = errors.New(fmt.Sprintf("{%s} event cannot be used for action {actionConfirmationError}", inEvent))
|
2020-08-07 16:20:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dkgProposalParticipant.UpdatedAt = request.CreatedAt
|
|
|
|
|
|
|
|
m.payload.DKGProposalPayload[request.ParticipantId] = dkgProposalParticipant
|
|
|
|
|
2020-08-08 14:44:52 -07:00
|
|
|
// TODO: Add outEvent
|
|
|
|
|
2020-08-06 17:20:13 -07:00
|
|
|
return
|
|
|
|
}
|