Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #111 from lidofinance/fix/operation-id
Browse files Browse the repository at this point in the history
fix: non-random operation ID
  • Loading branch information
zavgorodnii authored Dec 8, 2020
2 parents 2fe22e9 + 814ab52 commit 2a3fdeb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
15 changes: 6 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

sipf "github.com/lidofinance/dc4bc/fsm/state_machines/signing_proposal_fsm"

"github.com/google/uuid"
"github.com/lidofinance/dc4bc/client/types"
"github.com/lidofinance/dc4bc/fsm/types/requests"

Expand Down Expand Up @@ -312,18 +311,16 @@ func (c *BaseClient) ProcessMessage(message storage.Message) error {
}
}

bz, err := json.Marshal(resp.Data)
operationPayloadBz, err := json.Marshal(resp.Data)
if err != nil {
return fmt.Errorf("failed to marshal FSM response: %w", err)
}

operation = &types.Operation{
ID: uuid.New().String(),
Type: types.OperationType(resp.State),
Payload: bz,
DKGIdentifier: message.DkgRoundID,
CreatedAt: time.Now(),
}
operation = types.NewOperation(
message.DkgRoundID,
operationPayloadBz,
resp.State,
)
}
default:
c.Logger.Log("State %s does not require an operation", resp.State)
Expand Down
22 changes: 22 additions & 0 deletions client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package types

import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -43,6 +45,26 @@ type Operation struct {
Event fsm.Event
}

func NewOperation(
dkgRoundID string,
payload []byte,
state fsm.State,
) *Operation {
operationID := fmt.Sprintf(
"%s_%s",
dkgRoundID,
base64.StdEncoding.EncodeToString(payload),
)
operationIDmd5 := md5.Sum([]byte(operationID))
return &Operation{
ID: string(operationIDmd5[:]),
Type: OperationType(state),
Payload: payload,
DKGIdentifier: dkgRoundID,
CreatedAt: time.Now(),
}
}

func (o *Operation) Check(o2 *Operation) error {
if o.ID != o2.ID {
return fmt.Errorf("o1.ID (%s) != o2.ID (%s)", o.ID, o2.ID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dc4bc_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func getFSMStatusCommand() *cobra.Command {
fmt.Printf("Waiting for data from: %s\n", strings.Join(waiting, ", "))
}
if len(confirmed) > 0 {
fmt.Printf("Received a data from: %s\n", strings.Join(confirmed, ", "))
fmt.Printf("Received data from: %s\n", strings.Join(confirmed, ", "))
}
if len(failed) > 0 {
fmt.Printf("Participants who got some error during a process: %s\n", strings.Join(waiting, ", "))
Expand Down

0 comments on commit 2a3fdeb

Please sign in to comment.