Skip to content

Commit

Permalink
feat: add e2e-init-chain (#46)
Browse files Browse the repository at this point in the history
Add e2e-init-chain, it is necessary for e2e testing in new releases to
have the init chain with the same proto versions used at the v09 bin
  • Loading branch information
RafilxTenfen authored Sep 3, 2024
1 parent 0aa61b2 commit 009cd29
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 50 deletions.
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ endif

.PHONY: run-tests test test-all $(TEST_TARGETS)

test-e2e: build-docker
test-e2e: build-docker-e2e
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e

test-sim-nondeterminism:
Expand Down Expand Up @@ -408,17 +408,32 @@ proto-lint: ## Lint protobuf files

.PHONY: proto-gen proto-swagger-gen proto-format prot-lint

###############################################################################
### E2E build ###
###############################################################################

# Executed to build the binary for chain initialization, one of
## chain => test/e2e/initialization/chain/main.go
## node => test/e2e/initialization/node/main.go
e2e-build-script:
mkdir -p $(BUILDDIR)
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ ./test/e2e/initialization/$(E2E_SCRIPT_NAME)

###############################################################################
### Docker ###
###############################################################################

build-docker: ## Build babylond Docker image
$(MAKE) -C contrib/images babylond

build-docker-e2e:
$(MAKE) -C contrib/images babylond-e2e
$(MAKE) -C contrib/images e2e-init-chain

build-cosmos-relayer-docker: ## Build Docker image for the Cosmos relayer
$(MAKE) -C contrib/images cosmos-relayer

.PHONY: build-docker build-cosmos-relayer-docker
.PHONY: build-docker build-docker-e2e build-cosmos-relayer-docker

###############################################################################
### Localnet ###
Expand Down
19 changes: 15 additions & 4 deletions contrib/images/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
RELAYER_TAG := $(shell grep '^ENV RELAYER_TAG' cosmos-relayer/Dockerfile | cut -f3 -d\ )
BABYLON_FULL_PATH := $(shell git rev-parse --show-toplevel)

all: babylond cosmos-relayer

babylond: babylond-rmi
docker build --tag babylonlabs-io/babylond -f babylond/Dockerfile \
$(shell git rev-parse --show-toplevel)
docker build --tag babylonlabs-io/babylond -f babylond/Dockerfile ${BABYLON_FULL_PATH}

babylond-e2e:
docker build --tag babylonlabs-io/babylond -f babylond/Dockerfile ${BABYLON_FULL_PATH} \
--build-arg BUILD_TAGS="e2e"

babylond-rmi:
docker rmi babylonlabs-io/babylond 2>/dev/null; true

e2e-init-chain-rmi:
docker rmi babylonlabs-io/babylond-e2e-init-chain --force 2>/dev/null; true

e2e-init-chain:
docker build -t babylonlabs-io/babylond-e2e-init-chain --build-arg E2E_SCRIPT_NAME=chain \
-f e2e-initialization/init.Dockerfile ${BABYLON_FULL_PATH}

cosmos-relayer: cosmos-relayer-rmi
docker build --tag babylonlabs-io/cosmos-relayer:${RELAYER_TAG} -f cosmos-relayer/Dockerfile \
$(shell git rev-parse --show-toplevel)/contrib/images/cosmos-relayer
${BABYLON_FULL_PATH}/contrib/images/cosmos-relayer
docker tag babylonlabs-io/cosmos-relayer:${RELAYER_TAG} babylonlabs-io/cosmos-relayer:latest

cosmos-relayer-rmi:
docker rmi babylonlabs-io/cosmos-relayer 2>/dev/null; true

.PHONY: all babylond cosmos-relayer babylond-rmi cosmos-relayer-rmi
.PHONY: all babylond cosmos-relayer babylond-e2e e2e-init-chain e2e-init-chain-rmi babylond-rmi cosmos-relayer-rmi
46 changes: 46 additions & 0 deletions contrib/images/e2e-initialization/init.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM golang:1.21 as build-env

ARG E2E_SCRIPT_NAME
# Version to build. Default is empty
ARG VERSION

# Copy All
WORKDIR /go/src/github.com/babylonlabs-io/babylon
COPY ./ /go/src/github.com/babylonlabs-io/babylon/

# If version is set, then checkout this version
RUN if [ -n "${VERSION}" ]; then \
git checkout -f ${VERSION}; \
fi

RUN LEDGER_ENABLED=false LINK_STATICALLY=false E2E_SCRIPT_NAME=${E2E_SCRIPT_NAME} make e2e-build-script

FROM debian:bookworm-slim AS wasm-link

RUN apt-get update && apt-get install -y wget bash

# Label should match your github repo
LABEL org.opencontainers.image.source="https://github.com/babylonlabs-io/babylond:${VERSION}"

# Install libraries
# Cosmwasm - Download correct libwasmvm version
COPY --from=build-env /go/src/github.com/babylonlabs-io/babylon/go.mod /tmp
RUN WASMVM_VERSION=$(grep github.com/CosmWasm/wasmvm /tmp/go.mod | cut -d' ' -f2) && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm.$(uname -m).so \
-O /lib/libwasmvm.$(uname -m).so && \
# verify checksum
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \
sha256sum /lib/libwasmvm.$(uname -m).so | grep $(cat /tmp/checksums.txt | grep libwasmvm.$(uname -m) | cut -d ' ' -f 1)
RUN rm -f /tmp/go.mod

# Args only last for a single build stage - renew
ARG E2E_SCRIPT_NAME

COPY --from=build-env /go/src/github.com/babylonlabs-io/babylon/build/${E2E_SCRIPT_NAME} /bin/${E2E_SCRIPT_NAME}

# Docker ARGs are not expanded in ENTRYPOINT in the exec mode. At the same time,
# it is impossible to add CMD arguments when running a container in the shell mode.
# As a workaround, we create the entrypoint.sh script to bypass these issues.
RUN echo "#!/bin/bash\n${E2E_SCRIPT_NAME} \"\$@\"" >> entrypoint.sh && chmod +x entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]
16 changes: 9 additions & 7 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ func (s *BTCStakingTestSuite) Test3CommitPublicRandomnessAndSubmitFinalitySignat
s.Equal(prCommitMap[activatedHeight].Commitment, msgCommitPubRandList.Commitment)

