Skip to content

Commit

Permalink
Fix created btc tx stored values
Browse files Browse the repository at this point in the history
		I've updated the pegout data processor in the batch_pegout_created event process
		using the bridge state in the specific block that the event was emmited.
  • Loading branch information
ronaldsg20 committed Jul 19, 2023
1 parent fe37736 commit 9b80f0b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
39 changes: 29 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"@loopback/rest": "^9.1.1",
"@loopback/rest-explorer": "^5.0.9",
"@loopback/service-proxy": "^3.0.5",
"@rsksmart/bridge-state-data-parser": "git+https://github.com/rsksmart/bridge-state-data-parser.git#allow-block-argument",
"@rsksmart/rsk-precompiled-abis": "git+https://github.com/rsksmart/precompiled-abis.git#5.0.0-FINGERROOT",
"@types/mongoose": "^5.11.97",
"big.js": "^6.1.1",
"bitcoinjs-lib": "^6.0.1",
"bridge-state-data-parser": "https://github.com/rsksmart/bridge-state-data-parser#v1.1.0",
"bridge-transaction-parser": "git+https://github.com/rsksmart/bridge-transaction-parser.git#v0.5.0-beta",
"dotenv": "^8.6.0",
"jssha": "^3.2.0",
Expand Down
22 changes: 11 additions & 11 deletions src/__tests__/unit/services/pegout-data.processor.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {bridge} from '@rsksmart/rsk-precompiled-abis';
import { PegoutStatus, PegoutStatusDbDataModel } from '../../../models/rsk/pegout-status-data-model';
import { BridgeService } from '../../../services';
import * as constants from '../../../constants';
import { BridgeState } from 'bridge-state-data-parser';
import { BridgeState } from '@rsksmart/bridge-state-data-parser';
import { ExtendedBridgeEvent } from '../../../models/types/bridge-transaction-parser';
import { remove0x, ensure0x } from '../../../utils/hex-utils';

