From f1c7a36dfebf49c715c73616675f0db7410f9d3b Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Sun, 1 Jan 2023 14:32:32 -0500 Subject: [PATCH 1/7] added example to add car to blockstore --- examples/addcartopeer.go | 75 ++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 78 insertions(+) create mode 100644 examples/addcartopeer.go diff --git a/examples/addcartopeer.go b/examples/addcartopeer.go new file mode 100644 index 0000000..adedee3 --- /dev/null +++ b/examples/addcartopeer.go @@ -0,0 +1,75 @@ +package main + +import ( + "bytes" + "context" + "fmt" + whypfs "github.com/application-research/whypfs-core" + "github.com/ipfs/go-cid" + format "github.com/ipfs/go-ipld-format" + "github.com/ipfs/go-merkledag" + "github.com/ipld/go-car" +) + +// Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and +// then retrieving the file from the whypfs network. +func AddCarToPeer() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + whypfsPeer, err := whypfs.NewNode(whypfs.NewNodeParams{ + Ctx: ctx, + Datastore: whypfs.NewInMemoryDatastore(), + }) + whypfsPeer.BootstrapPeers(whypfs.DefaultBootstrapPeers()) + + baseNode := merkledag.NewRawNode([]byte("letsrebuildtolearnnewthings!")) + node1 := &merkledag.ProtoNode{} + node2 := &merkledag.ProtoNode{} + node3 := &merkledag.ProtoNode{} + rootNode := &merkledag.ProtoNode{} + node1.AddNodeLink("node1 - yehey", baseNode) + node1.SetData([]byte("node1 - yehey")) + node2.AddNodeLink("node2 - nice", node1) + node2.SetData([]byte("node2 - nice")) + node3.AddNodeLink("node3 - wow", node2) + node3.SetData([]byte("node3 - wow")) + rootNode.AddNodeLink("root - alright", node3) + rootNode.SetData([]byte("root - alright")) + + fmt.Println("Root CID before: ", rootNode.Cid().String()) + + assertAddNodes(whypfsPeer.DAGService, rootNode, node3, node2, node1, baseNode) + + buf := new(bytes.Buffer) + if err := car.WriteCar(context.Background(), whypfsPeer.DAGService, []cid.Cid{rootNode.Cid()}, buf); err != nil { + panic(err) + } + fmt.Println("CAR file size: ", buf.Len()) + ch, err := car.LoadCar(context.Background(), whypfsPeer.Blockservice.Blockstore(), buf) + if err != nil { + panic(err) + } + + fmt.Print(rootNode.Cid().String()) + fmt.Println(string(ch.Version)) + + rootCid, err := whypfsPeer.Get(ctx, rootNode.Cid()) + if err != nil { + panic(err) + } + fmt.Println("Root CID after: ", rootCid.String()) + + for _, link := range rootCid.Links() { + fmt.Println(link.Cid.String()) + } +} + +func assertAddNodes(ds format.DAGService, nds ...format.Node) { + for _, nd := range nds { + fmt.Println("Adding node: ", nd.Cid().String()) + if err := ds.Add(context.Background(), nd); err != nil { + panic(err) + } + } +} diff --git a/go.mod b/go.mod index 3de8e9d..558dbc1 100644 --- a/go.mod +++ b/go.mod @@ -78,6 +78,7 @@ require ( github.com/ipfs/go-log v1.0.5 // indirect github.com/ipfs/go-peertaskqueue v0.7.0 // indirect github.com/ipfs/go-verifcid v0.0.1 // indirect + github.com/ipld/go-car v0.5.0 // indirect github.com/ipld/go-codec-dagpb v1.3.1 // indirect github.com/ipld/go-ipld-prime v0.16.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect diff --git a/go.sum b/go.sum index c8a7ffe..df3f6d9 100644 --- a/go.sum +++ b/go.sum @@ -493,6 +493,8 @@ github.com/ipfs/go-unixfs v0.4.1 h1:nmJFKvF+khK03PIWyCxxydD/nkQX315NZDcgvRqMXf0= github.com/ipfs/go-unixfs v0.4.1/go.mod h1:2SUDFhUSzrcL408B1qpIkJJ5HznnyTzweViPXUAvkNg= github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E= github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0= +github.com/ipld/go-car v0.5.0 h1:kcCEa3CvYMs0iE5BzD5sV7O2EwMiCIp3uF8tA6APQT8= +github.com/ipld/go-car v0.5.0/go.mod h1:ppiN5GWpjOZU9PgpAZ9HbZd9ZgSpwPMr48fGRJOWmvE= github.com/ipld/go-codec-dagpb v1.3.1 h1:yVNlWRQexCa54ln3MSIiUN++ItH7pdhBFhh0hSgZu1w= github.com/ipld/go-codec-dagpb v1.3.1/go.mod h1:ErNNglIi5KMur/MfFE/svtgQthzVvf+43MrzLbpcIZY= github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db/go.mod h1:KvBLMr4PX1gWptgkzRjVZCrLmSGcZCb/jioOQwCqZN8= From 9b824d259bc11768840e734d66ad6e7b8a962654 Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Sun, 1 Jan 2023 14:44:54 -0500 Subject: [PATCH 2/7] added traverse links --- examples/addcartopeer.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/examples/addcartopeer.go b/examples/addcartopeer.go index adedee3..ff8d180 100644 --- a/examples/addcartopeer.go +++ b/examples/addcartopeer.go @@ -13,7 +13,7 @@ import ( // Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and // then retrieving the file from the whypfs network. -func AddCarToPeer() { +func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -54,15 +54,21 @@ func AddCarToPeer() { fmt.Print(rootNode.Cid().String()) fmt.Println(string(ch.Version)) - rootCid, err := whypfsPeer.Get(ctx, rootNode.Cid()) - if err != nil { - panic(err) - } - fmt.Println("Root CID after: ", rootCid.String()) + //rootCid, err := whypfsPeer.Get(ctx, rootNode.Cid()) + //if err != nil { + // panic(err) + //} + //fmt.Println("Root CID after: ", rootCid.String()) - for _, link := range rootCid.Links() { - fmt.Println(link.Cid.String()) + for _, c := range ch.Roots { + rootCid, err := whypfsPeer.Get(ctx, c) + fmt.Println("Root CID after: ", rootCid.String()) + if err != nil { + panic(err) + } + traverseLinks(ctx, whypfsPeer.DAGService, rootCid) } + //traverseLinks(ctx, whypfsPeer.DAGService, rootNode) } func assertAddNodes(ds format.DAGService, nds ...format.Node) { @@ -73,3 +79,15 @@ func assertAddNodes(ds format.DAGService, nds ...format.Node) { } } } + +// function to traverse all links +func traverseLinks(ctx context.Context, ds format.DAGService, nd format.Node) { + for _, link := range nd.Links() { + node, err := link.GetNode(ctx, ds) + if err != nil { + panic(err) + } + fmt.Println("Node CID: ", node.Cid().String()) + traverseLinks(ctx, ds, node) + } +} From 9b4694f75d2ad809dd9d464833b43741b28afa0d Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Sun, 1 Jan 2023 14:48:22 -0500 Subject: [PATCH 3/7] added traverse links --- examples/addcartopeer.go | 12 ++++++++++-- examples/test_file_for_car1.log | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 examples/test_file_for_car1.log diff --git a/examples/addcartopeer.go b/examples/addcartopeer.go index ff8d180..6d99440 100644 --- a/examples/addcartopeer.go +++ b/examples/addcartopeer.go @@ -9,6 +9,7 @@ import ( format "github.com/ipfs/go-ipld-format" "github.com/ipfs/go-merkledag" "github.com/ipld/go-car" + "io/ioutil" ) // Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and @@ -27,6 +28,7 @@ func main() { node1 := &merkledag.ProtoNode{} node2 := &merkledag.ProtoNode{} node3 := &merkledag.ProtoNode{} + node4 := &merkledag.ProtoNode{} rootNode := &merkledag.ProtoNode{} node1.AddNodeLink("node1 - yehey", baseNode) node1.SetData([]byte("node1 - yehey")) @@ -34,12 +36,18 @@ func main() { node2.SetData([]byte("node2 - nice")) node3.AddNodeLink("node3 - wow", node2) node3.SetData([]byte("node3 - wow")) - rootNode.AddNodeLink("root - alright", node3) + node4.AddNodeLink("node4 - cool", node3) + + // file + file, err := ioutil.ReadFile("test_file_for_car1.log") + node4.SetData(file) + + rootNode.AddNodeLink("root - alright", node4) rootNode.SetData([]byte("root - alright")) fmt.Println("Root CID before: ", rootNode.Cid().String()) - assertAddNodes(whypfsPeer.DAGService, rootNode, node3, node2, node1, baseNode) + assertAddNodes(whypfsPeer.DAGService, rootNode, node4, node3, node2, node1, baseNode) buf := new(bytes.Buffer) if err := car.WriteCar(context.Background(), whypfsPeer.DAGService, []cid.Cid{rootNode.Cid()}, buf); err != nil { diff --git a/examples/test_file_for_car1.log b/examples/test_file_for_car1.log new file mode 100644 index 0000000..68a4528 --- /dev/null +++ b/examples/test_file_for_car1.log @@ -0,0 +1 @@ +this is a test file \ No newline at end of file From 4cee0ea30493d7f9f27f73b23626611dbffab27a Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Sun, 1 Jan 2023 14:49:51 -0500 Subject: [PATCH 4/7] chore: added file to be added to the car file --- examples/test_file_for_car1.log | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/test_file_for_car1.log b/examples/test_file_for_car1.log index 68a4528..a6f1d0a 100644 --- a/examples/test_file_for_car1.log +++ b/examples/test_file_for_car1.log @@ -1 +1,19 @@ +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file +this is a test file this is a test file \ No newline at end of file From 324bf200e3b5139f0d41c1c66236c435123f076a Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Tue, 10 Jan 2023 23:35:56 -0500 Subject: [PATCH 5/7] config setting fixes --- examples/addcartopeer.go | 12 +++------ go.mod | 2 +- whypfs.go | 54 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/examples/addcartopeer.go b/examples/addcartopeer.go index 6d99440..83bc8f5 100644 --- a/examples/addcartopeer.go +++ b/examples/addcartopeer.go @@ -14,7 +14,7 @@ import ( // Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and // then retrieving the file from the whypfs network. -func main() { +func AddCarToPeerV1() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -44,8 +44,9 @@ func main() { rootNode.AddNodeLink("root - alright", node4) rootNode.SetData([]byte("root - alright")) + go - fmt.Println("Root CID before: ", rootNode.Cid().String()) + fmt.Println("Root CID before: ", rootNode.Cid().String()) assertAddNodes(whypfsPeer.DAGService, rootNode, node4, node3, node2, node1, baseNode) @@ -62,12 +63,6 @@ func main() { fmt.Print(rootNode.Cid().String()) fmt.Println(string(ch.Version)) - //rootCid, err := whypfsPeer.Get(ctx, rootNode.Cid()) - //if err != nil { - // panic(err) - //} - //fmt.Println("Root CID after: ", rootCid.String()) - for _, c := range ch.Roots { rootCid, err := whypfsPeer.Get(ctx, c) fmt.Println("Root CID after: ", rootCid.String()) @@ -76,7 +71,6 @@ func main() { } traverseLinks(ctx, whypfsPeer.DAGService, rootCid) } - //traverseLinks(ctx, whypfsPeer.DAGService, rootNode) } func assertAddNodes(ds format.DAGService, nds ...format.Node) { diff --git a/go.mod b/go.mod index 558dbc1..fbb5d66 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/ipfs/go-merkledag v0.8.0 github.com/ipfs/go-metrics-interface v0.0.1 github.com/ipfs/go-unixfs v0.4.1 + github.com/ipld/go-car v0.5.0 github.com/libp2p/go-libp2p v0.23.4 github.com/libp2p/go-libp2p-kad-dht v0.18.0 github.com/multiformats/go-multiaddr v0.7.0 @@ -78,7 +79,6 @@ require ( github.com/ipfs/go-log v1.0.5 // indirect github.com/ipfs/go-peertaskqueue v0.7.0 // indirect github.com/ipfs/go-verifcid v0.0.1 // indirect - github.com/ipld/go-car v0.5.0 // indirect github.com/ipld/go-codec-dagpb v1.3.1 // indirect github.com/ipld/go-ipld-prime v0.16.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect diff --git a/whypfs.go b/whypfs.go index 8fc72fc..2132c3b 100644 --- a/whypfs.go +++ b/whypfs.go @@ -149,14 +149,60 @@ func (cfg *Config) setDefaults() { cfg.AnnounceAddrs = []string{"/ip4/0.0.0.0/tcp/0"} } +func (cfg *Config) applyUserDefinedConfig(config *Config) { + if config.Offline { + cfg.Offline = config.Offline + } + if config.ReprovideInterval != 0 { + cfg.ReprovideInterval = config.ReprovideInterval + } + if config.NoBlockstoreCache { + cfg.NoBlockstoreCache = config.NoBlockstoreCache + } + if config.NoAnnounceContent { + cfg.NoAnnounceContent = config.NoAnnounceContent + } + if config.NoLimiter { + cfg.NoLimiter = config.NoLimiter + } + if config.BitswapConfig.MaxOutstandingBytesPerPeer != 0 { + cfg.BitswapConfig.MaxOutstandingBytesPerPeer = config.BitswapConfig.MaxOutstandingBytesPerPeer + } + if config.BitswapConfig.TargetMessageSize != 0 { + cfg.BitswapConfig.TargetMessageSize = config.BitswapConfig.TargetMessageSize + } + if config.ConnectionManagerConfig.HighWater != 0 { + cfg.ConnectionManagerConfig.HighWater = config.ConnectionManagerConfig.HighWater + } + if config.ConnectionManagerConfig.LowWater != 0 { + cfg.ConnectionManagerConfig.LowWater = config.ConnectionManagerConfig.LowWater + } + if config.DatastoreDir.Directory != "" { + cfg.DatastoreDir.Directory = config.DatastoreDir.Directory + } + if config.Blockstore != "" { + cfg.Blockstore = config.Blockstore + } + if config.Libp2pKeyFile != "" { + cfg.Libp2pKeyFile = config.Libp2pKeyFile + } + if config.ListenAddrs != nil { + cfg.ListenAddrs = config.ListenAddrs + } + if config.AnnounceAddrs != nil { + cfg.AnnounceAddrs = config.AnnounceAddrs + } +} + // NewNode creates a new WhyPFS node with the given configuration. func NewNode( nodeParams NewNodeParams) (*Node, error) { var err error - if nodeParams.Config == nil { - nodeParams.Config = &Config{} - nodeParams.Config.setDefaults() - } + // strictly set defaults + nodeParams.Config.setDefaults() + + // override from nodeparams + nodeParams.Config.applyUserDefinedConfig(nodeParams.Config) if nodeParams.Repo == "" { nodeParams.Repo = ".whypfs" From 1011089109a2c2f3898254580802f5012048e29c Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Wed, 11 Jan 2023 16:32:59 -0500 Subject: [PATCH 6/7] config setting fixes --- examples/addcartopeer.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/addcartopeer.go b/examples/addcartopeer.go index 83bc8f5..b618c5f 100644 --- a/examples/addcartopeer.go +++ b/examples/addcartopeer.go @@ -44,9 +44,7 @@ func AddCarToPeerV1() { rootNode.AddNodeLink("root - alright", node4) rootNode.SetData([]byte("root - alright")) - go - - fmt.Println("Root CID before: ", rootNode.Cid().String()) + go fmt.Println("Root CID before: ", rootNode.Cid().String()) assertAddNodes(whypfsPeer.DAGService, rootNode, node4, node3, node2, node1, baseNode) @@ -61,8 +59,6 @@ func AddCarToPeerV1() { } fmt.Print(rootNode.Cid().String()) - fmt.Println(string(ch.Version)) - for _, c := range ch.Roots { rootCid, err := whypfsPeer.Get(ctx, c) fmt.Println("Root CID after: ", rootCid.String()) From 05085bf076735985835c1bba2c6fe7c314bcd036 Mon Sep 17 00:00:00 2001 From: alvin-reyes Date: Thu, 12 Jan 2023 09:41:21 -0500 Subject: [PATCH 7/7] added custom configuration builder --- README.md | 41 ++++++++++++++++++++++++ whypfs.go | 84 +++++++++++++++++++++++++------------------------- whypfs_test.go | 35 +++++++++++++++++++++ 3 files changed, 118 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 620a229..25ad52d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,41 @@ if err != nil { } ``` +## Set up a node with your own config +``` +// initialize a node parameter +params := NewNodeParams{ + Ctx: context.Background(), + Datastore: NewInMemoryDatastore(), +} + +// create a new config of your own +newConfig := &Config{ + Offline: true, + ReprovideInterval: 0, + Libp2pKeyFile: "mykey", + ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"}, + AnnounceAddrs: nil, + DatastoreDir: struct { + Directory string + Options leveldb.Options + }{}, + Blockstore: "", + NoBlockstoreCache: false, + NoAnnounceContent: false, + NoLimiter: false, + BitswapConfig: BitswapConfig{}, + ConnectionManagerConfig: ConnectionManager{}, +} + +// set it +params.Config = params.ConfigurationBuilder(newConfig) +myNode, err := NewNode(params) +if err1 != nil { + t.Fatal(err) +} +``` + ## Add/Pin and Get a file ``` node, err := peer.AddPinFile(context.Background(), bytes.NewReader([]byte("letsrebuildtolearnnewthings!")), nil) @@ -49,3 +84,9 @@ mainDirNode, err := peer.GetDirectory(context.Background(), node) - GetFile function to get a file using a CID - GetDirectory function to retrieve an entire directory from a ipld.Node - Custom bootstrap nodes + +## Examples +There are a few examples on how to utilize the node which includes +- how to create a running peer node. +- how to create a CAR file and add it to the peer node +- how to add a file / dir to the peer node. \ No newline at end of file diff --git a/whypfs.go b/whypfs.go index 2132c3b..4bb2109 100644 --- a/whypfs.go +++ b/whypfs.go @@ -129,8 +129,9 @@ type BitswapConfig struct { TargetMessageSize int } -func (cfg *Config) setDefaults() { +func SetConfigDefaults() *Config { + cfg := &Config{} // optimal settings cfg.Offline = false cfg.ReprovideInterval = defaultReprovideInterval @@ -147,62 +148,61 @@ func (cfg *Config) setDefaults() { cfg.Libp2pKeyFile = filepath.Join("libp2p.key") cfg.ListenAddrs = []string{"/ip4/0.0.0.0/tcp/0"} cfg.AnnounceAddrs = []string{"/ip4/0.0.0.0/tcp/0"} + + return cfg } -func (cfg *Config) applyUserDefinedConfig(config *Config) { - if config.Offline { - cfg.Offline = config.Offline - } - if config.ReprovideInterval != 0 { - cfg.ReprovideInterval = config.ReprovideInterval - } - if config.NoBlockstoreCache { - cfg.NoBlockstoreCache = config.NoBlockstoreCache - } - if config.NoAnnounceContent { - cfg.NoAnnounceContent = config.NoAnnounceContent - } - if config.NoLimiter { - cfg.NoLimiter = config.NoLimiter - } - if config.BitswapConfig.MaxOutstandingBytesPerPeer != 0 { - cfg.BitswapConfig.MaxOutstandingBytesPerPeer = config.BitswapConfig.MaxOutstandingBytesPerPeer +func (n NewNodeParams) ConfigurationBuilder(config *Config) *Config { + + // get the default configuration and override it using the config + defaultConfig := SetConfigDefaults() + + // check each config if it has some value and override the defaultConfig + defaultConfig.NoAnnounceContent = config.NoAnnounceContent + + if config.ConnectionManagerConfig.LowWater > 0 { + defaultConfig.ConnectionManagerConfig.LowWater = config.ConnectionManagerConfig.LowWater } - if config.BitswapConfig.TargetMessageSize != 0 { - cfg.BitswapConfig.TargetMessageSize = config.BitswapConfig.TargetMessageSize + if config.ConnectionManagerConfig.HighWater > 0 { + defaultConfig.ConnectionManagerConfig.HighWater = config.ConnectionManagerConfig.HighWater } - if config.ConnectionManagerConfig.HighWater != 0 { - cfg.ConnectionManagerConfig.HighWater = config.ConnectionManagerConfig.HighWater + + if config.BitswapConfig.TargetMessageSize > 0 { + defaultConfig.BitswapConfig.TargetMessageSize = config.BitswapConfig.TargetMessageSize } - if config.ConnectionManagerConfig.LowWater != 0 { - cfg.ConnectionManagerConfig.LowWater = config.ConnectionManagerConfig.LowWater + if config.BitswapConfig.MaxOutstandingBytesPerPeer > 0 { + defaultConfig.BitswapConfig.TargetMessageSize = config.BitswapConfig.TargetMessageSize } - if config.DatastoreDir.Directory != "" { - cfg.DatastoreDir.Directory = config.DatastoreDir.Directory + + if config.AnnounceAddrs != nil { + defaultConfig.AnnounceAddrs = config.AnnounceAddrs } - if config.Blockstore != "" { - cfg.Blockstore = config.Blockstore + + if config.ListenAddrs != nil { + defaultConfig.ListenAddrs = config.ListenAddrs } + if config.Libp2pKeyFile != "" { - cfg.Libp2pKeyFile = config.Libp2pKeyFile + defaultConfig.Libp2pKeyFile = config.Libp2pKeyFile } - if config.ListenAddrs != nil { - cfg.ListenAddrs = config.ListenAddrs - } - if config.AnnounceAddrs != nil { - cfg.AnnounceAddrs = config.AnnounceAddrs + + if config.DatastoreDir.Directory != "datastore" { + defaultConfig.DatastoreDir = config.DatastoreDir } + + defaultConfig.DatastoreDir.Options = config.DatastoreDir.Options + return defaultConfig } // NewNode creates a new WhyPFS node with the given configuration. -func NewNode( - nodeParams NewNodeParams) (*Node, error) { +func NewNode(nodeParams NewNodeParams) (*Node, error) { + var err error - // strictly set defaults - nodeParams.Config.setDefaults() - // override from nodeparams - nodeParams.Config.applyUserDefinedConfig(nodeParams.Config) + // strictly set defaults + if nodeParams.Config == nil { + nodeParams.Config = SetConfigDefaults() + } if nodeParams.Repo == "" { nodeParams.Repo = ".whypfs" @@ -223,9 +223,9 @@ func NewNode( nodeParams.Config.Blockstore = ":flatfs:" + filepath.Join(nodeParams.Repo, "blocks") - // create the node node := &Node{} node.Config = nodeParams.Config + // create the node context node.Ctx = nodeParams.Ctx node.Datastore = nodeParams.Datastore node.Dht = nodeParams.Dht diff --git a/whypfs_test.go b/whypfs_test.go index c73caeb..6134e16 100644 --- a/whypfs_test.go +++ b/whypfs_test.go @@ -367,3 +367,38 @@ func TestAddPinDirectoryAndGetFromAnotherNode(t *testing.T) { assert.GreaterOrEqual(t, len(retrieveNode.Links()), 1) } + +func TestOverrideDefaultConfig(t *testing.T) { + params := NewNodeParams{ + Ctx: context.Background(), + Datastore: NewInMemoryDatastore(), + } + newConfig := &Config{ + Offline: true, + ReprovideInterval: 0, + Libp2pKeyFile: "mykey", + ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"}, + AnnounceAddrs: nil, + DatastoreDir: struct { + Directory string + Options leveldb.Options + }{}, + Blockstore: "", + NoBlockstoreCache: false, + NoAnnounceContent: false, + NoLimiter: false, + BitswapConfig: BitswapConfig{}, + ConnectionManagerConfig: ConnectionManager{}, + } + params.Config = params.ConfigurationBuilder(newConfig) + + p1, err1 := NewNode(params) + if err1 != nil { + t.Fatal(err1) + } + + assert.Equal(t, p1.Config.Libp2pKeyFile, "mykey") + assert.Equal(t, p1.Config.AnnounceAddrs, []string{"/ip4/0.0.0.0/tcp/0"}) + assert.Equal(t, p1.Config.ListenAddrs, []string{"/ip4/127.0.0.1/tcp/0"}) + assert.Equal(t, p1.Config.Offline, params.Config.Offline) +}