// no reward gauge for finality provider and delegation yet
fpBabylonAddr := sdk.AccAddress(nonValidatorNode.SecretKey.PubKey().Address().Bytes())
fpBabylonAddr, err := sdk.AccAddressFromBech32(cacheFP.Addr)
s.NoError(err)

_, err = nonValidatorNode.QueryRewardGauge(fpBabylonAddr)
s.Error(err)
delBabylonAddr := sdk.AccAddress(nonValidatorNode.SecretKey.PubKey().Address().Bytes())
_, err = nonValidatorNode.QueryRewardGauge(delBabylonAddr)
s.Error(err)
s.ErrorContains(err, itypes.ErrRewardGaugeNotFound.Error())
delBabylonAddr := fpBabylonAddr

/*
submit finality signature
Expand Down Expand Up @@ -358,8 +358,10 @@ func (s *BTCStakingTestSuite) Test4WithdrawReward() {
s.NoError(err)

// finality provider balance before withdraw
fpBabylonAddr := sdk.AccAddress(nonValidatorNode.SecretKey.PubKey().Address().Bytes())
delBabylonAddr := sdk.AccAddress(nonValidatorNode.SecretKey.PubKey().Address().Bytes())
fpBabylonAddr, err := sdk.AccAddressFromBech32(cacheFP.Addr)
s.NoError(err)
delBabylonAddr := fpBabylonAddr

fpBalance, err := nonValidatorNode.QueryBalances(fpBabylonAddr.String())
s.NoError(err)
// finality provider reward gauge should not be fully withdrawn
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/btc_timestamping_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (s *BTCTimestampingTestSuite) SetupSuite() {
// 3. Run IBC relayer between the two chains.
// 4. Execute various e2e tests, including IBC
s.configurer, err = configurer.NewBTCTimestampingConfigurer(s.T(), true)

s.Require().NoError(err)

err = s.configurer.ConfigureChains()
Expand All @@ -50,7 +49,9 @@ func (s *BTCTimestampingTestSuite) SetupSuite() {

func (s *BTCTimestampingTestSuite) TearDownSuite() {
err := s.configurer.ClearResources()
s.Require().NoError(err)
if err != nil {
s.T().Logf("error to clear resources %s", err.Error())
}
}

// Most simple test, just checking that two chains are up and connected through
Expand Down
25 changes: 24 additions & 1 deletion test/e2e/configurer/chain/chain.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package chain

import (
"encoding/hex"
"fmt"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
"strings"
"testing"
"time"

ibctesting "github.com/cosmos/ibc-go/v8/testing"

coretypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/test/e2e/configurer/config"
"github.com/babylonlabs-io/babylon/test/e2e/containers"
"github.com/babylonlabs-io/babylon/test/e2e/initialization"
btclighttypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
)

type Config struct {
Expand All @@ -27,6 +31,7 @@ type Config struct {
LatestProposalNumber int
LatestLockNumber int
NodeConfigs []*NodeConfig
BTCHeaders []*btclighttypes.BTCHeaderInfo
IBCConfig *ibctesting.ChannelConfig

LatestCodeId int
Expand Down Expand Up @@ -57,6 +62,7 @@ func New(t *testing.T, containerManager *containers.Manager, id string, initVali
ExpeditedVotingPeriod: config.PropDepositBlocks + numVal*config.PropVoteBlocks + config.PropBufferBlocks - 2,
t: t,
containerManager: containerManager,
BTCHeaders: []*btclighttypes.BTCHeaderInfo{},
}
}

Expand Down Expand Up @@ -173,3 +179,20 @@ func (c *Config) GetNodeAtIndex(nodeIndex int) (*NodeConfig, error) {
}
return c.NodeConfigs[nodeIndex], nil
}

// BTCHeaderBytesHexJoined join all the btc headers as byte string hex
func (c *Config) BTCHeaderBytesHexJoined() string {
if c.BTCHeaders == nil || len(c.BTCHeaders) == 0 {
return ""
}

strBtcHeaders := make([]string, len(c.BTCHeaders))
for i, btcHeader := range c.BTCHeaders {
bz, err := btcHeader.Marshal()
if err != nil {
panic(err)
}
strBtcHeaders[i] = hex.EncodeToString(bz)
}
return strings.Join(strBtcHeaders, ",")
}
22 changes: 11 additions & 11 deletions test/e2e/configurer/chain/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ func (n *NodeConfig) QueryHeaderDepth(hash string) (uint64, error) {
return blcResponse.Depth, nil
}

func (n *NodeConfig) QueryListHeaders(chainID string, pagination *query.PageRequest) (*zctypes.QueryListHeadersResponse, error) {
func (n *NodeConfig) QueryListHeaders(consumerID string, pagination *query.PageRequest) (*zctypes.QueryListHeadersResponse, error) {
queryParams := url.Values{}
if pagination != nil {
queryParams.Set("pagination.key", base64.URLEncoding.EncodeToString(pagination.Key))
queryParams.Set("pagination.limit", strconv.Itoa(int(pagination.Limit)))
}

path := fmt.Sprintf("babylon/zoneconcierge/v1/headers/%s", chainID)
path := fmt.Sprintf("babylon/zoneconcierge/v1/headers/%s", consumerID)
bz, err := n.QueryGRPCGateway(path, queryParams)
require.NoError(n.t, err)

Expand All @@ -262,10 +262,10 @@ func (n *NodeConfig) QueryListHeaders(chainID string, pagination *query.PageRequ
return &resp, nil
}

func (n *NodeConfig) QueryFinalizedChainsInfo(chainIDs []string) ([]*zctypes.FinalizedChainInfo, error) {
func (n *NodeConfig) QueryFinalizedChainsInfo(consumerIDs []string) ([]*zctypes.FinalizedChainInfo, error) {
queryParams := url.Values{}
for _, chainId := range chainIDs {
queryParams.Add("chain_ids", chainId)
for _, consumerID := range consumerIDs {
queryParams.Add("chain_ids", consumerID)
}

bz, err := n.QueryGRPCGateway("babylon/zoneconcierge/v1/finalized_chains_info", queryParams)
Expand All @@ -279,11 +279,11 @@ func (n *NodeConfig) QueryFinalizedChainsInfo(chainIDs []string) ([]*zctypes.Fin
return resp.FinalizedChainsInfo, nil
}

func (n *NodeConfig) QueryEpochChainsInfo(epochNum uint64, chainIDs []string) ([]*zctypes.ChainInfo, error) {
func (n *NodeConfig) QueryEpochChainsInfo(epochNum uint64, consumerIDs []string) ([]*zctypes.ChainInfo, error) {
queryParams := url.Values{}
for _, chainId := range chainIDs {
for _, consumerID := range consumerIDs {
queryParams.Add("epoch_num", fmt.Sprintf("%d", epochNum))
queryParams.Add("chain_ids", chainId)
queryParams.Add("chain_ids", consumerID)
}

bz, err := n.QueryGRPCGateway("babylon/zoneconcierge/v1/epoch_chains_info", queryParams)
Expand All @@ -307,10 +307,10 @@ func (n *NodeConfig) QueryChains() (*[]string, error) {
return &chainsResponse.ChainIds, nil
}

func (n *NodeConfig) QueryChainsInfo(chainIDs []string) ([]*zctypes.ChainInfo, error) {
func (n *NodeConfig) QueryChainsInfo(consumerIDs []string) ([]*zctypes.ChainInfo, error) {
queryParams := url.Values{}
for _, chainId := range chainIDs {
queryParams.Add("chain_ids", chainId)
for _, consumerId := range consumerIDs {
queryParams.Add("chain_ids", consumerId)
}

bz, err := n.QueryGRPCGateway("/babylon/zoneconcierge/v1/chains_info", queryParams)
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/configurer/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func (cb *CurrentBranchConfigurer) ConfigureChain(chainConfig *chain.Config) err
tmpDir,
chainConfig.ValidatorInitConfigs,
time.Duration(chainConfig.VotingPeriod*1000000000),
time.Duration(chainConfig.ExpeditedVotingPeriod*1000000000), 0)
time.Duration(chainConfig.ExpeditedVotingPeriod*1000000000),
0,
chainConfig.BTCHeaders,
)
if err != nil {
return err
}
Expand Down
32 changes: 31 additions & 1 deletion test/e2e/initialization/chain/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"

"github.com/babylonlabs-io/babylon/test/e2e/initialization"
btclighttypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
)

func main() {
Expand All @@ -16,6 +19,7 @@ func main() {
dataDir string
chainId string
config string
btcHeadersBytesHexStr string
votingPeriod time.Duration
expeditedVotingPeriod time.Duration
forkHeight int
Expand All @@ -24,6 +28,7 @@ func main() {
flag.StringVar(&dataDir, "data-dir", "", "chain data directory")
flag.StringVar(&chainId, "chain-id", "", "chain ID")
flag.StringVar(&config, "config", "", "serialized config")
flag.StringVar(&btcHeadersBytesHexStr, "btc-headers", "", "btc header bytes comma separated")
flag.DurationVar(&votingPeriod, "voting-period", 30000000000, "voting period")
flag.DurationVar(&expeditedVotingPeriod, "expedited-voting-period", 20000000000, "expedited voting period")
flag.IntVar(&forkHeight, "fork-height", 0, "fork height")
Expand All @@ -43,7 +48,8 @@ func main() {
panic(err)
}

createdChain, err := initialization.InitChain(chainId, dataDir, valConfig, votingPeriod, expeditedVotingPeriod, forkHeight)
btcHeaders := btcHeaderFromFlag(btcHeadersBytesHexStr)
createdChain, err := initialization.InitChain(chainId, dataDir, valConfig, votingPeriod, expeditedVotingPeriod, forkHeight, btcHeaders)
if err != nil {
panic(err)
}
Expand All @@ -54,3 +60,27 @@ func main() {
panic(err)
}
}

func btcHeaderFromFlag(btcHeadersBytesHexStr string) []*btclighttypes.BTCHeaderInfo {
btcHeaders := []*btclighttypes.BTCHeaderInfo{}
if len(btcHeadersBytesHexStr) == 0 {
return btcHeaders
}

btcHeadersBytesHex := strings.Split(btcHeadersBytesHexStr, ",")
for _, btcHeaderBytesHex := range btcHeadersBytesHex {
btcHeaderBytes, err := hex.DecodeString(btcHeaderBytesHex)
if err != nil {
panic(err)
}

btcHeader := &btclighttypes.BTCHeaderInfo{}
err = btcHeader.Unmarshal(btcHeaderBytes)
if err != nil {
panic(err)
}

btcHeaders = append(btcHeaders, btcHeader)
}
return btcHeaders
}
Loading

0 comments on commit 009cd29

Please sign in to comment.