This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot.go
192 lines (159 loc) · 4.06 KB
/
snapshot.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package snapshot
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/c3systems/c3-go/common/fileutil"
"github.com/c3systems/c3-go/common/hexutil"
"github.com/c3systems/c3-go/common/stringutil"
"github.com/c3systems/c3-go/core/docker"
"github.com/c3systems/c3-go/core/miner"
"github.com/c3systems/c3-go/core/p2p"
"github.com/c3systems/c3-go/core/sandbox"
"github.com/c3systems/c3-go/node/store"
"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
)
// Service ...
type Service struct {
Mempool store.Interface
P2P p2p.Interface
Sandbox sandbox.Interface
}
// Config ...
type Config struct {
Mempool store.Interface
P2P p2p.Interface
}
// New ...
func New(cfg *Config) *Service {
sb := sandbox.New(nil)
return &Service{
P2P: cfg.P2P,
Mempool: cfg.Mempool,
Sandbox: sb,
}
}
// Snapshot ...
func (s *Service) Snapshot(imageHash string, stateBlockNumber int) (string, error) {
// stateFileName ...
var stateFileName = "state.txt"
headBlock, err := s.Mempool.GetHeadBlock()
if err != nil {
return "", err
}
prevStateBlock, err := s.P2P.FetchMostRecentStateBlock(imageHash, &headBlock)
if err != nil {
return "", err
}
if prevStateBlock == nil {
return "", errors.New("state block not found")
}
var fileNames []string
defer cleanupDirs(&fileNames)
defer cleanupFiles(&fileNames)
ts := time.Now().Unix()
minr, err := miner.New(&miner.Props{
Context: context.Background(),
PreviousBlock: &headBlock,
P2P: s.P2P,
PendingTransactions: nil,
})
if err != nil {
return "", err
}
diffs, err := minr.GatherDiffs(prevStateBlock)
if err != nil {
return "", err
}
// debug logs
for i := range diffs {
log.Printf("diff %v\n%s", i, diffs[i].Props().Data)
}
var genesisState []byte
state, err := miner.GenerateStateFromDiffs(context.Background(), imageHash, genesisState, diffs)
if err != nil {
return "", err
}
sta, err := stringutil.CompactJSON(state)
if err != nil {
return "", err
}
var st map[string]string
err = json.Unmarshal(sta, &st)
if err != nil {
return "''", err
}
// debug logs
for key, value := range st {
k, _ := hexutil.DecodeString(key)
v, _ := hexutil.DecodeString(value)
log.Printf("[sandbox] state k/v %s=>%s", string(k), string(v))
}
stateFile, err := fileutil.CreateTempFile(fmt.Sprintf("%s/%v/%s", imageHash, ts, stateFileName))
if err != nil {
return "", err
}
fileNames = append(fileNames, stateFile.Name())
if _, err = stateFile.Write(state); err != nil {
return "", err
}
if err = stateFile.Close(); err != nil {
return "", err
}
nextStateFile, err := fileutil.CreateTempFile(fmt.Sprintf("%s/%v/nextState.txt", imageHash, ts))
if err != nil {
return "", err
}
fileNames = append(fileNames, nextStateFile.Name()) // clean up
if err = nextStateFile.Close(); err != nil {
return "", err
}
patchFile, err := fileutil.CreateTempFile(fmt.Sprintf("%s/%v/diff.patch", imageHash, ts))
if err != nil {
return "", err
}
fileNames = append(fileNames, patchFile.Name())
if err = patchFile.Close(); err != nil {
return "", err
}
spew.Dump(prevStateBlock)
latestState := state
log.Printf("[miner] setting docker container initial state to %q", string(state))
committedImageID, err := s.Sandbox.CommitPlay(&sandbox.PlayConfig{
ImageID: imageHash,
Payload: []byte(""),
InitialState: latestState,
})
if err != nil {
return "", err
}
if committedImageID == "" {
return "", errors.New("expected image ID")
}
return docker.ShortImageID(committedImageID), nil
}
func cleanupFiles(fileNames *[]string) error {
if fileNames == nil {
log.Info("no files to remove")
return nil
}
if err := fileutil.RemoveFiles(*fileNames); err != nil {
log.Errorf("[miner] err cleaning up files; %v", err)
return err
}
return nil
}
func cleanupDirs(fileNames *[]string) error {
if fileNames == nil {
log.Info("no files to remove")
return nil
}
dirs := fileutil.DirsFromFiles(*fileNames)
if err := fileutil.RemoveDirs(dirs); err != nil {
log.Errorf("err removing dirs\n%v", err)
}
return nil
}