Browse Source

working, next step convert REST functions to tcp msg p2p connections

master
arnaucode 6 years ago
parent
commit
bfa7b29639
7 changed files with 75 additions and 25 deletions
  1. +0
    -3
      memory.md
  2. +39
    -0
      peer/blockchainlib/blockchain.go
  3. +21
    -0
      peer/blockchainlib/init.go
  4. +6
    -8
      peer/blockchainlib/messageHandler.go
  5. +7
    -12
      peer/main.go
  6. +1
    -1
      peer/restRoutes.go
  7. +1
    -1
      runTmuxTestPeers.sh

+ 0
- 3
memory.md

@ -1,4 +1 @@
- p2plib/messages.go / MessageHandler
put in a way that the messages handlers can be created from the main code (not from the package)
- subsitute the REST functions by the messages in the MessageHandler - subsitute the REST functions by the messages in the MessageHandler

+ 39
- 0
peer/blockchainlib/blockchain.go

@ -119,6 +119,45 @@ func (bc *Blockchain) ReconstructBlockchainFromBlock(urlAPI string, h string) {
} }
bc.Print() bc.Print()
} }
func (bc *Blockchain) ReconstructBlockchainFromBlockRESTversion(urlAPI string, h string) {
color.Yellow("reconstructing the blockchain from last block in memory")
var block Block
var err error
block, err = bc.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 = bc.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 = bc.AddBlock(block)
check(err)
}
}
bc.Print()
}
func (bc *Blockchain) Print() { func (bc *Blockchain) Print() {
color.Green("Printing Blockchain stored in memory") color.Green("Printing Blockchain stored in memory")

+ 21
- 0
peer/blockchainlib/init.go

@ -0,0 +1,21 @@
package blockchainlib
import (
p2plib "../p2plib"
)
func (bc *Blockchain) InitializeBlockchain(role, ip, port, restport, serverip, serverport string) p2plib.ThisPeer {
//read the stored blockchain
err := bc.ReadFromDisk()
check(err)
bc.Print()
//get blockchain msgHandlerCases
configuredMsgCases := bc.CreateMsgHandlerCases()
//initialize p2plib, adding the configuredMsgCases to the p2plib msgCases to handle
tp := p2plib.InitializePeer(role, ip, port, restport, serverip,
serverport, configuredMsgCases)
//return thisPeer (tp)
return tp
}

peer/messageHandler.go → peer/blockchainlib/messageHandler.go

@ -1,21 +1,19 @@
package main
package blockchainlib
import ( import (
"fmt" "fmt"
p2plib "./p2plib"
blockchainlib "./blockchainlib"
p2plib "../p2plib"
) )
func createMsgHandlerCases() map[string]func(p2plib.Peer, p2plib.Msg) {
func (bc *Blockchain) CreateMsgHandlerCases() map[string]func(p2plib.Peer, p2plib.Msg) {
configuredMsgCases := make(map[string]func(p2plib.Peer, p2plib.Msg)) configuredMsgCases := make(map[string]func(p2plib.Peer, p2plib.Msg))
configuredMsgCases["Block"] = func(peer p2plib.Peer, msg p2plib.Msg) { configuredMsgCases["Block"] = func(peer p2plib.Peer, msg p2plib.Msg) {
//TODO check if the block is signed by an autorized emitter //TODO check if the block is signed by an autorized emitter
//block = msg.Data converted to Block //block = msg.Data converted to Block
var block blockchainlib.Block
if !blockchain.BlockExists(block) {
blockchain.AddBlock(block)
var block Block
if !bc.BlockExists(block) {
bc.AddBlock(block)
p2plib.PropagateData(peer, "block in string format") p2plib.PropagateData(peer, "block in string format")
} }
} }

+ 7
- 12
peer/main.go

@ -25,25 +25,20 @@ func main() {
//read configuration file //read configuration file
readConfig("config.json") readConfig("config.json")
//read the stored blockchain
err := blockchain.ReadFromDisk()
check(err)
blockchain.Print()
//initialize p2plib
configuredMsgCases := createMsgHandlerCases()
tp = p2plib.InitializePeer(os.Args[1], "127.0.0.1",
os.Args[2], os.Args[3], config.ServerIP, config.ServerPort, configuredMsgCases)
//initialize blockchainlib
//InitializeBlockchain(role, ip, port, restport, serverip, serverport)
config.RESTPort = os.Args[3]
tp := blockchain.InitializeBlockchain(os.Args[1], "127.0.0.1",
os.Args[2], os.Args[3], config.ServerIP, config.ServerPort)
if tp.RunningPeer.Role == "client" { if tp.RunningPeer.Role == "client" {
color.Red("http://" + config.IP + ":" + config.ServerRESTPort) color.Red("http://" + config.IP + ":" + config.ServerRESTPort)
fmt.Println(blockchain.GenesisBlock) fmt.Println(blockchain.GenesisBlock)
blockchain.ReconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.GenesisBlock) blockchain.ReconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.GenesisBlock)
} }
color.Blue("initialized")
go runRestServer()
color.Blue("peer and blokcchain initialized")
go runRestServer() //TODO this will not be necessary, due the communications will go full over tcp connections
fmt.Println(tp.Running)
for tp.Running { for tp.Running {
time.Sleep(1000 * time.Millisecond) time.Sleep(1000 * time.Millisecond)
} }

+ 1
- 1
peer/restRoutes.go

@ -106,7 +106,7 @@ func PropagateBlock(b blockchainlib.Block) {
func GenesisBlock(w http.ResponseWriter, r *http.Request) { func GenesisBlock(w http.ResponseWriter, r *http.Request) {
var genesis blockchainlib.Block var genesis blockchainlib.Block
if len(blockchain.Blocks) >= 0 {
if len(blockchain.Blocks) > 0 {
genesis = blockchain.Blocks[0] genesis = blockchain.Blocks[0]
} }

+ 1
- 1
runTmuxTestPeers.sh

@ -7,7 +7,7 @@ tmux split-window -d -t 0 -h
tmux send-keys -t 0 'cd peer && go run *.go server 3001 3002' enter tmux send-keys -t 0 'cd peer && go run *.go server 3001 3002' enter
sleep 2 sleep 2
tmux send-keys -t 1 "curl -X POST http://127.0.0.1:3002/register -d '{\"address\": \"firstaddress\"}'" enter
#tmux send-keys -t 1 "curl -X POST http://127.0.0.1:3002/register -d '{\"address\": \"firstaddress\"}'" enter
sleep 1 sleep 1
tmux send-keys -t 1 'cd peer && go run *.go client 3003 3004' enter tmux send-keys -t 1 'cd peer && go run *.go client 3003 3004' enter
tmux send-keys -t 2 'cd peer && go run *.go client 3005 3006' enter tmux send-keys -t 2 'cd peer && go run *.go client 3005 3006' enter

Loading…
Cancel
Save