Skip to content

Commit

Permalink
Fix cannon trace provider tests and small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Sep 12, 2023
1 parent d8367fd commit 5234cb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
26 changes: 11 additions & 15 deletions op-challenger/game/fault/trace/cannon/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ type ProofGenerator interface {
}

type CannonTraceProvider struct {
logger log.Logger
dir string
prestate string
generator ProofGenerator
lastStepWriter LastStepWriter
lastStepReader LastStepReader
logger log.Logger
dir string
prestate string
generator ProofGenerator

// lastStep stores the last step in the actual trace if known. 0 indicates unknown.
// Cached as an optimisation to avoid repeatedly attempting to execute beyond the end of the trace.
Expand Down Expand Up @@ -81,12 +79,10 @@ func NewTraceProvider(ctx context.Context, logger log.Logger, m CannonMetricer,

func NewTraceProviderFromInputs(logger log.Logger, m CannonMetricer, cfg *config.Config, localInputs LocalGameInputs, dir string) *CannonTraceProvider {
return &CannonTraceProvider{
logger: logger,
dir: dir,
prestate: cfg.CannonAbsolutePreState,
generator: NewExecutor(logger, m, cfg, localInputs),
lastStepWriter: WriteLastStep,
lastStepReader: LoadLastStep,
logger: logger,
dir: dir,
prestate: cfg.CannonAbsolutePreState,
generator: NewExecutor(logger, m, cfg, localInputs),
}
}

Expand Down Expand Up @@ -152,7 +148,7 @@ func (p *CannonTraceProvider) loadProof(ctx context.Context, i uint64) (*proofDa
}
// Attempt to read the last step from disk cache
if p.lastProof == nil && p.lastStep == 0 {
step, err := p.lastStepReader(p.dir)
step, err := ReadLastStep(p.dir)
if err != nil {
p.logger.Warn("Failed to read last step from disk cache", "err", err)
} else {
Expand Down Expand Up @@ -188,7 +184,7 @@ func (p *CannonTraceProvider) loadProof(ctx context.Context, i uint64) (*proofDa
if err != nil {
return nil, fmt.Errorf("cannot hash witness: %w", err)
}
if err := p.lastStepWriter(p.dir, p.lastStep+1); err != nil {
if err := WriteLastStep(p.dir, p.lastStep+1); err != nil {
p.logger.Warn("Failed to write last step to disk cache", "step", p.lastStep)
}
proof := &proofData{
Expand Down Expand Up @@ -222,7 +218,7 @@ type diskStateCacheObj struct {
Step uint64 `json:"step"`
}

func LoadLastStep(dir string) (uint64, error) {
func ReadLastStep(dir string) (uint64, error) {
state := diskStateCacheObj{}
file, err := ioutil.OpenDecompressed(filepath.Join(dir, diskStateCache))
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions op-challenger/game/fault/trace/cannon/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func TestGet(t *testing.T) {
}

func TestGetStepData(t *testing.T) {
dataDir, prestate := setupTestData(t)
t.Run("ExistingProof", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, generator := setupWithTestData(t, dataDir, prestate)
value, proof, data, err := provider.GetStepData(context.Background(), 0)
require.NoError(t, err)
Expand All @@ -80,6 +80,7 @@ func TestGetStepData(t *testing.T) {
})

t.Run("GenerateProof", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, generator := setupWithTestData(t, dataDir, prestate)
generator.finalState = &mipsevm.State{
Memory: &mipsevm.Memory{},
Expand All @@ -105,6 +106,7 @@ func TestGetStepData(t *testing.T) {
})

t.Run("ProofAfterEndOfTrace", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, generator := setupWithTestData(t, dataDir, prestate)
generator.finalState = &mipsevm.State{
Memory: &mipsevm.Memory{},
Expand All @@ -130,6 +132,7 @@ func TestGetStepData(t *testing.T) {
})

t.Run("ReadLastStepFromDisk", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, initGenerator := setupWithTestData(t, dataDir, prestate)
initGenerator.finalState = &mipsevm.State{
Memory: &mipsevm.Memory{},
Expand All @@ -146,7 +149,7 @@ func TestGetStepData(t *testing.T) {
}
_, _, _, err := provider.GetStepData(context.Background(), 7000)
require.NoError(t, err)
require.Contains(t, initGenerator.generated, 10, "should have tried to generate the proof")
require.Contains(t, initGenerator.generated, 7000, "should have tried to generate the proof")

provider, generator := setupWithTestData(t, dataDir, prestate)
generator.finalState = &mipsevm.State{
Expand All @@ -173,13 +176,15 @@ func TestGetStepData(t *testing.T) {
})

t.Run("MissingStateData", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, generator := setupWithTestData(t, dataDir, prestate)
_, _, _, err := provider.GetStepData(context.Background(), 1)
require.ErrorContains(t, err, "missing state data")
require.Empty(t, generator.generated)
})

t.Run("IgnoreUnknownFields", func(t *testing.T) {
dataDir, prestate := setupTestData(t)
provider, generator := setupWithTestData(t, dataDir, prestate)
value, proof, data, err := provider.GetStepData(context.Background(), 2)
require.NoError(t, err)
Expand Down Expand Up @@ -261,12 +266,10 @@ func setupTestData(t *testing.T) (string, string) {
func setupWithTestData(t *testing.T, dataDir string, prestate string) (*CannonTraceProvider, *stubGenerator) {
generator := &stubGenerator{}
return &CannonTraceProvider{
logger: testlog.Logger(t, log.LvlInfo),
dir: dataDir,
generator: generator,
prestate: filepath.Join(dataDir, prestate),
lastStepReader: LoadLastStep,
lastStepWriter: WriteLastStep,
logger: testlog.Logger(t, log.LvlInfo),
dir: dataDir,
generator: generator,
prestate: filepath.Join(dataDir, prestate),
}, generator
}

Expand Down

0 comments on commit 5234cb8

Please sign in to comment.