Skip to content

Commit a9b3d9f

Browse files
committed
hardfork test: implement advanced fork procedure
1 parent 35404f5 commit a9b3d9f

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/app/hardfork_test/src/internal/hardfork/fork_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (t *HardforkTest) GetForkConfig(port int) ([]byte, error) {
4343
}
4444

4545
// CreateRuntimeConfig creates the runtime configuration for the fork
46-
func (t *HardforkTest) CreateRuntimeConfig(forkGenesisTimestamp, forkConfigPath, configFile, baseConfigFile, forkHashesFile string) ([]byte, error) {
46+
func (t *HardforkTest) PatchRuntimeConfigLegacy(forkGenesisTimestamp, forkConfigPath, configFile, baseConfigFile, forkHashesFile string) ([]byte, error) {
4747
cmd := exec.Command(filepath.Join(t.ScriptDir, "create_runtime_config.sh"))
4848
cmd.Env = append(os.Environ(),
4949
"GENESIS_TIMESTAMP="+forkGenesisTimestamp,

src/app/hardfork_test/src/internal/hardfork/ledger.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ func (t *HardforkTest) GenerateAndValidatePreforkLedgers(analysis *BlockAnalysis
6464
return nil
6565
}
6666

67-
// GenerateForkConfigAndLedgers does the following:
67+
// PatchForkConfigAndGenerateLedgersLegacy does the following:
6868
// 1. generate fork ledgers with runtime-genesis-ledger
6969
// 2. patch the genesis time & slot for fork config with create_runtime_config.sh
7070
// 3. perform some base sanity check on the fork config
71-
func (t *HardforkTest) GenerateForkConfigAndLedgers(analysis *BlockAnalysisResult, forkConfigPath, forkLedgersDir, forkHashesFile, configFile, preforkGenesisConfigFile string, forkGenesisTs, mainGenesisTs int64) error {
71+
func (t *HardforkTest) PatchForkConfigAndGenerateLedgersLegacy(analysis *BlockAnalysisResult, forkConfigPath, forkLedgersDir, forkHashesFile, configFile, preforkGenesisConfigFile string, forkGenesisTs, mainGenesisTs int64) error {
7272
// Generate fork ledgers using fork network executable
7373
if err := t.GenerateForkLedgers(t.Config.ForkRuntimeGenesisLedger, forkConfigPath, forkLedgersDir, forkHashesFile); err != nil {
7474
return err
7575
}
7676

7777
// Create runtime config
7878
forkGenesisTimestamp := config.FormatTimestamp(forkGenesisTs)
79-
runtimeConfigBytes, err := t.CreateRuntimeConfig(forkGenesisTimestamp, forkConfigPath, configFile, preforkGenesisConfigFile, forkHashesFile)
79+
runtimeConfigBytes, err := t.PatchRuntimeConfigLegacy(forkGenesisTimestamp, forkConfigPath, configFile, preforkGenesisConfigFile, forkHashesFile)
8080
if err != nil {
8181
return err
8282
}
@@ -100,5 +100,10 @@ func (t *HardforkTest) AdvancedGenerateHardForkConfig(configDir string) error {
100100

101101
cmd.Wait()
102102

103+
_, err := os.Stat(fmt.Sprintf("%s/activated"))
104+
if err != nil {
105+
return fmt.Errorf("failed to check on activated file for advanced generate fork config: %w", err)
106+
}
107+
103108
return nil
104109
}

src/app/hardfork_test/src/internal/hardfork/phases.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package hardfork
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"math"
57
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"strconv"
611
"time"
712
)
813

@@ -113,7 +118,6 @@ func (t *HardforkTest) RunForkNetworkPhase(latestPreForkHeight int, forkData For
113118

114119
func (t *HardforkTest) LegacyForkPhase(analysis *BlockAnalysisResult, forkConfigBytes []byte, mainGenesisTs int64) (*ForkData, error) {
115120

116-
// Define all localnet file paths
117121
if err := os.MkdirAll("fork_data/prefork", 0755); err != nil {
118122
return nil, err
119123
}
@@ -150,11 +154,73 @@ func (t *HardforkTest) LegacyForkPhase(analysis *BlockAnalysisResult, forkConfig
150154
{
151155
preforkGenesisConfigFile := fmt.Sprintf("%s/daemon.json", t.Config.Root)
152156
forkHashesFile := "fork_data/hf_ledger_hashes.json"
153-
if err := t.GenerateForkConfigAndLedgers(analysis, preforkConfig, forkLedgersDir, forkHashesFile, postforkConfig, preforkGenesisConfigFile, forkGenesisTs, mainGenesisTs); err != nil {
157+
if err := t.PatchForkConfigAndGenerateLedgersLegacy(analysis, preforkConfig, forkLedgersDir, forkHashesFile, postforkConfig, preforkGenesisConfigFile, forkGenesisTs, mainGenesisTs); err != nil {
154158
return nil, err
155159
}
156160
}
157161

158162
return &ForkData{config: postforkConfig, ledgersDir: forkLedgersDir, genesis: forkGenesisTs}, nil
159163

160164
}
165+
166+
// Uses `mina advanced generate-hardfork-config CLI`
167+
func (t *HardforkTest) AdvancedForkPhase(analysis *BlockAnalysisResult, forkConfigBytes []byte, mainGenesisTs int64) (*ForkData, error) {
168+
169+
cwd := ""
170+
var err error = nil
171+
if cwd, err = os.Getwd(); err != nil {
172+
return nil, err
173+
}
174+
175+
forkDataPath := fmt.Sprintf("%s/fork_data", cwd)
176+
if err := os.MkdirAll(forkDataPath, 0755); err != nil {
177+
return nil, err
178+
}
179+
180+
if err := t.AdvancedGenerateHardForkConfig(forkDataPath); err != nil {
181+
return nil, err
182+
}
183+
184+
configToPatch := fmt.Sprintf("%s/daemon.json", forkDataPath)
185+
186+
configJsonString, err := os.ReadFile(configToPatch)
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
var configRaw map[string]map[string]string
192+
json.Unmarshal(configJsonString, &configRaw)
193+
194+
preforkChainEndTs, err := time.Parse(time.RFC3339Nano, configRaw["genesis"]["genesis_state_timestamp"])
195+
if err != nil {
196+
return nil, err
197+
}
198+
roughForkGenesisTs := time.Now().Add(time.Duration(t.Config.ForkDelay) * time.Minute)
199+
hardforkGenesisSecDelta := roughForkGenesisTs.Sub(preforkChainEndTs).Seconds()
200+
hardforkGenesisSlotDelta := int(math.Ceil(hardforkGenesisSecDelta / float64(t.Config.MainSlot)))
201+
forkSlotDuration := time.Duration(t.Config.MainSlot) * time.Second
202+
forkGenesisTs := preforkChainEndTs.Add(time.Duration(hardforkGenesisSlotDelta) * forkSlotDuration).Unix()
203+
204+
cmd := exec.Command(filepath.Join(t.ScriptDir, "patch-runtime-config-advanced-gen-fork-config.sh"))
205+
206+
cmd.Env = append(os.Environ(),
207+
"FORK_CONFIG_TO_PATCH="+configToPatch,
208+
"PREFORK_SLOT_TIME_SEC="+strconv.Itoa(t.Config.MainSlot),
209+
"HARDFORK_GENESIS_SLOT_DELTA="+strconv.Itoa(hardforkGenesisSlotDelta),
210+
)
211+
cmd.Stderr = os.Stderr
212+
213+
if err := cmd.Run(); err != nil {
214+
return nil, fmt.Errorf("failed to run patch-runtime-config-advanced-gen-fork-config.sh: %w", err)
215+
}
216+
217+
patchedConfigString, err := cmd.Output()
218+
219+
err = os.WriteFile(configToPatch, patchedConfigString, 0644)
220+
if err != nil {
221+
return nil, fmt.Errorf("failed to patch fork config: %w", err)
222+
}
223+
224+
forkLedgersDir := fmt.Sprintf("%s/genesis", forkDataPath)
225+
return &ForkData{config: configToPatch, ledgersDir: forkLedgersDir, genesis: forkGenesisTs}, nil
226+
}

0 commit comments

Comments
 (0)