-
Notifications
You must be signed in to change notification settings - Fork 211
/
nipost_state.go
165 lines (139 loc) · 4.29 KB
/
nipost_state.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
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
package localsql
import (
"encoding/binary"
"fmt"
"hash/crc64"
"io"
"os"
"path/filepath"
"github.com/natefinch/atomic"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
)
const (
challengeFilename = "nipost_challenge.bin"
builderFilename = "nipost_builder_state.bin"
postFilename = "post.bin"
)
func write(path string, data []byte) error {
tmpName := fmt.Sprintf("%s.tmp", path)
tmp, err := os.Create(tmpName)
if err != nil {
return fmt.Errorf("create temporary file %s: %w", tmpName, err)
}
defer tmp.Close()
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
w := io.MultiWriter(tmp, checksum)
if _, err := w.Write(data); err != nil {
return fmt.Errorf("write data %v: %w", tmp.Name(), err)
}
crc := make([]byte, crc64.Size)
binary.BigEndian.PutUint64(crc, checksum.Sum64())
if _, err := tmp.Write(crc); err != nil {
return fmt.Errorf("write checksum %s: %w", tmp.Name(), err)
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("failed to close tmp file %s: %w", tmp.Name(), err)
}
if err := atomic.ReplaceFile(tmp.Name(), path); err != nil {
return fmt.Errorf("save file from %s, %s: %w", tmp.Name(), path, err)
}
return nil
}
func read(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open file %s: %w", path, err)
}
defer file.Close()
fInfo, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("failed to get file info %s: %w", path, err)
}
if fInfo.Size() < crc64.Size {
return nil, fmt.Errorf("file %s is too small", path)
}
data := make([]byte, fInfo.Size()-crc64.Size)
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
if _, err := io.TeeReader(file, checksum).Read(data); err != nil {
return nil, fmt.Errorf("read file %s: %w", path, err)
}
saved := make([]byte, crc64.Size)
if _, err := file.Read(saved); err != nil {
return nil, fmt.Errorf("read checksum %s: %w", path, err)
}
savedChecksum := binary.BigEndian.Uint64(saved)
if savedChecksum != checksum.Sum64() {
return nil, fmt.Errorf("wrong checksum 0x%X, computed 0x%X", savedChecksum, checksum.Sum64())
}
return data, nil
}
func load(filename string, dst scale.Decodable) error {
data, err := read(filename)
if err != nil {
return fmt.Errorf("reading file: %w", err)
}
if err := codec.Decode(data, dst); err != nil {
return fmt.Errorf("decoding: %w", err)
}
return nil
}
func save(filename string, src scale.Encodable) error {
if _, err := os.Stat(filepath.Dir(filename)); err != nil {
return err
}
data, err := codec.Encode(src)
if err != nil {
return fmt.Errorf("encoding: %w", err)
}
if err := write(filename, data); err != nil {
return fmt.Errorf("writing file: %w", err)
}
return nil
}
func loadNipostChallenge(dir string) (*types.NIPostChallenge, error) {
var ch types.NIPostChallenge
if err := load(filepath.Join(dir, challengeFilename), &ch); err != nil {
return nil, fmt.Errorf("loading nipost challenge: %w", err)
}
return &ch, nil
}
func discardNipostChallenge(dir string) error {
filename := filepath.Join(dir, challengeFilename)
backupName := fmt.Sprintf("%s.bak", filename)
if err := atomic.ReplaceFile(filename, backupName); err != nil {
return fmt.Errorf("discarding nipost challenge: %w", err)
}
return nil
}
func loadBuilderState(dir string) (*NIPostBuilderState, error) {
var state NIPostBuilderState
if err := load(filepath.Join(dir, builderFilename), &state); err != nil {
return nil, fmt.Errorf("loading builder state: %w", err)
}
return &state, nil
}
func discardBuilderState(dir string) error {
filename := filepath.Join(dir, builderFilename)
backupName := fmt.Sprintf("%s.bak", filename)
if err := atomic.ReplaceFile(filename, backupName); err != nil {
return fmt.Errorf("discarding nipost builder state: %w", err)
}
return nil
}
func loadPost(dir string) (*types.Post, error) {
var post types.Post
if err := load(filepath.Join(dir, postFilename), &post); err != nil {
return nil, fmt.Errorf("loading post: %w", err)
}
return &post, nil
}
func discardPost(dir string) error {
filename := filepath.Join(dir, postFilename)
backupName := fmt.Sprintf("%s.bak", filename)
if err := atomic.ReplaceFile(filename, backupName); err != nil {
return fmt.Errorf("discarding post: %w", err)
}
return nil
}