forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdas_test.go
188 lines (156 loc) · 5.29 KB
/
das_test.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"context"
"fmt"
"testing"
"time"
"github.com/offchainlabs/nitro/util/testhelpers"
)
func testDASStoreRetrieveMultipleInstances(t *testing.T, storageType string) {
firstCtx, firstCancel := context.WithCancel(context.Background())
dbPath := t.TempDir()
_, _, err := GenerateAndStoreKeys(dbPath)
Require(t, err)
enableFileStorage, enableDbStorage := false, false
switch storageType {
case "db":
enableDbStorage = true
case "files":
enableFileStorage = true
default:
Fail(t, "unknown storage type")
}
dbConfig := DefaultLocalDBStorageConfig
dbConfig.Enable = enableDbStorage
dbConfig.DataDir = dbPath
config := DataAvailabilityConfig{
Enable: true,
Key: KeyConfig{
KeyDir: dbPath,
},
LocalFileStorage: LocalFileStorageConfig{
Enable: enableFileStorage,
DataDir: dbPath,
MaxRetention: DefaultLocalFileStorageConfig.MaxRetention,
},
LocalDBStorage: dbConfig,
ParentChainNodeURL: "none",
}
storageService, lifecycleManager, err := CreatePersistentStorageService(firstCtx, &config)
Require(t, err)
defer lifecycleManager.StopAndWaitUntil(time.Second)
daWriter, err := NewSignAfterStoreDASWriter(firstCtx, config, storageService)
Require(t, err, "no das")
var daReader DataAvailabilityServiceReader = storageService
// #nosec G115
timeout := uint64(time.Now().Add(time.Hour * 24).Unix())
messageSaved := []byte("hello world")
cert, err := daWriter.Store(firstCtx, messageSaved, timeout)
Require(t, err, "Error storing message")
if cert.Timeout != timeout {
Fail(t, fmt.Sprintf("Expected timeout of %d in cert, was %d", timeout, cert.Timeout))
}
messageRetrieved, err := daReader.GetByHash(firstCtx, cert.DataHash)
Require(t, err, "Failed to retrieve message")
if !bytes.Equal(messageSaved, messageRetrieved) {
Fail(t, "Retrieved message is not the same as stored one.")
}
firstCancel()
time.Sleep(500 * time.Millisecond)
// 2nd das instance can read keys from disk
secondCtx, secondCancel := context.WithCancel(context.Background())
defer secondCancel()
storageService2, lifecycleManager, err := CreatePersistentStorageService(secondCtx, &config)
Require(t, err)
defer lifecycleManager.StopAndWaitUntil(time.Second)
var daReader2 DataAvailabilityServiceReader = storageService2
messageRetrieved2, err := daReader2.GetByHash(secondCtx, cert.DataHash)
Require(t, err, "Failed to retrieve message")
if !bytes.Equal(messageSaved, messageRetrieved2) {
Fail(t, "Retrieved message is not the same as stored one.")
}
messageRetrieved2, err = daReader2.GetByHash(secondCtx, cert.DataHash)
Require(t, err, "Failed to getByHash message")
if !bytes.Equal(messageSaved, messageRetrieved2) {
Fail(t, "Retrieved message is not the same as stored one.")
}
}
func TestDASStoreRetrieveMultipleInstancesFiles(t *testing.T) {
testDASStoreRetrieveMultipleInstances(t, "files")
}
func TestDASStoreRetrieveMultipleInstancesDB(t *testing.T) {
testDASStoreRetrieveMultipleInstances(t, "db")
}
func testDASMissingMessage(t *testing.T, storageType string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dbPath := t.TempDir()
_, _, err := GenerateAndStoreKeys(dbPath)
Require(t, err)
enableFileStorage, enableDbStorage := false, false
switch storageType {
case "db":
enableDbStorage = true
case "files":
enableFileStorage = true
default:
Fail(t, "unknown storage type")
}
dbConfig := DefaultLocalDBStorageConfig
dbConfig.Enable = enableDbStorage
dbConfig.DataDir = dbPath
config := DataAvailabilityConfig{
Enable: true,
Key: KeyConfig{
KeyDir: dbPath,
},
LocalFileStorage: LocalFileStorageConfig{
Enable: enableFileStorage,
DataDir: dbPath,
MaxRetention: DefaultLocalFileStorageConfig.MaxRetention,
},
LocalDBStorage: dbConfig,
ParentChainNodeURL: "none",
}
storageService, lifecycleManager, err := CreatePersistentStorageService(ctx, &config)
Require(t, err)
defer lifecycleManager.StopAndWaitUntil(time.Second)
daWriter, err := NewSignAfterStoreDASWriter(ctx, config, storageService)
Require(t, err, "no das")
var daReader DataAvailabilityServiceReader = storageService
messageSaved := []byte("hello world")
// #nosec G115
timeout := uint64(time.Now().Add(time.Hour * 24).Unix())
cert, err := daWriter.Store(ctx, messageSaved, timeout)
Require(t, err, "Error storing message")
if cert.Timeout != timeout {
Fail(t, fmt.Sprintf("Expected timeout of %d in cert, was %d", timeout, cert.Timeout))
}
// Change the hash to look up
cert.DataHash[0] += 1
_, err = daReader.GetByHash(ctx, cert.DataHash)
if err == nil {
Fail(t, "Expected an error when retrieving message that is not in the store.")
}
_, err = daReader.GetByHash(ctx, cert.DataHash)
if err == nil {
Fail(t, "Expected an error when getting by hash a message that is not in the store.")
}
}
func TestDASMissingMessageFiles(t *testing.T) {
testDASMissingMessage(t, "files")
}
func TestDASMissingMessageDB(t *testing.T) {
testDASMissingMessage(t, "db")
}
func Require(t *testing.T, err error, printables ...interface{}) {
t.Helper()
testhelpers.RequireImpl(t, err, printables...)
}
func Fail(t *testing.T, printables ...interface{}) {
t.Helper()
testhelpers.FailImpl(t, printables...)
}