Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
.DS_Store
.vscode
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/nosequeldeebee/blockchain-tutorial

go 1.16

require (
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/mux v1.8.0
github.com/joho/godotenv v1.3.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func handleGetBlockchain(w http.ResponseWriter, r *http.Request) {

// takes JSON payload as an input for heart rate (BPM)
func handleWriteBlock(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var msg Message

decoder := json.NewDecoder(r.Body)
Expand All @@ -121,6 +120,7 @@ func handleWriteBlock(w http.ResponseWriter, r *http.Request) {
}

func respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
response, err := json.MarshalIndent(payload, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
8 changes: 8 additions & 0 deletions networking/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/nosequeldeebee/blockchain-tutorial/networking

go 1.16

require (
github.com/davecgh/go-spew v1.1.1
github.com/joho/godotenv v1.3.0
)
4 changes: 4 additions & 0 deletions networking/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
12 changes: 8 additions & 4 deletions networking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ func handleConn(conn net.Conn) {
log.Printf("%v not a number: %v", scanner.Text(), err)
continue
}
newBlock, err := generateBlock(Blockchain[len(Blockchain)-1], bpm)

mutex.Lock()
prevBlock := Blockchain[len(Blockchain)-1]
newBlock, err := generateBlock(prevBlock, bpm)
if err != nil {
log.Println(err)
continue
}
if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
if isBlockValid(newBlock, prevBlock) {
newBlockchain := append(Blockchain, newBlock)
replaceChain(newBlockchain)
}
mutex.Unlock()

bcServer <- Blockchain
io.WriteString(conn, "\nEnter a new BPM:")
Expand All @@ -112,7 +116,7 @@ func handleConn(conn net.Conn) {
}
}()

for _ = range bcServer {
for range bcServer {
spew.Dump(Blockchain)
}

Expand Down Expand Up @@ -146,7 +150,7 @@ func replaceChain(newBlocks []Block) {

// SHA256 hasing
func calculateHash(block Block) string {
record := string(block.Index) + block.Timestamp + string(block.BPM) + block.PrevHash
record := strconv.Itoa(block.Index) + block.Timestamp + strconv.Itoa(block.BPM) + block.PrevHash
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
Expand Down
27 changes: 16 additions & 11 deletions p2p/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Peer 2 Peer Blockchain Tutorial

### Getting started

You'll need to set up the `gx` package manager. If you don't know how to do that, don't worry. Hang tight and read the upcoming blog post.

In your 1st terminal `go run main.go -secio -l 10000`

Follow the instructions in your 1st terminal and copy and paste the given command into your 2nd terminal e.g. `go run main.go -l 10001 -d /ip4/127.0.0.1/tcp/10000/ipfs/QmZ8NayvdXc2U2A1cwh9qGaHK7uxXXVrZQEYwDqbfFydfj -secio`

Follow the instructions in your 2nd terminal and copy and paste the given command into your 3rd terminal e.g. `go run main.go -l 10002 -d /ip4/127.0.0.1/tcp/10001/ipfs/QmRAj9JJVKRJmWHbDKzvzKDVVFPWxuWYio3bPym4SgGPgF -secio`

Type a BPM into any of your terminals and watch your blockchain be broadcast to all terminals!
### Tutorial
[Read](https://medium.com/@mycoralhealth/code-a-simple-p2p-blockchain-in-go-46662601f417) the blog post for this tutorial first!

### Ask us anything!

Join our [Telegram](https://t.me/joinchat/FX6A7UThIZ1WOUNirDS_Ew) community and follow us on [Twitter](https://twitter.com/myCoralHealth)!

### Deployment steps:
- clone this repo
- navigate to this directory
- `go run main.go -l 10000 -secio`
- open a new terminal window
- follow the instructions in your 1st terminal and copy and paste the given command into your 2nd terminal e.g. `go run main.go -l 10001 -d /ip4/127.0.0.1/tcp/10000/ipfs/QmZ8NayvdXc2U2A1cwh9qGaHK7uxXXVrZQEYwDqbfFydfj -secio`
- open a new terminal window
- follow the instructions in your 2nd terminal and copy and paste the given command into your 3rd terminal e.g. `go run main.go -l 10002 -d /ip4/127.0.0.1/tcp/10001/ipfs/QmRAj9JJVKRJmWHbDKzvzKDVVFPWxuWYio3bPym4SgGPgF -secio`
- type a BPM into any of your terminals and watch your blockchain be broadcast to all terminals!


12 changes: 12 additions & 0 deletions p2p/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/nosequeldeebee/blockchain-tutorial/p2p

go 1.16

require (
github.com/davecgh/go-spew v1.1.1
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.1.3
github.com/libp2p/go-libp2p v0.14.1
github.com/libp2p/go-libp2p-core v0.8.5
github.com/multiformats/go-multiaddr v0.3.2
)
Loading