From 1e8af819bb91d170c4fc7660f2cc2361cfb12579 Mon Sep 17 00:00:00 2001 From: x88 Date: Tue, 11 Aug 2020 12:35:43 +0300 Subject: [PATCH] feat: added generation transactionId for `Create()` --- fsm/state_machines/provider.go | 30 ++++++++++++++++++++++++++--- fsm/state_machines/provider_test.go | 21 ++++++++++---------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/fsm/state_machines/provider.go b/fsm/state_machines/provider.go index db147d3..93f4ce9 100644 --- a/fsm/state_machines/provider.go +++ b/fsm/state_machines/provider.go @@ -1,6 +1,8 @@ package state_machines import ( + "crypto/rand" + "encoding/base64" "encoding/json" "errors" "github.com/depools/dc4bc/fsm/state_machines/dkg_proposal_fsm" @@ -12,6 +14,10 @@ import ( "github.com/depools/dc4bc/fsm/state_machines/signature_proposal_fsm" ) +const ( + dkgTransactionIdLength = 128 +) + // Is machine state scope dump will be locked? type FSMDump struct { TransactionId string @@ -37,9 +43,17 @@ func init() { // Create new fsm with unique id // transactionId required for unique identify dump -func Create(transactionId string) (*FSMInstance, error) { - var err error - i := &FSMInstance{} +func Create() (*FSMInstance, error) { + var ( + err error + i = &FSMInstance{} + ) + transactionId, err := generateDkgTransactionId() + + if err != nil { + return nil, err + } + err = i.InitDump(transactionId) if err != nil { @@ -148,3 +162,13 @@ func (d *FSMDump) Unmarshal(data []byte) error { return json.Unmarshal(data, d) } + +func generateDkgTransactionId() (string, error) { + b := make([]byte, dkgTransactionIdLength) + _, err := rand.Read(b) + if err != nil { + return "", err + } + + return base64.URLEncoding.EncodeToString(b), err +} diff --git a/fsm/state_machines/provider_test.go b/fsm/state_machines/provider_test.go index 701a163..5a17911 100644 --- a/fsm/state_machines/provider_test.go +++ b/fsm/state_machines/provider_test.go @@ -78,7 +78,7 @@ func init() { } func TestCreate_Positive(t *testing.T) { - testFSMInstance, err := Create(testTransactionId) + testFSMInstance, err := Create() if err != nil { t.Fatalf("expected nil error, got {%s}", err) } @@ -88,13 +88,6 @@ func TestCreate_Positive(t *testing.T) { } } -func TestCreate_Negative(t *testing.T) { - _, err := Create("") - if err == nil { - t.Fatalf("expected error for empty {transactionId}") - } -} - func compareErrNil(t *testing.T, got error) { if got != nil { t.Fatalf("expected nil error, got {%s}", got) @@ -126,7 +119,9 @@ func compareState(t *testing.T, expected fsm.State, got fsm.State) { } func Test_Workflow(t *testing.T) { - testFSMInstance, err := Create(testTransactionId) + testFSMInstance, err := Create() + + log.Println(testFSMInstance.Id()) compareErrNil(t, err) @@ -148,6 +143,10 @@ func Test_Workflow(t *testing.T) { compareState(t, spf.StateAwaitParticipantsConfirmations, fsmResponse.State) + if testFSMInstance.Id() != testTransactionId { + t.Fatalf("expected {testTransactionId}") + } + testParticipantsListResponse, ok := fsmResponse.Data.(responses.SignatureProposalParticipantInvitationsResponse) if !ok { @@ -227,7 +226,9 @@ func Test_Workflow(t *testing.T) { } } - log.Println(fsmResponse.State, err) + state, err := testFSMInstance.State() + + log.Println(err, state) } }