@argonprotocol/testing 1.4.3-dev.8c3666ee → 1.4.3-dev.8e7d7af7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -10,9 +10,9 @@ import { Keyring as Keyring3, TxSubmitter as TxSubmitter2 } from "@argonprotocol
10
10
  import * as process4 from "process";
11
11
  import HttpProxy from "http-proxy";
12
12
  import * as child_process4 from "child_process";
13
- import * as http from "http";
13
+ import * as http2 from "http";
14
14
  import * as url from "url";
15
- import * as Path6 from "path";
15
+ import * as Path7 from "path";
16
16
 
17
17
  // src/TestNotary.ts
18
18
  import { customAlphabet } from "nanoid";
@@ -326,9 +326,20 @@ var TestMainchain = class {
326
326
  return this.address;
327
327
  }
328
328
  async client() {
329
- const client = await getClient(this.address);
330
- disconnectOnTeardown(client);
331
- return client;
329
+ let lastError;
330
+ for (let attempt = 0; attempt < 20; attempt += 1) {
331
+ try {
332
+ const client = await getClient(this.address);
333
+ disconnectOnTeardown(client);
334
+ return client;
335
+ } catch (error) {
336
+ lastError = error;
337
+ await new Promise((resolve3) => setTimeout(resolve3, 250));
338
+ }
339
+ }
340
+ throw new Error(`Unable to connect to mainchain client at ${this.address}`, {
341
+ cause: lastError instanceof Error ? lastError : void 0
342
+ });
332
343
  }
333
344
  async bootAddress() {
334
345
  const client = await this.client();
@@ -541,11 +552,459 @@ var TestOracle = class _TestOracle {
541
552
  }
542
553
  };
543
554
 
