Skip to content

Commit

Permalink
Merge branch '56-add-some-debug-logs' into 'dev'
Browse files Browse the repository at this point in the history
Add some debug logs

Closes #56

See merge request ergo/rosen-bridge/rosen-chains!61
  • Loading branch information
zargarzadehm committed Sep 27, 2023
2 parents 24c5ad7 + 482445c commit b345827
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 36 deletions.
23 changes: 20 additions & 3 deletions packages/abstract-chain/lib/AbstractUtxoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ abstract class AbstractUtxoChain<BoxType> extends AbstractChain {
offset,
GET_BOX_API_LIMIT
);
this.logger.debug(`fetched [${boxes.length}] boxes`);
offset += GET_BOX_API_LIMIT;

// end process if there are no more boxes
Expand All @@ -66,20 +67,29 @@ abstract class AbstractUtxoChain<BoxType> extends AbstractChain {
for (const box of boxes) {
let trackedBox: BoxType | undefined = box;
let boxInfo = this.getBoxInfo(box);
this.logger.debug(`processing box [${boxInfo.id}] for covering`);

// track boxes
let skipBox = false;
while (trackMap.has(boxInfo.id)) {
trackedBox = trackMap.get(boxInfo.id);
if (!trackedBox) {
this.logger.debug(`box [${boxInfo.id}] is tracked to nothing`);
skipBox = true;
break;
}
const previousBoxId = boxInfo.id;
boxInfo = this.getBoxInfo(trackedBox);
this.logger.debug(
`box [${previousBoxId}] is tracked to box [${boxInfo.id}]`
);
}

// if tracked to no box or forbidden box, skip it
if (skipBox || forbiddenBoxIds.includes(boxInfo.id)) continue;
if (skipBox || forbiddenBoxIds.includes(boxInfo.id)) {
this.logger.debug(`box [${boxInfo.id}] is skipped`);
continue;
}

// check and add if box assets are useful to requirements
let isUseful = false;
Expand All @@ -92,6 +102,9 @@ abstract class AbstractUtxoChain<BoxType> extends AbstractChain {
const token = uncoveredTokens[tokenIndex];
if (token.value > boxToken.value) token.value -= boxToken.value;
else uncoveredTokens.splice(tokenIndex, 1);
this.logger.debug(
`box [${boxInfo.id}] is selected due to need of token [${token.id}]`
);
}
});
if (isUseful || uncoveredNativeToken > 0n) {
Expand All @@ -100,10 +113,14 @@ abstract class AbstractUtxoChain<BoxType> extends AbstractChain {
? boxInfo.assets.nativeToken
: uncoveredNativeToken;
result.push(trackedBox!);
}
this.logger.debug(`box [${boxInfo.id}] is selected`);
} else this.logger.debug(`box [${boxInfo.id}] is ignored`);

// end process if requirements are satisfied
if (!isRequirementRemaining()) break;
if (!isRequirementRemaining()) {
this.logger.debug(`requirements satisfied`);
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract-chain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/abstract-chain",
"version": "1.0.0",
"version": "1.0.1",
"description": "this project contains abstract classes to implement any chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
CardanoConfigs,
CardanoUtxo,
} from './types';
import JsonBigInt from '@rosen-bridge/json-bigint';

