Skip to content

Commit 996755c

Browse files
janosnonsense
authored andcommitted
cmd/swarm, swarm: LocalStore storage integration
1 parent c94d582 commit 996755c

62 files changed

Lines changed: 3092 additions & 3674 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/swarm/config.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Con
252252
}
253253

254254
if storePath := ctx.GlobalString(SwarmStorePath.Name); storePath != "" {
255-
currentConfig.LocalStoreParams.ChunkDbPath = storePath
255+
currentConfig.ChunkDbPath = storePath
256256
}
257257

258258
if storeCapacity := ctx.GlobalUint64(SwarmStoreCapacity.Name); storeCapacity != 0 {
259-
currentConfig.LocalStoreParams.DbCapacity = storeCapacity
259+
currentConfig.DbCapacity = storeCapacity
260260
}
261261

262262
if ctx.GlobalIsSet(SwarmStoreCacheCapacity.Name) {
263-
currentConfig.LocalStoreParams.CacheCapacity = ctx.GlobalUint(SwarmStoreCacheCapacity.Name)
263+
currentConfig.CacheCapacity = ctx.GlobalUint(SwarmStoreCacheCapacity.Name)
264264
}
265265

266266
if ctx.GlobalIsSet(SwarmBootnodeModeFlag.Name) {

‎cmd/swarm/config_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ func TestConfigCmdLineOverridesFile(t *testing.T) {
447447
t.Fatal("Expected Sync to be disabled, but is true")
448448
}
449449

450-
if info.LocalStoreParams.DbCapacity != 9000000 {
451-
t.Fatalf("Expected Capacity to be %d, got %d", 9000000, info.LocalStoreParams.DbCapacity)
450+
if info.DbCapacity != 9000000 {
451+
t.Fatalf("Expected Capacity to be %d, got %d", 9000000, info.DbCapacity)
452452
}
453453

454454
if info.HiveParams.KeepAliveInterval != 6000000000 {

‎cmd/swarm/db.go‎

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
package main
1818

1919
import (
20+
"archive/tar"
21+
"bytes"
22+
"encoding/binary"
23+
"encoding/hex"
2024
"fmt"
2125
"io"
2226
"os"
@@ -25,10 +29,22 @@ import (
2529
"github.com/ethereum/go-ethereum/cmd/utils"
2630
"github.com/ethereum/go-ethereum/common"
2731
"github.com/ethereum/go-ethereum/log"
28-
"github.com/ethereum/go-ethereum/swarm/storage"
32+
"github.com/ethereum/go-ethereum/rlp"
33+
"github.com/ethereum/go-ethereum/swarm/chunk"
34+
"github.com/ethereum/go-ethereum/swarm/storage/localstore"
35+
"github.com/syndtr/goleveldb/leveldb"
36+
"github.com/syndtr/goleveldb/leveldb/opt"
2937
"gopkg.in/urfave/cli.v1"
3038
)
3139

40+
var legacyKeyIndex = byte(0)
41+
var keyData = byte(6)
42+
43+
type dpaDBIndex struct {
44+
Idx uint64
45+
Access uint64
46+
}
47+
3248
var dbCommand = cli.Command{
3349
Name: "db",
3450
CustomHelpTemplate: helpTemplate,
@@ -67,6 +83,9 @@ The import may be quite large, consider piping the input through the Unix
6783
pv(1) tool to get a progress bar:
6884
6985
pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -`,
86+
Flags: []cli.Flag{
87+
SwarmLegacyFlag,
88+
},
7089
},
7190
},
7291
}
@@ -77,12 +96,6 @@ func dbExport(ctx *cli.Context) {
7796
utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to write the tar archive to, - for stdout) and the base key")
7897
}
7998

80-
store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
81-
if err != nil {
82-
utils.Fatalf("error opening local chunk database: %s", err)
83-
}
84-
defer store.Close()
85-
8699
var out io.Writer
87100
if args[1] == "-" {
88101
out = os.Stdout
@@ -95,6 +108,23 @@ func dbExport(ctx *cli.Context) {
95108
out = f
96109
}
97110

111+
isLegacy := localstore.IsLegacyDatabase(args[0])
112+
if isLegacy {
113+
count, err := exportLegacy(args[0], common.Hex2Bytes(args[2]), out)
114+
if err != nil {
115+
utils.Fatalf("error exporting legacy local chunk database: %s", err)
116+
}
117+
118+
log.Info(fmt.Sprintf("successfully exported %d chunks from legacy db", count))
119+
return
120+
}
121+
122+
store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
123+
if err != nil {
124+
utils.Fatalf("error opening local chunk database: %s", err)
125+
}
126+
defer store.Close()
127+
98128
count, err := store.Export(out)
99129
if err != nil {
100130
utils.Fatalf("error exporting local chunk database: %s", err)
@@ -109,6 +139,8 @@ func dbImport(ctx *cli.Context) {
109139
utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to read the tar archive from, - for stdin) and the base key")
110140
}
111141

142+
legacy := ctx.IsSet(SwarmLegacyFlag.Name)
143+
112144
store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
113145
if err != nil {
114146
utils.Fatalf("error opening local chunk database: %s", err)
@@ -127,21 +159,81 @@ func dbImport(ctx *cli.Context) {
127159
in = f
128160
}
129161

130-
count, err := store.Import(in)
162+
count, err := store.Import(in, legacy)
131163
if err != nil {
132164
utils.Fatalf("error importing local chunk database: %s", err)
133165
}
134166

135167
log.Info(fmt.Sprintf("successfully imported %d chunks", count))
136168
}
137169

138-
func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) {
170+
func openLDBStore(path string, basekey []byte) (*localstore.DB, error) {
139171
if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
140172
return nil, fmt.Errorf("invalid chunkdb path: %s", err)
141173
}
142174

143-
storeparams := storage.NewDefaultStoreParams()
144-
ldbparams := storage.NewLDBStoreParams(storeparams, path)
145-
ldbparams.BaseKey = basekey
146-
return storage.NewLDBStore(ldbparams)
175+
return localstore.New(path, basekey, nil)
176+
}
177+
178+
func decodeIndex(data []byte, index *dpaDBIndex) error {
179+
dec := rlp.NewStream(bytes.NewReader(data), 0)
180+
return dec.Decode(index)
181+
}
182+
183+
func getDataKey(idx uint64, po uint8) []byte {
184+
key := make([]byte, 10)
185+
key[0] = keyData
186+
key[1] = po
187+
binary.BigEndian.PutUint64(key[2:], idx)
188+
189+
return key
190+
}
191+
192+
func exportLegacy(path string, basekey []byte, out io.Writer) (int64, error) {
193+
tw := tar.NewWriter(out)
194+
defer tw.Close()
195+
db, err := leveldb.OpenFile(path, &opt.Options{OpenFilesCacheCapacity: 128})
196+
if err != nil {
197+
return 0, err
198+
}
199+
defer db.Close()
200+
201+
it := db.NewIterator(nil, nil)
202+
defer it.Release()
203+
var count int64
204+
for ok := it.Seek([]byte{legacyKeyIndex}); ok; ok = it.Next() {
205+
key := it.Key()
206+
if (key == nil) || (key[0] != legacyKeyIndex) {
207+
break
208+
}
209+
210+
var index dpaDBIndex
211+
212+
hash := key[1:]
213+
decodeIndex(it.Value(), &index)
214+
215+
po := uint8(chunk.Proximity(basekey, hash))
216+
217+
datakey := getDataKey(index.Idx, po)
218+
data, err := db.Get(datakey, nil)
219+
if err != nil {
220+
log.Crit(fmt.Sprintf("Chunk %x found but could not be accessed: %v, %x", key, err, datakey))
221+
continue
222+
}
223+
224+
hdr := &tar.Header{
225+
Name: hex.EncodeToString(hash),
226+
Mode: 0644,
227+
Size: int64(len(data)),
228+
}
229+
if err := tw.WriteHeader(hdr); err != nil {
230+
return count, err
231+
}
232+
if _, err := tw.Write(data); err != nil {
233+
return count, err
234+
}
235+
count++
236+
}
237+
238+
return count, nil
147239
}

0 commit comments

Comments
 (0)