Skip to content

Commit

Permalink
stable
Browse files Browse the repository at this point in the history
  • Loading branch information
liamzebedee committed Jun 28, 2024
1 parent af2c842 commit 69dbbe5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
26 changes: 22 additions & 4 deletions cli/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"encoding/hex"
"fmt"
"math/big"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
)

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

conf := nakamoto.ConsensusConfig{
EpochLengthBlocks: 5,
TargetEpochLengthMillis: 2000,
EpochLengthBlocks: 200,
TargetEpochLengthMillis: 1000 * 60 * 5, // 5 minutes
GenesisDifficulty: *genesis_difficulty,
GenesisBlockHash: genesisBlockHash,
MaxBlockSizeBytes: 2*1024*1024, // 2MB
Expand All @@ -78,6 +80,7 @@ func newBlockdag(dbPath string) (nakamoto.BlockDAG, nakamoto.ConsensusConfig, *s
func RunNode(cmdCtx *cli.Context) (error) {
port := cmdCtx.String("port")
dbPath := cmdCtx.String("db")
bootstrapPeers := cmdCtx.String("peers")

// DAG.
dag, _, _ := newBlockdag(dbPath)
Expand Down Expand Up @@ -108,8 +111,23 @@ func RunNode(cmdCtx *cli.Context) (error) {
os.Exit(1)
}()

node.Start()
// Bootstrap the node.
if bootstrapPeers != "" {
peerAddresses := []string{}
// Split the comma-separated list of peer addresses.
peerlist := strings.Split(bootstrapPeers, ",")
for _, peerAddress := range peerlist {
// Validate URL.
_, err := url.ParseRequestURI(peerAddress)
if err != nil {
return fmt.Errorf("Invalid peer address: %s", peerAddress)
}
peerAddresses = append(peerAddresses, peerAddress)
}

node.Peer.Bootstrap(peerAddresses)
}


node.Start()
return nil
}
10 changes: 8 additions & 2 deletions cli/tinychaind.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/urfave/cli/v2"
"github.com/liamzebedee/tinychain-go/cli/cmd"
"log"
"os"

"github.com/liamzebedee/tinychain-go/cli/cmd"
"github.com/urfave/cli/v2"
)

func main() {
Expand All @@ -28,6 +29,11 @@ func main() {
Usage: "The path to the tinychain database",
Value: "tinychain.db",
},
&cli.StringFlag{
Name: "peers",
Usage: "A list of comma-separated peer URL's used to bootstrap connection to the network",
Value: "",
},
},
},
},
Expand Down
29 changes: 15 additions & 14 deletions core/nakamoto/netpeer.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nakamoto

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"encoding/json"
"io/ioutil"
"time"
"log"
"os"
"bytes"
"sort"
"net/url"
"os"
"sort"
"time"
)

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

// Parse address and port from URI.
// url, err := url.Parse(peerInfo)
// if err != nil {
// peerLogger.Println("Failed to parse peer address: ", err)
// continue
// }
// Check URL valid.
_, err := url.Parse(peerInfo)
if err != nil {
peerLogger.Println("Failed to parse peer address: ", err)
continue
}

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

// Send heartbeat message to peer.
_, err := SendMessageToPeer(peer.url, heartbeatMsg)
_, err = SendMessageToPeer(peer.url, heartbeatMsg)
if err != nil {
peerLogger.Printf("Failed to send heartbeat to peer: %v", err)
continue
Expand Down

0 comments on commit 69dbbe5

Please sign in to comment.