Skip to content

Commit

Permalink
ipfs: refactor removing very old clutter and abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Oct 9, 2023
1 parent 09dd8a4 commit 3da1c4c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
2 changes: 1 addition & 1 deletion cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func main() {

// Storage service for Gateway
if globalCfg.Mode == types.ModeGateway {
srv.Storage, err = srv.IPFS(globalCfg.Ipfs)
srv.Storage, err = service.IPFS(globalCfg.Ipfs)
if err != nil {
log.Fatal(err)
}
Expand Down
40 changes: 0 additions & 40 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package data

import (
"context"
"fmt"

"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/types"
)
Expand All @@ -23,41 +21,3 @@ type Storage interface {
CollectMetrics(ctx context.Context, ma *metrics.Agent) error
Stop() error
}

// StorageID is the type for the different storage providers.
// Currently only IPFS is supported.
type StorageID int

const (
// IPFS is the InterPlanetary File System.
IPFS StorageID = iota + 1
)

// StorageIDFromString returns the Storage identifier from a string.
func StorageIDFromString(i string) StorageID {
switch i {
case "IPFS":
return IPFS
default:
return -1
}
}

// IPFSNewConfig returns a new DataStore configuration for IPFS.
func IPFSNewConfig(path string) *types.DataStore {
datastore := new(types.DataStore)
datastore.Datadir = path
return datastore
}

// Init returns a new Storage instance of type `t`.
func Init(t StorageID, d *types.DataStore) (Storage, error) {
switch t {
case IPFS:
s := new(ipfs.Handler)
err := s.Init(d)
return s, err
default:
return nil, fmt.Errorf("bad storage type or DataStore specification")
}
}
8 changes: 8 additions & 0 deletions data/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/multiformats/go-multihash"

"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
)

var ConfigRoot string
Expand All @@ -47,6 +48,13 @@ func init() {
}
}

// New returns a new DataStore configuration for IPFS.
func New() *types.DataStore {
datastore := new(types.DataStore)
datastore.Datadir = path
return datastore
}

// Init initializes the IPFS node and repository.
func initRepository(enableLocalDiscovery bool) error {
daemonLocked, err := fsrepo.LockedByOtherProcess(ConfigRoot)
Expand Down
13 changes: 7 additions & 6 deletions service/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import (
)

// IPFS starts the IPFS service
func (vs *VocdoniService) IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage, err error) {
func IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage, err error) {
log.Info("creating ipfs service")
os.Setenv("IPFS_FD_MAX", "1024")
ipfsStore := data.IPFSNewConfig(ipfsconfig.ConfigPath)

ipfsStore := ipfs.New()
ipfsStore.Datadir = ipfsconfig.ConfigPath
ipfsStore.EnableLocalDiscovery = ipfsconfig.LocalDiscovery
storage, err = data.Init(data.StorageIDFromString("IPFS"), ipfsStore)
if err != nil {
return
return nil, err
}

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

go storage.CollectMetrics(context.Background(), vs.MetricsAgent)

if len(ipfsconfig.ConnectKey) > 0 {
log.Infow("starting ipfsconnect service", "key", ipfsconfig.ConnectKey)
ipfsconn := ipfsconnect.New(
Expand All @@ -45,5 +46,5 @@ func (vs *VocdoniService) IPFS(ipfsconfig *config.IPFSCfg) (storage data.Storage
}
ipfsconn.Start()
}
return
return storage, nil
}
3 changes: 2 additions & 1 deletion vocone/vocone.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewVocone(dataDir string, keymanager *ethereum.SignKeys, disableIPFS bool,

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

// Create the data downloader and offchain data handler
vc.OffChainDataHandler()
Expand Down

0 comments on commit 3da1c4c

Please sign in to comment.