forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dependencies.go
338 lines (285 loc) · 9.41 KB
/
dependencies.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package modules
import (
"errors"
"io"
"io/ioutil"
"net"
"os"
"sync"
"time"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/persist"
"github.com/NebulousLabs/fastrand"
)
// Dependencies defines dependencies used by all of Sia's modules. Custom
// dependencies can be created to inject certain behavior during testing.
type (
Dependencies interface {
// AtLeastOne will return a value that is at least one. In production,
// the value should always be one. This function is used to test the
// idempotency of actions, so during testing sometimes the value
// returned will be higher, causing an idempotent action to be
// committed multiple times. If the action is truly idempotent,
// committing it multiple times should not cause any problems or
// changes.
AtLeastOne() uint64
// CreateFile gives the host the ability to create files on the
// operating system.
CreateFile(string) (File, error)
// Destruct will clean up the dependencies, panicking if there are
// unclosed resources.
Destruct()
// DialTimeout tries to create a tcp connection to the specified
// address with a certain timeout.
DialTimeout(NetAddress, time.Duration) (net.Conn, error)
// Disrupt can be inserted in the code as a way to inject problems,
// such as a network call that take 10 minutes or a disk write that
// never completes. disrupt will return true if the disruption is
// forcibly triggered. In production, disrupt will always return false.
Disrupt(string) bool
// Listen gives the host the ability to receive incoming connections.
Listen(string, string) (net.Listener, error)
// LoadFile allows the host to load a persistence structure form disk.
LoadFile(persist.Metadata, interface{}, string) error
// MkdirAll gives the host the ability to create chains of folders
// within the filesystem.
MkdirAll(string, os.FileMode) error
// NewLogger creates a logger that the host can use to log messages and
// write critical statements.
NewLogger(string) (*persist.Logger, error)
// OpenDatabase creates a database that the host can use to interact
// with large volumes of persistent data.
OpenDatabase(persist.Metadata, string) (*persist.BoltDatabase, error)
// OpenFile opens a file for the host.
OpenFile(string, int, os.FileMode) (File, error)
// RandRead fills the input bytes with random data.
RandRead([]byte) (int, error)
// ReadFile reads a file in full from the filesystem.
ReadFile(string) ([]byte, error)
// RemoveFile removes a file from file filesystem.
RemoveFile(string) error
// RenameFile renames a file on disk to another name.
RenameFile(string, string) error
// SaveFileSync writes JSON encoded data to disk and syncs the file
// afterwards.
SaveFileSync(persist.Metadata, interface{}, string) error
// Sleep blocks the calling thread for at least the specified duration.
Sleep(time.Duration)
// Symlink creates a sym link between a source and a destination.
Symlink(s1, s2 string) error
// WriteFile writes data to the filesystem using the provided filename.
WriteFile(string, []byte, os.FileMode) error
}
// File implements all of the methods that can be called on an os.File.
File interface {
io.ReadWriteCloser
Name() string
ReadAt([]byte, int64) (int, error)
Sync() error
Truncate(int64) error
WriteAt([]byte, int64) (int, error)
}
)
type (
// ProductionDependencies are the dependencies used in a Release or Debug
// production build.
ProductionDependencies struct {
shouldInit bool
openFiles map[string]int
mu sync.Mutex
}
// ProductionFile is the implementation of the File interface that is used
// in a Release or Debug production build.
ProductionFile struct {
pd *ProductionDependencies
*os.File
}
)
// Close will close a file, checking whether the file handle is open somewhere
// else before closing completely. This check is performed on Windows but not
// Linux, therefore a mock is used to ensure that linux testing picks up
// potential problems that would be seen on Windows.
func (pf *ProductionFile) Close() error {
if !build.DEBUG {
return pf.File.Close()
}
pf.pd.mu.Lock()
if pf.pd.openFiles == nil {
pf.pd.openFiles = make(map[string]int)
}
v, exists := pf.pd.openFiles[pf.File.Name()]
if !exists {
panic("file not registered")
}
if v == 1 {
delete(pf.pd.openFiles, pf.File.Name())
} else if v > 1 {
pf.pd.openFiles[pf.File.Name()] = v - 1
} else {
panic("inconsistent state")
}
pf.pd.mu.Unlock()
return pf.File.Close()
}
// AtLeastOne will return a value that is equal to 1 if debugging is disabled.
// If debugging is enabled, a higher value may be returned.
func (*ProductionDependencies) AtLeastOne() uint64 {
if !build.DEBUG {
return 1
}
// Probabilistically return a number greater than one.
val := uint64(1)
for fastrand.Intn(2) != 0 {
val++
}
return val
}
// CreateFile gives the host the ability to create files on the operating
// system.
func (pd *ProductionDependencies) CreateFile(s string) (File, error) {
if !build.DEBUG {
return os.Create(s)
}
f, err := os.Create(s)
if err != nil {
return f, err
}
pd.mu.Lock()
if pd.openFiles == nil {
pd.openFiles = make(map[string]int)
}
v := pd.openFiles[s]
pd.openFiles[s] = v + 1
pd.mu.Unlock()
return &ProductionFile{
pd: pd,
File: f,
}, nil
}
// Destruct checks that all resources have been cleaned up correctly.
func (pd *ProductionDependencies) Destruct() {
if !build.DEBUG {
return
}
pd.mu.Lock()
l := len(pd.openFiles)
pd.mu.Unlock()
if l != 0 {
panic("unclosed resources - most likely file handles")
}
}
// DialTimeout creates a tcp connection to a certain address with the specified
// timeout.
func (*ProductionDependencies) DialTimeout(addr NetAddress, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", string(addr), timeout)
}
// Disrupt can be used to inject specific behavior into a module by overwriting
// it using a custom dependency.
func (*ProductionDependencies) Disrupt(string) bool {
return false
}
// Listen gives the host the ability to receive incoming connections.
func (*ProductionDependencies) Listen(s1, s2 string) (net.Listener, error) {
return net.Listen(s1, s2)
}
// LoadFile loads JSON encoded data from a file.
func (*ProductionDependencies) LoadFile(meta persist.Metadata, data interface{}, filename string) error {
return persist.LoadJSON(meta, data, filename)
}
// SaveFileSync writes JSON encoded data to a file and syncs the file to disk
// afterwards.
func (*ProductionDependencies) SaveFileSync(meta persist.Metadata, data interface{}, filename string) error {
return persist.SaveJSON(meta, data, filename)
}
// MkdirAll gives the host the ability to create chains of folders within the
// filesystem.
func (*ProductionDependencies) MkdirAll(s string, fm os.FileMode) error {
return os.MkdirAll(s, fm)
}
// NewLogger creates a logger that the host can use to log messages and write
// critical statements.
func (*ProductionDependencies) NewLogger(s string) (*persist.Logger, error) {
return persist.NewFileLogger(s)
}
// OpenDatabase creates a database that the host can use to interact with large
// volumes of persistent data.
func (*ProductionDependencies) OpenDatabase(m persist.Metadata, s string) (*persist.BoltDatabase, error) {
return persist.OpenDatabase(m, s)
}
// OpenFile opens a file for the contract manager.
func (pd *ProductionDependencies) OpenFile(s string, i int, fm os.FileMode) (File, error) {
if !build.DEBUG {
return os.OpenFile(s, i, fm)
}
f, err := os.OpenFile(s, i, fm)
if err != nil {
return f, err
}
pd.mu.Lock()
if pd.openFiles == nil {
pd.openFiles = make(map[string]int)
}
v := pd.openFiles[s]
pd.openFiles[s] = v + 1
pd.mu.Unlock()
return &ProductionFile{
pd: pd,
File: f,
}, nil
}
// RandRead fills the input bytes with random data.
func (*ProductionDependencies) RandRead(b []byte) (int, error) {
return fastrand.Reader.Read(b)
}
// ReadFile reads a file from the filesystem.
func (*ProductionDependencies) ReadFile(s string) ([]byte, error) {
return ioutil.ReadFile(s)
}
// RemoveFile will remove a file from disk.
func (pd *ProductionDependencies) RemoveFile(s string) error {
if !build.DEBUG {
return os.Remove(s)
}
pd.mu.Lock()
if pd.openFiles == nil {
pd.openFiles = make(map[string]int)
}
v, exists := pd.openFiles[s]
pd.mu.Unlock()
if exists && v > 0 {
return errors.New("cannot remove the file, it's open somewhere else right now")
}
return os.Remove(s)
}
// RenameFile renames a file on disk.
func (pd *ProductionDependencies) RenameFile(s1 string, s2 string) error {
if !build.DEBUG {
return os.Rename(s1, s2)
}
pd.mu.Lock()
if pd.openFiles == nil {
pd.openFiles = make(map[string]int)
}
v1, exists1 := pd.openFiles[s1]
v2, exists2 := pd.openFiles[s2]
pd.mu.Unlock()
if exists1 && v1 > 0 {
return errors.New("cannot remove the file, it's open somewhere else right now")
}
if exists2 && v2 > 0 {
return errors.New("cannot remove the file, it's open somewhere else right now")
}
return os.Rename(s1, s2)
}
// Sleep blocks the calling thread for a certain duration.
func (*ProductionDependencies) Sleep(d time.Duration) {
time.Sleep(d)
}
// Symlink creates a symlink between a source and a destination file.
func (*ProductionDependencies) Symlink(s1, s2 string) error {
return os.Symlink(s1, s2)
}
// WriteFile writes a file to the filesystem.
func (*ProductionDependencies) WriteFile(s string, b []byte, fm os.FileMode) error {
return ioutil.WriteFile(s, b, fm)
}