-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,463 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"context" | ||
"crypto/ed25519" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/eigerco/strawberry/internal/block" | ||
"github.com/eigerco/strawberry/internal/state" | ||
"github.com/eigerco/strawberry/internal/statetransition" | ||
"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). | ||
func main() { | ||
globalState := &state.State{} | ||
mu := &sync.RWMutex{} | ||
|
||
// a simple http server that demonstrates the block import capabilities | ||
// this will be replaced with proper p2p network communication in milestone 2 | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/block/import", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
newBlock := block.Block{} | ||
if err := json.NewDecoder(r.Body).Decode(&newBlock); err != nil { | ||
jsonError(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
listenAddr := flag.String("addr", "", "Listen address (e.g., 0.0.0.0:9000)") | ||
connectTo := flag.String("connect", "", "Address to connect to (optional)") | ||
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) | ||
} | ||
|
||
// Create protocol manager | ||
protoConfig := protocol.Config{ | ||
ChainHash: "12345678", // Example chain hash | ||
IsBuilder: false, | ||
MaxBuilderSlots: 20, | ||
} | ||
protoManager, err := protocol.NewManager(protoConfig) | ||
if err != nil { | ||
log.Fatalf("Failed to create protocol manager: %v", err) | ||
} | ||
|
||
mu.Lock() | ||
defer mu.Unlock() | ||
// Register protocol handlers | ||
protoManager.Registry.RegisterHandler(protocol.StreamKindBlockRequest, handlers.NewBlockRequestHandler()) | ||
|
||
if err := statetransition.UpdateState(globalState, newBlock); err != nil { | ||
jsonError(w, err.Error(), http.StatusBadRequest) | ||
return | ||
// 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) | ||
if err != nil { | ||
log.Fatalf("Failed to create transport: %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) | ||
|
||
if err := json.NewEncoder(w).Encode(map[string]string{"status": "success"}); err != nil { | ||
jsonError(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
conn, err := tr.Connect(*connectTo) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to peer: %v", err) | ||
} | ||
}) | ||
log.Println("demo server running on port :8080") | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
} | ||
|
||
func jsonError(w http.ResponseWriter, message string, statusCode int) { | ||
w.WriteHeader(statusCode) | ||
if err := json.NewEncoder(w).Encode(map[string]string{ | ||
"status": "error", | ||
"message": message, | ||
}); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
// 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, conn.PeerKey(), 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") | ||
} | ||
|
||
// Keep the node running | ||
select {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.