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

Commit

Permalink
fix: LDC-04 Miscellaneous General Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyWh1te committed Feb 22, 2023
1 parent 7a391fa commit 665af4b
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 180 deletions.
16 changes: 8 additions & 8 deletions airgapped/airgapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package airgapped

import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -60,7 +61,7 @@ func NewMachine(dbPath string) (*Machine, error) {
}

if _, err = am.db.Get([]byte(operationsLogDBKey), nil); err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
operationsLogBz, _ := json.Marshal(RoundOperationLog{})
if err := am.db.Put([]byte(operationsLogDBKey), operationsLogBz, nil); err != nil {
return nil, fmt.Errorf("failed to init Operation log: %w", err)
Expand All @@ -79,14 +80,13 @@ func (am *Machine) SetResultFolder(resultFolder string) {

// InitKeys load keys public and private keys for DKG from LevelDB. If keys do not exist, it creates them.
func (am *Machine) InitKeys() error {
err := am.LoadKeysFromDB()
if err != nil && err != leveldb.ErrNotFound {
return fmt.Errorf("failed to load keys from db: %w", err)
}
if err := am.LoadKeysFromDB(); err != nil {
// If keys were not generated yet.
if errors.Is(err, leveldb.ErrNotFound) {
return am.GenerateKeys()
}

// If keys were not generated yet.
if err == leveldb.ErrNotFound {
return am.GenerateKeys()
return fmt.Errorf("failed to load keys from db: %w", err)
}

return nil
Expand Down
36 changes: 18 additions & 18 deletions airgapped/airgapped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func createTransport(participants []string) (*Transport, error) {
func createOperation(opType string, to string, req interface{}) (*client.Operation, error) {
reqBz, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %v", err)
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
op := &client.Operation{
ID: uuid.New().String(),
Expand Down Expand Up @@ -160,7 +160,7 @@ func (tr *Transport) initRequest(threshold int) error {
for _, n := range tr.nodes {
pubKey, err := n.Machine.pubKey.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal dkg pubkey: %v", err)
return fmt.Errorf("failed to marshal dkg pubkey: %w", err)
}
entry := &responses.SignatureProposalParticipantInvitationEntry{
ParticipantId: n.ParticipantID,
Expand Down Expand Up @@ -189,7 +189,7 @@ func (tr *Transport) commitsStep(threshold int) error {
for _, n := range tr.nodes {
pubKey, err := n.Machine.pubKey.MarshalBinary()
if err != nil {
return fmt.Errorf("%s: failed to marshal pubkey: %v", n.Participant, err)
return fmt.Errorf("%s: failed to marshal pubkey: %w", n.Participant, err)
}
entry := &responses.DKGProposalPubKeysParticipantEntry{
ParticipantId: n.ParticipantID,
Expand Down Expand Up @@ -335,28 +335,28 @@ func TestAirgappedAllSteps(t *testing.T) {

tr, err := createTransport(participants)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
t.Fatal(fmt.Errorf("failed to create transport: %w", err))
}
defer os.RemoveAll(testDir)

if err := tr.commitsStep(threshold); err != nil {
t.Fatalf("failed to do commits step: %v", err)
t.Fatal(fmt.Errorf("failed to do commits step: %w", err))
}

if err := tr.dealsStep(); err != nil {
t.Fatalf("failed to do deals step: %v", err)
t.Fatal(fmt.Errorf("failed to do deals step: %w", err))
}

if err := tr.responsesStep(); err != nil {
t.Fatalf("failed to do responses step: %v", err)
t.Fatal(fmt.Errorf("failed to do responses step: %w", err))
}

if err := tr.masterKeysStep(); err != nil {
t.Fatalf("failed to do master keys step: %v", err)
t.Fatal(fmt.Errorf("failed to do master keys step: %w", err))
}

if err := tr.checkReconstructedMasterKeys(); err != nil {
t.Fatalf("failed check master keys: %v", err)
t.Fatal(fmt.Errorf("failed check master keys: %w", err))
}

msgToSign := []requests.MessageToSign{
Expand All @@ -367,7 +367,7 @@ func TestAirgappedAllSteps(t *testing.T) {
}

if err := tr.partialSignsStep(successfulBatchSigningID, msgToSign); err != nil {
t.Fatalf("failed to do master keys step: %v", err)
t.Fatal(fmt.Errorf("failed to do master keys step: %w", err))
}

fmt.Println("DKG succeeded")
Expand All @@ -383,16 +383,16 @@ func TestAirgappedMachine_Replay(t *testing.T) {

tr, err := createTransport(participants)
if err != nil {
t.Fatalf("failed to create transport: %v", err)
t.Fatal(fmt.Errorf("failed to create transport: %w", err))
}
defer os.RemoveAll(testDir)

if err := tr.commitsStep(threshold); err != nil {
t.Fatalf("failed to do init request: %v", err)
t.Fatal(fmt.Errorf("failed to do init request: %w", err))
}

if err := tr.dealsStep(); err != nil {
t.Fatalf("failed to do init request: %v", err)
t.Fatal(fmt.Errorf("failed to do init request: %w", err))
}

// At this point something goes wrong and we have to restart the machines.
Expand All @@ -407,7 +407,7 @@ func TestAirgappedMachine_Replay(t *testing.T) {

newTr, err := createTransport(participants)
if err != nil {
t.Errorf("failed to create transport: %v", err)
t.Fatal(fmt.Errorf("failed to create transport: %w", err))
}
for i, n := range newTr.nodes {
n.deals = tr.nodes[i].deals
Expand All @@ -422,15 +422,15 @@ func TestAirgappedMachine_Replay(t *testing.T) {
tr = newTr

if err := tr.responsesStep(); err != nil {
t.Fatalf("failed to do init request: %v", err)
t.Fatal(fmt.Errorf("failed to do init request: %w", err))
}

if err := tr.masterKeysStep(); err != nil {
t.Fatalf("failed to do init request: %v", err)
t.Fatal(fmt.Errorf("failed to do init request: %w", err))
}

if err := tr.checkReconstructedMasterKeys(); err != nil {
t.Fatalf("failed check master keys: %v", err)
t.Fatal(fmt.Errorf("failed check master keys: %w", err))
}

msgToSign := []requests.MessageToSign{
Expand All @@ -441,7 +441,7 @@ func TestAirgappedMachine_Replay(t *testing.T) {
}

if err := tr.partialSignsStep(successfulBatchSigningID, msgToSign); err != nil {
t.Fatalf("failed to do init request: %v", err)
t.Fatal(fmt.Errorf("failed to do init request: %w", err))
}

fmt.Println("DKG succeeded")
Expand Down
14 changes: 7 additions & 7 deletions airgapped/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (am *Machine) getBaseSeed() ([]byte, error) {
func (am *Machine) storeOperation(o client.Operation) error {
roundOperationsLog, err := am.getRoundOperationLog()
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to get operationsLogBz from db: %w", err)
Expand All @@ -124,7 +124,7 @@ func (am *Machine) storeOperation(o client.Operation) error {
func (am *Machine) getOperationsLog(dkgIdentifier string) ([]client.Operation, error) {
roundOperationsLog, err := am.getRoundOperationLog()
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return nil, err
}
return nil, fmt.Errorf("failed to get operationsLogBz from db: %w", err)
Expand All @@ -141,7 +141,7 @@ func (am *Machine) getOperationsLog(dkgIdentifier string) ([]client.Operation, e
func (am *Machine) clearOperationsLog(dkgIdentifier string, remove func(o client.Operation) bool) error {
roundOperationsLog, err := am.getRoundOperationLog()
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to get operationsLogBz from db: %w", err)
Expand Down Expand Up @@ -173,7 +173,7 @@ func (am *Machine) clearOperationsLog(dkgIdentifier string, remove func(o client
func (am *Machine) dropRoundOperationLog(dkgIdentifier string) error {
roundOperationsLog, err := am.getRoundOperationLog()
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to get operationsLogBz from db: %w", err)
Expand Down Expand Up @@ -210,23 +210,23 @@ func (am *Machine) getRoundOperationLog() (RoundOperationLog, error) {
func (am *Machine) LoadKeysFromDB() error {
pubKeyBz, err := am.db.Get([]byte(pubKeyDBKey), nil)
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to get public key from db: %w", err)
}

privateKeyBz, err := am.db.Get([]byte(privateKeyDBKey), nil)
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to get private key from db: %w", err)
}

salt, err := am.db.Get([]byte(saltDBKey), nil)
if err != nil {
if err == leveldb.ErrNotFound {
if errors.Is(err, leveldb.ErrNotFound) {
return err
}
return fmt.Errorf("failed to read salt from db: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions client/api/http_api/handlers/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (a *HTTPApp) StartDKG(c echo.Context) error {
request := &req.StartDKGForm{}
request.Payload, err = ioutil.ReadAll(ctx.Request().Body)
if err != nil {
return ctx.JsonError(http.StatusBadRequest, fmt.Errorf("failed to read request body: %v", err))
return ctx.JsonError(http.StatusBadRequest, fmt.Errorf("failed to read request body: %w", err))
}
defer ctx.Request().Body.Close()

Expand Down Expand Up @@ -51,7 +51,7 @@ func (a *HTTPApp) ReInitDKG(c echo.Context) error {
formDTO := &ReInitDKGDTO{ID: request.ID}
formDTO.Payload, err = json.Marshal(request)
if err != nil {
return ctx.JsonError(http.StatusBadRequest, fmt.Errorf("failed to marshal request body: %v", err))
return ctx.JsonError(http.StatusBadRequest, fmt.Errorf("failed to marshal request body: %w", err))
}

if err = a.node.ReInitDKG(formDTO); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/api/http_api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *HTTPApp) GetOperation(c echo.Context) error {
if err != nil {
return stx.JsonError(
http.StatusInternalServerError,
fmt.Errorf("failed to get operations: %v", err),
fmt.Errorf("failed to get operations: %w", err),
)
}

Expand Down
2 changes: 1 addition & 1 deletion client/api/http_api/handlers/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (a *HTTPApp) GetStateOffset(c echo.Context) error {
stx := c.(*cs.ContextService)
offset, err := a.node.GetStateOffset()
if err != nil {
return stx.JsonError(http.StatusInternalServerError, fmt.Errorf("failed to load offset: %v", err))
return stx.JsonError(http.StatusInternalServerError, fmt.Errorf("failed to load offset: %w", err))
}
return stx.Json(http.StatusOK, offset)
}
Expand Down
Loading

0 comments on commit 665af4b

Please sign in to comment.