Skip to content

Commit

Permalink
style: apply format rules
Browse files Browse the repository at this point in the history
  • Loading branch information
antomor committed Sep 27, 2023
1 parent d0835aa commit aaa1337
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 128 deletions.
2 changes: 1 addition & 1 deletion test/relayserver/RelayServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ describe('RelayServer', function () {
});

const currentBlockNumber = await provider.getBlockNumber();
const receipts = await replenishStrategy(relayServer, workerIndex, 0)
const receipts = await replenishStrategy(relayServer, workerIndex, 0);

expect(receipts).to.be.deep.equal([]);
expect(currentBlockNumber);
Expand Down
176 changes: 93 additions & 83 deletions test/relayserver/TransactionManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,107 @@
import { KeyManager, ServerDependencies, StoredTransaction, TransactionManager, TxStoreManager } from "@rsksmart/rif-relay-server";
import {
KeyManager,
ServerDependencies,
StoredTransaction,
TransactionManager,
TxStoreManager,
} from '@rsksmart/rif-relay-server';
import { expect, use } from 'chai';
import { BigNumber, PopulatedTransaction } from "ethers";
import { BigNumber, PopulatedTransaction } from 'ethers';
import fs from 'fs/promises';
import sinon from "sinon";
import sinon from 'sinon';
import sinonChai from 'sinon-chai';

use(sinonChai);

describe('TransactionManager', function () {
const workdir = '/tmp/env-test';
let txManager: TransactionManager;
let workersKeyManager: KeyManager;
let workerAddress: string;
const workdir = '/tmp/env-test';
let txManager: TransactionManager;
let workersKeyManager: KeyManager;
let workerAddress: string;

beforeEach(async function () {
const managerKeyManager = new KeyManager(1, workdir);
workersKeyManager = new KeyManager(1, workdir);
workerAddress = workersKeyManager.getAddress(0)!;
const txStoreManager = new TxStoreManager({ workdir })
const dependencies: ServerDependencies = {
managerKeyManager,
workersKeyManager,
txStoreManager
};
await txStoreManager.putTx({
txId: 'id1',
attempts: 1,
nonce: 1,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(1),
nonceSigner: {
nonce: 1,
signer: workerAddress
},
from: workerAddress
} as StoredTransaction);
await txStoreManager.putTx({
txId: 'id2',
attempts: 1,
nonce: 2,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(2),
nonceSigner: {
nonce: 2,
signer: workerAddress
},
from: workerAddress
} as StoredTransaction);
beforeEach(async function () {
const managerKeyManager = new KeyManager(1, workdir);
workersKeyManager = new KeyManager(1, workdir);
workerAddress = workersKeyManager.getAddress(0)!;

Check warning on line 25 in test/relayserver/TransactionManager.test.ts

View workflow job for this annotation

GitHub Actions / format_lint_and_test

Forbidden non-null assertion
const txStoreManager = new TxStoreManager({ workdir });
const dependencies: ServerDependencies = {
managerKeyManager,
workersKeyManager,
txStoreManager,
};
await txStoreManager.putTx({
txId: 'id1',
attempts: 1,
nonce: 1,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(1),
nonceSigner: {
nonce: 1,
signer: workerAddress,
},
from: workerAddress,
} as StoredTransaction);
await txStoreManager.putTx({
txId: 'id2',
attempts: 1,
nonce: 2,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(2),
nonceSigner: {
nonce: 2,
signer: workerAddress,
},
from: workerAddress,
} as StoredTransaction);

txManager = new TransactionManager(dependencies);
})
txManager = new TransactionManager(dependencies);
});

it('should boost underpriced pending transactions for a given signer', async function () {
const currentBlockHeight = 100;
sinon.stub(txManager, '_resolveNewGasPrice').returns({
isMaxGasPriceReached: false,
newGasPrice: BigNumber.from(3)
});
const resendTransactionStub = sinon.stub(txManager, "resendTransaction").callsFake(async (
tx: StoredTransaction,
_: number,
newGasPrice: BigNumber,
__: boolean) => {
const {
to,
from,
nonce,
gasLimit,
} = tx
const txToSign: PopulatedTransaction = {
to,
from,
nonce,
gasLimit,
gasPrice: newGasPrice,
};
const signedTransaction = await workersKeyManager.signTransaction(
tx.from,
txToSign
);

return signedTransaction;
})
it('should boost underpriced pending transactions for a given signer', async function () {
const currentBlockHeight = 100;
sinon.stub(txManager, '_resolveNewGasPrice').returns({
isMaxGasPriceReached: false,
newGasPrice: BigNumber.from(3),
});
const resendTransactionStub = sinon
.stub(txManager, 'resendTransaction')
.callsFake(
async (
tx: StoredTransaction,
_: number,
newGasPrice: BigNumber,
__: boolean
) => {
const { to, from, nonce, gasLimit } = tx;
const txToSign: PopulatedTransaction = {
to,
from,
nonce,
gasLimit,
gasPrice: newGasPrice,
};
const signedTransaction = await workersKeyManager.signTransaction(
tx.from,
txToSign
);

const boostedTransactions = await txManager.boostUnderpricedPendingTransactionsForSigner(workerAddress, currentBlockHeight);
return signedTransaction;
}
);

expect(resendTransactionStub).to.have.been.callCount(2);
expect(boostedTransactions.size).to.be.eql(2);
});
const boostedTransactions =
await txManager.boostUnderpricedPendingTransactionsForSigner(
workerAddress,
currentBlockHeight
);

expect(resendTransactionStub).to.have.been.callCount(2);
expect(boostedTransactions.size).to.be.eql(2);
});

afterEach(async function () {
await fs.rm(workdir, { recursive: true, force: true });
})
afterEach(async function () {
await fs.rm(workdir, { recursive: true, force: true });
});
});
96 changes: 52 additions & 44 deletions test/relayserver/TxStoreManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
import { expect } from 'chai';
import { KeyManager, StoredTransaction, TxStoreManager } from "@rsksmart/rif-relay-server";
import { BigNumber } from "ethers";
import {
KeyManager,
StoredTransaction,
TxStoreManager,
} from '@rsksmart/rif-relay-server';
import { BigNumber } from 'ethers';
import fs from 'fs/promises';

