forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-node: Add option to only use finalized blocks as l1origin in seque…
…ncer (#209)
- Loading branch information
Showing
7 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package finalized | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
|
||
"github.com/ethereum-optimism/optimism/op-node/rollup/derive" | ||
"github.com/ethereum-optimism/optimism/op-service/eth" | ||
) | ||
|
||
type finalized struct { | ||
derive.L1Fetcher | ||
l1Finalized func() eth.L1BlockRef | ||
} | ||
|
||
func NewFinalized(l1Finalized func() eth.L1BlockRef, fetcher derive.L1Fetcher) *finalized { | ||
return &finalized{L1Fetcher: fetcher, l1Finalized: l1Finalized} | ||
} | ||
|
||
func (f *finalized) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1BlockRef, error) { | ||
l1Finalized := f.l1Finalized() | ||
if num == 0 || num <= l1Finalized.Number { | ||
return f.L1Fetcher.L1BlockRefByNumber(ctx, num) | ||
} | ||
return eth.L1BlockRef{}, ethereum.NotFound | ||
} | ||
|
||
var _ derive.L1Fetcher = (*finalized)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package finalized | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ethereum-optimism/optimism/op-service/eth" | ||
"github.com/ethereum-optimism/optimism/op-service/testutils" | ||
) | ||
|
||
var testFinalHash = common.Hash{0x01} | ||
|
||
type finalizedTest struct { | ||
name string | ||
final uint64 | ||
hash common.Hash // hash of finalized block | ||
req uint64 | ||
pass bool | ||
} | ||
|
||
func (ft *finalizedTest) Run(t *testing.T) { | ||
l1Fetcher := &testutils.MockL1Source{} | ||
l1Finalized := eth.L1BlockRef{Number: ft.final, Hash: ft.hash} | ||
l1FinalizedGetter := func() eth.L1BlockRef { return l1Finalized } | ||
|
||
f := NewFinalized(l1FinalizedGetter, l1Fetcher) | ||
|
||
if ft.pass { | ||
// no calls to the l1Fetcher are made if the block number is not finalized yet | ||
l1Fetcher.ExpectL1BlockRefByNumber(ft.req, eth.L1BlockRef{Number: ft.req}, nil) | ||
} | ||
|
||
out, err := f.L1BlockRefByNumber(context.Background(), ft.req) | ||
l1Fetcher.AssertExpectations(t) | ||
|
||
if ft.pass { | ||
require.NoError(t, err) | ||
require.Equal(t, out, eth.L1BlockRef{Number: ft.req}) | ||
} else { | ||
require.Equal(t, ethereum.NotFound, err) | ||
} | ||
} | ||
|
||
func TestFinalized(t *testing.T) { | ||
testCases := []finalizedTest{ | ||
{name: "finalized", final: 10, hash: testFinalHash, req: 10, pass: true}, | ||
{name: "finalized past", final: 10, hash: testFinalHash, req: 8, pass: true}, | ||
{name: "not finalized", final: 10, hash: testFinalHash, req: 11, pass: false}, | ||
{name: "no L1 state", req: 10, pass: false}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, tc.Run) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters