diff --git a/core/Block.go b/core/Block.go index 94c4f71..f54b087 100644 --- a/core/Block.go +++ b/core/Block.go @@ -1,4 +1,4 @@ -package main +package core import ( "time" diff --git a/core/Blockchain.go b/core/Blockchain.go index 91c58a1..7a32b8c 100644 --- a/core/Blockchain.go +++ b/core/Blockchain.go @@ -1,14 +1,16 @@ -package main +package core import ( "log" "strings" "time" + + "github.com/garritfra/blockchain-project/crypto" ) // Blockchain struct type Blockchain struct { - blocks []Block + Blocks []Block } // AddBlock adds a block to the chain @@ -18,12 +20,12 @@ func (bc *Blockchain) AddBlock(block Block) { // Mine Block for { - hash := calculateHash(block) + hash := crypto.CalculateHash(block) if strings.HasPrefix(hash, "0000") { block.Hash = hash block.Timestamp = time.Now() block.PreviousHash = bc.GetLastHash() - bc.blocks = append(bc.blocks, block) + bc.Blocks = append(bc.Blocks, block) log.Print("Block Added: ", block.Hash) break } @@ -36,7 +38,7 @@ func (bc *Blockchain) AddBlock(block Block) { func NewBlockchain() Blockchain { log.Print("Creating Blockchain...") - blockchain := Blockchain{blocks: make([]Block, 0)} + blockchain := Blockchain{Blocks: make([]Block, 0)} genesisBlock := generateGenesisBlock() blockchain.AddBlock(genesisBlock) @@ -55,10 +57,10 @@ func generateGenesisBlock() Block { // GetLastHash returns the hash of the latest block on the chain func (bc *Blockchain) GetLastHash() string { - bcLength := len(bc.blocks) + bcLength := len(bc.Blocks) if bcLength == 0 { return "0" } - return bc.blocks[len(bc.blocks)-1].Hash + return bc.Blocks[len(bc.Blocks)-1].Hash } diff --git a/core/Server.go b/core/Server.go deleted file mode 100644 index a8bcec1..0000000 --- a/core/Server.go +++ /dev/null @@ -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) -} diff --git a/core/Transaction.go b/core/Transaction.go index 7fb6155..266140e 100644 --- a/core/Transaction.go +++ b/core/Transaction.go @@ -1,4 +1,4 @@ -package main +package core // Transaction struct type Transaction struct { diff --git a/core/main.go b/core/main.go deleted file mode 100644 index 7b6fb33..0000000 --- a/core/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -func main() { - StartServer() -} diff --git a/core/crypto.go b/crypto/crypto.go similarity index 84% rename from core/crypto.go rename to crypto/crypto.go index e5d3bfe..4312dfe 100644 --- a/core/crypto.go +++ b/crypto/crypto.go @@ -1,4 +1,4 @@ -package main +package crypto import ( "bytes" @@ -7,7 +7,7 @@ import ( "encoding/hex" ) -func calculateHash(obj interface{}) string { +func CalculateHash(obj interface{}) string { var buffer bytes.Buffer encoder := gob.NewEncoder(&buffer) if err := encoder.Encode(obj); err != nil { diff --git a/main.go b/main.go new file mode 100644 index 0000000..ab6b7c1 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/garritfra/blockchain-project/server" + +func main() { + server.Start() +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..7a26abe --- /dev/null +++ b/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) +}