From fa091ceda66166b95090132d42cad7a8ee5b545a Mon Sep 17 00:00:00 2001 From: garritfra Date: Sat, 22 Dec 2018 17:40:58 +0100 Subject: [PATCH] Add peers --- README.md | 8 ++++---- core/Blockchain.go | 11 +++++++++-- server/webserver.go | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6579b1e..43a7d89 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ The REST API currently exposes these Endpoints: Use the following endpoints to connect multiple nodes together -| Method | Route | Description | -| :----- | :-------- | :------------------------------------------------ | -| POST | /add_peer | Add a new peer address to the network | -| GET | /update | Compare chain with peers, and update if nessesary | \ No newline at end of file +| Method | Route | Description | +| :----- | :--------- | :------------------------------------------------ | +| POST | /add_peers | Add new peer addresses to the network | +| GET | /update | Compare chain with peers, and update if nessesary | \ No newline at end of file diff --git a/core/Blockchain.go b/core/Blockchain.go index 5b3dcc8..adc332e 100644 --- a/core/Blockchain.go +++ b/core/Blockchain.go @@ -10,6 +10,7 @@ import ( type Blockchain struct { Blocks []Block PendingTransactions []Transaction + Peers []string } // MineBlock adds a block to the chain @@ -38,7 +39,7 @@ func (bc *Blockchain) MineBlock() Block { func NewBlockchain() Blockchain { log.Print("Creating Blockchain...") - blockchain := Blockchain{Blocks: make([]Block, 0), PendingTransactions: make([]Transaction, 0)} + blockchain := Blockchain{Blocks: make([]Block, 0), PendingTransactions: make([]Transaction, 0), Peers: make([]string, 0)} // Mine Genesis Block blockchain.MineBlock() @@ -62,6 +63,11 @@ func (bc *Blockchain) GetLastHash() string { return bc.Blocks[len(bc.Blocks)-1].Hash() } +// AddPeer appends the IP address to the list of peers known to the chain +func (bc *Blockchain) AddPeer(peer string) { + bc.Peers = append(bc.Peers, peer) +} + // IsValid checks, if the chain has any faulty blocks func (bc *Blockchain) IsValid() bool { for i := 1; i < len(bc.Blocks); i++ { @@ -78,11 +84,12 @@ type JSONBlockchain struct { Blocks []JSONBlock Blockcount int PendingTransactions []Transaction + Peers []string } // AsJSON returns the Blockchain as a JSON Blockchain func (bc *Blockchain) AsJSON() JSONBlockchain { - jsonChain := JSONBlockchain{PendingTransactions: bc.PendingTransactions} + jsonChain := JSONBlockchain{PendingTransactions: bc.PendingTransactions, Peers: bc.Peers} for _, block := range bc.Blocks { jsonChain.Blocks = append(jsonChain.Blocks, block.AsJSON()) diff --git a/server/webserver.go b/server/webserver.go index dda7d78..a9de49a 100644 --- a/server/webserver.go +++ b/server/webserver.go @@ -33,6 +33,8 @@ func registerRouteHandlers() { http.HandleFunc("/pending_transactions", handleListPendingTransactions) http.HandleFunc("/add_transaction", handleAddTransaction) http.HandleFunc("/is_valid", handleIsValid) + + http.HandleFunc("/add_peers", handleAddPeers) } func handleError(err error, w http.ResponseWriter, r *http.Request) { @@ -70,3 +72,21 @@ func handleIsValid(w http.ResponseWriter, r *http.Request) { valid := blockchain.IsValid() json.NewEncoder(w).Encode(valid) } + +// Takes an a string-slice, and adds it to the known peers +func handleAddPeers(w http.ResponseWriter, r *http.Request) { + decoder := json.NewDecoder(r.Body) + var receivedPeers []string + err := decoder.Decode(&receivedPeers) + + if err == nil { + for _, peer := range receivedPeers { + blockchain.AddPeer(peer) + } + } else { + handleError(err, w, r) + } + + json.NewEncoder(w).Encode(blockchain.Peers) + +}