-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (95 loc) · 2.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
)
// Transaction represents a simple transaction with a sender, receiver, and an amount.
type Transaction struct {
Sender string
Receiver string
Amount float64
}
// Block represents a block in the blockchain.
type Block struct {
Index int
Timestamp int64
Transactions []Transaction
PrevHash string
Hash string
}
// Blockchain is a series of blocks.
type Blockchain struct {
Blocks []*Block
}
// NewBlock creates a new block with the given transactions.
func NewBlock(index int, transactions []Transaction, prevHash string) *Block {
block := &Block{
Index: index,
Timestamp: time.Now().Unix(),
Transactions: transactions,
PrevHash: prevHash,
}
block.Hash = block.calculateHash()
return block
}
// serializeTransactions serializes the transactions in a block ensuring that it works correctly even when the block is empty.
func (b *Block) serializeTransactions() string {
serialized, err := json.Marshal(b.Transactions)
if err != nil {
return "[]"
}
return string(serialized)
}
// calculateHash generates a SHA-256 hash for the block.
func (b *Block) calculateHash() string {
serializedTransactions := b.serializeTransactions()
blockData := fmt.Sprintf("%d%d%s%s", b.Index, b.Timestamp, serializedTransactions, b.PrevHash)
hash := sha256.Sum256([]byte(blockData))
return hex.EncodeToString(hash[:])
}
// AddBlock appends a new block with the given transactions to the blockchain.
func (bc *Blockchain) AddBlock(transactions []Transaction) {
prevBlock := bc.Blocks[len(bc.Blocks)-1]
newBlock := NewBlock(prevBlock.Index+1, transactions, prevBlock.Hash)
bc.Blocks = append(bc.Blocks, newBlock)
}
// NewBlockchain creates a new blockchain with a genesis block.
func NewBlockchain() *Blockchain {
genesisBlock := NewBlock(0, []Transaction{}, "0")
return &Blockchain{Blocks: []*Block{genesisBlock}}
}
func main() {
// Initialize a new blockchain.
blockchain := NewBlockchain()
// Add a block with a sample transaction.
blockchain.AddBlock([]Transaction{
{
Sender: "Alice",
Receiver: "Bob",
Amount: 50,
},
})
// Add a block with a sample transaction.
blockchain.AddBlock([]Transaction{
{
Sender: "Bob",
Receiver: "Alice",
Amount: 25,
},
})
// Print the blocks in the blockchain.
for _, block := range blockchain.Blocks {
fmt.Printf("Index: %d\n", block.Index)
fmt.Printf("Timestamp: %d\n", block.Timestamp)
fmt.Printf("Prev. hash: %s\n", block.PrevHash)
fmt.Printf("Hash: %s\n", block.Hash)
fmt.Println("Transactions:")
for _, tx := range block.Transactions {
fmt.Printf(" Sender: %s, Receiver: %s, Amount: %.2f\n", tx.Sender, tx.Receiver, tx.Amount)
}
fmt.Println()
}
}