Skip to content

Commit

Permalink
Merge branch '116-catch-errors-before-submit-transaction-in-evmchain'…
Browse files Browse the repository at this point in the history
… into 'dev'

Resolve "catch errors before submit transaction in EvmChain"

Closes #116

See merge request ergo/rosen-bridge/rosen-chains!139
  • Loading branch information
vorujack committed Aug 31, 2024
2 parents 4591249 + fdd2474 commit 4ffb5bd
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 33 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions packages/chains/ethereum/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/ethereum

## 0.1.5

### Patch Changes

- Updated dependencies
- @rosen-chains/evm@4.0.1

## 0.1.4

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ethereum",
"version": "0.1.4",
"version": "0.1.5",
"description": "this project contains ethereum chain for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -31,6 +31,6 @@
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/tokens": "^1.2.1",
"@rosen-chains/abstract-chain": "^9.0.1",
"@rosen-chains/evm": "^4.0.0"
"@rosen-chains/evm": "^4.0.1"
}
}
6 changes: 6 additions & 0 deletions packages/chains/evm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rosen-chains/evm

## 4.0.1

### Patch Changes

- catch errors while checking tx before submission

## 4.0.0

### Major Changes
Expand Down
56 changes: 33 additions & 23 deletions packages/chains/evm/lib/EvmChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,33 +568,43 @@ abstract class EvmChain extends AbstractChain<Transaction> {
return;
}

// check type
if (tx.type !== 2) {
this.logger.warn(
`Cannot submit transaction [${transaction.txId}]: Transaction is not of type 2`
);
return;
}
try {
// check type
if (tx.type !== 2) {
this.logger.warn(
`Cannot submit transaction [${transaction.txId}]: Transaction is not of type 2`
);
return;
}

// check fees
const gasRequired = await this.network.getGasRequired(tx);
if (gasRequired > tx.gasLimit) {
this.logger.warn(
`Cannot submit transaction [${transaction.txId}]: Transaction gas limit [${tx.maxFeePerGas}] is less than the required gas [${gasRequired}]`
);
return;
}
// check fees
const gasRequired = await this.network.getGasRequired(tx);
if (gasRequired > tx.gasLimit) {
this.logger.warn(
`Cannot submit transaction [${transaction.txId}]: Transaction gas limit [${tx.maxFeePerGas}] is less than the required gas [${gasRequired}]`
);
return;
}

// check lock still has enough assets
const txAssets = await this.getTransactionAssets(transaction);
if (!(await this.hasLockAddressEnoughAssets(txAssets.inputAssets))) {
// check lock still has enough assets
const txAssets = await this.getTransactionAssets(transaction);
if (!(await this.hasLockAddressEnoughAssets(txAssets.inputAssets))) {
this.logger.warn(
`Cannot submit transaction [${
transaction.txId
}]: Locked assets cannot cover transaction assets: ${JSONBigInt.stringify(
txAssets.inputAssets
)}`
);
return;
}
} catch (e) {
this.logger.warn(
`Cannot submit transaction [${
transaction.txId
}]: Locked assets cannot cover transaction assets: ${JSONBigInt.stringify(
txAssets.inputAssets
)}`
`An error occurred while checking transaction [${transaction.txId}] for submission: ${e}`
);
if (e instanceof Error && e.stack) {
this.logger.warn(e.stack);
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/chains/evm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm",
"version": "4.0.0",
"version": "4.0.1",
"description": "this project contains evm chains for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down
40 changes: 40 additions & 0 deletions packages/chains/evm/tests/EvmChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,46 @@ describe('EvmChain', () => {
await evmChain.submitTransaction(paymentTx);
expect(submitTransactionSpy).not.toHaveBeenCalled();
});

/**
* @target EvmChain.submitTransaction should not submit the transaction
* when checking failed due to error
* @dependencies
* @scenario
* - mock invalid PaymentTransaction
* - mock getGasRequired to throw error
* - run test
* - check function is not called
* @expected
* - it should not call the function
*/
it('should not submit the transaction when checking failed due to error', async () => {
// mock getGasRequired
vi.spyOn(network, 'getGasRequired').mockImplementation(() => {
throw Error(`test Error`);
});

// mock PaymentTransaction
const tx = Transaction.from(TestData.transaction1Json);
tx.gasLimit = 55000n;
tx.maxFeePerGas = 21n;
tx.maxPriorityFeePerGas = 6n;
tx.value = 2n;

const eventId = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
const txType = TransactionType.payment;
const paymentTx = new PaymentTransaction(
evmChain.CHAIN,
tx.unsignedHash,
eventId,
Serializer.serialize(tx),
txType
);
const submitTransactionSpy = vi.spyOn(network, 'submitTransaction');
submitTransactionSpy.mockImplementation(async () => undefined);
await evmChain.submitTransaction(paymentTx);
expect(submitTransactionSpy).not.toHaveBeenCalled();
});
});

describe('verifyTransactionExtraConditions', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/networks/evm-rpc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @rosen-chains/evm-rpc

## 2.1.2

### Patch Changes

- improve log in `getGasRequired` function
- Updated dependencies
- @rosen-chains/evm@4.0.1

## 2.1.1

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/networks/evm-rpc/lib/EvmRpcNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ class EvmRpcNetwork extends AbstractEvmNetwork {
return gas;
} catch (e: unknown) {
const baseError = `Failed to get required Gas from ${this.chain} RPC: `;
if (isCallException(e))
this.logger.debug(
`Gas estimation failed on chain [${
this.chain
}] due to CALL_EXCEPTION: ${JsonBigInt.stringify(e)}`
);
throw new UnexpectedApiError(baseError + `${e}`);
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/evm-rpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm-rpc",
"version": "2.1.1",
"version": "2.1.2",
"description": "A package to be used as network api provider for @rosen-chains/evm package",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/evm-address-tx-extractor": "^1.0.3",
"@rosen-chains/abstract-chain": "^9.0.1",
"@rosen-chains/evm": "^4.0.0",
"@rosen-chains/evm": "^4.0.1",
"typeorm": "^0.3.20"
}
}

0 comments on commit 4ffb5bd

Please sign in to comment.