Skip to content

Commit

Permalink
nit
Browse files Browse the repository at this point in the history
  • Loading branch information
bap2pecs committed Jul 31, 2024
1 parent 5d6e1a3 commit dff4ec0
Showing 1 changed file with 107 additions and 106 deletions.
213 changes: 107 additions & 106 deletions op-node/rollup/finality/plasma_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package finality

import (
"context"
"math/rand" // nosemgrep
"testing"

// nosemgrep
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
Expand All @@ -13,6 +16,8 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup/event"
plasma "github.com/ethereum-optimism/optimism/op-plasma"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum-optimism/optimism/op-service/testutils"
)

type fakePlasmaBackend struct {
Expand All @@ -30,101 +35,97 @@ func (b *fakePlasmaBackend) OnFinalizedHeadSignal(f plasma.HeadSignalFn) {

var _ PlasmaBackend = (*fakePlasmaBackend)(nil)

// TODO: Fix this test
// func TestPlasmaFinalityData(t *testing.T) {
// logger := testlog.Logger(t, log.LevelInfo)
// l1F := &testutils.MockL1Source{}
// l2F := &testutils.MockL2Client{}

// rng := rand.New(rand.NewSource(1234))

// refA := testutils.RandomBlockRef(rng)
// refA0 := eth.L2BlockRef{
// Hash: testutils.RandomHash(rng),
// Number: 0,
// ParentHash: common.Hash{},
// Time: refA.Time,
// L1Origin: refA.ID(),
// SequenceNumber: 0,
// }

// cfg := &rollup.Config{
// Genesis: rollup.Genesis{
// L1: refA.ID(),
// L2: refA0.ID(),
// L2Time: refA0.Time,
// SystemConfig: eth.SystemConfig{
// BatcherAddr: common.Address{42},
// Overhead: [32]byte{123},
// Scalar: [32]byte{42},
// GasLimit: 20_000_000,
// },
// },
// BlockTime: 1,
// SeqWindowSize: 2,
// }
// plasmaCfg := &rollup.PlasmaConfig{
// DAChallengeWindow: 90,
// DAResolveWindow: 90,
// }
// babylonCfg := &rollup.BabylonConfig{
// ChainID: "chain-test",
// ContractAddress: "bbn1eyfccmjm6732k7wp4p6gdjwhxjwsvje44j0hfx8nkgrm8fs7vqfsa3n3gc",
// BitcoinRpc: "https://rpc.ankr.com/btc",
// }
// cfg.BabylonConfig = babylonCfg
// // shoud return l1 finality if plasma is not enabled
// require.Equal(t, uint64(defaultFinalityLookback), calcFinalityLookback(cfg))

// cfg.PlasmaConfig = plasmaCfg
// expFinalityLookback := 181
// require.Equal(t, uint64(expFinalityLookback), calcFinalityLookback(cfg))

// refA1 := eth.L2BlockRef{
// Hash: testutils.RandomHash(rng),
// Number: refA0.Number + 1,
// ParentHash: refA0.Hash,
// Time: refA0.Time + cfg.BlockTime,
// L1Origin: refA.ID(),
// SequenceNumber: 1,
// }

// // Simulate plasma finality by waiting for the finalized-inclusion
// // of a commitment to turn into undisputed finalized data.
// commitmentInclusionFinalized := eth.L1BlockRef{}
// plasmaBackend := &fakePlasmaBackend{
// plasmaFn: func(ref eth.L1BlockRef) {
// commitmentInclusionFinalized = ref
// },
// forwardTo: nil,
// }
func TestPlasmaFinalityData(t *testing.T) {
if true {
// TODO(snapchain): to fix
return
}
logger := testlog.Logger(t, log.LevelInfo)
l1F := &testutils.MockL1Source{}

rng := rand.New(rand.NewSource(1234))

refA := testutils.RandomBlockRef(rng)
refA0 := eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Number: 0,
ParentHash: common.Hash{},
Time: refA.Time,
L1Origin: refA.ID(),
SequenceNumber: 0,
}

cfg := &rollup.Config{
Genesis: rollup.Genesis{
L1: refA.ID(),
L2: refA0.ID(),
L2Time: refA0.Time,
SystemConfig: eth.SystemConfig{
BatcherAddr: common.Address{42},
Overhead: [32]byte{123},
Scalar: [32]byte{42},
GasLimit: 20_000_000,
},
},
BlockTime: 1,
SeqWindowSize: 2,
}
plasmaCfg := &rollup.PlasmaConfig{
DAChallengeWindow: 90,
DAResolveWindow: 90,
}
// shoud return l1 finality if plasma is not enabled
require.Equal(t, uint64(defaultFinalityLookback), calcFinalityLookback(cfg))

cfg.PlasmaConfig = plasmaCfg
expFinalityLookback := 181
require.Equal(t, uint64(expFinalityLookback), calcFinalityLookback(cfg))

refA1 := eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Number: refA0.Number + 1,
ParentHash: refA0.Hash,
Time: refA0.Time + cfg.BlockTime,
L1Origin: refA.ID(),
SequenceNumber: 1,
}

// Simulate plasma finality by waiting for the finalized-inclusion
// of a commitment to turn into undisputed finalized data.
commitmentInclusionFinalized := eth.L1BlockRef{}
plasmaBackend := &fakePlasmaBackend{
plasmaFn: func(ref eth.L1BlockRef) {
commitmentInclusionFinalized = ref
},
forwardTo: nil,
}

emitter := &testutils.MockEmitter{}
fi := NewPlasmaFinalizer(context.Background(), logger, cfg, l1F, plasmaBackend)
fi.AttachEmitter(emitter)
require.NotNil(t, plasmaBackend.forwardTo, "plasma backend must have access to underlying standard finalizer")

// require.Equal(t, expFinalityLookback, cap(fi.finalityData))
require.Equal(t, expFinalityLookback, cap(fi.finalityData))

// l1parent := refA
// l2parent := refA1
l1parent := refA
l2parent := refA1

// // advance over 200 l1 origins each time incrementing new l2 safe heads
// // and post processing.
// for i := uint64(0); i < 200; i++ {
// if i == 10 { // finalize a L1 commitment
// fi.OnEvent(FinalizeL1Event{FinalizedL1: l1parent})
// emitter.AssertExpectations(t) // no events emitted upon L1 finality
// require.Equal(t, l1parent, commitmentInclusionFinalized, "plasma backend received L1 signal")
// }
// advance over 200 l1 origins each time incrementing new l2 safe heads
// and post processing.
for i := uint64(0); i < 200; i++ {
if i == 10 { // finalize a L1 commitment
fi.OnEvent(FinalizeL1Event{FinalizedL1: l1parent})
emitter.AssertExpectations(t) // no events emitted upon L1 finality
require.Equal(t, l1parent, commitmentInclusionFinalized, "plasma backend received L1 signal")
}

// previous := l1parent
// l1parent = eth.L1BlockRef{
// Hash: testutils.RandomHash(rng),
// Number: previous.Number + 1,
// ParentHash: previous.Hash,
// Time: previous.Time + 12,
// }
previous := l1parent
l1parent = eth.L1BlockRef{
Hash: testutils.RandomHash(rng),
Number: previous.Number + 1,
ParentHash: previous.Hash,
Time: previous.Time + 12,
}

for j := uint64(0); j < 2; j++ {
l2parent = eth.L2BlockRef{
Expand All @@ -147,21 +148,21 @@ var _ PlasmaBackend = (*fakePlasmaBackend)(nil)
// clear expectations
emitter.Mock.ExpectedCalls = nil

// // no L2 finalize event, as no L1 finality signal has been forwarded by plasma backend yet
// fi.OnEvent(TryFinalizeEvent{})
// emitter.AssertExpectations(t)

// // Pretend to be the plasma backend,
// // send the original finalization signal to the underlying finalizer,
// // now that we are sure the commitment itself is not just finalized,
// // but the referenced data cannot be disputed anymore.
// plasmaFinalization := commitmentInclusionFinalized.Number + cfg.PlasmaConfig.DAChallengeWindow
// if commitmentInclusionFinalized != (eth.L1BlockRef{}) && l1parent.Number == plasmaFinalization {
// // When the signal is forwarded, a finalization attempt will be scheduled
// emitter.ExpectOnce(TryFinalizeEvent{})
// plasmaBackend.forwardTo(commitmentInclusionFinalized)
// emitter.AssertExpectations(t)
// require.Equal(t, commitmentInclusionFinalized, fi.finalizedL1, "finality signal now made its way in regular finalizer")
// no L2 finalize event, as no L1 finality signal has been forwarded by plasma backend yet
fi.OnEvent(TryFinalizeEvent{})
emitter.AssertExpectations(t)

// Pretend to be the plasma backend,
// send the original finalization signal to the underlying finalizer,
// now that we are sure the commitment itself is not just finalized,
// but the referenced data cannot be disputed anymore.
plasmaFinalization := commitmentInclusionFinalized.Number + cfg.PlasmaConfig.DAChallengeWindow
if commitmentInclusionFinalized != (eth.L1BlockRef{}) && l1parent.Number == plasmaFinalization {
// When the signal is forwarded, a finalization attempt will be scheduled
emitter.ExpectOnce(TryFinalizeEvent{})
plasmaBackend.forwardTo(commitmentInclusionFinalized)
emitter.AssertExpectations(t)
require.Equal(t, commitmentInclusionFinalized, fi.finalizedL1, "finality signal now made its way in regular finalizer")

// As soon as a finalization attempt is made, after the finality signal was triggered by plasma backend,
// we should get an attempt to get a finalized L2 block.
Expand All @@ -188,7 +189,7 @@ var _ PlasmaBackend = (*fakePlasmaBackend)(nil)
}
}

// // finality data does not go over challenge + resolve windows + 1 capacity
// // (prunes down to 180 then adds the extra 1 each time)
// require.Equal(t, expFinalityLookback, len(fi.finalityData))
// }
// finality data does not go over challenge + resolve windows + 1 capacity
// (prunes down to 180 then adds the extra 1 each time)
require.Equal(t, expFinalityLookback, len(fi.finalityData))
}

0 comments on commit dff4ec0

Please sign in to comment.