Skip to content

Commit 17e41e3

Browse files
author
Andrei Smirnov
authored
*: dkg and pk ux improvements (#4246)
After recent issues investigation, we would like to introduce these several improvement to UX, for dkg and privkey lock specifically. The main goal is to print better error messages. category: refactor ticket: none
1 parent f9042d5 commit 17e41e3

4 files changed

Lines changed: 80 additions & 26 deletions

File tree

‎app/privkeylock/privkeylock.go‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"os"
9+
"path/filepath"
910
"time"
1011

1112
"github.com/obolnetwork/charon/app/errors"
@@ -26,28 +27,56 @@ var (
2627

2728
// New returns new private key locking service. It errors if a recently-updated private key lock file exists.
2829
func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error) {
30+
// Sanity check: private key file exists
31+
info, err := os.Stat(privKeyFilePath)
32+
if err != nil {
33+
if errors.Is(err, os.ErrNotExist) {
34+
return Service{}, errors.New("private key file does not exist, please check the path", z.Str("path", privKeyFilePath))
35+
}
36+
37+
return Service{}, errors.Wrap(err, "fetch file info for private key file", z.Str("path", privKeyFilePath))
38+
}
39+
40+
if info.IsDir() {
41+
return Service{}, errors.New("private key file path is a directory, please check the path", z.Str("path", privKeyFilePath))
42+
}
43+
44+
privKeyFileLockPath := privKeyFilePath + ".lock"
45+
46+
// Sanity check: we can write to the directory
47+
parentDir := filepath.Dir(privKeyFileLockPath)
48+
if info, err := os.Stat(parentDir); err != nil {
49+
if errors.Is(err, os.ErrNotExist) {
50+
return Service{}, errors.New("parent directory, fetched from private key lock file path, does not exist", z.Str("parent_directory", parentDir), z.Str("lock_path", privKeyFileLockPath))
51+
}
52+
53+
return Service{}, errors.Wrap(err, "check parent directory", z.Str("dir", parentDir))
54+
} else if !info.IsDir() {
55+
return Service{}, errors.New("parent path is not a directory", z.Str("path", parentDir))
56+
} else if info.Mode().Perm()&0o200 == 0 {
57+
return Service{}, errors.New("parent directory is not writable", z.Str("dir", parentDir))
58+
}
59+
2960
clusterLockHash, err := readClusterLockHash(clusterLockFilePath)
3061
if err != nil {
3162
return Service{}, err
3263
}
3364

34-
privKeyFilePath += ".lock"
35-
36-
content, err := os.ReadFile(privKeyFilePath)
65+
content, err := os.ReadFile(privKeyFileLockPath)
3766
if errors.Is(err, os.ErrNotExist) { //nolint:revive // Empty block is fine.
3867
// No file, we will create it in run
3968
} else if err != nil {
40-
return Service{}, errors.Wrap(err, "read private key lock file", z.Str("path", privKeyFilePath))
69+
return Service{}, errors.Wrap(err, "read private key lock file", z.Str("path", privKeyFileLockPath))
4170
} else {
4271
var meta metadata
4372
if err := json.Unmarshal(content, &meta); err != nil {
44-
return Service{}, errors.Wrap(err, "decode private key lock file", z.Str("path", privKeyFilePath))
73+
return Service{}, errors.Wrap(err, "decode private key lock file", z.Str("path", privKeyFileLockPath))
4574
}
4675

4776
if time.Since(meta.Timestamp) <= staleDuration {
4877
return Service{}, errors.New(
4978
"existing private key lock file found, another charon instance may be running on your machine",
50-
z.Str("path", privKeyFilePath),
79+
z.Str("path", privKeyFileLockPath),
5180
z.Str("command", meta.Command),
5281
z.Str("cluster_lock_hash", meta.ClusterLockHash),
5382
)
@@ -61,7 +90,7 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
6190

6291
return Service{}, errors.New(
6392
errText,
64-
z.Str("path", privKeyFilePath),
93+
z.Str("path", privKeyFileLockPath),
6594
z.Str("command", meta.Command),
6695
z.Str("existing_cluster_lock_hash", meta.ClusterLockHash),
6796
z.Str("current_cluster_lock_hash", clusterLockHash),
@@ -71,14 +100,14 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
71100
}
72101
}
73102

74-
if err := writeFile(privKeyFilePath, clusterLockHash, command, time.Now()); err != nil {
103+
if err := writePrivateKeyLockFile(privKeyFileLockPath, clusterLockHash, command, time.Now()); err != nil {
75104
return Service{}, err
76105
}
77106

78107
return Service{
79108
clusterLockHash: clusterLockHash,
109+
lockFilePath: privKeyFileLockPath,
80110
command: command,
81-
path: privKeyFilePath,
82111
updatePeriod: updatePeriod,
83112
quit: make(chan struct{}),
84113
done: make(chan struct{}),
@@ -88,8 +117,8 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
88117
// Service is a private key locking service.
89118
type Service struct {
90119
clusterLockHash string
120+
lockFilePath string
91121
command string
92-
path string
93122
updatePeriod time.Duration
94123
quit chan struct{} // Quit exits the Run goroutine if closed.
95124
done chan struct{} // Done is closed when Run exits, which exits the Close goroutine.
@@ -105,14 +134,14 @@ func (s Service) Run() error {
105134
for {
106135
select {
107136
case <-s.quit:
108-
if err := os.Remove(s.path); err != nil {
137+
if err := os.Remove(s.lockFilePath); err != nil {
109138
return errors.Wrap(err, "delete private key lock file")
110139
}
111140

112141
return nil
113142
case <-tick.C:
114143
// Overwrite lockfile with new metadata
115-
if err := writeFile(s.path, s.clusterLockHash, s.command, time.Now()); err != nil {
144+
if err := writePrivateKeyLockFile(s.lockFilePath, s.clusterLockHash, s.command, time.Now()); err != nil {
116145
return err
117146
}
118147
}
@@ -133,15 +162,22 @@ type metadata struct {
133162
ClusterLockHash string `json:"cluster_lock_hash,omitempty"`
134163
}
135164

136-
// writeFile creates or updates the file with the latest metadata.
137-
func writeFile(path, clusterLockHash, command string, now time.Time) error {
165+
// writePrivateKeyLockFile creates or updates the lock file with the latest metadata.
166+
func writePrivateKeyLockFile(path, clusterLockHash, command string, now time.Time) error {
138167
b, err := json.Marshal(metadata{Command: command, Timestamp: now, ClusterLockHash: clusterLockHash})
139168
if err != nil {
140169
return errors.Wrap(err, "marshal private key lock file")
141170
}
142171

143172
//nolint:gosec // Readable and writable for all users is fine for this file.
144173
if err := os.WriteFile(path, b, 0o666); err != nil {
174+
if errors.Is(err, os.ErrPermission) {
175+
return errors.New(
176+
"permission denied while writing lock file, please check file permissions",
177+
z.Str("path", path),
178+
)
179+
}
180+
145181
return errors.Wrap(err, "write private key lock file", z.Str("path", path))
146182
}
147183

‎app/privkeylock/privkeylock_internal_test.go‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ func TestService(t *testing.T) {
2020
lockFilePath := filepath.Join(tmpDir, "cluster-lock.json")
2121
lockPath := privKeyPath + ".lock"
2222

23-
// Create cluster lock file.
23+
// Create test files.
2424
writeClusterLockFile(t, lockFilePath, "hash123")
25+
writePrivateKeyFile(t, privKeyPath)
2526

2627
// Create a stale file that is ignored.
27-
err := writeFile(lockPath, "hash123", "test", time.Now().Add(-staleDuration))
28+
err := writePrivateKeyLockFile(lockPath, "hash123", "test", time.Now().Add(-staleDuration))
2829
require.NoError(t, err)
2930

3031
// Create a new service.
@@ -65,11 +66,12 @@ func TestClusterHashMismatchWithinGracePeriod(t *testing.T) {
6566
lockFilePath := filepath.Join(tmpDir, "cluster-lock.json")
6667
lockPath := privKeyPath + ".lock"
6768

68-
// Create cluster lock file with hash1.
69+
// Create test files.
6970
writeClusterLockFile(t, lockFilePath, "hash1")
71+
writePrivateKeyFile(t, privKeyPath)
7072

7173
// Create a stale but within grace period lock file with hash1.
72-
err := writeFile(lockPath, "hash1", "test", time.Now().Add(-staleDuration-time.Second))
74+
err := writePrivateKeyLockFile(lockPath, "hash1", "test", time.Now().Add(-staleDuration-time.Second))
7375
require.NoError(t, err)
7476

7577
// Update cluster lock file to hash2.
@@ -88,11 +90,12 @@ func TestClusterHashMismatchAfterGracePeriod(t *testing.T) {
8890
lockFilePath := filepath.Join(tmpDir, "cluster-lock.json")
8991
lockPath := privKeyPath + ".lock"
9092

91-
// Create cluster lock file with hash1.
93+
// Create test files.
9294
writeClusterLockFile(t, lockFilePath, "hash1")
95+
writePrivateKeyFile(t, privKeyPath)
9396

9497
// Create an old lock file with hash1 (beyond grace period).
95-
err := writeFile(lockPath, "hash1", "test", time.Now().Add(-gracePeriod-time.Second))
98+
err := writePrivateKeyLockFile(lockPath, "hash1", "test", time.Now().Add(-gracePeriod-time.Second))
9699
require.NoError(t, err)
97100

98101
// Update cluster lock file to hash2.
@@ -119,11 +122,12 @@ func TestClusterHashMatchWithinGracePeriod(t *testing.T) {
119122
lockFilePath := filepath.Join(tmpDir, "cluster-lock.json")
120123
lockPath := privKeyPath + ".lock"
121124

122-
// Create cluster lock file with hash1.
125+
// Create test files.
123126
writeClusterLockFile(t, lockFilePath, "hash1")
127+
writePrivateKeyFile(t, privKeyPath)
124128

125129
// Create a recent lock file with hash1 (within stale duration).
126-
err := writeFile(lockPath, "hash1", "test", time.Now().Add(-time.Second))
130+
err := writePrivateKeyLockFile(lockPath, "hash1", "test", time.Now().Add(-time.Second))
127131
require.NoError(t, err)
128132

129133
// Try to create service with same hash - should fail due to staleness check.
@@ -132,7 +136,7 @@ func TestClusterHashMatchWithinGracePeriod(t *testing.T) {
132136
require.ErrorContains(t, err, "another charon instance may be running")
133137

134138
// Now create a stale lock file with same hash (beyond stale duration but within grace period).
135-
err = writeFile(lockPath, "hash1", "test", time.Now().Add(-staleDuration-time.Second))
139+
err = writePrivateKeyLockFile(lockPath, "hash1", "test", time.Now().Add(-staleDuration-time.Second))
136140
require.NoError(t, err)
137141

138142
// Should succeed since hash matches and file is stale.
@@ -145,6 +149,9 @@ func TestClusterLockFileNotFound(t *testing.T) {
145149
privKeyPath := filepath.Join(tmpDir, "privkey")
146150
lockFilePath := filepath.Join(tmpDir, "nonexistent.json")
147151

152+
// Create test files.
153+
writePrivateKeyFile(t, privKeyPath)
154+
148155
// Should succeed when cluster lock file doesn't exist (e.g., during DKG).
149156
// The cluster lock hash will be empty.
150157
_, err := New(privKeyPath, lockFilePath, "test")
@@ -185,17 +192,26 @@ func writeClusterLockFile(t *testing.T, path, lockHash string) {
185192
require.NoError(t, err)
186193
}
187194

195+
// writePrivateKeyFile creates a dummy private key file.
196+
func writePrivateKeyFile(t *testing.T, path string) {
197+
t.Helper()
198+
199+
err := os.WriteFile(path, []byte("private key content"), 0o644)
200+
require.NoError(t, err)
201+
}
202+
188203
func TestEmptyHashToHashMigration(t *testing.T) {
189204
tmpDir := t.TempDir()
190205
privKeyPath := filepath.Join(tmpDir, "privkey")
191206
lockFilePath := filepath.Join(tmpDir, "cluster-lock.json")
192207
lockPath := privKeyPath + ".lock"
193208

194-
// Create cluster lock file.
209+
// Create test files.
195210
writeClusterLockFile(t, lockFilePath, "newhash")
211+
writePrivateKeyFile(t, privKeyPath)
196212

197213
// Create a stale lock file with empty cluster hash (migration scenario).
198-
err := writeFile(lockPath, "", "test", time.Now().Add(-staleDuration*2))
214+
err := writePrivateKeyLockFile(lockPath, "", "test", time.Now().Add(-staleDuration*2))
199215
require.NoError(t, err)
200216

201217
// Should succeed - empty hash shouldn't trigger grace period.

‎dkg/dkg.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ func Run(ctx context.Context, conf Config) (err error) {
284284

285285
nextStepSync, stopSync, err := startSyncProtocol(ctx, p2pNode, key, def.DefinitionHash, peerIDs, cancel, conf.TestConfig, conf.Nickname)
286286
if err != nil {
287+
log.Error(ctx, "Failed to start sync protocol, please make sure you don't have any other long-running charon or dkg processes running", err)
288+
287289
return err
288290
}
289291

‎dkg/sync/client.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (c *Client) Run(ctx context.Context) error {
8484

8585
stream, err := c.connect(ctx)
8686
if err != nil {
87-
return errors.Wrap(err, "client connect", z.Str("peer", p2p.PeerName(c.peer)))
87+
return errors.Wrap(err, "client failed to connect, allow time before reconnecting", z.Str("peer", p2p.PeerName(c.peer)))
8888
}
8989

9090
c.setConnected()

0 commit comments

Comments
 (0)