Skip to content

Commit

Permalink
added transaction sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Feb 23, 2024
1 parent f9752d5 commit 20c9b98
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
44 changes: 42 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package blockchain

import (
"bytes"
"crypto/ecdsa"
"crypto/sha256"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -80,6 +82,10 @@ func (bc *Blockchain) TransactionPool() []*Transaction {
return bc.transactionPool
}

func (bc *Blockchain) ClearTransactionPool() {
bc.transactionPool = bc.transactionPool[:0]
}

func (bc *Blockchain) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Blocks []*Block `json:"chain"`
Expand All @@ -92,13 +98,47 @@ func (bc *Blockchain) CreateBlock(nonce int, previousHash [32]byte) *Block {
b := NewBlock(nonce, previousHash, bc.transactionPool)
bc.chain = append(bc.chain, b)
bc.transactionPool = []*Transaction{}

for _, n := range bc.neighbors {
endpoint := fmt.Sprintf("http://%s/transactions", n)
client := &http.Client{}
req, _ := http.NewRequest("DELETE", endpoint, nil)
resp, err := client.Do(req)
if err != nil {
log.Printf("%v", resp)
}
}

return b
}

func (bc *Blockchain) CreateTransaction(sender string, recipient string, value float32, senderPublicKey *ecdsa.PublicKey, s *utils.Signature) bool {
isTransacted := bc.AddTransaction(sender, recipient, value, senderPublicKey, s)
// TODO
// sync

if isTransacted {
for _, n := range bc.neighbors {
publicKeyStr := fmt.Sprintf("%064x%064x", senderPublicKey.X.Bytes(), senderPublicKey.Y.Bytes())
signatureStr := s.String()
bt := &TransactionRequest{
&sender,
&recipient,
&publicKeyStr,
&value,
&signatureStr}

m, _ := json.Marshal(bt)
buf := bytes.NewBuffer(m)
endpoint := fmt.Sprintf("http://%s/transactions", n)

client := &http.Client{}
req, _ := http.NewRequest("PUT", endpoint, buf)
resp, err := client.Do(req)
if err != nil {
log.Printf("%v", resp)
}
}
}

return isTransacted
}

Expand Down
34 changes: 34 additions & 0 deletions blockchain_node/blockchain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ func (bcn *BlockchainNode) Transactions(w http.ResponseWriter, r *http.Request)
}
io.WriteString(w, string(m))

case http.MethodPut:
decoder := json.NewDecoder((r.Body))
var t blockchain.TransactionRequest
err := decoder.Decode(&t)
if err != nil {
log.Printf("ERROR: %v", err)
io.WriteString(w, string(utils.JsonStatus("fail")))
return
}
if !t.Validate() {
log.Println("ERROR: missing field(S)")
io.WriteString(w, string(utils.JsonStatus("fail")))
return
}
publicKey := utils.String2PublicKey(*t.SenderPublicKey)
signature := utils.String2Signature(*t.Signature)
bc := bcn.GetBlockchain()
isUpdated := bc.AddTransaction(*t.SenderBlockchainAddress, *t.RecipientBlockchainAddress, *t.Value, publicKey, signature)

w.Header().Add("Content-Type", "application/json")
var m []byte
if !isUpdated {
w.WriteHeader(http.StatusBadRequest)
m = utils.JsonStatus("fail")
} else {
m = utils.JsonStatus("success")
}
io.WriteString(w, string(m))

case http.MethodDelete:
bc := bcn.GetBlockchain()
bc.ClearTransactionPool()
io.WriteString(w, string(utils.JsonStatus("success")))

default:
log.Println("ERROR: Invalid HTTP method")
w.WriteHeader(http.StatusBadRequest)
Expand Down

0 comments on commit 20c9b98

Please sign in to comment.