Skip to content

Commit

Permalink
cannon: Expand state codec tests (ethereum-optimism#12908)
Browse files Browse the repository at this point in the history
* cannon: Add version 3 state and enable detect test

* cannon: Get other DetectVersion tests running
  • Loading branch information
mbaxter authored Nov 15, 2024
1 parent 2a40d22 commit 1a06780
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions cannon/mipsevm/versions/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"strconv"
"testing"

"github.com/stretchr/testify/require"

"github.com/ethereum-optimism/optimism/cannon/mipsevm/arch"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/multithreaded"
"github.com/ethereum-optimism/optimism/cannon/mipsevm/singlethreaded"
"github.com/ethereum-optimism/optimism/op-service/ioutil"
"github.com/stretchr/testify/require"
)

const statesPath = "testdata/states"

//go:embed testdata/states
var historicStates embed.FS

func TestDetectVersion(t *testing.T) {
func TestDetectVersion_fromFile(t *testing.T) {
testDetection := func(t *testing.T, version StateVersion, ext string) {
filename := strconv.Itoa(int(version)) + ext
dir := t.TempDir()
Expand All @@ -34,9 +36,6 @@ func TestDetectVersion(t *testing.T) {
// Iterate all known versions to ensure we have a test case to detect every state version
for _, version := range StateVersionTypes {
version := version
if version == VersionMultiThreaded64 {
t.Skip("TODO(#12205)")
}
t.Run(version.String(), func(t *testing.T) {
testDetection(t, version, ".bin.gz")
})
Expand All @@ -47,28 +46,38 @@ func TestDetectVersion(t *testing.T) {
})
}
}
}

// Additionally, check that the latest supported versions write new states in a way that is detected correctly
t.Run("SingleThreadedBinary", func(t *testing.T) {
state, err := NewFromState(singlethreaded.CreateEmptyState())
require.NoError(t, err)
path := writeToFile(t, "state.bin.gz", state)
version, err := DetectVersion(path)
require.NoError(t, err)
require.Equal(t, VersionSingleThreaded2, version)
})
// Check that the latest supported versions write new states in a way that is detected correctly
func TestDetectVersion_singleThreadedBinary(t *testing.T) {
targetVersion := VersionSingleThreaded2
if !arch.IsMips32 {
t.Skip("Single-threaded states are not supported for 64-bit VMs")
}

t.Run("MultiThreadedBinary", func(t *testing.T) {
state, err := NewFromState(multithreaded.CreateEmptyState())
require.NoError(t, err)
path := writeToFile(t, "state.bin.gz", state)
version, err := DetectVersion(path)
require.NoError(t, err)
require.Equal(t, VersionMultiThreaded, version)
})
state, err := NewFromState(singlethreaded.CreateEmptyState())
require.NoError(t, err)
path := writeToFile(t, "state.bin.gz", state)
version, err := DetectVersion(path)
require.NoError(t, err)
require.Equal(t, targetVersion, version)
}

func TestDetectVersion_multiThreadedBinary(t *testing.T) {
targetVersion := VersionMultiThreaded
if !arch.IsMips32 {
targetVersion = VersionMultiThreaded64
}

state, err := NewFromState(multithreaded.CreateEmptyState())
require.NoError(t, err)
path := writeToFile(t, "state.bin.gz", state)
version, err := DetectVersion(path)
require.NoError(t, err)
require.Equal(t, targetVersion, version)
}

func TestDetectVersionInvalid(t *testing.T) {
func TestDetectVersion_invalid(t *testing.T) {
t.Run("bad gzip", func(t *testing.T) {
dir := t.TempDir()
filename := "state.bin.gz"
Expand Down
Binary file added cannon/mipsevm/versions/testdata/states/3.bin.gz
Binary file not shown.

0 comments on commit 1a06780

Please sign in to comment.