-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (70 loc) · 1.98 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
package main
import (
"fmt"
"github.com/urfave/cli/v2"
//"net/http"
"os"
)
func main() {
app := &cli.App{
Name: "goblockchain",
Usage: "Interface for running a blockchain node",
Action: func(c *cli.Context) error {
start("blockchain.db", "account")
return nil
},
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func start(dbFile string, accountFile string) {
fmt.Println("Starting")
var miner *Account
if accRaw, err := os.ReadFile(accountFile); err != nil {
fmt.Println("No account found")
if miner, err = NewAccount(); err != nil {
panic(err)
} else {
fmt.Println("Generated a new account")
os.WriteFile(accountFile, miner.Serialize(), 0644)
}
} else {
miner = AccountDeserialize(accRaw)
fmt.Printf("Account '%x' opened from file\n", miner.Id)
}
bc, err := NewBlockchain(dbFile, miner)
if err != nil {
panic(err)
}
defer bc.Close()
alice, _ := NewAccount()
bob, _ := NewAccount()
bc.AddKey(alice.PublicKey)
bc.AddKey(bob.PublicKey)
fmt.Println("Alice has: " + fmt.Sprint(bc.GetUTxOsForUser(alice.Id).Balance()))
fmt.Println("Bob has: " + fmt.Sprint(bc.GetUTxOsForUser(bob.Id).Balance()))
fmt.Println("The miner has: " + fmt.Sprint(bc.GetUTxOsForUser(miner.Id).Balance()))
if err = bc.Send(miner, alice.Id, 50); err != nil {
panic(err)
}
bc.MineNext()
fmt.Println("Alice has: " + fmt.Sprint(bc.GetUTxOsForUser(alice.Id).Balance()))
fmt.Println("Bob has: " + fmt.Sprint(bc.GetUTxOsForUser(bob.Id).Balance()))
fmt.Println("The miner has: " + fmt.Sprint(bc.GetUTxOsForUser(miner.Id).Balance()))
if err = bc.Send(alice, bob.Id, 30); err != nil {
panic(err)
}
block, err := bc.MineNext()
if err != nil {
panic(err)
}
err = bc.VerifyBlock(block)
if err != nil {
panic(err)
}
fmt.Println("Alice has: " + fmt.Sprint(bc.GetUTxOsForUser(alice.Id).Balance()))
fmt.Println("Bob has: " + fmt.Sprint(bc.GetUTxOsForUser(bob.Id).Balance()))
fmt.Println("The miner has: " + fmt.Sprint(bc.GetUTxOsForUser(miner.Id).Balance()))
}