555
+ // src/TestEthereum.ts
556
+ import * as fs4 from "fs/promises";
557
+ import * as os from "os";
558
+ import * as Path5 from "path";
559
+ import { spawn as spawn4, spawnSync } from "child_process";
560
+ import { detectPort as detectPort2 } from "detect-port";
561
+
562
+ // src/ethereumContracts.ts
563
+ import { readFileSync } from "fs";
564
+ function loadArtifact(fileName) {
565
+ return JSON.parse(
566
+ readFileSync(new URL(`./ethereum-contracts/${fileName}`, import.meta.url), "utf8")
567
+ );
568
+ }
569
+ var argonTokenArtifact = loadArtifact("ArgonToken.json");
570
+ var argonotTokenArtifact = loadArtifact("ArgonotToken.json");
571
+ var mintingGatewayArtifact = loadArtifact("MintingGateway.json");
572
+ var proxyAdminArtifact = loadArtifact("ProxyAdmin.json");
573
+ var transparentUpgradeableProxyArtifact = loadArtifact("TransparentUpgradeableProxy.json");
574
+
575
+ // src/TestEthereum.ts
576
+ import { privateKeyToAccount } from "viem/accounts";
577
+ import {
578
+ createPublicClient,
579
+ createWalletClient,
580
+ defineChain,
581
+ encodeFunctionData,
582
+ getAddress,
583
+ http,
584
+ zeroAddress
585
+ } from "viem";
586
+ var DEFAULT_KURTOSIS_BIN = "kurtosis";
587
+ var DEFAULT_ETHEREUM_PACKAGE = "github.com/ethpandaops/ethereum-package";
588
+ var DEFAULT_EL_PORT_START = 32e3;
589
+ var DEFAULT_CL_PORT_START = 33e3;
590
+ var PORT_RANGE_SIZE = 32;
591
+ var ENCLAVE_NAME_PREFIX = "argon-eth-";
592
+ var PROBE_INTERVAL_MS = 1e3;
593
+ var PROBE_TIMEOUT_MS = 6e4;
594
+ var LIGHT_CLIENT_READY_TIMEOUT_MS = 5 * 6e4;
595
+ var KURTOSIS_RUN_TIMEOUT_MS = 20 * 6e4;
596
+ var DEFAULT_SEED_ARGON_AMOUNT_BASE_UNITS = 1000000000n;
597
+ var ERC1967_ADMIN_SLOT = "0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103";
598
+ var TestEthereum = class {
599
+ enclaveName;
600
+ kurtosisBin;
601
+ packageRef;
602
+ executionRpcUrl;
603
+ beaconApiUrl;
604
+ chainId;
605
+ #argsDir;
606
+ constructor(enclaveName = `${ENCLAVE_NAME_PREFIX}${Math.random().toString(36).slice(2, 8)}`, kurtosisBin = DEFAULT_KURTOSIS_BIN, packageRef = DEFAULT_ETHEREUM_PACKAGE) {
607
+ this.enclaveName = enclaveName;
608
+ this.kurtosisBin = kurtosisBin;
609
+ this.packageRef = packageRef;
610
+ addTeardown(this);
611
+ }
612
+ static isInstalled(kurtosisBin = DEFAULT_KURTOSIS_BIN) {
613
+ return spawnSync(kurtosisBin, ["version"], { stdio: "ignore" }).status === 0;
614
+ }
615
+ async launch(options) {
616
+ const {
617
+ consensusClient = "lighthouse",
618
+ preset = "mainnet",
619
+ secondsPerSlot,
620
+ waitForFinalization = true,
621
+ prefundedAccounts
622
+ } = options ?? {};
623
+ const elPublicPortStart = await findFreePortRange(DEFAULT_EL_PORT_START, PORT_RANGE_SIZE);
624
+ const clPublicPortStart = await findFreePortRange(DEFAULT_CL_PORT_START, PORT_RANGE_SIZE);
625
+ this.#argsDir = await fs4.mkdtemp(Path5.join(os.tmpdir(), "argon-ethereum-devnet-"));
626
+ const argsFile = Path5.join(this.#argsDir, "network-params.yaml");
627
+ await fs4.writeFile(
628
+ argsFile,
629
+ renderEthereumArgs(
630
+ elPublicPortStart,
631
+ clPublicPortStart,
632
+ consensusClient,
633
+ preset,
634
+ secondsPerSlot,
635
+ waitForFinalization,
636
+ prefundedAccounts
637
+ )
638
+ );
639
+ await runCommand(
640
+ this.kurtosisBin,
641
+ ["run", "--enclave", this.enclaveName, this.packageRef, "--args-file", argsFile],
642
+ KURTOSIS_RUN_TIMEOUT_MS
643
+ );
644
+ const executionRpc = await waitForProbe(
645
+ () => findExecutionRpcUrl(elPublicPortStart, PORT_RANGE_SIZE),
646
+ PROBE_TIMEOUT_MS
647
+ );
648
+ const beaconApi = await waitForProbe(
649
+ () => findBeaconApiUrl(clPublicPortStart, PORT_RANGE_SIZE),
650
+ PROBE_TIMEOUT_MS
651
+ );
652
+ this.executionRpcUrl = executionRpc.url;
653
+ this.beaconApiUrl = beaconApi.url;
654
+ this.chainId = executionRpc.chainId;
655
+ await waitForProbe(
656
+ () => this.getBeacon("/eth/v1/beacon/genesis"),
657
+ LIGHT_CLIENT_READY_TIMEOUT_MS
658
+ );
659
+ return {
660
+ executionRpcUrl: this.executionRpcUrl,
661
+ beaconApiUrl: this.beaconApiUrl,
662
+ chainId: this.chainId
663
+ };
664
+ }
665
+ async callExecution(method, params = []) {
666
+ const executionRpcUrl = this.executionRpcUrl;
667
+ if (!executionRpcUrl) {
668
+ throw new Error("Execution RPC URL is not available before launch");
669
+ }
670
+ const response = await fetch(executionRpcUrl, {
671
+ method: "POST",
672
+ headers: { "content-type": "application/json" },
673
+ body: JSON.stringify({
674
+ id: 1,
675
+ jsonrpc: "2.0",
676
+ method,
677
+ params
678
+ }),
679
+ signal: AbortSignal.timeout(1e4)
680
+ });
681
+ if (!response.ok) {
682
+ throw new Error(`Execution RPC request failed for ${method}: ${response.status}`);
683
+ }
684
+ const body = await response.json();
685
+ if (body.error) {
686
+ throw new Error(
687
+ `Execution RPC ${method} failed (${body.error.code ?? "unknown"}): ${body.error.message ?? "unknown error"}`
688
+ );
689
+ }
690
+ return body.result;
691
+ }
692
+ async getBeacon(path2) {
693
+ const beaconApiUrl = this.beaconApiUrl;
694
+ if (!beaconApiUrl) {
695
+ throw new Error("Beacon API URL is not available before launch");
696
+ }
697
+ const response = await fetch(new URL(path2, `${beaconApiUrl}/`), {
698
+ signal: AbortSignal.timeout(1e4)
699
+ });
700
+ if (!response.ok) {
701
+ throw new Error(`Beacon API request failed for ${path2}: ${response.status}`);
702
+ }
703
+ return await response.json();
704
+ }
705
+ async deployMintingGatewayFixture(options) {
706
+ const { executionRpcUrl, chainId } = this;
707
+ if (!executionRpcUrl || !chainId) {
708
+ throw new Error("Ethereum devnet must be launched before deploying MintingGateway fixtures");
709
+ }
710
+ const account = privateKeyToAccount(options.deployerPrivateKey);
711
+ const adminSafe = options.adminSafe ?? account.address;
712
+ const guardianSafe = options.guardianSafe ?? adminSafe;
713
+ const chain = createExecutionChain(chainId, executionRpcUrl);
714
+ const publicClient = createPublicClient({
715
+ chain,
716
+ transport: http(executionRpcUrl)
717
+ });
718
+ const walletClient = createWalletClient({
719
+ account,
720
+ chain,
721
+ transport: http(executionRpcUrl)
722
+ });
723
+ const bootstrapImplementationAddress = await deployContract(walletClient, publicClient, {
724
+ abi: mintingGatewayArtifact.abi,
725
+ bytecode: mintingGatewayArtifact.bytecode,
726
+ args: [zeroAddress, zeroAddress]
727
+ });
728
+ const initializeData = encodeFunctionData({
729
+ abi: mintingGatewayArtifact.abi,
730
+ functionName: "initialize",
731
+ args: [adminSafe, guardianSafe]
732
+ });
733
+ const gatewayAddress = await deployContract(walletClient, publicClient, {
734
+ abi: transparentUpgradeableProxyArtifact.abi,
735
+ bytecode: transparentUpgradeableProxyArtifact.bytecode,
736
+ args: [bootstrapImplementationAddress, adminSafe, initializeData]
737
+ });
738
+ const proxyAdminAddress = getAddressFromStorage(
739
+ await publicClient.getStorageAt({
740
+ address: gatewayAddress,
741
+ slot: ERC1967_ADMIN_SLOT
742
+ })
743
+ );
744
+ const argonTokenAddress = await deployContract(walletClient, publicClient, {
745
+ abi: argonTokenArtifact.abi,
746
+ bytecode: argonTokenArtifact.bytecode,
747
+ args: [gatewayAddress]
748
+ });
749
+ const argonotTokenAddress = await deployContract(walletClient, publicClient, {
750
+ abi: argonotTokenArtifact.abi,
751
+ bytecode: argonotTokenArtifact.bytecode,
752
+ args: [gatewayAddress]
753
+ });
754
+ const finalImplementationAddress = await deployContract(walletClient, publicClient, {
755
+ abi: mintingGatewayArtifact.abi,
756
+ bytecode: mintingGatewayArtifact.bytecode,
757
+ args: [argonTokenAddress, argonotTokenAddress]
758
+ });
759
+ const upgradeHash = await walletClient.sendTransaction({
760
+ to: proxyAdminAddress,
761
+ data: encodeFunctionData({
762
+ abi: proxyAdminArtifact.abi,
763
+ functionName: "upgradeAndCall",
764
+ args: [gatewayAddress, finalImplementationAddress, "0x"]
765
+ })
766
+ });
767
+ const upgradeReceipt = await waitForExecutionReceipt(publicClient, upgradeHash);
768
+ if (upgradeReceipt.status !== "success") {
769
+ throw new Error("MintingGateway proxy upgrade failed");
770
+ }
771
+ if (options.seedArgonRecipient) {
772
+ const mintHash = await walletClient.sendTransaction({
773
+ to: gatewayAddress,
774
+ data: encodeFunctionData({
775
+ abi: mintingGatewayArtifact.abi,
776
+ functionName: "adminMintBatch",
777
+ args: [
778
+ argonTokenAddress,
779
+ [options.seedArgonRecipient],
780
+ [options.seedArgonAmountBaseUnits ?? DEFAULT_SEED_ARGON_AMOUNT_BASE_UNITS]
781
+ ]
782
+ })
783
+ });
784
+ const mintReceipt = await waitForExecutionReceipt(publicClient, mintHash);
785
+ if (mintReceipt.status !== "success") {
786
+ throw new Error("MintingGateway admin mint failed");
787
+ }
788
+ }
789
+ return {
790
+ argonTokenAddress,
791
+ argonotTokenAddress,
792
+ gatewayAddress
793
+ };
794
+ }
795
+ async teardown() {
796
+ if (this.#argsDir) {
797
+ await fs4.rm(this.#argsDir, { recursive: true, force: true });
798
+ this.#argsDir = void 0;
799
+ }
800
+ await runCommand(this.kurtosisBin, ["enclave", "rm", "-f", this.enclaveName], 6e4, true);
801
+ }
802
+ };
803
+ async function deployContract(walletClient, publicClient, request) {
804
+ const hash = await walletClient.deployContract({
805
+ ...request,
806
+ account: walletClient.account,
807
+ chain: walletClient.chain
808
+ });
809
+ const receipt = await waitForExecutionReceipt(publicClient, hash);
810
+ if (receipt.status !== "success" || !receipt.contractAddress) {
811
+ throw new Error(`Contract deployment failed for ${request.bytecode.slice(0, 10)}`);
812
+ }
813
+ return receipt.contractAddress;
814
+ }
815
+ function getAddressFromStorage(value) {
816
+ if (!value || value === "0x") {
817
+ throw new Error("Missing proxy admin address in ERC1967 admin slot");
818
+ }
819
+ return getAddress(`0x${value.slice(-40)}`);
820
+ }
821
+ function renderEthereumArgs(elPublicPortStart, clPublicPortStart, consensusClient, preset, secondsPerSlot, waitForFinalization, prefundedAccounts) {
822
+ const lines = [
823
+ "participants:",
824
+ " - el_type: geth",
825
+ ` cl_type: ${consensusClient}`,
826
+ "network_params:",
827
+ " network: kurtosis",
828
+ ` preset: ${preset}`,
829
+ ...secondsPerSlot ? [` seconds_per_slot: ${secondsPerSlot}`] : [],
830
+ ...prefundedAccounts && Object.keys(prefundedAccounts).length > 0 ? [` prefunded_accounts: '${JSON.stringify(prefundedAccounts)}'`] : [],
831
+ "additional_services: []",
832
+ `wait_for_finalization: ${waitForFinalization ? "true" : "false"}`,
833
+ "global_log_level: warn",
834
+ "port_publisher:",
835
+ " el:",
836
+ " enabled: true",
837
+ ` public_port_start: ${elPublicPortStart}`,
838
+ " cl:",
839
+ " enabled: true",
840
+ ` public_port_start: ${clPublicPortStart}`
841
+ ];
842
+ lines.push("");
843
+ return lines.join("\n");
844
+ }
845
+ async function findExecutionRpcUrl(portStart, rangeSize) {
846
+ for (let port2 = portStart; port2 < portStart + rangeSize; port2 += 1) {
847
+ const url2 = `http://127.0.0.1:${port2}`;
848
+ const response = await fetchJsonRpc(url2, "eth_chainId");
849
+ if (typeof response === "string") {
850
+ return { url: url2, chainId: response };
851
+ }
852
+ }
853
+ throw new Error(
854
+ `Unable to find an execution RPC endpoint in ${portStart}-${portStart + rangeSize - 1}`
855
+ );
856
+ }
857
+ async function findBeaconApiUrl(portStart, rangeSize) {
858
+ for (let port2 = portStart; port2 < portStart + rangeSize; port2 += 1) {
859
+ const url2 = `http://127.0.0.1:${port2}`;
860
+ try {
861
+ const response = await fetch(new URL("/eth/v1/node/version", `${url2}/`), {
862
+ signal: AbortSignal.timeout(2e3)
863
+ });
864
+ if (!response.ok) {
865
+ continue;
866
+ }
867
+ const body = await response.json();
868
+ if (body.data?.version) {
869
+ return { url: url2 };
870
+ }
871
+ } catch {
872
+ }
873
+ }
874
+ throw new Error(
875
+ `Unable to find a Beacon API endpoint in ${portStart}-${portStart + rangeSize - 1}`
876
+ );
877
+ }
878
+ async function fetchJsonRpc(url2, method) {
879
+ try {
880
+ const response = await fetch(url2, {
881
+ method: "POST",
882
+ headers: { "content-type": "application/json" },
883
+ body: JSON.stringify({
884
+ id: 1,
885
+ jsonrpc: "2.0",
886
+ method,
887
+ params: []
888
+ }),
889
+ signal: AbortSignal.timeout(2e3)
890
+ });
891
+ if (!response.ok) {
892
+ return null;
893
+ }
894
+ const body = await response.json();
895
+ return body.result ?? null;
896
+ } catch {
897
+ return null;
898
+ }
899
+ }
900
+ function createExecutionChain(chainId, executionRpcUrl) {
901
+ return defineChain({
902
+ id: Number.parseInt(chainId, 16),
903
+ name: "argon-test-ethereum",
904
+ nativeCurrency: {
905
+ name: "Ether",
906
+ symbol: "ETH",
907
+ decimals: 18
908
+ },
909
+ rpcUrls: {
910
+ default: {
911
+ http: [executionRpcUrl]
912
+ }
913
+ }
914
+ });
915
+ }
916
+ async function findFreePortRange(start, size) {
917
+ for (let candidate = start; candidate < start + 1e3; candidate += size) {
918
+ const ports = Array.from({ length: size }, (_, index) => candidate + index);
919
+ const results = await Promise.all(ports.map((port2) => detectPort2(port2)));
920
+ if (results.every((resolvedPort, index) => resolvedPort === ports[index])) {
921
+ return candidate;
922
+ }
923
+ }
924
+ throw new Error(`Unable to find a free port range starting near ${start}`);
925
+ }
926
+ async function waitForProbe(probe, timeoutMs) {
927
+ const start = Date.now();
928
+ let lastError;
929
+ while (Date.now() - start < timeoutMs) {
930
+ try {
931
+ return await probe();
932
+ } catch (error) {
933
+ lastError = error;
934
+ await delay(PROBE_INTERVAL_MS);
935
+ }
936
+ }
937
+ throw lastError instanceof Error ? lastError : new Error("Timed out waiting for probe");
938
+ }
939
+ async function waitForExecutionReceipt(publicClient, hash) {
940
+ const start = Date.now();
941
+ let lastError;
942
+ while (Date.now() - start < 12e4) {
943
+ try {
944
+ const receipt = await publicClient.getTransactionReceipt({ hash });
945
+ if (receipt) {
946
+ return receipt;
947
+ }
948
+ } catch (error) {
949
+ const errorText = error instanceof Error ? [
950
+ error.message,
951
+ "details" in error && typeof error.details === "string" ? error.details : void 0
952
+ ].filter(Boolean).join(" ") : String(error);
953
+ if (!errorText.includes("indexing is in progress") && !errorText.includes("Transaction receipt with hash") && !errorText.includes("could not be found")) {
954
+ throw error;
955
+ }
956
+ lastError = error instanceof Error ? error : new Error(errorText);
957
+ }
958
+ await delay(500);
959
+ }
960
+ throw lastError ?? new Error(`Timed out waiting for execution receipt ${hash}`);
961
+ }
962
+ async function runCommand(command, args, timeoutMs, allowFailure = false) {
963
+ await new Promise((resolve3, reject) => {
964
+ const child = spawn4(command, args, {
965
+ stdio: ["ignore", "pipe", "pipe"]
966
+ });
967
+ let stdout = "";
968
+ let stderr = "";
969
+ const timeout = setTimeout(() => {
970
+ child.kill("SIGTERM");
971
+ reject(new Error(`Command timed out: ${command} ${args.join(" ")}`));
972
+ }, timeoutMs);
973
+ child.stdout?.setEncoding("utf8");
974
+ child.stderr?.setEncoding("utf8");
975
+ child.stdout?.on("data", (chunk) => {
976
+ stdout += chunk;
977
+ });
978
+ child.stderr?.on("data", (chunk) => {
979
+ stderr += chunk;
980
+ });
981
+ child.on("error", (error) => {
982
+ clearTimeout(timeout);
983
+ reject(error);
984
+ });
985
+ child.on("exit", (code) => {
986
+ clearTimeout(timeout);
987
+ if (code === 0 || allowFailure) {
988
+ resolve3();
989
+ return;
990
+ }
991
+ reject(
992
+ new Error(
993
+ [`Command failed: ${command} ${args.join(" ")}`, stdout.trim(), stderr.trim()].filter(Boolean).join("\n")
994
+ )
995
+ );
996
+ });
997
+ });
998
+ }
999
+ async function delay(ms) {
1000
+ await new Promise((resolve3) => setTimeout(resolve3, ms));
1001
+ }
1002
+
544
1003
  // src/TestNetwork.ts
545
1004
  import * as docker from "docker-compose";
546
- import * as Path5 from "path";
1005
+ import * as Path6 from "path";
547
1006
  async function startNetwork(testName, options) {
548
- const config = Path5.join(__dirname, `dev.docker-compose.yml`);
1007
+ const config = Path6.join(__dirname, `dev.docker-compose.yml`);
549
1008
  const env4 = {
550
1009
  VERSION: "dev",
551
1010
  ARGON_CHAIN: "dev-docker",
@@ -590,7 +1049,7 @@ async function getProxy() {
590
1049
  autoRewrite: true
591
1050
  });
592
1051
  proxy.on("error", () => null);
593
- proxyServer = http.createServer(function(req, res) {
1052
+ proxyServer = http2.createServer(function(req, res) {
594
1053
  const queryData = url.parse(req.url, true).query;
595
1054
  if (!queryData.target) {
596
1055
  res.writeHead(500, { "Content-Type": "text/plain" });
@@ -640,12 +1099,12 @@ function stringifyExt(obj) {
640
1099
  }
641
1100
  function projectRoot() {
642
1101
  if (process4.env.ARGON_PROJECT_ROOT) {
643
- return Path6.join(process4.env.ARGON_PROJECT_ROOT);
1102
+ return Path7.join(process4.env.ARGON_PROJECT_ROOT);
644
1103
  }
645
- return Path6.join(__dirname, `../../..`);
1104
+ return Path7.join(__dirname, `../../..`);
646
1105
  }
647
1106
  async function runTestScript(relativePath) {
648
- const scriptPath = Path6.resolve(projectRoot(), relativePath);
1107
+ const scriptPath = Path7.resolve(projectRoot(), relativePath);
649
1108
  return child_process4.execSync(scriptPath, { encoding: "utf8" }).trim();
650
1109
  }
651
1110
  async function getDockerPortMapping(containerName, port2) {
@@ -695,16 +1154,19 @@ async function activateNotary(sudo2, client, notary) {
695
1154
  export {
696
1155
  SKIP_E2E,
697
1156
  TestBitcoinCli,
1157
+ TestEthereum,
698
1158
  TestMainchain,
699
1159
  TestNotary,
700
1160
  TestOracle,
701
1161
  activateNotary,
702
1162
  addTeardown,
1163
+ argonTokenArtifact,
703
1164
  cleanHostForDocker,
704
1165
  closeOnTeardown,
705
1166
  disconnectOnTeardown,
706
1167
  getDockerPortMapping,
707
1168
  getProxy,
1169
+ mintingGatewayArtifact,
708
1170
  projectRoot,
709
1171
  runOnTeardown,
710
1172
  runTestScript,