Skip to content

Commit

Permalink
Merge branch '117-improve-logs' into 'dev'
Browse files Browse the repository at this point in the history
improve logs

Closes #117

See merge request ergo/rosen-bridge/rosen-chains!140
  • Loading branch information
vorujack committed Sep 12, 2024
2 parents 4ffb5bd + 4c3c2ec commit 6f9669c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 54 deletions.
9 changes: 9 additions & 0 deletions .changeset/afraid-rivers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@rosen-chains/bitcoin-esplora': patch
'@rosen-chains/bitcoin': patch
'@rosen-chains/cardano': patch
'@rosen-chains/ergo': patch
'@rosen-chains/evm': patch
---

improve logs
8 changes: 4 additions & 4 deletions packages/chains/bitcoin/lib/BitcoinChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
);
if (feeDifferencePercent > this.configs.txFeeSlippage) {
this.logger.warn(
`Fee difference is high. Slippage is higher than allowed value [${feeDifferencePercent} > ${this.configs.txFeeSlippage}]. fee: ${fee}, estimated fee: ${estimatedFee}`
`Tx [${transaction.txId}] is not verified: Fee difference is too high. Slippage is higher than allowed value [${feeDifferencePercent} > ${this.configs.txFeeSlippage}]. fee: ${fee}, estimated fee: ${estimatedFee}`
);
return false;
}
Expand Down Expand Up @@ -346,8 +346,8 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
const changeBoxIndex = tx.txOutputs.length - 1;
const changeBox = tx.txOutputs[changeBoxIndex];
if (changeBox.script.toString('hex') !== this.lockScript) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Change box address is wrong`
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Change box address is wrong`
);
return false;
}
Expand Down Expand Up @@ -382,7 +382,7 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
for (let i = 0; i < tx.txInputs.length; i++) {
const boxId = getPsbtTxInputBoxId(tx.txInputs[i]);
if (!(await this.network.isBoxUnspentAndValid(boxId))) {
this.logger.debug(
this.logger.info(
`Tx [${transaction.txId}] is invalid due to spending invalid input box [${boxId}] at index [${i}]`
);
return {
Expand Down
17 changes: 11 additions & 6 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {
// check ttl
const ttl = txBody.ttl();
if (ttl && ttl < (await this.network.currentSlot())) {
this.logger.info(
`Tx [${transaction.txId}] is invalid: ttl [${ttl}] is expired`
);
return {
isValid: false,
details: {
Expand All @@ -508,7 +511,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {
for (let i = 0; i < txBody.inputs().len(); i++) {
const boxId = CardanoUtils.getBoxId(txBody.inputs().get(i));
if (!(await this.network.isBoxUnspentAndValid(boxId))) {
this.logger.debug(
this.logger.info(
`Tx [${transaction.txId}] is invalid due to spending invalid input box [${boxId}] at index [${i}]`
);
return {
Expand Down Expand Up @@ -563,8 +566,8 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {
if (
tx.body().fee().compare(CardanoUtils.bigIntToBigNum(this.configs.fee)) > 0
) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction fee [${tx
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Transaction fee [${tx
.body()
.fee()
.to_str()}] is more than maximum allowed fee [${this.configs.fee.toString()}]`
Expand All @@ -586,16 +589,18 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {

// check metadata
if (tx.auxiliary_data()) {
this.logger.debug(`Tx [${transaction.txId}] invalid: Contains metadata`);
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Contains metadata`
);
return false;
}

// check change box
const changeBoxIndex = tx.body().outputs().len() - 1;
const changeBox = tx.body().outputs().get(changeBoxIndex);
if (changeBox.address().to_bech32() !== this.configs.addresses.lock) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Change box address is wrong`
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Change box address is wrong`
);
return false;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
const box = outputBoxes.get(i);
if (box.ergo_tree().to_base16_bytes() === ErgoChain.feeBoxErgoTree) {
if (BigInt(box.value().as_i64().to_str()) > this.configs.fee) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction fee [${box
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Transaction fee [${box
.value()
.as_i64()
.to_str()}] is more than maximum allowed fee [${
Expand Down Expand Up @@ -545,8 +545,8 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
blockInfo.height - box.creation_height() >
NUMBER_OF_BLOCKS_PER_YEAR
) {
this.logger.info(
`Lock tx [${transaction.id().to_str()}] is not valid, box [${box
this.logger.error(
`Lock tx [${transaction.id().to_str()}] is not verified, box [${box
.box_id()
.to_str()}] creation_height [${box.creation_height()}] is more than a year ago`
);
Expand Down Expand Up @@ -581,10 +581,10 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {

const r4Value = output.register_value(4);
if (r4Value) {
this.logger.debug(
this.logger.warn(
`Tx [${
transaction.txId
}] is invalid. Change box at index [${i}] has value [${r4Value.encode_to_base16()}] in R4`
}] is not verified: Change box at index [${i}] has value [${r4Value.encode_to_base16()}] in R4`
);
return false;
}
Expand Down Expand Up @@ -618,7 +618,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
for (let i = 0; i < inputs.len(); i++) {
const boxId = inputs.get(i).box_id().to_str();
if (!(await this.network.isBoxUnspentAndValid(boxId))) {
this.logger.debug(
this.logger.info(
`Tx [${transaction.txId}] is invalid due to spending invalid input box [${boxId}] at index [${i}]`
);
return {
Expand Down
90 changes: 56 additions & 34 deletions packages/chains/evm/lib/EvmChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Signature, Transaction } from 'ethers';
import Serializer from './Serializer';
import * as EvmUtils from './EvmUtils';
import { AbstractLogger } from '@rosen-bridge/abstract-logger';
import JsonBigInt from '@rosen-bridge/json-bigint';

abstract class EvmChain extends AbstractChain<Transaction> {
declare network: AbstractEvmNetwork;
Expand Down Expand Up @@ -80,8 +81,12 @@ abstract class EvmChain extends AbstractChain<Transaction> {
unsignedTransactions: PaymentTransaction[],
serializedSignedTransactions: string[]
): Promise<PaymentTransaction[]> => {
// split orders
// split orders and aggregate
const orders = EvmUtils.splitPaymentOrders(order);
let orderRequiredAssets: AssetBalance = {
nativeToken: 0n,
tokens: [],
};
orders.forEach((singleOrder) => {
if (singleOrder.assets.tokens.length === 1) {
const assetId = singleOrder.assets.tokens[0].id;
Expand All @@ -90,8 +95,15 @@ abstract class EvmChain extends AbstractChain<Transaction> {
`Asset id [${assetId}] is not supported`
);
}
orderRequiredAssets = ChainUtils.sumAssetBalance(
orderRequiredAssets,
singleOrder.assets
);
}
});
this.logger.debug(
`Order required assets: ${JsonBigInt.stringify(orderRequiredAssets)}`
);

// check the number of parallel transactions won't be exceeded
let nextNonce = await this.network.getAddressNextAvailableNonce(
Expand Down Expand Up @@ -182,9 +194,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
}

// check the balance in the lock address
const requiredAssets: AssetBalance = orders.reduce(
(sum: AssetBalance, order: SinglePayment) =>
ChainUtils.sumAssetBalance(sum, order.assets),
const requiredAssets: AssetBalance = ChainUtils.sumAssetBalance(
orderRequiredAssets,
{
nativeToken: this.tokenMap.wrapAmount(
this.NATIVE_TOKEN_ID,
Expand All @@ -194,6 +205,9 @@ abstract class EvmChain extends AbstractChain<Transaction> {
tokens: [],
}
);
this.logger.debug(
`Required assets: ${JsonBigInt.stringify(requiredAssets)}`
);

if (!(await this.hasLockAddressEnoughAssets(requiredAssets))) {
const neededETH = requiredAssets.nativeToken.toString();
Expand Down Expand Up @@ -351,19 +365,23 @@ abstract class EvmChain extends AbstractChain<Transaction> {
try {
tx = Serializer.deserialize(transaction.txBytes);
} catch (error) {
this.logger.debug(`Tx [${transaction.txId}] invalid: ${error}`);
this.logger.warn(
`Failed to deserialize tx [${transaction.txId}]: ${error}`
);
return false;
}

if (tx.to === null) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: does not have \`to\``
this.logger.warn(
`Tx [${transaction.txId}] is not verified: does not have \`to\``
);
return false;
}

if (tx.type !== 2) {
this.logger.debug(`Tx [${transaction.txId}] invalid: is not of type 2`);
this.logger.warn(
`Tx [${transaction.txId}] is not verified: is not of type 2`
);
return false;
}

Expand All @@ -382,7 +400,7 @@ abstract class EvmChain extends AbstractChain<Transaction> {
// check gas limit
let estimatedRequiredGas = await this.network.getGasRequired(tx);
if (estimatedRequiredGas > this.configs.gasLimitCap) {
this.logger.debug(
this.logger.info(
`Estimated required gas is more than gas limit cap config and cap is used for verification [${estimatedRequiredGas} > ${this.configs.gasLimitCap}]`
);
estimatedRequiredGas = this.configs.gasLimitCap;
Expand All @@ -396,8 +414,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
: gasRequired - tx.gasLimit;

if (gasDifference > gasLimitSlippage) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction gas limit [${tx.gasLimit}] is too far from calculated gas limit [${gasRequired}]`
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Transaction gas limit [${tx.gasLimit}] is too far from calculated gas limit [${gasRequired}]`
);
return false;
}
Expand All @@ -412,8 +430,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
: networkMaxFee - tx.maxFeePerGas;

if (maxFeeDifference > maxFeeSlippage) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction max fee [${tx.maxFeePerGas}] is too far from network's max fee [${networkMaxFee}]`
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Transaction max fee [${tx.maxFeePerGas}] is too far from network's max fee [${networkMaxFee}]`
);
return false;
}
Expand All @@ -427,8 +445,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
: networkMaxPriorityFee - tx.maxPriorityFeePerGas;

if (maxPriorityFeeDifference > priorityFeeSlippage) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction max priority fee [${tx.maxPriorityFeePerGas}] is too far from network's max priority fee [${networkMaxPriorityFee}]`
this.logger.warn(
`Tx [${transaction.txId}] is not verified: Transaction max priority fee [${tx.maxPriorityFeePerGas}] is too far from network's max priority fee [${networkMaxPriorityFee}]`
);
return false;
}
Expand All @@ -452,8 +470,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
try {
trx = Serializer.deserialize(transaction.txBytes);
} catch (error) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: failed to deserialized due to error: ${error}`
this.logger.warn(
`Tx [${transaction.txId}] is invalid: failed to deserialized due to error: ${error}`
);
return {
isValid: false,
Expand All @@ -467,8 +485,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
// check if tx is failed
const txStatus = await this.network.getTransactionStatus(transaction.txId);
if (txStatus === EvmTxStatus.failed) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: tx is failed in blockchain`
this.logger.info(
`Tx [${transaction.txId}] is invalid: tx is failed in blockchain`
);
return {
isValid: false,
Expand All @@ -491,8 +509,8 @@ abstract class EvmChain extends AbstractChain<Transaction> {
details: undefined,
};
}
this.logger.debug(
`Tx [${transaction.txId}] invalid: Transaction's nonce [${trx.nonce}] is not available anymore according to address's current nonce [${nextNonce}]`
this.logger.info(
`Tx [${transaction.txId}] is invalid: Transaction's nonce [${trx.nonce}] is not available anymore according to address's current nonce [${nextNonce}]`
);
return {
isValid: false,
Expand Down Expand Up @@ -564,7 +582,9 @@ abstract class EvmChain extends AbstractChain<Transaction> {
try {
tx = Serializer.deserialize(transaction.txBytes);
} catch (error) {
this.logger.debug(`Tx [${transaction.txId}] invalid: ${error}`);
this.logger.warn(
`Failed to deserialize tx [${transaction.txId}]: ${error}`
);
return;
}

Expand Down Expand Up @@ -705,7 +725,9 @@ abstract class EvmChain extends AbstractChain<Transaction> {
const tx = Serializer.deserialize(transaction.txBytes);

if (tx.to === null) {
this.logger.debug(`Tx [${transaction.txId}] is invalid. \`to\` is null`);
this.logger.warn(
`Tx [${transaction.txId}] is not verified. \`to\` is null`
);
return false;
}

Expand All @@ -717,25 +739,25 @@ abstract class EvmChain extends AbstractChain<Transaction> {

// only type 2 transactions are allowed
if (tx.type !== 2) {
this.logger.debug(
`Tx [${transaction.txId}] is invalid. It is not of type 2`
this.logger.warn(
`Tx [${transaction.txId}] is not verified. It is not of type 2`
);
return false;
}

// tx data must have correct length
if (![eidlen + 2, eidlen + 2 + 136].includes(tx.data.length)) {
this.logger.debug(
`Tx [${transaction.txId}] is invalid. Unexpected \`data\` bytes length [${tx.data.length}]`
this.logger.warn(
`Tx [${transaction.txId}] is not verified. Unexpected \`data\` bytes length [${tx.data.length}]`
);
return false;
}

// eventId must be at the end of `data`
const eventId = tx.data.substring(tx.data.length - eidlen);
if (eventId !== transaction.eventId) {
this.logger.debug(
`Tx [${transaction.txId}] is invalid. Encoded eventId [${eventId}] does not match with the expected one [${transaction.eventId}]`
this.logger.warn(
`Tx [${transaction.txId}] is not verified. Encoded eventId [${eventId}] does not match with the expected one [${transaction.eventId}]`
);
return false;
}
Expand All @@ -745,17 +767,17 @@ abstract class EvmChain extends AbstractChain<Transaction> {
(tx.value === 0n && tx.data.length === eidlen + 2) ||
(tx.value !== 0n && tx.data.length === 136 + eidlen + 2)
) {
this.logger.debug(
`Tx [${transaction.txId}] is invalid. It both transfers native-token and has extra data.`
this.logger.warn(
`Tx [${transaction.txId}] is not verified. It both transfers native-token and has extra data.`
);
return false;
}

// only erc-20 `transfer` is allowed
if (tx.value === 0n) {
if (!EvmUtils.isTransfer(tx.to, tx.data)) {
this.logger.debug(
`Tx [${transaction.txId}] is invalid. \`data\` field [${tx.data}] can not be parsed with 'transfer' ABI.`
this.logger.warn(
`Tx [${transaction.txId}] is not verified. \`data\` field [${tx.data}] can not be parsed with 'transfer' ABI.`
);
return false;
}
Expand All @@ -782,7 +804,7 @@ abstract class EvmChain extends AbstractChain<Transaction> {
);
const txStatus = await this.network.getTransactionStatus(transaction.hash);
if (txStatus !== EvmTxStatus.succeed) {
this.logger.debug(
this.logger.error(
`Lock tx [${transaction.hash}] is not succeed (failed or unexpected status)`
);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ class BitcoinEsploraNetwork extends AbstractBitcoinNetwork {
.get<Array<string>>(`/api/mempool/txids`)
.then((res) => {
this.logger.debug(
`requested 'mempool/txids'. received: ${JsonBigInt.stringify(
res.data
)}`
`requested 'mempool/txids'. received [${res.data.length}] txs`
);
return res.data;
})
Expand Down

0 comments on commit 6f9669c

Please sign in to comment.