1717package commands
1818
1919import (
20+ "context"
2021 "os"
2122
2223 "github.com/erigontech/erigon/db/config3"
@@ -34,7 +35,12 @@ func init() {
3435 withDataDir2 (printCmd )
3536 withHistoryDomain (printCmd )
3637
38+ withDataDir2 (rebuildCmd )
39+ withHistoryDomain (rebuildCmd )
40+
3741 historyCmd .AddCommand (printCmd )
42+ historyCmd .AddCommand (rebuildCmd )
43+
3844 rootCmd .AddCommand (historyCmd )
3945}
4046
@@ -98,3 +104,55 @@ var printCmd = &cobra.Command{
98104 }
99105 },
100106}
107+
108+ var rebuildCmd = & cobra.Command {
109+ Use : "rebuild" ,
110+ Short : "Regenerate .ef .efi .v .vi domain history snapshots from step 0" ,
111+ Run : func (cmd * cobra.Command , args []string ) {
112+ logger := debug .SetupCobra (cmd , "integration" )
113+
114+ dirs , l , err := datadir .New (datadirCli ).MustFlock ()
115+ if err != nil {
116+ logger .Error ("Opening Datadir" , "error" , err )
117+ return
118+ }
119+ defer l .Unlock ()
120+
121+ domainKV , err := kv .String2Domain (historyDomain )
122+ if err != nil {
123+ logger .Error ("Failed to resolve domain" , "error" , err )
124+ return
125+ }
126+
127+ history , err := state .NewHistory (
128+ statecfg .Schema .GetDomainCfg (domainKV ).Hist ,
129+ config3 .DefaultStepSize ,
130+ config3 .DefaultStepsInFrozenFile ,
131+ dirs ,
132+ logger ,
133+ )
134+ if err != nil {
135+ logger .Error ("Failed to init history" , "error" , err )
136+ return
137+ }
138+ history .Scan (toStep * config3 .DefaultStepSize )
139+
140+ roTx := history .BeginFilesRo ()
141+ defer roTx .Close ()
142+
143+ for i := uint64 (0 ); i < roTx .FirstStepNotInFiles ().ToTxNum (config3 .DefaultStepSize ); {
144+ fromTxNum := i
145+ i += config3 .DefaultStepSize * config3 .DefaultStepsInFrozenFile
146+
147+ if i > roTx .FirstStepNotInFiles ().ToTxNum (config3 .DefaultStepSize ) {
148+ i = roTx .FirstStepNotInFiles ().ToTxNum (config3 .DefaultStepSize )
149+ }
150+
151+ err = roTx .CompactRange (context .TODO (), fromTxNum , i )
152+ if err != nil {
153+ logger .Error ("Failed to rebuild history" , "error" , err )
154+ return
155+ }
156+ }
157+ },
158+ }
0 commit comments