Browse Source

Add Pending Transactions

gRPC
garritfra 6 years ago
parent
commit
7b9e323f97
  1. 6
      core/Block.go
  2. 41
      core/Blockchain.go
  3. 2
      core/blockchain_test.go
  4. 24
      server/webserver.go

6
core/Block.go

@ -12,9 +12,3 @@ type Block struct {
Data []Transaction
Nonce int
}
// AddTransaction takes in a transaction and adds it to the block
func (block *Block) AddTransaction(transaction Transaction) error {
block.Data = append(block.Data, transaction)
return nil
}

41
core/Blockchain.go

@ -10,24 +10,28 @@ import (
// Blockchain struct
type Blockchain struct {
Blocks []Block
Blocks []Block
PendingTransactions []Transaction
}
// AddBlock adds a block to the chain
func (bc *Blockchain) AddBlock(block Block) {
log.Print("Mining Block...")
// MineBlock adds a block to the chain
func (bc *Blockchain) MineBlock() Block {
block := Block{}
block.Data = bc.PendingTransactions
block.Timestamp = time.Now()
block.PreviousHash = bc.GetLastHash()
// Mine Block
log.Print("Mining Block...")
for {
hash := crypto.CalculateHash(block)
if strings.HasPrefix(hash, "0000") {
if strings.HasPrefix(hash, "00000") {
block.Hash = hash
block.Timestamp = time.Now()
block.PreviousHash = bc.GetLastHash()
bc.Blocks = append(bc.Blocks, block)
bc.PendingTransactions = []Transaction{}
log.Print("Block Added: ", block.Hash)
break
return block
}
block.Nonce++
}
@ -38,20 +42,17 @@ 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), PendingTransactions: make([]Transaction, 0)}
genesisBlock := generateGenesisBlock()
blockchain.AddBlock(genesisBlock)
// Mine Genesis Block
blockchain.MineBlock()
return blockchain
}
func generateGenesisBlock() Block {
block := Block{PreviousHash: "0", Timestamp: time.Now()}
transaction := Transaction{Amount: 0, Sender: "0", Receiver: "0", Message: "Genesis"}
block.AddTransaction(transaction)
log.Print("Genesis Block created")
return block
// AddTransaction takes in a transaction and adds it to the block
func (bc *Blockchain) AddTransaction(transaction Transaction) error {
bc.PendingTransactions = append(bc.PendingTransactions, transaction)
return nil
}
// GetLastHash returns the hash of the latest block on the chain

2
core/blockchain_test.go

@ -23,7 +23,7 @@ func TestNewBlockchain(t *testing.T) {
func TestBlockchain_GetLastHash(t *testing.T) {
blockchain := NewBlockchain()
blockchain.AddBlock(Block{})
blockchain.MineBlock(Block{})
want := blockchain.Blocks[1].Hash
got := blockchain.GetLastHash()
if got != want {

24
server/webserver.go

@ -20,6 +20,8 @@ func ServeHTTP(port string) {
blockchain = core.NewBlockchain()
http.HandleFunc("/", handleMain)
http.HandleFunc("/mine_block", handleMineBlock)
http.HandleFunc("/pending_transactions", handleListPendingTransactions)
log.Print("Listening on port ", port)
if err := http.ListenAndServe(port, nil); err != nil {
@ -32,7 +34,7 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
case "GET":
handleListBlocks(w, r)
case "POST":
handleAddBlock(w, r)
handleAddTransaction(w, r)
}
}
@ -45,14 +47,24 @@ func handleListBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.Blocks)
}
func handleAddBlock(w http.ResponseWriter, r *http.Request) {
func handleAddTransaction(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var receivedBlock core.Block
err := decoder.Decode(&receivedBlock)
var receivedTransaction core.Transaction
err := decoder.Decode(&receivedTransaction)
if err == nil {
log.Println(receivedBlock.Data, " received")
go blockchain.AddBlock(receivedBlock)
log.Println("Transaction from", receivedTransaction.Sender, "to", receivedTransaction.Receiver, "received")
go blockchain.AddTransaction(receivedTransaction)
} else {
handleError(err, w, r)
}
}
func handleMineBlock(w http.ResponseWriter, r *http.Request) {
block := blockchain.MineBlock()
json.NewEncoder(w).Encode(block)
}
func handleListPendingTransactions(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.PendingTransactions)
}

Loading…
Cancel
Save