Skip to content

Instantly share code, notes, and snippets.

@tobowers

tobowers/main.go Secret

Created Aug 24, 2018
Embed
What would you like to do?
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-ipld-cbor"
"github.com/ipsn/go-ipfs/gxlibs/github.com/multiformats/go-multihash"
"github.com/ipsn/go-ipfs/core"
logging "github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-log"
)
func init() {
logging.SetLogLevel("core", "debug")
logging.SetLogLevel("blockstore", "debug")
logging.SetLogLevel("main", "debug")
}
var log = logging.Logger("main")
func main() {
shouldPut := flag.Bool("p", false, "Should we put the randevous?")
flag.Parse()
cNode, err := cbornode.WrapObject(map[string]interface{}{"hi": "hi"}, multihash.SHA2_256, -1)
if err != nil {
panic(fmt.Sprintf("error wrapping: %v", err))
}
if *shouldPut {
fmt.Println("putting node")
node, err := core.NewNode(context.TODO(), &core.BuildCfg{Online: true})
if err != nil {
log.Fatalf("Failed to start IPFS node: %v", err)
}
fmt.Println("adding cnode")
err = node.DAG.Add(context.TODO(), cNode)
if err != nil {
panic(err)
}
// node.Pinning.PinWithMode(cNode.Cid(), pin.Recursive)
// err = node.Pinning.Flush()
// if err != nil {
// panic(err)
// }
rnode, _ := node.DAG.Get(context.TODO(), cNode.Cid())
fmt.Printf("got node: %v", rnode)
} else {
ctx, cancelFunc := context.WithTimeout(context.Background(), 60*time.Second)
node2, err := core.NewNode(ctx, &core.BuildCfg{Online: true})
if err != nil {
log.Fatalf("Failed to start IPFS node: %v", err)
}
log.Info("finding providers")
peers, _ := node2.DHT.FindProviders(ctx, cNode.Cid())
log.Infof("peers found: %d", len(peers))
log.Infof("getting cnode from 2: %v\n", cNode.Cid().String())
rNode, err := node2.DAG.Get(context.TODO(), cNode.Cid())
// d := merkledag.NewDAGService(node2.Blocks)
// rNode, err := d.Session(context.TODO()).Get(context.TODO(), cNode.Cid())
if err != nil {
log.Fatalf("Failed to look up: %v", err)
}
cancelFunc()
log.Infof("rNode: %v", rNode)
}
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment