This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
provider.go
194 lines (155 loc) · 4.07 KB
/
provider.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package state_machines
import (
"crypto/ed25519"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/lidofinance/dc4bc/fsm/state_machines/signing_proposal_fsm"
"github.com/lidofinance/dc4bc/fsm/state_machines/dkg_proposal_fsm"
"github.com/lidofinance/dc4bc/fsm/fsm"
"github.com/lidofinance/dc4bc/fsm/fsm_pool"
"github.com/lidofinance/dc4bc/fsm/state_machines/internal"
"github.com/lidofinance/dc4bc/fsm/state_machines/signature_proposal_fsm"
)
// Is machine state scope dump will be locked?
type FSMDump struct {
TransactionId string
State fsm.State
Payload *internal.DumpedMachineStatePayload
}
type FSMInstance struct {
machine internal.DumpedMachineProvider
dump *FSMDump
}
type Participant interface {
GetStatus() internal.ParticipantStatus
GetUsername() string
}
// Create new fsm with unique id
// transactionId required for unique identify dump
func Create(dkgID string) (*FSMInstance, error) {
var (
err error
i = &FSMInstance{}
)
err = i.InitDump(dkgID)
if err != nil {
return nil, err
}
fsmPoolProvider := fsm_pool.Init(
signature_proposal_fsm.New(),
dkg_proposal_fsm.New(),
signing_proposal_fsm.New(),
)
machine, err := fsmPoolProvider.EntryPointMachine()
i.machine = machine.(internal.DumpedMachineProvider).
WithSetup(i.dump.State, i.dump.Payload)
return i, err
}
// DKGQuorumGet fsm from dump
func FromDump(data []byte) (*FSMInstance, error) {
var err error
if len(data) < 2 {
return nil, errors.New("machine dump is empty")
}
fsmPoolProvider := fsm_pool.Init(
signature_proposal_fsm.New(),
dkg_proposal_fsm.New(),
signing_proposal_fsm.New(),
)
i := &FSMInstance{
dump: &FSMDump{},
}
err = i.dump.Unmarshal(data)
// TODO: Add logger
if err != nil {
return nil, fmt.Errorf("cannot read machine dump: %w", err)
}
machine, err := fsmPoolProvider.MachineByState(i.dump.State)
if err != nil {
return nil, err
}
i.machine = machine.(internal.DumpedMachineProvider).
WithSetup(i.dump.State, i.dump.Payload)
return i, err
}
func (i *FSMInstance) GetPubKeyByUsername(username string) (ed25519.PublicKey, error) {
if i.dump == nil {
return nil, errors.New("dump not initialized")
}
return i.dump.Payload.GetPubKeyByUsername(username)
}
func (i *FSMInstance) GetIDByUsername(username string) (int, error) {
if i.dump == nil {
return -1, errors.New("dump not initialized")
}
return i.dump.Payload.GetIDByUsername(username)
}
func (i *FSMInstance) Do(event fsm.Event, args ...interface{}) (result *fsm.Response, dump []byte, err error) {
var dumpErr error
if i.machine == nil {
return nil, []byte{}, errors.New("machine is not initialized")
}
result, err = i.machine.Do(event, args...)
// On route errors result will be nil
if result != nil {
i.dump.State = result.State
dump, dumpErr = i.dump.Marshal()
if dumpErr != nil {
return result, []byte{}, err
}
}
return result, dump, err
}
func (i *FSMInstance) InitDump(dkgID string) error {
if i.dump != nil {
return errors.New("dump already initialized")
}
dkgID = strings.TrimSpace(dkgID)
if dkgID == "" {
return errors.New("empty {dkgID}")
}
i.dump = &FSMDump{
TransactionId: dkgID,
State: fsm.StateGlobalIdle,
Payload: &internal.DumpedMachineStatePayload{
DkgId: dkgID,
SignatureProposalPayload: nil,
DKGProposalPayload: nil,
},
}
return nil
}
func (i *FSMInstance) State() (fsm.State, error) {
if i.machine == nil {
return "", errors.New("machine is not initialized")
}
return i.machine.State(), nil
}
func (i *FSMInstance) Id() string {
if i.dump != nil {
return i.dump.TransactionId
}
return ""
}
func (i *FSMInstance) Dump() ([]byte, error) {
if i.dump == nil {
return []byte{}, errors.New("dump is not initialized")
}
return i.dump.Marshal()
}
func (i *FSMInstance) FSMDump() *FSMDump {
return i.dump
}
// TODO: Add encryption
func (d *FSMDump) Marshal() ([]byte, error) {
return json.Marshal(d)
}
// TODO: Add decryption
func (d *FSMDump) Unmarshal(data []byte) error {
if d == nil {
return errors.New("dump is not initialized")
}
return json.Unmarshal(data, d)
}