mirror of
https://github.com/arnaucube/blockchainIDsystem.git
synced 2026-02-07 02:56:43 +01:00
added GetBlockchain in REST API of the serverCA, also added the visualization to the serverCA webapp
This commit is contained in:
@@ -81,6 +81,11 @@ func (bc *Blockchain) addBlock(block Block) error {
|
|||||||
func reconstructBlockchainFromBlock(urlAPI string, h string) {
|
func reconstructBlockchainFromBlock(urlAPI string, h string) {
|
||||||
color.Yellow("reconstructing the blockchain from last block in memory")
|
color.Yellow("reconstructing the blockchain from last block in memory")
|
||||||
var block Block
|
var block Block
|
||||||
|
var err error
|
||||||
|
|
||||||
|
block, err = blockchain.getBlockByHash(h)
|
||||||
|
check(err)
|
||||||
|
|
||||||
if h == "" {
|
if h == "" {
|
||||||
//no genesis block yet
|
//no genesis block yet
|
||||||
color.Green(urlAPI + "/blocks/genesis")
|
color.Green(urlAPI + "/blocks/genesis")
|
||||||
@@ -97,16 +102,18 @@ func reconstructBlockchainFromBlock(urlAPI string, h string) {
|
|||||||
block.NextHash = h
|
block.NextHash = h
|
||||||
}
|
}
|
||||||
|
|
||||||
for block.NextHash != "" {
|
for block.NextHash != "" && block.Hash != "" {
|
||||||
res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
|
res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
|
||||||
check(err)
|
check(err)
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
check(err)
|
check(err)
|
||||||
err = json.Unmarshal(body, &block)
|
err = json.Unmarshal(body, &block)
|
||||||
check(err)
|
check(err)
|
||||||
color.Yellow("[New Block]: " + block.Hash)
|
if block.Hash != "" {
|
||||||
err = blockchain.addBlock(block)
|
color.Yellow("[New Block]: " + block.Hash)
|
||||||
check(err)
|
err = blockchain.addBlock(block)
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
blockchain.print()
|
blockchain.print()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,3 +27,10 @@ sleep 2
|
|||||||
|
|
||||||
echo "peer 3"
|
echo "peer 3"
|
||||||
xterm -hold -e './peer client 3007 3008' &
|
xterm -hold -e './peer client 3007 3008' &
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
echo "running serverCA"
|
||||||
|
cd ..
|
||||||
|
cd serverCA
|
||||||
|
xterm -hold -e 'go run *.go' &
|
||||||
|
|||||||
115
serverCA/blockchain.go
Normal file
115
serverCA/blockchain.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
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) 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 blockchain.blockExists(block) {
|
||||||
|
return errors.New("[Error adding Block]: Block already exists in the Blockchain")
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func reconstructBlockchainFromBlock(urlAPI string, h string) {
|
||||||
|
color.Yellow("reconstructing the blockchain from last block in memory")
|
||||||
|
var block Block
|
||||||
|
var err error
|
||||||
|
|
||||||
|
block, err = blockchain.getBlockByHash(h)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
if h == "" {
|
||||||
|
//no genesis block yet
|
||||||
|
color.Green(urlAPI + "/blocks/genesis")
|
||||||
|
res, err := http.Get(urlAPI + "/blocks/genesis")
|
||||||
|
check(err)
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
check(err)
|
||||||
|
err = json.Unmarshal(body, &block)
|
||||||
|
check(err)
|
||||||
|
color.Yellow("[New Block]: " + block.Hash)
|
||||||
|
err = blockchain.addBlock(block)
|
||||||
|
check(err)
|
||||||
|
} else {
|
||||||
|
block.NextHash = h
|
||||||
|
}
|
||||||
|
|
||||||
|
for block.NextHash != "" && block.Hash != "" {
|
||||||
|
res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
|
||||||
|
check(err)
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
check(err)
|
||||||
|
err = json.Unmarshal(body, &block)
|
||||||
|
check(err)
|
||||||
|
if block.Hash != "" {
|
||||||
|
color.Yellow("[New Block]: " + block.Hash)
|
||||||
|
err = blockchain.addBlock(block)
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
blockchain.print()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bc *Blockchain) print() {
|
||||||
|
color.Green("Printing Blockchain stored in memory")
|
||||||
|
color.Green("Genesis Block: " + bc.GenesisBlock)
|
||||||
|
for _, b := range bc.Blocks {
|
||||||
|
color.Green("Block height:")
|
||||||
|
fmt.Println(b.Height)
|
||||||
|
color.Green("Hash: " + b.Hash)
|
||||||
|
color.Green("Date: " + b.Date.String())
|
||||||
|
color.Green("---")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,8 @@ func main() {
|
|||||||
//read configuration file
|
//read configuration file
|
||||||
readConfig("config.json")
|
readConfig("config.json")
|
||||||
|
|
||||||
|
reconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, "")
|
||||||
|
|
||||||
//run thw webserver
|
//run thw webserver
|
||||||
go webserver()
|
go webserver()
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ var routes = Routes{
|
|||||||
"/peers",
|
"/peers",
|
||||||
GetPeers,
|
GetPeers,
|
||||||
},
|
},
|
||||||
|
Route{
|
||||||
|
"GetBlockchain",
|
||||||
|
"GET",
|
||||||
|
"/blockchain",
|
||||||
|
GetBlockchain,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
@@ -37,3 +43,13 @@ func GetPeers(w http.ResponseWriter, r *http.Request) {
|
|||||||
check(err)
|
check(err)
|
||||||
fmt.Fprintln(w, string(jResp))
|
fmt.Fprintln(w, string(jResp))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBlockchain(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Print("aaaaa: ")
|
||||||
|
fmt.Println(blockchain.Blocks[len(blockchain.Blocks)-1].Hash)
|
||||||
|
reconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.Blocks[len(blockchain.Blocks)-1].Hash)
|
||||||
|
|
||||||
|
jResp, err := json.Marshal(blockchain)
|
||||||
|
check(err)
|
||||||
|
fmt.Fprintln(w, string(jResp))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,16 +1,45 @@
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-3">
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
{{peerslist.PeerID}}
|
<div class="card-body">
|
||||||
<div class="card-body" style="max-height: 500px; overflow-y:scroll;">
|
<h4 class="card-title">
|
||||||
<div ng-repeat="peer in peerslist.peerslist">
|
Peers
|
||||||
<b>{{peer.id}}</b> - {{peer.role}}
|
</h4>
|
||||||
<br>
|
{{peerslist.PeerID}}
|
||||||
- {{peer.ip}}:{{peer.port}}, {{peer.restport}}
|
<div style="max-height: 500px; overflow-y:scroll;">
|
||||||
<hr>
|
<div ng-repeat="peer in peerslist.peerslist">
|
||||||
|
<b>{{peer.id}}</b> - {{peer.role}}
|
||||||
|
<br>
|
||||||
|
- {{peer.ip}}:{{peer.port}}, {{peer.restport}}
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
<h4 class="card-title">
|
||||||
|
Blockchain
|
||||||
|
</h4>
|
||||||
|
<div style="max-height: 500px; overflow-y:scroll;">
|
||||||
|
<div ng-repeat="block in blockchain.blocks">
|
||||||
|
Hash: <b>{{block.hash}}</b> - Height: {{block.height}}
|
||||||
|
<br>
|
||||||
|
Emitter: {{block.emitter}}
|
||||||
|
<br>
|
||||||
|
Date: {{block.date}}
|
||||||
|
<br>
|
||||||
|
Data: {{block.data}}
|
||||||
|
<br>
|
||||||
|
PreviousHash: {{block.previoushash}}
|
||||||
|
<br>
|
||||||
|
NextHash: {{block.nexthash}}
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,4 +19,14 @@ angular.module('app.main', ['ngRoute'])
|
|||||||
}, function(data, status, headers, config) {
|
}, function(data, status, headers, config) {
|
||||||
console.log('data error');
|
console.log('data error');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$scope.blockchain={};
|
||||||
|
$http.get(urlapi + 'blockchain')
|
||||||
|
.then(function(data, status, headers, config) {
|
||||||
|
console.log('data success');
|
||||||
|
console.log(data.data);
|
||||||
|
$scope.blockchain = data.data;
|
||||||
|
}, function(data, status, headers, config) {
|
||||||
|
console.log('data error');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user