From 69dbbe5bb49df86c3d02f85325b030e0dff9632a Mon Sep 17 00:00:00 2001 From: liamzebedee Date: Fri, 28 Jun 2024 19:18:32 +1000 Subject: [PATCH] stable --- cli/cmd/node.go | 26 ++++++++++++++++++++++---- cli/tinychaind.go | 10 ++++++++-- core/nakamoto/netpeer.go | 29 +++++++++++++++-------------- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/cli/cmd/node.go b/cli/cmd/node.go index 2d85d83..feddcaf 100644 --- a/cli/cmd/node.go +++ b/cli/cmd/node.go @@ -9,8 +9,10 @@ import ( "encoding/hex" "fmt" "math/big" + "net/url" "os" "os/signal" + "strings" "syscall" ) @@ -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 @@ -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) @@ -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 } \ No newline at end of file diff --git a/cli/tinychaind.go b/cli/tinychaind.go index 76f206b..ebb9857 100644 --- a/cli/tinychaind.go +++ b/cli/tinychaind.go @@ -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() { @@ -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: "", + }, }, }, }, diff --git a/core/nakamoto/netpeer.go b/core/nakamoto/netpeer.go index 41d2959..26538c8 100644 --- a/core/nakamoto/netpeer.go +++ b/core/nakamoto/netpeer.go @@ -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) @@ -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, @@ -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