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 9, 2024
1 parent 7a1aa8e commit 5c87696
Show file tree
Hide file tree
Showing 15 changed files with 535 additions and 294 deletions.
21 changes: 5 additions & 16 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,11 @@ 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
- name: Run E2E Test
run: npm run test:e2e
- name: Fetch bitcoin core logs
- name: Fetch Indexer logs
if: always()
run: docker compose -f "./e2e/helpers/docker/docker-compose.yml" logs bitcoin
- name: Fetch indexer logs
run: cat e2e/.logs/indexer.log
- name: Fetch Bitcoind logs
if: always()
run: cat indexer.log
- name: Stop test containers
run: docker compose -f "./e2e/helpers/docker/docker-compose.yml" down
run: cat e2e/.logs/bitcoind.log
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.

54 changes: 54 additions & 0 deletions e2e/file-logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { LoggerService } from '@nestjs/common';
import { createWriteStream, existsSync, mkdirSync, WriteStream } from 'fs';

export class FileLogger implements LoggerService {
private logFolderPath = `${__dirname}/.logs`;
private logFilePath: string;
private logFileStream: WriteStream;

constructor(logFile: string) {
if (!existsSync(this.logFolderPath)) {
mkdirSync(this.logFolderPath, { recursive: true });
}

this.logFilePath = `${this.logFolderPath}/${logFile}.log`;
this.logFileStream = createWriteStream(this.logFilePath, {
flags: 'a',
});
}

private writeLog(level: string, message: string, trace?: string) {
const timestamp = new Date().toISOString();
let logEntry = `[${timestamp}] [${level}] ${message}\n`;

if (trace) {
logEntry += `Trace: ${trace}\n`;
}

this.logFileStream.write(logEntry);
}

log(message: string) {
this.writeLog('LOG', message);
}

error(message: string, trace: string) {
this.writeLog('ERROR', message, trace);
}

warn(message: string) {
this.writeLog('WARN', message);
}

debug(message: string) {
this.writeLog('DEBUG', message);
}

verbose(message: string) {
this.writeLog('VERBOSE', message);
}

getWriteStream() {
return this.logFileStream;
}
}
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.

Loading

0 comments on commit 5c87696

Please sign in to comment.