Browse Source

list blocks

gRPC
garritfra 6 years ago
parent
commit
e1c1917cc7
  1. 19
      core/Block.go
  2. 5
      core/Blockchain.go
  3. 32
      core/Server.go
  4. 8
      core/Transaction.go
  5. 35
      core/main.go

19
core/Block.go

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"time"
)
@ -12,10 +13,19 @@ type Block struct {
Timestamp time.Time
Hash []byte
PreviousHash []byte
Data []byte
Data interface{}
}
func newBlock(previousHash []byte, data []byte) Block {
// BlockJSON is a modified Block struct for json representation
type BlockJSON struct {
Timestamp time.Time
Hash string
PreviouseHash string
Data interface{}
}
// NewBlock creates a new Block
func NewBlock(previousHash []byte, data interface{}) Block {
block := Block{Timestamp: time.Now(), PreviousHash: previousHash, Data: data}
block.calculateHash()
@ -37,3 +47,8 @@ func (block *Block) calculateHash() {
block.Hash = sum
}
// EncodeJSON encodes a Block to a JSON representable struct
func (block *Block) EncodeJSON() BlockJSON {
return BlockJSON{Timestamp: block.Timestamp, PreviouseHash: hex.EncodeToString(block.PreviousHash), Hash: hex.EncodeToString(block.Hash), Data: block.Data}
}

5
core/Blockchain.go

@ -9,7 +9,8 @@ func (bc *Blockchain) addBlock(block Block) {
bc.blocks = append(bc.blocks, block)
}
func newBlockchain() Blockchain {
// NewBlockchain creates a new Blockchain
func NewBlockchain() Blockchain {
blockchain := Blockchain{blocks: make([]Block, 0)}
@ -20,6 +21,6 @@ func newBlockchain() Blockchain {
}
func generateGenesisBlock() Block {
block := newBlock([]byte{}, []byte("Genesis"))
block := NewBlock([]byte{}, []byte("Genesis"))
return block
}

32
core/Server.go

@ -1,9 +1,9 @@
package main
import (
"encoding/gob"
"encoding/json"
"net/http"
"strings"
)
var blockchain Blockchain
@ -11,17 +11,15 @@ var blockchain Blockchain
// StartServer starts the HTTP server on port 8080
func StartServer() {
blockchain = newBlockchain()
gob.Register(Block{})
gob.Register(Transaction{})
gob.Register(Blockchain{})
data, _ := json.Marshal(Transaction{Sender: "foo", Receiver: "bar", Amount: 100})
block := newBlock(blockchain.blocks[0].Hash, data)
blockchain = NewBlockchain()
transaction := Transaction{Sender: "foo", Receiver: "bar", Amount: 100}
block := NewBlock(blockchain.blocks[0].Hash, transaction)
blockchain.addBlock(block)
data2, _ := json.Marshal(Transaction{Sender: "bar", Receiver: "baz", Amount: 5000})
block = newBlock(blockchain.blocks[1].Hash, data2)
blockchain.addBlock(block)
http.HandleFunc("/", sayHello)
http.HandleFunc("/blockchain", listBlocks)
if err := http.ListenAndServe(":8080", nil); err != nil {
@ -29,13 +27,13 @@ func StartServer() {
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello " + message
w.Write([]byte(message))
}
func listBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.blocks)
var jsonBlocks []BlockJSON
for _, block := range blockchain.blocks {
jsonBlocks = append(jsonBlocks, block.EncodeJSON())
}
json.NewEncoder(w).Encode(jsonBlocks)
}

8
core/Transaction.go

@ -0,0 +1,8 @@
package main
// Transaction struct
type Transaction struct {
Sender string
Receiver string
Amount int
}

35
core/main.go

@ -1,40 +1,5 @@
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
)
// Transaction struct
type Transaction struct {
Sender string
Receiver string
Amount int
}
func main() {
blockchain := newBlockchain()
StartServer()
data, _ := json.Marshal(Transaction{Sender: "foo", Receiver: "bar", Amount: 100})
block := newBlock(blockchain.blocks[0].Hash, data)
blockchain.addBlock(block)
data2, _ := json.Marshal(Transaction{Sender: "bar", Receiver: "baz", Amount: 5000})
block = newBlock(blockchain.blocks[1].Hash, data2)
blockchain.addBlock(block)
for i := 0; i < len(blockchain.blocks); i++ {
block := blockchain.blocks[i]
fmt.Println("Block " + string(i))
fmt.Println(hex.EncodeToString(block.PreviousHash))
fmt.Println(hex.EncodeToString(block.Hash))
data := string(block.Data)
fmt.Println("Data: " + data)
}
}

Loading…
Cancel
Save