Skip to content

Commit 69dbbe5

Browse files
committed
stable
1 parent af2c842 commit 69dbbe5

3 files changed

Lines changed: 45 additions & 20 deletions

File tree

cli/cmd/node.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"encoding/hex"
1010
"fmt"
1111
"math/big"
12+
"net/url"
1213
"os"
1314
"os/signal"
15+
"strings"
1416
"syscall"
1517
)
1618

@@ -60,8 +62,8 @@ func newBlockdag(dbPath string) (nakamoto.BlockDAG, nakamoto.ConsensusConfig, *s
6062
copy(genesisBlockHash[:], genesisBlockHash_)
6163

6264
conf := nakamoto.ConsensusConfig{
63-
EpochLengthBlocks: 5,
64-
TargetEpochLengthMillis: 2000,
65+
EpochLengthBlocks: 200,
66+
TargetEpochLengthMillis: 1000 * 60 * 5, // 5 minutes
6567
GenesisDifficulty: *genesis_difficulty,
6668
GenesisBlockHash: genesisBlockHash,
6769
MaxBlockSizeBytes: 2*1024*1024, // 2MB
@@ -78,6 +80,7 @@ func newBlockdag(dbPath string) (nakamoto.BlockDAG, nakamoto.ConsensusConfig, *s
7880
func RunNode(cmdCtx *cli.Context) (error) {
7981
port := cmdCtx.String("port")
8082
dbPath := cmdCtx.String("db")
83+
bootstrapPeers := cmdCtx.String("peers")
8184

8285
// DAG.
8386
dag, _, _ := newBlockdag(dbPath)
@@ -108,8 +111,23 @@ func RunNode(cmdCtx *cli.Context) (error) {
108111
os.Exit(1)
109112
}()
110113

111-
node.Start()
114+
// Bootstrap the node.
115+
if bootstrapPeers != "" {
116+
peerAddresses := []string{}
117+
// Split the comma-separated list of peer addresses.
118+
peerlist := strings.Split(bootstrapPeers, ",")
119+
for _, peerAddress := range peerlist {
120+
// Validate URL.
121+
_, err := url.ParseRequestURI(peerAddress)
122+
if err != nil {
123+
return fmt.Errorf("Invalid peer address: %s", peerAddress)
124+
}
125+
peerAddresses = append(peerAddresses, peerAddress)
126+
}
127+
128+
node.Peer.Bootstrap(peerAddresses)
129+
}
112130

113-
131+
node.Start()
114132
return nil
115133
}

cli/tinychaind.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4-
"github.com/urfave/cli/v2"
5-
"github.com/liamzebedee/tinychain-go/cli/cmd"
64
"log"
75
"os"
6+
7+
"github.com/liamzebedee/tinychain-go/cli/cmd"
8+
"github.com/urfave/cli/v2"
89
)
910

1011
func main() {
@@ -28,6 +29,11 @@ func main() {
2829
Usage: "The path to the tinychain database",
2930
Value: "tinychain.db",
3031
},
32+
&cli.StringFlag{
33+
Name: "peers",
34+
Usage: "A list of comma-separated peer URL's used to bootstrap connection to the network",
35+
Value: "",
36+
},
3137
},
3238
},
3339
},

core/nakamoto/netpeer.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package nakamoto
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
7+
"io/ioutil"
8+
"log"
59
"net/http"
6-
"encoding/json"
7-
"io/ioutil"
8-
"time"
9-
"log"
10-
"os"
11-
"bytes"
12-
"sort"
10+
"net/url"
11+
"os"
12+
"sort"
13+
"time"
1314
)
1415

1516
var peerLogger = log.New(os.Stdout, "peer: ", log.Lshortfile)
@@ -410,12 +411,12 @@ func (p *PeerCore) Bootstrap(peerInfos []string) {
410411
for i, peerInfo := range peerInfos {
411412
peerLogger.Printf("Connecting to bootstrap peer #%d at %s\n", i, peerInfo)
412413

413-
// Parse address and port from URI.
414-
// url, err := url.Parse(peerInfo)
415-
// if err != nil {
416-
// peerLogger.Println("Failed to parse peer address: ", err)
417-
// continue
418-
// }
414+
// Check URL valid.
415+
_, err := url.Parse(peerInfo)
416+
if err != nil {
417+
peerLogger.Println("Failed to parse peer address: ", err)
418+
continue
419+
}
419420

420421
peer := Peer{
421422
url: peerInfo,
@@ -442,7 +443,7 @@ func (p *PeerCore) Bootstrap(peerInfos []string) {
442443
}
443444

444445
// Send heartbeat message to peer.
445-
_, err := SendMessageToPeer(peer.url, heartbeatMsg)
446+
_, err = SendMessageToPeer(peer.url, heartbeatMsg)
446447
if err != nil {
447448
peerLogger.Printf("Failed to send heartbeat to peer: %v", err)
448449
continue

0 commit comments

Comments
 (0)