Skip to content

Commit 3da1c4c

Browse files
committed
ipfs: refactor removing very old clutter and abstractions
1 parent 09dd8a4 commit 3da1c4c

5 files changed

Lines changed: 18 additions & 48 deletions

File tree

cmd/node/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func main() {
423423

424424
// Storage service for Gateway
425425
if globalCfg.Mode == types.ModeGateway {
426-
srv.Storage, err = srv.IPFS(globalCfg.Ipfs)
426+
srv.Storage, err = service.IPFS(globalCfg.Ipfs)
427427
if err != nil {
428428
log.Fatal(err)
429429
}

data/data.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package data
33

44
import (
55
"context"
6-
"fmt"
76

8-
"go.vocdoni.io/dvote/data/ipfs"
97
"go.vocdoni.io/dvote/metrics"
108
"go.vocdoni.io/dvote/types"
119
)
@@ -23,41 +21,3 @@ type Storage interface {
2321
CollectMetrics(ctx context.Context, ma *metrics.Agent) error
2422
Stop() error
2523
}
26-
27-
// StorageID is the type for the different storage providers.
28-
// Currently only IPFS is supported.
29-
type StorageID int
30-
31-
const (
32-
// IPFS is the InterPlanetary File System.
33-
IPFS StorageID = iota + 1
34-
)
35-
36-
// StorageIDFromString returns the Storage identifier from a string.
37-
func StorageIDFromString(i string) StorageID {
38-
switch i {
39-
case "IPFS":
40-
return IPFS
41-
default:
42-
return -1
43-
}
44-
}
45-
46-
// IPFSNewConfig returns a new DataStore configuration for IPFS.
47-
func IPFSNewConfig(path string) *types.DataStore {
48-
datastore := new(types.DataStore)
49-
datastore.Datadir = path
50-
return datastore
51-
}
52-
53-
// Init returns a new Storage instance of type `t`.
54-
func Init(t StorageID, d *types.DataStore) (Storage, error) {
55-
switch t {
56-
case IPFS:
57-
s := new(ipfs.Handler)
58-
err := s.Init(d)
59-
return s, err
60-
default:
61-
return nil, fmt.Errorf("bad storage type or DataStore specification")
62-
}
63-
}

data/ipfs/init.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/multiformats/go-multihash"
2626

2727
"go.vocdoni.io/dvote/log"
28+
"go.vocdoni.io/dvote/types"
2829
)
2930

3031
var ConfigRoot string
@@ -47,6 +48,13 @@ func init() {
4748
}
4849
}
4950

51+
// New returns a new DataStore configuration for IPFS.
52+
func New() *types.DataStore {
53+
datastore := new(types.DataStore)
54+
datastore.Datadir = path
55+
return datastore
56+
}
57+
5058
// Init initializes the IPFS node and repository.
5159
func initRepository(enableLocalDiscovery bool) error {
5260
daemonLocked, err := fsrepo.LockedByOtherProcess(ConfigRoot)

service/ipfs.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ import (
1313
)
1414

1515
// IPFS starts the IPFS service
16-
func (vs *VocdoniService) IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage, err error) {
16+
func IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage, err error) {
1717
log.Info("creating ipfs service")
1818
os.Setenv("IPFS_FD_MAX", "1024")
19-
ipfsStore := data.IPFSNewConfig(ipfsconfig.ConfigPath)
19+
20+
ipfsStore := ipfs.New()
21+
ipfsStore.Datadir = ipfsconfig.ConfigPath
22+
ipfsStore.EnableLocalDiscovery = ipfsconfig.LocalDiscovery
2023
storage, err = data.Init(data.StorageIDFromString("IPFS"), ipfsStore)
2124
if err != nil {
22-
return
25+
return nil, err
2326
}
2427

2528
go func() {
@@ -31,8 +34,6 @@ func (vs *VocdoniService) IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage
3134
}
3235
}()
3336

34-
go storage.CollectMetrics(context.Background(), vs.MetricsAgent)
35-
3637
if len(ipfsconfig.ConnectKey) > 0 {
3738
log.Infow("starting ipfsconnect service", "key", ipfsconfig.ConnectKey)
3839
ipfsconn := ipfsconnect.New(
@@ -45,5 +46,5 @@ func (vs *VocdoniService) IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage
4546
}
4647
ipfsconn.Start()
4748
}
48-
return
49+
return storage, nil
4950
}

vocone/vocone.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func NewVocone(dataDir string, keymanager *ethereum.SignKeys, disableIPFS bool,
105105

106106
// Create the IPFS storage layer
107107
if !disableIPFS {
108-
vc.Storage, err = vc.IPFS(&config.IPFSCfg{
108+
vc.Storage, err = service.IPFS(&config.IPFSCfg{
109109
ConfigPath: filepath.Join(dataDir, "ipfs"),
110110
ConnectKey: connectKey,
111111
ConnectPeers: connectPeers,
@@ -114,6 +114,7 @@ func NewVocone(dataDir string, keymanager *ethereum.SignKeys, disableIPFS bool,
114114
if err != nil {
115115
return nil, err
116116
}
117+
go vc.Storage.CollectMetrics(context.Background(), vc.MetricsAgent)
117118

118119
// Create the data downloader and offchain data handler
119120
vc.OffChainDataHandler()

0 commit comments

Comments
 (0)