Skip to content

Commit

Permalink
chore: Improved E2E Test Structure
Browse files Browse the repository at this point in the history
Draft: Improved E2E Test

implemented improved e2e test structure
  • Loading branch information
aruokhai committed Dec 4, 2024
1 parent 7a1aa8e commit 4c798eb
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 296 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/actions-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ jobs:
with:
node-version: 20.12.x
cache: npm
- name: Start test containers
run: docker compose -f "./e2e/helpers/docker/docker-compose.yml" up -d
- name: Cache node modules
id: cache-node-modules
uses: actions/cache@v4
Expand All @@ -54,20 +52,5 @@ jobs:
- name: Install Dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm install
- name: Start indexer
run: npm run start:e2e > indexer.log 2>&1 &
- name: Wait for indexer to become available
run: |
for i in {1..24}; do
curl --fail -X GET http://localhost:3000/health && break || sleep 2
done
- name: Run e2e tests
run: npm run test:e2e
- name: Fetch bitcoin core logs
if: always()
run: docker compose -f "./e2e/helpers/docker/docker-compose.yml" logs bitcoin
- name: Fetch indexer logs
if: always()
run: cat indexer.log
- name: Stop test containers
run: docker compose -f "./e2e/helpers/docker/docker-compose.yml" down
- name: Run E2E Test
run: npm run test:e2e
55 changes: 0 additions & 55 deletions e2e/Makefile

This file was deleted.

39 changes: 0 additions & 39 deletions e2e/README.md

This file was deleted.

17 changes: 0 additions & 17 deletions e2e/heath-check.e2e-spec.ts

This file was deleted.

77 changes: 28 additions & 49 deletions e2e/helpers/common.helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Payment, Transaction } from 'bitcoinjs-lib';
import { IndexerService } from '@/indexer/indexer.service';
import {
Transaction as TransactionEntity,
TransactionOutput,
} from '@/transactions/transaction.entity';
import { SATS_PER_BTC } from '@/common/constants';
import * as currency from 'currency.js';
import { varIntSize } from '@/common/common';

export function generateScanTweak(
export function generateScanTweakAndOutputEntity(
transaction: Transaction,
outputs: Payment[],
indexerService: IndexerService,
): string {
): [string, TransactionOutput[]] {
const txins = transaction.ins.map((input, index) => {
const isWitness = transaction.hasWitnesses();

Expand All @@ -27,9 +29,29 @@ export function generateScanTweak(
value: output.value,
}));

const [scanTweak] = indexerService.computeScanTweak(txins, txouts);
const [scanTweak, outputEntity] = new IndexerService().computeScanTweak(
txins,
txouts,
);

return scanTweak.toString('hex');
return [scanTweak.toString('hex'), outputEntity];
}

export function transactionToEntity(
transaction: Transaction,
txid: string,
blockHash: string,
blockHeight: number,
outputs: Payment[],
): TransactionEntity {
const entityTransaction = new TransactionEntity();
entityTransaction.blockHash = blockHash;
entityTransaction.blockHeight = blockHeight;
entityTransaction.isSpent = false;
[entityTransaction.scanTweak, entityTransaction.outputs] =
generateScanTweakAndOutputEntity(transaction, outputs);
entityTransaction.id = txid;
return entityTransaction;
}

export const readVarInt = (data: Buffer, cursor: number): number => {
Expand All @@ -50,49 +72,6 @@ export interface SilentBlockTransaction {
scanTweak: string;
}

export interface SilentBlock {
type: number;
transactions: SilentBlockTransaction[];
}

export const parseSilentBlock = (data: Buffer): SilentBlock => {
const type = data.readUInt8(0);
const transactions: SilentBlockTransaction[] = [];

let cursor = 1;
const count = readVarInt(data, cursor);
cursor += varIntSize(count);

for (let i = 0; i < count; i++) {
const txid = data.subarray(cursor, cursor + 32).toString('hex');
cursor += 32;

const outputs = [];
const outputCount = readVarInt(data, cursor);
cursor += varIntSize(outputCount);

for (let j = 0; j < outputCount; j++) {
const value = Number(data.readBigUInt64BE(cursor));
cursor += 8;

const pubkey = data.subarray(cursor, cursor + 32).toString('hex');
cursor += 32;

const vout = data.readUint32BE(cursor);
cursor += 4;

outputs.push({ value, pubkey, vout });
}

const scanTweak = data.subarray(cursor, cursor + 33).toString('hex');
cursor += 33;

transactions.push({ txid, outputs, scanTweak });
}

return { type, transactions };
};

export const btcToSats = (amount: number): number => {
return currency(amount, { precision: 8 }).multiply(SATS_PER_BTC).value;
};
22 changes: 0 additions & 22 deletions e2e/helpers/docker/docker-compose.yml

This file was deleted.

40 changes: 40 additions & 0 deletions e2e/helpers/rpc.helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import axios, { AxiosRequestConfig } from 'axios';
import { setTimeout } from 'timers/promises';

type PartialUtxo = {
value: number;
Expand Down Expand Up @@ -32,6 +33,18 @@ export class BitcoinRPCUtil {
};
}

public async waitForBitcoind(): Promise<void> {
for (let i = 0; i < 10; i++) {
try {
await this.getBlockchainInfo();
return;
} catch (error) {
await setTimeout(2000);
}
}
throw new Error('Bitcoind refused to start');
}

public async request(config: AxiosRequestConfig): Promise<any> {
try {
const response = await axios.request({
Expand Down Expand Up @@ -60,6 +73,15 @@ export class BitcoinRPCUtil {
});
}

loadWallet(walletName: string): Promise<any> {
return this.request({
data: {
method: 'createwallet',
params: [walletName],
},
});
}

getNewAddress(): Promise<string> {
return this.request({
data: {
Expand All @@ -69,6 +91,24 @@ export class BitcoinRPCUtil {
});
}

getBlockCount(): Promise<number> {
return this.request({
data: {
method: 'getblockcount',
params: [],
},
});
}

getBlockchainInfo(): Promise<object> {
return this.request({
data: {
method: 'getblockchaininfo',
params: [],
},
});
}

mineToAddress(numBlocks: number, address: string): Promise<any> {
return this.request({
data: {
Expand Down
Loading

0 comments on commit 4c798eb

Please sign in to comment.