Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add listen-port and key-path #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions unison-poc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ go build -o ./unison ./unison-poc
```text
Usage of ./unison:
-batch-size int
Batch size to be produced every 'batch-time' (bytes). 0 disables batch production (default 250000)
Batch size to be produced every 'batch-time' (bytes). 0 disables batch production (default 250000)
-batch-time duration
Batch production time (default 1s)
Batch production time (default 1s)
-bootstrapper string
Specifies network bootstrapper multiaddr
Specifies network bootstrapper multiaddr
-is-bootstrapper
To indicate node is bootstrapper
To indicate node is bootstrapper
-key-path string
Path to the p2p private key (default "/.unison/key")
-kickoff-timeout duration
Timeout before starting block production (default 5s)
Timeout before starting block production (default 5s)
-listen-port int
Port to listen on for libp2p connections (default 10000)
-network-size int
Expected network size to wait for before starting the network. SKips if 0

Expected network size to wait for before starting the network. Skips if 0
```

6 changes: 5 additions & 1 deletion unison-poc/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (serv *Service) Start(ctx context.Context, bootstrapper peer.AddrInfo) erro
if err != nil {
return fmt.Errorf("connecting to bootstrapper: %w", err)
}
serv.log.DebugContext(ctx, "connected to bootstrapper")
serv.log.DebugContext(ctx, "connected to bootstrapper", "addr", bootstrapper.Addrs)

// this gives time for connections to settle on the bootstrapper and gets us all the peers
select {
Expand Down Expand Up @@ -81,6 +81,10 @@ func (serv *Service) Start(ctx context.Context, bootstrapper peer.AddrInfo) erro
if err != nil {
serv.log.Error("connecting to peer", "err", err)
}
slog.DebugContext(ctx, "connected to peer",
"peer", s.Conn().RemotePeer(),
"stream", s.ID(),
"protocol", s.Protocol())
}()
}

Expand Down
36 changes: 21 additions & 15 deletions unison-poc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/iykyk-syn/unison/rebro/gossip"
bootstrap2 "github.com/iykyk-syn/unison/unison-poc/bootstrap"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-pubsub"
pubsub "github.com/libp2p/go-libp2p-pubsub"
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
p2phost "github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
Expand All @@ -42,6 +42,8 @@ var (
batchSize int
batchTime time.Duration
networkSize int
listenPort int
keyPath string
)

func init() {
Expand All @@ -50,7 +52,9 @@ func init() {
flag.DurationVar(&kickoffTimeout, "kickoff-timeout", time.Second*5, "Timeout before starting block production")
flag.IntVar(&batchSize, "batch-size", 2000*125, "Batch size to be produced every 'batch-time' (bytes). 0 disables batch production")
flag.DurationVar(&batchTime, "batch-time", time.Second, "Batch production time")
flag.IntVar(&networkSize, "network-size", 0, "Expected network size to wait for before starting the network. SKips if 0")
flag.IntVar(&networkSize, "network-size", 0, "Expected network size to wait for before starting the network. Skips if 0")
flag.IntVar(&listenPort, "listen-port", 10000, "Port to listen on for libp2p connections")
flag.StringVar(&keyPath, "key-path", "/.unison/key", "Path to the p2p private key")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to specify the path to the unison folder only. We may want to add more persistent data to PoC besides the key and define every single persistence through the flag as not scalable. The key file name can be constant and defined close to the getIndentity function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to incorporate today or tomorrow 👍

flag.Parse()

slog.SetLogLoggerLevel(slog.LevelDebug)
Expand All @@ -68,7 +72,7 @@ func main() {
}

func run(ctx context.Context) error {
p2pKey, privKey, err := getIdentity()
p2pKey, privKey, err := getIdentity(keyPath)
if err != nil {
return err
}
Expand All @@ -79,8 +83,8 @@ func run(ctx context.Context) error {
}

listenAddrs := []string{
"/ip4/0.0.0.0/udp/10000/quic-v1",
"/ip6/::/udp/10000/quic-v1",
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic-v1", listenPort),
fmt.Sprintf("/ip6/::/udp/%d/quic-v1", listenPort),
}
listenMAddrs := make([]multiaddr.Multiaddr, 0, len(listenAddrs))
for _, s := range listenAddrs {
Expand Down Expand Up @@ -158,13 +162,13 @@ func run(ctx context.Context) error {
return ctx.Err()
}

memebers, err := bootstrap.GetMembers(0)
members, err := bootstrap.GetMembers(0)
if err != nil {
return err
}

dagger := dag.NewChain(broadcaster, mcastPool, func(round uint64) (*quorum.Includers, error) {
return memebers, nil
return members, nil
}, privKey.PubKey())
dagger.Start()
defer dagger.Stop()
Expand All @@ -179,24 +183,26 @@ func run(ctx context.Context) error {
return nil
}

const dir = "/.unison"

func getIdentity() (libp2pcrypto.PrivKey, crypto.PrivKey, error) {
func getIdentity(keyPath string) (libp2pcrypto.PrivKey, crypto.PrivKey, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}

dir := home + dir
if err = os.Mkdir(dir, os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
if keyPath == "" {
keyPath = "/.unison/key"
}
fullPath := home + keyPath

dir := home + "/.unison"
if err = os.MkdirAll(dir, os.ModePerm); err != nil && !errors.Is(err, os.ErrExist) {
return nil, nil, err
}

var keyBytes []byte
path := dir + "/key"
f, err := os.Open(path)
f, err := os.Open(fullPath)
if err != nil {
f, err = os.Create(path)
f, err = os.Create(fullPath)
if err != nil {
return nil, nil, err
}
Expand Down