Skip to content

Commit

Permalink
Merge pull request #334 from lazyledger/hlib/add-ipfs-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan authored May 20, 2021
2 parents 54c781f + f828736 commit 7d73008
Show file tree
Hide file tree
Showing 43 changed files with 1,021 additions and 631 deletions.
97 changes: 3 additions & 94 deletions cmd/tendermint/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package commands

import (
"fmt"
"os"
"path/filepath"
"sync"

ipfscfg "github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/plugin/loader"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/spf13/cobra"

cfg "github.com/lazyledger/lazyledger-core/config"
"github.com/lazyledger/lazyledger-core/ipfs"
tmos "github.com/lazyledger/lazyledger-core/libs/os"
tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
"github.com/lazyledger/lazyledger-core/p2p"
Expand Down Expand Up @@ -79,7 +73,6 @@ func initFilesWithConfig(config *cfg.Config) error {
if tmos.FileExists(genFile) {
logger.Info("Found genesis file", "path", genFile)
} else {

genDoc := types.GenesisDoc{
ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
GenesisTime: tmtime.Now(),
Expand All @@ -106,90 +99,6 @@ func initFilesWithConfig(config *cfg.Config) error {
logger.Info("Generated genesis file", "path", genFile)
}

if err := InitIpfs(config); err != nil {
return err
}

return nil
}

// InitIpfs takes a few config flags from the tendermint config.IPFS
// and applies them to the freshly created IPFS repo.
// The IPFS config will stored under config.IPFS.ConfigRootPath.
// TODO(ismail) move into separate file, and consider making IPFS initialization
// independent from the `tendermint init` subcommand.
// TODO(ismail): add counter part in ResetAllCmd
func InitIpfs(config *cfg.Config) error {
repoRoot := config.IPFSRepoRoot()
if fsrepo.IsInitialized(repoRoot) {
logger.Info("IPFS was already initialized", "ipfs-path", repoRoot)
return nil
}
var conf *ipfscfg.Config

identity, err := ipfscfg.CreateIdentity(os.Stdout, []options.KeyGenerateOption{
options.Key.Type(options.Ed25519Key),
})
if err != nil {
return err
}

logger.Info("initializing IPFS node", "ipfs-path", repoRoot)

if err := tmos.EnsureDir(repoRoot, 0700); err != nil {
return err
}

conf, err = ipfscfg.InitWithIdentity(identity)
if err != nil {
return err
}

applyFromTmConfig(conf, config.IPFS)
if err := setupPlugins(repoRoot); err != nil {
return err
}

if err := fsrepo.Init(repoRoot, conf); err != nil {
return err
}
return nil
}

// Inject replies on several global vars internally.
// For instance fsrepo.AddDatastoreConfigHandler will error
// if called multiple times with the same datastore.
// But for CI and integration tests, we want to setup the plugins
// for each repo but only inject once s.t. we can init multiple
// repos from the same runtime.
// TODO(ismail): find a more elegant way to achieve the same.
var injectPluginsOnce sync.Once

func setupPlugins(path string) error {
// Load plugins. This will skip the repo if not available.
plugins, err := loader.NewPluginLoader(filepath.Join(path, "plugins"))
if err != nil {
return fmt.Errorf("error loading plugins: %s", err)
}

if err := plugins.Initialize(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}

injectPluginsOnce.Do(func() {
err = plugins.Inject()
})
if err != nil {
return fmt.Errorf("error injecting plugins once: %w", err)
}

return nil
}

func applyFromTmConfig(ipfsConf *ipfscfg.Config, tmConf *cfg.IPFSConfig) {
ipfsConf.Addresses.API = ipfscfg.Strings{tmConf.API}
ipfsConf.Addresses.Gateway = ipfscfg.Strings{tmConf.Gateway}
ipfsConf.Addresses.Swarm = tmConf.Swarm
ipfsConf.Addresses.Announce = tmConf.Announce
ipfsConf.Addresses.NoAnnounce = tmConf.NoAnnounce
// TODO(ismail): add counter part in ResetAllCmd
return ipfs.InitRepo(config.IPFS.Path(), logger)
}
25 changes: 24 additions & 1 deletion cmd/tendermint/commands/run_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"github.com/spf13/cobra"

cfg "github.com/lazyledger/lazyledger-core/config"
"github.com/lazyledger/lazyledger-core/ipfs"
tmos "github.com/lazyledger/lazyledger-core/libs/os"
nm "github.com/lazyledger/lazyledger-core/node"
)

var (
genesisHash []byte
initIPFS bool
)

// AddNodeFlags exposes some common configuration options on the command-line
Expand Down Expand Up @@ -89,6 +91,23 @@ func AddNodeFlags(cmd *cobra.Command) {
"db-dir",
config.DBPath,
"database directory")

cmd.Flags().String(
"ipfs.repo-path",
config.IPFS.RepoPath,
"custom IPFS repository path. Defaults to `.{RootDir}/ipfs`",
)
cmd.Flags().Bool(
"ipfs.serve-api",
config.IPFS.ServeAPI,
"set this to expose IPFS API(useful for debugging)",
)
cmd.Flags().BoolVar(
&initIPFS,
"ipfs.init",
false,
"set this to initialize repository for embedded IPFS node. Flag is ignored if repo is already initialized",
)
}

// NewRunNodeCmd returns the command that allows the CLI to start a node.
Expand All @@ -103,7 +122,11 @@ func NewRunNodeCmd(nodeProvider nm.Provider) *cobra.Command {
return err
}

n, err := nodeProvider(config, logger)
n, err := nodeProvider(
config,
ipfs.Embedded(initIPFS, config.IPFS, logger),
logger,
)
if err != nil {
return fmt.Errorf("failed to create node: %w", err)
}
Expand Down
27 changes: 15 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"time"

"github.com/lazyledger/lazyledger-core/ipfs"
)

