|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// Hardfork describes a mainnet hardfork activation point used by the |
| 11 | +// partial-archive picker. All block numbers are verified for Ethereum |
| 12 | +// mainnet — paris from the reth chainspec source, post-merge forks from |
| 13 | +// the beacon API by computing slot = (fork_ts - beacon_genesis) / 12 and |
| 14 | +// reading execution_payload.block_number from the canonical block at (or |
| 15 | +// shortly after) that slot. |
| 16 | +// |
| 17 | +// Verified 2026-05-27 against: |
| 18 | +// - reth v2.2.0 crates/chainspec/src/spec.rs (paris) |
| 19 | +// - go-ethereum params/config.go (post-merge fork timestamps) |
| 20 | +// - ethereum-beacon-api.publicnode.com (slot → execution block lookups) |
| 21 | +type Hardfork struct { |
| 22 | + // Name is the canonical EL fork name used by --since=<name>. |
| 23 | + Name string |
| 24 | + // DisplayName is shown in the interactive picker. |
| 25 | + DisplayName string |
| 26 | + // Block is the first execution block at or after the fork activation. |
| 27 | + Block uint64 |
| 28 | + // Date is the activation date, used only for picker labels. |
| 29 | + Date string |
| 30 | + // ApproxArchiveSizeTB is a rough estimate of mainnet archive disk usage |
| 31 | + // when pruning state history before this block (reth). Used in picker |
| 32 | + // labels so users have a realistic expectation. Values rounded. |
| 33 | + ApproxArchiveSizeTB float64 |
| 34 | +} |
| 35 | + |
| 36 | +// MainnetHardforks is the ordered list of mainnet hardforks supported by |
| 37 | +// --since presets, oldest first. |
| 38 | +var MainnetHardforks = []Hardfork{ |
| 39 | + { |
| 40 | + Name: "merge", |
| 41 | + DisplayName: "the merge", |
| 42 | + Block: 15537394, |
| 43 | + Date: "2022-09-15", |
| 44 | + ApproxArchiveSizeTB: 1.5, |
| 45 | + }, |
| 46 | + { |
| 47 | + Name: "shanghai", |
| 48 | + DisplayName: "shanghai", |
| 49 | + Block: 17034870, |
| 50 | + Date: "2023-04-12", |
| 51 | + ApproxArchiveSizeTB: 1.2, |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "cancun", |
| 55 | + DisplayName: "cancun", |
| 56 | + Block: 19426587, |
| 57 | + Date: "2024-03-13", |
| 58 | + ApproxArchiveSizeTB: 0.8, |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: "prague", |
| 62 | + DisplayName: "prague", |
| 63 | + Block: 22431084, |
| 64 | + Date: "2025-05-07", |
| 65 | + ApproxArchiveSizeTB: 0.4, |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: "osaka", |
| 69 | + DisplayName: "osaka", |
| 70 | + Block: 23935694, |
| 71 | + Date: "2025-12-03", |
| 72 | + ApproxArchiveSizeTB: 0.2, |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +// HardforkByName returns the hardfork with the given name, or nil. |
| 77 | +func HardforkByName(name string) *Hardfork { |
| 78 | + for i := range MainnetHardforks { |
| 79 | + if MainnetHardforks[i].Name == strings.ToLower(name) { |
| 80 | + return &MainnetHardforks[i] |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// ArchiveScope is the resolved meaning of --since after parsing. |
| 87 | +type ArchiveScope struct { |
| 88 | + // Kind is one of: "all" (full archive from genesis), "before" (prune |
| 89 | + // before a specific block), "distance" (keep last N blocks of history). |
| 90 | + Kind string |
| 91 | + // Block is set when Kind == "before". |
| 92 | + Block uint64 |
| 93 | + // Distance is set when Kind == "distance". |
| 94 | + Distance uint64 |
| 95 | + // Label is a human-readable description for logs/picker confirmation. |
| 96 | + Label string |
| 97 | +} |
| 98 | + |
| 99 | +// ParseSince resolves a --since value into an ArchiveScope. Accepted |
| 100 | +// forms: |
| 101 | +// - "genesis", "all": full archive from genesis |
| 102 | +// - "merge", "shanghai", "cancun", "prague", "osaka": EL hardfork name |
| 103 | +// - "<N>d", "<N>mo", "<N>y": duration back from chain head (12s slots) |
| 104 | +// - "<block>": raw block number (any unsigned integer) |
| 105 | +// |
| 106 | +// Mode is reth-anchored: "before" presets get translated to reth's |
| 107 | +// --prune.<segment>.before flags; "distance" presets to --prune.<segment>.distance. |
| 108 | +func ParseSince(raw string) (ArchiveScope, error) { |
| 109 | + v := strings.TrimSpace(strings.ToLower(raw)) |
| 110 | + if v == "" { |
| 111 | + return ArchiveScope{}, fmt.Errorf("--since cannot be empty") |
| 112 | + } |
| 113 | + |
| 114 | + if v == "genesis" || v == "all" { |
| 115 | + return ArchiveScope{Kind: "all", Label: "all history (from genesis)"}, nil |
| 116 | + } |
| 117 | + |
| 118 | + if hf := HardforkByName(v); hf != nil { |
| 119 | + return ArchiveScope{ |
| 120 | + Kind: "before", |
| 121 | + Block: hf.Block, |
| 122 | + Label: fmt.Sprintf("since %s (block %d, %s)", hf.DisplayName, hf.Block, hf.Date), |
| 123 | + }, nil |
| 124 | + } |
| 125 | + |
| 126 | + // Duration: <N>{d,mo,y}. Translates to block distance using the |
| 127 | + // post-merge slot time of 12s. "mo" is 30 days; "y" is 365 days. |
| 128 | + if blocks, ok := parseDurationBlocks(v); ok { |
| 129 | + return ArchiveScope{ |
| 130 | + Kind: "distance", |
| 131 | + Distance: blocks, |
| 132 | + Label: fmt.Sprintf("last %s (~%d blocks from tip)", v, blocks), |
| 133 | + }, nil |
| 134 | + } |
| 135 | + |
| 136 | + // Raw block number. |
| 137 | + if n, err := strconv.ParseUint(v, 10, 64); err == nil { |
| 138 | + return ArchiveScope{ |
| 139 | + Kind: "before", |
| 140 | + Block: n, |
| 141 | + Label: fmt.Sprintf("since block %d", n), |
| 142 | + }, nil |
| 143 | + } |
| 144 | + |
| 145 | + return ArchiveScope{}, fmt.Errorf("unrecognized --since value %q (try: genesis, merge, shanghai, cancun, prague, osaka, 365d, 1y, or a block number)", raw) |
| 146 | +} |
| 147 | + |
| 148 | +// parseDurationBlocks accepts "<N>d|mo|y" and returns the equivalent |
| 149 | +// post-merge block distance (12s slots, ignoring missed slots — close |
| 150 | +// enough for pruning purposes since reth uses --prune.*.distance which |
| 151 | +// is anchored to chain tip at run time). |
| 152 | +func parseDurationBlocks(v string) (uint64, bool) { |
| 153 | + var n uint64 |
| 154 | + var unit string |
| 155 | + for i := 0; i < len(v); i++ { |
| 156 | + if v[i] < '0' || v[i] > '9' { |
| 157 | + parsed, err := strconv.ParseUint(v[:i], 10, 64) |
| 158 | + if err != nil || i == 0 { |
| 159 | + return 0, false |
| 160 | + } |
| 161 | + n = parsed |
| 162 | + unit = v[i:] |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + if unit == "" { |
| 167 | + return 0, false |
| 168 | + } |
| 169 | + |
| 170 | + var seconds uint64 |
| 171 | + switch unit { |
| 172 | + case "d", "day", "days": |
| 173 | + seconds = n * uint64(24*time.Hour/time.Second) |
| 174 | + case "mo", "month", "months": |
| 175 | + seconds = n * 30 * uint64(24*time.Hour/time.Second) |
| 176 | + case "y", "yr", "year", "years": |
| 177 | + seconds = n * 365 * uint64(24*time.Hour/time.Second) |
| 178 | + default: |
| 179 | + return 0, false |
| 180 | + } |
| 181 | + |
| 182 | + // 12s per slot post-merge. |
| 183 | + return seconds / 12, true |
| 184 | +} |
0 commit comments