-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistence.go
More file actions
85 lines (75 loc) · 1.68 KB
/
Copy pathpersistence.go
File metadata and controls
85 lines (75 loc) · 1.68 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
// ABOUTME: Node persistence to and from a JSON file, capturing the
// ABOUTME: chain, mempool, and miner identity for restart recovery.
package quark
import (
"crypto/x509"
"encoding/json"
"errors"
"os"
)
type nodeFile struct {
PrivateKey []byte `json:"private_key"`
Chain *BlockChain `json:"chain"`
Mempool []*Transaction `json:"mempool"`
}
func (n *Node) Save(path string) error {
n.mu.Lock()
defer n.mu.Unlock()
keyBytes := x509.MarshalPKCS1PrivateKey(n.Miner.Wallet.privateKey)
file := &nodeFile{
PrivateKey: keyBytes,
Chain: n.Chain,
Mempool: n.Mempool.Pending(),
}
data, err := json.MarshalIndent(file, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0600)
}
func LoadNode(path string) (*Node, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var file nodeFile
if err := json.Unmarshal(data, &file); err != nil {
return nil, err
}
if file.Chain == nil {
return nil, errors.New("loaded node has no chain")
}
if file.Chain.Config == nil {
file.Chain.Config = DefaultDifficultyConfig()
}
priv, err := x509.ParsePKCS1PrivateKey(file.PrivateKey)
if err != nil {
return nil, err
}
wallet := &Wallet{
privateKey: priv,
publicKey: &priv.PublicKey,
}
mempool := NewMempool()
for _, tx := range file.Mempool {
if err := mempool.Add(tx); err != nil {
return nil, err
}
}
node := &Node{
Chain: file.Chain,
Mempool: mempool,
Miner: &Miner{Wallet: wallet},
}
return node, nil
}
func LoadOrCreateNode(path string) (*Node, error) {
_, err := os.Stat(path)
if err == nil {
return LoadNode(path)
}
if !os.IsNotExist(err) {
return nil, err
}
return NewNode()
}