Skip to content

Commit

Permalink
Stcp implementation. (#549)
Browse files Browse the repository at this point in the history
Working implementation of stcp.
  • Loading branch information
志宇 authored Sep 5, 2019
1 parent d0eb961 commit e0b423b
Show file tree
Hide file tree
Showing 101 changed files with 13,136 additions and 1,070 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ install:
- go get -u github.com/FiloSottile/vendorcheck
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.17.1

before_script:
- ci_scripts/create-ip-aliases.sh

script:
- make check
- make lint
- make test
- make test-no-ci
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ DOCKER_NETWORK?=SKYNET
DOCKER_NODE?=SKY01
DOCKER_OPTS?=GO111MODULE=on GOOS=linux # go options for compiling for docker container
TEST_OPTS?=-race -tags no_ci -cover -timeout=5m
TEST_OPTS_NOCI?=-race -cover -timeout=5m -v
BUILD_OPTS?=-race

check: lint test ## Run linters and tests
Expand Down Expand Up @@ -64,6 +65,10 @@ test: ## Run tests
${OPTS} go test ${TEST_OPTS} ./internal/...
${OPTS} go test ${TEST_OPTS} ./pkg/...

test-no-ci: ## Run no_ci tests
-go clean -testcache
${OPTS} go test ${TEST_OPTS_NOCI} ./pkg/transport/... -run "TCP|PubKeyTable"

install-linters: ## Install linters
- VERSION=1.17.1 ./ci_scripts/install-golangci-lint.sh
# GO111MODULE=off go get -u github.com/FiloSottile/vendorcheck
Expand Down
13 changes: 13 additions & 0 deletions ci_scripts/create-ip-aliases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

if [[ "$OSTYPE" == "linux-gnu" ]]; then
for ((i=1; i<=255; i++))
do
sudo ip addr add 12.12.12.$i/32 dev lo
done
elif [[ "$OSTYPE" == "darwin" ]]; then
for ((i=1; i<=255; i++))
do
sudo ip addr add 12.12.12.$i/32 dev lo0
done
fi
1 change: 1 addition & 0 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (cfg *runCfg) readConfig() *runCfg {
if err := json.NewDecoder(rdr).Decode(&cfg.conf); err != nil {
cfg.logger.Fatalf("Failed to decode %s: %s", rdr, err)
}
fmt.Println("TCP Factory conf:", cfg.conf.TCPTransport)
return cfg
}

Expand Down
21 changes: 11 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ module github.com/skycoin/skywire
go 1.12

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/creack/pty v1.1.7
github.com/go-chi/chi v4.0.2+incompatible
github.com/google/uuid v1.1.1
github.com/gorilla/handlers v1.4.0
github.com/gorilla/handlers v1.4.2
github.com/gorilla/securecookie v1.1.1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/kr/pty v1.1.5 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/profile v1.3.0
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/common v0.4.1
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/common v0.6.0
github.com/sirupsen/logrus v1.4.2
github.com/skycoin/dmsg v0.0.0-20190904181013-b781e3cbebc6
github.com/skycoin/skycoin v0.26.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.4.0
go.etcd.io/bbolt v1.3.3
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
golang.org/x/sys v0.0.0-20190825160603-fb81701db80f // indirect
golang.org/x/tools v0.0.0-20190826060629-95c3470cfb70 // indirect
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
)

// Uncomment for tests with alternate branches of 'dmsg'
// replace github.com/skycoin/dmsg => ../dmsg
//replace github.com/skycoin/dmsg => ../dmsg
71 changes: 32 additions & 39 deletions go.sum

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions internal/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
package testhelpers

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

const timeout = 5 * time.Second
Expand All @@ -17,3 +20,10 @@ func WithinTimeout(ch <-chan error) error {
return nil
}
}

// NoErrorN performs require.NoError on multiple errors
func NoErrorN(t *testing.T, errs ...error) {
for _, err := range errs {
require.NoError(t, err)
}
}
63 changes: 55 additions & 8 deletions pkg/snet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"sync"

