Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix created btc tx stored values #239

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 20 additions & 15 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 @@ -603,9 +603,10 @@ describe('Service: PegoutDataProcessor', () => {

const rskBlockHeight = 2869973;
const rskBlockHash = '0xe934eb559aa52270dcad6ca6a890b19ba8605381b90a72f4a19a850a2e79d661';
const batchPegoutRskTxHash = 'testhash';

dbPegoutWaitingForSignature.rskTxHash = rskTxHash;
dbPegoutWaitingForSignature.btcRecipientAddress = 'mpKPLWXnmqjtXyoqi5yRBYgmF4PswMGj55';
dbPegoutWaitingForSignature.btcRecipientAddress = 'mgM4vPBnDKa8cKkXki4Bp5nQ7hgTGd4va8';
dbPegoutWaitingForSignature.createdOn = createdOn;
dbPegoutWaitingForSignature.originatingRskTxHash = originatingRskTxHash;
dbPegoutWaitingForSignature.rskBlockHeight = rskBlockHeight - 200;
Expand All @@ -616,13 +617,17 @@ describe('Service: PegoutDataProcessor', () => {
dbPegoutWaitingForSignature.valueRequestedInSatoshis = 521000;
dbPegoutWaitingForSignature.originatingRskBlockHash = blockHash;
dbPegoutWaitingForSignature.rskBlockHash = rskBlockHash;
dbPegoutWaitingForSignature.batchPegoutRskTxHash = batchPegoutRskTxHash;

mockedPegoutStatusDataService.getAllNotFinishedByBtcRecipientAddress.resolves([dbPegoutWaitingForSignature]);
mockedPegoutStatusDataService.getPegoutByRecipientAndCreationTx
.withArgs(dbPegoutWaitingForSignature.btcRecipientAddress, batchPegoutRskTxHash)
.resolves([dbPegoutWaitingForSignature]);

mockedBridgeService.getBridgeState.resolves(bridgeState);

const relaseBtcEventsArgs = {
btcRawTransaction : btcRawTx3,
releaseRskTxHash: batchPegoutRskTxHash,
};

const bridgeTransaction: Transaction = {
Expand Down Expand Up @@ -657,7 +662,7 @@ describe('Service: PegoutDataProcessor', () => {
const pegoutWithSigned: PegoutStatusDbDataModel = new PegoutStatusDbDataModel();

pegoutWithSigned.rskTxHash = rskTxHash;
pegoutWithSigned.btcRecipientAddress = 'mpKPLWXnmqjtXyoqi5yRBYgmF4PswMGj55';
pegoutWithSigned.btcRecipientAddress = 'mgM4vPBnDKa8cKkXki4Bp5nQ7hgTGd4va8';
pegoutWithSigned.createdOn = createdOn;
pegoutWithSigned.originatingRskTxHash = originatingRskTxHash;
pegoutWithSigned.rskBlockHeight = rskBlockHeight;
Expand All @@ -674,19 +679,19 @@ describe('Service: PegoutDataProcessor', () => {
pegoutWithSigned.btcRawTransaction= '0200000001701d53fb64c827699dffe2885601b41df981a0a0183a9a44782587f27de65c4503000000fd270100000000004d1f01645321025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed62102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21032822626c45fc1c4e3a3def5b4983636d6291a7a6677f66874c337e78bc3b7784210357a2621df0252caa3c4ccb383d6b309c93adbc6708bccfe751bb0cfeb12d34282103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858155ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff028cff0500000000001976a91409197f6153cb3a91bb51eec373360a1cb3b7c0e088ac2caaa9020000000017a914899e3000cbc7bb817e15c9a8e7a4fd6e78a04c488700000000',

expect(thisService.isMethodAccepted(extendedBridgeTx)).equal(true);
sinon.assert.callCount(mockedPegoutStatusDataService.set, 4);
sinon.assert.calledWithMatch(mockedPegoutStatusDataService.set, dbPegoutWaitingForSignature);
});

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(),
getLastByOriginatingRskTxHash: sinon.stub(),
getLastByOriginatingRskTxHashNewest: sinon.stub(),
getAllNotFinishedByBtcRecipientAddress: sinon.stub(),
getPegoutByRecipientAndCreationTx: sinon.stub(),
set: sinon.stub(),
getManyWaitingForConfirmationNewest: sinon.stub(),
getManyWaitingForSignaturesNewest: sinon.stub(),
Expand All @@ -698,7 +703,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 +764,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 +818,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 +842,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 +855,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 +906,7 @@ describe('Service: PegoutDataProcessor', () => {

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

await thisService['processIndividualPegout'](extendedBridgeTx);
sinon.assert.calledTwice(mockedPegoutStatusDataService.set);
})
Expand Down
1 change: 1 addition & 0 deletions src/models/types/bridge-transaction-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export interface ExtendedBridgeEvent extends BridgeEvent{
protocolVersion: string;
senderBtcAddress: string;
releaseRskTxHashes: string;
releaseRskTxHash: string;
};
}
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);
}

}
Loading