-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
451 lines (377 loc) · 10.7 KB
/
main.go
File metadata and controls
451 lines (377 loc) · 10.7 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"sync"
"time"
)
// Transaction represents a blockchain transaction
type Transaction struct {
Sender string
Receiver string
Amount float64
Timestamp int64
}
// NewTransaction creates a new transaction
func NewTransaction(sender, receiver string, amount float64) Transaction {
return Transaction{
Sender: sender,
Receiver: receiver,
Amount: amount,
Timestamp: time.Now().Unix(),
}
}
// CalculateHash returns the hash of a transaction
func (tx *Transaction) CalculateHash() string {
record := fmt.Sprintf("%s%s%f%d", tx.Sender, tx.Receiver, tx.Amount, tx.Timestamp)
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
// Block represents a block in the blockchain
type Block struct {
Index int64
Timestamp int64
Transactions []Transaction
PreviousHash string
Hash string
Nonce int64
}
// NewBlock creates a new block
func NewBlock(index int64, transactions []Transaction, previousHash string) Block {
block := Block{
Index: index,
Timestamp: time.Now().Unix(),
Transactions: transactions,
PreviousHash: previousHash,
Nonce: 0,
}
block.Hash = block.CalculateHash()
return block
}
// CalculateHash returns the hash of a block
func (b *Block) CalculateHash() string {
record := fmt.Sprintf("%d%d%s%d", b.Index, b.Timestamp, b.PreviousHash, b.Nonce)
for _, tx := range b.Transactions {
record += tx.CalculateHash()
}
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
// MineBlock mines a block with the given difficulty
func (b *Block) MineBlock(difficulty int) {
target := ""
for i := 0; i < difficulty; i++ {
target += "0"
}
for b.Hash[:difficulty] != target {
b.Nonce++
b.Hash = b.CalculateHash()
}
fmt.Printf("Block mined: %s\n", b.Hash)
}
// Blockchain represents the blockchain
type Blockchain struct {
Chain []Block
PendingTransactions []Transaction
Difficulty int
MiningReward float64
mu sync.Mutex
}
// NewBlockchain creates a new blockchain
func NewBlockchain(difficulty int, miningReward float64) *Blockchain {
blockchain := &Blockchain{
Chain: []Block{},
PendingTransactions: []Transaction{},
Difficulty: difficulty,
MiningReward: miningReward,
}
blockchain.CreateGenesisBlock()
return blockchain
}
// CreateGenesisBlock creates the genesis block
func (bc *Blockchain) CreateGenesisBlock() {
genesisBlock := NewBlock(0, []Transaction{}, "0")
bc.Chain = append(bc.Chain, genesisBlock)
}
// GetLatestBlock returns the latest block in the chain
func (bc *Blockchain) GetLatestBlock() Block {
return bc.Chain[len(bc.Chain)-1]
}
// AddTransaction adds a transaction to the pending transactions
func (bc *Blockchain) AddTransaction(transaction Transaction) {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.PendingTransactions = append(bc.PendingTransactions, transaction)
}
// MinePendingTransactions mines the pending transactions
func (bc *Blockchain) MinePendingTransactions(miningRewardAddress string) {
bc.mu.Lock()
defer bc.mu.Unlock()
// Create a reward transaction for the miner
rewardTx := NewTransaction("BLOCKCHAIN", miningRewardAddress, bc.MiningReward)
transactionsToMine := append(bc.PendingTransactions, rewardTx)
// Create a new block
newBlock := NewBlock(
int64(len(bc.Chain)),
transactionsToMine,
bc.GetLatestBlock().Hash,
)
// Mine the block
newBlock.MineBlock(bc.Difficulty)
// Add the block to the chain
bc.Chain = append(bc.Chain, newBlock)
// Clear the pending transactions
bc.PendingTransactions = []Transaction{}
}
// GetBalance returns the balance of an address
func (bc *Blockchain) GetBalance(address string) float64 {
bc.mu.Lock()
defer bc.mu.Unlock()
balance := 0.0
for _, block := range bc.Chain {
for _, tx := range block.Transactions {
if tx.Sender == address {
balance -= tx.Amount
}
if tx.Receiver == address {
balance += tx.Amount
}
}
}
return balance
}
// IsChainValid checks if the chain is valid
func (bc *Blockchain) IsChainValid() bool {
bc.mu.Lock()
defer bc.mu.Unlock()
for i := 1; i < len(bc.Chain); i++ {
currentBlock := bc.Chain[i]
previousBlock := bc.Chain[i-1]
// Verify the hash of the current block
if currentBlock.Hash != currentBlock.CalculateHash() {
return false
}
// Verify the previous hash reference
if currentBlock.PreviousHash != previousBlock.Hash {
return false
}
}
return true
}
// TransactionProcessor processes transactions concurrently
type TransactionProcessor struct {
Blockchain *Blockchain
TxPool []Transaction
TxChan chan Transaction
mu sync.Mutex
}
// NewTransactionProcessor creates a new transaction processor
func NewTransactionProcessor(blockchain *Blockchain) *TransactionProcessor {
return &TransactionProcessor{
Blockchain: blockchain,
TxPool: []Transaction{},
TxChan: make(chan Transaction, 100),
mu: sync.Mutex{},
}
}
// AddTransaction adds a transaction to the pool
func (tp *TransactionProcessor) AddTransaction(transaction Transaction) {
tp.TxChan <- transaction
}
// ProcessTransactions processes transactions from the channel
func (tp *TransactionProcessor) ProcessTransactions() {
go func() {
for {
tx := <-tp.TxChan
tp.mu.Lock()
tp.TxPool = append(tp.TxPool, tx)
tp.mu.Unlock()
// If we have enough transactions, process them
if len(tp.TxPool) >= 5 {
tp.FlushTransactions()
}
}
}()
// Periodically flush transactions even if we don't have enough
go func() {
for {
time.Sleep(5 * time.Second)
tp.FlushTransactions()
}
}()
}
// FlushTransactions flushes transactions to the blockchain
func (tp *TransactionProcessor) FlushTransactions() {
tp.mu.Lock()
defer tp.mu.Unlock()
if len(tp.TxPool) == 0 {
return
}
// Add all transactions to the blockchain
for _, tx := range tp.TxPool {
tp.Blockchain.AddTransaction(tx)
}
// Clear the pool
tp.TxPool = []Transaction{}
}
// Miner mines blocks
type Miner struct {
Blockchain *Blockchain
MiningAddress string
StopChan chan struct{}
IsMining bool
mu sync.Mutex
}
// NewMiner creates a new miner
func NewMiner(blockchain *Blockchain, miningAddress string) *Miner {
return &Miner{
Blockchain: blockchain,
MiningAddress: miningAddress,
StopChan: make(chan struct{}),
IsMining: false,
mu: sync.Mutex{},
}
}
// StartMining starts mining
func (m *Miner) StartMining() {
m.mu.Lock()
if m.IsMining {
m.mu.Unlock()
return
}
m.IsMining = true
m.mu.Unlock()
go func() {
for {
select {
case <-m.StopChan:
m.mu.Lock()
m.IsMining = false
m.mu.Unlock()
return
default:
// Check if there are pending transactions
m.Blockchain.mu.Lock()
pendingCount := len(m.Blockchain.PendingTransactions)
m.Blockchain.mu.Unlock()
if pendingCount > 0 {
fmt.Printf("Mining block with %d transactions\n", pendingCount)
m.Blockchain.MinePendingTransactions(m.MiningAddress)
} else {
time.Sleep(3 * time.Second)
}
}
}
}()
}
// StopMining stops mining
func (m *Miner) StopMining() {
m.mu.Lock()
defer m.mu.Unlock()
if m.IsMining {
m.StopChan <- struct{}{}
}
}
// BlockchainNetwork represents a blockchain network
type BlockchainNetwork struct {
Blockchain *Blockchain
TransactionProcessor *TransactionProcessor
Miners map[string]*Miner
mu sync.Mutex
}
// NewBlockchainNetwork creates a new blockchain network
func NewBlockchainNetwork(difficulty int, miningReward float64) *BlockchainNetwork {
blockchain := NewBlockchain(difficulty, miningReward)
transactionProcessor := NewTransactionProcessor(blockchain)
return &BlockchainNetwork{
Blockchain: blockchain,
TransactionProcessor: transactionProcessor,
Miners: make(map[string]*Miner),
mu: sync.Mutex{},
}
}
// Start starts the network
func (bn *BlockchainNetwork) Start() {
bn.TransactionProcessor.ProcessTransactions()
bn.mu.Lock()
defer bn.mu.Unlock()
for _, miner := range bn.Miners {
miner.StartMining()
}
}
// AddMiner adds a miner to the network
func (bn *BlockchainNetwork) AddMiner(id, address string) {
bn.mu.Lock()
defer bn.mu.Unlock()
miner := NewMiner(bn.Blockchain, address)
bn.Miners[id] = miner
}
// SubmitTransaction submits a transaction to the network
func (bn *BlockchainNetwork) SubmitTransaction(sender, receiver string, amount float64) {
transaction := NewTransaction(sender, receiver, amount)
bn.TransactionProcessor.AddTransaction(transaction)
}
// GetBalance returns the balance of an address
func (bn *BlockchainNetwork) GetBalance(address string) float64 {
return bn.Blockchain.GetBalance(address)
}
// PrintChain prints the blockchain
func (bn *BlockchainNetwork) PrintChain() {
fmt.Printf("Blockchain with %d blocks\n", len(bn.Blockchain.Chain))
for i, block := range bn.Blockchain.Chain {
// Safely get hash prefixes
hashPrefix := block.Hash
if len(hashPrefix) > 10 {
hashPrefix = hashPrefix[:10]
}
prevHashPrefix := block.PreviousHash
if len(prevHashPrefix) > 10 {
prevHashPrefix = prevHashPrefix[:10]
}
fmt.Printf("Block %d: Index=%d, Hash=%s, PrevHash=%s, Nonce=%d, Transactions=%d\n",
i, block.Index, hashPrefix, prevHashPrefix, block.Nonce, len(block.Transactions))
}
}
func main() {
// Create a new blockchain network with difficulty 2 and mining reward 100
network := NewBlockchainNetwork(2, 100.0)
// Add miners
network.AddMiner("miner1", "miner1_address")
network.AddMiner("miner2", "miner2_address")
// Start the network
network.Start()
// Create a wait group to wait for all goroutines to finish
var wg sync.WaitGroup
wg.Add(1)
// Spawn a goroutine to simulate transactions
go func() {
defer wg.Done()
// Submit some transactions
network.SubmitTransaction("alice", "bob", 50.0)
network.SubmitTransaction("bob", "charlie", 25.0)
network.SubmitTransaction("charlie", "alice", 10.0)
// Wait for mining to happen
time.Sleep(10 * time.Second)
// Submit more transactions
network.SubmitTransaction("alice", "charlie", 15.0)
network.SubmitTransaction("bob", "alice", 5.0)
// Wait for mining to happen
time.Sleep(10 * time.Second)
// Print the chain
network.PrintChain()
// Print balances
fmt.Printf("Alice's balance: %f\n", network.GetBalance("alice"))
fmt.Printf("Bob's balance: %f\n", network.GetBalance("bob"))
fmt.Printf("Charlie's balance: %f\n", network.GetBalance("charlie"))
fmt.Printf("Miner1's balance: %f\n", network.GetBalance("miner1_address"))
fmt.Printf("Miner2's balance: %f\n", network.GetBalance("miner2_address"))
}()
// Wait for the simulation to complete
wg.Wait()
fmt.Println("Blockchain simulation completed")
}