Skip to content

Commit

Permalink
IPFS rebase second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacia committed Jun 26, 2018
1 parent 0361db6 commit 159c03b
Show file tree
Hide file tree
Showing 3,181 changed files with 1,072,649 additions and 172 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1,807 changes: 1,807 additions & 0 deletions Godeps/Godeps.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Godeps/Readme

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

2 changes: 1 addition & 1 deletion api/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewGateway(n *core.OpenBazaarNode, authCookie http.Cookie, l net.Listener,
if err != nil {
return nil, err
}
wsAPI, err := newWSAPIHandler(n, n.Context, authCookie, config)
wsAPI, err := newWSAPIHandler(n, authCookie, config)
if err != nil {
return nil, err
}
Expand Down
38 changes: 16 additions & 22 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,7 @@ func (i *jsonAPIHandler) GETStatus(w http.ResponseWriter, r *http.Request) {
}

func (i *jsonAPIHandler) GETPeers(w http.ResponseWriter, r *http.Request) {
peers, err := ipfs.ConnectedPeers(i.node.Context)
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

peers := ipfs.ConnectedPeers(i.node.IpfsNode)
peerJson, err := json.MarshalIndent(peers, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -1011,7 +1006,7 @@ func (i *jsonAPIHandler) PATCHSettings(w http.ResponseWriter, r *http.Request) {
func (i *jsonAPIHandler) GETClosestPeers(w http.ResponseWriter, r *http.Request) {
_, peerId := path.Split(r.URL.Path)
var peerIds []string
peers, err := ipfs.Query(i.node.Context, peerId)
peers, err := ipfs.Query(i.node.IpfsNode, peerId)
if err == nil {
for _, p := range peers {
peerIds = append(peerIds, p.Pretty())
Expand Down Expand Up @@ -1389,7 +1384,7 @@ func (i *jsonAPIHandler) GETListing(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusNotFound, "Listing not found.")
return
}
hash, err := ipfs.GetHashOfFile(i.node.Context, path.Join(i.node.RepoPath, "root", "listings", listingId+".json"))
hash, err := ipfs.GetHashOfFile(i.node.IpfsNode, path.Join(i.node.RepoPath, "root", "listings", listingId+".json"))
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -1429,7 +1424,7 @@ func (i *jsonAPIHandler) GETListing(w http.ResponseWriter, r *http.Request) {
var hash string
_, err := cid.Decode(listingId)
if err == nil {
listingBytes, err = ipfs.Cat(i.node.Context, listingId, time.Minute)
listingBytes, err = ipfs.Cat(i.node.IpfsNode, listingId, time.Minute)
if err != nil {
ErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -1448,7 +1443,7 @@ func (i *jsonAPIHandler) GETListing(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusNotFound, err.Error())
return
}
hash, err = ipfs.GetHash(i.node.Context, bytes.NewReader(listingBytes))
hash, err = ipfs.GetHash(i.node.IpfsNode, bytes.NewReader(listingBytes))
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -3046,7 +3041,7 @@ func (i *jsonAPIHandler) POSTBlockNode(w http.ResponseWriter, r *http.Request) {
nodes = append(nodes, pid)
}
}
go ipfs.RemoveAll(i.node.Context, peerId)
go ipfs.RemoveAll(i.node.IpfsNode, peerId)
nodes = append(nodes, peerId)
settings.BlockedNodes = &nodes
if err := i.node.Datastore.Settings().Put(settings); err != nil {
Expand Down Expand Up @@ -3304,7 +3299,7 @@ func (i *jsonAPIHandler) GETRatings(w http.ResponseWriter, r *http.Request) {
func (i *jsonAPIHandler) GETRating(w http.ResponseWriter, r *http.Request) {
_, ratingID := path.Split(r.URL.Path)

ratingBytes, err := ipfs.Cat(i.node.Context, ratingID, time.Minute)
ratingBytes, err := ipfs.Cat(i.node.IpfsNode, ratingID, time.Minute)
if err != nil {
ErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand Down Expand Up @@ -3347,7 +3342,7 @@ func (i *jsonAPIHandler) POSTFetchRatings(w http.ResponseWriter, r *http.Request
for _, id := range rp {
wg.Add(1)
go func(rid string) {
ratingBytes, err := ipfs.Cat(i.node.Context, rid, time.Minute)
ratingBytes, err := ipfs.Cat(i.node.IpfsNode, rid, time.Minute)
if err != nil {
return
}
Expand Down Expand Up @@ -3421,7 +3416,7 @@ func (i *jsonAPIHandler) POSTFetchRatings(w http.ResponseWriter, r *http.Request
i.node.Broadcast <- repo.PremarshalledNotifier{ret}
return
}
ratingBytes, err := ipfs.Cat(i.node.Context, rid, time.Minute)
ratingBytes, err := ipfs.Cat(i.node.IpfsNode, rid, time.Minute)
if err != nil {
respondWithError("Not Found")
return
Expand Down Expand Up @@ -3501,11 +3496,11 @@ func (i *jsonAPIHandler) GETHealthCheck(w http.ResponseWriter, r *http.Request)
if ferr != nil {
re.IPFSRoot = false
}
peers, perr := ipfs.ConnectedPeers(i.node.Context)
if perr != nil || len(peers) == 0 {
peers := ipfs.ConnectedPeers(i.node.IpfsNode)
if len(peers) == 0 {
re.Peers = false
}
if pingErr != nil || ferr != nil || perr != nil {
if pingErr != nil || ferr != nil {
ret, _ := json.MarshalIndent(re, "", " ")
ErrorResponse(w, http.StatusNotFound, string(ret))
return
Expand Down Expand Up @@ -3621,8 +3616,7 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

go ipfs.Resolve(i.node.Context, peerId, time.Minute)
go ipfs.Resolve(i.node.IpfsNode, pid, time.Minute)
fmt.Fprint(w, string(retBytes))
}

Expand Down Expand Up @@ -3895,7 +3889,7 @@ func (i *jsonAPIHandler) GETPost(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusNotFound, "Post not found.")
return
}
hash, err := ipfs.GetHashOfFile(i.node.Context, path.Join(i.node.RepoPath, "root", "posts", postId+".json"))
hash, err := ipfs.GetHashOfFile(i.node.IpfsNode, path.Join(i.node.RepoPath, "root", "posts", postId+".json"))
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -3915,7 +3909,7 @@ func (i *jsonAPIHandler) GETPost(w http.ResponseWriter, r *http.Request) {
var hash string
_, err := cid.Decode(postId)
if err == nil {
postBytes, err = ipfs.Cat(i.node.Context, postId, time.Minute)
postBytes, err = ipfs.Cat(i.node.IpfsNode, postId, time.Minute)
if err != nil {
ErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -3934,7 +3928,7 @@ func (i *jsonAPIHandler) GETPost(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusNotFound, err.Error())
return
}
hash, err = ipfs.GetHash(i.node.Context, bytes.NewReader(postBytes))
hash, err = ipfs.GetHash(i.node.IpfsNode, bytes.NewReader(postBytes))
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
5 changes: 2 additions & 3 deletions api/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type wsHandler struct {
password string
}

func newWSAPIHandler(node *core.OpenBazaarNode, ctx commands.Context, authCookie http.Cookie, config schema.APIConfig) (*wsHandler, error) {
func newWSAPIHandler(node *core.OpenBazaarNode, authCookie http.Cookie, config schema.APIConfig) (*wsHandler, error) {
hub := newHub()
go hub.run()
allowedIps := make(map[string]bool)
Expand All @@ -76,8 +76,7 @@ func newWSAPIHandler(node *core.OpenBazaarNode, ctx commands.Context, authCookie
}
handler = wsHandler{
h: hub,
path: ctx.ConfigRoot,
context: ctx,
path: node.RepoPath,
enabled: config.Enabled,
authenticated: config.Authenticated,
allowedIPs: allowedIps,
Expand Down
14 changes: 1 addition & 13 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

"github.com/OpenBazaar/openbazaar-go/ipfs"

"github.com/ipfs/go-ipfs/commands"
ipfscore "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/config"
"io/ioutil"
"strings"

Expand Down Expand Up @@ -240,16 +238,6 @@ func (x *Convert) Execute(args []string) error {
return err
}

ctx := commands.Context{}
ctx.Online = false
ctx.ConfigRoot = repoPath
ctx.LoadConfig = func(path string) (*config.Config, error) {
return fsrepo.ConfigAt(repoPath)
}
ctx.ConstructNode = func() (*ipfscore.IpfsNode, error) {
return nd, nil
}

files, err := ioutil.ReadDir(path.Join(repoPath, "root", "listings"))
if err != nil {
return err
Expand Down Expand Up @@ -294,7 +282,7 @@ func (x *Convert) Execute(args []string) error {
if err := ioutil.WriteFile(path.Join(repoPath, "root", "listings", f.Name()), []byte(out), os.ModePerm); err != nil {
return err
}
h, err := ipfs.GetHashOfFile(ctx, path.Join(repoPath, "root", "listings", f.Name()))
h, err := ipfs.GetHashOfFile(nd, path.Join(repoPath, "root", "listings", f.Name()))
if err != nil {
return err
}
Expand Down
62 changes: 29 additions & 33 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (

"github.com/OpenBazaar/openbazaar-go/ipfs"
obnet "github.com/OpenBazaar/openbazaar-go/net"
"github.com/ipfs/go-ipfs/commands"
ipfscore "github.com/ipfs/go-ipfs/core"
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap/network"
"github.com/ipfs/go-ipfs/namesys"
"github.com/ipfs/go-ipfs/repo/config"
"io/ioutil"
"strings"

Expand All @@ -24,25 +22,26 @@ import (
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/openbazaar-go/repo/db"
"github.com/OpenBazaar/openbazaar-go/schema"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
ipfspath "github.com/ipfs/go-ipfs/path"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/proxy"
"gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
metrics "gx/ipfs/QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy/go-libp2p-metrics"
"gx/ipfs/QmZPrWxuM8GHr4cGKbyF5CCT11sFUP9hgqpeUHALvx2nUr/go-libp2p-interface-pnet"
addrutil "gx/ipfs/QmNSWW3Sb4eju4o2djPQ1L1c2Zj9XN9sMYJL8r1cbxdc6b/go-addr-util"
p2pbhost "gx/ipfs/QmNh1kGFFdsPu79KNSaL4NUKUPb4Eiz4KHdMtFY6664RDp/go-libp2p/p2p/host/basic"
p2phost "gx/ipfs/QmNmJZL7FQySMtE2BQuLMuZg2EB2CLEunJJUSVSc9YnnbV/go-libp2p-host"
dht "gx/ipfs/QmRaVcGchmC1stHHK7YhcgEuTk5k1JiGS568pfYWMgT91H/go-libp2p-kad-dht"
dhtutil "gx/ipfs/QmRaVcGchmC1stHHK7YhcgEuTk5k1JiGS568pfYWMgT91H/go-libp2p-kad-dht/util"
addrutil "gx/ipfs/QmNSWW3Sb4eju4o2djPQ1L1c2Zj9XN9sMYJL8r1cbxdc6b/go-addr-util"
swarm "gx/ipfs/QmSwZMWwFZSUpe5muU2xgTUwppH24KfMwdPXiwbEp2c6G5/go-libp2p-swarm"
oniontp "gx/ipfs/QmVYZ6jGE4uogWAZK2w8PrKWDEKMvYaQWTSXWCbYJLEuKs/go-onion-transport"
swarm "gx/ipfs/QmWpJ4y2vxJ6GZpPfQbpVpQxAYS3UeR6AKNbAHxw7wN3qw/go-libp2p-swarm"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
smux "gx/ipfs/QmY9JXR3FupnYAYJWK9aMr9bCpqWKcToQ1tz8DVGTrHpHw/go-stream-muxer"
p2phost "gx/ipfs/QmaSxYRuMq4pkpBBG2CYaRrPx2z7NmMVEs34b9g61biQA6/go-libp2p-host"
"gx/ipfs/QmZPrWxuM8GHr4cGKbyF5CCT11sFUP9hgqpeUHALvx2nUr/go-libp2p-interface-pnet"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
"gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
metrics "gx/ipfs/QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy/go-libp2p-metrics"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -270,16 +269,6 @@ func (x *Restore) Execute(args []string) error {
return err
}

ctx := commands.Context{}
ctx.Online = true
ctx.ConfigRoot = repoPath
ctx.LoadConfig = func(path string) (*config.Config, error) {
return fsrepo.ConfigAt(repoPath)
}
ctx.ConstructNode = func() (*ipfscore.IpfsNode, error) {
return nd, nil
}

// Set IPNS query size
querySize := cfg.Ipns.QuerySize
if querySize <= 20 && querySize > 0 {
Expand All @@ -292,7 +281,12 @@ func (x *Restore) Execute(args []string) error {
<-dht.DefaultBootstrapConfig.DoneChan
wg := new(sync.WaitGroup)
wg.Add(10)
k, err := ipfs.Resolve(ctx, identity.PeerID, time.Minute)
pid, err := peer.IDB58Decode(identity.PeerID)
if err != nil {
PrintError(err.Error())
return err
}
k, err := ipfs.Resolve(nd, pid, time.Minute)
if err != nil || k == "" {
PrintError(fmt.Sprintf("IPNS record for %s not found on network\n", identity.PeerID))
return err
Expand All @@ -302,40 +296,42 @@ func (x *Restore) Execute(args []string) error {
PrintError(err.Error())
return err
}
links, err := nd.DAG.GetLinks(context.Background(), c)
node, err := nd.DAG.Get(context.Background(), c)
if err != nil {
PrintError(err.Error())
return err
}
links := node.Links()
for _, l := range links {
if l.Name == "listings" || l.Name == "ratings" || l.Name == "feed" || l.Name == "channel" || l.Name == "files" {
go RestoreDirectory(repoPath, l.Name, nd, l.Cid, wg)
} else if l.Name == "images" {
ilinks, err := nd.DAG.GetLinks(context.Background(), l.Cid)
node, err := nd.DAG.Get(context.Background(), l.Cid)
if err != nil {
PrintError(err.Error())
return err
}
ilinks := node.Links()
for _, link := range ilinks {
wg.Add(1)
go RestoreDirectory(repoPath, path.Join("images", link.Name), nd, link.Cid, wg)
}
}
}

go RestoreFile(repoPath, identity.PeerID, "profile.json", ctx, wg)
go RestoreFile(repoPath, identity.PeerID, "ratings.json", ctx, wg)
go RestoreFile(repoPath, identity.PeerID, "listings.json", ctx, wg)
go RestoreFile(repoPath, identity.PeerID, "following.json", ctx, wg)
go RestoreFile(repoPath, identity.PeerID, "followers.json", ctx, wg)
go RestoreFile(repoPath, identity.PeerID, "profile.json", nd, wg)
go RestoreFile(repoPath, identity.PeerID, "ratings.json", nd, wg)
go RestoreFile(repoPath, identity.PeerID, "listings.json", nd, wg)
go RestoreFile(repoPath, identity.PeerID, "following.json", nd, wg)
go RestoreFile(repoPath, identity.PeerID, "followers.json", nd, wg)
wg.Wait()
fmt.Println("Finished")
return nil
}

func RestoreFile(repoPath, peerID, filename string, ctx commands.Context, wg *sync.WaitGroup) {
func RestoreFile(repoPath, peerID, filename string, n *core.IpfsNode, wg *sync.WaitGroup) {
defer wg.Done()
b, err := ipfs.ResolveThenCat(ctx, ipfspath.FromString(path.Join(peerID, filename)), time.Minute)
b, err := ipfs.ResolveThenCat(n, ipfspath.FromString(path.Join(peerID, filename)), time.Minute)
if err != nil {
PrintError(fmt.Sprintf("Failed to find %s\n", filename))
} else {
Expand All @@ -349,12 +345,12 @@ func RestoreFile(repoPath, peerID, filename string, ctx commands.Context, wg *sy

func RestoreDirectory(repoPath, directory string, nd *ipfscore.IpfsNode, id *cid.Cid, wg *sync.WaitGroup) {
defer wg.Done()
links, err := nd.DAG.GetLinks(context.Background(), id)
node, err := nd.DAG.Get(context.Background(), id)
if err != nil {
PrintError(err.Error())
return
}
for _, l := range links {
for _, l := range node.Links() {
wg.Add(1)
go func(link *ipld.Link) {
defer wg.Done()
Expand Down
Loading

0 comments on commit 159c03b

Please sign in to comment.