-
Notifications
You must be signed in to change notification settings - Fork 95
/
api_epoch_based_provider.go
254 lines (191 loc) · 7.2 KB
/
api_epoch_based_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package iotago
import (
"sync"
"time"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/serializer/v2/stream"
)
type EpochBasedProvider struct {
mutex sync.RWMutex
protocolParametersByVersion map[Version]ProtocolParameters
futureProtocolParametersByVersion map[Version]Identifier
protocolVersions *ProtocolEpochVersions
latestAPI API
committedAPI API
committedSlotMutex sync.RWMutex
committedSlot SlotIndex
optsAPIForMissingVersionCallback func(protocolParameters ProtocolParameters) (API, error)
}
func WithAPIForMissingVersionCallback(callback func(protocolParameters ProtocolParameters) (API, error)) options.Option[EpochBasedProvider] {
return func(provider *EpochBasedProvider) {
provider.optsAPIForMissingVersionCallback = callback
}
}
func NewEpochBasedProvider(opts ...options.Option[EpochBasedProvider]) *EpochBasedProvider {
return options.Apply(&EpochBasedProvider{
protocolParametersByVersion: make(map[Version]ProtocolParameters),
futureProtocolParametersByVersion: make(map[Version]Identifier),
protocolVersions: NewProtocolEpochVersions(),
}, opts)
}
func (e *EpochBasedProvider) SetCommittedSlot(slot SlotIndex) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.committedSlotMutex.Lock()
e.committedSlot = slot
e.committedSlotMutex.Unlock()
e.updateCommittedAPI(slot)
}
func (e *EpochBasedProvider) AddProtocolParametersAtEpoch(protocolParameters ProtocolParameters, epoch EpochIndex) {
e.AddProtocolParameters(protocolParameters)
e.AddVersion(protocolParameters.Version(), epoch)
}
func (e *EpochBasedProvider) AddProtocolParameters(protocolParameters ProtocolParameters) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.protocolParametersByVersion[protocolParameters.Version()] = protocolParameters
delete(e.futureProtocolParametersByVersion, protocolParameters.Version())
if e.latestAPI == nil || e.latestAPI.Version() < protocolParameters.Version() {
e.latestAPI = lo.PanicOnErr(e.apiForVersion(protocolParameters.Version()))
}
}
func (e *EpochBasedProvider) AddVersion(version Version, epoch EpochIndex) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.protocolVersions.Add(version, epoch)
e.committedSlotMutex.Lock()
defer e.committedSlotMutex.Unlock()
e.updateCommittedAPI(e.committedSlot)
}
func (e *EpochBasedProvider) AddFutureVersion(version Version, protocolParamsHash Identifier, epoch EpochIndex) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.protocolVersions.Add(version, epoch)
e.futureProtocolParametersByVersion[version] = protocolParamsHash
}
func (e *EpochBasedProvider) apiForVersion(version Version) (API, error) {
if e.latestAPI != nil && e.latestAPI.Version() == version {
return e.latestAPI, nil
}
if e.committedAPI != nil && e.committedAPI.Version() == version {
return e.committedAPI, nil
}
protocolParams, exists := e.protocolParametersByVersion[version]
if !exists {
return nil, ierrors.Errorf("protocol parameters for version %d are not set", version)
}
//nolint:gocritic // further version will be added here
switch protocolParams.Version() {
case 3:
return V3API(protocolParams), nil
}
if e.optsAPIForMissingVersionCallback != nil {
return e.optsAPIForMissingVersionCallback(protocolParams)
}
return nil, ierrors.Errorf("no api available for parameters with version %d", protocolParams.Version())
}
func (e *EpochBasedProvider) APIForVersion(version Version) (API, error) {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.apiForVersion(version)
}
func (e *EpochBasedProvider) APIForTime(t time.Time) API {
e.mutex.RLock()
defer e.mutex.RUnlock()
slot := e.latestAPI.TimeProvider().SlotFromTime(t)
epoch := e.latestAPI.TimeProvider().EpochFromSlot(slot)
return lo.PanicOnErr(e.apiForVersion(e.protocolVersions.VersionForEpoch(epoch)))
}
func (e *EpochBasedProvider) APIForSlot(slot SlotIndex) API {
e.mutex.RLock()
defer e.mutex.RUnlock()
epoch := e.latestAPI.TimeProvider().EpochFromSlot(slot)
return lo.PanicOnErr(e.apiForVersion(e.protocolVersions.VersionForEpoch(epoch)))
}
func (e *EpochBasedProvider) APIForEpoch(epoch EpochIndex) API {
e.mutex.RLock()
defer e.mutex.RUnlock()
return lo.PanicOnErr(e.apiForVersion(e.protocolVersions.VersionForEpoch(epoch)))
}
func (e *EpochBasedProvider) LatestAPI() API {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.latestAPI
}
func (e *EpochBasedProvider) CommittedAPI() API {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.committedAPI
}
func (e *EpochBasedProvider) VersionsAndProtocolParametersHash() (Identifier, error) {
e.mutex.RLock()
defer e.mutex.RUnlock()
byteBuffer := stream.NewByteBuffer()
for _, version := range e.protocolVersions.Slice() {
if err := stream.Write(byteBuffer, version.Version); err != nil {
return Identifier{}, ierrors.Wrap(err, "failed to write Version")
}
if err := stream.Write(byteBuffer, version.StartEpoch); err != nil {
return Identifier{}, ierrors.Wrap(err, "failed to write StartEpoch")
}
var paramsHash Identifier
params, paramsExist := e.protocolParametersByVersion[version.Version]
if paramsExist {
var err error
if paramsHash, err = params.Hash(); err != nil {
return Identifier{}, ierrors.Wrap(err, "failed to get protocol parameters hash")
}
} else {
var hashExists bool
if paramsHash, hashExists = e.futureProtocolParametersByVersion[version.Version]; !hashExists {
return Identifier{}, ierrors.Errorf("protocol parameters for version %d are not set", version.Version)
}
}
if err := stream.Write(byteBuffer, paramsHash); err != nil {
return Identifier{}, ierrors.Wrap(err, "failed to write protocol parameters hash bytes")
}
}
return IdentifierFromData(lo.PanicOnErr(byteBuffer.Bytes())), nil
}
func (e *EpochBasedProvider) ProtocolParameters(version Version) ProtocolParameters {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.protocolParametersByVersion[version]
}
func (e *EpochBasedProvider) ProtocolParametersHash(version Version) Identifier {
e.mutex.RLock()
defer e.mutex.RUnlock()
if params, exists := e.protocolParametersByVersion[version]; exists {
return lo.PanicOnErr(params.Hash())
}
return e.futureProtocolParametersByVersion[version]
}
func (e *EpochBasedProvider) ProtocolEpochVersions() []ProtocolEpochVersion {
return e.protocolVersions.Slice()
}
func (e *EpochBasedProvider) EpochForVersion(version Version) (EpochIndex, bool) {
e.mutex.RLock()
defer e.mutex.RUnlock()
return e.protocolVersions.EpochForVersion(version)
}
func (e *EpochBasedProvider) VersionForSlot(slot SlotIndex) Version {
e.mutex.RLock()
defer e.mutex.RUnlock()
epoch := e.latestAPI.TimeProvider().EpochFromSlot(slot)
return e.protocolVersions.VersionForEpoch(epoch)
}
func (e *EpochBasedProvider) updateCommittedAPI(slot SlotIndex) {
if e.latestAPI == nil {
return
}
epoch := e.latestAPI.TimeProvider().EpochFromSlot(slot)
version := e.versionForEpoch(epoch)
if e.committedAPI == nil || version > e.committedAPI.ProtocolParameters().Version() {
e.committedAPI = lo.PanicOnErr(e.apiForVersion(version))
}
}
func (e *EpochBasedProvider) versionForEpoch(epoch EpochIndex) Version {
return e.protocolVersions.VersionForEpoch(epoch)
}