Skip to content

Commit

Permalink
Improve bootstrap and discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Nov 27, 2023
1 parent ffc21ad commit 06e733c
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 270 deletions.
51 changes: 26 additions & 25 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,44 @@ import (
"time"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma/key/set"
"go.deanishe.net/env"
)

const (
name = "go-ma-actor"

// The default entity to connect to.
GO_MA_ACTOR_KEYSET_VAR = "GO_MA_ACTOR_KEYSET"
GO_MA_ACTOR_ENTITY_VAR = "GO_MA_ACTOR_ENTITY"
GO_MA_ACTOR_DISCOVERY_TIMEOUT_VAR = "GO_MA_ACTOR_DISCOVERY_TIMEOUT"
GO_MA_ACTOR_LOW_WATERMARK_VAR = "GO_MA_ACTOR_LOW_WATERMARK"
GO_MA_ACTOR_HIGH_WATERMARK_VAR = "GO_MA_ACTOR_HIGH_WATERMARK"
GO_MA_ACTOR_CONNMGR_GRACE_VAR = "GO_MA_ACTOR_CONNMGR_GRACE"

defaultDiscoveryTimeout int = 300
defaultLowWaterMark int = 2
defaultHighWaterMark int = 10
defaultConnMgrGrace time.Duration = time.Minute * 1
GO_MA_ACTOR_KEYSET_VAR = "GO_MA_ACTOR_KEYSET"
GO_MA_ACTOR_ENTITY_VAR = "GO_MA_ACTOR_ENTITY"
GO_MA_ACTOR_DISCOVERY_TIMEOUT_VAR = "GO_MA_ACTOR_DISCOVERY_TIMEOUT"
GO_MA_ACTOR_DISCOVERY_RETRY_INTERVAL_VAR = "GO_MA_ACTOR_DISCOVERY_RETRY"
GO_MA_ACTOR_DESIRED_PEERS_VAR = "GO_MA_ACTOR_DESIRED_PEERS"
GO_MA_ACTOR_LOW_WATERMARK_VAR = "GO_MA_ACTOR_LOW_WATERMARK"
GO_MA_ACTOR_HIGH_WATERMARK_VAR = "GO_MA_ACTOR_HIGH_WATERMARK"
GO_MA_ACTOR_CONNMGR_GRACE_VAR = "GO_MA_ACTOR_CONNMGR_GRACE"
GO_MA_ACTOR_LOGLEVEL_VAR = "GO_MA_ACTOR_LOGLEVEL"
GO_MA_ACTOR_LOGFILE_VAR = "GO_MA_ACTOR_LOGFILE"

defaultLowWaterMark int = 3 // This will be used for the connection manager and the number of peers to search for
defaultHighWaterMark int = 10
defaultDesiredPeers int = 3

defaultDiscoveryTimeout time.Duration = time.Second * 30
defaultConnMgrGrace time.Duration = time.Minute * 1
defaultDiscoveryRetryInterval time.Duration = time.Second * 1
)

