forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_backed_storage_service.go
83 lines (69 loc) · 2.05 KB
/
memory_backed_storage_service.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"errors"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbstate/daprovider"
"github.com/offchainlabs/nitro/das/dastree"
)
type MemoryBackedStorageService struct { // intended for testing and debugging
contents map[[32]byte][]byte
rwmutex sync.RWMutex
closed bool
}
var ErrClosed = errors.New("cannot access a StorageService that has been Closed")
func NewMemoryBackedStorageService(ctx context.Context) StorageService {
return &MemoryBackedStorageService{
contents: make(map[[32]byte][]byte),
}
}
func (m *MemoryBackedStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) {
log.Trace("das.MemoryBackedStorageService.GetByHash", "key", key, "this", m)
m.rwmutex.RLock()
defer m.rwmutex.RUnlock()
if m.closed {
return nil, ErrClosed
}
res, found := m.contents[key]
if !found {
return nil, ErrNotFound
}
return res, nil
}
func (m *MemoryBackedStorageService) Put(ctx context.Context, data []byte, expirationTime uint64) error {
logPut("das.MemoryBackedStorageService.Store", data, expirationTime, m)
m.rwmutex.Lock()
defer m.rwmutex.Unlock()
if m.closed {
return ErrClosed
}
m.contents[dastree.Hash(data)] = append([]byte{}, data...)
return nil
}
func (m *MemoryBackedStorageService) Sync(ctx context.Context) error {
m.rwmutex.RLock()
defer m.rwmutex.RUnlock()
if m.closed {
return ErrClosed
}
return nil
}
func (m *MemoryBackedStorageService) Close(ctx context.Context) error {
m.rwmutex.Lock()
defer m.rwmutex.Unlock()
m.closed = true
return nil
}
func (m *MemoryBackedStorageService) ExpirationPolicy(ctx context.Context) (daprovider.ExpirationPolicy, error) {
return daprovider.KeepForever, nil
}
func (m *MemoryBackedStorageService) String() string {
return "MemoryBackedStorageService"
}
func (m *MemoryBackedStorageService) HealthCheck(ctx context.Context) error {
return nil
}