forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdamock.go
186 lines (152 loc) · 4.57 KB
/
damock.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
package altda
import (
"context"
"errors"
"io"
"net/http"
"sync"
"time"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/log"
)
// MockDAClient mocks a DA storage provider to avoid running an HTTP DA server
// in unit tests.
type MockDAClient struct {
CommitmentType CommitmentType
store ethdb.KeyValueStore
log log.Logger
}
func NewMockDAClient(log log.Logger) *MockDAClient {
return &MockDAClient{
CommitmentType: Keccak256CommitmentType,
store: memorydb.New(),
log: log,
}
}
func (c *MockDAClient) GetInput(ctx context.Context, key CommitmentData) ([]byte, error) {
bytes, err := c.store.Get(key.Encode())
if err != nil {
return nil, ErrNotFound
}
return bytes, nil
}
func (c *MockDAClient) SetInput(ctx context.Context, data []byte) (CommitmentData, error) {
key := NewCommitmentData(c.CommitmentType, data)
return key, c.store.Put(key.Encode(), data)
}
func (c *MockDAClient) DeleteData(key []byte) error {
return c.store.Delete(key)
}
type DAErrFaker struct {
Client *MockDAClient
getInputErr error
setInputErr error
}
func (f *DAErrFaker) GetInput(ctx context.Context, key CommitmentData) ([]byte, error) {
if err := f.getInputErr; err != nil {
f.getInputErr = nil
return nil, err
}
return f.Client.GetInput(ctx, key)
}
func (f *DAErrFaker) SetInput(ctx context.Context, data []byte) (CommitmentData, error) {
if err := f.setInputErr; err != nil {
f.setInputErr = nil
return nil, err
}
return f.Client.SetInput(ctx, data)
}
func (f *DAErrFaker) ActGetPreImageFail() {
f.getInputErr = errors.New("get input failed")
}
func (f *DAErrFaker) ActSetPreImageFail() {
f.setInputErr = errors.New("set input failed")
}
var Disabled = &AltDADisabled{}
var ErrNotEnabled = errors.New("altDA not enabled")
// AltDADisabled is a noop AltDA implementation for stubbing.
type AltDADisabled struct{}
func (d *AltDADisabled) GetInput(ctx context.Context, l1 L1Fetcher, commitment CommitmentData, blockId eth.L1BlockRef) (eth.Data, error) {
return nil, ErrNotEnabled
}
func (d *AltDADisabled) Reset(ctx context.Context, base eth.L1BlockRef, baseCfg eth.SystemConfig) error {
return io.EOF
}
func (d *AltDADisabled) Finalize(ref eth.L1BlockRef) {
}
func (d *AltDADisabled) OnFinalizedHeadSignal(f HeadSignalFn) {
}
func (d *AltDADisabled) AdvanceL1Origin(ctx context.Context, l1 L1Fetcher, blockId eth.BlockID) error {
return ErrNotEnabled
}
// FakeDAServer is a fake DA server for e2e tests.
// It is a small wrapper around DAServer that allows for setting request latencies,
// to mimic a DA service with slow responses (eg. eigenDA with 10 min batching interval).
type FakeDAServer struct {
*DAServer
putRequestLatency time.Duration
getRequestLatency time.Duration
}
func NewFakeDAServer(host string, port int, log log.Logger) *FakeDAServer {
store := NewMemStore()
fakeDAServer := &FakeDAServer{
DAServer: NewDAServer(host, port, store, log, true),
putRequestLatency: 0,
getRequestLatency: 0,
}
return fakeDAServer
}
func (s *FakeDAServer) HandleGet(w http.ResponseWriter, r *http.Request) {
time.Sleep(s.getRequestLatency)
s.DAServer.HandleGet(w, r)
}
func (s *FakeDAServer) HandlePut(w http.ResponseWriter, r *http.Request) {
time.Sleep(s.putRequestLatency)
s.DAServer.HandlePut(w, r)
}
func (s *FakeDAServer) Start() error {
err := s.DAServer.Start()
if err != nil {
return err
}
// Override the HandleGet/Put method registrations
mux := http.NewServeMux()
mux.HandleFunc("/get/", s.HandleGet)
mux.HandleFunc("/put", s.HandlePut)
s.httpServer.Handler = mux
return nil
}
func (s *FakeDAServer) SetPutRequestLatency(latency time.Duration) {
s.putRequestLatency = latency
}
func (s *FakeDAServer) SetGetRequestLatency(latency time.Duration) {
s.getRequestLatency = latency
}
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
}