forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_file_storage_service.go
125 lines (107 loc) · 3.32 KB
/
local_file_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
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
// Copyright 2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"context"
"encoding/base32"
"errors"
"fmt"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbstate"
"github.com/offchainlabs/nitro/das/dastree"
"github.com/offchainlabs/nitro/util/pretty"
flag "github.com/spf13/pflag"
"golang.org/x/sys/unix"
)
type LocalFileStorageConfig struct {
Enable bool `koanf:"enable"`
DataDir string `koanf:"data-dir"`
}
var DefaultLocalFileStorageConfig = LocalFileStorageConfig{
DataDir: "",
}
func LocalFileStorageConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultLocalFileStorageConfig.Enable, "enable storage/retrieval of sequencer batch data from a directory of files, one per batch")
f.String(prefix+".data-dir", DefaultLocalFileStorageConfig.DataDir, "local data directory")
}
type LocalFileStorageService struct {
dataDir string
}
func NewLocalFileStorageService(dataDir string) (StorageService, error) {
if unix.Access(dataDir, unix.W_OK|unix.R_OK) != nil {
return nil, fmt.Errorf("couldn't start LocalFileStorageService, directory '%s' must be readable and writeable", dataDir)
}
return &LocalFileStorageService{dataDir: dataDir}, nil
}
func (s *LocalFileStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) {
log.Trace("das.LocalFileStorageService.GetByHash", "key", pretty.PrettyHash(key), "this", s)
pathname := s.dataDir + "/" + EncodeStorageServiceKey(key)
data, err := os.ReadFile(pathname)
if err != nil {
// Just for backward compatability.
pathname = s.dataDir + "/" + base32.StdEncoding.EncodeToString(key.Bytes())
data, err = os.ReadFile(pathname)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, ErrNotFound
}
return nil, err
}
return data, nil
}
return data, nil
}
func (s *LocalFileStorageService) Put(ctx context.Context, data []byte, timeout uint64) error {
logPut("das.LocalFileStorageService.Store", data, timeout, s)
fileName := EncodeStorageServiceKey(dastree.Hash(data))
finalPath := s.dataDir + "/" + fileName
// Use a temp file and rename to achieve atomic writes.
f, err := os.CreateTemp(s.dataDir, fileName)
if err != nil {
return err
}
err = f.Chmod(0600)
if err != nil {
return err
}
_, err = f.Write(data)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return os.Rename(f.Name(), finalPath)
}
func (s *LocalFileStorageService) Sync(ctx context.Context) error {
return nil
}
func (s *LocalFileStorageService) Close(ctx context.Context) error {
return nil
}
func (s *LocalFileStorageService) ExpirationPolicy(ctx context.Context) (arbstate.ExpirationPolicy, error) {
return arbstate.KeepForever, nil
}
func (s *LocalFileStorageService) String() string {
return "LocalFileStorageService(" + s.dataDir + ")"
}
func (s *LocalFileStorageService) HealthCheck(ctx context.Context) error {
testData := []byte("Test-Data")
err := s.Put(ctx, testData, uint64(time.Now().Add(time.Minute).Unix()))
if err != nil {
return err
}
res, err := s.GetByHash(ctx, dastree.Hash(testData))
if err != nil {
return err
}
if !bytes.Equal(res, testData) {
return errors.New("invalid GetByHash result")
}
return nil
}