Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node and Peer management #267

Merged
merged 4 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 15 additions & 87 deletions cmd/strawberry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,116 +6,44 @@ import (
"flag"
"fmt"
"log"
"time"
"net"

"github.com/eigerco/strawberry/pkg/network/cert"
"github.com/eigerco/strawberry/pkg/network/handlers"
"github.com/eigerco/strawberry/pkg/network/peer"
"github.com/eigerco/strawberry/pkg/network/protocol"
"github.com/eigerco/strawberry/pkg/network/transport"
)

// main starts a blockchain node.
//
// To run the first node (listener):
//
// go run main.go -addr localhost:9000
//
// To run a second node that connects to the first node:
//
// go run main.go -addr localhost:9001 -connect localhost:9000
//
// - The first node listens on port 9000.
// - The second node listens on port 9001 and connects to the first node's address (localhost:9000).
// go run main.go -addr localhost:9000
func main() {
listenAddr := flag.String("addr", "", "Listen address (e.g., 0.0.0.0:9000)")
connectTo := flag.String("connect", "", "Address to connect to (optional)")
ctx := context.Background()
listenAddr := flag.String("addr", "", "Listen address")
flag.Parse()

if *listenAddr == "" {
log.Fatal("listen address is required")
}

// Generate node keys
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
log.Fatalf("Failed to generate keys: %v", err)
}

// Create certificate
certGen := cert.NewGenerator(cert.Config{
PublicKey: pub,
PrivateKey: priv,
CertValidityPeriod: 24 * time.Hour,
})
tlsCert, err := certGen.GenerateCertificate()
if err != nil {
log.Fatalf("Failed to generate certificate: %v", err)
keys := peer.ValidatorKeys{
EdPrv: priv,
EdPub: pub,
}

// Create protocol manager
protoConfig := protocol.Config{
ChainHash: "12345678", // Example chain hash
IsBuilder: false,
MaxBuilderSlots: 20,
}
protoManager, err := protocol.NewManager(protoConfig)
address, err := net.ResolveUDPAddr("", *listenAddr)
if err != nil {
log.Fatalf("Failed to create protocol manager: %v", err)
fmt.Printf("err: %v\n", err)
}

// Register protocol handlers
protoManager.Registry.RegisterHandler(protocol.StreamKindBlockRequest, handlers.NewBlockRequestHandler())

// Create transport with minimal config
transportConfig := transport.Config{
PublicKey: pub,
PrivateKey: priv,
TLSCert: tlsCert,
ListenAddr: *listenAddr,
CertValidator: cert.NewValidator(),
Handler: protoManager, // Protocol manager implements ConnectionHandler
}

tr, err := transport.NewTransport(transportConfig)
fmt.Printf("listening on: %v\n", address)
node, err := peer.NewNode(ctx, address, keys)
if err != nil {
log.Fatalf("Failed to create transport: %v", err)
log.Fatalf("Failed to create node: %v", err)
}

if err := tr.Start(); err != nil {
log.Fatalf("Failed to start transport: %v", err)
}
defer func() {
if err := tr.Stop(); err != nil {
fmt.Printf("Failed to stop transport: %v\n", err)
}
}()

log.Printf("Node listening on %s", *listenAddr)

// If we have an address to connect to, make a request
if *connectTo != "" {
log.Printf("Connecting to peer at %s", *connectTo)

conn, err := tr.Connect(*connectTo)
if err != nil {
log.Fatalf("Failed to connect to peer: %v", err)
}

// Create a dummy block hash for the request
hash := [32]byte{1, 2, 3, 4} // Example hash

// Create peer with protocol connection
p := peer.NewPeer(conn, protoManager)
ctx := context.Background()
blocks, err := p.RequestBlocks(ctx, hash, true)
if err != nil {
log.Fatalf("Failed to request blocks: %v", err)
}
fmt.Printf("blocks: %v\n", blocks)
log.Printf("Block request completed")
err = node.Start()
if err != nil {
fmt.Printf("err: %v\n", err)
}

// Keep the node running
select {}
}
Loading
Loading