2020-07-14 01:56:29 +00:00
package commands
2020-07-07 04:00:25 +00:00
import (
2022-05-19 05:28:33 +00:00
"encoding/binary"
2020-07-07 04:00:25 +00:00
"fmt"
2020-07-25 07:35:08 +00:00
"os"
2020-08-05 10:13:35 +00:00
"text/tabwriter"
2020-07-07 04:00:25 +00:00
2022-01-19 03:49:07 +00:00
"github.com/ledgerwatch/erigon-lib/common"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
2022-05-19 05:28:33 +00:00
"github.com/ledgerwatch/erigon/core/rawdb"
2022-06-07 03:24:50 +00:00
reset2 "github.com/ledgerwatch/erigon/core/rawdb/rawdbreset"
2021-05-20 18:25:53 +00:00
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
Pruning for: exec, log_index, tx_lookup, history stages (#2399)
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* add tvm flag
* save
* db migration for storage mode
add flag --prune=
remove flag --storage-mode=
add flag --experiments=tevm,...
rename integration set_storage_mode to set_prune
* fix
* forward move of stages must skip everything before PruneTo
* keep in db progress of prune method
* keep in db progress of prune method
* simplify logs
* simplify logs
* simplify logs
* fix test
* simplify logs
* simplify logs
* simplify logs
* simplify logs
* remove callTraceSet as dupsort
use etl transform for txlookup prune
remove some logs
* cleanup tests a bit
* print_stages and eth_sync to show prune progress
* fix print_stages
* add readme about --prune.to flag
* more docs
* add --prune.history.older and other flags support
* fix migration on empty db
* better toString
* better toString
2021-07-20 20:03:19 +00:00
"github.com/ledgerwatch/erigon/ethdb/prune"
2022-07-28 09:57:38 +00:00
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
2020-07-07 04:00:25 +00:00
"github.com/spf13/cobra"
)
var cmdResetState = & cobra . Command {
Use : "reset_state" ,
2020-08-10 17:46:06 +00:00
Short : "Reset StateStages (5,6,7,8,9,10) and buckets" ,
2020-07-07 04:00:25 +00:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-01-19 03:49:07 +00:00
ctx , _ := common . RootContext ( )
2022-08-12 14:45:09 +00:00
db := openDB ( dbCfg ( kv . ChainDB , chaindata ) , true )
2020-10-28 03:18:10 +00:00
defer db . Close ( )
2022-07-28 09:57:38 +00:00
if err := db . View ( ctx , func ( tx kv . Tx ) error { return printStages ( tx , allSnapshots ( db ) ) } ) ; err != nil {
2022-06-07 03:24:50 +00:00
return err
}
2020-10-28 03:18:10 +00:00
2022-08-12 14:45:09 +00:00
err := reset2 . ResetState ( db , ctx , chain )
2020-07-07 04:00:25 +00:00
if err != nil {
log . Error ( err . Error ( ) )
return err
}
2020-08-10 23:38:19 +00:00
2022-06-07 03:24:50 +00:00
// set genesis after reset all buckets
fmt . Printf ( "After reset: \n" )
2022-07-28 09:57:38 +00:00
if err := db . View ( ctx , func ( tx kv . Tx ) error { return printStages ( tx , allSnapshots ( db ) ) } ) ; err != nil {
2022-06-07 03:24:50 +00:00
return err
}
2020-08-10 23:38:19 +00:00
return nil
} ,
}
2020-07-07 04:00:25 +00:00
func init ( ) {
2022-02-22 17:39:48 +00:00
withDataDir ( cmdResetState )
2021-06-16 04:31:25 +00:00
withChain ( cmdResetState )
2020-07-07 04:00:25 +00:00
rootCmd . AddCommand ( cmdResetState )
}
2022-07-28 09:57:38 +00:00
func printStages ( db kv . Tx , snapshots * snapshotsync . RoSnapshots ) error {
2020-07-07 04:00:25 +00:00
var err error
var progress uint64
2020-08-05 10:13:35 +00:00
w := new ( tabwriter . Writer )
defer w . Flush ( )
w . Init ( os . Stdout , 8 , 8 , 0 , '\t' , 0 )
Pruning for: exec, log_index, tx_lookup, history stages (#2399)
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* add tvm flag
* save
* db migration for storage mode
add flag --prune=
remove flag --storage-mode=
add flag --experiments=tevm,...
rename integration set_storage_mode to set_prune
* fix
* forward move of stages must skip everything before PruneTo
* keep in db progress of prune method
* keep in db progress of prune method
* simplify logs
* simplify logs
* simplify logs
* fix test
* simplify logs
* simplify logs
* simplify logs
* simplify logs
* remove callTraceSet as dupsort
use etl transform for txlookup prune
remove some logs
* cleanup tests a bit
* print_stages and eth_sync to show prune progress
* fix print_stages
* add readme about --prune.to flag
* more docs
* add --prune.history.older and other flags support
* fix migration on empty db
* better toString
* better toString
2021-07-20 20:03:19 +00:00
fmt . Fprintf ( w , "Note: prune_at doesn't mean 'all data before were deleted' - it just mean stage.Prune function were run to this block. Because 1 stage may prune multiple data types to different prune distance.\n" )
2022-08-17 09:24:11 +00:00
fmt . Fprint ( w , "\n \t\t stage_at \t prune_at\n" )
2020-09-05 16:07:27 +00:00
for _ , stage := range stages . AllStages {
2020-12-21 11:01:37 +00:00
if progress , err = stages . GetStageProgress ( db , stage ) ; err != nil {
2020-07-07 04:00:25 +00:00
return err
}
Pruning for: exec, log_index, tx_lookup, history stages (#2399)
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* add tvm flag
* save
* db migration for storage mode
add flag --prune=
remove flag --storage-mode=
add flag --experiments=tevm,...
rename integration set_storage_mode to set_prune
* fix
* forward move of stages must skip everything before PruneTo
* keep in db progress of prune method
* keep in db progress of prune method
* simplify logs
* simplify logs
* simplify logs
* fix test
* simplify logs
* simplify logs
* simplify logs
* simplify logs
* remove callTraceSet as dupsort
use etl transform for txlookup prune
remove some logs
* cleanup tests a bit
* print_stages and eth_sync to show prune progress
* fix print_stages
* add readme about --prune.to flag
* more docs
* add --prune.history.older and other flags support
* fix migration on empty db
* better toString
* better toString
2021-07-20 20:03:19 +00:00
prunedTo , err := stages . GetStagePruneProgress ( db , stage )
if err != nil {
return err
}
2022-08-17 09:24:11 +00:00
fmt . Fprintf ( w , "%s \t\t %d \t %d\n" , string ( stage ) , progress , prunedTo )
Pruning for: exec, log_index, tx_lookup, history stages (#2399)
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* add tvm flag
* save
* db migration for storage mode
add flag --prune=
remove flag --storage-mode=
add flag --experiments=tevm,...
rename integration set_storage_mode to set_prune
* fix
* forward move of stages must skip everything before PruneTo
* keep in db progress of prune method
* keep in db progress of prune method
* simplify logs
* simplify logs
* simplify logs
* fix test
* simplify logs
* simplify logs
* simplify logs
* simplify logs
* remove callTraceSet as dupsort
use etl transform for txlookup prune
remove some logs
* cleanup tests a bit
* print_stages and eth_sync to show prune progress
* fix print_stages
* add readme about --prune.to flag
* more docs
* add --prune.history.older and other flags support
* fix migration on empty db
* better toString
* better toString
2021-07-20 20:03:19 +00:00
}
pm , err := prune . Get ( db )
if err != nil {
return err
2020-07-07 04:00:25 +00:00
}
Pruning for: exec, log_index, tx_lookup, history stages (#2399)
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* Pruning for: exec, log_index, tx_lookup, history stages
* add tvm flag
* save
* db migration for storage mode
add flag --prune=
remove flag --storage-mode=
add flag --experiments=tevm,...
rename integration set_storage_mode to set_prune
* fix
* forward move of stages must skip everything before PruneTo
* keep in db progress of prune method
* keep in db progress of prune method
* simplify logs
* simplify logs
* simplify logs
* fix test
* simplify logs
* simplify logs
* simplify logs
* simplify logs
* remove callTraceSet as dupsort
use etl transform for txlookup prune
remove some logs
* cleanup tests a bit
* print_stages and eth_sync to show prune progress
* fix print_stages
* add readme about --prune.to flag
* more docs
* add --prune.history.older and other flags support
* fix migration on empty db
* better toString
* better toString
2021-07-20 20:03:19 +00:00
fmt . Fprintf ( w , "--\n" )
2021-07-24 09:50:42 +00:00
fmt . Fprintf ( w , "prune distance: %s\n\n" , pm . String ( ) )
2022-03-27 03:07:58 +00:00
s1 , err := db . ReadSequence ( kv . EthTx )
if err != nil {
return err
}
s2 , err := db . ReadSequence ( kv . NonCanonicalTxs )
if err != nil {
return err
}
fmt . Fprintf ( w , "sequence: EthTx=%d, NonCanonicalTx=%d\n\n" , s1 , s2 )
2022-05-19 05:28:33 +00:00
{
firstNonGenesis , err := rawdb . SecondKey ( db , kv . Headers )
if err != nil {
return err
}
2022-05-26 14:41:05 +00:00
if firstNonGenesis != nil {
fmt . Fprintf ( w , "first header in db: %d\n" , binary . BigEndian . Uint64 ( firstNonGenesis ) )
} else {
fmt . Fprintf ( w , "no headers in db\n" )
}
firstNonGenesis , err = rawdb . SecondKey ( db , kv . BlockBody )
if err != nil {
return err
}
if firstNonGenesis != nil {
fmt . Fprintf ( w , "first body in db: %d\n\n" , binary . BigEndian . Uint64 ( firstNonGenesis ) )
} else {
fmt . Fprintf ( w , "no bodies in db\n\n" )
}
2022-05-19 05:28:33 +00:00
}
2022-07-28 09:57:38 +00:00
fmt . Fprintf ( w , "--\n" )
fmt . Fprintf ( w , "snapsthos: blocks=%d, segments=%d, indices=%d\n\n" , snapshots . BlocksAvailable ( ) , snapshots . SegmentsMax ( ) , snapshots . IndicesMax ( ) )
2022-08-17 09:24:11 +00:00
//fmt.Printf("==== state =====\n")
//db.ForEach(kv.PlainState, nil, func(k, v []byte) error {
// fmt.Printf("st: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== code =====\n")
//db.ForEach(kv.Code, nil, func(k, v []byte) error {
// fmt.Printf("code: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== PlainContractCode =====\n")
//db.ForEach(kv.PlainContractCode, nil, func(k, v []byte) error {
// fmt.Printf("code2: %x, %x\n", k, v)
// return nil
//})
//fmt.Printf("==== IncarnationMap =====\n")
//db.ForEach(kv.IncarnationMap, nil, func(k, v []byte) error {
// fmt.Printf("IncarnationMap: %x, %x\n", k, v)
// return nil
//})
2020-07-07 04:00:25 +00:00
return nil
}