@ -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 |
@ -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 ( |
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") |
||||
} |
} |
||||
} |
} |