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 Message struct { | |
BPM int | |
} |
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 respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) { | |
response, err := json.MarshalIndent(payload, "", " ") | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("HTTP 500: Internal Server Error")) | |
return | |
} | |
w.WriteHeader(code) | |
w.Write(response) | |
} |
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 main() { | |
err := godotenv.Load() | |
if err != nil { | |
log.Fatal(err) | |
} | |
go func() { | |
t := time.Now() | |
genesisBlock := Block{0, t.String(), 0, "", ""} | |
spew.Dump(genesisBlock) |
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 ( | |
"bufio" | |
"crypto/sha256" | |
"encoding/hex" | |
"encoding/json" | |
"io" | |
"log" | |
"net" |
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
// Block represents each 'item' in the blockchain | |
type Block struct { | |
Index int | |
Timestamp string | |
BPM int | |
Hash string | |
PrevHash string | |
} | |
// Blockchain is a series of validated Blocks |
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
// SHA256 hashing | |
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
// create a new block using previous block's hash | |
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 |
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
// make sure block is valid by checking index, and comparing the hash of the previous block | |
func isBlockValid(newBlock, oldBlock Block) bool { | |
if oldBlock.Index+1 != newBlock.Index { | |
return false | |
} | |
if oldBlock.Hash != newBlock.PrevHash { | |
return false | |
} |
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
// make sure the chain we're checking is longer than the current blockchain | |
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
// bcServer handles incoming concurrent Blocks | |
var bcServer chan []Block |