From be1c9f2f98805f8ab13644940519729288a0b269 Mon Sep 17 00:00:00 2001 From: parketh Date: Thu, 1 Aug 2024 23:22:22 +0800 Subject: [PATCH 01/10] test: add back finality gadget e2e test --- Makefile | 5 +- go.mod | 4 +- go.sum | 8 +-- itest/opstackl2/op_e2e_test.go | 49 +++++++++++++++++ itest/opstackl2/op_test_manager.go | 86 +++++++++++++++++++++++++----- 5 files changed, 130 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 4917a0d1..016096cb 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ TOOLS_DIR := tools BABYLON_PKG := github.com/babylonlabs-io/babylon/cmd/babylond WASMD_PKG := github.com/CosmWasm/wasmd/cmd/wasmd -BCD_PKG := github.com/babylonchain/babylon-sdk/demo/cmd/bcd +BCD_PKG := github.com/babylonlabs-io/babylon-sdk/demo/cmd/bcd GO_BIN := ${GOPATH}/bin BTCD_BIN := $(GO_BIN)/btcd @@ -112,8 +112,9 @@ test-e2e-bcd: clean-e2e install-babylond install-bcd test-e2e-wasmd: clean-e2e install-babylond install-wasmd @go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_wasmd +FILTER ?= . test-e2e-op: clean-e2e install-babylond - @go test -race -mod=readonly -timeout=25m -v $(PACKAGES_E2E_OP) -count=1 --tags=e2e_op + @go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E_OP) -count=1 --tags=e2e_op --run ^$(FILTER)$ test-e2e-op-ci: clean-e2e install-babylond go test -list . ./itest/opstackl2 --tags=e2e_op | grep Test \ diff --git a/go.mod b/go.mod index 92cb6b67..e54ae3db 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/avast/retry-go/v4 v4.5.1 github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 github.com/babylonlabs-io/babylon v0.9.0 - github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801002122-6d6bca47ec56 + github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af github.com/btcsuite/btcd v0.24.2 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/btcsuite/btcd/btcutil v1.1.5 @@ -381,7 +381,7 @@ require ( github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect github.com/zondax/hid v0.9.2 // indirect github.com/zondax/ledger-go v0.14.3 // indirect - go.etcd.io/bbolt v1.3.8 // indirect + go.etcd.io/bbolt v1.3.10 // indirect go.etcd.io/etcd/api/v3 v3.5.10 // indirect go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect go.etcd.io/etcd/client/v2 v2.305.10 // indirect diff --git a/go.sum b/go.sum index 75f2118a..5caaaa9a 100644 --- a/go.sum +++ b/go.sum @@ -300,8 +300,8 @@ github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 h1:V github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:X4QovCWMwqMjoTriu18w4gfqX1sYqOWRnZwXGYD8bnE= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 h1:W8jr9BHOLD9RJviI6TmnWPwZpMSjliQNIbNuflBx6vo= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801002122-6d6bca47ec56 h1:xZ+x4nfezT99jbBYDfqxQx+KBmqICbJtu8WLrjS1sVY= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801002122-6d6bca47ec56/go.mod h1:IZUMNT4MCzKPzsniq5EeNITWy/HeNX5k8hPoJNjFSqQ= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af h1:dMjsU7c7PV3XamSscfIp++3AHXtwQQTe7yKKq39B61g= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js= github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866 h1:FjrkthFS4JRGCckPh/uTdznhqwQOzjD8JJ0blcrS1VY= @@ -1675,8 +1675,8 @@ github.com/zondax/ledger-go v0.14.3 h1:wEpJt2CEcBJ428md/5MgSLsXLBos98sBOyxNmCjfU github.com/zondax/ledger-go v0.14.3/go.mod h1:IKKaoxupuB43g4NxeQmbLXv7T9AlQyie1UpHb342ycI= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.5-0.20200615073812-232d8fc87f50/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= -go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA= -go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= +go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0= +go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ= go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= go.etcd.io/etcd/api/v3 v3.5.10 h1:szRajuUUbLyppkhs9K6BRtjY37l66XQQmw7oZRANE4k= go.etcd.io/etcd/api/v3 v3.5.10/go.mod h1:TidfmT4Uycad3NM/o25fG3J07odo4GBB9hoxaodFCtI= diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index 445c7fe9..498fdd91 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -5,6 +5,7 @@ package e2etest_op import ( "encoding/hex" + "fmt" "testing" "time" @@ -185,3 +186,51 @@ func TestFinalityStuckAndRecover(t *testing.T) { "OP chain fianlity is recovered, the latest finalized block height %d", ), nextFinalizedHeight) } + +func TestFinalityGadget(t *testing.T) { + // start the consumer manager + ctm := StartOpL2ConsumerManager(t, 2) + defer ctm.Stop(t) + + // register, get BTC delegations, and start FPs + n := 2 + fpList := ctm.SetupFinalityProviders(t, n, []stakingParam{ + // for the first FP, we give it more power b/c it will be used later + {e2eutils.StakingTime, 3 * e2eutils.StakingAmount}, + {e2eutils.StakingTime, e2eutils.StakingAmount}, + }) + + // check both FPs have committed their first public randomness + // TODO: we might use go routine to do this in parallel + for i := 0; i < n; i++ { + e2eutils.WaitForFpPubRandCommitted(t, fpList[i]) + } + + // wait until the BTC staking is activated + l2BlockAfterActivation := ctm.waitForBTCStakingActivation(t) + + // both FP will sign the first block + targetBlockHeight := ctm.WaitForTargetBlockPubRand(t, fpList, l2BlockAfterActivation) + ctm.WaitForFpVoteReachHeight(t, fpList[0], targetBlockHeight) + ctm.WaitForFpVoteReachHeight(t, fpList[1], targetBlockHeight) + t.Logf(log.Prefix("Both FP instances signed the first block")) + + // both FP will sign the second block + ctm.WaitForFpVoteReachHeight(t, fpList[0], targetBlockHeight+1) + ctm.WaitForFpVoteReachHeight(t, fpList[1], targetBlockHeight+1) + t.Logf(log.Prefix("Both FP instances signed the second block")) + + // run the finality gadget + t.Logf(log.Prefix("Starting finality gadget")) + err := ctm.FinalityGadgetServer.RunUntilShutdown() + require.NoError(t, err) + require.Eventually(t, func() bool { + block, err := ctm.FinalityGadgetClient.GetLatestBlock() + require.NoError(t, err) + return block.BlockHeight == targetBlockHeight+1 + }, 5*time.Second, time.Second, "Failed to process blocks") + + // get latest finalized block via API and check response + fmt.Printf("targetBlockHeight: %d\n", targetBlockHeight) + ctm.checkLatestBlock(t, targetBlockHeight+1) +} diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index 2156d18d..1b61e1e2 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -14,9 +14,14 @@ import ( "time" sdkmath "cosmossdk.io/math" + fgclient "github.com/babylonlabs-io/babylon-finality-gadget/client" + "github.com/babylonlabs-io/babylon-finality-gadget/db" + "github.com/babylonlabs-io/babylon-finality-gadget/finalitygadget" + fgcfg "github.com/babylonlabs-io/babylon-finality-gadget/finalitygadget/config" "github.com/babylonlabs-io/babylon-finality-gadget/sdk/btcclient" sdkclient "github.com/babylonlabs-io/babylon-finality-gadget/sdk/client" sdkcfg "github.com/babylonlabs-io/babylon-finality-gadget/sdk/config" + fgsrv "github.com/babylonlabs-io/babylon-finality-gadget/server" bbncfg "github.com/babylonlabs-io/babylon/client/config" bbntypes "github.com/babylonlabs-io/babylon/types" api "github.com/babylonlabs-io/finality-provider/clientcontroller/api" @@ -37,6 +42,7 @@ import ( ope2e "github.com/ethereum-optimism/optimism/op-e2e" optestlog "github.com/ethereum-optimism/optimism/op-service/testlog" gethlog "github.com/ethereum/go-ethereum/log" + sig "github.com/lightningnetwork/lnd/signal" "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -52,13 +58,16 @@ type BaseTestManager = base_test_manager.BaseTestManager type OpL2ConsumerTestManager struct { BaseTestManager - BabylonHandler *e2eutils.BabylonNodeHandler - BabylonFpApp *service.FinalityProviderApp - ConsumerFpApps []*service.FinalityProviderApp - EOTSServerHandler *e2eutils.EOTSServerHandler - BaseDir string - SdkClient *sdkclient.SdkClient - OpSystem *ope2e.System + BabylonHandler *e2eutils.BabylonNodeHandler + BabylonFpApp *service.FinalityProviderApp + ConsumerFpApps []*service.FinalityProviderApp + EOTSServerHandler *e2eutils.EOTSServerHandler + BaseDir string + SdkClient *sdkclient.SdkClient + FinalityGadgetServer *fgsrv.Server + FinalityGadgetClient *fgclient.FinalityGadgetGrpcClient + Db *db.BBoltHandler + OpSystem *ope2e.System } func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2ConsumerTestManager { @@ -109,18 +118,59 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume require.NoError(t, err) t.Logf(log.Prefix("Register consumer %s to Babylon"), opConsumerId) + // Define finality gadget configs + cfg := fgcfg.Config{ + L2RPCHost: opL2ConsumerConfig.OPStackL2RPCAddress, + BitcoinRPCHost: trimLeadingHttp(opSys.Cfg.DeployConfig.BabylonFinalityGadgetBitcoinRpc), + FGContractAddress: opSys.Cfg.DeployConfig.BabylonFinalityGadgetContractAddress, + BBNChainID: opSys.Cfg.DeployConfig.BabylonFinalityGadgetChainID, + BBNRPCAddress: opL2ConsumerConfig.RPCAddr, + DBFilePath: "data.db", + GRPCServerPort: "8080", + PollInterval: time.Second * time.Duration(10), + } + + // Init local DB for storing and querying blocks + db, err := db.NewBBoltHandler(cfg.DBFilePath) + require.NoError(t, err) + defer db.Close() + err = db.TryCreateInitialBuckets() + require.NoError(t, err) + + // Create finality gadget + fg, err := finalitygadget.NewFinalityGadget(&cfg, db) + require.NoError(t, err) + + // Start grpc server + // Hook interceptor for os signals. + shutdownInterceptor, err := sig.Intercept() + require.NoError(t, err) + srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor) + go func() { + err = srv.RunUntilShutdown() + require.NoError(t, err) + }() + + // Create grpc client + hostAddr := "localhost:" + cfg.GRPCServerPort + client, err := fgclient.NewFinalityGadgetGrpcClient(db, hostAddr) + require.NoError(t, err) + ctm := &OpL2ConsumerTestManager{ BaseTestManager: BaseTestManager{ BBNClient: babylonClient, CovenantPrivKeys: covenantPrivKeys, }, - BabylonHandler: bh, - EOTSServerHandler: eotsHandler, - BabylonFpApp: babylonFpApp, - ConsumerFpApps: consumerFpApps, - BaseDir: testDir, - SdkClient: sdkClient, - OpSystem: opSys, + BabylonHandler: bh, + EOTSServerHandler: eotsHandler, + BabylonFpApp: babylonFpApp, + ConsumerFpApps: consumerFpApps, + BaseDir: testDir, + SdkClient: sdkClient, + FinalityGadgetServer: srv, + FinalityGadgetClient: client, + Db: db, + OpSystem: opSys, } ctm.WaitForServicesStart(t) @@ -928,6 +978,12 @@ func (ctm *OpL2ConsumerTestManager) waitForBTCStakingActivation(t *testing.T) ui return l2BlockAfterActivation } +func (ctm *OpL2ConsumerTestManager) checkLatestBlock(t *testing.T, exp uint64) { + block, err := ctm.FinalityGadgetClient.GetLatestBlock() + require.NoError(t, err) + require.Equal(t, exp, block.BlockHeight) +} + func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) { t.Log("Stopping test manager") var err error @@ -949,4 +1005,6 @@ func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) { ctm.EOTSServerHandler.Stop() err = os.RemoveAll(ctm.BaseDir) require.NoError(t, err) + err = ctm.Db.Close() + require.NoError(t, err) } From fa6469c9fee7dbc00e1dde998f8317751186f325 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 13:01:35 +0100 Subject: [PATCH 02/10] test: fix finality gadget e2e test --- go.mod | 2 +- go.sum | 4 ++-- itest/eotsmanager_handler.go | 14 ++++++------- itest/opstackl2/op_e2e_test.go | 19 +++++++++--------- itest/opstackl2/op_test_manager.go | 32 ++++++++++++++++++++++-------- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index e54ae3db..68febcd7 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/avast/retry-go/v4 v4.5.1 github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 github.com/babylonlabs-io/babylon v0.9.0 - github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af + github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5 github.com/btcsuite/btcd v0.24.2 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/btcsuite/btcd/btcutil v1.1.5 diff --git a/go.sum b/go.sum index 5caaaa9a..0c03c292 100644 --- a/go.sum +++ b/go.sum @@ -300,8 +300,8 @@ github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 h1:V github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:X4QovCWMwqMjoTriu18w4gfqX1sYqOWRnZwXGYD8bnE= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 h1:W8jr9BHOLD9RJviI6TmnWPwZpMSjliQNIbNuflBx6vo= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af h1:dMjsU7c7PV3XamSscfIp++3AHXtwQQTe7yKKq39B61g= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240801151637-036cb072d0af/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5 h1:VR4/NyySQc5ar7mqX3O6S3X2pR/hL64mRuNJIXKfGdw= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js= github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866 h1:FjrkthFS4JRGCckPh/uTdznhqwQOzjD8JJ0blcrS1VY= diff --git a/itest/eotsmanager_handler.go b/itest/eotsmanager_handler.go index 5e65e470..f0b38f3e 100644 --- a/itest/eotsmanager_handler.go +++ b/itest/eotsmanager_handler.go @@ -19,11 +19,8 @@ type EOTSServerHandler struct { } func NewEOTSServerHandlerMultiFP( - t *testing.T, configs []*config.Config, eotsHomeDirs []string, logger *zap.Logger, + t *testing.T, configs []*config.Config, eotsHomeDirs []string, logger *zap.Logger, shutdownInterceptor *signal.Interceptor, ) *EOTSServerHandler { - shutdownInterceptor, err := signal.Intercept() - require.NoError(t, err) - eotsServers := make([]*service.Server, 0, len(configs)) for i, cfg := range configs { dbBackend, err := cfg.DatabaseConfig.GetDbBackend() @@ -32,21 +29,24 @@ func NewEOTSServerHandlerMultiFP( eotsManager, err := eotsmanager.NewLocalEOTSManager(eotsHomeDirs[i], cfg.KeyringBackend, dbBackend, logger) require.NoError(t, err) - eotsServer := service.NewEOTSManagerServer(cfg, logger, eotsManager, dbBackend, shutdownInterceptor) + eotsServer := service.NewEOTSManagerServer(cfg, logger, eotsManager, dbBackend, *shutdownInterceptor) eotsServers = append(eotsServers, eotsServer) } return &EOTSServerHandler{ t: t, eotsServers: eotsServers, - interceptor: &shutdownInterceptor, + interceptor: shutdownInterceptor, } } func NewEOTSServerHandler(t *testing.T, cfg *config.Config, eotsHomeDir string) *EOTSServerHandler { // TODO: no-op logger makes it hard to debug. replace w real logger. + // create shutdown interceptor + shutdownInterceptor, err := signal.Intercept() + require.NoError(t, err) // this need refactor of NewEOTSServerHandler - return NewEOTSServerHandlerMultiFP(t, []*config.Config{cfg}, []string{eotsHomeDir}, zap.NewNop()) + return NewEOTSServerHandlerMultiFP(t, []*config.Config{cfg}, []string{eotsHomeDir}, zap.NewNop(), &shutdownInterceptor) } func (eh *EOTSServerHandler) Start() { diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index 498fdd91..d8342fd6 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -5,7 +5,6 @@ package e2etest_op import ( "encoding/hex" - "fmt" "testing" "time" @@ -221,16 +220,16 @@ func TestFinalityGadget(t *testing.T) { t.Logf(log.Prefix("Both FP instances signed the second block")) // run the finality gadget - t.Logf(log.Prefix("Starting finality gadget")) - err := ctm.FinalityGadgetServer.RunUntilShutdown() - require.NoError(t, err) + go func() { + t.Logf(log.Prefix("Starting finality gadget")) + err := ctm.FinalityGadget.ProcessBlocks() + require.NoError(t, err) + }() + + // check latest block require.Eventually(t, func() bool { block, err := ctm.FinalityGadgetClient.GetLatestBlock() require.NoError(t, err) - return block.BlockHeight == targetBlockHeight+1 - }, 5*time.Second, time.Second, "Failed to process blocks") - - // get latest finalized block via API and check response - fmt.Printf("targetBlockHeight: %d\n", targetBlockHeight) - ctm.checkLatestBlock(t, targetBlockHeight+1) + return block.BlockHeight > targetBlockHeight+6 + }, 40*time.Second, 5*time.Second, "Failed to process blocks") } diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index 1b61e1e2..3e41f845 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + defaultlog "log" "os" "path/filepath" "strings" @@ -42,7 +43,7 @@ import ( ope2e "github.com/ethereum-optimism/optimism/op-e2e" optestlog "github.com/ethereum-optimism/optimism/op-service/testlog" gethlog "github.com/ethereum/go-ethereum/log" - sig "github.com/lightningnetwork/lnd/signal" + "github.com/lightningnetwork/lnd/signal" "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -64,13 +65,15 @@ type OpL2ConsumerTestManager struct { EOTSServerHandler *e2eutils.EOTSServerHandler BaseDir string SdkClient *sdkclient.SdkClient - FinalityGadgetServer *fgsrv.Server + FinalityGadget *finalitygadget.FinalityGadget FinalityGadgetClient *fgclient.FinalityGadgetGrpcClient Db *db.BBoltHandler OpSystem *ope2e.System } func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2ConsumerTestManager { + defaultlog.SetOutput(os.Stdout) + // Setup base dir and logger testDir, err := e2eutils.BaseDir("fpe2etest") require.NoError(t, err) @@ -96,6 +99,10 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume // init SDK client sdkClient := initSdkClient(opSys, opL2ConsumerConfig, t) + // create shutdown interceptor + shutdownInterceptor, err := signal.Intercept() + require.NoError(t, err) + // start multiple FPs. each FP has its own EOTS manager and finality provider app // there is one Babylon FP and multiple Consumer FPs babylonFpApp, consumerFpApps, eotsHandler := createMultiFps( @@ -104,6 +111,7 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume opL2ConsumerConfig, numOfConsumerFPs+1, logger, + &shutdownInterceptor, t, ) @@ -133,28 +141,29 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume // Init local DB for storing and querying blocks db, err := db.NewBBoltHandler(cfg.DBFilePath) require.NoError(t, err) - defer db.Close() err = db.TryCreateInitialBuckets() require.NoError(t, err) + t.Logf(log.Prefix("Init local DB for finality gadget server")) // Create finality gadget fg, err := finalitygadget.NewFinalityGadget(&cfg, db) require.NoError(t, err) + t.Logf(log.Prefix("Created finality gadget")) // Start grpc server // Hook interceptor for os signals. - shutdownInterceptor, err := sig.Intercept() - require.NoError(t, err) srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor) go func() { err = srv.RunUntilShutdown() require.NoError(t, err) }() + t.Logf(log.Prefix("Started finality gadget grpc server")) // Create grpc client hostAddr := "localhost:" + cfg.GRPCServerPort client, err := fgclient.NewFinalityGadgetGrpcClient(db, hostAddr) require.NoError(t, err) + t.Logf(log.Prefix("Started finality gadget grpc client")) ctm := &OpL2ConsumerTestManager{ BaseTestManager: BaseTestManager{ @@ -167,7 +176,7 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume ConsumerFpApps: consumerFpApps, BaseDir: testDir, SdkClient: sdkClient, - FinalityGadgetServer: srv, + FinalityGadget: fg, FinalityGadgetClient: client, Db: db, OpSystem: opSys, @@ -183,6 +192,7 @@ func createMultiFps( opL2ConsumerConfig *fpcfg.OPStackL2Config, numOfConsumerFPs uint8, logger *zap.Logger, + shutdownInterceptor *signal.Interceptor, t *testing.T, ) (*service.FinalityProviderApp, []*service.FinalityProviderApp, *e2eutils.EOTSServerHandler) { babylonFpCfg, consumerFpCfgs := createFpConfigs( @@ -194,7 +204,7 @@ func createMultiFps( t, ) - eotsHandler, eotsClients := startEotsManagers(testDir, t, babylonFpCfg, consumerFpCfgs, logger) + eotsHandler, eotsClients := startEotsManagers(testDir, t, babylonFpCfg, consumerFpCfgs, logger, shutdownInterceptor) babylonFpApp, consumerFpApps := createFpApps( babylonFpCfg, @@ -402,6 +412,7 @@ func startEotsManagers( babylonFpCfg *fpcfg.Config, consumerFpCfgs []*fpcfg.Config, logger *zap.Logger, + shutdownInterceptor *signal.Interceptor, ) (*e2eutils.EOTSServerHandler, []*client.EOTSManagerGRpcClient) { allConfigs := append([]*fpcfg.Config{babylonFpCfg}, consumerFpCfgs...) eotsClients := make([]*client.EOTSManagerGRpcClient, len(allConfigs)) @@ -423,7 +434,7 @@ func startEotsManagers( ) eotsConfigs[i] = eotsCfg } - eh := e2eutils.NewEOTSServerHandlerMultiFP(t, eotsConfigs, eotsHomeDirs, logger) + eh := e2eutils.NewEOTSServerHandlerMultiFP(t, eotsConfigs, eotsHomeDirs, logger, shutdownInterceptor) eh.Start() // create EOTS clients @@ -999,12 +1010,17 @@ func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) { t.Logf(log.Prefix("Stopped Consumer FP App %d"), i) } + err = ctm.FinalityGadget.DeleteDB() + require.NoError(t, err) ctm.OpSystem.Close() err = ctm.BabylonHandler.Stop() require.NoError(t, err) ctm.EOTSServerHandler.Stop() err = os.RemoveAll(ctm.BaseDir) require.NoError(t, err) + ctm.FinalityGadget.Close() + err = ctm.FinalityGadgetClient.Close() + require.NoError(t, err) err = ctm.Db.Close() require.NoError(t, err) } From a4ee62b52464a7582efbbd3a9b2a3638684a1d65 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 13:01:46 +0100 Subject: [PATCH 03/10] docs: add cmd for single test case --- itest/opstackl2/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/itest/opstackl2/README.md b/itest/opstackl2/README.md index bcd495f9..8001c5e2 100644 --- a/itest/opstackl2/README.md +++ b/itest/opstackl2/README.md @@ -12,4 +12,7 @@ Then run the following command to start the e2e tests: ```bash $ make test-e2e-op -``` \ No newline at end of file + +# Filter specific test +$ make test-e2e-op Filter=TestFinalityGadget +``` From bef4546cbb48395572e962a0b7989909f01b0ed3 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 13:04:33 +0100 Subject: [PATCH 04/10] test: update ci parallelism for new test case --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4e4eb1a0..08c42352 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -143,7 +143,7 @@ jobs: test_e2e_op: machine: image: ubuntu-2204:2024.01.1 - parallelism: 3 + parallelism: 4 resource_class: large steps: - go/install: From 7d9736c1a1d52909de243c9ed57e78ccd40ed195 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 13:25:54 +0100 Subject: [PATCH 05/10] test: add option to skip finality gadget in test manager --- itest/opstackl2/op_e2e_test.go | 8 ++-- itest/opstackl2/op_test_manager.go | 77 +++++++++++++++++------------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index d8342fd6..ecfb41fe 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -17,7 +17,7 @@ import ( // tests the finality signature submission to the op-finality-gadget contract func TestOpSubmitFinalitySignature(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 1) + ctm := StartOpL2ConsumerManager(t, 1, false) defer ctm.Stop(t) consumerFpPkList := ctm.RegisterConsumerFinalityProvider(t, 1) @@ -54,7 +54,7 @@ func TestOpSubmitFinalitySignature(t *testing.T) { // 1. block has both two FP signs, so it would be finalized // 2. block has only one FP with smaller power (1/4) signs, so it would not be considered as finalized func TestOpMultipleFinalityProviders(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 2) + ctm := StartOpL2ConsumerManager(t, 2, false) defer ctm.Stop(t) // register, get BTC delegations, and start FPs @@ -125,7 +125,7 @@ func TestOpMultipleFinalityProviders(t *testing.T) { } func TestFinalityStuckAndRecover(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 1) + ctm := StartOpL2ConsumerManager(t, 1, false) defer ctm.Stop(t) // register, get BTC delegations, and start FPs @@ -188,7 +188,7 @@ func TestFinalityStuckAndRecover(t *testing.T) { func TestFinalityGadget(t *testing.T) { // start the consumer manager - ctm := StartOpL2ConsumerManager(t, 2) + ctm := StartOpL2ConsumerManager(t, 2, true) defer ctm.Stop(t) // register, get BTC delegations, and start FPs diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index 3e41f845..e4bbe2d6 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -71,7 +71,7 @@ type OpL2ConsumerTestManager struct { OpSystem *ope2e.System } -func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2ConsumerTestManager { +func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8, runFinalityGadget bool) *OpL2ConsumerTestManager { defaultlog.SetOutput(os.Stdout) // Setup base dir and logger @@ -138,32 +138,41 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume PollInterval: time.Second * time.Duration(10), } - // Init local DB for storing and querying blocks - db, err := db.NewBBoltHandler(cfg.DBFilePath) - require.NoError(t, err) - err = db.TryCreateInitialBuckets() - require.NoError(t, err) - t.Logf(log.Prefix("Init local DB for finality gadget server")) - - // Create finality gadget - fg, err := finalitygadget.NewFinalityGadget(&cfg, db) - require.NoError(t, err) - t.Logf(log.Prefix("Created finality gadget")) + // Start finality gadget + var database *db.BBoltHandler + var finalityGadget *finalitygadget.FinalityGadget + var client *fgclient.FinalityGadgetGrpcClient + if runFinalityGadget { + // Init local DB for storing and querying blocks + db, err := db.NewBBoltHandler(cfg.DBFilePath) + require.NoError(t, err) + database = db + err = db.TryCreateInitialBuckets() + require.NoError(t, err) + t.Logf(log.Prefix("Init local DB for finality gadget server")) - // Start grpc server - // Hook interceptor for os signals. - srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor) - go func() { - err = srv.RunUntilShutdown() + // Create finality gadget + fg, err := finalitygadget.NewFinalityGadget(&cfg, db) require.NoError(t, err) - }() - t.Logf(log.Prefix("Started finality gadget grpc server")) + finalityGadget = fg + t.Logf(log.Prefix("Created finality gadget")) + + // Start grpc server + // Hook interceptor for os signals. + srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor) + go func() { + err = srv.RunUntilShutdown() + require.NoError(t, err) + }() + t.Logf(log.Prefix("Started finality gadget grpc server")) - // Create grpc client - hostAddr := "localhost:" + cfg.GRPCServerPort - client, err := fgclient.NewFinalityGadgetGrpcClient(db, hostAddr) - require.NoError(t, err) - t.Logf(log.Prefix("Started finality gadget grpc client")) + // Create grpc client + hostAddr := "localhost:" + cfg.GRPCServerPort + fgClient, err := fgclient.NewFinalityGadgetGrpcClient(db, hostAddr) + require.NoError(t, err) + client = fgClient + t.Logf(log.Prefix("Started finality gadget grpc client")) + } ctm := &OpL2ConsumerTestManager{ BaseTestManager: BaseTestManager{ @@ -176,9 +185,9 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume ConsumerFpApps: consumerFpApps, BaseDir: testDir, SdkClient: sdkClient, - FinalityGadget: fg, + FinalityGadget: finalityGadget, FinalityGadgetClient: client, - Db: db, + Db: database, OpSystem: opSys, } @@ -1010,17 +1019,19 @@ func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) { t.Logf(log.Prefix("Stopped Consumer FP App %d"), i) } - err = ctm.FinalityGadget.DeleteDB() - require.NoError(t, err) + if ctm.FinalityGadget != nil { + err = ctm.FinalityGadget.DeleteDB() + require.NoError(t, err) + ctm.FinalityGadget.Close() + err = ctm.FinalityGadgetClient.Close() + require.NoError(t, err) + err = ctm.Db.Close() + require.NoError(t, err) + } ctm.OpSystem.Close() err = ctm.BabylonHandler.Stop() require.NoError(t, err) ctm.EOTSServerHandler.Stop() err = os.RemoveAll(ctm.BaseDir) require.NoError(t, err) - ctm.FinalityGadget.Close() - err = ctm.FinalityGadgetClient.Close() - require.NoError(t, err) - err = ctm.Db.Close() - require.NoError(t, err) } From bc4dde323d5131d0288598cad14c59033d2e15a4 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 13:29:39 +0100 Subject: [PATCH 06/10] test: fix single fp test case --- itest/opstackl2/op_e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index ecfb41fe..f6b5ef57 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -46,7 +46,7 @@ func TestOpSubmitFinalitySignature(t *testing.T) { // note: QueryFinalityProviderHasPower is hardcode to return true so FPs can still submit finality sigs even if they // don't have voting power. But the finality sigs will not be counted at tally time. _, err = ctm.SdkClient.QueryIsBlockBabylonFinalized(queryParams) - require.ErrorIs(t, err, sdkclient.ErrNoFpHasVotingPower) + require.ErrorIs(t, err, sdkclient.ErrBtcStakingNotActivated) t.Logf(log.Prefix("Expected no voting power")) } From 5815f78abf5157c3281d8a90378a3cd462a092cc Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 14:15:02 +0100 Subject: [PATCH 07/10] test: improve finality gadget shutdown --- go.mod | 2 +- go.sum | 4 ++-- itest/opstackl2/op_e2e_test.go | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 68febcd7..a1c26b41 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/avast/retry-go/v4 v4.5.1 github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 github.com/babylonlabs-io/babylon v0.9.0 - github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5 + github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c github.com/btcsuite/btcd v0.24.2 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/btcsuite/btcd/btcutil v1.1.5 diff --git a/go.sum b/go.sum index 0c03c292..fb236ce0 100644 --- a/go.sum +++ b/go.sum @@ -300,8 +300,8 @@ github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 h1:V github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:X4QovCWMwqMjoTriu18w4gfqX1sYqOWRnZwXGYD8bnE= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 h1:W8jr9BHOLD9RJviI6TmnWPwZpMSjliQNIbNuflBx6vo= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5 h1:VR4/NyySQc5ar7mqX3O6S3X2pR/hL64mRuNJIXKfGdw= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805113957-c2b799ee30f5/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c h1:RgaS4KpJ5VQm04TJeg+GDUhLrwOGgiFWYJ/2Cv8f3+o= +github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js= github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866 h1:FjrkthFS4JRGCckPh/uTdznhqwQOzjD8JJ0blcrS1VY= diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index f6b5ef57..8a642e1d 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -4,6 +4,7 @@ package e2etest_op import ( + "context" "encoding/hex" "testing" "time" @@ -220,9 +221,10 @@ func TestFinalityGadget(t *testing.T) { t.Logf(log.Prefix("Both FP instances signed the second block")) // run the finality gadget + ctx, cancel := context.WithCancel(context.Background()) go func() { t.Logf(log.Prefix("Starting finality gadget")) - err := ctm.FinalityGadget.ProcessBlocks() + err := ctm.FinalityGadget.ProcessBlocks(ctx) require.NoError(t, err) }() @@ -232,4 +234,7 @@ func TestFinalityGadget(t *testing.T) { require.NoError(t, err) return block.BlockHeight > targetBlockHeight+6 }, 40*time.Second, 5*time.Second, "Failed to process blocks") + + // stop the finality gadget + cancel() } From cf69c49aaa759f8a05d97d2d112060113808e3b5 Mon Sep 17 00:00:00 2001 From: parketh Date: Mon, 5 Aug 2024 23:00:45 +0100 Subject: [PATCH 08/10] fix: rename `finality-gadget` dep --- go.mod | 5 +++-- go.sum | 8 ++++---- itest/opstackl2/op_e2e_test.go | 4 ++-- itest/opstackl2/op_test_manager.go | 16 ++++++++-------- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index a1c26b41..ccabf914 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/avast/retry-go/v4 v4.5.1 github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 github.com/babylonlabs-io/babylon v0.9.0 - github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c + github.com/babylonlabs-io/finality-gadget v0.1.0 github.com/btcsuite/btcd v0.24.2 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/btcsuite/btcd/btcutil v1.1.5 @@ -440,8 +440,9 @@ replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 github.com/babylonlabs-io/babylon => github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 + github.com/babylonlabs-io/finality-gadget => github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a github.com/cockroachdb/pebble => github.com/cockroachdb/pebble v0.0.0-20231018212520-f6cde3fc2fa4 - github.com/ethereum-optimism/optimism => github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866 + github.com/ethereum-optimism/optimism => github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28 github.com/ethereum/go-ethereum => github.com/ethereum-optimism/op-geth v1.101315.1-rc.5 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 diff --git a/go.sum b/go.sum index fb236ce0..fde525aa 100644 --- a/go.sum +++ b/go.sum @@ -300,12 +300,12 @@ github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 h1:V github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:X4QovCWMwqMjoTriu18w4gfqX1sYqOWRnZwXGYD8bnE= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 h1:W8jr9BHOLD9RJviI6TmnWPwZpMSjliQNIbNuflBx6vo= github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c h1:RgaS4KpJ5VQm04TJeg+GDUhLrwOGgiFWYJ/2Cv8f3+o= -github.com/babylonlabs-io/babylon-finality-gadget v0.0.0-20240805131414-fb077df2023c/go.mod h1:+7ssrcyvdI/X7dqU8e2/wVMAAwlFqSQh3RwObQsoeOY= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js= -github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866 h1:FjrkthFS4JRGCckPh/uTdznhqwQOzjD8JJ0blcrS1VY= -github.com/babylonlabs-io/optimism v1.8.1-0.20240731214220-e7d71bf38866/go.mod h1:MAppo9HosEUwyUCNnvJxJb3GQr5iP72HYb0Xv5YY4JE= +github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a h1:wMQ0sCWzwjXMXkww3kr5N0MgjizmulVGKvNcvyTEGhg= +github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a/go.mod h1:+BICJA7mUJpfYOSYds1G5w6nLO0gKVe9XwdmpIUiXmk= +github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28 h1:1xw+4A+IAvHff0f02UsXR1h0bg04Xn726ZvEAwrc/7Y= +github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28/go.mod h1:cVDnobSCpjb8XIfy8/8tlnm1gkO9lV2xfCezG/7YacY= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index 8a642e1d..b75c0fe5 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -9,8 +9,8 @@ import ( "testing" "time" - sdkclient "github.com/babylonlabs-io/babylon-finality-gadget/sdk/client" - "github.com/babylonlabs-io/babylon-finality-gadget/sdk/cwclient" + sdkclient "github.com/babylonlabs-io/finality-gadget/sdk/client" + "github.com/babylonlabs-io/finality-gadget/sdk/cwclient" e2eutils "github.com/babylonlabs-io/finality-provider/itest" "github.com/babylonlabs-io/finality-provider/testutil/log" "github.com/stretchr/testify/require" diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index e4bbe2d6..17a74bd5 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -15,16 +15,16 @@ import ( "time" sdkmath "cosmossdk.io/math" - fgclient "github.com/babylonlabs-io/babylon-finality-gadget/client" - "github.com/babylonlabs-io/babylon-finality-gadget/db" - "github.com/babylonlabs-io/babylon-finality-gadget/finalitygadget" - fgcfg "github.com/babylonlabs-io/babylon-finality-gadget/finalitygadget/config" - "github.com/babylonlabs-io/babylon-finality-gadget/sdk/btcclient" - sdkclient "github.com/babylonlabs-io/babylon-finality-gadget/sdk/client" - sdkcfg "github.com/babylonlabs-io/babylon-finality-gadget/sdk/config" - fgsrv "github.com/babylonlabs-io/babylon-finality-gadget/server" bbncfg "github.com/babylonlabs-io/babylon/client/config" bbntypes "github.com/babylonlabs-io/babylon/types" + fgclient "github.com/babylonlabs-io/finality-gadget/client" + "github.com/babylonlabs-io/finality-gadget/db" + "github.com/babylonlabs-io/finality-gadget/finalitygadget" + fgcfg "github.com/babylonlabs-io/finality-gadget/finalitygadget/config" + "github.com/babylonlabs-io/finality-gadget/sdk/btcclient" + sdkclient "github.com/babylonlabs-io/finality-gadget/sdk/client" + sdkcfg "github.com/babylonlabs-io/finality-gadget/sdk/config" + fgsrv "github.com/babylonlabs-io/finality-gadget/server" api "github.com/babylonlabs-io/finality-provider/clientcontroller/api" bbncc "github.com/babylonlabs-io/finality-provider/clientcontroller/babylon" "github.com/babylonlabs-io/finality-provider/clientcontroller/opstackl2" From c2158a4ec35e2aeb5aaa57a857a98bb1cab8588a Mon Sep 17 00:00:00 2001 From: parketh Date: Sun, 11 Aug 2024 00:57:38 +0100 Subject: [PATCH 09/10] feat: update for finality gadget client --- finality-provider/config/opstackl2.go | 11 +- go.mod | 4 +- go.sum | 8 +- itest/opstackl2/README.md | 2 - itest/opstackl2/op_e2e_test.go | 34 ++++--- itest/opstackl2/op_test_manager.go | 140 +++++++++++--------------- 6 files changed, 89 insertions(+), 110 deletions(-) diff --git a/finality-provider/config/opstackl2.go b/finality-provider/config/opstackl2.go index 17aa510c..eb7505a6 100644 --- a/finality-provider/config/opstackl2.go +++ b/finality-provider/config/opstackl2.go @@ -11,8 +11,9 @@ import ( ) type OPStackL2Config struct { - OPStackL2RPCAddress string `long:"opstackl2-rpc-address" description:"the rpc address of the op-stack-l2 node to connect to"` - OPFinalityGadgetAddress string `long:"op-finality-gadget" description:"the contract address of the op-finality-gadget"` + OPStackL2RPCAddress string `long:"opstackl2-rpc-address" description:"the rpc address of the op-stack-l2 node to connect to"` + OPFinalityGadgetAddress string `long:"op-finality-gadget" description:"the contract address of the op-finality-gadget"` + BabylonFinalityGadgetRpc string `long:"babylon-finality-gadget-rpc" description:"the rpc address of babylon op finality gadget"` // Below configurations are needed for the Babylon client Key string `long:"key" description:"name of the babylon key to sign transactions with"` ChainID string `long:"chain-id" description:"chain id of the babylon chain to connect to"` @@ -41,6 +42,12 @@ func (cfg *OPStackL2Config) Validate() error { if !strings.HasPrefix(cfg.OPFinalityGadgetAddress, cfg.AccountPrefix) { return fmt.Errorf("op-finality-gadget: invalid address prefix: %w", err) } + if cfg.BabylonFinalityGadgetRpc == "" { + return fmt.Errorf("babylon-finality-gadget-rpc is required") + } + if _, err := url.Parse(cfg.BabylonFinalityGadgetRpc); err != nil { + return fmt.Errorf("babylon-finality-gadget-rpc is not correctly formatted: %w", err) + } if _, err := url.Parse(cfg.RPCAddr); err != nil { return fmt.Errorf("rpc-addr is not correctly formatted: %w", err) } diff --git a/go.mod b/go.mod index ccabf914..61e12cb7 100644 --- a/go.mod +++ b/go.mod @@ -440,9 +440,9 @@ replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 github.com/babylonlabs-io/babylon => github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 - github.com/babylonlabs-io/finality-gadget => github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a + github.com/babylonlabs-io/finality-gadget => github.com/babylonlabs-io/finality-gadget v0.0.0-20240810191700-9ddea258bce5 github.com/cockroachdb/pebble => github.com/cockroachdb/pebble v0.0.0-20231018212520-f6cde3fc2fa4 - github.com/ethereum-optimism/optimism => github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28 + github.com/ethereum-optimism/optimism => github.com/babylonlabs-io/optimism v1.8.1-0.20240808190817-3279057d5250 github.com/ethereum/go-ethereum => github.com/ethereum-optimism/op-geth v1.101315.1-rc.5 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 diff --git a/go.sum b/go.sum index fde525aa..222d8673 100644 --- a/go.sum +++ b/go.sum @@ -302,10 +302,10 @@ github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 h1:W8jr github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M= github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js= -github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a h1:wMQ0sCWzwjXMXkww3kr5N0MgjizmulVGKvNcvyTEGhg= -github.com/babylonlabs-io/finality-gadget v0.0.0-20240805210950-4ff43c720b6a/go.mod h1:+BICJA7mUJpfYOSYds1G5w6nLO0gKVe9XwdmpIUiXmk= -github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28 h1:1xw+4A+IAvHff0f02UsXR1h0bg04Xn726ZvEAwrc/7Y= -github.com/babylonlabs-io/optimism v1.8.1-0.20240804005223-75f4cdcfed28/go.mod h1:cVDnobSCpjb8XIfy8/8tlnm1gkO9lV2xfCezG/7YacY= +github.com/babylonlabs-io/finality-gadget v0.0.0-20240810191700-9ddea258bce5 h1:PgJJS7ae2fFJKRRmb5epXabwuVl0SYIDIRz4n+NUNG0= +github.com/babylonlabs-io/finality-gadget v0.0.0-20240810191700-9ddea258bce5/go.mod h1:+BICJA7mUJpfYOSYds1G5w6nLO0gKVe9XwdmpIUiXmk= +github.com/babylonlabs-io/optimism v1.8.1-0.20240808190817-3279057d5250 h1:3FNsa55pbil/Ff7NICtaZzcdLwRs9HsCXJNmW7hDzPs= +github.com/babylonlabs-io/optimism v1.8.1-0.20240808190817-3279057d5250/go.mod h1:ZZVrpyP7weyHNHnN2Wr6A4lnMpqcJHyL5Ke7LQ0zzHs= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= diff --git a/itest/opstackl2/README.md b/itest/opstackl2/README.md index 8001c5e2..c90deccd 100644 --- a/itest/opstackl2/README.md +++ b/itest/opstackl2/README.md @@ -6,8 +6,6 @@ To run the e2e tests, first you need to set up the devnet data: $ make op-e2e-devnet ``` -Next, replace `babylonFinalityGadgetBitcoinRpc` in `itest/opstackl2/devnet-data/devnetL1.json` with your own Bitcoin mainnet RPC URL to avoid potential rate limit issues. - Then run the following command to start the e2e tests: ```bash diff --git a/itest/opstackl2/op_e2e_test.go b/itest/opstackl2/op_e2e_test.go index b75c0fe5..ec39acca 100644 --- a/itest/opstackl2/op_e2e_test.go +++ b/itest/opstackl2/op_e2e_test.go @@ -9,8 +9,7 @@ import ( "testing" "time" - sdkclient "github.com/babylonlabs-io/finality-gadget/sdk/client" - "github.com/babylonlabs-io/finality-gadget/sdk/cwclient" + fgtypes "github.com/babylonlabs-io/finality-gadget/types" e2eutils "github.com/babylonlabs-io/finality-provider/itest" "github.com/babylonlabs-io/finality-provider/testutil/log" "github.com/stretchr/testify/require" @@ -18,7 +17,7 @@ import ( // tests the finality signature submission to the op-finality-gadget contract func TestOpSubmitFinalitySignature(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 1, false) + ctm := StartOpL2ConsumerManager(t, 1) defer ctm.Stop(t) consumerFpPkList := ctm.RegisterConsumerFinalityProvider(t, 1) @@ -38,7 +37,7 @@ func TestOpSubmitFinalitySignature(t *testing.T) { // wait for the fp sign ctm.WaitForFpVoteReachHeight(t, fpInstance, testBlock.Height) - queryParams := cwclient.L2Block{ + queryBlock := &fgtypes.Block{ BlockHeight: testBlock.Height, BlockHash: hex.EncodeToString(testBlock.Hash), BlockTimestamp: 12345, // doesn't matter b/c the BTC client is mocked @@ -46,8 +45,8 @@ func TestOpSubmitFinalitySignature(t *testing.T) { // note: QueryFinalityProviderHasPower is hardcode to return true so FPs can still submit finality sigs even if they // don't have voting power. But the finality sigs will not be counted at tally time. - _, err = ctm.SdkClient.QueryIsBlockBabylonFinalized(queryParams) - require.ErrorIs(t, err, sdkclient.ErrBtcStakingNotActivated) + _, err = ctm.FinalityGadget.QueryIsBlockBabylonFinalized(queryBlock) + require.ErrorIs(t, err, fgtypes.ErrBtcStakingNotActivated) t.Logf(log.Prefix("Expected no voting power")) } @@ -55,7 +54,7 @@ func TestOpSubmitFinalitySignature(t *testing.T) { // 1. block has both two FP signs, so it would be finalized // 2. block has only one FP with smaller power (1/4) signs, so it would not be considered as finalized func TestOpMultipleFinalityProviders(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 2, false) + ctm := StartOpL2ConsumerManager(t, 2) defer ctm.Stop(t) // register, get BTC delegations, and start FPs @@ -89,12 +88,12 @@ func TestOpMultipleFinalityProviders(t *testing.T) { testBlock, err := ctm.getOpCCAtIndex(1).QueryBlock(targetBlockHeight) require.NoError(t, err) - queryParams := cwclient.L2Block{ + queryBlock := &fgtypes.Block{ BlockHeight: testBlock.Height, BlockHash: hex.EncodeToString(testBlock.Hash), BlockTimestamp: 12345, // doesn't matter b/c the BTC client is mocked } - finalized, err := ctm.SdkClient.QueryIsBlockBabylonFinalized(queryParams) + finalized, err := ctm.FinalityGadget.QueryIsBlockBabylonFinalized(queryBlock) require.NoError(t, err) require.Equal(t, true, finalized) t.Logf(log.Prefix("Test case 1: block %d is finalized"), testBlock.Height) @@ -113,20 +112,20 @@ func TestOpMultipleFinalityProviders(t *testing.T) { testNextBlock, err := ctm.getOpCCAtIndex(1).QueryBlock(testNextBlockHeight) require.NoError(t, err) - queryNextParams := cwclient.L2Block{ + queryNextBlock := &fgtypes.Block{ BlockHeight: testNextBlock.Height, BlockHash: hex.EncodeToString(testNextBlock.Hash), BlockTimestamp: 12345, // doesn't matter b/c the BTC client is mocked } // testNextBlock only have 1/4 total voting power - nextFinalized, err := ctm.SdkClient.QueryIsBlockBabylonFinalized(queryNextParams) + nextFinalized, err := ctm.FinalityGadget.QueryIsBlockBabylonFinalized(queryNextBlock) require.NoError(t, err) require.Equal(t, false, nextFinalized) t.Logf(log.Prefix("Test case 2: block %d is not finalized"), testNextBlock.Height) } func TestFinalityStuckAndRecover(t *testing.T) { - ctm := StartOpL2ConsumerManager(t, 1, false) + ctm := StartOpL2ConsumerManager(t, 1) defer ctm.Stop(t) // register, get BTC delegations, and start FPs @@ -187,9 +186,9 @@ func TestFinalityStuckAndRecover(t *testing.T) { ), nextFinalizedHeight) } -func TestFinalityGadget(t *testing.T) { +func TestFinalityGadgetServer(t *testing.T) { // start the consumer manager - ctm := StartOpL2ConsumerManager(t, 2, true) + ctm := StartOpL2ConsumerManager(t, 2) defer ctm.Stop(t) // register, get BTC delegations, and start FPs @@ -220,7 +219,7 @@ func TestFinalityGadget(t *testing.T) { ctm.WaitForFpVoteReachHeight(t, fpList[1], targetBlockHeight+1) t.Logf(log.Prefix("Both FP instances signed the second block")) - // run the finality gadget + // start finality gadget processing blocks ctx, cancel := context.WithCancel(context.Background()) go func() { t.Logf(log.Prefix("Starting finality gadget")) @@ -230,7 +229,10 @@ func TestFinalityGadget(t *testing.T) { // check latest block require.Eventually(t, func() bool { - block, err := ctm.FinalityGadgetClient.GetLatestBlock() + block, err := ctm.FinalityGadget.QueryLatestFinalizedBlock() + if block == nil { + return false + } require.NoError(t, err) return block.BlockHeight > targetBlockHeight+6 }, 40*time.Second, 5*time.Second, "Failed to process blocks") diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index 17a74bd5..8a63338c 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -18,12 +18,9 @@ import ( bbncfg "github.com/babylonlabs-io/babylon/client/config" bbntypes "github.com/babylonlabs-io/babylon/types" fgclient "github.com/babylonlabs-io/finality-gadget/client" + fgcfg "github.com/babylonlabs-io/finality-gadget/config" "github.com/babylonlabs-io/finality-gadget/db" "github.com/babylonlabs-io/finality-gadget/finalitygadget" - fgcfg "github.com/babylonlabs-io/finality-gadget/finalitygadget/config" - "github.com/babylonlabs-io/finality-gadget/sdk/btcclient" - sdkclient "github.com/babylonlabs-io/finality-gadget/sdk/client" - sdkcfg "github.com/babylonlabs-io/finality-gadget/sdk/config" fgsrv "github.com/babylonlabs-io/finality-gadget/server" api "github.com/babylonlabs-io/finality-provider/clientcontroller/api" bbncc "github.com/babylonlabs-io/finality-provider/clientcontroller/babylon" @@ -64,21 +61,21 @@ type OpL2ConsumerTestManager struct { ConsumerFpApps []*service.FinalityProviderApp EOTSServerHandler *e2eutils.EOTSServerHandler BaseDir string - SdkClient *sdkclient.SdkClient FinalityGadget *finalitygadget.FinalityGadget FinalityGadgetClient *fgclient.FinalityGadgetGrpcClient - Db *db.BBoltHandler + Db db.IDatabaseHandler + DbPath string OpSystem *ope2e.System } -func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8, runFinalityGadget bool) *OpL2ConsumerTestManager { +func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2ConsumerTestManager { defaultlog.SetOutput(os.Stdout) // Setup base dir and logger testDir, err := e2eutils.BaseDir("fpe2etest") require.NoError(t, err) - logger := createLogger(t, zapcore.ErrorLevel) + logger := createLogger(t, zapcore.DebugLevel) // generate covenant committee covenantQuorum := 2 @@ -90,15 +87,16 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8, runFinalityG err = bh.Start() require.NoError(t, err) + // specify Babylon finality gadget rpc + babylonFinalityGadgetRpcPort := "8080" + babylonFinalityGadgetRpc := "localhost:" + babylonFinalityGadgetRpcPort + // deploy op-finality-gadget contract and start op stack system - opL2ConsumerConfig, opSys := startExtSystemsAndCreateConsumerCfg(t, logger, bh) + opL2ConsumerConfig, opSys := startExtSystemsAndCreateConsumerCfg(t, logger, bh, babylonFinalityGadgetRpc) // TODO: this is a hack to try to fix a flaky data race // https://github.com/babylonchain/finality-provider/issues/528 time.Sleep(5 * time.Second) - // init SDK client - sdkClient := initSdkClient(opSys, opL2ConsumerConfig, t) - // create shutdown interceptor shutdownInterceptor, err := signal.Intercept() require.NoError(t, err) @@ -126,53 +124,42 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8, runFinalityG require.NoError(t, err) t.Logf(log.Prefix("Register consumer %s to Babylon"), opConsumerId) - // Define finality gadget configs + // define finality gadget configs cfg := fgcfg.Config{ L2RPCHost: opL2ConsumerConfig.OPStackL2RPCAddress, - BitcoinRPCHost: trimLeadingHttp(opSys.Cfg.DeployConfig.BabylonFinalityGadgetBitcoinRpc), - FGContractAddress: opSys.Cfg.DeployConfig.BabylonFinalityGadgetContractAddress, - BBNChainID: opSys.Cfg.DeployConfig.BabylonFinalityGadgetChainID, + BitcoinRPCHost: "rpc.ankr.com/btc", + FGContractAddress: opL2ConsumerConfig.OPFinalityGadgetAddress, + BBNChainID: "chain-test", BBNRPCAddress: opL2ConsumerConfig.RPCAddr, DBFilePath: "data.db", - GRPCServerPort: "8080", + GRPCServerPort: babylonFinalityGadgetRpcPort, PollInterval: time.Second * time.Duration(10), } + // Init local DB for storing and querying blocks + db, err := db.NewBBoltHandler(cfg.DBFilePath, logger) + require.NoError(t, err) + err = db.CreateInitialSchema() + require.NoError(t, err) + t.Logf(log.Prefix("Init local DB for finality gadget server")) + // Start finality gadget - var database *db.BBoltHandler - var finalityGadget *finalitygadget.FinalityGadget - var client *fgclient.FinalityGadgetGrpcClient - if runFinalityGadget { - // Init local DB for storing and querying blocks - db, err := db.NewBBoltHandler(cfg.DBFilePath) - require.NoError(t, err) - database = db - err = db.TryCreateInitialBuckets() - require.NoError(t, err) - t.Logf(log.Prefix("Init local DB for finality gadget server")) + fg, err := finalitygadget.NewFinalityGadget(&cfg, db, logger) + require.NoError(t, err) + t.Logf(log.Prefix("Created finality gadget")) - // Create finality gadget - fg, err := finalitygadget.NewFinalityGadget(&cfg, db) + // Start finality gadget server + srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor, logger) + go func() { + err = srv.RunUntilShutdown() require.NoError(t, err) - finalityGadget = fg - t.Logf(log.Prefix("Created finality gadget")) - - // Start grpc server - // Hook interceptor for os signals. - srv := fgsrv.NewFinalityGadgetServer(&cfg, db, fg, shutdownInterceptor) - go func() { - err = srv.RunUntilShutdown() - require.NoError(t, err) - }() - t.Logf(log.Prefix("Started finality gadget grpc server")) + }() + t.Logf(log.Prefix("Started finality gadget grpc server")) - // Create grpc client - hostAddr := "localhost:" + cfg.GRPCServerPort - fgClient, err := fgclient.NewFinalityGadgetGrpcClient(db, hostAddr) - require.NoError(t, err) - client = fgClient - t.Logf(log.Prefix("Started finality gadget grpc client")) - } + // Create grpc client + client, err := fgclient.NewFinalityGadgetGrpcClient(babylonFinalityGadgetRpc) + require.NoError(t, err) + t.Logf(log.Prefix("Started finality gadget grpc client")) ctm := &OpL2ConsumerTestManager{ BaseTestManager: BaseTestManager{ @@ -184,10 +171,10 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8, runFinalityG BabylonFpApp: babylonFpApp, ConsumerFpApps: consumerFpApps, BaseDir: testDir, - SdkClient: sdkClient, - FinalityGadget: finalityGadget, + FinalityGadget: fg, FinalityGadgetClient: client, - Db: database, + Db: db, + DbPath: cfg.DBFilePath, OpSystem: opSys, } @@ -464,24 +451,6 @@ func startEotsManagers( return eh, eotsClients } -func initSdkClient( - opSys *ope2e.System, - opL2ConsumerConfig *fpcfg.OPStackL2Config, - t *testing.T, -) *sdkclient.SdkClient { - // We pass in an external Bitcoin RPC address but otherwise use the default configs. - btcConfig := btcclient.DefaultBTCConfig() - // The RPC url must be trimmed to remove the http:// or https:// prefix. - btcConfig.RPCHost = trimLeadingHttp(opSys.Cfg.DeployConfig.BabylonFinalityGadgetBitcoinRpc) - sdkClient, err := sdkclient.NewClient(&sdkcfg.Config{ - ChainID: opSys.Cfg.DeployConfig.BabylonFinalityGadgetChainID, - ContractAddr: opL2ConsumerConfig.OPFinalityGadgetAddress, - BTCConfig: btcConfig, - }) - require.NoError(t, err) - return sdkClient -} - func deployCwContract( t *testing.T, logger *zap.Logger, @@ -732,6 +701,7 @@ func startExtSystemsAndCreateConsumerCfg( t *testing.T, logger *zap.Logger, bh *e2eutils.BabylonNodeHandler, + babylonFinalityGadgetRpc string, ) (*fpcfg.OPStackL2Config, *ope2e.System) { // create consumer config // TODO: using babylon node key dir is a hack. we should fix it @@ -739,24 +709,20 @@ func startExtSystemsAndCreateConsumerCfg( // DefaultSystemConfig load the op deploy config from devnet-data folder opSysCfg := ope2e.DefaultSystemConfig(t) - require.Equal( - t, - e2eutils.ChainID, - opSysCfg.DeployConfig.BabylonFinalityGadgetChainID, - "should be chain-test in devnetL1.json that means to connect with the Babylon localnet", - ) opConsumerId := getConsumerChainId(&opSysCfg) // deploy op-finality-gadget contract cwContractAddress := deployCwContract(t, logger, opL2ConsumerConfig, opConsumerId) - // replace the contract address - opSysCfg.DeployConfig.BabylonFinalityGadgetContractAddress = cwContractAddress // supress OP system logs opSysCfg.Loggers["verifier"] = optestlog.Logger(t, gethlog.LevelError).New("role", "verifier") opSysCfg.Loggers["sequencer"] = optestlog.Logger(t, gethlog.LevelError).New("role", "sequencer") opSysCfg.Loggers["batcher"] = optestlog.Logger(t, gethlog.LevelError).New("role", "watcher") + // specify babylon finality gadget rpc address + opL2ConsumerConfig.BabylonFinalityGadgetRpc = babylonFinalityGadgetRpc + opSysCfg.DeployConfig.BabylonFinalityGadgetRpc = babylonFinalityGadgetRpc + // start op stack system opSys, err := opSysCfg.Start(t) require.NoError(t, err, "Error starting up op stack system") @@ -871,6 +837,10 @@ func (ctm *OpL2ConsumerTestManager) WaitForBlockFinalized( require.Eventually(t, func() bool { // doesn't matter which FP we use to query. so we use the first consumer FP latestFinalizedBlock, err := ctm.getOpCCAtIndex(0).QueryLatestFinalizedBlock() + if err != nil { + t.Logf(log.Prefix("failed to query latest finalized block %s"), err.Error()) + return false + } require.NoError(t, err) finalizedBlockHeight = latestFinalizedBlock.Height return finalizedBlockHeight >= checkedHeight @@ -984,7 +954,7 @@ func (ctm *OpL2ConsumerTestManager) waitForBTCStakingActivation(t *testing.T) ui require.NoError(t, err) l2BlockAfterActivation = latestBlock.Number.Uint64() - activatedTimestamp, err := ctm.SdkClient.QueryBtcStakingActivatedTimestamp() + activatedTimestamp, err := ctm.FinalityGadget.QueryBtcStakingActivatedTimestamp() if err != nil { t.Logf(log.Prefix("Failed to query BTC staking activated timestamp: %v"), err) return false @@ -999,7 +969,7 @@ func (ctm *OpL2ConsumerTestManager) waitForBTCStakingActivation(t *testing.T) ui } func (ctm *OpL2ConsumerTestManager) checkLatestBlock(t *testing.T, exp uint64) { - block, err := ctm.FinalityGadgetClient.GetLatestBlock() + block, err := ctm.FinalityGadget.QueryLatestFinalizedBlock() require.NoError(t, err) require.Equal(t, exp, block.BlockHeight) } @@ -1019,15 +989,17 @@ func (ctm *OpL2ConsumerTestManager) Stop(t *testing.T) { t.Logf(log.Prefix("Stopped Consumer FP App %d"), i) } - if ctm.FinalityGadget != nil { - err = ctm.FinalityGadget.DeleteDB() - require.NoError(t, err) - ctm.FinalityGadget.Close() - err = ctm.FinalityGadgetClient.Close() + err = ctm.FinalityGadgetClient.Close() + require.NoError(t, err) + if ctm.DbPath != "" { + err = os.Remove(ctm.DbPath) require.NoError(t, err) + } + if ctm.Db != nil { err = ctm.Db.Close() require.NoError(t, err) } + ctm.FinalityGadget.Close() ctm.OpSystem.Close() err = ctm.BabylonHandler.Stop() require.NoError(t, err) From 942edfa474db9241bd2f2927bcf76f65fffe82c0 Mon Sep 17 00:00:00 2001 From: parketh Date: Sun, 11 Aug 2024 08:26:34 +0100 Subject: [PATCH 10/10] reduce log level --- itest/opstackl2/op_test_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itest/opstackl2/op_test_manager.go b/itest/opstackl2/op_test_manager.go index 8a63338c..44b565f9 100644 --- a/itest/opstackl2/op_test_manager.go +++ b/itest/opstackl2/op_test_manager.go @@ -75,7 +75,7 @@ func StartOpL2ConsumerManager(t *testing.T, numOfConsumerFPs uint8) *OpL2Consume testDir, err := e2eutils.BaseDir("fpe2etest") require.NoError(t, err) - logger := createLogger(t, zapcore.DebugLevel) + logger := createLogger(t, zapcore.ErrorLevel) // generate covenant committee covenantQuorum := 2