Skip to content

Commit

Permalink
added method to resolve conflicts betwen nodes about the actual lengt…
Browse files Browse the repository at this point in the history
…h of the blockchain
  • Loading branch information
jagottsicher committed Feb 23, 2024
1 parent e8d2732 commit 3890804
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockchain

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -62,3 +63,24 @@ func (b *Block) MarshalJSON() ([]byte, error) {
Transactions: b.transactions,
})
}

func (b *Block) UnmarshalJSON(data []byte) error {
var previousHash string
v := &struct {
Timestamp *int64 `json: "timestamp"`
Nonce *int `json: "nonce"`
PreviousHash *string `json: "previous_hash"`
Transactions *[]*Transaction `json: "transactions"`
}{
Timestamp: &b.timestamp,
Nonce: &b.nonce,
PreviousHash: &previousHash,
Transactions: &b.transactions,
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
ph, _ := hex.DecodeString(*v.PreviousHash)
copy(b.previousHash[:], ph[:32])
return nil
}
46 changes: 46 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func NewBlockchain(blockchainAddress string, port uint16) *Blockchain {
return bc
}

func (bc *Blockchain) Chain() []*Block {
return bc.chain
}

func (bc *Blockchain) Run() {
bc.StartSyncNeighbors()
}
Expand Down Expand Up @@ -268,6 +272,36 @@ func (bc *Blockchain) ValidChain(chain []*Block) bool {
return true
}

func (bc *Blockchain) ResolveConflicts() bool {
var longestChain []*Block = nil
maxLength := len(bc.chain)

for _, n := range bc.neighbors {
endpoint := fmt.Sprintf("http://%s/chain", n)
resp, _ := http.Get(endpoint)
if resp.StatusCode == 200 {
var bcResponse Blockchain
decoder := json.NewDecoder(resp.Body)
_ = decoder.Decode(&bcResponse)

chain := bcResponse.Chain()

if len(chain) > maxLength && bc.ValidChain(chain) {
maxLength = len(chain)
longestChain = chain
}
}
}

if longestChain != nil {
bc.chain = longestChain
log.Printf("Resolved conflicts: blockchain was replaced")
return true
}
log.Printf("Resolved conflicts: blockchain was nor replaced")
return false
}

type AmountResponse struct {
Amount float32 `json:"amount"`
}
Expand All @@ -279,3 +313,15 @@ func (ar *AmountResponse) MarshalJSON() ([]byte, error) {
Amount: ar.Amount,
})
}

func (bc *Blockchain) UnmarshalJSON(data []byte) error {
v := &struct {
Blocks *[]*Block `json: "chain"`
}{
Blocks: &bc.chain,
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
return nil
}
16 changes: 16 additions & 0 deletions blockchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
})
}

func (t *Transaction) UnmarshalJSON(data []byte) error {
v := &struct {
Sender *string `json:"sender_blockchain_address"`
Recipient *string `json:"recipient_blockchain_address"`
Value *float32 `json:"value"`
}{
Sender: &t.senderBlockchainAddress,
Recipient: &t.recipientBlockchainAddress,
Value: &t.value,
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
return nil
}

type TransactionRequest struct {
SenderBlockchainAddress *string `json:"sender_blockchain_address"`
RecipientBlockchainAddress *string `json:"receipient_blockchain_address"`
Expand Down

0 comments on commit 3890804

Please sign in to comment.