Skip to content

Commit

Permalink
Merge branch 'republish'
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacia committed Oct 9, 2017
2 parents 651f633 + 1dd3afb commit f1ca18d
Show file tree
Hide file tree
Showing 19 changed files with 1,189 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func (n *OpenBazaarNode) publish(hash string) {
}
}

func (n *OpenBazaarNode) SetUpRepublisher(interval time.Duration) {
if interval == 0 {
return
}
ticker := time.NewTicker(interval)
go func() {
for range ticker.C {
n.SeedNode()
}
}()
}

/* This is a placeholder until the libsignal is operational.
For now we will just encrypt outgoing offline messages with the long lived identity key.
Optionally you may provide a public key, to avoid doing an IPFS lookup */
Expand Down
10 changes: 10 additions & 0 deletions mobile/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ func (n *Node) Start() error {
proto.Unmarshal(dhtrec.GetValue(), e)
n.node.RootHash = ipath.Path(e.Value).String()

configFile, err := ioutil.ReadFile(path.Join(n.node.RepoPath, "config"))
if err != nil {
return nil, err
}
republishInterval, err := repo.GetRepublishInterval(configFile)
if err != nil {
return nil, err
}

// Offline messaging storage
n.node.MessageStorage = selfhosted.NewSelfHostedStorage(n.node.RepoPath, ctx, n.node.PushNodes, n.node.SendStore)

Expand Down Expand Up @@ -350,6 +359,7 @@ func (n *Node) Start() error {
if !core.InitalPublishComplete {
core.Node.SeedNode()
}
core.Node.SetUpRepublisher(republishInterval)
}()

return nil
Expand Down
50 changes: 34 additions & 16 deletions openbazaard.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/base58"
tm "github.com/buger/goterm"
"github.com/fatih/color"
"github.com/ipfs/go-ipfs/commands"
ipfscore "github.com/ipfs/go-ipfs/core"
Expand Down Expand Up @@ -394,11 +395,6 @@ func (x *Init) Execute(args []string) error {
func (x *Start) Execute(args []string) error {
printSplashScreen()

err := core.CheckAndSetUlimit()
if err != nil {
return err
}

if x.Testnet && x.Regtest {
return errors.New("Invalid combination of testnet and regtest modes")
}
Expand Down Expand Up @@ -442,7 +438,7 @@ func (x *Start) Execute(args []string) error {
backendStdoutFormatter = logging.NewBackendFormatter(backendStdout, stdoutLogFormat)
logging.SetBackend(backendStdoutFormatter)
} else {
printInPlace("initializing...")
DrawTerminal("initializing...")
}

if !x.NoLogFiles {
Expand Down Expand Up @@ -482,6 +478,11 @@ func (x *Start) Execute(args []string) error {
}
logging.SetLevel(level, "")

err = core.CheckAndSetUlimit()
if err != nil {
return err
}

// If the database cannot be decrypted, exit
if sqliteDB.Config().IsEncrypted() {
sqliteDB.Close()
Expand Down Expand Up @@ -542,6 +543,11 @@ func (x *Start) Execute(args []string) error {
log.Error(err)
return err
}
republishInterval, err := repo.GetRepublishInterval(configFile)
if err != nil {
log.Error(err)
return err
}

// IPFS node setup
r, err := fsrepo.Open(repoPath)
Expand Down Expand Up @@ -1005,11 +1011,11 @@ func (x *Start) Execute(args []string) error {

go func() {
if !x.Verbose {
printInPlace("bootstrapping...")
DrawTerminal("bootstrapping...")
}
<-dht.DefaultBootstrapConfig.DoneChan
if !x.Verbose {
printInPlace("downloading messages...")
DrawTerminal("downloading messages...")
}
core.Node.Service = service.New(core.Node, ctx, sqliteDB)
MR := ret.NewMessageRetriever(sqliteDB, ctx, nd, bm, core.Node.Service, 14, core.Node.PushNodes, torDialer, core.Node.SendOfflineAck)
Expand All @@ -1032,17 +1038,18 @@ func (x *Start) Execute(args []string) error {
core.PublishLock.Unlock()
core.Node.UpdateFollow()
if !x.Verbose {
printInPlace("publishing...")
DrawTerminal("publishing...")
}
if !core.InitalPublishComplete {
core.Node.SeedNode()
}
core.Node.SetUpRepublisher(republishInterval)
if !x.Verbose {
time.Sleep(time.Second * 3)
core.PublishLock.Lock()
printInPlace("Running...")
DrawTerminal("running...")
core.PublishLock.Unlock()
}

}()

// Start gateway
Expand Down Expand Up @@ -1232,11 +1239,22 @@ func printSplashScreen() {
blue.DisableColor()
white.DisableColor()
fmt.Println("")
fmt.Println("OpenBazaar Server v" + core.VERSION + " starting...")
fmt.Println("OpenBazaar Server v" + core.VERSION)
fmt.Println("")
fmt.Println("Welcome to OpenBazaar! Make sure you read the server docs to learn how to configure your node privately and securely <https://github.com/OpenBazaar/openbazaar-go/tree/master/docs>.")
fmt.Println("")
}

func printInPlace(s string) {
fmt.Printf("\033[0;0H")
fmt.Println(s)
fmt.Println("[Press Ctrl+C to exit]")
var drawStarted bool

func DrawTerminal(s string) {
if drawStarted {
tm.MoveCursorUp(3)
}
tm.ResetLine(" ")
tm.Println(fmt.Sprintf("- %s%s", s, strings.Repeat(" ", 100)))
tm.Println("[Press Ctrl+C to exit]")
tm.MoveCursorUp(1)
tm.Flush()
drawStarted = true
}
28 changes: 28 additions & 0 deletions repo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/config"
"time"
)

var DefaultBootstrapAddresses = []string{
Expand Down Expand Up @@ -369,6 +370,33 @@ func GetDropboxApiToken(cfgBytes []byte) (string, error) {
return tokenStr, nil
}

func GetRepublishInterval(cfgBytes []byte) (time.Duration, error) {
var cfgIface interface{}
json.Unmarshal(cfgBytes, &cfgIface)

cfg, ok := cfgIface.(map[string]interface{})
if !ok {
return time.Duration(0), MalformedConfigError
}

interval, ok := cfg["RepublishInterval"]
if !ok {
return time.Duration(0), MalformedConfigError
}
intervalStr, ok := interval.(string)
if !ok {
return time.Duration(0), MalformedConfigError
}
if intervalStr == "" {
return time.Duration(0), nil
}
d, err := time.ParseDuration(intervalStr)
if err != nil {
return d, err
}
return d, nil
}

func GetDataSharing(cfgBytes []byte) (*DataSharing, error) {
var cfgIface interface{}
json.Unmarshal(cfgBytes, &cfgIface)
Expand Down
23 changes: 23 additions & 0 deletions repo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"
)

const testConfigFolder = "testdata"
Expand Down Expand Up @@ -128,6 +129,28 @@ func TestGetDropboxApiToken(t *testing.T) {
}
}

func TestRepublishInterval(t *testing.T) {
configFile, err := ioutil.ReadFile(testConfigPath)
if err != nil {
t.Error(err)
}
interval, err := GetRepublishInterval(configFile)
if interval != time.Hour*24 {
t.Error("RepublishInterval does not equal expected value")
}
if err != nil {
t.Error("RepublishInterval threw an unexpected error")
}

interval, err = GetRepublishInterval([]byte{})
if interval != time.Second*0 {
t.Error("Expected zero duration, got ", interval)
}
if err == nil {
t.Error("GetRepublishInterval didn't throw an error")
}
}

func TestGetResolverConfig(t *testing.T) {
configFile, err := ioutil.ReadFile(testConfigPath)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions repo/db/following.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func (f *FollowingDB) Get(offsetId string, limit int) ([]string, error) {
} else {
stm = "select peerID from following order by rowid desc limit " + strconv.Itoa(limit) + " offset 0"
}
rows, _ := f.db.Query(stm)
defer rows.Close()
var ret []string
rows, err := f.db.Query(stm)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var peerID string
rows.Scan(&peerID)
Expand Down
5 changes: 4 additions & 1 deletion repo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"
)

const RepoVersion = "3"
const RepoVersion = "4"

var log = logging.MustGetLogger("repo")
var ErrRepoExists = errors.New("IPFS configuration file exists. Reinitializing would overwrite your keys. Use -f to force overwrite.")
Expand Down Expand Up @@ -241,6 +241,9 @@ func addConfigExtensions(repoRoot string, testnet bool) error {
if err := extendConfigFile(r, "Dropbox-api-token", ""); err != nil {
return err
}
if err := extendConfigFile(r, "RepublishInterval", "24h"); err != nil {
return err
}
if err := extendConfigFile(r, "JSON-API", a); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions repo/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var Migrations = []Migration{
migrations.Migration000,
migrations.Migration001,
migrations.Migration002,
migrations.Migration003,
}

// MigrateUp looks at the currently active migration version
Expand Down
93 changes: 93 additions & 0 deletions repo/migrations/Migrations003.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package migrations

import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path"
)

var Migration003 migration003

type migration003 struct{}

func (migration003) Up(repoPath string) error {
configFile, err := ioutil.ReadFile(path.Join(repoPath, "config"))
if err != nil {
return err
}
var cfgIface interface{}
json.Unmarshal(configFile, &cfgIface)
cfg, ok := cfgIface.(map[string]interface{})
if !ok {
return errors.New("Invalid config file")
}

cfg["RepublishInterval"] = "24h"

out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
f, err := os.Create(path.Join(repoPath, "config"))
if err != nil {
return err
}
_, err = f.Write(out)
if err != nil {
return err
}
f.Close()

f1, err := os.Create(path.Join(repoPath, "repover"))
if err != nil {
return err
}
_, err = f1.Write([]byte("4"))
if err != nil {
return err
}
f1.Close()
return nil
}

func (migration003) Down(repoPath string) error {
configFile, err := ioutil.ReadFile(path.Join(repoPath, "config"))
if err != nil {
return err
}
var cfgIface interface{}
json.Unmarshal(configFile, &cfgIface)
cfg, ok := cfgIface.(map[string]interface{})
if !ok {
return errors.New("Invalid config file")
}

delete(cfg, "RepublishInterval")

out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
f, err := os.Create(path.Join(repoPath, "config"))
if err != nil {
return err
}
_, err = f.Write(out)
if err != nil {
return err
}
f.Close()

f1, err := os.Create(path.Join(repoPath, "repover"))
if err != nil {
return err
}
_, err = f1.Write([]byte("3"))
if err != nil {
return err
}
f1.Close()
return nil
}
Loading

0 comments on commit f1ca18d

Please sign in to comment.