Skip to content

Commit 1522bd8

Browse files
committed
Add --min_connections option
if minimum connections not satisfied, exit app
1 parent 9f2ace5 commit 1522bd8

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

cmd/phantom/main.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"log"
3737
"strconv"
3838
"sync"
39+
"os"
3940
"time"
4041

4142
"../../pkg/phantom"
@@ -45,6 +46,7 @@ import (
4546
"github.com/btcsuite/btcd/chaincfg/chainhash"
4647
)
4748

49+
var minConnections uint
4850
var maxConnections uint
4951

5052
var magicBytes uint32
@@ -63,13 +65,17 @@ var userAgent string
6365
var cachedPeers error
6466
var numberConnections int
6567

66-
const VERSION = "1.2.7"
68+
const VERSION = "1.2.8"
69+
70+
var StartTime time.Time
6771

6872
func main() {
6973

7074
//disable all logging
7175
//log.SetOutput(ioutil.Discard)
7276

77+
StartTime := time.Now()
78+
7379
var magicHex string
7480
var magicMsgNewLine bool
7581
var protocolNum uint
@@ -81,7 +87,8 @@ func main() {
8187

8288
flag.StringVar(&coinConfString, "coin_conf", "coinconf.json", "Name of the file to load the coin information from.")
8389
flag.StringVar(&masternodeConf, "masternode_conf", "masternodeconf.json", "Name of the file to load the masternode information from.")
84-
flag.UintVar(&maxConnections, "max_connections", 64, "the number of peers to maintain")
90+
flag.UintVar(&minConnections, "min_connections", 0, "the minimum acceptable number of peers to maintain. If not satified in 5 minutes after app starts, then exit (default = 0 = never exit)")
91+
flag.UintVar(&maxConnections, "max_connections", 64, "the maximum number of peers to maintain")
8592
flag.StringVar(&magicHex, "magicbytes", "", "a hex string for the magic bytes")
8693
flag.UintVar(&defaultPort, "port", 0, "the default port number")
8794
flag.UintVar(&protocolNum, "protocol_number", 0, "the protocol number to connect and ping with")
@@ -240,6 +247,9 @@ func main() {
240247
fmt.Println("Sentinel Version: ", sentinelVersion)
241248
fmt.Println("Daemon Version: ", daemonVersion)
242249
fmt.Println("Listen for broadcasts: ", broadcastListen)
250+
fmt.Println()
251+
fmt.Println("Minimum connections: ", minConnections)
252+
fmt.Println("Maximum connections: ", maxConnections)
243253
fmt.Println("\n\n")
244254

245255
db, err := storage.InitialiseDB(dbPath)
@@ -297,6 +307,11 @@ func main() {
297307
go generatePings(pingGeneratorChannel, hashQueue, magicMessage, broadcastSet)
298308

299309
waitGroup.Wait()
310+
311+
elapsed := time.Since(StartTime)
312+
fmt.Println("Started in %s", elapsed)
313+
314+
StartTime = time.Now()
300315
}
301316

302317
func generatePings(pingChannel chan phantom.MasternodePing, queue *phantom.Queue,
@@ -426,6 +441,15 @@ func sendPings(connectionSet map[string]*phantom.PingerConnection, peerSet map[s
426441

427442
log.Println("Current number of connections to network: (", len(connectionSet), "/", maxConnections, ")")
428443

444+
if numberConnections < int(minConnections) && time.Now().Sub(StartTime).Seconds() > 300 {
445+
var runningTime time.Duration = time.Now().Sub(StartTime)
446+
447+
log.Println("Minimum number of connections (", minConnections, ") not satisfied. application has been running for ", runningTime)
448+
log.Println("Closing Application now")
449+
450+
os.Exit(0)
451+
}
452+
429453
//spawn off extra nodes here if we don't have enough
430454
if len(connectionSet) < int(maxConnections) {
431455

pkg/phantom/coinconf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
type CoinConf struct {
1111
Name string `json:"name"`
1212
Magicbytes string `json:"magicbytes"`
13-
Port uint `json:"port"`
14-
ProtocolNumber uint `json:"protocol_number"`
13+
Port uint `json:"port"`
14+
ProtocolNumber uint `json:"protocol_number"`
1515
MagicMessage string `json:"magic_message"`
1616
MagicMessageNewline bool `json:"magic_message_newline,omitempty"`
1717
BootstrapURL string `json:"bootstrap_url,omitempty"`

0 commit comments

Comments
 (0)