describe('TxStoreManager', function () {
const workdir = '/tmp/env-test';
it('should return transactions with gasLimit and gasPrice as BigNumber', async function() {
const workersKeyManager = new KeyManager(1, workdir);
const workerAddress = workersKeyManager.getAddress(0)!;
const txStoreManager = new TxStoreManager({workdir})
await txStoreManager.putTx({
txId: 'id1',
attempts: 1,
nonce: 1,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(1),
nonceSigner: {
nonce: 1,
signer: workerAddress
},
from: workerAddress
} as StoredTransaction);
await txStoreManager.putTx({
txId: 'id2',
attempts: 1,
nonce: 2,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(2),
nonceSigner: {
nonce: 2,
signer: workerAddress
},
from: workerAddress
} as StoredTransaction);
const workdir = '/tmp/env-test';
it('should return transactions with gasLimit and gasPrice as BigNumber', async function () {
const workersKeyManager = new KeyManager(1, workdir);
const workerAddress = workersKeyManager.getAddress(0)!;

Check warning on line 14 in test/relayserver/TxStoreManager.test.ts

View workflow job for this annotation

GitHub Actions / format_lint_and_test

Forbidden non-null assertion
const txStoreManager = new TxStoreManager({ workdir });
await txStoreManager.putTx({
txId: 'id1',
attempts: 1,
nonce: 1,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(1),
nonceSigner: {
nonce: 1,
signer: workerAddress,
},
from: workerAddress,
} as StoredTransaction);
await txStoreManager.putTx({
txId: 'id2',
attempts: 1,
nonce: 2,
creationBlockNumber: 0,
gasLimit: BigNumber.from(1),
gasPrice: BigNumber.from(2),
nonceSigner: {
nonce: 2,
signer: workerAddress,
},
from: workerAddress,
} as StoredTransaction);

const txs = await txStoreManager.getAllBySigner(workerAddress);
const txs = await txStoreManager.getAllBySigner(workerAddress);

const bn = BigNumber.from(1);
const bigNumberProperties = Object.getOwnPropertyNames(bn);
const isBigNumber = (value: any) => bigNumberProperties.every(prop => prop in value);
const txExpectation = ({gasPrice, gasLimit}: StoredTransaction) => isBigNumber(gasPrice) && isBigNumber(gasLimit);
const bn = BigNumber.from(1);
const bigNumberProperties = Object.getOwnPropertyNames(bn);
const isBigNumber = (value: any) =>

Check warning on line 47 in test/relayserver/TxStoreManager.test.ts

View workflow job for this annotation

GitHub Actions / format_lint_and_test

Unexpected any. Specify a different type
bigNumberProperties.every((prop) => prop in value);
const txExpectation = ({ gasPrice, gasLimit }: StoredTransaction) =>
isBigNumber(gasPrice) && isBigNumber(gasLimit);

expect(txs.every(txExpectation), 'the objects returned do not have gasPrice and gasLimit as BigNumber').to.be.true;

});
expect(
txs.every(txExpectation),
'the objects returned do not have gasPrice and gasLimit as BigNumber'
).to.be.true;
});

afterEach(async function() {
await fs.rm(workdir, {recursive: true, force: true});
})
afterEach(async function () {
await fs.rm(workdir, { recursive: true, force: true });
});
});

0 comments on commit aaa1337

Please sign in to comment.