Skip to content

Commit

Permalink
Add pegout transaction created event assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanieliov committed Jun 7, 2024
1 parent 36dbb1e commit 13ea2e4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const waitForSync = async (rskTransactionHelpers) => {
* @param {Number} maxAttempts defaults to 80 attempts by block.
* @returns {Promise<Number>} the latest block number the same or greater than `blockNumber`.
*/
const waitForBlock = (rskClient, blockNumber, waitTime = 200, maxAttempts = 80) => {
const waitForBlock = (rskClient, blockNumber, waitTime = 200, maxAttempts = 200) => {
return new Promise((resolve, reject) => {
let attempts = 1;
let latestBlockNumber = -1;
Expand Down
30 changes: 28 additions & 2 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const expect = require('chai').expect
const { ensure0x } = require('../utils');
const { ensure0x, removePrefix0x} = require('../utils');
const whitelistingAssertions = require('../assertions/whitelisting');
const rskUtils = require('../rsk-utils');
const CustomError = require('../CustomError');
Expand All @@ -10,6 +10,8 @@ const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivatio
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { sendTxToBridge, sendPegin, ensurePeginIsRegistered, donateToBridge } = require('../2wp-utils');
const { waitAndUpdateBridge } = require('../rsk-utils');
const {VarInt} = require("../varint");
const {getBridgeState} = require("@rsksmart/bridge-state-data-parser");

const DONATION_AMOUNT = 250;
const REJECTED_REASON = 1;
Expand Down Expand Up @@ -142,7 +144,9 @@ const execute = (description, getRskHost) => {
expect(unlocked, 'Account was not unlocked').to.be.true;

await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE)));


const bridgeStateBeforePegout = await getBridgeState(rskTxHelper.getClient());
const activeFederationUtxosBeforePegout = bridgeStateBeforePegout.activeFederationUtxos;
const pegoutTransaction = await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address);

const isFingerroot500AlreadyActive = await Runners.common.forks.fingerroot500.isAlreadyActive();
Expand Down Expand Up @@ -175,6 +179,28 @@ const execute = (description, getRskHost) => {
expect(batchPegoutCreatedEvent).to.not.be.null;
expect(batchPegoutCreatedEvent.arguments.releaseRskTxHashes.includes(pegoutTransaction.transactionHash)).to.be.true;
}

const isLovell700AlreadyActive = await Runners.common.forks.lovell700.isAlreadyActive();
if (isLovell700AlreadyActive) {
const pegoutTransactionCreatedEvent = await rskUtils.findEventInBlock(localRskTxHelper, 'pegout_transaction_created');
expect(pegoutTransactionCreatedEvent).to.not.be.null;
const encodedUtxoOutpointValues = Buffer.from(removePrefix0x(pegoutTransactionCreatedEvent.arguments.utxoOutpointValues), 'hex');

const federationUtxoValues = activeFederationUtxosBeforePegout.reduce((map, utxo) => {
map[utxo.valueInSatoshis] = Buffer.from(new VarInt(utxo.valueInSatoshis).encode()).toString("hex");
return map;
}, {});

let offset = 0;
let idx = 0;
while (encodedUtxoOutpointValues.length > offset) {
let utxoOutpointValue = new VarInt(encodedUtxoOutpointValues, offset);
expect(utxoOutpointValue.value in federationUtxoValues).to.be.true

offset += utxoOutpointValue.getSizeInBytes();
idx++;
}
}
};

const pegoutConfirmedValidations = async (localRskTxHelper) => {
Expand Down
66 changes: 66 additions & 0 deletions lib/varint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class VarInt {
constructor(value) {
if (typeof value === 'number' || typeof value === 'bigint') {
this.value = BigInt(value);
this.originallyEncodedSize = this.getSizeInBytes();
} else if (Buffer.isBuffer(value)) {
this.value = this.decode(value);
this.originallyEncodedSize = this.getSizeInBytes();
} else {
throw new Error('Invalid input: value should be a number or buffer');
}
}

getOriginalSizeInBytes() {
return this.originallyEncodedSize;
}

getSizeInBytes() {
return VarInt.sizeOf(this.value);
}

static sizeOf(value) {
if (value < 0) return 9;
if (value < 253) return 1;
if (value <= 0xFFFF) return 3;
if (value <= 0xFFFFFFFF) return 5;
return 9;
}

encode() {
let bytes;
switch (this.getSizeInBytes()) {
case 1:
return Buffer.from([Number(this.value)]);
case 3:
return Buffer.from([253, Number(this.value & 0xFF), Number((this.value >> 8) & 0xFF)]);
case 5:
bytes = Buffer.alloc(5);
bytes[0] = 254;
bytes.writeUInt32LE(Number(this.value), 1);
return bytes;
case 9:
bytes = Buffer.alloc(9);
bytes[0] = 255;
bytes.writeBigUInt64LE(this.value, 1);
return bytes;
default:
throw new Error('Invalid size for encoding');
}
}

decode(buffer) {
const first = buffer[0];
if (first < 253) {
return BigInt(first);
} else if (first === 253) {
return BigInt(buffer.readUInt16LE(1));
} else if (first === 254) {
return BigInt(buffer.readUInt32LE(1));
} else {
return buffer.readBigUInt64LE(1);
}
}
}

module.exports = { VarInt };

0 comments on commit 13ea2e4

Please sign in to comment.