This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
registry.go
102 lines (82 loc) · 2.6 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package testhelper
import (
"fmt"
"github.com/keep-network/keep-common/pkg/persistence"
"github.com/keep-network/keep-ecdsa/pkg/ecdsa/tss"
)
// PersistenceHandleMock is a mock of persistence handle used in tests.
type PersistenceHandleMock struct {
PersistedGroups []*TestFileInfo
Snapshots []*TestFileInfo
ArchivedGroups []string
outputDataChan chan persistence.DataDescriptor
outputErrorsChan chan error
}
// NewPersistenceHandleMock creates a mocked persistence handle.
func NewPersistenceHandleMock(outputDataChanSize int) *PersistenceHandleMock {
return &PersistenceHandleMock{
outputDataChan: make(chan persistence.DataDescriptor, outputDataChanSize),
outputErrorsChan: make(chan error),
}
}
// TestFileInfo holds test data stored in persistence handle.
type TestFileInfo struct {
Data []byte
Directory string
Name string
}
// Save stores data in persistence handle.
func (phm *PersistenceHandleMock) Save(data []byte, directory string, name string) error {
phm.PersistedGroups = append(
phm.PersistedGroups,
&TestFileInfo{data, directory, name},
)
return nil
}
// Snapshot creates a snapshot of data in persistence handle.
func (phm *PersistenceHandleMock) Snapshot(data []byte, directory string, name string) error {
phm.Snapshots = append(
phm.Snapshots,
&TestFileInfo{data, directory, name},
)
return nil
}
// MockSigner registers a mock of a signer for membership and keep.
func (phm *PersistenceHandleMock) MockSigner(membershipIndex int, keepID string, signer *tss.ThresholdSigner) error {
signerBytes, err := signer.Marshal()
if err != nil {
return fmt.Errorf("failed to marshal signer: %w", err)
}
phm.outputDataChan <- &testDataDescriptor{
fmt.Sprintf("/membership_%d", membershipIndex),
keepID,
signerBytes,
}
return nil
}
// ReadAll reads all data stored in persistence handle.
func (phm *PersistenceHandleMock) ReadAll() (<-chan persistence.DataDescriptor, <-chan error) {
close(phm.outputDataChan)
close(phm.outputErrorsChan)
return phm.outputDataChan, phm.outputErrorsChan
}
// Archive archives data in persistence handle.
func (phm *PersistenceHandleMock) Archive(directory string) error {
phm.ArchivedGroups = append(phm.ArchivedGroups, directory)
phm.PersistedGroups = phm.PersistedGroups[:len(phm.ArchivedGroups)-1]
return nil
}
type testDataDescriptor struct {
name string
directory string
content []byte
}
func (tdd *testDataDescriptor) Name() string {
return tdd.name
}
func (tdd *testDataDescriptor) Directory() string {
return tdd.directory
}
func (tdd *testDataDescriptor) Content() ([]byte, error) {
return tdd.content, nil
}