This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Block struct { | |
Index int | |
Timestamp string | |
BPM int | |
Hash string | |
PrevHash string | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Blockchain []Block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func calculateHash(block Block) string { | |
record := string(block.Index) + block.Timestamp + string(block.BPM) + block.PrevHash | |
h := sha256.New() | |
h.Write([]byte(record)) | |
hashed := h.Sum(nil) | |
return hex.EncodeToString(hashed) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateBlock(oldBlock Block, BPM int) (Block, error) { | |
var newBlock Block | |
t := time.Now() | |
newBlock.Index = oldBlock.Index + 1 | |
newBlock.Timestamp = t.String() | |
newBlock.BPM = BPM | |
newBlock.PrevHash = oldBlock.Hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isBlockValid(newBlock, oldBlock Block) bool { | |
if oldBlock.Index+1 != newBlock.Index { | |
return false | |
} | |
if oldBlock.Hash != newBlock.PrevHash { | |
return false | |
} | |
if calculateHash(newBlock) != newBlock.Hash { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func replaceChain(newBlocks []Block) { | |
if len(newBlocks) > len(Blockchain) { | |
Blockchain = newBlocks | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha256" | |
"encoding/hex" | |
"encoding/json" | |
"io" | |
"log" | |
"net/http" | |
"os" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func run() error { | |
mux := makeMuxRouter() | |
httpAddr := os.Getenv("ADDR") | |
log.Println("Listening on ", os.Getenv("ADDR")) | |
s := &http.Server{ | |
Addr: ":" + httpAddr, | |
Handler: mux, | |
ReadTimeout: 10 * time.Second, | |
WriteTimeout: 10 * time.Second, | |
MaxHeaderBytes: 1 << 20, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makeMuxRouter() http.Handler { | |
muxRouter := mux.NewRouter() | |
muxRouter.HandleFunc("/", handleGetBlockchain).Methods("GET") | |
muxRouter.HandleFunc("/", handleWriteBlock).Methods("POST") | |
return muxRouter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handleGetBlockchain(w http.ResponseWriter, r *http.Request) { | |
bytes, err := json.MarshalIndent(Blockchain, "", " ") | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
io.WriteString(w, string(bytes)) | |
} |
OlderNewer