Browse Source

fixed p2p layer

master
arnaucode 6 years ago
parent
commit
b38582ad68
7 changed files with 102 additions and 40 deletions
  1. +2
    -0
      README.md
  2. +3
    -1
      peer/main.go
  3. +25
    -0
      peer/messageHandler.go
  4. +38
    -1
      peer/p2plib/init.go
  5. +13
    -38
      peer/p2plib/messages.go
  6. +15
    -0
      peer/p2plib/peers.go
  7. +6
    -0
      peer/p2plib/petitions.go

+ 2
- 0
README.md

@ -1,3 +1,5 @@
# bc # bc
Own p2p network and own blockchain libraries written in Go, to develop own decentralized apps. Own p2p network and own blockchain libraries written in Go, to develop own decentralized apps.
The purpose of this code is to learn to develop a p2p network and a blockchain system.

+ 3
- 1
peer/main.go

@ -30,8 +30,10 @@ func main() {
check(err) check(err)
blockchain.Print() blockchain.Print()
//initialize p2plib
configuredMsgCases := createMsgHandlerCases()
tp = p2plib.InitializePeer(os.Args[1], "127.0.0.1", tp = p2plib.InitializePeer(os.Args[1], "127.0.0.1",
os.Args[2], os.Args[3], config.ServerIP, config.ServerPort)
os.Args[2], os.Args[3], config.ServerIP, config.ServerPort, configuredMsgCases)
if tp.RunningPeer.Role == "client" { if tp.RunningPeer.Role == "client" {
color.Red("http://" + config.IP + ":" + config.ServerRESTPort) color.Red("http://" + config.IP + ":" + config.ServerRESTPort)

+ 25
- 0
peer/messageHandler.go

@ -0,0 +1,25 @@
package main
import (
"fmt"
p2plib "./p2plib"
blockchainlib "./blockchainlib"
)
func 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)
p2plib.PropagateData(peer, "block in string format")
}
}
fmt.Println(configuredMsgCases)
return configuredMsgCases
}

+ 38
- 1
peer/p2plib/init.go

@ -4,12 +4,17 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"time" "time"
"github.com/fatih/color"
) )
func InitializePeer(role, ip, port, restport, serverip, serverport string) ThisPeer {
func InitializePeer(role, ip, port, restport, serverip, serverport string,
configuredMsgCases map[string]func(Peer, Msg)) ThisPeer {
//initialize some vars //initialize some vars
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
InitializeDefaultMsgCases(configuredMsgCases)
var tp ThisPeer var tp ThisPeer
tp.Running = true tp.Running = true
tp.RunningPeer.Role = role tp.RunningPeer.Role = role
@ -41,3 +46,35 @@ func InitializePeer(role, ip, port, restport, serverip, serverport string) ThisP
return tp return tp
} }
func InitializeDefaultMsgCases(configuredMsgCases map[string]func(Peer, Msg)) {
//msgCases := make(map[string]func(Peer, Msg)) --> no, it's used the global
msgCases = make(map[string]func(Peer, Msg))
//get the user configured msgCases
for k, v := range configuredMsgCases {
msgCases[k] = v
}
msgCases["Hi"] = func(peer Peer, msg Msg) {
color.Yellow(msg.Type)
color.Green(msg.Content)
}
msgCases["PeersList"] = func(peer Peer, msg Msg) {
color.Blue("newPeerslist")
fmt.Println(msg.PeersList)
color.Red("PeersList")
UpdateNetworkPeersList(peer.Conn, msg.PeersList)
PropagatePeersList(peer)
PrintPeersList()
}
msgCases["Data"] = func(peer Peer, msg Msg) {
color.Yellow(msg.Type)
color.Green(msg.Content)
PropagateData(peer, "data")
}
fmt.Println(msgCases)
}

+ 13
- 38
peer/p2plib/messages.go

@ -2,11 +2,8 @@ package p2plib
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"time" "time"
"github.com/fatih/color"
) )
type Msg struct { type Msg struct {
@ -17,46 +14,24 @@ type Msg struct {
Data []byte `json:"data"` Data []byte `json:"data"`
} }
type Case struct {
Case string
Function func(Peer, Msg)
}
var msgCases map[string]func(Peer, Msg)
func MessageHandler(peer Peer, msg Msg) { func MessageHandler(peer Peer, msg Msg) {
log.Println("[New msg]") log.Println("[New msg]")
log.Println(msg) log.Println(msg)
switch msg.Type {
case "Hi":
color.Yellow(msg.Type)
color.Green(msg.Content)
break
case "PeersList":
color.Blue("newPeerslist")
fmt.Println(msg.PeersList)
color.Red("PeersList")
UpdateNetworkPeersList(peer.Conn, msg.PeersList)
PropagatePeersList(peer)
PrintPeersList()
break
case "PeersList_Response":
//for the moment is not beeing used
color.Blue("newPeerslist, from PeersList_Response")
fmt.Println(msg.PeersList)
color.Red("PeersList_Response")
UpdateNetworkPeersList(peer.Conn, msg.PeersList)
PropagatePeersList(peer)
PrintPeersList()
break
case "Block":
/*//TODO check if the block is signed by an autorized emitter
if !blockchain.blockExists(msg.Block) {
blockchain.addBlock(msg.Block)
propagateBlock(msg.Block)
}*/
break
default:
log.Println("Msg.Type not supported")
break
}
/*for c, caseFunction := range msgCases {
if msg.Type == c {
caseFunction(peer, msg)
}
}*/
msgCases[msg.Type](peer, msg)
} }
func (msg *Msg) Construct(msgtype string, msgcontent string) { func (msg *Msg) Construct(msgtype string, msgcontent string) {

+ 15
- 0
peer/p2plib/peers.go

@ -147,6 +147,21 @@ func PrintPeersList() {
} }
fmt.Println("") fmt.Println("")
} }
func PropagateData(p Peer, s string) {
//prepare the msg to send to all connected peers
var msg Msg
msg.Construct("Data", "new Data")
msg.Data = []byte(s)
msgB := msg.ToBytes()
for _, peer := range globalTP.PeersConnections.Outcoming.Peers {
if peer.Conn != nil {
if peer.ID != p.ID && p.ID != "" {
_, err := peer.Conn.Write(msgB)
check(err)
}
}
}
}
//send the block to all the peers of the outcomingPeersList //send the block to all the peers of the outcomingPeersList
/*func PropagateBlock(b Block) { /*func PropagateBlock(b Block) {

+ 6
- 0
peer/p2plib/petitions.go

@ -0,0 +1,6 @@
package p2plib
func SendPetition(peer Peer, petition string) {
_, err := peer.Conn.Write([]byte(petition))
check(err)
}

Loading…
Cancel
Save