const (
Expand Down Expand Up @@ -66,7 +68,7 @@ type Config struct {
TxIndex *TxIndexConfig `mapstructure:"tx-index"`
Instrumentation *InstrumentationConfig `mapstructure:"instrumentation"`
// Options for IPFS service
IPFS *IPFSConfig `mapstructure:"ipfs"`
IPFS *ipfs.Config `mapstructure:"ipfs"`
}

// DefaultConfig returns a default configuration for a Tendermint node
Expand All @@ -81,7 +83,7 @@ func DefaultConfig() *Config {
Consensus: DefaultConsensusConfig(),
TxIndex: DefaultTxIndexConfig(),
Instrumentation: DefaultInstrumentationConfig(),
IPFS: DefaultIPFSConfig(),
IPFS: ipfs.DefaultConfig(),
}
}

Expand All @@ -108,6 +110,7 @@ func (cfg *Config) SetRoot(root string) *Config {
cfg.P2P.RootDir = root
cfg.Mempool.RootDir = root
cfg.Consensus.RootDir = root
cfg.IPFS.RootDir = root
return cfg
}

Expand Down Expand Up @@ -841,16 +844,16 @@ func DefaultConsensusConfig() *ConsensusConfig {
// TestConsensusConfig returns a configuration for testing the consensus service
func TestConsensusConfig() *ConsensusConfig {
cfg := DefaultConsensusConfig()
cfg.TimeoutPropose = 40 * time.Millisecond
cfg.TimeoutProposeDelta = 1 * time.Millisecond
cfg.TimeoutPrevote = 10 * time.Millisecond
cfg.TimeoutPrevoteDelta = 1 * time.Millisecond
cfg.TimeoutPrecommit = 10 * time.Millisecond
cfg.TimeoutPrecommitDelta = 1 * time.Millisecond
cfg.TimeoutPropose = 100 * time.Millisecond
cfg.TimeoutProposeDelta = 10 * time.Millisecond
cfg.TimeoutPrevote = 40 * time.Millisecond
cfg.TimeoutPrevoteDelta = 10 * time.Millisecond
cfg.TimeoutPrecommit = 40 * time.Millisecond
cfg.TimeoutPrecommitDelta = 10 * time.Millisecond
// NOTE: when modifying, make sure to update time_iota_ms (testGenesisFmt) in toml.go
cfg.TimeoutCommit = 10 * time.Millisecond
cfg.TimeoutCommit = 40 * time.Millisecond
cfg.SkipTimeoutCommit = true
cfg.PeerGossipSleepDuration = 5 * time.Millisecond
cfg.PeerGossipSleepDuration = 10 * time.Millisecond
cfg.PeerQueryMaj23SleepDuration = 250 * time.Millisecond
cfg.DoubleSignCheckHeight = int64(0)
return cfg
Expand Down Expand Up @@ -1007,8 +1010,8 @@ func DefaultInstrumentationConfig() *InstrumentationConfig {
}
}

func TetsIpfsConfig() *IPFSConfig {
return DefaultIPFSConfig()
func TetsIpfsConfig() *ipfs.Config {
return ipfs.DefaultConfig()
}

// TestInstrumentationConfig returns a default configuration for metrics
Expand Down
34 changes: 0 additions & 34 deletions config/ipfs_config.go

This file was deleted.

20 changes: 3 additions & 17 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,23 +439,9 @@ namespace = "{{ .Instrumentation.Namespace }}"
#######################################################
[ipfs]
# IPFS repo root.
repo-root = "{{ .IPFS.ConfigRootPath}}"
## Below options will be passed to ipfs when initializing an ipfs repo.
## They will be written into the repo-root/config on init.
## To modify the generated config, edit repo-root/config accordingly.
# Address for the local API (RPC).
api = "{{ .IPFS.API }}"
# Address to listen on for IPFS HTTP object gateway.
gateway = "{{ .IPFS.Gateway }}"
# Addresses for the swarm to listen on
swarm = [{{ range .IPFS.Swarm }}{{ printf "%q, " . }}{{end}}]
# Swarm addresses to announce to the network
announce = [{{ range .IPFS.Announce }}{{ printf "%q, " . }}{{end}}]
# Swarm addresses not to announce to the network
no-announce = [{{ range .IPFS.NoAnnounce }}{{ printf "%q, " . }}{{end}}]
# IPFS related configuration
repo-path = "{{ .IPFS.RepoPath}}"
serve-api = "{{ .IPFS.ServeAPI}}"
`

/****** these are for test settings ***********/
Expand Down
2 changes: 1 addition & 1 deletion consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type cleanupFunc func()
var (
config *cfg.Config // NOTE: must be reset for each _test.go file
consensusReplayConfig *cfg.Config
ensureTimeout = 600 * time.Millisecond
ensureTimeout = 1000 * time.Millisecond
)

func ensureDir(dir string, mode os.FileMode) {
Expand Down
16 changes: 12 additions & 4 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/gogo/protobuf/proto"
ipfsapi "github.com/ipfs/interface-go-ipfs-core"
ipface "github.com/ipfs/interface-go-ipfs-core"
cfg "github.com/lazyledger/lazyledger-core/config"
cstypes "github.com/lazyledger/lazyledger-core/consensus/types"
"github.com/lazyledger/lazyledger-core/crypto"
Expand All @@ -25,6 +25,7 @@ import (
"github.com/lazyledger/lazyledger-core/libs/service"
tmsync "github.com/lazyledger/lazyledger-core/libs/sync"
"github.com/lazyledger/lazyledger-core/p2p"
"github.com/lazyledger/lazyledger-core/p2p/ipld"
tmproto "github.com/lazyledger/lazyledger-core/proto/tendermint/types"
sm "github.com/lazyledger/lazyledger-core/state"
"github.com/lazyledger/lazyledger-core/types"
Expand Down Expand Up @@ -93,7 +94,7 @@ type State struct {
// store blocks and commits
blockStore sm.BlockStore

IpfsAPI ipfsapi.CoreAPI
ipfs ipface.CoreAPI

// create and execute blocks
blockExec *sm.BlockExecutor
Expand Down Expand Up @@ -206,6 +207,13 @@ func NewState(
//----------------------------------------
// Public interface

// SetIPFSApi sets the IPFSAPI
func (cs *State) SetIPFSApi(api ipface.CoreAPI) {
cs.mtx.Lock()
defer cs.mtx.Unlock()
cs.ipfs = api
}

// SetLogger implements Service.
func (cs *State) SetLogger(l log.Logger) {
cs.BaseService.Logger = l
Expand Down Expand Up @@ -1111,12 +1119,12 @@ func (cs *State) defaultDecideProposal(height int64, round int32) {

// post data to ipfs
// TODO(evan): don't hard code context and timeout
if cs.IpfsAPI != nil {
if cs.ipfs != nil {
// longer timeouts result in block proposers failing to propose blocks in time.
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1500)
defer cancel()
// TODO: post data to IPFS in a goroutine
err := block.PutBlock(ctx, cs.IpfsAPI.Dag())
err := ipld.PutBlock(ctx, cs.ipfs.Dag(), block)
if err != nil {
cs.Logger.Error(fmt.Sprintf("failure to post block data to IPFS: %s", err.Error()))
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-ipfs v0.8.0
github.com/ipfs/go-ipfs-api v0.2.0
github.com/ipfs/go-ipfs-config v0.12.0
github.com/ipfs/go-ipfs-config v0.11.0
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-path v0.0.9 // indirect
github.com/ipfs/go-verifcid v0.0.1
Expand All @@ -30,6 +30,7 @@ require (
github.com/lazyledger/rsmt2d v0.2.0
github.com/libp2p/go-buffer-pool v0.0.2
github.com/minio/highwayhash v1.0.1
github.com/multiformats/go-multiaddr v0.3.1
github.com/multiformats/go-multihash v0.0.14
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1
Expand Down
Loading

0 comments on commit 7d73008

Please sign in to comment.