feat: added generation transactionId for `Create()`

This commit is contained in:
x88 2020-08-11 12:35:43 +03:00
parent 7a910a0e8d
commit 1e8af819bb
2 changed files with 38 additions and 13 deletions

View File

@ -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
}

View File

@ -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)
}
}