Skip to content

Commit

Permalink
update: set up code for integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
perfogic committed Sep 24, 2024
1 parent 69aee78 commit 93111cc
Show file tree
Hide file tree
Showing 25 changed files with 1,814 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.oraid/*
# Logs
logs
*.log
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
"build-tsc": "tsc -p",
"deploy": "yarn build && lerna publish",
"prepare": "husky install",
"build:docker": "lerna run build --concurrency 1"
"build:docker": "lerna run build --concurrency 1",
"integration:setup": "yarn run:regtest && yarn run:oraid",
"integration:clean": "yarn clean:regtest && yarn clean:oraid",
"integration:run": "jest --testPathPattern=integrations --forceExit",
"integration:all": "npm run integration:clean; npm run integration:setup; npm run integration:run; npm run integration:clean",
"run:regtest": "bitcoind -regtest -server -rpcuser=satoshi -rpcpassword=nakamoto --fallbackfee=0.001 --daemon --datadir=$HOME",
"run:oraid": "sh $PWD/scripts/single-local-node.sh",
"clean:regtest": "killall bitcoind; rm -rf $HOME/regtest",
"clean:oraid": "killall oraid; rm -rf $HOME/oraid"
},
"workspaces": [
"packages/*"
Expand All @@ -29,7 +37,8 @@
"@cosmjs/tendermint-rpc": "0.31.3",
"@oraichain/oraidex-common": "^1.0.10",
"joi": "^17.13.3",
"readline-sync": "^1.4.10"
"readline-sync": "^1.4.10",
"regtest-client": "^0.2.1"
},
"devDependencies": {
"@babel/traverse": "7.24.1",
Expand Down
35 changes: 32 additions & 3 deletions packages/orchestrator/src/services/relayer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Dest,
WrappedHeader,
} from "@oraichain/bitcoin-bridge-contracts-sdk/build/CwBitcoin.types";
import { redeemScript } from "@oraichain/bitcoin-bridge-lib-js";
import { BitcoinNetwork, redeemScript } from "@oraichain/bitcoin-bridge-lib-js";
import {
commitmentBytes,
decodeRawTx,
Expand Down Expand Up @@ -737,10 +737,39 @@ class RelayerService implements RelayerInterface {
return deposits;
}

// for testing purpose only
async generateDepositAddress(
checkpointIndex: number,
dest: Dest,
network: btc.networks.Network
) {
const checkpoint = await (checkpointIndex
? this.appBitcoinClient.checkpointByIndex({
index: checkpointIndex,
})
: this.appBitcoinClient.buildingCheckpoint());

const checkpointConfig = await this.appBitcoinClient.checkpointConfig();
const sigset = checkpoint.sigset;
const encodedDest = commitmentBytes(convertSdkDestToWasmDest(dest));
const depositScript = redeemScript(
sigset,
Buffer.from(encodedDest),
checkpointConfig.sigset_threshold
);
let wsh = btc.payments.p2wsh({
redeem: { output: depositScript },
network,
});
let address = wsh.address;
return address;
}

async submitDepositAddress(
depositAddr: string,
checkpointIndex: number,
dest: Dest
dest: Dest,
network?: BitcoinNetwork
): Promise<void> {
const checkpoint = await (checkpointIndex
? this.appBitcoinClient.checkpointByIndex({
Expand All @@ -760,7 +789,7 @@ class RelayerService implements RelayerInterface {
);
let wsh = btc.payments.p2wsh({
redeem: { output: depositScript },
network: getCurrentNetwork(),
network: getCurrentNetwork(network),
});
let address = wsh.address;

Expand Down
10 changes: 7 additions & 3 deletions packages/orchestrator/src/utils/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export enum ScriptPubkeyType {
WitnessScriptHash = "witness_v0_scripthash",
}

export function getCurrentNetwork() {
if (env.bitcoin.network === "mainnet") {
export function getCurrentNetwork(network?: btc.networks.Network) {
let envNetwork = network || env.bitcoin.network;
if (envNetwork === "mainnet") {
return btc.networks.bitcoin;
}
if (env.bitcoin.network === "testnet") {
if (envNetwork === "testnet") {
return btc.networks.testnet;
}
if (envNetwork === "regtest") {
return btc.networks.regtest;
}
throw new Error("Invalid network");
}

Expand Down
158 changes: 158 additions & 0 deletions packages/orchestrator/tests/integrations/_init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import path from "path";
import { uploadContracts } from "./helpers/contract";
import { EntryPointClient } from "./testdata/interfaces";

export async function initOsorContract(
client: SigningCosmWasmClient,
address: string
) {
const contracts = [
{
name: "skip_api_entry_point",
wasmFile: path.join(__dirname, "testdata/skip-api-entry-point.wasm"),
},
{
name: "skip_api_ibc_adapter_ibc_hooks",
wasmFile: path.join(
__dirname,
"testdata/skip-api-ibc-adapter-ibc-hooks.wasm"
),
},
];

// upload contract
const codeId = await uploadContracts(client, address, contracts);
const contractId = {
skipApiEntryPoint: codeId.skip_api_entry_point,
skipApiIbcAdapterIbcHooks: codeId.skip_api_ibc_adapter_ibc_hooks,
};

const entryPoint = await client.instantiate(
address,
contractId.skipApiEntryPoint,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{} as epInstantiateMsg,
"entry point test",
"auto",
{
admin: address,
}
);
console.log("Entrypoint contract:", entryPoint.contractAddress);

const ibcHooks = await client.instantiate(
address,
contractId.skipApiIbcAdapterIbcHooks,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{
entry_point_contract_address: entryPoint.contractAddress,
},
"ibc hooks test",
"auto",
{
admin: address,
}
);

const entryPointClient = new EntryPointClient(
client,
address,
entryPoint.contractAddress
);
const tx = await entryPointClient.updateConfig({
ibcTransferContractAddress: ibcHooks.contractAddress,
});

return {
entryPointAddress: entryPoint.contractAddress,
ibcHooksAddress: ibcHooks.contractAddress,
};
}

export async function initBitcoinContract(
client: SigningCosmWasmClient,
address: string,
entryPointAddress: string
) {
const contracts = [
{
name: "cw_light_client_bitcoin",
wasmFile: path.join(__dirname, "testdata/cw-light-client-bitcoin.wasm"),
},
{
name: "cw_app_bitcoin",
wasmFile: path.join(__dirname, "testdata/cw-app-bitcoin.wasm"),
},
{
name: "token_factory",
wasmFile: path.join(__dirname, "testdata/tokenfactory.wasm"),
},
];
// upload contract
const codeId = await uploadContracts(client, address, contracts);
const contractId = {
tokenFactory: codeId.token_factory,
cwLightClientBitcoin: codeId.cw_light_client_bitcoin,
cwAppBitcoin: codeId.cw_app_bitcoin,
};

const tokenFactoryMsg = {};
const tokenFactoryContract = await client.instantiate(
address,
contractId.tokenFactory,
tokenFactoryMsg,
"bitcoin app contract",
"auto",
{
admin: address,
}
);

const lightClientMsg = {};
const lightClientContract = await client.instantiate(
address,
contractId.cwLightClientBitcoin,
lightClientMsg,
"bitcoin light client contract",
"auto",
{
admin: address,
}
);

const appMsg = {
light_client_contract: lightClientContract.contractAddress,
token_factory_contract: tokenFactoryContract.contractAddress,
relayer_fee: "0",
relayer_fee_receiver: "orai1ehmhqcn8erf3dgavrca69zgp4rtxj5kqgtcnyd",
relayer_fee_token: {
native_token: {
denom: "orai",
},
},
token_fee_receiver: "orai1ehmhqcn8erf3dgavrca69zgp4rtxj5kqgtcnyd",
osor_entry_point_contract: entryPointAddress,
};

const appContract = await client.instantiate(
address,
contractId.cwAppBitcoin,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
appMsg,
"bitcoin app contract",
"auto",
{
admin: address,
}
);

return {
lightClientBitcoinAddress: lightClientContract.contractAddress,
appBitcoinAddress: appContract.contractAddress,
tokenFactoryAddress: tokenFactoryContract.contractAddress,
};
}
Loading

0 comments on commit 93111cc

Please sign in to comment.