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

This commit is contained in:
programmer10110 2020-08-25 15:56:40 +03:00
commit 232665a3eb
4 changed files with 446 additions and 94 deletions

View File

@ -3,13 +3,12 @@ package dkg_proposal_fsm
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"github.com/depools/dc4bc/fsm/config" "github.com/depools/dc4bc/fsm/config"
"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" "github.com/depools/dc4bc/fsm/types/responses"
"reflect"
) )
// Init // Init
@ -69,7 +68,6 @@ func (m *DKGProposalFSM) actionInitDKGProposal(inEvent fsm.Event, args ...interf
} }
// Commits // Commits
func (m *DKGProposalFSM) actionCommitConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) { func (m *DKGProposalFSM) actionCommitConfirmationReceived(inEvent fsm.Event, args ...interface{}) (outEvent fsm.Event, response interface{}, err error) {
m.payloadMu.Lock() m.payloadMu.Lock()
defer m.payloadMu.Unlock() defer m.payloadMu.Unlock()
@ -107,7 +105,7 @@ func (m *DKGProposalFSM) actionCommitConfirmationReceived(inEvent fsm.Event, arg
dkgProposalParticipant.Status = internal.CommitConfirmed dkgProposalParticipant.Status = internal.CommitConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
m.payload.SignatureProposalPayload.UpdatedAt = request.CreatedAt m.payload.DKGProposalPayload.UpdatedAt = request.CreatedAt
m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant) m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant)
@ -123,7 +121,7 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitCommits(inEvent fsm.Event
defer m.payloadMu.Unlock() defer m.payloadMu.Unlock()
if m.payload.DKGProposalPayload.IsExpired() { if m.payload.DKGProposalPayload.IsExpired() {
outEvent = eventDKGCommitsConfirmationCancelByErrorInternal outEvent = eventDKGCommitsConfirmationCancelByTimeoutInternal
return return
} }
@ -137,7 +135,7 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitCommits(inEvent fsm.Event
} }
if isContainsError { if isContainsError {
outEvent = eventDKGCommitsConfirmationCancelByTimeoutInternal outEvent = eventDKGCommitsConfirmationCancelByErrorInternal
return return
} }
@ -209,7 +207,7 @@ func (m *DKGProposalFSM) actionDealConfirmationReceived(inEvent fsm.Event, args
dkgProposalParticipant.Status = internal.DealConfirmed dkgProposalParticipant.Status = internal.DealConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
m.payload.SignatureProposalPayload.UpdatedAt = request.CreatedAt m.payload.DKGProposalPayload.UpdatedAt = request.CreatedAt
m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant) m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant)
@ -314,7 +312,7 @@ func (m *DKGProposalFSM) actionResponseConfirmationReceived(inEvent fsm.Event, a
dkgProposalParticipant.Status = internal.ResponseConfirmed dkgProposalParticipant.Status = internal.ResponseConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
m.payload.SignatureProposalPayload.UpdatedAt = request.CreatedAt m.payload.DKGProposalPayload.UpdatedAt = request.CreatedAt
m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant) m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant)
@ -416,7 +414,7 @@ func (m *DKGProposalFSM) actionMasterKeyConfirmationReceived(inEvent fsm.Event,
dkgProposalParticipant.Status = internal.MasterKeyConfirmed dkgProposalParticipant.Status = internal.MasterKeyConfirmed
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
m.payload.SignatureProposalPayload.UpdatedAt = request.CreatedAt m.payload.DKGProposalPayload.UpdatedAt = request.CreatedAt
m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant) m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant)
@ -455,21 +453,15 @@ func (m *DKGProposalFSM) actionValidateDkgProposalAwaitMasterKey(inEvent fsm.Eve
// Temporary simplest match master keys // Temporary simplest match master keys
if len(masterKeys) > 1 { if len(masterKeys) > 1 {
for i, masterKey := range masterKeys { for _, masterKey := range masterKeys {
for j := range masterKeys { if !reflect.DeepEqual(masterKey, masterKeys[0]) {
if i == j { for _, participant := range m.payload.DKGProposalPayload.Quorum {
continue participant.Status = internal.MasterKeyConfirmationError
participant.Error = errors.New("master key is mismatched")
} }
if !reflect.DeepEqual(masterKey, masterKeys[i]) { outEvent = eventDKGMasterKeyConfirmationCancelByErrorInternal
for _, participant := range m.payload.DKGProposalPayload.Quorum { return
participant.Status = internal.MasterKeyConfirmationError
participant.Error = errors.New("master key is mismatched")
}
outEvent = eventDKGMasterKeyConfirmationCancelByErrorInternal
return
}
} }
} }
} }
@ -589,7 +581,7 @@ func (m *DKGProposalFSM) actionConfirmationError(inEvent fsm.Event, args ...inte
dkgProposalParticipant.Error = request.Error dkgProposalParticipant.Error = request.Error
dkgProposalParticipant.UpdatedAt = request.CreatedAt dkgProposalParticipant.UpdatedAt = request.CreatedAt
m.payload.SignatureProposalPayload.UpdatedAt = request.CreatedAt m.payload.DKGProposalPayload.UpdatedAt = request.CreatedAt
m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant) m.payload.DKGQuorumUpdate(request.ParticipantId, dkgProposalParticipant)

View File

@ -116,7 +116,7 @@ func New() internal.DumpedMachineProvider {
{Name: EventDKGDealConfirmationReceived, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitConfirmations}, {Name: EventDKGDealConfirmationReceived, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitConfirmations},
// Canceled // Canceled
{Name: EventDKGDealConfirmationError, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitCanceledByError}, {Name: EventDKGDealConfirmationError, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitCanceledByError},
{Name: eventDKGDealsConfirmationCancelByTimeoutInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitConfirmations, IsInternal: true}, {Name: eventDKGDealsConfirmationCancelByTimeoutInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitCanceledByTimeout, IsInternal: true},
{Name: eventAutoDKGValidateConfirmationDealsInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitConfirmations, IsInternal: true, IsAuto: true}, {Name: eventAutoDKGValidateConfirmationDealsInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgDealsAwaitConfirmations, IsInternal: true, IsAuto: true},
{Name: eventDKGDealsConfirmedInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgResponsesAwaitConfirmations, IsInternal: true}, {Name: eventDKGDealsConfirmedInternal, SrcState: []fsm.State{StateDkgDealsAwaitConfirmations}, DstState: StateDkgResponsesAwaitConfirmations, IsInternal: true},
@ -135,6 +135,7 @@ func New() internal.DumpedMachineProvider {
{Name: EventDKGMasterKeyConfirmationReceived, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitConfirmations}, {Name: EventDKGMasterKeyConfirmationReceived, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitConfirmations},
{Name: EventDKGMasterKeyConfirmationError, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitCanceledByError}, {Name: EventDKGMasterKeyConfirmationError, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitCanceledByError},
{Name: eventDKGMasterKeyConfirmationCancelByErrorInternal, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitCanceledByError, IsInternal: true},
{Name: eventDKGMasterKeyConfirmationCancelByTimeoutInternal, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitCanceledByTimeout, IsInternal: true}, {Name: eventDKGMasterKeyConfirmationCancelByTimeoutInternal, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitCanceledByTimeout, IsInternal: true},
{Name: eventAutoDKGValidateMasterKeyConfirmationInternal, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitConfirmations, IsInternal: true, IsAuto: true}, {Name: eventAutoDKGValidateMasterKeyConfirmationInternal, SrcState: []fsm.State{StateDkgMasterKeyAwaitConfirmations}, DstState: StateDkgMasterKeyAwaitConfirmations, IsInternal: true, IsAuto: true},

View File

@ -4,6 +4,7 @@ import (
"crypto/ed25519" "crypto/ed25519"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors"
sif "github.com/depools/dc4bc/fsm/state_machines/signing_proposal_fsm" sif "github.com/depools/dc4bc/fsm/state_machines/signing_proposal_fsm"
"reflect" "reflect"
"testing" "testing"
@ -47,7 +48,7 @@ var (
CreatedAt: tm, CreatedAt: tm,
} }
testFSMDump []byte testFSMDump = map[fsm.State][]byte{}
) )
func init() { func init() {
@ -133,21 +134,23 @@ func Test_SignatureProposal_Init(t *testing.T) {
} }
compareState(t, spf.StateParticipantsConfirmationsInit, testFSMInstance.machine.State()) compareState(t, spf.StateParticipantsConfirmationsInit, testFSMInstance.machine.State())
testFSMDump, err = testFSMInstance.Dump() testFSMDump[spf.StateParticipantsConfirmationsInit], err = testFSMInstance.Dump()
compareErrNil(t, err) compareDumpNotZero(t, testFSMDump[spf.StateParticipantsConfirmationsInit])
} }
// EventInitProposal // EventInitProposal
func Test_SignatureProposal_EventInitProposal(t *testing.T) { func Test_SignatureProposal_EventInitProposal_Positive(t *testing.T) {
var fsmResponse *fsm.Response var fsmResponse *fsm.Response
testFSMInstance, err := FromDump(testFSMDump) testFSMInstance, err := FromDump(testFSMDump[spf.StateParticipantsConfirmationsInit])
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
compareState(t, spf.StateParticipantsConfirmationsInit, testFSMInstance.machine.State())
// Make request // Make request
request := make([]*requests.SignatureProposalParticipantsEntry, 0) request := make([]*requests.SignatureProposalParticipantsEntry, 0)
@ -162,12 +165,10 @@ func Test_SignatureProposal_EventInitProposal(t *testing.T) {
testParticipantsListRequest.Participants = request testParticipantsListRequest.Participants = request
testParticipantsListRequest.SigningThreshold = len(request) testParticipantsListRequest.SigningThreshold = len(request)
fsmResponse, testFSMDump, err = testFSMInstance.Do(spf.EventInitProposal, testParticipantsListRequest) fsmResponse, testFSMDump[spf.StateAwaitParticipantsConfirmations], err = testFSMInstance.Do(spf.EventInitProposal, testParticipantsListRequest)
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareState(t, spf.StateAwaitParticipantsConfirmations, fsmResponse.State) compareState(t, spf.StateAwaitParticipantsConfirmations, fsmResponse.State)
@ -200,32 +201,42 @@ func Test_SignatureProposal_EventInitProposal(t *testing.T) {
testIdMapParticipants[participant.ParticipantId] = participantEntry testIdMapParticipants[participant.ParticipantId] = participantEntry
} }
compareDumpNotZero(t, testFSMDump[spf.StateAwaitParticipantsConfirmations])
} }
// EventConfirmSignatureProposal // EventConfirmSignatureProposal
func Test_SignatureProposal_EventConfirmSignatureProposal(t *testing.T) { func Test_SignatureProposal_EventConfirmSignatureProposal_Positive(t *testing.T) {
var fsmResponse *fsm.Response var (
fsmResponse *fsm.Response
testFSMDumpLocal []byte
)
participantsCount := len(testIdMapParticipants) participantsCount := len(testIdMapParticipants)
participantCounter := participantsCount participantCounter := participantsCount
// testFSMDumpLocal = make([]b)
testFSMDumpLocal = testFSMDump[spf.StateAwaitParticipantsConfirmations]
for participantId, _ := range testIdMapParticipants { for participantId, _ := range testIdMapParticipants {
participantCounter-- participantCounter--
testFSMInstance, err := FromDump(testFSMDump) testFSMInstance, err := FromDump(testFSMDumpLocal)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(spf.EventConfirmSignatureProposal, requests.SignatureProposalParticipantRequest{ inState, _ := testFSMInstance.State()
compareState(t, spf.StateAwaitParticipantsConfirmations, inState)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(spf.EventConfirmSignatureProposal, requests.SignatureProposalParticipantRequest{
ParticipantId: participantId, ParticipantId: participantId,
CreatedAt: time.Now(), CreatedAt: time.Now(),
}) })
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump) compareDumpNotZero(t, testFSMDumpLocal)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
@ -236,27 +247,82 @@ func Test_SignatureProposal_EventConfirmSignatureProposal(t *testing.T) {
} }
compareState(t, spf.StateSignatureProposalCollected, fsmResponse.State) compareState(t, spf.StateSignatureProposalCollected, fsmResponse.State)
testFSMDump[spf.StateSignatureProposalCollected] = testFSMDumpLocal
compareDumpNotZero(t, testFSMDump[spf.StateSignatureProposalCollected])
} }
func Test_DkgProposal_Positive(t *testing.T) { func Test_SignatureProposal_EventConfirmSignatureProposal_Canceled_Participant(t *testing.T) {
var fsmResponse *fsm.Response testFSMInstance, err := FromDump(testFSMDump[spf.StateAwaitParticipantsConfirmations])
testFSMInstance, err := FromDump(testFSMDump)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(dpf.EventDKGInitProcess, requests.DefaultRequest{ inState, _ := testFSMInstance.State()
compareState(t, spf.StateAwaitParticipantsConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(spf.EventDeclineProposal, requests.SignatureProposalParticipantRequest{
ParticipantId: 0,
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareDumpNotZero(t, testFSMDumpLocal)
compareFSMResponseNotNil(t, fsmResponse)
compareState(t, spf.StateValidationCanceledByParticipant, fsmResponse.State)
}
func Test_SignatureProposal_EventConfirmSignatureProposal_Canceled_Timeout(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[spf.StateAwaitParticipantsConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, spf.StateAwaitParticipantsConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(spf.EventConfirmSignatureProposal, requests.SignatureProposalParticipantRequest{
ParticipantId: 0,
CreatedAt: time.Now().Add(36 * time.Hour),
})
compareErrNil(t, err)
compareDumpNotZero(t, testFSMDumpLocal)
compareFSMResponseNotNil(t, fsmResponse)
compareState(t, spf.StateValidationCanceledByTimeout, fsmResponse.State)
}
func Test_DkgProposal_EventDKGInitProcess_Positive(t *testing.T) {
var fsmResponse *fsm.Response
testFSMInstance, err := FromDump(testFSMDump[spf.StateSignatureProposalCollected])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, spf.StateSignatureProposalCollected, inState)
fsmResponse, testFSMDump[dpf.StateDkgCommitsAwaitConfirmations], err = testFSMInstance.Do(dpf.EventDKGInitProcess, requests.DefaultRequest{
CreatedAt: time.Now(), CreatedAt: time.Now(),
}) })
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareState(t, dpf.StateDkgCommitsAwaitConfirmations, fsmResponse.State)
response, ok := fsmResponse.Data.(responses.DKGProposalPubKeysParticipantResponse) response, ok := fsmResponse.Data.(responses.DKGProposalPubKeysParticipantResponse)
if !ok { if !ok {
@ -281,21 +347,29 @@ func Test_DkgProposal_Positive(t *testing.T) {
} }
} }
compareState(t, dpf.StateDkgCommitsAwaitConfirmations, fsmResponse.State) compareDumpNotZero(t, testFSMDump[dpf.StateDkgCommitsAwaitConfirmations])
} }
// Commits // Commits
func Test_DkgProposal_EventDKGCommitConfirmationReceived(t *testing.T) { func Test_DkgProposal_EventDKGCommitConfirmationReceived(t *testing.T) {
var fsmResponse *fsm.Response var (
fsmResponse *fsm.Response
testFSMDumpLocal []byte
)
testFSMDumpLocal = testFSMDump[dpf.StateDkgCommitsAwaitConfirmations]
for participantId, participant := range testIdMapParticipants { for participantId, participant := range testIdMapParticipants {
testFSMInstance, err := FromDump(testFSMDump) testFSMInstance, err := FromDump(testFSMDumpLocal)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(dpf.EventDKGCommitConfirmationReceived, requests.DKGProposalCommitConfirmationRequest{ inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgCommitsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(dpf.EventDKGCommitConfirmationReceived, requests.DKGProposalCommitConfirmationRequest{
ParticipantId: participantId, ParticipantId: participantId,
Commit: participant.DkgCommit, Commit: participant.DkgCommit,
CreatedAt: tm, CreatedAt: tm,
@ -303,10 +377,10 @@ func Test_DkgProposal_EventDKGCommitConfirmationReceived(t *testing.T) {
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
} }
compareState(t, dpf.StateDkgDealsAwaitConfirmations, fsmResponse.State) compareState(t, dpf.StateDkgDealsAwaitConfirmations, fsmResponse.State)
@ -334,20 +408,84 @@ func Test_DkgProposal_EventDKGCommitConfirmationReceived(t *testing.T) {
t.Fatalf("expected valid {DkgCommit}") t.Fatalf("expected valid {DkgCommit}")
} }
} }
testFSMDump[dpf.StateDkgDealsAwaitConfirmations] = testFSMDumpLocal
compareDumpNotZero(t, testFSMDump[dpf.StateDkgDealsAwaitConfirmations])
}
func Test_DkgProposal_EventDKGCommitConfirmationReceived_Canceled_Error(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgCommitsAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgCommitsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGCommitConfirmationError, requests.DKGProposalConfirmationErrorRequest{
ParticipantId: 0,
Error: errors.New("test error"),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgCommitsAwaitCanceledByError, fsmResponse.State)
}
func Test_DkgProposal_EventDKGCommitConfirmationReceived_Canceled_Timeout(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgCommitsAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgCommitsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGCommitConfirmationReceived, requests.DKGProposalCommitConfirmationRequest{
ParticipantId: 0,
Commit: testIdMapParticipants[0].DkgCommit,
CreatedAt: time.Now().Add(36 * time.Hour),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgCommitsAwaitCanceledByTimeout, fsmResponse.State)
} }
// Deals // Deals
func Test_DkgProposal_EventDKGDealConfirmationReceived(t *testing.T) { func Test_DkgProposal_EventDKGDealConfirmationReceived(t *testing.T) {
var fsmResponse *fsm.Response var (
fsmResponse *fsm.Response
testFSMDumpLocal []byte
)
testFSMDumpLocal = testFSMDump[dpf.StateDkgDealsAwaitConfirmations]
for participantId, participant := range testIdMapParticipants { for participantId, participant := range testIdMapParticipants {
testFSMInstance, err := FromDump(testFSMDump) testFSMInstance, err := FromDump(testFSMDumpLocal)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(dpf.EventDKGDealConfirmationReceived, requests.DKGProposalDealConfirmationRequest{ inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgDealsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(dpf.EventDKGDealConfirmationReceived, requests.DKGProposalDealConfirmationRequest{
ParticipantId: participantId, ParticipantId: participantId,
Deal: participant.DkgDeal, Deal: participant.DkgDeal,
CreatedAt: tm, CreatedAt: tm,
@ -355,10 +493,10 @@ func Test_DkgProposal_EventDKGDealConfirmationReceived(t *testing.T) {
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
// Deals reached, next stage // Deals reached, next stage
if fsmResponse.State == dpf.StateDkgResponsesAwaitConfirmations { if fsmResponse.State == dpf.StateDkgResponsesAwaitConfirmations {
break break
@ -391,22 +529,84 @@ func Test_DkgProposal_EventDKGDealConfirmationReceived(t *testing.T) {
t.Fatalf("expected valid {DkgDeal}") t.Fatalf("expected valid {DkgDeal}")
} }
} }
testFSMDump[dpf.StateDkgResponsesAwaitConfirmations] = testFSMDumpLocal
compareDumpNotZero(t, testFSMDump[dpf.StateDkgResponsesAwaitConfirmations])
}
func Test_DkgProposal_EventDKGDealConfirmationReceived_Canceled_Error(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgDealsAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgDealsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGDealConfirmationError, requests.DKGProposalConfirmationErrorRequest{
ParticipantId: 0,
Error: errors.New("test error"),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgDealsAwaitCanceledByError, fsmResponse.State)
}
func Test_DkgProposal_EventDKGDealConfirmationReceived_Canceled_Timeout(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgDealsAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgDealsAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGDealConfirmationReceived, requests.DKGProposalDealConfirmationRequest{
ParticipantId: 0,
Deal: testIdMapParticipants[0].DkgDeal,
CreatedAt: time.Now().Add(36 * time.Hour),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgDealsAwaitCanceledByTimeout, fsmResponse.State)
} }
// Responses // Responses
func Test_DkgProposal_EventDKGResponseConfirmationReceived(t *testing.T) { func Test_DkgProposal_EventDKGResponseConfirmationReceived_Positive(t *testing.T) {
var fsmResponse *fsm.Response var (
fsmResponse *fsm.Response
testFSMDumpLocal []byte
)
testFSMDumpLocal = testFSMDump[dpf.StateDkgResponsesAwaitConfirmations]
pCounter := 0
for participantId, participant := range testIdMapParticipants { for participantId, participant := range testIdMapParticipants {
pCounter-- testFSMInstance, err := FromDump(testFSMDumpLocal)
testFSMInstance, err := FromDump(testFSMDump)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(dpf.EventDKGResponseConfirmationReceived, requests.DKGProposalResponseConfirmationRequest{ inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgResponsesAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(dpf.EventDKGResponseConfirmationReceived, requests.DKGProposalResponseConfirmationRequest{
ParticipantId: participantId, ParticipantId: participantId,
Response: participant.DkgResponse, Response: participant.DkgResponse,
CreatedAt: tm, CreatedAt: tm,
@ -414,10 +614,9 @@ func Test_DkgProposal_EventDKGResponseConfirmationReceived(t *testing.T) {
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
} }
compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, fsmResponse.State) compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, fsmResponse.State)
@ -445,70 +644,238 @@ func Test_DkgProposal_EventDKGResponseConfirmationReceived(t *testing.T) {
t.Fatalf("expected valid {DkgResponse}") t.Fatalf("expected valid {DkgResponse}")
} }
} }
testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations] = testFSMDumpLocal
compareDumpNotZero(t, testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations])
}
func Test_DkgProposal_EventDKGResponseConfirmationError_Canceled_Error(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgResponsesAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgResponsesAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGResponseConfirmationError, requests.DKGProposalConfirmationErrorRequest{
ParticipantId: 0,
Error: errors.New("test error"),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgResponsesAwaitCanceledByError, fsmResponse.State)
}
func Test_DkgProposal_EventDKGResponseConfirmationReceived_Canceled_Timeout(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgResponsesAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgResponsesAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGResponseConfirmationReceived, requests.DKGProposalResponseConfirmationRequest{
ParticipantId: 0,
Response: testIdMapParticipants[0].DkgResponse,
CreatedAt: time.Now().Add(36 * time.Hour),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgResponsesAwaitCanceledByTimeout, fsmResponse.State)
} }
// Master keys // Master keys
func Test_DkgProposal_EventDKGMasterKeyConfirmationReceived(t *testing.T) { func Test_DkgProposal_EventDKGMasterKeyConfirmationReceived_Positive(t *testing.T) {
var fsmResponse *fsm.Response var (
fsmResponse *fsm.Response
testFSMDumpLocal []byte
)
pCounter := 0 testFSMDumpLocal = testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations]
for participantId, participant := range testIdMapParticipants {
pCounter-- masterKeyMockup := genDataMock(keysMockLen)
testFSMInstance, err := FromDump(testFSMDump)
for participantId, _ := range testIdMapParticipants {
testFSMInstance, err := FromDump(testFSMDumpLocal)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationReceived, requests.DKGProposalMasterKeyConfirmationRequest{ inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationReceived, requests.DKGProposalMasterKeyConfirmationRequest{
ParticipantId: participantId, ParticipantId: participantId,
MasterKey: participant.DkgPartialKey, MasterKey: masterKeyMockup,
CreatedAt: tm, CreatedAt: tm,
}) })
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
} }
compareState(t, dpf.StateDkgMasterKeyCollected, fsmResponse.State) compareState(t, dpf.StateDkgMasterKeyCollected, fsmResponse.State)
testFSMDump[dpf.StateDkgMasterKeyCollected] = testFSMDumpLocal
compareDumpNotZero(t, testFSMDump[dpf.StateDkgMasterKeyCollected])
return return
} }
// Signing func Test_DkgProposal_EventDKGMasterKeyConfirmationError_Canceled_Error(t *testing.T) {
func Test_SigningProposal_Positive(t *testing.T) { testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations])
var fsmResponse *fsm.Response
testFSMInstance, err := FromDump(testFSMDump)
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(sif.EventSigningInit, requests.DefaultRequest{ inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationError, requests.DKGProposalConfirmationErrorRequest{
ParticipantId: 0,
Error: errors.New("test error"),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgMasterKeyAwaitCanceledByError, fsmResponse.State)
}
func Test_DkgProposal_EventDKGMasterKeyConfirmationReceived_Canceled_Timeout(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationReceived, requests.DKGProposalMasterKeyConfirmationRequest{
ParticipantId: 0,
MasterKey: genDataMock(keysMockLen),
CreatedAt: time.Now().Add(36 * time.Hour),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgMasterKeyAwaitCanceledByTimeout, fsmResponse.State)
}
func Test_DkgProposal_EventDKGMasterKeyConfirmationReceived_Canceled_Mismatched(t *testing.T) {
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgMasterKeyAwaitConfirmations])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgMasterKeyAwaitConfirmations, inState)
fsmResponse, testFSMDumpLocal, err := testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationReceived, requests.DKGProposalMasterKeyConfirmationRequest{
ParticipantId: 0,
MasterKey: genDataMock(keysMockLen),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
fsmResponse, testFSMDumpLocal, err = testFSMInstance.Do(dpf.EventDKGMasterKeyConfirmationReceived, requests.DKGProposalMasterKeyConfirmationRequest{
ParticipantId: 1,
MasterKey: genDataMock(keysMockLen),
CreatedAt: time.Now(),
})
compareErrNil(t, err)
compareFSMResponseNotNil(t, fsmResponse)
compareDumpNotZero(t, testFSMDumpLocal)
compareState(t, dpf.StateDkgMasterKeyAwaitCanceledByError, fsmResponse.State)
}
// Signing
func Test_SigningProposal_EventSigningInit(t *testing.T) {
var fsmResponse *fsm.Response
testFSMInstance, err := FromDump(testFSMDump[dpf.StateDkgMasterKeyCollected])
compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance)
inState, _ := testFSMInstance.State()
compareState(t, dpf.StateDkgMasterKeyCollected, inState)
fsmResponse, testFSMDump[sif.StateSigningIdle], err = testFSMInstance.Do(sif.EventSigningInit, requests.DefaultRequest{
CreatedAt: time.Now(), CreatedAt: time.Now(),
}) })
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareState(t, sif.StateSigningIdle, fsmResponse.State) compareState(t, sif.StateSigningIdle, fsmResponse.State)
// Start compareDumpNotZero(t, testFSMDump[sif.StateSigningIdle])
testFSMInstance, err = FromDump(testFSMDump) }
// Start
func Test_SigningProposal_EventSigningStart(t *testing.T) {
var fsmResponse *fsm.Response
testFSMInstance, err := FromDump(testFSMDump[sif.StateSigningIdle])
compareErrNil(t, err) compareErrNil(t, err)
compareFSMInstanceNotNil(t, testFSMInstance) compareFSMInstanceNotNil(t, testFSMInstance)
fsmResponse, testFSMDump, err = testFSMInstance.Do(sif.EventSigningStart, requests.SigningProposalStartRequest{ inState, _ := testFSMInstance.State()
compareState(t, sif.StateSigningIdle, inState)
fsmResponse, testFSMDump[sif.StateSigningAwaitConfirmations], err = testFSMInstance.Do(sif.EventSigningStart, requests.SigningProposalStartRequest{
ParticipantId: 1, ParticipantId: 1,
SrcPayload: []byte("message to sign"), SrcPayload: []byte("message to sign"),
CreatedAt: time.Now(), CreatedAt: time.Now(),
@ -516,12 +883,12 @@ func Test_SigningProposal_Positive(t *testing.T) {
compareErrNil(t, err) compareErrNil(t, err)
compareDumpNotZero(t, testFSMDump)
compareFSMResponseNotNil(t, fsmResponse) compareFSMResponseNotNil(t, fsmResponse)
compareState(t, sif.StateSigningAwaitConfirmations, fsmResponse.State) compareState(t, sif.StateSigningAwaitConfirmations, fsmResponse.State)
compareDumpNotZero(t, testFSMDump[sif.StateSigningAwaitConfirmations])
testSigningParticipantsListResponse, ok := fsmResponse.Data.(responses.SigningProposalParticipantInvitationsResponse) testSigningParticipantsListResponse, ok := fsmResponse.Data.(responses.SigningProposalParticipantInvitationsResponse)
if !ok { if !ok {

View File

@ -31,11 +31,3 @@ type DKGProposalResponseParticipantEntry struct {
Addr string Addr string
DkgResponse []byte DkgResponse []byte
} }
type DKGProposalResponsesParticipantResponse []*DKGProposalResponsesParticipantEntry
type DKGProposalResponsesParticipantEntry struct {
ParticipantId int
Title string
Responses []byte
}