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

Add e2e test case to check for monomer rollbacks #192

Merged
merged 9 commits into from
Sep 9, 2024
11 changes: 10 additions & 1 deletion e2e/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/ethereum-optimism/optimism/indexer/bindings"
opgenesis "github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand Down Expand Up @@ -51,6 +52,7 @@ type StackConfig struct {
L1Portal *bindings.OptimismPortal
L2Client *bftclient.HTTP
MonomerClient *MonomerClient
RollupConfig *rollup.Config
WaitL1 func(numBlocks int) error
WaitL2 func(numBlocks int) error
}
Expand Down Expand Up @@ -250,11 +252,17 @@ func (s *stack) run(ctx context.Context, env *environment.Env) (*StackConfig, er
}

// construct L2 client
l2Client, err := bftclient.New(s.monomerCometURL.String(), s.monomerCometURL.String())
l2Client, err := bftclient.New(s.monomerCometURL.String(), "/websocket")
if err != nil {
return nil, fmt.Errorf("new Comet client: %v", err)
}

// start the L2 client
if err = l2Client.Start(); err != nil {
return nil, fmt.Errorf("start Comet client: %v", err)
}
env.DeferErr("stop Comet client", l2Client.Stop)

wait := func(numBlocks, layer int) error {
var client interface {
BlockByNumber(context.Context, *big.Int) (*ethtypes.Block, error)
Expand Down Expand Up @@ -290,6 +298,7 @@ func (s *stack) run(ctx context.Context, env *environment.Env) (*StackConfig, er
MonomerClient: monomerClient,
Operator: l1users[0],
Users: l1users[1:],
RollupConfig: rollupConfig,
WaitL1: func(numBlocks int) error {
return wait(numBlocks, 1)
},
Expand Down
47 changes: 47 additions & 0 deletions e2e/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (
"github.com/cometbft/cometbft/config"
bftclient "github.com/cometbft/cometbft/rpc/client/http"
bfttypes "github.com/cometbft/cometbft/types"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/polymerdao/monomer"
"github.com/polymerdao/monomer/e2e"
"github.com/polymerdao/monomer/environment"
"github.com/polymerdao/monomer/node"
Expand Down Expand Up @@ -56,6 +59,10 @@ var e2eTests = []struct {
name: "AttributesTX",
run: containsAttributesTx,
},
{
name: "No Rollbacks",
run: checkForRollbacks,
},
}

func TestE2E(t *testing.T) {
Expand Down Expand Up @@ -117,6 +124,46 @@ func TestE2E(t *testing.T) {
runningTests.Wait()
}

func checkForRollbacks(t *testing.T, stack *e2e.StackConfig) {
// Subscribe to new block events
const subscriber = "rollbackChecker"
eventChan, err := stack.L2Client.Subscribe(stack.Ctx, subscriber, bfttypes.QueryForEvent(bfttypes.EventNewBlock).String())
require.NoError(t, err)
defer func() {
require.NoError(t, stack.L2Client.UnsubscribeAll(stack.Ctx, subscriber))
}()

var lastBlockHeight int64

// Check new block events for rollbacks
for event := range eventChan {
eventNewBlock, ok := event.Data.(bfttypes.EventDataNewBlock)
require.True(t, ok)
currentHeight := eventNewBlock.Block.Header.Height

// Skip the rollback check if lastBlockHeight is not initialized yet
if lastBlockHeight > 0 {
// Ensure that the current block height is the last checked height + 1
require.Equal(t, currentHeight, lastBlockHeight+1, "monomer has rolled back")
}
lastBlockHeight = currentHeight

// Get the L1 block info from the first tx in the block
ethTxs, err := monomer.AdaptCosmosTxsToEthTxs(eventNewBlock.Block.Txs)
require.NoError(t, err)
l1BlockInfo, err := derive.L1BlockInfoFromBytes(&rollup.Config{}, uint64(eventNewBlock.Block.Time.Unix()), ethTxs[0].Data())
natebeauregard marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

// End the test once a sequencing window has passed.
if l1BlockInfo.Number >= stack.RollupConfig.SeqWindowSize+1 {
t.Log("No Monomer rollbacks detected")
return
}
}
natebeauregard marked this conversation as resolved.
Show resolved Hide resolved

require.Fail(t, "event chan closed prematurely")
}

func containsAttributesTx(t *testing.T, stack *e2e.StackConfig) {
targetHeight := uint64(5)

Expand Down
Loading