forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpstate.go
163 lines (147 loc) · 3.57 KB
/
dumpstate.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
package main
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/0xPolygonHermez/zkevm-node/config"
"github.com/0xPolygonHermez/zkevm-node/db"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/urfave/cli/v2"
)
const (
dumpStateFlagDescription = "description"
dumpStateFlagOutput = "output"
)
var dumpStateFlags = []cli.Flag{
&cli.StringFlag{
Name: dumpStateFlagDescription,
Aliases: []string{"desc"},
Usage: "Description of the state being dumped",
Required: true,
},
&cli.StringFlag{
Name: dumpStateFlagOutput,
Aliases: []string{"o"},
Usage: "Output file to save the dump, should end in .json",
Required: true,
},
&configFileFlag,
&networkFlag,
}
type dumpedState struct {
Description string
Genesis genesis
Batches []batchMeta
}
type genesis state.Genesis
func (g genesis) MarshalJSON() ([]byte, error) {
for _, action := range g.GenesisActions {
if !strings.HasPrefix(action.Value, "0x") {
action.Value = fmt.Sprintf("0x%s", action.Value)
}
if action.Bytecode != "" && !strings.HasPrefix(action.Bytecode, "0x") {
action.Bytecode = fmt.Sprintf("0x%s", action.Bytecode)
}
if action.StoragePosition != "" && !strings.HasPrefix(action.StoragePosition, "0x") {
action.StoragePosition = fmt.Sprintf("0x%s", action.StoragePosition)
}
}
// Create JSON
type Alias genesis
return json.Marshal(&struct {
Alias
Actions []*state.GenesisAction
}{
Alias: (Alias)(g),
Actions: g.GenesisActions,
})
}
type batchMeta struct {
Virtualized bool
Consolidated bool
Batch batch
}
type batch state.Batch
func (b *batch) MarshalJSON() ([]byte, error) {
type Alias batch
var strDataPtr *string
if b.BatchL2Data != nil {
strData := "0x" + hex.EncodeToString(b.BatchL2Data)
strDataPtr = &strData
}
return json.Marshal(&struct {
*Alias
Timestamp int64
BatchL2Data *string
}{
Alias: (*Alias)(b),
Timestamp: b.Timestamp.Unix(),
BatchL2Data: strDataPtr,
})
}
func dumpState(ctx *cli.Context) error {
// Load config
c, err := config.Load(ctx, true)
if err != nil {
return err
}
setupLog(c.Log)
description := ctx.String(dumpStateFlagDescription)
outputFile := ctx.String(dumpStateFlagOutput)
if !strings.Contains(outputFile, ".json") {
return errors.New("output file must end in .json")
}
// Connect to SQL
stateSqlDB, err := db.NewSQLDB(c.State.DB)
if err != nil {
return err
}
stateDB := state.NewPostgresStorage(state.Config{}, stateSqlDB)
dump := dumpedState{
Description: description,
Genesis: genesis(c.NetworkConfig.Genesis),
}
// Load batches
dbCtx := context.Background()
lastBatchNum, err := stateDB.GetLastBatchNumber(dbCtx, nil)
if err != nil {
return err
}
lastVirtualBatchNum, err := stateDB.GetLastVirtualBatchNum(dbCtx, nil)
if err != nil {
return err
}
lastConsolidatedBatch, err := stateDB.GetLastVerifiedBatch(dbCtx, nil)
if err != nil {
return err
}
lastConsolidatedBatchNum := lastConsolidatedBatch.BatchNumber
for i := uint64(0); i <= lastBatchNum; i++ {
b, err := stateDB.GetBatchByNumber(dbCtx, i, nil)
if err != nil {
return err
}
isClosed, err := stateDB.IsBatchClosed(dbCtx, i, nil)
if err != nil {
return err
}
if !isClosed {
continue
}
dump.Batches = append(dump.Batches, batchMeta{
Virtualized: i <= lastVirtualBatchNum,
Consolidated: i <= lastConsolidatedBatchNum,
Batch: batch(*b),
})
}
// Dump JSON
file, err := json.MarshalIndent(dump, "", " ")
if err != nil {
return err
}
return os.WriteFile(outputFile, file, 0600) //nolint:gomnd
}