Skip to content

Commit

Permalink
Use internal port range 2000-3000 (resolves #56) (#57)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored Sep 22, 2020
1 parent 94d31ed commit 0ada9b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
3 changes: 3 additions & 0 deletions internal/app/microfabd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ func DefaultConfig() (*Config, error) {
return nil, err
}
}
if config.Port >= startPort && config.Port < endPort {
logger.Fatalf("Cannot specify port %d, must be outside port range %d-%d", config.Port, 2000, 3000)
}
return config, nil
}
53 changes: 36 additions & 17 deletions internal/app/microfabd/microfabd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (

var logger = log.New(os.Stdout, fmt.Sprintf("[%16s] ", "microfabd"), log.LstdFlags)

const startPort = 2000
const endPort = 3000

// Microfab represents an instance of the Microfab application.
type Microfab struct {
sync.Mutex
Expand All @@ -52,6 +55,7 @@ type Microfab struct {
genesisBlocks map[string]*common.Block
console *console.Console
proxy *proxy.Proxy
currentPort int
}

// New creates an instance of the Microfab application.
Expand All @@ -61,10 +65,11 @@ func New() (*Microfab, error) {
return nil, err
}
return &Microfab{
config: config,
sigs: make(chan os.Signal, 1),
done: make(chan struct{}, 1),
started: false,
config: config,
sigs: make(chan os.Signal, 1),
done: make(chan struct{}, 1),
started: false,
currentPort: startPort,
}, nil
}

Expand Down Expand Up @@ -122,25 +127,27 @@ func (m *Microfab) Start() error {

// Create and start all of the components (orderer, peers, CAs).
eg.Go(func() error {
return m.createAndStartOrderer(m.ordererOrganization, 7050, 7051)
apiPort := m.allocatePort()
operationsPort := m.allocatePort()
return m.createAndStartOrderer(m.ordererOrganization, apiPort, operationsPort)
})
for i := range m.endorsingOrganizations {
organization := m.endorsingOrganizations[i]
numPorts := 6
peerAPIPort := 7052 + (i * numPorts)
peerChaincodePort := 7053 + (i * numPorts)
peerOperationsPort := 7054 + (i * numPorts)
couchDBProxyPort := 7055 + (i * numPorts)
caAPIPort := 7056 + (i * numPorts)
caOperationsPort := 7057 + (i * numPorts)
if m.config.CouchDB {
go m.createAndStartCouchDBProxy(organization, couchDBProxyPort)
}
eg.Go(func() error {
return m.createAndStartPeer(organization, peerAPIPort, peerChaincodePort, peerOperationsPort, m.config.CouchDB, couchDBProxyPort)
peerAPIPort := m.allocatePort()
peerChaincodePort := m.allocatePort()
peerOperationsPort := m.allocatePort()
if m.config.CouchDB {
couchDBProxyPort := m.allocatePort()
go m.createAndStartCouchDBProxy(organization, couchDBProxyPort)
return m.createAndStartPeer(organization, peerAPIPort, peerChaincodePort, peerOperationsPort, m.config.CouchDB, couchDBProxyPort)
}
return m.createAndStartPeer(organization, peerAPIPort, peerChaincodePort, peerOperationsPort, false, 0)
})
if m.config.CertificateAuthorities {
eg.Go(func() error {
caAPIPort := m.allocatePort()
caOperationsPort := m.allocatePort()
return m.createAndStartCA(organization, caAPIPort, caOperationsPort)
})
}
Expand All @@ -156,7 +163,8 @@ func (m *Microfab) Start() error {
})

// Create and start the console.
console, err := console.New(m.organizations, m.orderer, m.peers, m.cas, 8081, fmt.Sprintf("http://console.%s:%d", m.config.Domain, m.config.Port))
consolePort := m.allocatePort()
console, err := console.New(m.organizations, m.orderer, m.peers, m.cas, consolePort, fmt.Sprintf("http://console.%s:%d", m.config.Domain, m.config.Port))
if err != nil {
return err
}
Expand Down Expand Up @@ -237,6 +245,17 @@ func (m *Microfab) Wait() {
}
}

func (m *Microfab) allocatePort() int {
m.Lock()
defer m.Unlock()
if m.currentPort >= endPort {
logger.Fatalf("Failed to allocate port, port range %d-%d exceeded", startPort, endPort)
}
result := m.currentPort
m.currentPort++
return result
}

func (m *Microfab) ensureDirectory() error {
if m.directoryExists() {
err := m.removeDirectory()
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ type Console struct {
orderer *orderer.Orderer
peers []*peer.Peer
cas []*ca.CA
port int32
port int
url *url.URL
}

// New creates a new instance of a console.
func New(organizations []*organization.Organization, orderer *orderer.Orderer, peers []*peer.Peer, cas []*ca.CA, port int32, curl string) (*Console, error) {
func New(organizations []*organization.Organization, orderer *orderer.Orderer, peers []*peer.Peer, cas []*ca.CA, port int, curl string) (*Console, error) {
staticComponents := components{}
for _, organization := range organizations {
orgHide := organization == orderer.Organization()
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *Console) Stop() error {
}

// Port returns the port of the console.
func (c *Console) Port() int32 {
func (c *Console) Port() int {
return c.port
}

Expand Down

0 comments on commit 0ada9b0

Please sign in to comment.