mirror of
https://github.com/arnaucube/bc.git
synced 2026-02-06 18:46:42 +01:00
working, next step convert REST functions to tcp msg p2p connections
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -119,6 +119,45 @@ func (bc *Blockchain) ReconstructBlockchainFromBlock(urlAPI string, h string) {
|
||||
}
|
||||
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() {
|
||||
color.Green("Printing Blockchain stored in memory")
|
||||
|
||||
21
peer/blockchainlib/init.go
Normal file
21
peer/blockchainlib/init.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -1,21 +1,19 @@
|
||||
package main
|
||||
package blockchainlib
|
||||
|
||||
import (
|
||||
"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["Block"] = func(peer p2plib.Peer, msg p2plib.Msg) {
|
||||
//TODO check if the block is signed by an autorized emitter
|
||||
//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")
|
||||
}
|
||||
}
|
||||
19
peer/main.go
19
peer/main.go
@@ -25,25 +25,20 @@ func main() {
|
||||
//read configuration file
|
||||
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" {
|
||||
color.Red("http://" + config.IP + ":" + config.ServerRESTPort)
|
||||
fmt.Println(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 {
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ func PropagateBlock(b blockchainlib.Block) {
|
||||
|
||||
func GenesisBlock(w http.ResponseWriter, r *http.Request) {
|
||||
var genesis blockchainlib.Block
|
||||
if len(blockchain.Blocks) >= 0 {
|
||||
if len(blockchain.Blocks) > 0 {
|
||||
genesis = blockchain.Blocks[0]
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user