forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaclient_test.go
161 lines (117 loc) · 3.75 KB
/
daclient_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
package altda
import (
"context"
"fmt"
"math/rand"
"sync"
"testing"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
)
type MemStore struct {
db map[string][]byte
lock sync.RWMutex
}
func NewMemStore() *MemStore {
return &MemStore{
db: make(map[string][]byte),
}
}
// Get retrieves the given key if it's present in the key-value store.
func (s *MemStore) Get(ctx context.Context, key []byte) ([]byte, error) {
s.lock.RLock()
defer s.lock.RUnlock()
if entry, ok := s.db[string(key)]; ok {
return common.CopyBytes(entry), nil
}
return nil, ErrNotFound
}
// Put inserts the given value into the key-value store.
func (s *MemStore) Put(ctx context.Context, key []byte, value []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
s.db[string(key)] = common.CopyBytes(value)
return nil
}
func TestDAClientPrecomputed(t *testing.T) {
store := NewMemStore()
logger := testlog.Logger(t, log.LevelDebug)
ctx := context.Background()
server := NewDAServer("127.0.0.1", 0, store, logger, false)
require.NoError(t, server.Start())
cfg := CLIConfig{
Enabled: true,
DAServerURL: fmt.Sprintf("http://%s", server.Endpoint()),
VerifyOnRead: true,
}
require.NoError(t, cfg.Check())
client := cfg.NewDAClient()
rng := rand.New(rand.NewSource(1234))
input := RandomData(rng, 2000)
comm, err := client.SetInput(ctx, input)
require.NoError(t, err)
require.Equal(t, comm, NewKeccak256Commitment(input))
stored, err := client.GetInput(ctx, comm)
require.NoError(t, err)
require.Equal(t, input, stored)
// set a bad commitment in the store
require.NoError(t, store.Put(ctx, comm.Encode(), []byte("bad data")))
_, err = client.GetInput(ctx, comm)
require.ErrorIs(t, err, ErrCommitmentMismatch)
// test not found error
comm = NewKeccak256Commitment(RandomData(rng, 32))
_, err = client.GetInput(ctx, comm)
require.ErrorIs(t, err, ErrNotFound)
// test storing bad data
_, err = client.SetInput(ctx, []byte{})
require.ErrorIs(t, err, ErrInvalidInput)
// server not responsive
require.NoError(t, server.Stop())
_, err = client.SetInput(ctx, input)
require.Error(t, err)
_, err = client.GetInput(ctx, NewKeccak256Commitment(input))
require.Error(t, err)
}
func TestDAClientService(t *testing.T) {
store := NewMemStore()
logger := testlog.Logger(t, log.LevelDebug)
ctx := context.Background()
server := NewDAServer("127.0.0.1", 0, store, logger, false)
require.NoError(t, server.Start())
cfg := CLIConfig{
Enabled: true,
DAServerURL: fmt.Sprintf("http://%s", server.Endpoint()),
VerifyOnRead: false,
GenericDA: false,
}
require.NoError(t, cfg.Check())
client := cfg.NewDAClient()
rng := rand.New(rand.NewSource(1234))
input := RandomData(rng, 2000)
comm, err := client.SetInput(ctx, input)
require.NoError(t, err)
require.Equal(t, comm.String(), NewKeccak256Commitment(input).String())
stored, err := client.GetInput(ctx, comm)
require.NoError(t, err)
require.Equal(t, input, stored)
// set a bad commitment in the store
require.NoError(t, store.Put(ctx, comm.Encode(), []byte("bad data")))
// assert no error as generic commitments cannot be verified client side
_, err = client.GetInput(ctx, comm)
require.NoError(t, err)
// test not found error
comm = NewKeccak256Commitment(RandomData(rng, 32))
_, err = client.GetInput(ctx, comm)
require.ErrorIs(t, err, ErrNotFound)
// test storing bad data
_, err = client.SetInput(ctx, []byte{})
require.ErrorIs(t, err, ErrInvalidInput)
// server not responsive
require.NoError(t, server.Stop())
_, err = client.SetInput(ctx, input)
require.Error(t, err)
_, err = client.GetInput(ctx, NewKeccak256Commitment(input))
require.Error(t, err)
}