From 7265c5ec8e6f538e3629882c11e93a6d8b2d5b5d Mon Sep 17 00:00:00 2001 From: ivoras Date: Fri, 1 Feb 2019 23:11:30 +0100 Subject: [PATCH] Implemented block file serving over HTTP --- blockchain.go | 5 +++++ blockwebserver.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ config.go | 5 +++++ main.go | 1 + p2p.go | 2 +- 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 blockwebserver.go diff --git a/blockchain.go b/blockchain.go index 7633a6f..5dcc5fd 100644 --- a/blockchain.go +++ b/blockchain.go @@ -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 { diff --git a/blockwebserver.go b/blockwebserver.go new file mode 100644 index 0000000..2378dc6 --- /dev/null +++ b/blockwebserver.go @@ -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) + } +} diff --git a/config.go b/config.go index 9810c69..1bb5bcf 100644 --- a/config.go +++ b/config.go @@ -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" @@ -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 @@ -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 { diff --git a/main.go b/main.go index eb3d9e9..d5f1650 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ func main() { go p2pCoordinator.Run() go p2pServer() go p2pClient() + go blockWebServer() for { select { diff --git a/p2p.go b/p2p.go index 3d3db79..40d4a34 100644 --- a/p2p.go +++ b/p2p.go @@ -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 {