-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (71 loc) · 3.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
package main
import (
"./block"
"./block_chain"
"./transaction"
"./wallet"
"fmt"
"strings"
)
func main() {
fmt.Println("BlockChain examples")
partSecond()
}
func partSecond() {
fmt.Printf("%s example part two \n", strings.Repeat("=", 25))
minerWallet := wallet.NewWallet()
walletUserA := wallet.NewWallet()
walletUserB := wallet.NewWallet()
//walletUserC := wallet.NewWallet()
chain := blockchain.New()
t := wallet.NewTransaction(minerWallet.PrivateKey(), minerWallet.PublicKey(), minerWallet.BlockchainAddress(), walletUserB.BlockchainAddress(), 1000)
t2 := wallet.NewTransaction(walletUserB.PrivateKey(), walletUserB.PublicKey(), walletUserB.BlockchainAddress(), walletUserA.BlockchainAddress(), 100)
chain.BlockList = append(chain.BlockList, block.CreateGenesisBlock([]*transaction.Transaction{transaction.New("genesis", minerWallet.BlockchainAddress(), 3000)}))
chain.Mining()
isOk := chain.AddTransaction(minerWallet.BlockchainAddress(), walletUserB.BlockchainAddress(), 1000, minerWallet.PublicKey(), t.GenerateSignature())
fmt.Printf("is ok, %v \n", isOk)
chain.Mining()
isOk = chain.AddTransaction(walletUserB.BlockchainAddress(), walletUserA.BlockchainAddress(), 100, walletUserB.PublicKey(), t2.GenerateSignature())
fmt.Printf("is ok, %v \n", isOk)
chain.Mining()
fmt.Printf("A %.1f\n", chain.CalculateTotalAmount(walletUserA.BlockchainAddress()))
fmt.Printf("B %.1f\n", chain.CalculateTotalAmount(walletUserB.BlockchainAddress()))
fmt.Printf("M %.1f\n", chain.CalculateTotalAmount(minerWallet.BlockchainAddress()))
fmt.Printf("%s example part two complete\n", strings.Repeat("=", 25))
}
func partOne() {
fmt.Printf("%s wallet example part one \n", strings.Repeat("=", 25))
chain := blockchain.New()
chain.BlockList = append(chain.BlockList, block.CreateGenesisBlock([]*transaction.Transaction{transaction.New("A", "B", 3)}))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("B", "B", 3))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("B", "B", 2))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("B", "B", 5))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("D", "B", 5))
chain.Mining()
chain.TransactionPool = append(chain.TransactionPool, transaction.New("C", "B", 3))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("C", "B", 2))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("C", "B", 5))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("FF", "B", 5))
chain.Mining()
chain.TransactionPool = append(chain.TransactionPool, transaction.New("E", "B", 3))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("E", "B", 2))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("E", "B", 5))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("ED", "B", 5))
chain.Mining()
chain.TransactionPool = append(chain.TransactionPool, transaction.New("F", "B", 3))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("F", "B", 2))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("G", "B", 5))
chain.TransactionPool = append(chain.TransactionPool, transaction.New("H", "B", 5))
chain.Mining()
chain.Validate()
chain.Print()
fmt.Printf("%s chain example one Done \n", strings.Repeat("=", 25))
fmt.Println("hello")
w := wallet.NewWallet()
fmt.Println(w.PrivateKeyStr())
fmt.Println(w.PublicKeyStr())
fmt.Printf("wallet address -> %s \n", w.BlockchainAddress())
t := wallet.NewTransaction(w.PrivateKey(), w.PublicKey(), w.BlockchainAddress(), "recipientAddress", 1.42)
fmt.Printf("signature %s \n", t.GenerateSignature())
fmt.Printf("%s wallet example one Done \n", strings.Repeat("=", 25))
}