Expand Down Expand Up @@ -680,7 +680,7 @@ describe('Service: PegoutDataProcessor', () => {

it('handles BATCH_PEGOUT_CREATED status', async () => {
sandbox.stub(process.env, 'NETWORK').value(constants.NETWORK_MAINNET);
const mockedPegoutStatusDataService:PegoutStatusDataService =
const mockedPegoutStatusDataService:PegoutStatusDataService =
{
deleteByRskBlockHeight: sinon.stub(),
getManyByOriginatingRskTxHash: sinon.stub(),
Expand All @@ -698,7 +698,7 @@ describe('Service: PegoutDataProcessor', () => {
start: sinon.stub(),
stop: sinon.stub()
};

const mockedBridgeService = sinon.createStubInstance(BridgeService) as SinonStubbedInstance<BridgeService> & BridgeService;
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const createdOn = new Date();
Expand Down Expand Up @@ -759,7 +759,7 @@ describe('Service: PegoutDataProcessor', () => {

let allPegoutTxs = remove0x(batchPegoutsEvent!.arguments.releaseRskTxHashes);
let totalHashes = allPegoutTxs.length / 64;

expect(totalHashes).equal(9);

let eventData = '';
Expand Down Expand Up @@ -813,19 +813,19 @@ describe('Service: PegoutDataProcessor', () => {
it('returns same valueInSatoshisToBeReceived when did not find pegout in pegoutsWaitingForConfirmations', async () => {
const mockedPegoutStatusDataService = sinon.createStubInstance(PegoutStatusMongoDbDataService) as SinonStubbedInstance<PegoutStatusDataService>;
const mockedBridgeService = sinon.createStubInstance(BridgeService) as SinonStubbedInstance<BridgeService> & BridgeService;
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const mockedPegoutStatus = new PegoutStatusDbDataModel();
mockedPegoutStatus.originatingRskTxHash = rskTxHash;
mockedPegoutStatus.valueInSatoshisToBeReceived = 1000;
mockedBridgeService.getBridgeState.resolves(bridgeState);
await thisService['addValueInSatoshisToBeReceivedAndFee'](mockedPegoutStatus);
expect(mockedPegoutStatus.valueInSatoshisToBeReceived).equal(1000);
})

it('returns same valueInSatoshisToBeReceived when found a pegout but did not find an output containing the btcRecipientAddress', async () => {
const mockedPegoutStatusDataService = sinon.createStubInstance(PegoutStatusMongoDbDataService) as SinonStubbedInstance<PegoutStatusDataService>;
const mockedBridgeService = sinon.createStubInstance(BridgeService) as SinonStubbedInstance<BridgeService> & BridgeService;
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const mockedPegoutStatus = new PegoutStatusDbDataModel();
mockedPegoutStatus.originatingRskTxHash = '0x5628682b56ef179e066fd12ee25a84436def371b0a11b45cf1d8308ed06f4698';
mockedPegoutStatus.valueInSatoshisToBeReceived = 1000;
Expand All @@ -837,10 +837,10 @@ describe('Service: PegoutDataProcessor', () => {
it('returns calculated valueInSatoshisToBeReceived when found pegout in pegoutsWaitingForConfirmations', async () => {
const mockedPegoutStatusDataService = sinon.createStubInstance(PegoutStatusMongoDbDataService) as SinonStubbedInstance<PegoutStatusDataService>;
const mockedBridgeService = sinon.createStubInstance(BridgeService) as SinonStubbedInstance<BridgeService> & BridgeService;
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const mockedPegoutStatus = new PegoutStatusDbDataModel();
mockedPegoutStatus.originatingRskTxHash = '0x5628682b56ef179e066fd12ee25a84436def371b0a11b45cf1d8308ed06f4698';
mockedPegoutStatus.btcRawTransaction = btcRawTx2;
mockedPegoutStatus.btcRawTransaction = btcRawTx2;
mockedPegoutStatus.btcRecipientAddress = 'mpKPLWXnmqjtXyoqi5yRBYgmF4PswMGj55'; // output address with value 337100 from raw tx
mockedBridgeService.getBridgeState.resolves(bridgeState);
await thisService['addValueInSatoshisToBeReceivedAndFee'](mockedPegoutStatus);
Expand All @@ -850,7 +850,7 @@ describe('Service: PegoutDataProcessor', () => {
it('processIndividualPegout', async () => {
const mockedPegoutStatusDataService = sinon.createStubInstance(PegoutStatusMongoDbDataService) as SinonStubbedInstance<PegoutStatusDataService>;
const mockedBridgeService = sinon.createStubInstance(BridgeService) as SinonStubbedInstance<BridgeService> & BridgeService;
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);
const thisService = new PegoutDataProcessor(mockedPegoutStatusDataService, mockedBridgeService);

const extendedBridgeTx: ExtendedBridgeTx = {
txHash: "0x6843cfeaafe38e1044ec5638877ff766015b44887d32c7aef7daec84aa3af7c5",
Expand Down Expand Up @@ -901,7 +901,7 @@ describe('Service: PegoutDataProcessor', () => {

expect(hasEvent).true;
expect(releaseRequestedEvent).not.null;

await thisService['processIndividualPegout'](extendedBridgeTx);
sinon.assert.calledTwice(mockedPegoutStatusDataService.set);
})
Expand Down
6 changes: 3 additions & 3 deletions src/services/bridge.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {getLogger, Logger} from 'log4js';
import Web3 from 'web3';
import {Contract} from 'web3-eth-contract';
import bridgeTransactionParser, {Transaction} from 'bridge-transaction-parser';
import { getBridgeState, BridgeState} from 'bridge-state-data-parser';
import { getBridgeState, BridgeState } from '@rsksmart/bridge-state-data-parser';

export class BridgeService {
private bridgeContract: Contract;
Expand Down Expand Up @@ -112,8 +112,8 @@ export class BridgeService {
return await bridgeTransactionParser.getBridgeTransactionByTxHash(this.web3, txHash);
}

public async getBridgeState(): Promise<BridgeState> {
return await getBridgeState(this.web3);
public async getBridgeState(defaultBlock: string | number = 'latest'): Promise<BridgeState> {
return await getBridgeState(this.web3, defaultBlock);
}

}
21 changes: 12 additions & 9 deletions src/services/pegout-data.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class PegoutDataProcessor implements FilteredBridgeTransactionProcessor {

this.logger.trace(`[processSignedStatusByRtx] Got a pegout waiting signatures.`);
this.logPegoutData(oldPegoutStatus);

const newPegoutStatus = PegoutStatusDbDataModel.clonePegoutStatusInstance(oldPegoutStatus);
newPegoutStatus.setRskTxInformation(extendedBridgeTx);
newPegoutStatus.btcRawTransaction = rawTx;
Expand All @@ -168,7 +168,7 @@ export class PegoutDataProcessor implements FilteredBridgeTransactionProcessor {
newPegoutStatus.feeInSatoshisToBePaid = newPegoutStatus.valueRequestedInSatoshis - newPegoutStatus.valueInSatoshisToBeReceived;
newPegoutStatus.btcRawTxInputsHash = this.getInputsHash(parsedBtcTransaction);
newPegoutStatus.rskTxHash = `${extendedBridgeTx.txHash}___${index}`;

this.logPegoutData(newPegoutStatus);
this.logger.trace(`[processSignedStatusByRtx] PegOut being released
with amount in weis: ${(await this.getTxFromRskTransaction(originatingRskTxHash)).valueInWeis}`);
Expand Down Expand Up @@ -233,8 +233,8 @@ export class PegoutDataProcessor implements FilteredBridgeTransactionProcessor {
this.logPegoutData(newClonedPegoutStatus);
this.logger.trace(`[processBatchPegouts] PegOut waiting for confirmations
with amount in weis: ${(await this.getTxFromRskTransaction(originatingRskTxHash)).valueInWeis}`);
await this.addBatchValueInSatoshisToBeReceivedAndFee(newClonedPegoutStatus, extendedBridgeTx.txHash);

await this.addBatchValueInSatoshisToBeReceivedAndFee(newClonedPegoutStatus, extendedBridgeTx.txHash, extendedBridgeTx.blockNumber);

try {
// Update previous status as outdated
Expand All @@ -251,16 +251,19 @@ export class PegoutDataProcessor implements FilteredBridgeTransactionProcessor {

}

private async addBatchValueInSatoshisToBeReceivedAndFee(pegoutStatus: PegoutStatusDbDataModel, rskTxHash: string): Promise<void> {
private async addBatchValueInSatoshisToBeReceivedAndFee(
pegoutStatus: PegoutStatusDbDataModel,
txHash: string, blockNumber: number,
): Promise<void> {
try {
const bridgeState = await this.bridgeService.getBridgeState();
const batchedPegout = bridgeState.pegoutsWaitingForConfirmations.find(pegout => pegout.rskTxHash === rskTxHash);
const bridgeState = await this.bridgeService.getBridgeState(blockNumber);
const batchedPegout = bridgeState.pegoutsWaitingForConfirmations.find(pegout => pegout.rskTxHash === remove0x(txHash));

if(!batchedPegout) {
this.logger.debug(`[addValueInSatoshisToBeReceivedAndFee] did not find the batched pegout in the bridge state pegoutsWaitingForConfirmations. [rsktxhash: ${rskTxHash}][originatingRskTxHash:${pegoutStatus.originatingRskTxHash}]`);
this.logger.debug(`[addValueInSatoshisToBeReceivedAndFee] did not find the batched pegout in the bridge state pegoutsWaitingForConfirmations. [rsktxhash: ${txHash}][originatingRskTxHash:${pegoutStatus.originatingRskTxHash}]`);
return;
}
this.logger.debug(`[addValueInSatoshisToBeReceivedAndFee] Got the batched pegout in the bridge state pegoutsWaitingForConfirmations. [rsktxhash: ${rskTxHash}][originatingRskTxHash:${pegoutStatus.originatingRskTxHash}]`);
this.logger.debug(`[addValueInSatoshisToBeReceivedAndFee] Got the batched pegout in the bridge state pegoutsWaitingForConfirmations. [rsktxhash: ${txHash}][originatingRskTxHash:${pegoutStatus.originatingRskTxHash}]`);

const parsedBtcTransaction = bitcoin.Transaction.fromHex(batchedPegout.btcRawTx);

Expand Down

0 comments on commit 9b80f0b

Please sign in to comment.