Browse Source

Organize packages

gRPC
garritfra 6 years ago
parent
commit
c46347c1d7
  1. 2
      core/Block.go
  2. 16
      core/Blockchain.go
  3. 47
      core/Server.go
  4. 2
      core/Transaction.go
  5. 5
      core/main.go
  6. 4
      crypto/crypto.go
  7. 7
      main.go
  8. 47
      server/server.go

2
core/Block.go

@ -1,4 +1,4 @@
package main package core
import ( import (
"time" "time"

16
core/Blockchain.go

@ -1,14 +1,16 @@
package main package core
import ( import (
"log" "log"
"strings" "strings"
"time" "time"
"github.com/garritfra/blockchain-project/crypto"
) )
// Blockchain struct // Blockchain struct
type Blockchain struct { type Blockchain struct {
blocks []Block Blocks []Block
} }
// AddBlock adds a block to the chain // AddBlock adds a block to the chain
@ -18,12 +20,12 @@ func (bc *Blockchain) AddBlock(block Block) {
// Mine Block // Mine Block
for { for {
hash := calculateHash(block) hash := crypto.CalculateHash(block)
if strings.HasPrefix(hash, "0000") { if strings.HasPrefix(hash, "0000") {
block.Hash = hash block.Hash = hash
block.Timestamp = time.Now() block.Timestamp = time.Now()
block.PreviousHash = bc.GetLastHash() block.PreviousHash = bc.GetLastHash()
bc.blocks = append(bc.blocks, block) bc.Blocks = append(bc.Blocks, block)
log.Print("Block Added: ", block.Hash) log.Print("Block Added: ", block.Hash)
break break
} }
@ -36,7 +38,7 @@ func (bc *Blockchain) AddBlock(block Block) {
func NewBlockchain() Blockchain { func NewBlockchain() Blockchain {
log.Print("Creating Blockchain...") log.Print("Creating Blockchain...")
blockchain := Blockchain{blocks: make([]Block, 0)} blockchain := Blockchain{Blocks: make([]Block, 0)}
genesisBlock := generateGenesisBlock() genesisBlock := generateGenesisBlock()
blockchain.AddBlock(genesisBlock) blockchain.AddBlock(genesisBlock)
@ -55,10 +57,10 @@ func generateGenesisBlock() Block {
// GetLastHash returns the hash of the latest block on the chain // GetLastHash returns the hash of the latest block on the chain
func (bc *Blockchain) GetLastHash() string { func (bc *Blockchain) GetLastHash() string {
bcLength := len(bc.blocks) bcLength := len(bc.Blocks)
if bcLength == 0 { if bcLength == 0 {
return "0" return "0"
} }
return bc.blocks[len(bc.blocks)-1].Hash return bc.Blocks[len(bc.Blocks)-1].Hash
} }

47
core/Server.go

@ -1,47 +0,0 @@
package main
import (
"encoding/gob"
"encoding/json"
"net/http"
)
var blockchain Blockchain
// StartServer starts the HTTP server on port 8080
func StartServer() {
gob.Register(Block{})
gob.Register(Transaction{})
gob.Register(Blockchain{})
blockchain = NewBlockchain()
block := Block{}
transaction := Transaction{Sender: "foo", Receiver: "bar", Amount: 100}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
block = Block{}
transaction = Transaction{Sender: "bar", Receiver: "baz", Amount: 500}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
http.HandleFunc("/", redirect)
http.HandleFunc("/blockchain", listBlocks)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/blockchain", 301)
}
func listBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.blocks)
}

2
core/Transaction.go

@ -1,4 +1,4 @@
package main package core
// Transaction struct // Transaction struct
type Transaction struct { type Transaction struct {

5
core/main.go

@ -1,5 +0,0 @@
package main
func main() {
StartServer()
}

4
core/crypto.go → crypto/crypto.go

@ -1,4 +1,4 @@
package main package crypto
import ( import (
"bytes" "bytes"
@ -7,7 +7,7 @@ import (
"encoding/hex" "encoding/hex"
) )
func calculateHash(obj interface{}) string { func CalculateHash(obj interface{}) string {
var buffer bytes.Buffer var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer) encoder := gob.NewEncoder(&buffer)
if err := encoder.Encode(obj); err != nil { if err := encoder.Encode(obj); err != nil {

7
main.go

@ -0,0 +1,7 @@
package main
import "github.com/garritfra/blockchain-project/server"
func main() {
server.Start()
}

47
server/server.go

@ -0,0 +1,47 @@
package server
import (
"encoding/gob"
"encoding/json"
"net/http"
core "github.com/garritfra/blockchain-project/core"
)
var blockchain core.Blockchain
// Start starts the HTTP server on port 8080
func Start() {
gob.Register(core.Block{})
gob.Register(core.Transaction{})
gob.Register(core.Blockchain{})
blockchain = core.NewBlockchain()
block := core.Block{}
transaction := core.Transaction{Sender: "foo", Receiver: "bar", Amount: 100}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
block = core.Block{}
transaction = core.Transaction{Sender: "bar", Receiver: "baz", Amount: 500}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
http.HandleFunc("/", redirect)
http.HandleFunc("/blockchain", listBlocks)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/blockchain", 301)
}
func listBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.Blocks)
}
Loading…
Cancel
Save