From 2e66a09c9ec889759bb8950823706cab8c5de407 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Fri, 7 Jul 2023 23:35:17 +0800 Subject: [PATCH] Update README.md (#54) --- README.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index fd8e125..fb77e57 100644 --- a/README.md +++ b/README.md @@ -8,33 +8,98 @@ The go p2p library implementation of the CESS distributed storage network, which is customized based on [go-libp2p](https://github.com/libp2p/go-libp2p), has many advantages, and at the same time abandons the bandwidth and space redundancy caused by multiple file backups, making nodes more focused on storage allocation Give yourself data to make it more in line with the needs of the CESS network. -## Reporting a Vulnerability -If you find out any vulnerability, Please send an email to frode@cess.one, we are happy to communicate with you. +## 📝 Reporting a Vulnerability +If you find out any system bugs or you have a better suggestions, please send an email to frode@cess.one or join [CESS discord](https://discord.gg/mYHTMfBwNS) to communicate with us. -## Installation +## 🏗 Usage To get the package use the standard: ``` go get -u "github.com/CESSProject/p2p-go" ``` -Using Go modules is recommended. -## Documentation +## 📖 Documentation Please refer to https://pkg.go.dev/github.com/CESSProject/p2p-go -## Examples -Please refer to https://github.com/CESSProject/p2p-go/tree/main/examples +## 💡 Examples -## Usage - -The following is an example of creating an p2p host: +The following code demonstrates how to create a p2p node and perform node discovery: ``` -p2phost, err := p2pgo.New( - privatekeyPath, - p2pgo.ListenAddrStrings(addr, port), // regular tcp connections - p2pgo.Workspace(workspace), +package main + +import ( + "context" + "fmt" + "log" + "os" + "strconv" + "time" + + p2pgo "github.com/CESSProject/p2p-go" + "github.com/CESSProject/p2p-go/core" + "golang.org/x/time/rate" ) + +func main() { + var ok bool + var node = &core.Node{} + ctx := context.Background() + port, err := strconv.Atoi(os.Args[1]) + if err != nil { + fmt.Println("please enter os.Args[1] as port") + os.Exit(1) + } + + h1, err := p2pgo.New( + ctx, + p2pgo.PrivatekeyFile(".private1"), + p2pgo.ListenPort(port), + p2pgo.Workspace("."), + p2pgo.BootPeers([]string{ + "_dnsaddr.boot-kldr-testnet.cess.cloud", + }), + p2pgo.ProtocolPrefix("/kldr-testnet"), + ) + if err != nil { + panic(err) + } + defer h1.Close() + + node, ok = h1.(*core.Node) + if !ok { + panic(err) + } + + fmt.Println(node.Addrs(), node.ID()) + + node.RouteTableFindPeers(0) + + tick := time.NewTicker(time.Second * 30) + defer tick.Stop() + + var r = rate.Every(time.Second * 3) + var limit = rate.NewLimiter(r, 1) + + for { + select { + case peer, ok := <-node.GetDiscoveredPeers(): + if !ok { + break + } + if limit.Allow() { + tick.Reset(time.Second * 30) + } + if len(peer.Responses) == 0 { + break + } + for _, v := range peer.Responses { + log.Println("found: ", v.ID.Pretty(), v.Addrs) + } + case <-tick.C: + node.RouteTableFindPeers(0) + } + } +} ``` -Creating a p2p host requires you to configure some information, you can refer to `options.go`. ## License Licensed under [Apache 2.0](https://github.com/CESSProject/p2p-go/blob/main/LICENSE)