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

tests: tallying and block finalization #446

Merged
merged 25 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PACKAGES_E2E=$(shell go list ./... | grep '/itest')
# due to multiple packages found
# context: https://github.com/golang/go/issues/24929
PACKAGES_E2E_OP=$(shell go list -tags=e2e_op ./... | grep '/itest/opstackl2')
PACKAGES_E2E_BCD=$(shell go list -tags=e2e_bcd ./... | grep '/itest/cosmwasm/bcd')

ifeq ($(LINK_STATICALLY),true)
ldflags += -linkmode=external -extldflags "-Wl,-z,muldefs -static" -v
Expand Down Expand Up @@ -97,16 +98,16 @@ clean-e2e:
test-e2e: test-e2e-babylon test-e2e-wasmd test-e2e-bcd test-e2e-op

test-e2e-babylon: clean-e2e install-babylond
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_babylon
@go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_babylon

test-e2e-bcd: clean-e2e install-babylond install-bcd
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_bcd
@go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E_BCD) -count=1 --tags=e2e_bcd

test-e2e-wasmd: clean-e2e install-babylond install-wasmd
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_wasmd
@go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e_wasmd

test-e2e-op: clean-e2e install-babylond
go test -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

DEVNET_REPO_URL := https://github.com/babylonchain/op-e2e-devnet
TARGET_DIR := ./itest/opstackl2/devnet-data
Expand Down
7 changes: 3 additions & 4 deletions clientcontroller/cosmwasm/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ func (wc *CosmwasmConsumerController) queryLatestBlocks(startAfter, limit *uint6
return blocks, nil
}

