Skip to content

Commit

Permalink
Implemented sending and receiving blocks over http instead of inline
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoras committed Feb 1, 2019
1 parent 7265c5e commit d78af6e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 68 deletions.
2 changes: 1 addition & 1 deletion blockwebserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func blockWebServer() {
r := mux.NewRouter()
r.HandleFunc("/block/{height}", blockWebSendBlock)

serverAddress := fmt.Sprintf(":%d", DefaultBlockWebServerPort)
serverAddress := fmt.Sprintf(":%d", cfg.httpPort)

log.Println("HTTP listening on", serverAddress)
err := http.ListenAndServe(serverAddress, r)
Expand Down
15 changes: 10 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ const DefaultConfigFile = "/etc/daisy/config.json"
const DefaultDataDir = ".daisy"

var cfg struct {
configFile string
P2pPort int `json:"p2p_port"`
DataDir string `json:"data_dir"`
showHelp bool
faster bool
configFile string
P2pPort int `json:"p2p_port"`
DataDir string `json:"data_dir"`
httpPort int
showHelp bool
faster bool
p2pBlockInline bool
}

// Initialises defaults, parses command line
Expand All @@ -40,6 +42,7 @@ func configInit() {

// Init defaults
cfg.P2pPort = DefaultP2PPort
cfg.httpPort = DefaultBlockWebServerPort

// Config file is parsed first
for i, arg := range os.Args {
Expand All @@ -56,9 +59,11 @@ func configInit() {

// Then override the configuration with command-line flags
flag.IntVar(&cfg.P2pPort, "port", cfg.P2pPort, "P2P port")
flag.IntVar(&cfg.httpPort, "http-port", cfg.httpPort, "HTTP port")
flag.StringVar(&cfg.DataDir, "dir", cfg.DataDir, "Data directory")
flag.BoolVar(&cfg.showHelp, "help", false, "Shows CLI usage information")
flag.BoolVar(&cfg.faster, "faster", false, "Be faster when starting up")
flag.BoolVar(&cfg.p2pBlockInline, "p2pblockinline", false, "Send blocks to peers inline instead of over HTTP")
flag.Parse()

if cfg.showHelp {
Expand Down
164 changes: 102 additions & 62 deletions p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/ioutil"
"log"
"net"
"net/http"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -541,33 +542,43 @@ func (p2pc *p2pConnection) handleGetBlock(msg StrIfMap) {
return
}
fileSize := st.Size()
f, err := os.Open(fileName)
if err != nil {
log.Println(err)
return
}
defer func() {
err = f.Close()

var msgBlockEncoding, msgBlockData string

if cfg.p2pBlockInline {
f, err := os.Open(fileName)
if err != nil {
log.Printf("handleGetBlock f.Close: %v", err)
log.Println(err)
return
}
}()
var zbuf bytes.Buffer
w := zlib.NewWriter(&zbuf)
written, err := io.Copy(w, f)
if err != nil {
log.Println(err)
return
}
if written != fileSize {
log.Println("Something broke when working with zlib:", written, "vs", fileSize)
return
}
err = w.Close()
if err != nil {
log.Panic(err)
defer func() {
err = f.Close()
if err != nil {
log.Printf("handleGetBlock f.Close: %v", err)
}
}()
var zbuf bytes.Buffer
w := zlib.NewWriter(&zbuf)
written, err := io.Copy(w, f)
if err != nil {
log.Println(err)
return
}
if written != fileSize {
log.Println("Something broke when working with zlib:", written, "vs", fileSize)
return
}
err = w.Close()
if err != nil {
log.Panic(err)
}
msgBlockEncoding = "zlib-base64"
msgBlockData = base64.StdEncoding.EncodeToString(zbuf.Bytes())
} else {
msgBlockEncoding = "http"
msgBlockData = fmt.Sprintf("http://%s:%d/block/%d", getLocalAddresses()[0], cfg.httpPort, dbb.Height)
}
b64block := base64.StdEncoding.EncodeToString(zbuf.Bytes())

respMsg := p2pMsgBlockStruct{
p2pMsgHeader: p2pMsgHeader{
P2pID: p2pEphemeralID,
Expand All @@ -576,8 +587,8 @@ func (p2pc *p2pConnection) handleGetBlock(msg StrIfMap) {
},
Hash: hash,
HashSignature: hex.EncodeToString(dbb.HashSignature),
Encoding: "zlib-base64",
Data: b64block,
Encoding: msgBlockEncoding,
Data: msgBlockData,
Size: fileSize,
}
p2pc.chanToPeer <- respMsg
Expand Down Expand Up @@ -615,50 +626,79 @@ func (p2pc *p2pConnection) handleBlock(msg StrIfMap) {
log.Printf("encoding: %v", err)
return
}
if encoding != "zlib-base64" {
log.Println("Unsupported encoding:", encoding)
return
}
zlibData, err := base64.StdEncoding.DecodeString(dataString)
if err != nil {
log.Println(err)
return
}
blockFile, err = ioutil.TempFile("", "daisy")
if err != nil {
log.Println(err)
return
}
defer func() {
err = blockFile.Close()
if encoding == "zlib-base64" {
zlibData, err := base64.StdEncoding.DecodeString(dataString)
if err != nil {
log.Println(err)
return
}
blockFile, err = ioutil.TempFile("", "daisy")
if err != nil {
log.Printf("handleBlock blockFile.Close: %v", err)
log.Println(err)
return
}
err = os.Remove(blockFile.Name())
defer func() {
err = blockFile.Close()
if err != nil {
log.Printf("handleBlock blockFile.Close: %v", err)
}
err = os.Remove(blockFile.Name())
if err != nil {
log.Printf("remove: %v", err)
}
}()
r, err := zlib.NewReader(bytes.NewReader(zlibData))
if err != nil {
log.Printf("remove: %v", err)
log.Println(err)
return
}
}()
r, err := zlib.NewReader(bytes.NewReader(zlibData))
if err != nil {
log.Println(err)
return
}
defer func() {
err = r.Close()
defer func() {
err = r.Close()
if err != nil {
log.Printf("handleBlock r.Close: %v", err)
}
}()
written, err := io.Copy(blockFile, r)
if err != nil {
log.Printf("handleBlock r.Close: %v", err)
log.Println(err)
return
}
}()
written, err := io.Copy(blockFile, r)
if err != nil {
log.Println(err)
return
}
if written != fileSize {
log.Println("Error decoding block: sizes don't match:", written, "vs", fileSize)
if written != fileSize {
log.Println("Error decoding block: sizes don't match:", written, "vs", fileSize)
return
}
} else if encoding == "http" {
blockFile, err = ioutil.TempFile("", "daisy")
if err != nil {
log.Println(err)
return
}
defer func() {
err = blockFile.Close()
if err != nil {
log.Printf("handleBlock blockFile.Close: %v", err)
}
err = os.Remove(blockFile.Name())
if err != nil {
log.Printf("remove: %v", err)
}
}()
resp, err := http.Get(dataString)
if err != nil {
log.Println("Error receiving block at", dataString, err)
return
}
defer resp.Body.Close()
written, err := io.Copy(blockFile, resp.Body)
if written != fileSize {
log.Println("Error decoding block: sizes don't match:", written, "vs", fileSize)
return
}
} else {
log.Println("Unknown block encoding:", encoding)
return
}

blk, err := OpenBlockFile(blockFile.Name())
if err != nil {
log.Println(p2pc.conn, err)
Expand Down

0 comments on commit d78af6e

Please sign in to comment.