class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
declare network: AbstractCardanoNetwork;
Expand Down Expand Up @@ -106,13 +107,19 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
unsignedTransactions: PaymentTransaction[],
serializedSignedTransactions: string[]
): Promise<PaymentTransaction> => {
this.logger.debug(
`Generating Cardano transaction for Order: ${JsonBigInt.stringify(order)}`
);
// calculate required assets
const requiredAssets = order
.map((order) => order.assets)
.reduce(ChainUtils.sumAssetBalance, {
nativeToken: this.getMinimumNativeToken() + this.configs.fee,
tokens: [],
});
this.logger.debug(
`Required assets: ${JsonBigInt.stringify(requiredAssets)}`
);

if (!(await this.hasLockAddressEnoughAssets(requiredAssets))) {
const neededADA = requiredAssets.nativeToken.toString();
Expand Down Expand Up @@ -242,6 +249,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
const changeAmount: CardanoWasm.Value =
CardanoWasm.Value.new(changeBoxLovelace);
changeAmount.set_multiasset(changeBoxMultiAsset);
this.logger.debug(`Change box amount: ${changeAmount.to_json()}`);
const changeBox = CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(this.configs.lockAddress),
changeAmount
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano",
"version": "1.0.0",
"version": "1.0.1",
"description": "this project contains cardano chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -24,7 +24,7 @@
"@rosen-bridge/logger-interface": "^0.1.0",
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-chains/abstract-chain": "^1.0.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"bech32": "^2.0.0",
"blake2b": "^2.1.3"
},
Expand Down
17 changes: 17 additions & 0 deletions packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import AbstractErgoNetwork from './network/AbstractErgoNetwork';
import Serializer from './Serializer';
import { ErgoConfigs, GuardsPkConfig } from './types';
import JsonBI from '@rosen-bridge/json-bigint';
import JsonBigInt from '@rosen-bridge/json-bigint';

class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
static feeBoxErgoTree =
Expand Down Expand Up @@ -81,10 +82,16 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
inputs: Array<string>,
dataInputs: Array<string>
): Promise<PaymentTransaction> => {
this.logger.debug(
`Generating Ergo transaction for Order: ${JsonBigInt.stringify(order)}`
);
// calculate required assets
const orderRequiredAssets = order
.map((order) => order.assets)
.reduce(ChainUtils.sumAssetBalance, { nativeToken: 0n, tokens: [] });
this.logger.debug(
`Order required assets: ${JsonBigInt.stringify(orderRequiredAssets)}`
);
const inputAssets = inputs
.map(
(serializedBox) =>
Expand All @@ -93,6 +100,9 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
).assets
)
.reduce(ChainUtils.sumAssetBalance, { nativeToken: 0n, tokens: [] });
this.logger.debug(
`Pre-selected boxes assets: ${JsonBigInt.stringify(inputAssets)}`
);
const requiredAssets = ChainUtils.sumAssetBalance(
ChainUtils.subtractAssetBalance(
orderRequiredAssets,
Expand All @@ -105,6 +115,9 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
tokens: [],
}
);
this.logger.debug(
`Required assets: ${JsonBigInt.stringify(requiredAssets)}`
);

// check if there are enough assets in address
if (!(await this.hasLockAddressEnoughAssets(requiredAssets))) {
Expand Down Expand Up @@ -177,6 +190,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
ErgoUtils.getBoxAssets(box)
);
});
this.logger.debug(`Input assets: ${JsonBigInt.stringify(remainingAssets)}`);

// generate data input boxes objects
const dataInBoxes = dataInputs.map((serializedBox) =>
Expand Down Expand Up @@ -226,6 +240,9 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
this.configs.minBoxValue
);
});
this.logger.debug(
`Remaining assets: ${JsonBigInt.stringify(remainingAssets)}`
);

// create change box
const boxBuilder = new wasm.ErgoBoxCandidateBuilder(
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/ergo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ergo",
"version": "1.0.1",
"version": "1.0.2",
"description": "this project contains ergo chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -23,7 +23,7 @@
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-bridge/minimum-fee": "^0.1.13",
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-chains/abstract-chain": "^1.0.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"blakejs": "^1.2.1",
"ergo-lib-wasm-nodejs": "^0.24.1"
},
Expand Down
70 changes: 66 additions & 4 deletions packages/networks/cardano-koios/lib/CardanoKoiosNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TxInfoItemOutputsItemAssetListItem,
} from '@rosen-clients/cardano-koios';
import { Transaction } from '@emurgo/cardano-serialization-lib-nodejs';
import JsonBigInt from '@rosen-bridge/json-bigint';