//nolint:unused
func (wc *CosmwasmConsumerController) queryIndexedBlock(height uint64) (*IndexedBlock, error) {
func (wc *CosmwasmConsumerController) QueryIndexedBlock(height uint64) (*IndexedBlock, error) {
// Construct the query message
queryMsgStruct := QueryMsgBlock{
Block: BlockQuery{
Expand Down Expand Up @@ -364,7 +363,7 @@ func (wc *CosmwasmConsumerController) queryIndexedBlock(height uint64) (*Indexed

func (wc *CosmwasmConsumerController) QueryBlock(height uint64) (*fptypes.BlockInfo, error) {
//// Use the helper function to get the IndexedBlock
//resp, err := wc.queryIndexedBlock(height)
//resp, err := wc.QueryIndexedBlock(height)
//if err != nil {
// return nil, err
//}
Expand Down Expand Up @@ -447,7 +446,7 @@ func (wc *CosmwasmConsumerController) QueryLastPublicRandCommit(fpPk *btcec.Publ

func (wc *CosmwasmConsumerController) QueryIsBlockFinalized(height uint64) (bool, error) {
//// Use the helper function to get the IndexedBlock
//resp, err := wc.queryIndexedBlock(height)
//resp, err := wc.QueryIndexedBlock(height)
//if err != nil {
// return false, err
//}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/CosmWasm/wasmd v0.51.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonchain/babylon v0.8.6
github.com/babylonchain/babylon-sdk/demo v0.0.0-20240624102351-3a809d4fdcce
github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.5
Expand Down Expand Up @@ -196,7 +196,7 @@ require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.44.312 // indirect
github.com/babylonchain/babylon-da-sdk v0.1.3-alpha.0.20240702185408-0cda6f3ff461
github.com/babylonchain/babylon-sdk/x v0.0.0-20240624102351-3a809d4fdcce // indirect
github.com/babylonchain/babylon-sdk/x v0.0.0-20240705194516-4e2c5650cde8 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ github.com/babylonchain/babylon-da-sdk v0.0.0-20240705141841-e18d8f028ec9 h1:/Df
github.com/babylonchain/babylon-da-sdk v0.0.0-20240705141841-e18d8f028ec9/go.mod h1:0kTt9a/njIMLOtiBlr7WMRYM8gTFuTEOnhK05oL/N5g=
github.com/babylonchain/babylon-private v0.8.6-0.20240705135310-e91ff7f60ead h1:LyyrFtdSbx0a5ZLHA/qMNMjAxZltfnNJgj5+uEgqZdI=
github.com/babylonchain/babylon-private v0.8.6-0.20240705135310-e91ff7f60ead/go.mod h1:Tdi+29Y+DzCaaz0V0sBRXF1tXXLnH6JHJsOG8uktWLU=
github.com/babylonchain/babylon-sdk/demo v0.0.0-20240624102351-3a809d4fdcce h1:73v+DHCPD4Wi42O2NPJfor+adBiNRkAVddf27idbHrQ=
github.com/babylonchain/babylon-sdk/demo v0.0.0-20240624102351-3a809d4fdcce/go.mod h1:X4QovCWMwqMjoTriu18w4gfqX1sYqOWRnZwXGYD8bnE=
github.com/babylonchain/babylon-sdk/x v0.0.0-20240624102351-3a809d4fdcce h1:UYg5QEbMUBt3IIVdcTDXqVTsj1iJoFQBfGC+e6uEtZg=
github.com/babylonchain/babylon-sdk/x v0.0.0-20240624102351-3a809d4fdcce/go.mod h1:Ojrlnwh9z7fvmTUzYH4Tk+8hWSctCrZYWiC5+t4X32I=
github.com/babylonchain/babylon-sdk/demo v0.0.0-20240705194516-4e2c5650cde8 h1:VzLjjsynyYaOsIiLwVwMQDXbh1nsQWVpLYLi09R0mh0=
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/babylonchain/optimism v0.0.0-20240705144333-0e1916f9e050 h1:5pn1kf5U19i5QRFU44Skmvp1tjM/W4KvTjPzTMi7DRE=
github.com/babylonchain/optimism v0.0.0-20240705144333-0e1916f9e050/go.mod h1:1e/m2EHvBIZoklRNAtU5lQQckgjG+aWRmFeeD3Qbxak=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
5 changes: 4 additions & 1 deletion itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ func NewBabylonNodeHandler(t *testing.T, covenantQuorum int, covenantPks []*type
"babylond",
"start",
fmt.Sprintf("--home=%s", nodeDataDir),
"--log_level=debug",
"--log_level=trace",
"--trace",
)

fmt.Println("Starting babylond with command: ", startCmd.String())

startCmd.Stdout = f

return &BabylonNodeHandler{
Expand Down
Binary file modified itest/bytecode/babylon_contract.wasm
Binary file not shown.
Binary file modified itest/bytecode/btc_staking.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion itest/bytecode/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.7.0-rc.2
a33989c5c7c11b882bc7d5f50c1dfe30bae47fa6
101 changes: 65 additions & 36 deletions itest/cosmwasm/bcd/bcd_consumer_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
// 1. Upload Babylon and BTC staking contracts to bcd chain
// 2. Instantiate Babylon contract with admin
// 3. Register consumer chain to Babylon
// 4. Register finality provider to Babylon
// 5. Inject finality provider and delegation in BTC staking contract using admin
// 4. Inject consumer fp in BTC staking contract using admin
// 6. Start the finality provider daemon and app
// 7. Wait for fp daemon to submit public randomness and finality signature
// 8. Inject consumer delegation in BTC staking contract using admin, this will give voting power to fp
// 9. Ensure fp has voting power in smart contract
// 10. Ensure finality sigs are being submitted by fp daemon and block is finalized
// NOTE: the delegation is injected after ensuring pub randomness loop in fp daemon has started
// this order is necessary otherwise pub randomness loop takes time to start and due to this blocks won't get finalized.
func TestConsumerFpLifecycle(t *testing.T) {
ctm := StartBcdTestManager(t)
defer ctm.Stop(t)
Expand Down Expand Up @@ -80,6 +84,7 @@ func TestConsumerFpLifecycle(t *testing.T) {
require.NoError(t, err)

// register consumer fps to babylon
// this will be submitted to babylon once fp daemon starts
app := ctm.Fpa
cfg := app.GetConfig()
fpName := e2eutils.FpNamePrefix + bcdChainID
Expand All @@ -95,42 +100,22 @@ func TestConsumerFpLifecycle(t *testing.T) {
_, err = app.RegisterFinalityProvider(fpPk.MarshalHex())
require.NoError(t, err)

// inject fp and delegation in smart contract using admin
msg := e2eutils.GenBtcStakingExecMsg(fpPk.MarshalHex())
msgBytes, err := json.Marshal(msg)
// inject fp in smart contract using admin
fpMsg := e2eutils.GenBtcStakingFpExecMsg(fpPk.MarshalHex())
fpMsgBytes, err := json.Marshal(fpMsg)
require.NoError(t, err)
_, err = ctm.BcdConsumerClient.ExecuteContract(msgBytes)
_, err = ctm.BcdConsumerClient.ExecuteContract(fpMsgBytes)
require.NoError(t, err)

// query finality providers in smart contract
consumerFpsResp, err := ctm.BcdConsumerClient.QueryFinalityProviders()
require.NoError(t, err)
require.NotNil(t, consumerFpsResp)
require.Len(t, consumerFpsResp.Fps, 1)
require.Equal(t, msg.BtcStaking.NewFP[0].ConsumerID, consumerFpsResp.Fps[0].ConsumerId)
require.Equal(t, msg.BtcStaking.NewFP[0].BTCPKHex, consumerFpsResp.Fps[0].BtcPkHex)

// query delegations in smart contract
consumerDelsResp, err := ctm.BcdConsumerClient.QueryDelegations()
require.NoError(t, err)
require.NotNil(t, consumerDelsResp)
require.Len(t, consumerDelsResp.Delegations, 1)
require.Empty(t, consumerDelsResp.Delegations[0].UndelegationInfo.DelegatorUnbondingSig) // assert there is no delegator unbonding sig
require.Equal(t, msg.BtcStaking.ActiveDel[0].BTCPkHex, consumerDelsResp.Delegations[0].BtcPkHex)
require.Equal(t, msg.BtcStaking.ActiveDel[0].StartHeight, consumerDelsResp.Delegations[0].StartHeight)
require.Equal(t, msg.BtcStaking.ActiveDel[0].EndHeight, consumerDelsResp.Delegations[0].EndHeight)
require.Equal(t, msg.BtcStaking.ActiveDel[0].TotalSat, consumerDelsResp.Delegations[0].TotalSat)
require.Equal(t, msg.BtcStaking.ActiveDel[0].StakingTx, consumerDelsResp.Delegations[0].StakingTx)
require.Equal(t, msg.BtcStaking.ActiveDel[0].SlashingTx, consumerDelsResp.Delegations[0].SlashingTx)

// ensure fp has voting power in smart contract
consumerFpsByPowerResp, err := ctm.BcdConsumerClient.QueryFinalityProvidersByPower()
require.NoError(t, err)
require.NotNil(t, consumerFpsByPowerResp)
require.Len(t, consumerFpsByPowerResp.Fps, 1)
require.Equal(t, msg.BtcStaking.NewFP[0].BTCPKHex, consumerFpsByPowerResp.Fps[0].BtcPkHex)
require.Equal(t, consumerDelsResp.Delegations[0].TotalSat, consumerFpsByPowerResp.Fps[0].Power)
require.Equal(t, fpMsg.BtcStaking.NewFP[0].ConsumerID, consumerFpsResp.Fps[0].ConsumerId)
require.Equal(t, fpMsg.BtcStaking.NewFP[0].BTCPKHex, consumerFpsResp.Fps[0].BtcPkHex)

// start finality provider daemon
err = app.StartHandlingFinalityProvider(fpPk, e2eutils.Passphrase)
require.NoError(t, err)
fpIns, err := app.GetFinalityProviderInstance(fpPk)
Expand All @@ -139,6 +124,7 @@ func TestConsumerFpLifecycle(t *testing.T) {
require.NoError(t, err)

// ensure consumer finality providers are stored in Babylon
// this will happen after the finality provider daemon has started
require.Eventually(t, func() bool {
fps, err := ctm.BBNClient.QueryConsumerFinalityProviders(bcdChainID)
if err != nil {
Expand All @@ -160,11 +146,6 @@ func TestConsumerFpLifecycle(t *testing.T) {
return true
}, e2eutils.EventuallyWaitTimeOut, e2eutils.EventuallyPollTime)

wasmdNodeStatus, err := ctm.BcdConsumerClient.GetCometNodeStatus()
require.NoError(t, err)
cometLatestHeight := wasmdNodeStatus.SyncInfo.LatestBlockHeight
lookupHeight := cometLatestHeight + 20 // TODO: this is a hack, as its possible the randomness/sigs submission loops haven't started yet

// ensure pub rand is submitted to smart contract
require.Eventually(t, func() bool {
fpPubRandResp, err := ctm.BcdConsumerClient.QueryLastPublicRandCommit(fpPk.MustToBTCPK())
Expand All @@ -179,17 +160,65 @@ func TestConsumerFpLifecycle(t *testing.T) {
return true
}, e2eutils.EventuallyWaitTimeOut, e2eutils.EventuallyPollTime)

// inject delegation in smart contract using admin
delMsg := e2eutils.GenBtcStakingDelExecMsg(fpPk.MarshalHex())
delMsgBytes, err := json.Marshal(delMsg)
require.NoError(t, err)
_, err = ctm.BcdConsumerClient.ExecuteContract(delMsgBytes)
require.NoError(t, err)

// query delegations in smart contract
consumerDelsResp, err := ctm.BcdConsumerClient.QueryDelegations()
require.NoError(t, err)
require.NotNil(t, consumerDelsResp)
require.Len(t, consumerDelsResp.Delegations, 1)
require.Empty(t, consumerDelsResp.Delegations[0].UndelegationInfo.DelegatorUnbondingSig) // assert there is no delegator unbonding sig
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].BTCPkHex, consumerDelsResp.Delegations[0].BtcPkHex)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].StartHeight, consumerDelsResp.Delegations[0].StartHeight)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].EndHeight, consumerDelsResp.Delegations[0].EndHeight)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].TotalSat, consumerDelsResp.Delegations[0].TotalSat)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].StakingTx, consumerDelsResp.Delegations[0].StakingTx)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].SlashingTx, consumerDelsResp.Delegations[0].SlashingTx)

// ensure fp has voting power in smart contract
consumerFpsByPowerResp, err := ctm.BcdConsumerClient.QueryFinalityProvidersByPower()
require.NoError(t, err)
require.NotNil(t, consumerFpsByPowerResp)
require.Len(t, consumerFpsByPowerResp.Fps, 1)
require.Equal(t, fpMsg.BtcStaking.NewFP[0].BTCPKHex, consumerFpsByPowerResp.Fps[0].BtcPkHex)
require.Equal(t, delMsg.BtcStaking.ActiveDel[0].TotalSat, consumerFpsByPowerResp.Fps[0].Power)

// get comet latest height
wasmdNodeStatus, err := ctm.BcdConsumerClient.GetCometNodeStatus()
require.NoError(t, err)
// TODO: this is a hack as its possible that latest comet height is less than activated height
// and the sigs/finalization can only happen after activated height
lookupHeight := wasmdNodeStatus.SyncInfo.LatestBlockHeight + 5

// ensure finality signature is submitted to smart contract
require.Eventually(t, func() bool {
fpSigsResponse, err := ctm.BcdConsumerClient.QueryFinalitySignature(fpPk.MarshalHex(), uint64(lookupHeight))
if err != nil {
t.Logf("failed to query finality signature: %s", err.Error())
return false
}
if fpSigsResponse == nil {
if fpSigsResponse == nil || fpSigsResponse.Signature == nil || len(fpSigsResponse.Signature) == 0 {
return false
}
return true
}, e2eutils.EventuallyWaitTimeOut, e2eutils.EventuallyPollTime)

// ensure latest comet block is finalized
require.Eventually(t, func() bool {
idxBlockedResponse, err := ctm.BcdConsumerClient.QueryIndexedBlock(uint64(lookupHeight))
if err != nil {
t.Logf("failed to query indexed block: %s", err.Error())
return false
}
if idxBlockedResponse == nil {
return false
}
if fpSigsResponse.Signature == nil || len(fpSigsResponse.Signature) == 0 {
if !idxBlockedResponse.Finalized {
return false
}
return true
Expand Down
Loading