Skip to content

Commit

Permalink
CCIP-4408: Adding mock for lbtc test
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami committed Dec 4, 2024
1 parent 836ea2c commit 3444dfc
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type CCIPJobSpecParams struct {
DestStartBlock uint64
USDCAttestationAPI string
USDCConfig *config.USDCConfig
LBTCConfig *config.LBTCConfig
P2PV2Bootstrappers pq.StringArray
}

Expand Down Expand Up @@ -295,6 +296,11 @@ func (params CCIPJobSpecParams) ExecutionJobSpec() (*OCR2TaskJobSpec, error) {
ocrSpec.PluginConfig["USDCConfig.SourceMessageTransmitterAddress"] = fmt.Sprintf(`"%s"`, params.USDCConfig.SourceMessageTransmitterAddress)
ocrSpec.PluginConfig["USDCConfig.AttestationAPITimeoutSeconds"] = params.USDCConfig.AttestationAPITimeoutSeconds
}
if params.LBTCConfig != nil {
ocrSpec.PluginConfig["LBTCConfig.AttestationAPI"] = fmt.Sprintf(`"%s"`, params.LBTCConfig.AttestationAPI)
ocrSpec.PluginConfig["LBTCConfig.SourceTokenAddress"] = fmt.Sprintf(`"%s"`, params.LBTCConfig.SourceTokenAddress)
ocrSpec.PluginConfig["LBTCConfig.AttestationAPITimeoutSeconds"] = params.LBTCConfig.AttestationAPITimeoutSeconds
}
return &OCR2TaskJobSpec{
OCR2OracleSpec: ocrSpec,
JobType: "offchainreporting2",
Expand Down
79 changes: 79 additions & 0 deletions integration-tests/ccip-tests/actions/ccip_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"math/big"
"net/http"
"runtime"
Expand All @@ -31,11 +32,13 @@ import (
"golang.org/x/exp/rand"
"golang.org/x/sync/errgroup"

"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/lib/utils/ptr"

chainselectors "github.com/smartcontractkit/chain-selectors"

commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake"
"github.com/smartcontractkit/chainlink-testing-framework/lib/blockchain"
ctfClient "github.com/smartcontractkit/chainlink-testing-framework/lib/client"
ctftestenv "github.com/smartcontractkit/chainlink-testing-framework/lib/docker/test_env"
Expand Down Expand Up @@ -150,6 +153,7 @@ func GetUSDCDomain(networkName string, simulated bool) (uint32, error) {
if val, ok := lookup[networkName]; ok {
return val, nil
}

return 0, fmt.Errorf("USDC domain not found for chain %s", networkName)
}

Expand All @@ -176,6 +180,7 @@ type CCIPCommon struct {
MulticallContract common.Address
ExistingDeployment bool
USDCMockDeployment *bool
LBTCMockDeployment *bool
TokenMessenger *common.Address
TokenTransmitter *contracts.TokenTransmitter
IsConnectionRestoredRecently *atomic.Bool
Expand Down Expand Up @@ -720,6 +725,10 @@ func (ccipModule *CCIPCommon) IsUSDCDeployment() bool {
return pointer.GetBool(ccipModule.USDCMockDeployment)
}

func (ccipModule *CCIPCommon) IsLBTCDeployment() bool {
return pointer.GetBool(ccipModule.LBTCMockDeployment)
}

func (ccipModule *CCIPCommon) WriteLaneConfig(conf *laneconfig.LaneConfig) {
var btAddresses, btpAddresses []string
priceAggrs := make(map[string]string)
Expand Down Expand Up @@ -925,6 +934,16 @@ func (ccipModule *CCIPCommon) DeployContracts(
if err != nil {
return fmt.Errorf("granting minter role to token transmitter shouldn't fail %w", err)
}
} else if ccipModule.IsLBTCDeployment() && i == 0 {
// if it's USDC deployment, we deploy the burn mint token 677 with decimal 6 and cast it to ERC20Token
lbtcToken, err := ccipModule.tokenDeployer.DeployBurnMintERC677(new(big.Int).Mul(big.NewInt(1e6), big.NewInt(1e18)))
if err != nil {
return fmt.Errorf("deploying bridge lbtc token contract shouldn't fail %w", err)
}
token, err = ccipModule.tokenDeployer.NewERC20TokenContract(lbtcToken.ContractAddress)
if err != nil {
return fmt.Errorf("getting new bridge lbtc token contract shouldn't fail %w", err)
}
} else {
// otherwise we deploy link token and cast it to ERC20Token
linkToken, err := ccipModule.tokenDeployer.DeployLinkTokenContract()
Expand Down Expand Up @@ -3734,6 +3753,16 @@ func (lane *CCIPLane) DeployNewCCIPLane(
AttestationAPITimeoutSeconds: 5,
}
}
if !lane.Source.Common.ExistingDeployment && lane.Source.Common.IsLBTCDeployment() {
api := "/bridge/v1/deposits/getByHash"
url := framework.HostDockerInternal() + api
// Only one LBTC allowed per chain
jobParams.LBTCConfig = &config.LBTCConfig{
SourceTokenAddress: common.HexToAddress(lane.Source.Common.BridgeTokens[0].Address()),
AttestationAPI: url,
AttestationAPITimeoutSeconds: 5,
}
}
if !bootstrapAdded.Load() {
bootstrapAdded.Store(true)
err := CreateBootstrapJob(jobParams, bootstrapCommit, bootstrapExec)
Expand Down Expand Up @@ -4422,6 +4451,56 @@ func SetMockServerWithUSDCAttestation(
return nil
}

// SetMockServerWithLBTCAttestation responds with a mock attestation for any msgHash
// The path is set with regex to match any path that starts with /v1/attestations
func SetMockServerWithLBTCAttestation() error {
apiPath := "/bridge/v1/deposits/getByHash"
cfg := &fake.Input{
Port: 9111,
}
out, err := fake.NewFakeDataProvider(cfg)
if err != nil {
return fmt.Errorf("failed to create fake data provider: %w", err)
}
path := out.BaseURLDocker + apiPath
method := "POST"
err = fake.Func(
method,
apiPath,
func(c *gin.Context) {
var requestBody struct {
MessageHashes []string `json:"messageHash"`
}
if err := c.ShouldBindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON"})
return
}

// You can log the request body or process it if needed
fmt.Println("Received messageHash:", requestBody.MessageHashes[0])

// Mock response body based on the request
mockResponse := gin.H{
"attestations": []map[string]any{
{
"message_hash": requestBody.MessageHashes[0],
"status": "NOTARIZATION_STATUS_SESSION_APPROVED",
"attestation": "0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000e45c70a5050000000000000000000000000000000000000000000000000000000000aa36a7000000000000000000000000845f8e3c214d8d0e4d83fc094f302aa26a12a0bc0000000000000000000000000000000000000000000000000000000000014a34000000000000000000000000845f8e3c214d8d0e4d83fc094f302aa26a12a0bc00000000000000000000000062f10ce5b727edf787ea45776bd050308a61150800000000000000000000000000000000000000000000000000000000000003e60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000040277eeafba008d767c2636d9428f2ebb13ab29ac70337f4fc34b0f5606767cae546f9be3f12160de6d142e5b3c1c3ebd0bf4298662b32b597d0cc5970c7742fc10000000000000000000000000000000000000000000000000000000000000040bbcd60ecc9e06f2effe7c94161219498a1eb435b419387adadb86ec9a52dfb066ce027532517df7216404049d193a25b85c35edfa3e7c5aa4757bfe84887a3980000000000000000000000000000000000000000000000000000000000000040da4a6dc619b5ca2349783cabecc4efdbc910090d3e234d7b8d0430165f8fae532f9a965ceb85c18bb92e059adefa7ce5835850a705761ab9e026d2db4a13ef9a",
},
},
}

// Return the mocked response
c.JSON(http.StatusOK, mockResponse)
},
)
if err != nil {
return fmt.Errorf("failed to create mock response: %w", err)
}
log.Info().Str("path", path).Msg("setting attestation-api response for any msgHash")
return nil
}

// SetMockserverWithTokenPriceValue sets the mock responses in mockserver that are read by chainlink nodes
// to simulate different price feed value.
// it keeps updating the response every 15 seconds to simulate price feed updates
Expand Down
1 change: 1 addition & 0 deletions integration-tests/ccip-tests/testconfig/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ type CCIPTestGroupConfig struct {
MaxNoOfLanes int `toml:",omitempty"`
ChaosDuration *config.Duration `toml:",omitempty"`
USDCMockDeployment *bool `toml:",omitempty"`
LBTCMockDeployment *bool `toml:",omitempty"`
CommitOCRParams *contracts.OffChainAggregatorV2Config `toml:",omitempty"`
ExecOCRParams *contracts.OffChainAggregatorV2Config `toml:",omitempty"`
OffRampConfig *OffRampConfig `toml:",omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ NoOfSendsInMulticall = 5 # if MulticallInOneTx=true , this denotes the number of

NoOfNetworks = 2 # this is used with Networks in `CCIP.Env`, `NoOfNetworks < len(CCIP.Env.Networks)` test only uses first NoOfNetworks from` CCIP.Env.Networks`.
# This value is ignored if CCIP.Groups.<TestGroup>.NetworkPairs is provided

LBTCMockDeployment = true

[CCIP.Groups.smoke.MsgDetails]
MsgType = 'DataWithToken' # type of message to be sent, either 'Token' or 'DataWithToken' Or 'Data'
Expand Down
9 changes: 8 additions & 1 deletion integration-tests/ccip-tests/testsetups/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,14 @@ func CCIPDefaultTestSetUp(
// we need to set it only once for all the lanes as the attestation path uses regex to match the path for
// all messages across all lanes
err = actions.SetMockServerWithUSDCAttestation(killgrave, setUpArgs.Env.MockServer)
require.NoError(t, err, "failed to set up mock server for attestation")
require.NoError(t, err, "failed to set up mock server for USDC attestation")
}
if pointer.GetBool(setUpArgs.Cfg.TestGroupInput.LBTCMockDeployment) {
// if it's a new LBTC deployment, set up mock server for attestation,
// we need to set it only once for all the lanes as the attestation path uses regex to match the path for
// all messages across all lanes
err = actions.SetMockServerWithLBTCAttestation()
require.NoError(t, err, "failed to set up mock server for LBTC attestation")
}
}
// deploy all lane specific contracts
Expand Down
Loading

0 comments on commit 3444dfc

Please sign in to comment.