mirror of
https://github.com/arnaucube/bc.git
synced 2026-02-07 02:56:42 +01:00
working, next step convert REST functions to tcp msg p2p connections
This commit is contained in:
@@ -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
|
||||
}
|
||||
23
peer/blockchainlib/messageHandler.go
Normal file
23
peer/blockchainlib/messageHandler.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package blockchainlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
p2plib "../p2plib"
|
||||
)
|
||||
|
||||
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 Block
|
||||
if !bc.BlockExists(block) {
|
||||
bc.AddBlock(block)
|
||||
p2plib.PropagateData(peer, "block in string format")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(configuredMsgCases)
|
||||
return configuredMsgCases
|
||||
}
|
||||
Reference in New Issue
Block a user