class CardanoKoiosNetwork extends AbstractCardanoNetwork {
private client: ReturnType<typeof cardanoKoiosClientFactory>;
Expand All @@ -49,6 +50,9 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
return this.client
.getTip()
.then((block) => {
this.logger.debug(
`requested 'getTip'. res: ${JsonBigInt.stringify(block)}`
);
const height = block[0].block_no;
if (height) return Number(height);
throw new KoiosNullValueError('Height of last block is null');
Expand All @@ -74,6 +78,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
return this.client
.postTxStatus({ _tx_hashes: [transactionId] })
.then((res) => {
this.logger.debug(
`requested 'postTxStatus' for txId [${transactionId}]. res: ${JsonBigInt.stringify(
res
)}`
);
const confirmation = res[0].num_confirmations;
if (confirmation) return Number(confirmation);
return -1;
Expand Down Expand Up @@ -105,6 +114,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
addressInfo = await this.client.postAddressInfo({
_addresses: [address],
});
this.logger.debug(
`requested 'postAddressInfo' for address [${address}]. res: ${JsonBigInt.stringify(
addressInfo
)}`
);
} catch (e: any) {
const baseError = `Failed to get address [${address}] assets from Koios: `;
if (e.response) {
Expand All @@ -127,6 +141,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
addressAssets = await this.client.postAddressAssets({
_addresses: [address],
});
this.logger.debug(
`requested 'postAddressAssets' for address [${address}]. res: ${JsonBigInt.stringify(
addressAssets
)}`
);
} catch (e: any) {
const baseError = `Failed to get address [${address}] assets from Koios: `;
if (e.response) {
Expand Down Expand Up @@ -166,6 +185,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
return this.client
.postBlockTxs({ _block_hashes: [blockId] })
.then((res) => {
this.logger.debug(
`requested 'postBlockTxs' for blockId [${blockId}]. res: ${JsonBigInt.stringify(
res
)}`
);
const txIds = res[0].tx_hashes;
if (!txIds)
throw new KoiosNullValueError(`Block tx hashes list is null`);
Expand All @@ -192,6 +216,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
return this.client
.postBlockInfo({ _block_hashes: [blockId] })
.then((res) => {
this.logger.debug(
`requested 'postBlockInfo' for blockId [${blockId}]. res: ${JsonBigInt.stringify(
res
)}`
);
const block = res[0];
if (!block.block_height || !block.hash || !block.parent_hash)
throw new KoiosNullValueError(`Block info data are null`);
Expand Down Expand Up @@ -225,7 +254,14 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
): Promise<CardanoTx> => {
const koiosTx = await this.client
.postTxInfo({ _tx_hashes: [transactionId] })
.then((res) => res[0])
.then((res) => {
this.logger.debug(
`requested 'postTxInfo' for txId [${transactionId}]. res: ${JsonBigInt.stringify(
res
)}`
);
return res[0];
})
.catch((e) => {
const baseError = `Failed to get transaction [${transactionId}] from Koios: `;
if (e.response) {
Expand Down Expand Up @@ -300,6 +336,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
const boxes = await this.client
.postAddressInfo({ _addresses: [address] })
.then((res) => {
this.logger.debug(
`requested 'postAddressInfo' for address [${address}]. res: ${JsonBigInt.stringify(
res
)}`
);
if (res.length === 0) return [];
const utxos = res[0].utxo_set;
if (!utxos) throw new KoiosNullValueError(`Address UTxO list is null`);
Expand Down Expand Up @@ -327,7 +368,14 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
const [txId, index] = boxId.split('.');
const tx = await this.client
.postTxInfo({ _tx_hashes: [txId] })
.then((res) => (res.length === 0 ? undefined : res[0]))
.then((res) => {
this.logger.debug(
`requested 'postTxUtxos' for txId [${txId}]. res: ${JsonBigInt.stringify(
res
)}`
);
return res.length === 0 ? undefined : res[0];
})
.catch((e) => {
const baseError = `Failed to get transaction [${txId}] UTxOs from Koios: `;
if (e.response) {
Expand Down Expand Up @@ -362,7 +410,11 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
throw new UnexpectedApiError(baseError + e.message);
}
});

this.logger.debug(
`requested 'postCredentialUtxos' for box cred [${boxAddressCred}]. res: ${JsonBigInt.stringify(
utxos
)}`
);
const box = utxos.find(
(utxo) => utxo.tx_hash === txId && utxo.tx_index?.toString() === index
);
Expand All @@ -377,6 +429,9 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
return this.client
.getTip()
.then((block) => {
this.logger.debug(
`requested 'getTip'. res: ${JsonBigInt.stringify(block)}`
);
const slot = block[0].abs_slot;
if (slot) return Number(slot);
throw new KoiosNullValueError('Slot of last block is null');
Expand All @@ -402,7 +457,14 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
const [txId, index] = boxId.split('.');
const tx = await this.client
.postTxInfo({ _tx_hashes: [txId] })
.then((res) => res[0])
.then((res) => {
this.logger.debug(
`requested 'postTxInfo' for txId [${txId}]. res: ${JsonBigInt.stringify(
res
)}`
);
return res[0];
})
.catch((e) => {
const baseError = `Failed to get transaction [${txId}] UTxOs from Koios: `;
if (e.response) {
Expand Down
6 changes: 3 additions & 3 deletions packages/networks/cardano-koios/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano-koios-network",
"version": "1.0.0",
"version": "1.0.1",
"description": "cardano koios network package for rosen cardano chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -25,8 +25,8 @@
"@rosen-bridge/logger-interface": "^0.1.0",
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/tokens": "^0.2.0",
"@rosen-chains/abstract-chain": "^1.0.0",
"@rosen-chains/cardano": "^1.0.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"@rosen-chains/cardano": "^1.0.1",
"@rosen-clients/cardano-koios": "^1.0.3"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit b345827

Please sign in to comment.