var (
generate *bool
genenv *bool
publish *bool
forcePublish *bool

keyset *set.Keyset

discoveryTimeout int = env.GetInt(GO_MA_ACTOR_DISCOVERY_TIMEOUT_VAR, defaultDiscoveryTimeout)
lowWaterMark int = env.GetInt(GO_MA_ACTOR_LOW_WATERMARK_VAR, defaultLowWaterMark)
highWaterMark int = env.GetInt(GO_MA_ACTOR_HIGH_WATERMARK_VAR, defaultHighWaterMark)
connmgrGracePeriod time.Duration = env.GetDuration(GO_MA_ACTOR_CONNMGR_GRACE_VAR, defaultConnMgrGrace)

logLevel string = env.Get(ma.LOGLEVEL_VAR, "info")
logfile string = env.Get(ma.LOGFILE_VAR, name+".log")

desiredPeers int = env.GetInt(GO_MA_ACTOR_DESIRED_PEERS_VAR, defaultDesiredPeers)
highWaterMark int = env.GetInt(GO_MA_ACTOR_HIGH_WATERMARK_VAR, defaultHighWaterMark)
lowWaterMark int = env.GetInt(GO_MA_ACTOR_LOW_WATERMARK_VAR, defaultLowWaterMark)

connmgrGracePeriod time.Duration = env.GetDuration(GO_MA_ACTOR_CONNMGR_GRACE_VAR, defaultConnMgrGrace)
discoveryTimeout time.Duration = env.GetDuration(GO_MA_ACTOR_DISCOVERY_TIMEOUT_VAR, defaultDiscoveryTimeout)
discoveryRetryInterval time.Duration = env.GetDuration(GO_MA_ACTOR_DISCOVERY_RETRY_INTERVAL_VAR, defaultDiscoveryRetryInterval)

// What we want to communicate with initially
entity string = env.Get(GO_MA_ACTOR_ENTITY_VAR, "")

Expand Down
60 changes: 47 additions & 13 deletions config/init.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
package config

import (
"context"
"flag"
"os"
"time"

"github.com/bahner/go-ma/key/set"
log "github.com/sirupsen/logrus"
)

func init() {
var (
name = "go-ma-actor"

// Flags - user configurations
flag.StringVar(&logLevel, "loglevel", logLevel, "Loglevel to use for application")
flag.StringVar(&logfile, "logfile", logfile, "Logfile to use for application")
keyset *set.Keyset

// P"P Settings
flag.IntVar(&discoveryTimeout, "discoveryTimeout", discoveryTimeout, "Timeout for peer discovery")
flag.IntVar(&lowWaterMark, "lowWaterMark", lowWaterMark, "Low watermark for peer discovery")
flag.IntVar(&highWaterMark, "highWaterMark", highWaterMark, "High watermark for peer discovery")
flag.DurationVar(&connmgrGracePeriod, "connmgrGracePeriod", connmgrGracePeriod, "Grace period for connection manager")
generate *bool
genenv *bool
publish *bool
forcePublish *bool
)

// Actor
flag.StringVar(&nick, "nick", nick, "Nickname to use in character creation")
flag.StringVar(&keyset_string, "keyset", keyset_string, "Base58 encoded *secret* keyset used to identify the client. You.")
flag.StringVar(&entity, "entity", entity, "DID of the entity to communicate with.")
func init() {

// Booleans with control flow
generate = flag.Bool("generate", false, "Generates one-time keyset and uses it")
genenv = flag.Bool("genenv", false, "Generates a keyset and prints it to stdout and uses it")
publish = flag.Bool("publish", false, "Publishes keyset to IPFS when using genenv or generate")
forcePublish = flag.Bool("forcePublish", false, "Like -publish, force publication even if keyset is already published. This is probably the one you want.")

// Entities
flag.StringVar(&nick, "nick", nick, "Nickname to use in character creation")
flag.StringVar(&keyset_string, "keyset", keyset_string, "Base58 encoded *secret* keyset used to identify the client. You. You can use environment variable "+GO_MA_ACTOR_KEYSET_VAR+" to set this.")
flag.StringVar(&entity, "entity", entity, "DID of the entity to communicate with. You can use environment variable "+GO_MA_ACTOR_ENTITY_VAR+" to set this.")

// P2P Settings
flag.IntVar(&lowWaterMark, "lowWaterMark", lowWaterMark, "Low watermark for peer discovery. You can use environment variable "+GO_MA_ACTOR_LOW_WATERMARK_VAR+" to set this.")
flag.IntVar(&highWaterMark, "highWaterMark", highWaterMark, "High watermark for peer discovery. You can use environment variable "+GO_MA_ACTOR_HIGH_WATERMARK_VAR+" to set this.")
flag.IntVar(&desiredPeers, "desiredPeers", desiredPeers, "Desired number of peers to connect to. You can use environment variable "+GO_MA_ACTOR_DESIRED_PEERS_VAR+" to set this.")

flag.DurationVar(&connmgrGracePeriod, "connmgrGracePeriod", connmgrGracePeriod, "Grace period for connection manager. You can use environment variable "+GO_MA_ACTOR_CONNMGR_GRACE_VAR+" to set this.")
flag.DurationVar(&discoveryRetryInterval, "discoveryRetryInterval", discoveryRetryInterval, "Retry interval for peer discovery. You can use environment variable "+GO_MA_ACTOR_DISCOVERY_RETRY_INTERVAL_VAR+" to set this.")
flag.DurationVar(&discoveryTimeout, "discoveryTimeout", discoveryTimeout, "Timeout for peer discovery. You can use environment variable "+GO_MA_ACTOR_DISCOVERY_TIMEOUT_VAR+" to set this.")

// Logging
flag.StringVar(&logLevel, "loglevel", logLevel, "Loglevel to use for application. You can use environment variable "+GO_MA_ACTOR_LOGLEVEL_VAR+" to set this.")
flag.StringVar(&logfile, "logfile", logfile, "Logfile to use for application. You can use environment variable "+GO_MA_ACTOR_LOGFILE_VAR+" to set this.")

flag.Parse()

// Init logger
Expand Down Expand Up @@ -75,6 +91,7 @@ func GetForcePublish() bool {
return *forcePublish
}

// P2P Settings
func GetDiscoveryTimeout() time.Duration {
return time.Duration(discoveryTimeout) * time.Second
}
Expand All @@ -90,3 +107,20 @@ func GetHighWaterMark() int {
func GetConnMgrGracePeriod() time.Duration {
return connmgrGracePeriod
}

func GetDiscoveryContext() (context.Context, func()) {

ctx := context.Background()

discoveryCtx, cancel := context.WithTimeout(ctx, GetDiscoveryTimeout())

return discoveryCtx, cancel
}

func GetDiscoveryRetryInterval() time.Duration {
return discoveryRetryInterval
}

func GetDesiredPeers() int {
return desiredPeers
}
5 changes: 5 additions & 0 deletions config/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/bahner/go-ma/did/doc"
"github.com/bahner/go-ma/key/ipns"
"github.com/bahner/go-ma/key/set"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -108,3 +109,7 @@ func publishIdentity(k *set.Keyset) {
func GetKeyset() *set.Keyset {
return keyset
}

func GetIPNSKey() *ipns.Key {
return keyset.IPNSKey
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5 h1:Olf3MlIvYYfgdDQnofqcKSQOdO7hcGleD68ofrptxsA=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/bahner/go-ma v0.0.6-0.20231126211854-c1323073bd64 h1:dBGw1zZm46xJWUnVDT9BAbL8GUu8AwjWbP5Ddc7yRS4=
github.com/bahner/go-ma v0.0.6-0.20231126211854-c1323073bd64/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
31 changes: 9 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
package main

import (
"context"
"os"

"github.com/bahner/go-ma-actor/actor"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
"github.com/libp2p/go-libp2p/core/host"

log "github.com/sirupsen/logrus"
)

var (
ctx context.Context
err error

e string
n host.Host
)

func init() {
func main() {

ctx = context.Background()
p, err := p2p.Init(nil)
if err != nil {
log.Errorf("failed to initialize p2p: %v", err)
os.Exit(75)
}

// Try and run this as a goroutine. Not sure if it will work.
n, _, err = p2p.Init(
ctx,
config.GetKeyset().IPNSKey,
config.GetDiscoveryTimeout())
p.DHT.DiscoverPeers()

if err != nil {
log.Errorf("failed to initialize p2p: %v", err)
os.Exit(75)
}

}

func main() {
a, err := actor.NewFromKeyset(config.GetKeyset(), config.GetForcePublish())
if err != nil || a == nil {
log.Errorf("failed to create actor: %v", err)
os.Exit(70)
}

e = config.GetEntity()
e := config.GetEntity()
// Draw the UI.
log.Debugf("Starting text UI")
ui := ui.NewChatUI(ctx, n, a, e)
ui := ui.NewChatUI(p, a, e)
if err := ui.Run(); err != nil {
log.Errorf("error running text UI: %s", err)
}
Expand Down
Loading

0 comments on commit 06e733c

Please sign in to comment.