"github.com/skycoin/skywire/pkg/snet/stcp"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/dmsg"
Expand All @@ -26,6 +28,7 @@ const (
// Network types.
const (
DmsgType = "dmsg"
STcpType = "stcp"
)

var (
Expand All @@ -41,55 +44,84 @@ type Config struct {

DmsgDiscAddr string
DmsgMinSrvs int

STCPLocalAddr string // if empty, don't listen.
STCPTable map[cipher.PubKey]string
}

// Network represents a network between nodes in Skywire.
type Network struct {
conf Config
dmsgC *dmsg.Client
stcpC *stcp.Client
}

// New creates a network from a config.
func New(conf Config) *Network {
dmsgC := dmsg.NewClient(conf.PubKey, conf.SecKey, disc.NewHTTP(conf.DmsgDiscAddr), dmsg.SetLogger(logging.MustGetLogger("snet.dmsgC")))
return &Network{
conf: conf,
dmsgC: dmsgC,
}
dmsgC := dmsg.NewClient(
conf.PubKey,
conf.SecKey,
disc.NewHTTP(conf.DmsgDiscAddr),
dmsg.SetLogger(logging.MustGetLogger("snet.dmsgC")))

stcpC := stcp.NewClient(
logging.MustGetLogger("snet.stcpC"),
conf.PubKey,
conf.SecKey,
stcp.NewTable(conf.STCPTable))

return NewRaw(conf, dmsgC, stcpC)
}

// NewRaw creates a network from a config and a dmsg client.
func NewRaw(conf Config, dmsgC *dmsg.Client) *Network {
func NewRaw(conf Config, dmsgC *dmsg.Client, stcpC *stcp.Client) *Network {
return &Network{
conf: conf,
dmsgC: dmsgC,
stcpC: stcpC,
}
}

// Init initiates server connections.
func (n *Network) Init(ctx context.Context) error {
fmt.Println("dmsg: min_servers:", n.conf.DmsgMinSrvs)
if err := n.dmsgC.InitiateServerConnections(ctx, n.conf.DmsgMinSrvs); err != nil {
return fmt.Errorf("failed to initiate 'dmsg': %v", err)
}
if n.conf.STCPLocalAddr != "" {
if err := n.stcpC.Serve(n.conf.STCPLocalAddr); err != nil {
return fmt.Errorf("failed to initiate 'stcp': %v", err)
}
} else {
fmt.Println("No config found for stcp")
}
return nil
}

// Close closes underlying connections.
func (n *Network) Close() error {
wg := new(sync.WaitGroup)
wg.Add(1)
wg.Add(2)

var dmsgErr error
go func() {
dmsgErr = n.dmsgC.Close()
wg.Done()
}()

var stcpErr error
go func() {
stcpErr = n.stcpC.Close()
wg.Done()
}()

wg.Wait()

if dmsgErr != nil {
return dmsgErr
}
if stcpErr != nil {
return stcpErr
}
return nil
}

Expand All @@ -105,6 +137,9 @@ func (n *Network) TransportNetworks() []string { return n.conf.TpNetworks }
// Dmsg returns underlying dmsg client.
func (n *Network) Dmsg() *dmsg.Client { return n.dmsgC }

// STcp returns the underlying stcp.Client.
func (n *Network) STcp() *stcp.Client { return n.stcpC }

// Dial dials a node by its public key and returns a connection.
func (n *Network) Dial(network string, pk cipher.PubKey, port uint16) (*Conn, error) {
ctx := context.Background()
Expand All @@ -115,6 +150,12 @@ func (n *Network) Dial(network string, pk cipher.PubKey, port uint16) (*Conn, er
return nil, err
}
return makeConn(conn, network), nil
case STcpType:
conn, err := n.stcpC.Dial(ctx, pk, port)
if err != nil {
return nil, err
}
return makeConn(conn, network), nil
default:
return nil, ErrUnknownNetwork
}
Expand All @@ -129,6 +170,12 @@ func (n *Network) Listen(network string, port uint16) (*Listener, error) {
return nil, err
}
return makeListener(lis, network), nil
case STcpType:
lis, err := n.stcpC.Listen(port)
if err != nil {
return nil, err
}
return makeListener(lis, network), nil
default:
return nil, ErrUnknownNetwork
}
Expand Down
1 change: 1 addition & 0 deletions pkg/snet/snettest/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewEnv(t *testing.T, keys []KeyPair) *Env {
DmsgMinSrvs: 1,
},
dmsg.NewClient(pairs.PK, pairs.SK, dmsgD),
nil,
)
require.NoError(t, n.Init(context.TODO()))
ns[i] = n
Expand Down
Loading

0 comments on commit e0b423b

Please sign in to comment.