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 readData(rw *bufio.ReadWriter) { | |
for { | |
str, err := rw.ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
if str == "" { | |
return |
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 handleStream(s net.Stream) { | |
log.Println("Got a new stream!") | |
// Create a buffer stream for non blocking read and write. | |
rw := bufio.NewReadWriter(bufio.NewReader(s), bufio.NewWriter(s)) | |
go readData(rw) | |
go writeData(rw) |
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
// makeBasicHost creates a LibP2P host with a random peer ID listening on the | |
// given multiaddress. It will use secio if secio is true. | |
func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error) { | |
// If the seed is zero, use real cryptographic randomness. Otherwise, use a | |
// deterministic randomness source to make generated keys stay the same | |
// across multiple runs | |
var r io.Reader | |
if randseed == 0 { | |
r = rand.Reader |
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
// 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
package main | |
import ( | |
"bufio" | |
"context" | |
"crypto/rand" | |
"crypto/sha256" | |
"encoding/hex" | |
"encoding/json" | |
"flag" |
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) | |
} | |
// create genesis block | |
t := time.Now() | |
genesisBlock := Block{} | |
genesisBlock = Block{0, t.String(), 0, calculateBlockHash(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
// pickWinner creates a lottery pool of validators and chooses the validator who gets to forge a block to the blockchain | |
// by random selecting from the pool, weighted by amount of tokens staked | |
func pickWinner() { | |
time.Sleep(30 * time.Second) | |
mutex.Lock() | |
temp := tempBlocks | |
mutex.Unlock() | |
lotteryPool := []string{} | |
if len(temp) > 0 { |
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 handleConn(conn net.Conn) { | |
defer conn.Close() | |
go func() { | |
for { | |
msg := <-announcements | |
io.WriteString(conn, msg) | |
} | |
}() | |
// validator address |
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
// isBlockValid makes 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 | |
} |