@ -0,0 +1 @@ |
|||||
|
tests |
@ -0,0 +1,69 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
type Block struct { |
||||
|
Hash string `json:"hash"` |
||||
|
Height int64 `json:"height"` |
||||
|
Date time.Time `json:"date"` |
||||
|
PreviousHash string `json:"previoushash"` |
||||
|
NextHash string `json:"nexthash"` |
||||
|
Data []Address `json:"data"` |
||||
|
Emitter string `json:"emitter"` //the ID of the peer that has emmited the block
|
||||
|
} |
||||
|
|
||||
|
type Blockchain struct { |
||||
|
GenesisBlock string `json:"genesisblock"` |
||||
|
LastUpdate time.Time `json:"lastupdate"` |
||||
|
Blocks []Block `json:"blocks"` |
||||
|
} |
||||
|
|
||||
|
var blockchain Blockchain |
||||
|
|
||||
|
func (bc *Blockchain) getBlockByHash(hash string) (Block, error) { |
||||
|
for _, block := range bc.Blocks { |
||||
|
if block.Hash == hash { |
||||
|
return block, nil |
||||
|
} |
||||
|
} |
||||
|
var b Block |
||||
|
return b, errors.New("Block Hash not found") |
||||
|
} |
||||
|
|
||||
|
func (bc *Blockchain) createBlock(address Address) Block { |
||||
|
var b Block |
||||
|
b.Height = int64(len(bc.Blocks)) |
||||
|
if len(bc.Blocks) == 0 { |
||||
|
b.Height = 0 |
||||
|
} else { |
||||
|
b.PreviousHash = bc.Blocks[len(bc.Blocks)-1].Hash |
||||
|
} |
||||
|
b.Date = time.Now() |
||||
|
b.Data = append(b.Data, address) |
||||
|
b.Emitter = runningPeer.ID |
||||
|
|
||||
|
b.Hash = hashBlock(b) |
||||
|
return b |
||||
|
} |
||||
|
|
||||
|
func (bc *Blockchain) blockExists(block Block) bool { |
||||
|
for _, b := range bc.Blocks { |
||||
|
if b.Hash == block.Hash { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
func (bc *Blockchain) addBlock(block Block) error { |
||||
|
if len(bc.Blocks) > 0 { |
||||
|
bc.Blocks[len(bc.Blocks)-1].NextHash = block.Hash |
||||
|
} else { |
||||
|
bc.GenesisBlock = block.Hash |
||||
|
} |
||||
|
bc.Blocks = append(bc.Blocks, block) |
||||
|
|
||||
|
return nil |
||||
|
} |
@ -0,0 +1,67 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"net/http" |
||||
|
|
||||
|
"github.com/fatih/color" |
||||
|
) |
||||
|
|
||||
|
type Routes []Route |
||||
|
|
||||
|
var routes = Routes{ |
||||
|
Route{ |
||||
|
"Index", |
||||
|
"GET", |
||||
|
"/", |
||||
|
Index, |
||||
|
}, |
||||
|
Route{ |
||||
|
"GetPeers", |
||||
|
"GET", |
||||
|
"/peers", |
||||
|
GetPeers, |
||||
|
}, |
||||
|
Route{ |
||||
|
"PostUser", |
||||
|
"POST", |
||||
|
"/register", |
||||
|
PostUser, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
type Address struct { |
||||
|
Address string `json:"address"` //the pubK of the user, to perform logins
|
||||
|
} |
||||
|
|
||||
|
func Index(w http.ResponseWriter, r *http.Request) { |
||||
|
fmt.Fprintln(w, runningPeer.ID) |
||||
|
} |
||||
|
func GetPeers(w http.ResponseWriter, r *http.Request) { |
||||
|
jResp, err := json.Marshal(outcomingPeersList) |
||||
|
check(err) |
||||
|
fmt.Fprintln(w, string(jResp)) |
||||
|
} |
||||
|
func PostUser(w http.ResponseWriter, r *http.Request) { |
||||
|
|
||||
|
decoder := json.NewDecoder(r.Body) |
||||
|
var address Address |
||||
|
err := decoder.Decode(&address) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
defer r.Body.Close() |
||||
|
fmt.Println(address) |
||||
|
color.Blue(address.Address) |
||||
|
|
||||
|
//TODO add the verification of the address, to decide if it's accepted to create a new Block
|
||||
|
block := blockchain.createBlock(address) |
||||
|
blockchain.addBlock(block) |
||||
|
|
||||
|
go propagateBlock(block) |
||||
|
|
||||
|
jResp, err := json.Marshal(blockchain) |
||||
|
check(err) |
||||
|
fmt.Fprintln(w, string(jResp)) |
||||
|
} |
@ -1,22 +0,0 @@ |
|||||
package main |
|
||||
|
|
||||
import ( |
|
||||
"fmt" |
|
||||
"net/http" |
|
||||
) |
|
||||
|
|
||||
type Routes []Route |
|
||||
|
|
||||
var routes = Routes{ |
|
||||
Route{ |
|
||||
"Index", |
|
||||
"GET", |
|
||||
"/", |
|
||||
Index, |
|
||||
}, |
|
||||
} |
|
||||
|
|
||||
func Index(w http.ResponseWriter, r *http.Request) { |
|
||||
//ipFilter(w, r)
|
|
||||
fmt.Fprintln(w, runningPeer.ID) |
|
||||
} |
|
@ -0,0 +1 @@ |
|||||
|
curl -X POST -F address="thisisasampleaddress" http://127.0.0.1:3002/register |