Skip to content

Commit

Permalink
Implemented block file serving over HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoras committed Feb 1, 2019
1 parent ea3576e commit 7265c5e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
5 changes: 5 additions & 0 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ func blockchainInit(createDefault bool) {
// Verifies the entire blockchain to see if there are errors.
// TODO: Dynamic adding and revoking of key is not yet checked
func blockchainVerifyEverything() error {
if cfg.faster {
log.Println("Skipping blockchain consistency checks")
return nil
}
log.Println("Verifying all the blocks...")
maxHeight := dbGetBlockchainHeight()
for height := 0; height <= maxHeight; height++ {
if height > 0 && height%1000 == 0 {
Expand Down
47 changes: 47 additions & 0 deletions blockwebserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"strconv"

"github.com/gorilla/mux"
)

func blockWebSendBlock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

blockHeight, err := strconv.Atoi(vars["height"])
if err != nil {
log.Println(vars)
w.WriteHeader(http.StatusBadRequest)
return
}

blockFilename := blockchainGetFilename(blockHeight)
if _, err := os.Stat(blockFilename); os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
log.Println("Block file not found:", blockFilename)
return
}

log.Println("Serving block", blockHeight)
w.Header().Set("Content-Type", "application/x-sqlite3")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%08x.db\"", blockHeight))
http.ServeFile(w, r, blockFilename)
}

func blockWebServer() {
r := mux.NewRouter()
r.HandleFunc("/block/{height}", blockWebSendBlock)

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

log.Println("HTTP listening on", serverAddress)
err := http.ListenAndServe(serverAddress, r)
if err != nil {
panic(err)
}
}
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
// DefaultP2PPort is the default TCP port for p2p connections
const DefaultP2PPort = 2017

// DefaultBlockWebServerPort is the default TCP port for the HTTP server
const DefaultBlockWebServerPort = 2018

// DefaultConfigFile is the default configuration filename
const DefaultConfigFile = "/etc/daisy/config.json"

Expand All @@ -24,6 +27,7 @@ var cfg struct {
P2pPort int `json:"p2p_port"`
DataDir string `json:"data_dir"`
showHelp bool
faster bool
}

// Initialises defaults, parses command line
Expand Down Expand Up @@ -54,6 +58,7 @@ func configInit() {
flag.IntVar(&cfg.P2pPort, "port", cfg.P2pPort, "P2P 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.Parse()

if cfg.showHelp {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func main() {
go p2pCoordinator.Run()
go p2pServer()
go p2pClient()
go blockWebServer()

for {
select {
Expand Down
2 changes: 1 addition & 1 deletion p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func p2pServer() {
log.Fatalf("p2pServer l.Close: %v", err)
}
}()
log.Println("Listening on", serverAddress)
log.Println("P2P listening on", serverAddress)
for {
conn, err := l.Accept()
if err != nil {
Expand Down

0 comments on commit 7265c5e

Please sign in to comment.