Browse Source

Add peers

now.sh
garritfra 5 years ago
parent
commit
fa091ceda6
  1. 8
      README.md
  2. 11
      core/Blockchain.go
  3. 20
      server/webserver.go

8
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 |
| Method | Route | Description |
| :----- | :--------- | :------------------------------------------------ |
| POST | /add_peers | Add new peer addresses to the network |
| GET | /update | Compare chain with peers, and update if nessesary |

11
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())

20
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)
}

Loading…
Cancel
Save