-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnode.go
More file actions
158 lines (133 loc) · 3.62 KB
/
node.go
File metadata and controls
158 lines (133 loc) · 3.62 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
package cmd
import (
"github.com/liamzebedee/tinychain-go/core"
"github.com/liamzebedee/tinychain-go/core/nakamoto"
"github.com/liamzebedee/tinychain-go/explorer"
"github.com/urfave/cli/v2"
"database/sql"
"fmt"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
)
type MockStateMachine struct{}
func newMockStateMachine() *MockStateMachine {
return &MockStateMachine{}
}
func (m *MockStateMachine) VerifyTx(tx nakamoto.RawTransaction) error {
return nil
}
func newBlockdag(dbPath string, conf nakamoto.ConsensusConfig) (nakamoto.BlockDAG, nakamoto.ConsensusConfig, *sql.DB) {
// TODO validate connection string.
fmt.Println("database path: ", dbPath)
db, err := nakamoto.OpenDB(dbPath)
if err != nil {
panic(err)
}
_, err = db.Exec("PRAGMA journal_mode = WAL;")
if err != nil {
panic(err)
}
stateMachine := newMockStateMachine()
blockdag, err := nakamoto.NewBlockDAGFromDB(db, stateMachine, conf)
if err != nil {
panic(err)
}
return blockdag, conf, db
}
func getMinerWallet(db *sql.DB) (*core.Wallet, error) {
walletsStore, err := nakamoto.LoadDataStore[nakamoto.WalletsStore](db, "wallets")
if err != nil {
return nil, err
}
if 0 == len(walletsStore.Wallets) {
wallet, err := core.CreateRandomWallet()
if err != nil {
return nil, err
}
walletsStore.Wallets = append(walletsStore.Wallets, nakamoto.UserWallet{
Label: "miner",
PrivateKeyString: wallet.PrvkeyStr(),
})
err = nakamoto.SaveDataStore(db, "wallets", *walletsStore)
if err != nil {
return nil, err
}
}
minerWallet, err := core.WalletFromPrivateKey(walletsStore.Wallets[0].PrivateKeyString)
if err != nil {
return nil, err
}
return minerWallet, nil
}
func RunNode(cmdCtx *cli.Context) error {
port := cmdCtx.String("port")
dbPath := cmdCtx.String("db")
bootstrapPeers := cmdCtx.String("peers")
runMiner := cmdCtx.Bool("miner")
runExplorer := cmdCtx.Bool("explorer")
network := cmdCtx.String("network")
graffitiTag := cmdCtx.String("miner-tag")
if network == "" {
network = "testnet1"
}
// DAG.
networks := nakamoto.GetNetworks()
net, ok := networks[network]
if !ok {
availableNetworks := []string{}
for k := range networks {
availableNetworks = append(availableNetworks, k)
}
fmt.Printf("Available networks: %s\n", strings.Join(availableNetworks, ", "))
return fmt.Errorf("Unknown network: %s", network)
}
dag, _, db := newBlockdag(dbPath, net.ConsensusConfig)
// Miner.
minerWallet, err := getMinerWallet(db)
if err != nil {
return err
}
fmt.Printf("Miner wallet: %x\n", minerWallet.PubkeyBytes())
miner := nakamoto.NewMiner(dag, minerWallet)
miner.GraffitiTag = nakamoto.StringToBytes32(graffitiTag)
// Peer.
peer := nakamoto.NewPeerCore(nakamoto.NewPeerConfig("0.0.0.0", port, []string{}))
// Create the node.
node := nakamoto.NewNode(&dag, miner, peer)
// Handle process signals.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Shutting down...")
node.Shutdown()
os.Exit(1)
}()
// Bootstrap the node.
if bootstrapPeers != "" {
peerAddresses := []string{}
// Split the comma-separated list of peer addresses.
peerlist := strings.Split(bootstrapPeers, ",")
for _, peerAddress := range peerlist {
// Validate URL.
_, err := url.ParseRequestURI(peerAddress)
if err != nil {
return fmt.Errorf("Invalid peer address: %s", peerAddress)
}
peerAddresses = append(peerAddresses, peerAddress)
}
node.Peer.Bootstrap(peerAddresses)
}
if runMiner {
go node.Miner.Start(-1)
}
if runExplorer {
expl := explorer.NewBlockExplorerServer(&dag, 9000)
go expl.Start()
}
node.Start()
return nil
}