2023-03-20 15:25:52 +00:00
|
|
|
package consensustests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
|
|
|
)
|
|
|
|
|
|
|
|
func forkTest(context testContext) error {
|
|
|
|
prevContext := context
|
|
|
|
prevContext.version--
|
|
|
|
preState, err := decodeStateFromFile(prevContext, "pre.ssz_snappy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
postState, err := decodeStateFromFile(context, "post.ssz_snappy")
|
|
|
|
expectedError := os.IsNotExist(err)
|
|
|
|
if err != nil && !expectedError {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:25:08 +00:00
|
|
|
preState.StartCollectingReverseChangeSet()
|
2023-03-20 15:25:52 +00:00
|
|
|
if preState.Version() == clparams.Phase0Version {
|
|
|
|
if err := preState.UpgradeToAltair(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if preState.Version() == clparams.AltairVersion {
|
|
|
|
if err := preState.UpgradeToBellatrix(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if preState.Version() == clparams.BellatrixVersion {
|
|
|
|
if err := preState.UpgradeToCapella(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-26 22:25:08 +00:00
|
|
|
change := preState.StopCollectingReverseChangeSet()
|
|
|
|
|
2023-03-20 15:25:52 +00:00
|
|
|
if expectedError {
|
|
|
|
return fmt.Errorf("expected error")
|
|
|
|
}
|
|
|
|
root, err := preState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
expectedRoot, err := postState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if root != expectedRoot {
|
|
|
|
return fmt.Errorf("mismatching state roots")
|
|
|
|
}
|
2023-03-26 22:25:08 +00:00
|
|
|
if context.version == clparams.AltairVersion {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// now do unwind
|
|
|
|
initialState, err := decodeStateFromFile(prevContext, "pre.ssz_snappy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
preState.RevertWithChangeset(change)
|
|
|
|
root, err = preState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
expectedRoot, err = initialState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if root != expectedRoot {
|
|
|
|
return fmt.Errorf("mismatching state roots with unwind")
|
|
|
|
}
|
2023-03-20 15:25:52 +00:00
|
|
|
return nil
|
|
|
|
}
|