Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix test: plasma_test.go #4

Merged
merged 19 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions op-node/rollup/finality/plasma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"

fgtypes "github.com/babylonlabs-io/finality-gadget/types"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/rollup/engine"
"github.com/ethereum-optimism/optimism/op-node/rollup/event"
fgmocks "github.com/ethereum-optimism/optimism/op-node/testutils"
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"
"go.uber.org/mock/gomock"
)

type fakePlasmaBackend struct {
Expand All @@ -36,10 +39,6 @@ func (b *fakePlasmaBackend) OnFinalizedHeadSignal(f plasma.HeadSignalFn) {
var _ PlasmaBackend = (*fakePlasmaBackend)(nil)

func TestPlasmaFinalityData(t *testing.T) {
if true {
// TODO(snapchain): to fix
return
}
logger := testlog.Logger(t, log.LevelInfo)
l1F := &testutils.MockL1Source{}
l2F := &testutils.MockL2Client{}
Expand Down Expand Up @@ -68,8 +67,9 @@ func TestPlasmaFinalityData(t *testing.T) {
GasLimit: 20_000_000,
},
},
BlockTime: 1,
SeqWindowSize: 2,
BlockTime: 1,
SeqWindowSize: 2,
BabylonFinalityGadgetRpc: "https://mock-finality-gadget-rpc.com",
}
plasmaCfg := &rollup.PlasmaConfig{
DAChallengeWindow: 90,
Expand Down Expand Up @@ -105,6 +105,11 @@ func TestPlasmaFinalityData(t *testing.T) {
fi := NewPlasmaFinalizer(context.Background(), logger, cfg, l1F, l2F, plasmaBackend)
fi.AttachEmitter(emitter)
require.NotNil(t, plasmaBackend.forwardTo, "plasma backend must have access to underlying standard finalizer")
ctl := gomock.NewController(t)
defer ctl.Finish()
fgclient := fgmocks.NewMockIFinalityGadgetClient(ctl)
fi.babylonFinalityClient = fgclient
mockActivatedTimestamp(fgclient)

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

Expand All @@ -113,8 +118,9 @@ func TestPlasmaFinalityData(t *testing.T) {

// advance over 200 l1 origins each time incrementing new l2 safe heads
// and post processing.
l1FinalityBlockNumber := uint64(10)
for i := uint64(0); i < 200; i++ {
if i == 10 { // finalize a L1 commitment
if i == l1FinalityBlockNumber { // 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")
Expand All @@ -128,6 +134,11 @@ func TestPlasmaFinalityData(t *testing.T) {
Time: previous.Time + 12,
}

mockL2Refs := []eth.L2BlockRef{}
if i == 0 {
mockL2Refs = append(mockL2Refs, l2parent)
l2F.ExpectL2BlockRefByNumber(l2parent.Number, l2parent, nil)
}
for j := uint64(0); j < 2; j++ {
l2parent = eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Expand All @@ -137,9 +148,16 @@ func TestPlasmaFinalityData(t *testing.T) {
L1Origin: previous.ID(), // reference previous origin, not the block the batch was included in
SequenceNumber: j,
}
if i < l1FinalityBlockNumber {
mockL2Refs = append(mockL2Refs, l2parent)
l2F.ExpectL2BlockRefByNumber(l2parent.Number, l2parent, nil)
}
fi.OnEvent(engine.SafeDerivedEvent{Safe: l2parent, DerivedFrom: l1parent})
emitter.AssertExpectations(t)
}
if i < l1FinalityBlockNumber {
mockQueryBlockRangeBabylonFinalized(fgclient, mockL2Refs)
}
// might trigger finalization attempt, if expired finality delay
emitter.ExpectMaybeRun(func(ev event.Event) {
require.IsType(t, TryFinalizeEvent{}, ev)
Expand Down Expand Up @@ -194,3 +212,15 @@ func TestPlasmaFinalityData(t *testing.T) {
// (prunes down to 180 then adds the extra 1 each time)
require.Equal(t, expFinalityLookback, len(fi.finalityData))
}

func mockQueryBlockRangeBabylonFinalized(fgclient *fgmocks.MockIFinalityGadgetClient, refs []eth.L2BlockRef) {
queryBlocks := make([]*fgtypes.Block, len(refs))
for i, ref := range refs {
queryBlocks[i] = &fgtypes.Block{
BlockHeight: ref.Number,
BlockHash: ref.Hash.String(),
BlockTimestamp: ref.Time,
}
}
fgclient.EXPECT().QueryBlockRangeBabylonFinalized(queryBlocks).Return(&refs[len(refs)-1].Number, nil)
}