Skip to content

Commit

Permalink
chore: refactor op e2e tests - part 4 (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesterli authored Dec 4, 2024
1 parent 01402ac commit 22a3214
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
10 changes: 0 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,6 @@ test-e2e-op-ci: clean-e2e install-babylond
"xargs go test -race -mod=readonly -timeout=25m -v $(PACKAGES_E2E_OP) -count=1 --tags=e2e_op --run" \
--split-by=name --timings-type=name

DEVNET_REPO_URL := https://github.com/babylonlabs-io/op-e2e-devnet
TARGET_DIR := ./itest/opstackl2/devnet-data

.PHONY: op-e2e-devnet
op-e2e-devnet:
@rm -rf $(TARGET_DIR)
@mkdir -p $(TARGET_DIR)
@git clone $(DEVNET_REPO_URL) $(TARGET_DIR)
@echo "Devnet data downloaded to $(TARGET_DIR)"

###############################################################################
### Protobuf ###
###############################################################################
Expand Down
18 changes: 18 additions & 0 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ func (app *FinalityProviderApp) Start() error {
return startErr
}

// StartWithoutSyncFpStatus starts only the finality-provider daemon without any finality-provider instances
// and without syncing the finality-provider status loop for testing purpose
// b/c this loop detects FP status and then automatically starts the FP instance when it is ACTIVE
// Note: this is only for testing purposes
func (app *FinalityProviderApp) StartWithoutSyncFpStatus() error {
var startErr error
app.startOnce.Do(func() {
app.logger.Info("Starting FinalityProviderApp")

app.wg.Add(3)
go app.eventLoop()
go app.registrationLoop()
go app.metricsUpdateLoop()
})

return startErr
}

func (app *FinalityProviderApp) Stop() error {
var stopErr error
app.stopOnce.Do(func() {
Expand Down
14 changes: 3 additions & 11 deletions itest/opstackl2/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# OP-stack itest

To run the e2e tests, first you need to set up the devnet data:
Run the following command to start the e2e tests:

```bash
$ make op-e2e-devnet
```

Then run the following command to start the e2e tests:

```bash
$ make test-e2e-op

# Run all tests
$ make test-e2e-op
make test-e2e-op

# Filter specific test
$ make test-e2e-op-filter FILTER=TestFinalityGadget
make test-e2e-op-filter FILTER=TestPubRandCommitment
```
30 changes: 27 additions & 3 deletions itest/opstackl2/op_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,40 @@ package e2etest_op

import (
"testing"

"github.com/stretchr/testify/require"
)

// This test case will be removed by the final PR
func TestOpTestManagerSetup(t *testing.T) {
// TestPubRandCommitment tests the consumer controller's functions:
// - CommitPubRandList
// - QueryLastPublicRandCommit
func TestPubRandCommitment(t *testing.T) {
ctm := StartOpL2ConsumerManager(t)
defer ctm.Stop(t)

// create and register Babylon FP and OP consumer FP
fps := ctm.setupBabylonAndConsumerFp(t)

// send a BTC delegation and wait for activation
ctm.delegateBTCAndWaitForActivation(t, fps[0], fps[1])
consumerFpPk := fps[1]
ctm.delegateBTCAndWaitForActivation(t, fps[0], consumerFpPk)

// get the consumer FP instance
consumerFpInstance := ctm.getConsumerFpInstance(t, consumerFpPk)

// commit pub rand with start height 1
// this will call consumer controller's CommitPubRandList function
_, err := consumerFpInstance.CommitPubRand(1)
require.NoError(t, err)

// query the last pub rand
pubRand, err := ctm.OpConsumerController.QueryLastPublicRandCommit(consumerFpPk.MustToBTCPK())
require.NoError(t, err)
require.NotNil(t, pubRand)

// check the end height of the pub rand
// endHeight = startHeight + numberPubRand - 1
// startHeight is 1 in this case, so EndHeight should equal NumPubRand
consumerCfg := ctm.ConsumerFpApp.GetConfig()
require.Equal(t, uint64(consumerCfg.NumPubRand), pubRand.EndHeight())
}
25 changes: 24 additions & 1 deletion itest/opstackl2/op_test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
bbncc "github.com/babylonlabs-io/finality-provider/clientcontroller/babylon"
opcc "github.com/babylonlabs-io/finality-provider/clientcontroller/opstackl2"
cwclient "github.com/babylonlabs-io/finality-provider/cosmwasmclient/client"
"github.com/babylonlabs-io/finality-provider/eotsmanager/client"
eotsconfig "github.com/babylonlabs-io/finality-provider/eotsmanager/config"
fpcfg "github.com/babylonlabs-io/finality-provider/finality-provider/config"
"github.com/babylonlabs-io/finality-provider/finality-provider/service"
Expand Down Expand Up @@ -50,6 +51,7 @@ type OpL2ConsumerTestManager struct {
EOTSServerHandler *e2eutils.EOTSServerHandler
BabylonFpApp *service.FinalityProviderApp
ConsumerFpApp *service.FinalityProviderApp
ConsumerEOTSClient *client.EOTSManagerGRpcClient
}

// Config is the config of the OP finality gadget cw contract
Expand Down Expand Up @@ -139,6 +141,7 @@ func StartOpL2ConsumerManager(t *testing.T) *OpL2ConsumerTestManager {
EOTSServerHandler: eotsHandler,
BabylonFpApp: babylonFpApp,
ConsumerFpApp: consumerFpApp,
ConsumerEOTSClient: EOTSClients[1],
}

return ctm
Expand Down Expand Up @@ -335,6 +338,22 @@ func (ctm *OpL2ConsumerTestManager) setupBabylonAndConsumerFp(t *testing.T) []*b
return []*bbntypes.BIP340PubKey{babylonFpPk, consumerFpPk}
}

func (ctm *OpL2ConsumerTestManager) getConsumerFpInstance(
t *testing.T,
consumerFpPk *bbntypes.BIP340PubKey,
) *service.FinalityProviderInstance {
fpCfg := ctm.ConsumerFpApp.GetConfig()
fpStore := ctm.ConsumerFpApp.GetFinalityProviderStore()
pubRandStore := ctm.ConsumerFpApp.GetPubRandProofStore()
bc := ctm.BabylonFpApp.GetBabylonController()
logger := ctm.ConsumerFpApp.Logger()
fpInstance, err := service.TestNewUnregisteredFinalityProviderInstance(
consumerFpPk, fpCfg, fpStore, pubRandStore, bc, ctm.OpConsumerController, ctm.ConsumerEOTSClient,
metrics.NewFpMetrics(), "", make(chan<- *service.CriticalError), logger)
require.NoError(t, err)
return fpInstance
}

func (ctm *OpL2ConsumerTestManager) delegateBTCAndWaitForActivation(t *testing.T, babylonFpPk *bbntypes.BIP340PubKey, consumerFpPk *bbntypes.BIP340PubKey) {
// send a BTC delegation
ctm.InsertBTCDelegation(t, []*btcec.PublicKey{babylonFpPk.MustToBTCPK(), consumerFpPk.MustToBTCPK()},
Expand All @@ -355,9 +374,13 @@ func (ctm *OpL2ConsumerTestManager) delegateBTCAndWaitForActivation(t *testing.T
func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) {
t.Log("Stopping test manager")
var err error
err = ctm.BabylonFpApp.Stop()
require.NoError(t, err)
err = ctm.ConsumerFpApp.Stop()
require.NoError(t, err)
err = ctm.BabylonHandler.Stop()
require.NoError(t, err)

ctm.EOTSServerHandler.Stop()
err = os.RemoveAll(ctm.BaseDir)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion itest/test-manager/base_test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func CreateAndStartFpApp(
fpApp, err := service.NewFinalityProviderApp(cfg, bc, cc, eotsCli, fpdb, logger)
require.NoError(t, err)

err = fpApp.Start()
err = fpApp.StartWithoutSyncFpStatus()
require.NoError(t, err)

return fpApp
Expand Down

0 comments on commit 22a3214

Please sign in to comment.