From 9db0fb219acfee5d42df589163966563b901f7b3 Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Thu, 23 Apr 2020 15:56:37 -0500 Subject: [PATCH] ipfs mode enum; configuration --- cmd/superNode.go | 3 +- pkg/super_node/config.go | 9 ++++- pkg/super_node/resync/config.go | 9 ++++- pkg/super_node/shared/env.go | 11 ++++++ pkg/super_node/shared/functions.go | 10 ++++++ pkg/super_node/shared/ipfs_mode.go | 58 ++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 pkg/super_node/shared/ipfs_mode.go diff --git a/cmd/superNode.go b/cmd/superNode.go index e2cfdd08..43f31473 100644 --- a/cmd/superNode.go +++ b/cmd/superNode.go @@ -18,11 +18,10 @@ package cmd import ( "sync" - "github.com/spf13/viper" - "github.com/ethereum/go-ethereum/rpc" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/vulcanize/vulcanizedb/pkg/ipfs" "github.com/vulcanize/vulcanizedb/pkg/super_node" diff --git a/pkg/super_node/config.go b/pkg/super_node/config.go index be3d09c3..2bb83cc7 100644 --- a/pkg/super_node/config.go +++ b/pkg/super_node/config.go @@ -52,6 +52,7 @@ type Config struct { // Ubiquitous fields Chain shared.ChainType IPFSPath string + IPFSMode shared.IPFSMode DB *postgres.DB DBConfig config.Database Quit chan bool @@ -98,10 +99,16 @@ func NewSuperNodeConfig() (*Config, error) { return nil, err } - c.IPFSPath, err = shared.GetIPFSPath() + c.IPFSMode, err = shared.GetIPFSMode() if err != nil { return nil, err } + if c.IPFSMode == shared.LocalInterface || c.IPFSMode == shared.RemoteClient { + c.IPFSPath, err = shared.GetIPFSPath() + if err != nil { + return nil, err + } + } c.Sync = viper.GetBool("superNode.sync") if c.Sync { diff --git a/pkg/super_node/resync/config.go b/pkg/super_node/resync/config.go index bd35c00c..0979a196 100644 --- a/pkg/super_node/resync/config.go +++ b/pkg/super_node/resync/config.go @@ -52,6 +52,7 @@ type Config struct { DB *postgres.DB DBConfig config.Database IPFSPath string + IPFSMode shared.IPFSMode HTTPClient interface{} // Note this client is expected to support the retrieval of the specified data type(s) NodeInfo core.Node // Info for the associated node @@ -92,10 +93,16 @@ func NewReSyncConfig() (*Config, error) { c.ClearOldCache = viper.GetBool("resync.clearOldCache") c.ResetValidation = viper.GetBool("resync.resetValidation") - c.IPFSPath, err = shared.GetIPFSPath() + c.IPFSMode, err = shared.GetIPFSMode() if err != nil { return nil, err } + if c.IPFSMode == shared.LocalInterface || c.IPFSMode == shared.RemoteClient { + c.IPFSPath, err = shared.GetIPFSPath() + if err != nil { + return nil, err + } + } resyncType := viper.GetString("resync.type") c.ResyncType, err = shared.GenerateResyncTypeFromString(resyncType) if err != nil { diff --git a/pkg/super_node/shared/env.go b/pkg/super_node/shared/env.go index 4a895f29..0dbfe490 100644 --- a/pkg/super_node/shared/env.go +++ b/pkg/super_node/shared/env.go @@ -30,6 +30,7 @@ import ( // Env variables const ( IPFS_PATH = "IPFS_PATH" + IPFS_MODE = "IPFS_MODE" HTTP_TIMEOUT = "HTTP_TIMEOUT" ETH_WS_PATH = "ETH_WS_PATH" @@ -82,6 +83,16 @@ func GetIPFSPath() (string, error) { return ipfsPath, nil } +// GetIPFSMode returns the ipfs mode of operation from the config or env variable +func GetIPFSMode() (IPFSMode, error) { + viper.BindEnv("ipfs.mode", IPFS_MODE) + ipfsMode := viper.GetString("ipfs.mode") + if ipfsMode == "" { + return DirectPostgres, nil + } + return NewIPLFMode(ipfsMode) +} + // GetBtcNodeAndClient returns btc node info from path url func GetBtcNodeAndClient(path string) (core.Node, *rpcclient.ConnConfig) { viper.BindEnv("bitcoin.nodeID", BTC_NODE_ID) diff --git a/pkg/super_node/shared/functions.go b/pkg/super_node/shared/functions.go index ef338fb7..5547f8bf 100644 --- a/pkg/super_node/shared/functions.go +++ b/pkg/super_node/shared/functions.go @@ -19,6 +19,9 @@ package shared import ( "bytes" + "github.com/jmoiron/sqlx" + "github.com/sirupsen/logrus" + "github.com/ethereum/go-ethereum/common" "github.com/vulcanize/vulcanizedb/pkg/ipfs" @@ -69,3 +72,10 @@ func HandleNullAddr(to common.Address) string { } return to.Hex() } + +// Rollback sql transaction and log any error +func Rollback(tx *sqlx.Tx) { + if err := tx.Rollback(); err != nil { + logrus.Error(err) + } +} diff --git a/pkg/super_node/shared/ipfs_mode.go b/pkg/super_node/shared/ipfs_mode.go new file mode 100644 index 00000000..fe8111a1 --- /dev/null +++ b/pkg/super_node/shared/ipfs_mode.go @@ -0,0 +1,58 @@ +// VulcanizeDB +// Copyright © 2019 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package shared + +import ( + "errors" + "strings" +) + +// IPFSMode enum for specifying how we want to interface and publish objects to IPFS +type IPFSMode int + +const ( + Unknown IPFSMode = iota + LocalInterface + RemoteClient + DirectPostgres +) + +func (c IPFSMode) String() string { + switch c { + case LocalInterface: + return "Local" + case RemoteClient: + return "Remote" + case DirectPostgres: + return "Postgres" + default: + return "" + } +} + +func NewIPLFMode(name string) (IPFSMode, error) { + switch strings.ToLower(name) { + case "local", "interface", "minimal": + return LocalInterface, nil + case "remote", "client": + return RemoteClient, nil + case "postgres", "direct": + return DirectPostgres, nil + default: + return Unknown, errors.New("invalid name for ipfs mode") + } +}