Skip to content

Commit

Permalink
add mutex locks to prevent data race
Browse files Browse the repository at this point in the history
  • Loading branch information
nosequeldeebee committed Mar 4, 2018
1 parent 37611d5 commit c473b6c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func main() {
genesisBlock := Block{}
genesisBlock = Block{0, t.String(), 0, calculateHash(genesisBlock), ""}
spew.Dump(genesisBlock)

mutex.Lock()
Blockchain = append(Blockchain, genesisBlock)
mutex.Unlock()
}()
log.Fatal(run())

Expand Down
7 changes: 5 additions & 2 deletions proof-work/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/joho/godotenv"
)

const difficulty = 2
const difficulty = 1

// Block represents each 'item' in the blockchain
type Block struct {
Expand Down Expand Up @@ -53,7 +53,10 @@ func main() {
genesisBlock := Block{}
genesisBlock = Block{0, t.String(), 0, calculateHash(genesisBlock), "", difficulty, ""}
spew.Dump(genesisBlock)

mutex.Lock()
Blockchain = append(Blockchain, genesisBlock)
mutex.Unlock()
}()
log.Fatal(run())

Expand Down Expand Up @@ -182,6 +185,7 @@ func generateBlock(oldBlock Block, BPM int) (Block, error) {
newBlock.Nonce = hex
if !isHashValid(calculateHash(newBlock), newBlock.Difficulty) {
fmt.Println(calculateHash(newBlock), " do more work!")
time.Sleep(time.Second)
continue
} else {
fmt.Println(calculateHash(newBlock), " work done!")
Expand All @@ -190,7 +194,6 @@ func generateBlock(oldBlock Block, BPM int) (Block, error) {
}

}

return newBlock, nil
}

Expand Down

0 comments on commit c473b6c

Please sign in to comment.