Skip to content

Commit

Permalink
Add a test for escape hatch (#433)
Browse files Browse the repository at this point in the history
* Add a test for escape hatch
  • Loading branch information
ImJeremyHe authored Jan 16, 2025
1 parent d99a3ed commit 17734f4
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions system_tests/espresso_escape_hatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"encoding/json"
"os/exec"
"sync"
"testing"
"time"

lightclientmock "github.com/EspressoSystems/espresso-sequencer-go/light-client-mock"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"

"github.com/offchainlabs/nitro/arbutil"
Expand Down Expand Up @@ -147,3 +150,140 @@ func TestEspressoEscapeHatch(t *testing.T) {
// TODO: Find a way to check if any hotshot transaction is submitted,
// then set the hotshot live again.
}

func TestEspressoEscapeHatchShouldNotHaltTheChain(t *testing.T) {
var wg sync.WaitGroup

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Disabling the delayed sequencer helps up check the
// message count easily
builder, cleanup := createL1AndL2Node(ctx, t, false)
defer cleanup()

err := waitForL1Node(ctx)
Require(t, err)

cleanEspresso := runEspresso()
defer cleanEspresso()

// wait for the builder
err = waitForEspressoNode(ctx)
Require(t, err)

// wait for the latest hotshot block
err = waitFor(ctx, func() bool {
out, err := exec.Command("curl", "http://127.0.0.1:41000/status/block-height", "-L").Output()
if err != nil {
return false
}
h := 0
err = json.Unmarshal(out, &h)
if err != nil {
return false
}
// Wait for the hotshot to generate some blocks to better simulate the real-world environment.
// Chosen based on intuition; no empirical data supports this value.
return h > 10
})
Require(t, err)

// Modify it manually
builder.L2.ConsensusNode.TxStreamer.UseEscapeHatch = true

txInterval := time.Second * 5
totalTx := uint64(20)

wg.Add(1)
go keepL2ChainMoving(t, ctx, builder.L2Info, builder.L2.Client, txInterval, totalTx, &wg)

address := common.HexToAddress(lightClientAddress)
txOpts := builder.L1Info.GetDefaultTransactOpts("Faucet", ctx)

wg.Add(1)
go keepEscapeHatchToggling(t, ctx, address, txOpts, builder, &wg)

err = waitForWith(ctx, time.Second*200, time.Second*5, func() bool {
msgCnt, err := builder.L2.ConsensusNode.TxStreamer.GetMessageCount()
Require(t, err)
return msgCnt >= arbutil.MessageIndex(totalTx)
})

Require(t, err)

t.Cleanup(func() {
cancel()
})
}

func keepEscapeHatchToggling(t *testing.T, ctx context.Context, address common.Address, txOpts bind.TransactOpts, builder *NodeBuilder, wg *sync.WaitGroup) {
defer wg.Done()
delay := time.Second * 12

for {
select {
case <-ctx.Done():
return
default:
if ctx.Err() != nil {
break
}
err := lightclientmock.FreezeL1Height(t, builder.L1.Client, address, &txOpts)
Require(t, err)

if err := sleepWithContext(ctx, delay); err != nil {
return
}

err = lightclientmock.UnfreezeL1Height(t, builder.L1.Client, address, &txOpts)
Require(t, err)

if err := sleepWithContext(ctx, delay); err != nil {
return
}
}
}
}

// Continuously send an L2 transaction to keep the chain moving.
func keepL2ChainMoving(t *testing.T, ctx context.Context, l2Info *BlockchainTestInfo, l2Client *ethclient.Client, delay time.Duration, total uint64, wg *sync.WaitGroup) {
defer wg.Done()

txCnt := uint64(0)
for {
select {
case <-ctx.Done():
return
default:
if ctx.Err() != nil {
break
}

if txCnt >= total {
break
}

if err := sleepWithContext(ctx, delay); err != nil {
return
}

tx := l2Info.PrepareTx("Faucet", "Faucet", 3e7, common.Big1, nil)
err := l2Client.SendTransaction(ctx, tx)
Require(t, err)
txCnt += 1
}
}
}

func sleepWithContext(ctx context.Context, delay time.Duration) error {
timer := time.NewTimer(delay)
defer timer.Stop()

select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}

0 comments on commit 17734f4

Please sign in to comment.