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: plasma finalize #10392

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion op-plasma/damgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (d *DA) isExpired(bn uint64) bool {
// trigger a derivation reset.
func (d *DA) AdvanceL1Origin(ctx context.Context, l1 L1Fetcher, block eth.BlockID) error {
// do not repeat for the same origin
if block.Number <= d.origin.Number {
0xyjk marked this conversation as resolved.
Show resolved Hide resolved
if block.Number < d.origin.Number {
return nil
}
// sync challenges for the given block ID
Expand Down
49 changes: 46 additions & 3 deletions op-plasma/damgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,49 @@ func RandomData(rng *rand.Rand, size int) []byte {
return out
}

// TestAdvanceL1Origin test advance l1 origin when there are multiple commitments in the same block
func TestAdvanceL1Origin(t *testing.T) {
var (
logger = testlog.Logger(t, log.LevelDebug)
ctx = context.Background()
bn uint64 = 1
l1F = &mockL1Fetcher{}
err error
)
l1F.ExpectFetchReceipts(common.HexToHash(""), nil, types.Receipts{}, nil)
l1F.ExpectL1BlockRefByNumber(bn, eth.L1BlockRef{Number: bn}, nil)

// simulate one block has multiple commitments
state := NewState(logger, &NoopMetrics{})
state.SetInputCommitment([]byte("0x1"), bn, 0)
0xyjk marked this conversation as resolved.
Show resolved Hide resolved
state.SetInputCommitment([]byte("0x2"), bn, 0)
state.SetInputCommitment([]byte("0x3"), bn, 0)

da := NewPlasmaDAWithState(logger, Config{}, NewMockDAClient(logger), &NoopMetrics{}, state)
err = da.AdvanceL1Origin(ctx, l1F, eth.BlockID{Number: bn})
require.NoError(t, err)
require.Equal(t, da.state.finalized, bn)

// simulate another block with no commitments, finalize should not moved
bn++
l1F.ExpectFetchReceipts(common.HexToHash(""), nil, types.Receipts{}, nil)
l1F.ExpectL1BlockRefByNumber(bn, eth.L1BlockRef{Number: bn}, nil)
err = da.AdvanceL1Origin(ctx, l1F, eth.BlockID{Number: bn})
require.NoError(t, err)
require.Equal(t, da.state.finalized, bn-1)

// simulate another block with multiple commitments, finalize should moved
bn++
l1F.ExpectFetchReceipts(common.HexToHash(""), nil, types.Receipts{}, nil)
l1F.ExpectL1BlockRefByNumber(bn, eth.L1BlockRef{Number: bn}, nil)
state.SetInputCommitment([]byte("0x4"), bn, 0)
state.SetInputCommitment([]byte("0x5"), bn, 0)
state.SetInputCommitment([]byte("0x6"), bn, 0)
err = da.AdvanceL1Origin(ctx, l1F, eth.BlockID{Number: bn})
require.NoError(t, err)
require.Equal(t, da.state.finalized, bn)
}
trianglesphere marked this conversation as resolved.
Show resolved Hide resolved

// TestDAChallengeState is a simple test with small values to verify the finalized head logic
func TestDAChallengeState(t *testing.T) {
logger := testlog.Logger(t, log.LvlDebug)
Expand Down Expand Up @@ -269,7 +312,7 @@ func (m *mockL1Fetcher) InfoAndTxsByHash(ctx context.Context, hash common.Hash)
}

func (m *mockL1Fetcher) ExpectInfoAndTxsByHash(hash common.Hash, info eth.BlockInfo, transactions types.Transactions, err error) {
m.Mock.On("InfoAndTxsByHash", hash).Once().Return(info, transactions, err)
m.Mock.On("InfoAndTxsByHash", hash).Return(info, transactions, err)
}

func (m *mockL1Fetcher) FetchReceipts(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Receipts, error) {
Expand All @@ -278,7 +321,7 @@ func (m *mockL1Fetcher) FetchReceipts(ctx context.Context, blockHash common.Hash
}

func (m *mockL1Fetcher) ExpectFetchReceipts(hash common.Hash, info eth.BlockInfo, receipts types.Receipts, err error) {
m.Mock.On("FetchReceipts", hash).Once().Return(&info, receipts, err)
m.Mock.On("FetchReceipts", hash).Return(&info, receipts, err)
}

func (m *mockL1Fetcher) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1BlockRef, error) {
Expand All @@ -287,7 +330,7 @@ func (m *mockL1Fetcher) L1BlockRefByNumber(ctx context.Context, num uint64) (eth
}

func (m *mockL1Fetcher) ExpectL1BlockRefByNumber(num uint64, ref eth.L1BlockRef, err error) {
m.Mock.On("L1BlockRefByNumber", num).Once().Return(ref, err)
m.Mock.On("L1BlockRefByNumber", num).Return(ref, err)
}

func TestFilterInvalidBlockNumber(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion op-plasma/dastate.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *State) GetResolvedInput(key []byte) ([]byte, error) {
// it returns an error to signal that a derivation pipeline reset is required.
func (s *State) ExpireChallenges(bn uint64) (uint64, error) {
var err error
for s.activeComms.Len() > 0 && s.activeComms[0].expiresAt <= bn && s.activeComms[0].blockNumber > s.finalized {
for s.activeComms.Len() > 0 && s.activeComms[0].expiresAt <= bn && s.activeComms[0].blockNumber >= s.finalized {
// move from the active to the expired queue
c := heap.Pop(&s.activeComms).(*Commitment)
heap.Push(&s.expiredComms, c)
Expand Down